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