xref: /dflybsd-src/contrib/gdb-7/gdb/macroexp.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* C preprocessor macro expansion for GDB.
2*c50c785cSJohn Marino    Copyright (C) 2002, 2007, 2008, 2009, 2010, 2011
3*c50c785cSJohn Marino    Free Software Foundation, Inc.
45796c8dcSSimon Schubert    Contributed by Red Hat, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "gdb_obstack.h"
235796c8dcSSimon Schubert #include "bcache.h"
245796c8dcSSimon Schubert #include "macrotab.h"
255796c8dcSSimon Schubert #include "macroexp.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert #include "c-lang.h"
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert /* A resizeable, substringable string type.  */
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert /* A string type that we can resize, quickly append to, and use to
355796c8dcSSimon Schubert    refer to substrings of other strings.  */
365796c8dcSSimon Schubert struct macro_buffer
375796c8dcSSimon Schubert {
385796c8dcSSimon Schubert   /* An array of characters.  The first LEN bytes are the real text,
395796c8dcSSimon Schubert      but there are SIZE bytes allocated to the array.  If SIZE is
405796c8dcSSimon Schubert      zero, then this doesn't point to a malloc'ed block.  If SHARED is
415796c8dcSSimon Schubert      non-zero, then this buffer is actually a pointer into some larger
425796c8dcSSimon Schubert      string, and we shouldn't append characters to it, etc.  Because
435796c8dcSSimon Schubert      of sharing, we can't assume in general that the text is
445796c8dcSSimon Schubert      null-terminated.  */
455796c8dcSSimon Schubert   char *text;
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert   /* The number of characters in the string.  */
485796c8dcSSimon Schubert   int len;
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert   /* The number of characters allocated to the string.  If SHARED is
515796c8dcSSimon Schubert      non-zero, this is meaningless; in this case, we set it to zero so
525796c8dcSSimon Schubert      that any "do we have room to append something?" tests will fail,
535796c8dcSSimon Schubert      so we don't always have to check SHARED before using this field.  */
545796c8dcSSimon Schubert   int size;
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert   /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
575796c8dcSSimon Schubert      block).  Non-zero if TEXT is actually pointing into the middle of
585796c8dcSSimon Schubert      some other block, and we shouldn't reallocate it.  */
595796c8dcSSimon Schubert   int shared;
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert   /* For detecting token splicing.
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert      This is the index in TEXT of the first character of the token
645796c8dcSSimon Schubert      that abuts the end of TEXT.  If TEXT contains no tokens, then we
655796c8dcSSimon Schubert      set this equal to LEN.  If TEXT ends in whitespace, then there is
665796c8dcSSimon Schubert      no token abutting the end of TEXT (it's just whitespace), and
675796c8dcSSimon Schubert      again, we set this equal to LEN.  We set this to -1 if we don't
685796c8dcSSimon Schubert      know the nature of TEXT.  */
695796c8dcSSimon Schubert   int last_token;
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert   /* If this buffer is holding the result from get_token, then this
725796c8dcSSimon Schubert      is non-zero if it is an identifier token, zero otherwise.  */
735796c8dcSSimon Schubert   int is_identifier;
745796c8dcSSimon Schubert };
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* Set the macro buffer *B to the empty string, guessing that its
785796c8dcSSimon Schubert    final contents will fit in N bytes.  (It'll get resized if it
795796c8dcSSimon Schubert    doesn't, so the guess doesn't have to be right.)  Allocate the
805796c8dcSSimon Schubert    initial storage with xmalloc.  */
815796c8dcSSimon Schubert static void
825796c8dcSSimon Schubert init_buffer (struct macro_buffer *b, int n)
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert   b->size = n;
855796c8dcSSimon Schubert   if (n > 0)
865796c8dcSSimon Schubert     b->text = (char *) xmalloc (n);
875796c8dcSSimon Schubert   else
885796c8dcSSimon Schubert     b->text = NULL;
895796c8dcSSimon Schubert   b->len = 0;
905796c8dcSSimon Schubert   b->shared = 0;
915796c8dcSSimon Schubert   b->last_token = -1;
925796c8dcSSimon Schubert }
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert /* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
965796c8dcSSimon Schubert    shared substring.  */
975796c8dcSSimon Schubert static void
985796c8dcSSimon Schubert init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
995796c8dcSSimon Schubert {
1005796c8dcSSimon Schubert   buf->text = addr;
1015796c8dcSSimon Schubert   buf->len = len;
1025796c8dcSSimon Schubert   buf->shared = 1;
1035796c8dcSSimon Schubert   buf->size = 0;
1045796c8dcSSimon Schubert   buf->last_token = -1;
1055796c8dcSSimon Schubert }
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert /* Free the text of the buffer B.  Raise an error if B is shared.  */
1095796c8dcSSimon Schubert static void
1105796c8dcSSimon Schubert free_buffer (struct macro_buffer *b)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   gdb_assert (! b->shared);
1135796c8dcSSimon Schubert   if (b->size)
1145796c8dcSSimon Schubert     xfree (b->text);
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert /* A cleanup function for macro buffers.  */
1195796c8dcSSimon Schubert static void
1205796c8dcSSimon Schubert cleanup_macro_buffer (void *untyped_buf)
1215796c8dcSSimon Schubert {
1225796c8dcSSimon Schubert   free_buffer ((struct macro_buffer *) untyped_buf);
1235796c8dcSSimon Schubert }
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Resize the buffer B to be at least N bytes long.  Raise an error if
1275796c8dcSSimon Schubert    B shouldn't be resized.  */
1285796c8dcSSimon Schubert static void
1295796c8dcSSimon Schubert resize_buffer (struct macro_buffer *b, int n)
1305796c8dcSSimon Schubert {
1315796c8dcSSimon Schubert   /* We shouldn't be trying to resize shared strings.  */
1325796c8dcSSimon Schubert   gdb_assert (! b->shared);
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert   if (b->size == 0)
1355796c8dcSSimon Schubert     b->size = n;
1365796c8dcSSimon Schubert   else
1375796c8dcSSimon Schubert     while (b->size <= n)
1385796c8dcSSimon Schubert       b->size *= 2;
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert   b->text = xrealloc (b->text, b->size);
1415796c8dcSSimon Schubert }
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert /* Append the character C to the buffer B.  */
1455796c8dcSSimon Schubert static void
1465796c8dcSSimon Schubert appendc (struct macro_buffer *b, int c)
1475796c8dcSSimon Schubert {
1485796c8dcSSimon Schubert   int new_len = b->len + 1;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   if (new_len > b->size)
1515796c8dcSSimon Schubert     resize_buffer (b, new_len);
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   b->text[b->len] = c;
1545796c8dcSSimon Schubert   b->len = new_len;
1555796c8dcSSimon Schubert }
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert /* Append the LEN bytes at ADDR to the buffer B.  */
1595796c8dcSSimon Schubert static void
1605796c8dcSSimon Schubert appendmem (struct macro_buffer *b, char *addr, int len)
1615796c8dcSSimon Schubert {
1625796c8dcSSimon Schubert   int new_len = b->len + len;
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert   if (new_len > b->size)
1655796c8dcSSimon Schubert     resize_buffer (b, new_len);
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   memcpy (b->text + b->len, addr, len);
1685796c8dcSSimon Schubert   b->len = new_len;
1695796c8dcSSimon Schubert }
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert /* Recognizing preprocessor tokens.  */
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert int
1775796c8dcSSimon Schubert macro_is_whitespace (int c)
1785796c8dcSSimon Schubert {
1795796c8dcSSimon Schubert   return (c == ' '
1805796c8dcSSimon Schubert           || c == '\t'
1815796c8dcSSimon Schubert           || c == '\n'
1825796c8dcSSimon Schubert           || c == '\v'
1835796c8dcSSimon Schubert           || c == '\f');
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert int
1885796c8dcSSimon Schubert macro_is_digit (int c)
1895796c8dcSSimon Schubert {
1905796c8dcSSimon Schubert   return ('0' <= c && c <= '9');
1915796c8dcSSimon Schubert }
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert int
1955796c8dcSSimon Schubert macro_is_identifier_nondigit (int c)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert   return (c == '_'
1985796c8dcSSimon Schubert           || ('a' <= c && c <= 'z')
1995796c8dcSSimon Schubert           || ('A' <= c && c <= 'Z'));
2005796c8dcSSimon Schubert }
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert static void
2045796c8dcSSimon Schubert set_token (struct macro_buffer *tok, char *start, char *end)
2055796c8dcSSimon Schubert {
2065796c8dcSSimon Schubert   init_shared_buffer (tok, start, end - start);
2075796c8dcSSimon Schubert   tok->last_token = 0;
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   /* Presumed; get_identifier may overwrite this.  */
2105796c8dcSSimon Schubert   tok->is_identifier = 0;
2115796c8dcSSimon Schubert }
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert static int
2155796c8dcSSimon Schubert get_comment (struct macro_buffer *tok, char *p, char *end)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   if (p + 2 > end)
2185796c8dcSSimon Schubert     return 0;
2195796c8dcSSimon Schubert   else if (p[0] == '/'
2205796c8dcSSimon Schubert            && p[1] == '*')
2215796c8dcSSimon Schubert     {
2225796c8dcSSimon Schubert       char *tok_start = p;
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert       p += 2;
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert       for (; p < end; p++)
2275796c8dcSSimon Schubert         if (p + 2 <= end
2285796c8dcSSimon Schubert             && p[0] == '*'
2295796c8dcSSimon Schubert             && p[1] == '/')
2305796c8dcSSimon Schubert           {
2315796c8dcSSimon Schubert             p += 2;
2325796c8dcSSimon Schubert             set_token (tok, tok_start, p);
2335796c8dcSSimon Schubert             return 1;
2345796c8dcSSimon Schubert           }
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert       error (_("Unterminated comment in macro expansion."));
2375796c8dcSSimon Schubert     }
2385796c8dcSSimon Schubert   else if (p[0] == '/'
2395796c8dcSSimon Schubert            && p[1] == '/')
2405796c8dcSSimon Schubert     {
2415796c8dcSSimon Schubert       char *tok_start = p;
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert       p += 2;
2445796c8dcSSimon Schubert       for (; p < end; p++)
2455796c8dcSSimon Schubert         if (*p == '\n')
2465796c8dcSSimon Schubert           break;
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert       set_token (tok, tok_start, p);
2495796c8dcSSimon Schubert       return 1;
2505796c8dcSSimon Schubert     }
2515796c8dcSSimon Schubert   else
2525796c8dcSSimon Schubert     return 0;
2535796c8dcSSimon Schubert }
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert static int
2575796c8dcSSimon Schubert get_identifier (struct macro_buffer *tok, char *p, char *end)
2585796c8dcSSimon Schubert {
2595796c8dcSSimon Schubert   if (p < end
2605796c8dcSSimon Schubert       && macro_is_identifier_nondigit (*p))
2615796c8dcSSimon Schubert     {
2625796c8dcSSimon Schubert       char *tok_start = p;
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert       while (p < end
2655796c8dcSSimon Schubert              && (macro_is_identifier_nondigit (*p)
2665796c8dcSSimon Schubert                  || macro_is_digit (*p)))
2675796c8dcSSimon Schubert         p++;
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert       set_token (tok, tok_start, p);
2705796c8dcSSimon Schubert       tok->is_identifier = 1;
2715796c8dcSSimon Schubert       return 1;
2725796c8dcSSimon Schubert     }
2735796c8dcSSimon Schubert   else
2745796c8dcSSimon Schubert     return 0;
2755796c8dcSSimon Schubert }
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert static int
2795796c8dcSSimon Schubert get_pp_number (struct macro_buffer *tok, char *p, char *end)
2805796c8dcSSimon Schubert {
2815796c8dcSSimon Schubert   if (p < end
2825796c8dcSSimon Schubert       && (macro_is_digit (*p)
2835796c8dcSSimon Schubert           || (*p == '.'
2845796c8dcSSimon Schubert 	      && p + 2 <= end
2855796c8dcSSimon Schubert 	      && macro_is_digit (p[1]))))
2865796c8dcSSimon Schubert     {
2875796c8dcSSimon Schubert       char *tok_start = p;
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert       while (p < end)
2905796c8dcSSimon Schubert         {
2915796c8dcSSimon Schubert 	  if (p + 2 <= end
2925796c8dcSSimon Schubert 	      && strchr ("eEpP", *p)
2935796c8dcSSimon Schubert 	      && (p[1] == '+' || p[1] == '-'))
2945796c8dcSSimon Schubert             p += 2;
2955796c8dcSSimon Schubert           else if (macro_is_digit (*p)
2965796c8dcSSimon Schubert 		   || macro_is_identifier_nondigit (*p)
2975796c8dcSSimon Schubert 		   || *p == '.')
2985796c8dcSSimon Schubert             p++;
2995796c8dcSSimon Schubert           else
3005796c8dcSSimon Schubert             break;
3015796c8dcSSimon Schubert         }
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert       set_token (tok, tok_start, p);
3045796c8dcSSimon Schubert       return 1;
3055796c8dcSSimon Schubert     }
3065796c8dcSSimon Schubert   else
3075796c8dcSSimon Schubert     return 0;
3085796c8dcSSimon Schubert }
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert /* If the text starting at P going up to (but not including) END
3135796c8dcSSimon Schubert    starts with a character constant, set *TOK to point to that
3145796c8dcSSimon Schubert    character constant, and return 1.  Otherwise, return zero.
3155796c8dcSSimon Schubert    Signal an error if it contains a malformed or incomplete character
3165796c8dcSSimon Schubert    constant.  */
3175796c8dcSSimon Schubert static int
3185796c8dcSSimon Schubert get_character_constant (struct macro_buffer *tok, char *p, char *end)
3195796c8dcSSimon Schubert {
3205796c8dcSSimon Schubert   /* ISO/IEC 9899:1999 (E)  Section 6.4.4.4  paragraph 1
3215796c8dcSSimon Schubert      But of course, what really matters is that we handle it the same
3225796c8dcSSimon Schubert      way GDB's C/C++ lexer does.  So we call parse_escape in utils.c
3235796c8dcSSimon Schubert      to handle escape sequences.  */
3245796c8dcSSimon Schubert   if ((p + 1 <= end && *p == '\'')
3255796c8dcSSimon Schubert       || (p + 2 <= end
3265796c8dcSSimon Schubert 	  && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
3275796c8dcSSimon Schubert 	  && p[1] == '\''))
3285796c8dcSSimon Schubert     {
3295796c8dcSSimon Schubert       char *tok_start = p;
3305796c8dcSSimon Schubert       char *body_start;
3315796c8dcSSimon Schubert       int char_count = 0;
3325796c8dcSSimon Schubert 
3335796c8dcSSimon Schubert       if (*p == '\'')
3345796c8dcSSimon Schubert         p++;
3355796c8dcSSimon Schubert       else if (*p == 'L' || *p == 'u' || *p == 'U')
3365796c8dcSSimon Schubert         p += 2;
3375796c8dcSSimon Schubert       else
338*c50c785cSJohn Marino         gdb_assert_not_reached ("unexpected character constant");
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert       body_start = p;
3415796c8dcSSimon Schubert       for (;;)
3425796c8dcSSimon Schubert         {
3435796c8dcSSimon Schubert           if (p >= end)
3445796c8dcSSimon Schubert             error (_("Unmatched single quote."));
3455796c8dcSSimon Schubert           else if (*p == '\'')
3465796c8dcSSimon Schubert             {
3475796c8dcSSimon Schubert               if (!char_count)
3485796c8dcSSimon Schubert                 error (_("A character constant must contain at least one "
3495796c8dcSSimon Schubert                        "character."));
3505796c8dcSSimon Schubert               p++;
3515796c8dcSSimon Schubert               break;
3525796c8dcSSimon Schubert             }
3535796c8dcSSimon Schubert           else if (*p == '\\')
3545796c8dcSSimon Schubert             {
3555796c8dcSSimon Schubert               p++;
3565796c8dcSSimon Schubert 	      char_count += c_parse_escape (&p, NULL);
3575796c8dcSSimon Schubert             }
3585796c8dcSSimon Schubert           else
3595796c8dcSSimon Schubert 	    {
3605796c8dcSSimon Schubert 	      p++;
3615796c8dcSSimon Schubert 	      char_count++;
3625796c8dcSSimon Schubert 	    }
3635796c8dcSSimon Schubert         }
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert       set_token (tok, tok_start, p);
3665796c8dcSSimon Schubert       return 1;
3675796c8dcSSimon Schubert     }
3685796c8dcSSimon Schubert   else
3695796c8dcSSimon Schubert     return 0;
3705796c8dcSSimon Schubert }
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert /* If the text starting at P going up to (but not including) END
3745796c8dcSSimon Schubert    starts with a string literal, set *TOK to point to that string
3755796c8dcSSimon Schubert    literal, and return 1.  Otherwise, return zero.  Signal an error if
3765796c8dcSSimon Schubert    it contains a malformed or incomplete string literal.  */
3775796c8dcSSimon Schubert static int
3785796c8dcSSimon Schubert get_string_literal (struct macro_buffer *tok, char *p, char *end)
3795796c8dcSSimon Schubert {
3805796c8dcSSimon Schubert   if ((p + 1 <= end
3815796c8dcSSimon Schubert        && *p == '"')
3825796c8dcSSimon Schubert       || (p + 2 <= end
3835796c8dcSSimon Schubert           && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
3845796c8dcSSimon Schubert           && p[1] == '"'))
3855796c8dcSSimon Schubert     {
3865796c8dcSSimon Schubert       char *tok_start = p;
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert       if (*p == '"')
3895796c8dcSSimon Schubert         p++;
3905796c8dcSSimon Schubert       else if (*p == 'L' || *p == 'u' || *p == 'U')
3915796c8dcSSimon Schubert         p += 2;
3925796c8dcSSimon Schubert       else
393*c50c785cSJohn Marino         gdb_assert_not_reached ("unexpected string literal");
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert       for (;;)
3965796c8dcSSimon Schubert         {
3975796c8dcSSimon Schubert           if (p >= end)
3985796c8dcSSimon Schubert             error (_("Unterminated string in expression."));
3995796c8dcSSimon Schubert           else if (*p == '"')
4005796c8dcSSimon Schubert             {
4015796c8dcSSimon Schubert               p++;
4025796c8dcSSimon Schubert               break;
4035796c8dcSSimon Schubert             }
4045796c8dcSSimon Schubert           else if (*p == '\n')
4055796c8dcSSimon Schubert             error (_("Newline characters may not appear in string "
4065796c8dcSSimon Schubert                    "constants."));
4075796c8dcSSimon Schubert           else if (*p == '\\')
4085796c8dcSSimon Schubert             {
4095796c8dcSSimon Schubert               p++;
4105796c8dcSSimon Schubert               c_parse_escape (&p, NULL);
4115796c8dcSSimon Schubert             }
4125796c8dcSSimon Schubert           else
4135796c8dcSSimon Schubert             p++;
4145796c8dcSSimon Schubert         }
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert       set_token (tok, tok_start, p);
4175796c8dcSSimon Schubert       return 1;
4185796c8dcSSimon Schubert     }
4195796c8dcSSimon Schubert   else
4205796c8dcSSimon Schubert     return 0;
4215796c8dcSSimon Schubert }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert 
4245796c8dcSSimon Schubert static int
4255796c8dcSSimon Schubert get_punctuator (struct macro_buffer *tok, char *p, char *end)
4265796c8dcSSimon Schubert {
4275796c8dcSSimon Schubert   /* Here, speed is much less important than correctness and clarity.  */
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   /* ISO/IEC 9899:1999 (E)  Section 6.4.6  Paragraph 1.
4305796c8dcSSimon Schubert      Note that this table is ordered in a special way.  A punctuator
4315796c8dcSSimon Schubert      which is a prefix of another punctuator must appear after its
4325796c8dcSSimon Schubert      "extension".  Otherwise, the wrong token will be returned.  */
4335796c8dcSSimon Schubert   static const char * const punctuators[] = {
4345796c8dcSSimon Schubert     "[", "]", "(", ")", "{", "}", "?", ";", ",", "~",
4355796c8dcSSimon Schubert     "...", ".",
4365796c8dcSSimon Schubert     "->", "--", "-=", "-",
4375796c8dcSSimon Schubert     "++", "+=", "+",
4385796c8dcSSimon Schubert     "*=", "*",
4395796c8dcSSimon Schubert     "!=", "!",
4405796c8dcSSimon Schubert     "&&", "&=", "&",
4415796c8dcSSimon Schubert     "/=", "/",
4425796c8dcSSimon Schubert     "%>", "%:%:", "%:", "%=", "%",
4435796c8dcSSimon Schubert     "^=", "^",
4445796c8dcSSimon Schubert     "##", "#",
4455796c8dcSSimon Schubert     ":>", ":",
4465796c8dcSSimon Schubert     "||", "|=", "|",
4475796c8dcSSimon Schubert     "<<=", "<<", "<=", "<:", "<%", "<",
4485796c8dcSSimon Schubert     ">>=", ">>", ">=", ">",
4495796c8dcSSimon Schubert     "==", "=",
4505796c8dcSSimon Schubert     0
4515796c8dcSSimon Schubert   };
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert   int i;
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert   if (p + 1 <= end)
4565796c8dcSSimon Schubert     {
4575796c8dcSSimon Schubert       for (i = 0; punctuators[i]; i++)
4585796c8dcSSimon Schubert         {
4595796c8dcSSimon Schubert           const char *punctuator = punctuators[i];
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert           if (p[0] == punctuator[0])
4625796c8dcSSimon Schubert             {
4635796c8dcSSimon Schubert               int len = strlen (punctuator);
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert               if (p + len <= end
4665796c8dcSSimon Schubert                   && ! memcmp (p, punctuator, len))
4675796c8dcSSimon Schubert                 {
4685796c8dcSSimon Schubert                   set_token (tok, p, p + len);
4695796c8dcSSimon Schubert                   return 1;
4705796c8dcSSimon Schubert                 }
4715796c8dcSSimon Schubert             }
4725796c8dcSSimon Schubert         }
4735796c8dcSSimon Schubert     }
4745796c8dcSSimon Schubert 
4755796c8dcSSimon Schubert   return 0;
4765796c8dcSSimon Schubert }
4775796c8dcSSimon Schubert 
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert /* Peel the next preprocessor token off of SRC, and put it in TOK.
4805796c8dcSSimon Schubert    Mutate TOK to refer to the first token in SRC, and mutate SRC to
4815796c8dcSSimon Schubert    refer to the text after that token.  SRC must be a shared buffer;
4825796c8dcSSimon Schubert    the resulting TOK will be shared, pointing into the same string SRC
4835796c8dcSSimon Schubert    does.  Initialize TOK's last_token field.  Return non-zero if we
4845796c8dcSSimon Schubert    succeed, or 0 if we didn't find any more tokens in SRC.  */
4855796c8dcSSimon Schubert static int
4865796c8dcSSimon Schubert get_token (struct macro_buffer *tok,
4875796c8dcSSimon Schubert            struct macro_buffer *src)
4885796c8dcSSimon Schubert {
4895796c8dcSSimon Schubert   char *p = src->text;
4905796c8dcSSimon Schubert   char *end = p + src->len;
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert   gdb_assert (src->shared);
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert   /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert      preprocessing-token:
4975796c8dcSSimon Schubert          header-name
4985796c8dcSSimon Schubert          identifier
4995796c8dcSSimon Schubert          pp-number
5005796c8dcSSimon Schubert          character-constant
5015796c8dcSSimon Schubert          string-literal
5025796c8dcSSimon Schubert          punctuator
5035796c8dcSSimon Schubert          each non-white-space character that cannot be one of the above
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert      We don't have to deal with header-name tokens, since those can
5065796c8dcSSimon Schubert      only occur after a #include, which we will never see.  */
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert   while (p < end)
5095796c8dcSSimon Schubert     if (macro_is_whitespace (*p))
5105796c8dcSSimon Schubert       p++;
5115796c8dcSSimon Schubert     else if (get_comment (tok, p, end))
5125796c8dcSSimon Schubert       p += tok->len;
5135796c8dcSSimon Schubert     else if (get_pp_number (tok, p, end)
5145796c8dcSSimon Schubert              || get_character_constant (tok, p, end)
5155796c8dcSSimon Schubert              || get_string_literal (tok, p, end)
5165796c8dcSSimon Schubert              /* Note: the grammar in the standard seems to be
5175796c8dcSSimon Schubert                 ambiguous: L'x' can be either a wide character
5185796c8dcSSimon Schubert                 constant, or an identifier followed by a normal
5195796c8dcSSimon Schubert                 character constant.  By trying `get_identifier' after
5205796c8dcSSimon Schubert                 we try get_character_constant and get_string_literal,
5215796c8dcSSimon Schubert                 we give the wide character syntax precedence.  Now,
5225796c8dcSSimon Schubert                 since GDB doesn't handle wide character constants
5235796c8dcSSimon Schubert                 anyway, is this the right thing to do?  */
5245796c8dcSSimon Schubert              || get_identifier (tok, p, end)
5255796c8dcSSimon Schubert              || get_punctuator (tok, p, end))
5265796c8dcSSimon Schubert       {
5275796c8dcSSimon Schubert         /* How many characters did we consume, including whitespace?  */
5285796c8dcSSimon Schubert         int consumed = p - src->text + tok->len;
529cf7f2e2dSJohn Marino 
5305796c8dcSSimon Schubert         src->text += consumed;
5315796c8dcSSimon Schubert         src->len -= consumed;
5325796c8dcSSimon Schubert         return 1;
5335796c8dcSSimon Schubert       }
5345796c8dcSSimon Schubert     else
5355796c8dcSSimon Schubert       {
5365796c8dcSSimon Schubert         /* We have found a "non-whitespace character that cannot be
5375796c8dcSSimon Schubert            one of the above."  Make a token out of it.  */
5385796c8dcSSimon Schubert         int consumed;
5395796c8dcSSimon Schubert 
5405796c8dcSSimon Schubert         set_token (tok, p, p + 1);
5415796c8dcSSimon Schubert         consumed = p - src->text + tok->len;
5425796c8dcSSimon Schubert         src->text += consumed;
5435796c8dcSSimon Schubert         src->len -= consumed;
5445796c8dcSSimon Schubert         return 1;
5455796c8dcSSimon Schubert       }
5465796c8dcSSimon Schubert 
5475796c8dcSSimon Schubert   return 0;
5485796c8dcSSimon Schubert }
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert 
5525796c8dcSSimon Schubert /* Appending token strings, with and without splicing  */
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert /* Append the macro buffer SRC to the end of DEST, and ensure that
5565796c8dcSSimon Schubert    doing so doesn't splice the token at the end of SRC with the token
5575796c8dcSSimon Schubert    at the beginning of DEST.  SRC and DEST must have their last_token
5585796c8dcSSimon Schubert    fields set.  Upon return, DEST's last_token field is set correctly.
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert    For example:
5615796c8dcSSimon Schubert 
5625796c8dcSSimon Schubert    If DEST is "(" and SRC is "y", then we can return with
5635796c8dcSSimon Schubert    DEST set to "(y" --- we've simply appended the two buffers.
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert    However, if DEST is "x" and SRC is "y", then we must not return
5665796c8dcSSimon Schubert    with DEST set to "xy" --- that would splice the two tokens "x" and
5675796c8dcSSimon Schubert    "y" together to make a single token "xy".  However, it would be
5685796c8dcSSimon Schubert    fine to return with DEST set to "x y".  Similarly, "<" and "<" must
5695796c8dcSSimon Schubert    yield "< <", not "<<", etc.  */
5705796c8dcSSimon Schubert static void
5715796c8dcSSimon Schubert append_tokens_without_splicing (struct macro_buffer *dest,
5725796c8dcSSimon Schubert                                 struct macro_buffer *src)
5735796c8dcSSimon Schubert {
5745796c8dcSSimon Schubert   int original_dest_len = dest->len;
5755796c8dcSSimon Schubert   struct macro_buffer dest_tail, new_token;
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert   gdb_assert (src->last_token != -1);
5785796c8dcSSimon Schubert   gdb_assert (dest->last_token != -1);
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert   /* First, just try appending the two, and call get_token to see if
5815796c8dcSSimon Schubert      we got a splice.  */
5825796c8dcSSimon Schubert   appendmem (dest, src->text, src->len);
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert   /* If DEST originally had no token abutting its end, then we can't
5855796c8dcSSimon Schubert      have spliced anything, so we're done.  */
5865796c8dcSSimon Schubert   if (dest->last_token == original_dest_len)
5875796c8dcSSimon Schubert     {
5885796c8dcSSimon Schubert       dest->last_token = original_dest_len + src->last_token;
5895796c8dcSSimon Schubert       return;
5905796c8dcSSimon Schubert     }
5915796c8dcSSimon Schubert 
5925796c8dcSSimon Schubert   /* Set DEST_TAIL to point to the last token in DEST, followed by
5935796c8dcSSimon Schubert      all the stuff we just appended.  */
5945796c8dcSSimon Schubert   init_shared_buffer (&dest_tail,
5955796c8dcSSimon Schubert                       dest->text + dest->last_token,
5965796c8dcSSimon Schubert                       dest->len - dest->last_token);
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert   /* Re-parse DEST's last token.  We know that DEST used to contain
5995796c8dcSSimon Schubert      at least one token, so if it doesn't contain any after the
6005796c8dcSSimon Schubert      append, then we must have spliced "/" and "*" or "/" and "/" to
6015796c8dcSSimon Schubert      make a comment start.  (Just for the record, I got this right
6025796c8dcSSimon Schubert      the first time.  This is not a bug fix.)  */
6035796c8dcSSimon Schubert   if (get_token (&new_token, &dest_tail)
6045796c8dcSSimon Schubert       && (new_token.text + new_token.len
6055796c8dcSSimon Schubert           == dest->text + original_dest_len))
6065796c8dcSSimon Schubert     {
6075796c8dcSSimon Schubert       /* No splice, so we're done.  */
6085796c8dcSSimon Schubert       dest->last_token = original_dest_len + src->last_token;
6095796c8dcSSimon Schubert       return;
6105796c8dcSSimon Schubert     }
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert   /* Okay, a simple append caused a splice.  Let's chop dest back to
6135796c8dcSSimon Schubert      its original length and try again, but separate the texts with a
6145796c8dcSSimon Schubert      space.  */
6155796c8dcSSimon Schubert   dest->len = original_dest_len;
6165796c8dcSSimon Schubert   appendc (dest, ' ');
6175796c8dcSSimon Schubert   appendmem (dest, src->text, src->len);
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert   init_shared_buffer (&dest_tail,
6205796c8dcSSimon Schubert                       dest->text + dest->last_token,
6215796c8dcSSimon Schubert                       dest->len - dest->last_token);
6225796c8dcSSimon Schubert 
6235796c8dcSSimon Schubert   /* Try to re-parse DEST's last token, as above.  */
6245796c8dcSSimon Schubert   if (get_token (&new_token, &dest_tail)
6255796c8dcSSimon Schubert       && (new_token.text + new_token.len
6265796c8dcSSimon Schubert           == dest->text + original_dest_len))
6275796c8dcSSimon Schubert     {
6285796c8dcSSimon Schubert       /* No splice, so we're done.  */
6295796c8dcSSimon Schubert       dest->last_token = original_dest_len + 1 + src->last_token;
6305796c8dcSSimon Schubert       return;
6315796c8dcSSimon Schubert     }
6325796c8dcSSimon Schubert 
6335796c8dcSSimon Schubert   /* As far as I know, there's no case where inserting a space isn't
6345796c8dcSSimon Schubert      enough to prevent a splice.  */
6355796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
6365796c8dcSSimon Schubert                   _("unable to avoid splicing tokens during macro expansion"));
6375796c8dcSSimon Schubert }
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert /* Stringify an argument, and insert it into DEST.  ARG is the text to
6405796c8dcSSimon Schubert    stringify; it is LEN bytes long.  */
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert static void
6435796c8dcSSimon Schubert stringify (struct macro_buffer *dest, char *arg, int len)
6445796c8dcSSimon Schubert {
6455796c8dcSSimon Schubert   /* Trim initial whitespace from ARG.  */
6465796c8dcSSimon Schubert   while (len > 0 && macro_is_whitespace (*arg))
6475796c8dcSSimon Schubert     {
6485796c8dcSSimon Schubert       ++arg;
6495796c8dcSSimon Schubert       --len;
6505796c8dcSSimon Schubert     }
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert   /* Trim trailing whitespace from ARG.  */
6535796c8dcSSimon Schubert   while (len > 0 && macro_is_whitespace (arg[len - 1]))
6545796c8dcSSimon Schubert     --len;
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert   /* Insert the string.  */
6575796c8dcSSimon Schubert   appendc (dest, '"');
6585796c8dcSSimon Schubert   while (len > 0)
6595796c8dcSSimon Schubert     {
6605796c8dcSSimon Schubert       /* We could try to handle strange cases here, like control
6615796c8dcSSimon Schubert 	 characters, but there doesn't seem to be much point.  */
6625796c8dcSSimon Schubert       if (macro_is_whitespace (*arg))
6635796c8dcSSimon Schubert 	{
6645796c8dcSSimon Schubert 	  /* Replace a sequence of whitespace with a single space.  */
6655796c8dcSSimon Schubert 	  appendc (dest, ' ');
6665796c8dcSSimon Schubert 	  while (len > 1 && macro_is_whitespace (arg[1]))
6675796c8dcSSimon Schubert 	    {
6685796c8dcSSimon Schubert 	      ++arg;
6695796c8dcSSimon Schubert 	      --len;
6705796c8dcSSimon Schubert 	    }
6715796c8dcSSimon Schubert 	}
6725796c8dcSSimon Schubert       else if (*arg == '\\' || *arg == '"')
6735796c8dcSSimon Schubert 	{
6745796c8dcSSimon Schubert 	  appendc (dest, '\\');
6755796c8dcSSimon Schubert 	  appendc (dest, *arg);
6765796c8dcSSimon Schubert 	}
6775796c8dcSSimon Schubert       else
6785796c8dcSSimon Schubert 	appendc (dest, *arg);
6795796c8dcSSimon Schubert       ++arg;
6805796c8dcSSimon Schubert       --len;
6815796c8dcSSimon Schubert     }
6825796c8dcSSimon Schubert   appendc (dest, '"');
6835796c8dcSSimon Schubert   dest->last_token = dest->len;
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert /* Expanding macros!  */
6885796c8dcSSimon Schubert 
6895796c8dcSSimon Schubert 
6905796c8dcSSimon Schubert /* A singly-linked list of the names of the macros we are currently
6915796c8dcSSimon Schubert    expanding --- for detecting expansion loops.  */
6925796c8dcSSimon Schubert struct macro_name_list {
6935796c8dcSSimon Schubert   const char *name;
6945796c8dcSSimon Schubert   struct macro_name_list *next;
6955796c8dcSSimon Schubert };
6965796c8dcSSimon Schubert 
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert /* Return non-zero if we are currently expanding the macro named NAME,
6995796c8dcSSimon Schubert    according to LIST; otherwise, return zero.
7005796c8dcSSimon Schubert 
7015796c8dcSSimon Schubert    You know, it would be possible to get rid of all the NO_LOOP
7025796c8dcSSimon Schubert    arguments to these functions by simply generating a new lookup
7035796c8dcSSimon Schubert    function and baton which refuses to find the definition for a
7045796c8dcSSimon Schubert    particular macro, and otherwise delegates the decision to another
7055796c8dcSSimon Schubert    function/baton pair.  But that makes the linked list of excluded
7065796c8dcSSimon Schubert    macros chained through untyped baton pointers, which will make it
7075796c8dcSSimon Schubert    harder to debug.  :(  */
7085796c8dcSSimon Schubert static int
7095796c8dcSSimon Schubert currently_rescanning (struct macro_name_list *list, const char *name)
7105796c8dcSSimon Schubert {
7115796c8dcSSimon Schubert   for (; list; list = list->next)
7125796c8dcSSimon Schubert     if (strcmp (name, list->name) == 0)
7135796c8dcSSimon Schubert       return 1;
7145796c8dcSSimon Schubert 
7155796c8dcSSimon Schubert   return 0;
7165796c8dcSSimon Schubert }
7175796c8dcSSimon Schubert 
7185796c8dcSSimon Schubert 
7195796c8dcSSimon Schubert /* Gather the arguments to a macro expansion.
7205796c8dcSSimon Schubert 
7215796c8dcSSimon Schubert    NAME is the name of the macro being invoked.  (It's only used for
7225796c8dcSSimon Schubert    printing error messages.)
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert    Assume that SRC is the text of the macro invocation immediately
7255796c8dcSSimon Schubert    following the macro name.  For example, if we're processing the
7265796c8dcSSimon Schubert    text foo(bar, baz), then NAME would be foo and SRC will be (bar,
7275796c8dcSSimon Schubert    baz).
7285796c8dcSSimon Schubert 
7295796c8dcSSimon Schubert    If SRC doesn't start with an open paren ( token at all, return
7305796c8dcSSimon Schubert    zero, leave SRC unchanged, and don't set *ARGC_P to anything.
7315796c8dcSSimon Schubert 
7325796c8dcSSimon Schubert    If SRC doesn't contain a properly terminated argument list, then
7335796c8dcSSimon Schubert    raise an error.
7345796c8dcSSimon Schubert 
7355796c8dcSSimon Schubert    For a variadic macro, NARGS holds the number of formal arguments to
7365796c8dcSSimon Schubert    the macro.  For a GNU-style variadic macro, this should be the
7375796c8dcSSimon Schubert    number of named arguments.  For a non-variadic macro, NARGS should
7385796c8dcSSimon Schubert    be -1.
7395796c8dcSSimon Schubert 
7405796c8dcSSimon Schubert    Otherwise, return a pointer to the first element of an array of
7415796c8dcSSimon Schubert    macro buffers referring to the argument texts, and set *ARGC_P to
7425796c8dcSSimon Schubert    the number of arguments we found --- the number of elements in the
7435796c8dcSSimon Schubert    array.  The macro buffers share their text with SRC, and their
7445796c8dcSSimon Schubert    last_token fields are initialized.  The array is allocated with
7455796c8dcSSimon Schubert    xmalloc, and the caller is responsible for freeing it.
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert    NOTE WELL: if SRC starts with a open paren ( token followed
7485796c8dcSSimon Schubert    immediately by a close paren ) token (e.g., the invocation looks
7495796c8dcSSimon Schubert    like "foo()"), we treat that as one argument, which happens to be
7505796c8dcSSimon Schubert    the empty list of tokens.  The caller should keep in mind that such
7515796c8dcSSimon Schubert    a sequence of tokens is a valid way to invoke one-parameter
7525796c8dcSSimon Schubert    function-like macros, but also a valid way to invoke zero-parameter
7535796c8dcSSimon Schubert    function-like macros.  Eeew.
7545796c8dcSSimon Schubert 
7555796c8dcSSimon Schubert    Consume the tokens from SRC; after this call, SRC contains the text
7565796c8dcSSimon Schubert    following the invocation.  */
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert static struct macro_buffer *
7595796c8dcSSimon Schubert gather_arguments (const char *name, struct macro_buffer *src,
7605796c8dcSSimon Schubert 		  int nargs, int *argc_p)
7615796c8dcSSimon Schubert {
7625796c8dcSSimon Schubert   struct macro_buffer tok;
7635796c8dcSSimon Schubert   int args_len, args_size;
7645796c8dcSSimon Schubert   struct macro_buffer *args = NULL;
7655796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (free_current_contents, &args);
7665796c8dcSSimon Schubert 
7675796c8dcSSimon Schubert   /* Does SRC start with an opening paren token?  Read from a copy of
7685796c8dcSSimon Schubert      SRC, so SRC itself is unaffected if we don't find an opening
7695796c8dcSSimon Schubert      paren.  */
7705796c8dcSSimon Schubert   {
7715796c8dcSSimon Schubert     struct macro_buffer temp;
772cf7f2e2dSJohn Marino 
7735796c8dcSSimon Schubert     init_shared_buffer (&temp, src->text, src->len);
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert     if (! get_token (&tok, &temp)
7765796c8dcSSimon Schubert         || tok.len != 1
7775796c8dcSSimon Schubert         || tok.text[0] != '(')
7785796c8dcSSimon Schubert       {
7795796c8dcSSimon Schubert         discard_cleanups (back_to);
7805796c8dcSSimon Schubert         return 0;
7815796c8dcSSimon Schubert       }
7825796c8dcSSimon Schubert   }
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert   /* Consume SRC's opening paren.  */
7855796c8dcSSimon Schubert   get_token (&tok, src);
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert   args_len = 0;
7885796c8dcSSimon Schubert   args_size = 6;
7895796c8dcSSimon Schubert   args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert   for (;;)
7925796c8dcSSimon Schubert     {
7935796c8dcSSimon Schubert       struct macro_buffer *arg;
7945796c8dcSSimon Schubert       int depth;
7955796c8dcSSimon Schubert 
7965796c8dcSSimon Schubert       /* Make sure we have room for the next argument.  */
7975796c8dcSSimon Schubert       if (args_len >= args_size)
7985796c8dcSSimon Schubert         {
7995796c8dcSSimon Schubert           args_size *= 2;
8005796c8dcSSimon Schubert           args = xrealloc (args, sizeof (*args) * args_size);
8015796c8dcSSimon Schubert         }
8025796c8dcSSimon Schubert 
8035796c8dcSSimon Schubert       /* Initialize the next argument.  */
8045796c8dcSSimon Schubert       arg = &args[args_len++];
8055796c8dcSSimon Schubert       set_token (arg, src->text, src->text);
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert       /* Gather the argument's tokens.  */
8085796c8dcSSimon Schubert       depth = 0;
8095796c8dcSSimon Schubert       for (;;)
8105796c8dcSSimon Schubert         {
8115796c8dcSSimon Schubert           if (! get_token (&tok, src))
8125796c8dcSSimon Schubert             error (_("Malformed argument list for macro `%s'."), name);
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert           /* Is tok an opening paren?  */
8155796c8dcSSimon Schubert           if (tok.len == 1 && tok.text[0] == '(')
8165796c8dcSSimon Schubert             depth++;
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert           /* Is tok is a closing paren?  */
8195796c8dcSSimon Schubert           else if (tok.len == 1 && tok.text[0] == ')')
8205796c8dcSSimon Schubert             {
8215796c8dcSSimon Schubert               /* If it's a closing paren at the top level, then that's
8225796c8dcSSimon Schubert                  the end of the argument list.  */
8235796c8dcSSimon Schubert               if (depth == 0)
8245796c8dcSSimon Schubert                 {
8255796c8dcSSimon Schubert 		  /* In the varargs case, the last argument may be
8265796c8dcSSimon Schubert 		     missing.  Add an empty argument in this case.  */
8275796c8dcSSimon Schubert 		  if (nargs != -1 && args_len == nargs - 1)
8285796c8dcSSimon Schubert 		    {
8295796c8dcSSimon Schubert 		      /* Make sure we have room for the argument.  */
8305796c8dcSSimon Schubert 		      if (args_len >= args_size)
8315796c8dcSSimon Schubert 			{
8325796c8dcSSimon Schubert 			  args_size++;
8335796c8dcSSimon Schubert 			  args = xrealloc (args, sizeof (*args) * args_size);
8345796c8dcSSimon Schubert 			}
8355796c8dcSSimon Schubert 		      arg = &args[args_len++];
8365796c8dcSSimon Schubert 		      set_token (arg, src->text, src->text);
8375796c8dcSSimon Schubert 		    }
8385796c8dcSSimon Schubert 
8395796c8dcSSimon Schubert                   discard_cleanups (back_to);
8405796c8dcSSimon Schubert                   *argc_p = args_len;
8415796c8dcSSimon Schubert                   return args;
8425796c8dcSSimon Schubert                 }
8435796c8dcSSimon Schubert 
8445796c8dcSSimon Schubert               depth--;
8455796c8dcSSimon Schubert             }
8465796c8dcSSimon Schubert 
8475796c8dcSSimon Schubert           /* If tok is a comma at top level, then that's the end of
8485796c8dcSSimon Schubert              the current argument.  However, if we are handling a
8495796c8dcSSimon Schubert              variadic macro and we are computing the last argument, we
8505796c8dcSSimon Schubert              want to include the comma and remaining tokens.  */
8515796c8dcSSimon Schubert           else if (tok.len == 1 && tok.text[0] == ',' && depth == 0
8525796c8dcSSimon Schubert 		   && (nargs == -1 || args_len < nargs))
8535796c8dcSSimon Schubert             break;
8545796c8dcSSimon Schubert 
8555796c8dcSSimon Schubert           /* Extend the current argument to enclose this token.  If
8565796c8dcSSimon Schubert              this is the current argument's first token, leave out any
8575796c8dcSSimon Schubert              leading whitespace, just for aesthetics.  */
8585796c8dcSSimon Schubert           if (arg->len == 0)
8595796c8dcSSimon Schubert             {
8605796c8dcSSimon Schubert               arg->text = tok.text;
8615796c8dcSSimon Schubert               arg->len = tok.len;
8625796c8dcSSimon Schubert               arg->last_token = 0;
8635796c8dcSSimon Schubert             }
8645796c8dcSSimon Schubert           else
8655796c8dcSSimon Schubert             {
8665796c8dcSSimon Schubert               arg->len = (tok.text + tok.len) - arg->text;
8675796c8dcSSimon Schubert               arg->last_token = tok.text - arg->text;
8685796c8dcSSimon Schubert             }
8695796c8dcSSimon Schubert         }
8705796c8dcSSimon Schubert     }
8715796c8dcSSimon Schubert }
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert 
8745796c8dcSSimon Schubert /* The `expand' and `substitute_args' functions both invoke `scan'
8755796c8dcSSimon Schubert    recursively, so we need a forward declaration somewhere.  */
8765796c8dcSSimon Schubert static void scan (struct macro_buffer *dest,
8775796c8dcSSimon Schubert                   struct macro_buffer *src,
8785796c8dcSSimon Schubert                   struct macro_name_list *no_loop,
8795796c8dcSSimon Schubert                   macro_lookup_ftype *lookup_func,
8805796c8dcSSimon Schubert                   void *lookup_baton);
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert 
8835796c8dcSSimon Schubert /* A helper function for substitute_args.
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert    ARGV is a vector of all the arguments; ARGC is the number of
8865796c8dcSSimon Schubert    arguments.  IS_VARARGS is true if the macro being substituted is a
8875796c8dcSSimon Schubert    varargs macro; in this case VA_ARG_NAME is the name of the
8885796c8dcSSimon Schubert    "variable" argument.  VA_ARG_NAME is ignored if IS_VARARGS is
8895796c8dcSSimon Schubert    false.
8905796c8dcSSimon Schubert 
8915796c8dcSSimon Schubert    If the token TOK is the name of a parameter, return the parameter's
8925796c8dcSSimon Schubert    index.  If TOK is not an argument, return -1.  */
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert static int
8955796c8dcSSimon Schubert find_parameter (const struct macro_buffer *tok,
8965796c8dcSSimon Schubert 		int is_varargs, const struct macro_buffer *va_arg_name,
8975796c8dcSSimon Schubert 		int argc, const char * const *argv)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert   int i;
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert   if (! tok->is_identifier)
9025796c8dcSSimon Schubert     return -1;
9035796c8dcSSimon Schubert 
9045796c8dcSSimon Schubert   for (i = 0; i < argc; ++i)
905*c50c785cSJohn Marino     if (tok->len == strlen (argv[i])
906*c50c785cSJohn Marino 	&& !memcmp (tok->text, argv[i], tok->len))
9075796c8dcSSimon Schubert       return i;
9085796c8dcSSimon Schubert 
9095796c8dcSSimon Schubert   if (is_varargs && tok->len == va_arg_name->len
9105796c8dcSSimon Schubert       && ! memcmp (tok->text, va_arg_name->text, tok->len))
9115796c8dcSSimon Schubert     return argc - 1;
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert   return -1;
9145796c8dcSSimon Schubert }
9155796c8dcSSimon Schubert 
9165796c8dcSSimon Schubert /* Given the macro definition DEF, being invoked with the actual
9175796c8dcSSimon Schubert    arguments given by ARGC and ARGV, substitute the arguments into the
9185796c8dcSSimon Schubert    replacement list, and store the result in DEST.
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert    IS_VARARGS should be true if DEF is a varargs macro.  In this case,
9215796c8dcSSimon Schubert    VA_ARG_NAME should be the name of the "variable" argument -- either
9225796c8dcSSimon Schubert    __VA_ARGS__ for c99-style varargs, or the final argument name, for
9235796c8dcSSimon Schubert    GNU-style varargs.  If IS_VARARGS is false, this parameter is
9245796c8dcSSimon Schubert    ignored.
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert    If it is necessary to expand macro invocations in one of the
9275796c8dcSSimon Schubert    arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
9285796c8dcSSimon Schubert    definitions, and don't expand invocations of the macros listed in
9295796c8dcSSimon Schubert    NO_LOOP.  */
9305796c8dcSSimon Schubert 
9315796c8dcSSimon Schubert static void
9325796c8dcSSimon Schubert substitute_args (struct macro_buffer *dest,
9335796c8dcSSimon Schubert                  struct macro_definition *def,
9345796c8dcSSimon Schubert 		 int is_varargs, const struct macro_buffer *va_arg_name,
9355796c8dcSSimon Schubert                  int argc, struct macro_buffer *argv,
9365796c8dcSSimon Schubert                  struct macro_name_list *no_loop,
9375796c8dcSSimon Schubert                  macro_lookup_ftype *lookup_func,
9385796c8dcSSimon Schubert                  void *lookup_baton)
9395796c8dcSSimon Schubert {
9405796c8dcSSimon Schubert   /* A macro buffer for the macro's replacement list.  */
9415796c8dcSSimon Schubert   struct macro_buffer replacement_list;
9425796c8dcSSimon Schubert   /* The token we are currently considering.  */
9435796c8dcSSimon Schubert   struct macro_buffer tok;
9445796c8dcSSimon Schubert   /* The replacement list's pointer from just before TOK was lexed.  */
9455796c8dcSSimon Schubert   char *original_rl_start;
9465796c8dcSSimon Schubert   /* We have a single lookahead token to handle token splicing.  */
9475796c8dcSSimon Schubert   struct macro_buffer lookahead;
9485796c8dcSSimon Schubert   /* The lookahead token might not be valid.  */
9495796c8dcSSimon Schubert   int lookahead_valid;
9505796c8dcSSimon Schubert   /* The replacement list's pointer from just before LOOKAHEAD was
9515796c8dcSSimon Schubert      lexed.  */
9525796c8dcSSimon Schubert   char *lookahead_rl_start;
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert   init_shared_buffer (&replacement_list, (char *) def->replacement,
9555796c8dcSSimon Schubert                       strlen (def->replacement));
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert   gdb_assert (dest->len == 0);
9585796c8dcSSimon Schubert   dest->last_token = 0;
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert   original_rl_start = replacement_list.text;
9615796c8dcSSimon Schubert   if (! get_token (&tok, &replacement_list))
9625796c8dcSSimon Schubert     return;
9635796c8dcSSimon Schubert   lookahead_rl_start = replacement_list.text;
9645796c8dcSSimon Schubert   lookahead_valid = get_token (&lookahead, &replacement_list);
9655796c8dcSSimon Schubert 
9665796c8dcSSimon Schubert   for (;;)
9675796c8dcSSimon Schubert     {
9685796c8dcSSimon Schubert       /* Just for aesthetics.  If we skipped some whitespace, copy
9695796c8dcSSimon Schubert          that to DEST.  */
9705796c8dcSSimon Schubert       if (tok.text > original_rl_start)
9715796c8dcSSimon Schubert         {
9725796c8dcSSimon Schubert           appendmem (dest, original_rl_start, tok.text - original_rl_start);
9735796c8dcSSimon Schubert           dest->last_token = dest->len;
9745796c8dcSSimon Schubert         }
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert       /* Is this token the stringification operator?  */
9775796c8dcSSimon Schubert       if (tok.len == 1
9785796c8dcSSimon Schubert           && tok.text[0] == '#')
9795796c8dcSSimon Schubert 	{
9805796c8dcSSimon Schubert 	  int arg;
9815796c8dcSSimon Schubert 
9825796c8dcSSimon Schubert 	  if (!lookahead_valid)
9835796c8dcSSimon Schubert 	    error (_("Stringification operator requires an argument."));
9845796c8dcSSimon Schubert 
9855796c8dcSSimon Schubert 	  arg = find_parameter (&lookahead, is_varargs, va_arg_name,
9865796c8dcSSimon Schubert 				def->argc, def->argv);
9875796c8dcSSimon Schubert 	  if (arg == -1)
9885796c8dcSSimon Schubert 	    error (_("Argument to stringification operator must name "
9895796c8dcSSimon Schubert 		     "a macro parameter."));
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert 	  stringify (dest, argv[arg].text, argv[arg].len);
9925796c8dcSSimon Schubert 
9935796c8dcSSimon Schubert 	  /* Read one token and let the loop iteration code handle the
9945796c8dcSSimon Schubert 	     rest.  */
9955796c8dcSSimon Schubert 	  lookahead_rl_start = replacement_list.text;
9965796c8dcSSimon Schubert 	  lookahead_valid = get_token (&lookahead, &replacement_list);
9975796c8dcSSimon Schubert 	}
9985796c8dcSSimon Schubert       /* Is this token the splicing operator?  */
9995796c8dcSSimon Schubert       else if (tok.len == 2
10005796c8dcSSimon Schubert 	       && tok.text[0] == '#'
10015796c8dcSSimon Schubert 	       && tok.text[1] == '#')
10025796c8dcSSimon Schubert 	error (_("Stray splicing operator"));
10035796c8dcSSimon Schubert       /* Is the next token the splicing operator?  */
10045796c8dcSSimon Schubert       else if (lookahead_valid
10055796c8dcSSimon Schubert 	       && lookahead.len == 2
10065796c8dcSSimon Schubert 	       && lookahead.text[0] == '#'
10075796c8dcSSimon Schubert 	       && lookahead.text[1] == '#')
10085796c8dcSSimon Schubert 	{
1009cf7f2e2dSJohn Marino 	  int finished = 0;
10105796c8dcSSimon Schubert 	  int prev_was_comma = 0;
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert 	  /* Note that GCC warns if the result of splicing is not a
10135796c8dcSSimon Schubert 	     token.  In the debugger there doesn't seem to be much
10145796c8dcSSimon Schubert 	     benefit from doing this.  */
10155796c8dcSSimon Schubert 
10165796c8dcSSimon Schubert 	  /* Insert the first token.  */
10175796c8dcSSimon Schubert 	  if (tok.len == 1 && tok.text[0] == ',')
10185796c8dcSSimon Schubert 	    prev_was_comma = 1;
10195796c8dcSSimon Schubert 	  else
10205796c8dcSSimon Schubert 	    {
10215796c8dcSSimon Schubert 	      int arg = find_parameter (&tok, is_varargs, va_arg_name,
10225796c8dcSSimon Schubert 					def->argc, def->argv);
1023cf7f2e2dSJohn Marino 
10245796c8dcSSimon Schubert 	      if (arg != -1)
10255796c8dcSSimon Schubert 		appendmem (dest, argv[arg].text, argv[arg].len);
10265796c8dcSSimon Schubert 	      else
10275796c8dcSSimon Schubert 		appendmem (dest, tok.text, tok.len);
10285796c8dcSSimon Schubert 	    }
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert 	  /* Apply a possible sequence of ## operators.  */
10315796c8dcSSimon Schubert 	  for (;;)
10325796c8dcSSimon Schubert 	    {
10335796c8dcSSimon Schubert 	      if (! get_token (&tok, &replacement_list))
10345796c8dcSSimon Schubert 		error (_("Splicing operator at end of macro"));
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert 	      /* Handle a comma before a ##.  If we are handling
10375796c8dcSSimon Schubert 		 varargs, and the token on the right hand side is the
10385796c8dcSSimon Schubert 		 varargs marker, and the final argument is empty or
10395796c8dcSSimon Schubert 		 missing, then drop the comma.  This is a GNU
10405796c8dcSSimon Schubert 		 extension.  There is one ambiguous case here,
10415796c8dcSSimon Schubert 		 involving pedantic behavior with an empty argument,
10425796c8dcSSimon Schubert 		 but we settle that in favor of GNU-style (GCC uses an
10435796c8dcSSimon Schubert 		 option).  If we aren't dealing with varargs, we
10445796c8dcSSimon Schubert 		 simply insert the comma.  */
10455796c8dcSSimon Schubert 	      if (prev_was_comma)
10465796c8dcSSimon Schubert 		{
10475796c8dcSSimon Schubert 		  if (! (is_varargs
10485796c8dcSSimon Schubert 			 && tok.len == va_arg_name->len
10495796c8dcSSimon Schubert 			 && !memcmp (tok.text, va_arg_name->text, tok.len)
10505796c8dcSSimon Schubert 			 && argv[argc - 1].len == 0))
10515796c8dcSSimon Schubert 		    appendmem (dest, ",", 1);
10525796c8dcSSimon Schubert 		  prev_was_comma = 0;
10535796c8dcSSimon Schubert 		}
10545796c8dcSSimon Schubert 
10555796c8dcSSimon Schubert 	      /* Insert the token.  If it is a parameter, insert the
10565796c8dcSSimon Schubert 		 argument.  If it is a comma, treat it specially.  */
10575796c8dcSSimon Schubert 	      if (tok.len == 1 && tok.text[0] == ',')
10585796c8dcSSimon Schubert 		prev_was_comma = 1;
10595796c8dcSSimon Schubert 	      else
10605796c8dcSSimon Schubert 		{
10615796c8dcSSimon Schubert 		  int arg = find_parameter (&tok, is_varargs, va_arg_name,
10625796c8dcSSimon Schubert 					    def->argc, def->argv);
1063cf7f2e2dSJohn Marino 
10645796c8dcSSimon Schubert 		  if (arg != -1)
10655796c8dcSSimon Schubert 		    appendmem (dest, argv[arg].text, argv[arg].len);
10665796c8dcSSimon Schubert 		  else
10675796c8dcSSimon Schubert 		    appendmem (dest, tok.text, tok.len);
10685796c8dcSSimon Schubert 		}
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert 	      /* Now read another token.  If it is another splice, we
10715796c8dcSSimon Schubert 		 loop.  */
10725796c8dcSSimon Schubert 	      original_rl_start = replacement_list.text;
10735796c8dcSSimon Schubert 	      if (! get_token (&tok, &replacement_list))
10745796c8dcSSimon Schubert 		{
10755796c8dcSSimon Schubert 		  finished = 1;
10765796c8dcSSimon Schubert 		  break;
10775796c8dcSSimon Schubert 		}
10785796c8dcSSimon Schubert 
10795796c8dcSSimon Schubert 	      if (! (tok.len == 2
10805796c8dcSSimon Schubert 		     && tok.text[0] == '#'
10815796c8dcSSimon Schubert 		     && tok.text[1] == '#'))
10825796c8dcSSimon Schubert 		break;
10835796c8dcSSimon Schubert 	    }
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert 	  if (prev_was_comma)
10865796c8dcSSimon Schubert 	    {
10875796c8dcSSimon Schubert 	      /* We saw a comma.  Insert it now.  */
10885796c8dcSSimon Schubert 	      appendmem (dest, ",", 1);
10895796c8dcSSimon Schubert 	    }
10905796c8dcSSimon Schubert 
10915796c8dcSSimon Schubert           dest->last_token = dest->len;
10925796c8dcSSimon Schubert 	  if (finished)
10935796c8dcSSimon Schubert 	    lookahead_valid = 0;
10945796c8dcSSimon Schubert 	  else
10955796c8dcSSimon Schubert 	    {
10965796c8dcSSimon Schubert 	      /* Set up for the loop iterator.  */
10975796c8dcSSimon Schubert 	      lookahead = tok;
10985796c8dcSSimon Schubert 	      lookahead_rl_start = original_rl_start;
10995796c8dcSSimon Schubert 	      lookahead_valid = 1;
11005796c8dcSSimon Schubert 	    }
11015796c8dcSSimon Schubert 	}
11025796c8dcSSimon Schubert       else
11035796c8dcSSimon Schubert 	{
11045796c8dcSSimon Schubert 	  /* Is this token an identifier?  */
11055796c8dcSSimon Schubert 	  int substituted = 0;
11065796c8dcSSimon Schubert 	  int arg = find_parameter (&tok, is_varargs, va_arg_name,
11075796c8dcSSimon Schubert 				    def->argc, def->argv);
11085796c8dcSSimon Schubert 
11095796c8dcSSimon Schubert 	  if (arg != -1)
11105796c8dcSSimon Schubert 	    {
11115796c8dcSSimon Schubert 	      struct macro_buffer arg_src;
11125796c8dcSSimon Schubert 
11135796c8dcSSimon Schubert 	      /* Expand any macro invocations in the argument text,
11145796c8dcSSimon Schubert 		 and append the result to dest.  Remember that scan
11155796c8dcSSimon Schubert 		 mutates its source, so we need to scan a new buffer
11165796c8dcSSimon Schubert 		 referring to the argument's text, not the argument
11175796c8dcSSimon Schubert 		 itself.  */
11185796c8dcSSimon Schubert 	      init_shared_buffer (&arg_src, argv[arg].text, argv[arg].len);
11195796c8dcSSimon Schubert 	      scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
11205796c8dcSSimon Schubert 	      substituted = 1;
11215796c8dcSSimon Schubert 	    }
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert 	  /* If it wasn't a parameter, then just copy it across.  */
11245796c8dcSSimon Schubert 	  if (! substituted)
11255796c8dcSSimon Schubert 	    append_tokens_without_splicing (dest, &tok);
11265796c8dcSSimon Schubert 	}
11275796c8dcSSimon Schubert 
11285796c8dcSSimon Schubert       if (! lookahead_valid)
11295796c8dcSSimon Schubert 	break;
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert       tok = lookahead;
11325796c8dcSSimon Schubert       original_rl_start = lookahead_rl_start;
11335796c8dcSSimon Schubert 
11345796c8dcSSimon Schubert       lookahead_rl_start = replacement_list.text;
11355796c8dcSSimon Schubert       lookahead_valid = get_token (&lookahead, &replacement_list);
11365796c8dcSSimon Schubert     }
11375796c8dcSSimon Schubert }
11385796c8dcSSimon Schubert 
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert /* Expand a call to a macro named ID, whose definition is DEF.  Append
11415796c8dcSSimon Schubert    its expansion to DEST.  SRC is the input text following the ID
11425796c8dcSSimon Schubert    token.  We are currently rescanning the expansions of the macros
11435796c8dcSSimon Schubert    named in NO_LOOP; don't re-expand them.  Use LOOKUP_FUNC and
11445796c8dcSSimon Schubert    LOOKUP_BATON to find definitions for any nested macro references.
11455796c8dcSSimon Schubert 
11465796c8dcSSimon Schubert    Return 1 if we decided to expand it, zero otherwise.  (If it's a
11475796c8dcSSimon Schubert    function-like macro name that isn't followed by an argument list,
11485796c8dcSSimon Schubert    we don't expand it.)  If we return zero, leave SRC unchanged.  */
11495796c8dcSSimon Schubert static int
11505796c8dcSSimon Schubert expand (const char *id,
11515796c8dcSSimon Schubert         struct macro_definition *def,
11525796c8dcSSimon Schubert         struct macro_buffer *dest,
11535796c8dcSSimon Schubert         struct macro_buffer *src,
11545796c8dcSSimon Schubert         struct macro_name_list *no_loop,
11555796c8dcSSimon Schubert         macro_lookup_ftype *lookup_func,
11565796c8dcSSimon Schubert         void *lookup_baton)
11575796c8dcSSimon Schubert {
11585796c8dcSSimon Schubert   struct macro_name_list new_no_loop;
11595796c8dcSSimon Schubert 
11605796c8dcSSimon Schubert   /* Create a new node to be added to the front of the no-expand list.
11615796c8dcSSimon Schubert      This list is appropriate for re-scanning replacement lists, but
11625796c8dcSSimon Schubert      it is *not* appropriate for scanning macro arguments; invocations
11635796c8dcSSimon Schubert      of the macro whose arguments we are gathering *do* get expanded
11645796c8dcSSimon Schubert      there.  */
11655796c8dcSSimon Schubert   new_no_loop.name = id;
11665796c8dcSSimon Schubert   new_no_loop.next = no_loop;
11675796c8dcSSimon Schubert 
11685796c8dcSSimon Schubert   /* What kind of macro are we expanding?  */
11695796c8dcSSimon Schubert   if (def->kind == macro_object_like)
11705796c8dcSSimon Schubert     {
11715796c8dcSSimon Schubert       struct macro_buffer replacement_list;
11725796c8dcSSimon Schubert 
11735796c8dcSSimon Schubert       init_shared_buffer (&replacement_list, (char *) def->replacement,
11745796c8dcSSimon Schubert                           strlen (def->replacement));
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert       scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
11775796c8dcSSimon Schubert       return 1;
11785796c8dcSSimon Schubert     }
11795796c8dcSSimon Schubert   else if (def->kind == macro_function_like)
11805796c8dcSSimon Schubert     {
11815796c8dcSSimon Schubert       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11825796c8dcSSimon Schubert       int argc = 0;
11835796c8dcSSimon Schubert       struct macro_buffer *argv = NULL;
11845796c8dcSSimon Schubert       struct macro_buffer substituted;
11855796c8dcSSimon Schubert       struct macro_buffer substituted_src;
1186*c50c785cSJohn Marino       struct macro_buffer va_arg_name = {0};
11875796c8dcSSimon Schubert       int is_varargs = 0;
11885796c8dcSSimon Schubert 
11895796c8dcSSimon Schubert       if (def->argc >= 1)
11905796c8dcSSimon Schubert 	{
11915796c8dcSSimon Schubert 	  if (strcmp (def->argv[def->argc - 1], "...") == 0)
11925796c8dcSSimon Schubert 	    {
11935796c8dcSSimon Schubert 	      /* In C99-style varargs, substitution is done using
11945796c8dcSSimon Schubert 		 __VA_ARGS__.  */
11955796c8dcSSimon Schubert 	      init_shared_buffer (&va_arg_name, "__VA_ARGS__",
11965796c8dcSSimon Schubert 				  strlen ("__VA_ARGS__"));
11975796c8dcSSimon Schubert 	      is_varargs = 1;
11985796c8dcSSimon Schubert 	    }
11995796c8dcSSimon Schubert 	  else
12005796c8dcSSimon Schubert 	    {
12015796c8dcSSimon Schubert 	      int len = strlen (def->argv[def->argc - 1]);
1202cf7f2e2dSJohn Marino 
12035796c8dcSSimon Schubert 	      if (len > 3
12045796c8dcSSimon Schubert 		  && strcmp (def->argv[def->argc - 1] + len - 3, "...") == 0)
12055796c8dcSSimon Schubert 		{
12065796c8dcSSimon Schubert 		  /* In GNU-style varargs, the name of the
12075796c8dcSSimon Schubert 		     substitution parameter is the name of the formal
12085796c8dcSSimon Schubert 		     argument without the "...".  */
12095796c8dcSSimon Schubert 		  init_shared_buffer (&va_arg_name,
12105796c8dcSSimon Schubert 				      (char *) def->argv[def->argc - 1],
12115796c8dcSSimon Schubert 				      len - 3);
12125796c8dcSSimon Schubert 		  is_varargs = 1;
12135796c8dcSSimon Schubert 		}
12145796c8dcSSimon Schubert 	    }
12155796c8dcSSimon Schubert 	}
12165796c8dcSSimon Schubert 
12175796c8dcSSimon Schubert       make_cleanup (free_current_contents, &argv);
12185796c8dcSSimon Schubert       argv = gather_arguments (id, src, is_varargs ? def->argc : -1,
12195796c8dcSSimon Schubert 			       &argc);
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert       /* If we couldn't find any argument list, then we don't expand
12225796c8dcSSimon Schubert          this macro.  */
12235796c8dcSSimon Schubert       if (! argv)
12245796c8dcSSimon Schubert         {
12255796c8dcSSimon Schubert           do_cleanups (back_to);
12265796c8dcSSimon Schubert           return 0;
12275796c8dcSSimon Schubert         }
12285796c8dcSSimon Schubert 
12295796c8dcSSimon Schubert       /* Check that we're passing an acceptable number of arguments for
12305796c8dcSSimon Schubert          this macro.  */
12315796c8dcSSimon Schubert       if (argc != def->argc)
12325796c8dcSSimon Schubert         {
12335796c8dcSSimon Schubert 	  if (is_varargs && argc >= def->argc - 1)
12345796c8dcSSimon Schubert 	    {
12355796c8dcSSimon Schubert 	      /* Ok.  */
12365796c8dcSSimon Schubert 	    }
12375796c8dcSSimon Schubert           /* Remember that a sequence of tokens like "foo()" is a
12385796c8dcSSimon Schubert              valid invocation of a macro expecting either zero or one
12395796c8dcSSimon Schubert              arguments.  */
12405796c8dcSSimon Schubert           else if (! (argc == 1
12415796c8dcSSimon Schubert 		      && argv[0].len == 0
12425796c8dcSSimon Schubert 		      && def->argc == 0))
12435796c8dcSSimon Schubert             error (_("Wrong number of arguments to macro `%s' "
12445796c8dcSSimon Schubert                    "(expected %d, got %d)."),
12455796c8dcSSimon Schubert                    id, def->argc, argc);
12465796c8dcSSimon Schubert         }
12475796c8dcSSimon Schubert 
12485796c8dcSSimon Schubert       /* Note that we don't expand macro invocations in the arguments
12495796c8dcSSimon Schubert          yet --- we let subst_args take care of that.  Parameters that
12505796c8dcSSimon Schubert          appear as operands of the stringifying operator "#" or the
12515796c8dcSSimon Schubert          splicing operator "##" don't get macro references expanded,
12525796c8dcSSimon Schubert          so we can't really tell whether it's appropriate to macro-
12535796c8dcSSimon Schubert          expand an argument until we see how it's being used.  */
12545796c8dcSSimon Schubert       init_buffer (&substituted, 0);
12555796c8dcSSimon Schubert       make_cleanup (cleanup_macro_buffer, &substituted);
12565796c8dcSSimon Schubert       substitute_args (&substituted, def, is_varargs, &va_arg_name,
12575796c8dcSSimon Schubert 		       argc, argv, no_loop, lookup_func, lookup_baton);
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert       /* Now `substituted' is the macro's replacement list, with all
12605796c8dcSSimon Schubert          argument values substituted into it properly.  Re-scan it for
12615796c8dcSSimon Schubert          macro references, but don't expand invocations of this macro.
12625796c8dcSSimon Schubert 
12635796c8dcSSimon Schubert          We create a new buffer, `substituted_src', which points into
12645796c8dcSSimon Schubert          `substituted', and scan that.  We can't scan `substituted'
12655796c8dcSSimon Schubert          itself, since the tokenization process moves the buffer's
12665796c8dcSSimon Schubert          text pointer around, and we still need to be able to find
12675796c8dcSSimon Schubert          `substituted's original text buffer after scanning it so we
12685796c8dcSSimon Schubert          can free it.  */
12695796c8dcSSimon Schubert       init_shared_buffer (&substituted_src, substituted.text, substituted.len);
12705796c8dcSSimon Schubert       scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert       do_cleanups (back_to);
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert       return 1;
12755796c8dcSSimon Schubert     }
12765796c8dcSSimon Schubert   else
12775796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, _("bad macro definition kind"));
12785796c8dcSSimon Schubert }
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert 
12815796c8dcSSimon Schubert /* If the single token in SRC_FIRST followed by the tokens in SRC_REST
12825796c8dcSSimon Schubert    constitute a macro invokation not forbidden in NO_LOOP, append its
12835796c8dcSSimon Schubert    expansion to DEST and return non-zero.  Otherwise, return zero, and
12845796c8dcSSimon Schubert    leave DEST unchanged.
12855796c8dcSSimon Schubert 
12865796c8dcSSimon Schubert    SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
12875796c8dcSSimon Schubert    SRC_FIRST must be a string built by get_token.  */
12885796c8dcSSimon Schubert static int
12895796c8dcSSimon Schubert maybe_expand (struct macro_buffer *dest,
12905796c8dcSSimon Schubert               struct macro_buffer *src_first,
12915796c8dcSSimon Schubert               struct macro_buffer *src_rest,
12925796c8dcSSimon Schubert               struct macro_name_list *no_loop,
12935796c8dcSSimon Schubert               macro_lookup_ftype *lookup_func,
12945796c8dcSSimon Schubert               void *lookup_baton)
12955796c8dcSSimon Schubert {
12965796c8dcSSimon Schubert   gdb_assert (src_first->shared);
12975796c8dcSSimon Schubert   gdb_assert (src_rest->shared);
12985796c8dcSSimon Schubert   gdb_assert (! dest->shared);
12995796c8dcSSimon Schubert 
13005796c8dcSSimon Schubert   /* Is this token an identifier?  */
13015796c8dcSSimon Schubert   if (src_first->is_identifier)
13025796c8dcSSimon Schubert     {
13035796c8dcSSimon Schubert       /* Make a null-terminated copy of it, since that's what our
13045796c8dcSSimon Schubert          lookup function expects.  */
13055796c8dcSSimon Schubert       char *id = xmalloc (src_first->len + 1);
13065796c8dcSSimon Schubert       struct cleanup *back_to = make_cleanup (xfree, id);
1307cf7f2e2dSJohn Marino 
13085796c8dcSSimon Schubert       memcpy (id, src_first->text, src_first->len);
13095796c8dcSSimon Schubert       id[src_first->len] = 0;
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert       /* If we're currently re-scanning the result of expanding
13125796c8dcSSimon Schubert          this macro, don't expand it again.  */
13135796c8dcSSimon Schubert       if (! currently_rescanning (no_loop, id))
13145796c8dcSSimon Schubert         {
13155796c8dcSSimon Schubert           /* Does this identifier have a macro definition in scope?  */
13165796c8dcSSimon Schubert           struct macro_definition *def = lookup_func (id, lookup_baton);
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert           if (def && expand (id, def, dest, src_rest, no_loop,
13195796c8dcSSimon Schubert                              lookup_func, lookup_baton))
13205796c8dcSSimon Schubert             {
13215796c8dcSSimon Schubert               do_cleanups (back_to);
13225796c8dcSSimon Schubert               return 1;
13235796c8dcSSimon Schubert             }
13245796c8dcSSimon Schubert         }
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert       do_cleanups (back_to);
13275796c8dcSSimon Schubert     }
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert   return 0;
13305796c8dcSSimon Schubert }
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert 
13335796c8dcSSimon Schubert /* Expand macro references in SRC, appending the results to DEST.
13345796c8dcSSimon Schubert    Assume we are re-scanning the result of expanding the macros named
13355796c8dcSSimon Schubert    in NO_LOOP, and don't try to re-expand references to them.
13365796c8dcSSimon Schubert 
13375796c8dcSSimon Schubert    SRC must be a shared buffer; DEST must not be one.  */
13385796c8dcSSimon Schubert static void
13395796c8dcSSimon Schubert scan (struct macro_buffer *dest,
13405796c8dcSSimon Schubert       struct macro_buffer *src,
13415796c8dcSSimon Schubert       struct macro_name_list *no_loop,
13425796c8dcSSimon Schubert       macro_lookup_ftype *lookup_func,
13435796c8dcSSimon Schubert       void *lookup_baton)
13445796c8dcSSimon Schubert {
13455796c8dcSSimon Schubert   gdb_assert (src->shared);
13465796c8dcSSimon Schubert   gdb_assert (! dest->shared);
13475796c8dcSSimon Schubert 
13485796c8dcSSimon Schubert   for (;;)
13495796c8dcSSimon Schubert     {
13505796c8dcSSimon Schubert       struct macro_buffer tok;
13515796c8dcSSimon Schubert       char *original_src_start = src->text;
13525796c8dcSSimon Schubert 
13535796c8dcSSimon Schubert       /* Find the next token in SRC.  */
13545796c8dcSSimon Schubert       if (! get_token (&tok, src))
13555796c8dcSSimon Schubert         break;
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert       /* Just for aesthetics.  If we skipped some whitespace, copy
13585796c8dcSSimon Schubert          that to DEST.  */
13595796c8dcSSimon Schubert       if (tok.text > original_src_start)
13605796c8dcSSimon Schubert         {
13615796c8dcSSimon Schubert           appendmem (dest, original_src_start, tok.text - original_src_start);
13625796c8dcSSimon Schubert           dest->last_token = dest->len;
13635796c8dcSSimon Schubert         }
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert       if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton))
13665796c8dcSSimon Schubert         /* We didn't end up expanding tok as a macro reference, so
13675796c8dcSSimon Schubert            simply append it to dest.  */
13685796c8dcSSimon Schubert         append_tokens_without_splicing (dest, &tok);
13695796c8dcSSimon Schubert     }
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert   /* Just for aesthetics.  If there was any trailing whitespace in
13725796c8dcSSimon Schubert      src, copy it to dest.  */
13735796c8dcSSimon Schubert   if (src->len)
13745796c8dcSSimon Schubert     {
13755796c8dcSSimon Schubert       appendmem (dest, src->text, src->len);
13765796c8dcSSimon Schubert       dest->last_token = dest->len;
13775796c8dcSSimon Schubert     }
13785796c8dcSSimon Schubert }
13795796c8dcSSimon Schubert 
13805796c8dcSSimon Schubert 
13815796c8dcSSimon Schubert char *
13825796c8dcSSimon Schubert macro_expand (const char *source,
13835796c8dcSSimon Schubert               macro_lookup_ftype *lookup_func,
13845796c8dcSSimon Schubert               void *lookup_func_baton)
13855796c8dcSSimon Schubert {
13865796c8dcSSimon Schubert   struct macro_buffer src, dest;
13875796c8dcSSimon Schubert   struct cleanup *back_to;
13885796c8dcSSimon Schubert 
13895796c8dcSSimon Schubert   init_shared_buffer (&src, (char *) source, strlen (source));
13905796c8dcSSimon Schubert 
13915796c8dcSSimon Schubert   init_buffer (&dest, 0);
13925796c8dcSSimon Schubert   dest.last_token = 0;
13935796c8dcSSimon Schubert   back_to = make_cleanup (cleanup_macro_buffer, &dest);
13945796c8dcSSimon Schubert 
13955796c8dcSSimon Schubert   scan (&dest, &src, 0, lookup_func, lookup_func_baton);
13965796c8dcSSimon Schubert 
13975796c8dcSSimon Schubert   appendc (&dest, '\0');
13985796c8dcSSimon Schubert 
13995796c8dcSSimon Schubert   discard_cleanups (back_to);
14005796c8dcSSimon Schubert   return dest.text;
14015796c8dcSSimon Schubert }
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert char *
14055796c8dcSSimon Schubert macro_expand_once (const char *source,
14065796c8dcSSimon Schubert                    macro_lookup_ftype *lookup_func,
14075796c8dcSSimon Schubert                    void *lookup_func_baton)
14085796c8dcSSimon Schubert {
14095796c8dcSSimon Schubert   error (_("Expand-once not implemented yet."));
14105796c8dcSSimon Schubert }
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert 
14135796c8dcSSimon Schubert char *
14145796c8dcSSimon Schubert macro_expand_next (char **lexptr,
14155796c8dcSSimon Schubert                    macro_lookup_ftype *lookup_func,
14165796c8dcSSimon Schubert                    void *lookup_baton)
14175796c8dcSSimon Schubert {
14185796c8dcSSimon Schubert   struct macro_buffer src, dest, tok;
14195796c8dcSSimon Schubert   struct cleanup *back_to;
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert   /* Set up SRC to refer to the input text, pointed to by *lexptr.  */
14225796c8dcSSimon Schubert   init_shared_buffer (&src, *lexptr, strlen (*lexptr));
14235796c8dcSSimon Schubert 
14245796c8dcSSimon Schubert   /* Set up DEST to receive the expansion, if there is one.  */
14255796c8dcSSimon Schubert   init_buffer (&dest, 0);
14265796c8dcSSimon Schubert   dest.last_token = 0;
14275796c8dcSSimon Schubert   back_to = make_cleanup (cleanup_macro_buffer, &dest);
14285796c8dcSSimon Schubert 
14295796c8dcSSimon Schubert   /* Get the text's first preprocessing token.  */
14305796c8dcSSimon Schubert   if (! get_token (&tok, &src))
14315796c8dcSSimon Schubert     {
14325796c8dcSSimon Schubert       do_cleanups (back_to);
14335796c8dcSSimon Schubert       return 0;
14345796c8dcSSimon Schubert     }
14355796c8dcSSimon Schubert 
14365796c8dcSSimon Schubert   /* If it's a macro invocation, expand it.  */
14375796c8dcSSimon Schubert   if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
14385796c8dcSSimon Schubert     {
14395796c8dcSSimon Schubert       /* It was a macro invocation!  Package up the expansion as a
14405796c8dcSSimon Schubert          null-terminated string and return it.  Set *lexptr to the
14415796c8dcSSimon Schubert          start of the next token in the input.  */
14425796c8dcSSimon Schubert       appendc (&dest, '\0');
14435796c8dcSSimon Schubert       discard_cleanups (back_to);
14445796c8dcSSimon Schubert       *lexptr = src.text;
14455796c8dcSSimon Schubert       return dest.text;
14465796c8dcSSimon Schubert     }
14475796c8dcSSimon Schubert   else
14485796c8dcSSimon Schubert     {
14495796c8dcSSimon Schubert       /* It wasn't a macro invocation.  */
14505796c8dcSSimon Schubert       do_cleanups (back_to);
14515796c8dcSSimon Schubert       return 0;
14525796c8dcSSimon Schubert     }
14535796c8dcSSimon Schubert }
1454