]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/macroexp.c
Updated copyright notices for most files.
[thirdparty/binutils-gdb.git] / gdb / macroexp.c
CommitLineData
ec2bcbe7 1/* C preprocessor macro expansion for GDB.
9b254dd1 2 Copyright (C) 2002, 2007, 2008 Free Software Foundation, Inc.
ec2bcbe7
JB
3 Contributed by Red Hat, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
ec2bcbe7
JB
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ec2bcbe7
JB
19
20#include "defs.h"
04ea0df1 21#include "gdb_obstack.h"
ec2bcbe7
JB
22#include "bcache.h"
23#include "macrotab.h"
24#include "macroexp.h"
25#include "gdb_assert.h"
26
27
28\f
29/* A resizeable, substringable string type. */
30
31
32/* A string type that we can resize, quickly append to, and use to
33 refer to substrings of other strings. */
34struct macro_buffer
35{
36 /* An array of characters. The first LEN bytes are the real text,
37 but there are SIZE bytes allocated to the array. If SIZE is
38 zero, then this doesn't point to a malloc'ed block. If SHARED is
39 non-zero, then this buffer is actually a pointer into some larger
40 string, and we shouldn't append characters to it, etc. Because
41 of sharing, we can't assume in general that the text is
42 null-terminated. */
43 char *text;
44
45 /* The number of characters in the string. */
46 int len;
47
48 /* The number of characters allocated to the string. If SHARED is
49 non-zero, this is meaningless; in this case, we set it to zero so
50 that any "do we have room to append something?" tests will fail,
51 so we don't always have to check SHARED before using this field. */
52 int size;
53
54 /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
55 block). Non-zero if TEXT is actually pointing into the middle of
56 some other block, and we shouldn't reallocate it. */
57 int shared;
58
59 /* For detecting token splicing.
60
61 This is the index in TEXT of the first character of the token
62 that abuts the end of TEXT. If TEXT contains no tokens, then we
63 set this equal to LEN. If TEXT ends in whitespace, then there is
64 no token abutting the end of TEXT (it's just whitespace), and
65 again, we set this equal to LEN. We set this to -1 if we don't
66 know the nature of TEXT. */
67 int last_token;
68
69 /* If this buffer is holding the result from get_token, then this
70 is non-zero if it is an identifier token, zero otherwise. */
71 int is_identifier;
72};
73
74
75/* Set the macro buffer *B to the empty string, guessing that its
76 final contents will fit in N bytes. (It'll get resized if it
77 doesn't, so the guess doesn't have to be right.) Allocate the
78 initial storage with xmalloc. */
79static void
80init_buffer (struct macro_buffer *b, int n)
81{
ec2bcbe7
JB
82 b->size = n;
83 if (n > 0)
84 b->text = (char *) xmalloc (n);
85 else
a86bc61c 86 b->text = NULL;
ec2bcbe7
JB
87 b->len = 0;
88 b->shared = 0;
89 b->last_token = -1;
90}
91
92
93/* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
94 shared substring. */
95static void
96init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
97{
98 buf->text = addr;
99 buf->len = len;
100 buf->shared = 1;
101 buf->size = 0;
102 buf->last_token = -1;
103}
104
105
106/* Free the text of the buffer B. Raise an error if B is shared. */
107static void
108free_buffer (struct macro_buffer *b)
109{
110 gdb_assert (! b->shared);
111 if (b->size)
112 xfree (b->text);
113}
114
115
116/* A cleanup function for macro buffers. */
117static void
118cleanup_macro_buffer (void *untyped_buf)
119{
120 free_buffer ((struct macro_buffer *) untyped_buf);
121}
122
123
124/* Resize the buffer B to be at least N bytes long. Raise an error if
125 B shouldn't be resized. */
126static void
127resize_buffer (struct macro_buffer *b, int n)
128{
129 /* We shouldn't be trying to resize shared strings. */
130 gdb_assert (! b->shared);
131
132 if (b->size == 0)
133 b->size = n;
134 else
135 while (b->size <= n)
136 b->size *= 2;
137
138 b->text = xrealloc (b->text, b->size);
139}
140
141
142/* Append the character C to the buffer B. */
39efb398 143static void
ec2bcbe7
JB
144appendc (struct macro_buffer *b, int c)
145{
146 int new_len = b->len + 1;
147
148 if (new_len > b->size)
149 resize_buffer (b, new_len);
150
151 b->text[b->len] = c;
152 b->len = new_len;
153}
154
155
156/* Append the LEN bytes at ADDR to the buffer B. */
39efb398 157static void
ec2bcbe7
JB
158appendmem (struct macro_buffer *b, char *addr, int len)
159{
160 int new_len = b->len + len;
161
162 if (new_len > b->size)
163 resize_buffer (b, new_len);
164
165 memcpy (b->text + b->len, addr, len);
166 b->len = new_len;
167}
168
169
170\f
171/* Recognizing preprocessor tokens. */
172
173
174static int
175is_whitespace (int c)
176{
177 return (c == ' '
178 || c == '\t'
179 || c == '\n'
180 || c == '\v'
181 || c == '\f');
182}
183
184
185static int
186is_digit (int c)
187{
188 return ('0' <= c && c <= '9');
189}
190
191
192static int
193is_identifier_nondigit (int c)
194{
195 return (c == '_'
196 || ('a' <= c && c <= 'z')
197 || ('A' <= c && c <= 'Z'));
198}
199
200
201static void
202set_token (struct macro_buffer *tok, char *start, char *end)
203{
204 init_shared_buffer (tok, start, end - start);
205 tok->last_token = 0;
206
207 /* Presumed; get_identifier may overwrite this. */
208 tok->is_identifier = 0;
209}
210
211
212static int
213get_comment (struct macro_buffer *tok, char *p, char *end)
214{
215 if (p + 2 > end)
216 return 0;
217 else if (p[0] == '/'
218 && p[1] == '*')
219 {
220 char *tok_start = p;
221
222 p += 2;
223
224 for (; p < end; p++)
225 if (p + 2 <= end
226 && p[0] == '*'
227 && p[1] == '/')
228 {
229 p += 2;
230 set_token (tok, tok_start, p);
231 return 1;
232 }
233
8a3fe4f8 234 error (_("Unterminated comment in macro expansion."));
ec2bcbe7
JB
235 }
236 else if (p[0] == '/'
237 && p[1] == '/')
238 {
239 char *tok_start = p;
240
241 p += 2;
242 for (; p < end; p++)
243 if (*p == '\n')
244 break;
245
246 set_token (tok, tok_start, p);
247 return 1;
248 }
249 else
250 return 0;
251}
252
253
254static int
255get_identifier (struct macro_buffer *tok, char *p, char *end)
256{
257 if (p < end
258 && is_identifier_nondigit (*p))
259 {
260 char *tok_start = p;
261
262 while (p < end
263 && (is_identifier_nondigit (*p)
264 || is_digit (*p)))
265 p++;
266
267 set_token (tok, tok_start, p);
268 tok->is_identifier = 1;
269 return 1;
270 }
271 else
272 return 0;
273}
274
275
276static int
277get_pp_number (struct macro_buffer *tok, char *p, char *end)
278{
279 if (p < end
280 && (is_digit (*p)
281 || *p == '.'))
282 {
283 char *tok_start = p;
284
285 while (p < end)
286 {
287 if (is_digit (*p)
288 || is_identifier_nondigit (*p)
289 || *p == '.')
290 p++;
291 else if (p + 2 <= end
292 && strchr ("eEpP.", *p)
293 && (p[1] == '+' || p[1] == '-'))
294 p += 2;
295 else
296 break;
297 }
298
299 set_token (tok, tok_start, p);
300 return 1;
301 }
302 else
303 return 0;
304}
305
306
307
308/* If the text starting at P going up to (but not including) END
309 starts with a character constant, set *TOK to point to that
310 character constant, and return 1. Otherwise, return zero.
311 Signal an error if it contains a malformed or incomplete character
312 constant. */
313static int
314get_character_constant (struct macro_buffer *tok, char *p, char *end)
315{
316 /* ISO/IEC 9899:1999 (E) Section 6.4.4.4 paragraph 1
317 But of course, what really matters is that we handle it the same
318 way GDB's C/C++ lexer does. So we call parse_escape in utils.c
319 to handle escape sequences. */
320 if ((p + 1 <= end && *p == '\'')
321 || (p + 2 <= end && p[0] == 'L' && p[1] == '\''))
322 {
323 char *tok_start = p;
324 char *body_start;
325
326 if (*p == '\'')
327 p++;
328 else if (*p == 'L')
329 p += 2;
330 else
331 gdb_assert (0);
332
333 body_start = p;
334 for (;;)
335 {
336 if (p >= end)
8a3fe4f8 337 error (_("Unmatched single quote."));
ec2bcbe7
JB
338 else if (*p == '\'')
339 {
340 if (p == body_start)
8a3fe4f8
AC
341 error (_("A character constant must contain at least one "
342 "character."));
ec2bcbe7
JB
343 p++;
344 break;
345 }
346 else if (*p == '\\')
347 {
348 p++;
349 parse_escape (&p);
350 }
351 else
352 p++;
353 }
354
355 set_token (tok, tok_start, p);
356 return 1;
357 }
358 else
359 return 0;
360}
361
362
363/* If the text starting at P going up to (but not including) END
364 starts with a string literal, set *TOK to point to that string
365 literal, and return 1. Otherwise, return zero. Signal an error if
366 it contains a malformed or incomplete string literal. */
367static int
368get_string_literal (struct macro_buffer *tok, char *p, char *end)
369{
370 if ((p + 1 <= end
371 && *p == '\"')
372 || (p + 2 <= end
373 && p[0] == 'L'
374 && p[1] == '\"'))
375 {
376 char *tok_start = p;
377
378 if (*p == '\"')
379 p++;
380 else if (*p == 'L')
381 p += 2;
382 else
383 gdb_assert (0);
384
385 for (;;)
386 {
387 if (p >= end)
8a3fe4f8 388 error (_("Unterminated string in expression."));
ec2bcbe7
JB
389 else if (*p == '\"')
390 {
391 p++;
392 break;
393 }
394 else if (*p == '\n')
8a3fe4f8
AC
395 error (_("Newline characters may not appear in string "
396 "constants."));
ec2bcbe7
JB
397 else if (*p == '\\')
398 {
399 p++;
400 parse_escape (&p);
401 }
402 else
403 p++;
404 }
405
406 set_token (tok, tok_start, p);
407 return 1;
408 }
409 else
410 return 0;
411}
412
413
414static int
415get_punctuator (struct macro_buffer *tok, char *p, char *end)
416{
417 /* Here, speed is much less important than correctness and clarity. */
418
419 /* ISO/IEC 9899:1999 (E) Section 6.4.6 Paragraph 1 */
420 static const char * const punctuators[] = {
421 "[", "]", "(", ")", "{", "}", ".", "->",
422 "++", "--", "&", "*", "+", "-", "~", "!",
423 "/", "%", "<<", ">>", "<", ">", "<=", ">=", "==", "!=",
424 "^", "|", "&&", "||",
425 "?", ":", ";", "...",
426 "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=",
427 ",", "#", "##",
428 "<:", ":>", "<%", "%>", "%:", "%:%:",
429 0
430 };
431
432 int i;
433
434 if (p + 1 <= end)
435 {
436 for (i = 0; punctuators[i]; i++)
437 {
438 const char *punctuator = punctuators[i];
439
440 if (p[0] == punctuator[0])
441 {
442 int len = strlen (punctuator);
443
444 if (p + len <= end
445 && ! memcmp (p, punctuator, len))
446 {
447 set_token (tok, p, p + len);
448 return 1;
449 }
450 }
451 }
452 }
453
454 return 0;
455}
456
457
458/* Peel the next preprocessor token off of SRC, and put it in TOK.
459 Mutate TOK to refer to the first token in SRC, and mutate SRC to
460 refer to the text after that token. SRC must be a shared buffer;
461 the resulting TOK will be shared, pointing into the same string SRC
462 does. Initialize TOK's last_token field. Return non-zero if we
463 succeed, or 0 if we didn't find any more tokens in SRC. */
464static int
465get_token (struct macro_buffer *tok,
466 struct macro_buffer *src)
467{
468 char *p = src->text;
469 char *end = p + src->len;
470
471 gdb_assert (src->shared);
472
473 /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
474
475 preprocessing-token:
476 header-name
477 identifier
478 pp-number
479 character-constant
480 string-literal
481 punctuator
482 each non-white-space character that cannot be one of the above
483
484 We don't have to deal with header-name tokens, since those can
485 only occur after a #include, which we will never see. */
486
487 while (p < end)
488 if (is_whitespace (*p))
489 p++;
490 else if (get_comment (tok, p, end))
491 p += tok->len;
492 else if (get_pp_number (tok, p, end)
493 || get_character_constant (tok, p, end)
494 || get_string_literal (tok, p, end)
495 /* Note: the grammar in the standard seems to be
496 ambiguous: L'x' can be either a wide character
497 constant, or an identifier followed by a normal
498 character constant. By trying `get_identifier' after
499 we try get_character_constant and get_string_literal,
500 we give the wide character syntax precedence. Now,
501 since GDB doesn't handle wide character constants
502 anyway, is this the right thing to do? */
503 || get_identifier (tok, p, end)
504 || get_punctuator (tok, p, end))
505 {
506 /* How many characters did we consume, including whitespace? */
507 int consumed = p - src->text + tok->len;
508 src->text += consumed;
509 src->len -= consumed;
510 return 1;
511 }
512 else
513 {
514 /* We have found a "non-whitespace character that cannot be
515 one of the above." Make a token out of it. */
516 int consumed;
517
518 set_token (tok, p, p + 1);
519 consumed = p - src->text + tok->len;
520 src->text += consumed;
521 src->len -= consumed;
522 return 1;
523 }
524
525 return 0;
526}
527
528
529\f
530/* Appending token strings, with and without splicing */
531
532
533/* Append the macro buffer SRC to the end of DEST, and ensure that
534 doing so doesn't splice the token at the end of SRC with the token
535 at the beginning of DEST. SRC and DEST must have their last_token
536 fields set. Upon return, DEST's last_token field is set correctly.
537
538 For example:
539
540 If DEST is "(" and SRC is "y", then we can return with
541 DEST set to "(y" --- we've simply appended the two buffers.
542
543 However, if DEST is "x" and SRC is "y", then we must not return
544 with DEST set to "xy" --- that would splice the two tokens "x" and
545 "y" together to make a single token "xy". However, it would be
546 fine to return with DEST set to "x y". Similarly, "<" and "<" must
547 yield "< <", not "<<", etc. */
548static void
549append_tokens_without_splicing (struct macro_buffer *dest,
550 struct macro_buffer *src)
551{
552 int original_dest_len = dest->len;
553 struct macro_buffer dest_tail, new_token;
554
555 gdb_assert (src->last_token != -1);
556 gdb_assert (dest->last_token != -1);
557
558 /* First, just try appending the two, and call get_token to see if
559 we got a splice. */
560 appendmem (dest, src->text, src->len);
561
562 /* If DEST originally had no token abutting its end, then we can't
563 have spliced anything, so we're done. */
564 if (dest->last_token == original_dest_len)
565 {
566 dest->last_token = original_dest_len + src->last_token;
567 return;
568 }
569
570 /* Set DEST_TAIL to point to the last token in DEST, followed by
571 all the stuff we just appended. */
572 init_shared_buffer (&dest_tail,
573 dest->text + dest->last_token,
574 dest->len - dest->last_token);
575
576 /* Re-parse DEST's last token. We know that DEST used to contain
577 at least one token, so if it doesn't contain any after the
578 append, then we must have spliced "/" and "*" or "/" and "/" to
579 make a comment start. (Just for the record, I got this right
580 the first time. This is not a bug fix.) */
581 if (get_token (&new_token, &dest_tail)
582 && (new_token.text + new_token.len
583 == dest->text + original_dest_len))
584 {
585 /* No splice, so we're done. */
586 dest->last_token = original_dest_len + src->last_token;
587 return;
588 }
589
590 /* Okay, a simple append caused a splice. Let's chop dest back to
591 its original length and try again, but separate the texts with a
592 space. */
593 dest->len = original_dest_len;
594 appendc (dest, ' ');
595 appendmem (dest, src->text, src->len);
596
597 init_shared_buffer (&dest_tail,
598 dest->text + dest->last_token,
599 dest->len - dest->last_token);
600
601 /* Try to re-parse DEST's last token, as above. */
602 if (get_token (&new_token, &dest_tail)
603 && (new_token.text + new_token.len
604 == dest->text + original_dest_len))
605 {
606 /* No splice, so we're done. */
607 dest->last_token = original_dest_len + 1 + src->last_token;
608 return;
609 }
610
611 /* As far as I know, there's no case where inserting a space isn't
612 enough to prevent a splice. */
613 internal_error (__FILE__, __LINE__,
e2e0b3e5 614 _("unable to avoid splicing tokens during macro expansion"));
ec2bcbe7
JB
615}
616
617
618\f
619/* Expanding macros! */
620
621
622/* A singly-linked list of the names of the macros we are currently
623 expanding --- for detecting expansion loops. */
624struct macro_name_list {
625 const char *name;
626 struct macro_name_list *next;
627};
628
629
630/* Return non-zero if we are currently expanding the macro named NAME,
631 according to LIST; otherwise, return zero.
632
633 You know, it would be possible to get rid of all the NO_LOOP
634 arguments to these functions by simply generating a new lookup
635 function and baton which refuses to find the definition for a
636 particular macro, and otherwise delegates the decision to another
637 function/baton pair. But that makes the linked list of excluded
638 macros chained through untyped baton pointers, which will make it
639 harder to debug. :( */
640static int
641currently_rescanning (struct macro_name_list *list, const char *name)
642{
643 for (; list; list = list->next)
a86bc61c 644 if (strcmp (name, list->name) == 0)
ec2bcbe7
JB
645 return 1;
646
647 return 0;
648}
649
650
651/* Gather the arguments to a macro expansion.
652
653 NAME is the name of the macro being invoked. (It's only used for
654 printing error messages.)
655
656 Assume that SRC is the text of the macro invocation immediately
657 following the macro name. For example, if we're processing the
658 text foo(bar, baz), then NAME would be foo and SRC will be (bar,
659 baz).
660
661 If SRC doesn't start with an open paren ( token at all, return
662 zero, leave SRC unchanged, and don't set *ARGC_P to anything.
663
664 If SRC doesn't contain a properly terminated argument list, then
665 raise an error.
666
667 Otherwise, return a pointer to the first element of an array of
668 macro buffers referring to the argument texts, and set *ARGC_P to
669 the number of arguments we found --- the number of elements in the
670 array. The macro buffers share their text with SRC, and their
671 last_token fields are initialized. The array is allocated with
672 xmalloc, and the caller is responsible for freeing it.
673
674 NOTE WELL: if SRC starts with a open paren ( token followed
675 immediately by a close paren ) token (e.g., the invocation looks
676 like "foo()"), we treat that as one argument, which happens to be
677 the empty list of tokens. The caller should keep in mind that such
678 a sequence of tokens is a valid way to invoke one-parameter
679 function-like macros, but also a valid way to invoke zero-parameter
680 function-like macros. Eeew.
681
682 Consume the tokens from SRC; after this call, SRC contains the text
683 following the invocation. */
684
685static struct macro_buffer *
686gather_arguments (const char *name, struct macro_buffer *src, int *argc_p)
687{
688 struct macro_buffer tok;
689 int args_len, args_size;
a86bc61c 690 struct macro_buffer *args = NULL;
ec2bcbe7
JB
691 struct cleanup *back_to = make_cleanup (free_current_contents, &args);
692
693 /* Does SRC start with an opening paren token? Read from a copy of
694 SRC, so SRC itself is unaffected if we don't find an opening
695 paren. */
696 {
697 struct macro_buffer temp;
698 init_shared_buffer (&temp, src->text, src->len);
699
700 if (! get_token (&tok, &temp)
701 || tok.len != 1
702 || tok.text[0] != '(')
703 {
704 discard_cleanups (back_to);
705 return 0;
706 }
707 }
708
709 /* Consume SRC's opening paren. */
710 get_token (&tok, src);
711
712 args_len = 0;
b1ddacc7 713 args_size = 6;
ec2bcbe7
JB
714 args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);
715
716 for (;;)
717 {
718 struct macro_buffer *arg;
719 int depth;
720
721 /* Make sure we have room for the next argument. */
722 if (args_len >= args_size)
723 {
724 args_size *= 2;
725 args = xrealloc (args, sizeof (*args) * args_size);
726 }
727
728 /* Initialize the next argument. */
729 arg = &args[args_len++];
730 set_token (arg, src->text, src->text);
731
732 /* Gather the argument's tokens. */
733 depth = 0;
734 for (;;)
735 {
736 char *start = src->text;
737
738 if (! get_token (&tok, src))
8a3fe4f8 739 error (_("Malformed argument list for macro `%s'."), name);
ec2bcbe7
JB
740
741 /* Is tok an opening paren? */
742 if (tok.len == 1 && tok.text[0] == '(')
743 depth++;
744
745 /* Is tok is a closing paren? */
746 else if (tok.len == 1 && tok.text[0] == ')')
747 {
748 /* If it's a closing paren at the top level, then that's
749 the end of the argument list. */
750 if (depth == 0)
751 {
752 discard_cleanups (back_to);
753 *argc_p = args_len;
754 return args;
755 }
756
757 depth--;
758 }
759
760 /* If tok is a comma at top level, then that's the end of
761 the current argument. */
762 else if (tok.len == 1 && tok.text[0] == ',' && depth == 0)
763 break;
764
765 /* Extend the current argument to enclose this token. If
766 this is the current argument's first token, leave out any
767 leading whitespace, just for aesthetics. */
768 if (arg->len == 0)
769 {
770 arg->text = tok.text;
771 arg->len = tok.len;
772 arg->last_token = 0;
773 }
774 else
775 {
776 arg->len = (tok.text + tok.len) - arg->text;
777 arg->last_token = tok.text - arg->text;
778 }
779 }
780 }
781}
782
783
784/* The `expand' and `substitute_args' functions both invoke `scan'
785 recursively, so we need a forward declaration somewhere. */
786static void scan (struct macro_buffer *dest,
787 struct macro_buffer *src,
788 struct macro_name_list *no_loop,
789 macro_lookup_ftype *lookup_func,
790 void *lookup_baton);
791
792
793/* Given the macro definition DEF, being invoked with the actual
794 arguments given by ARGC and ARGV, substitute the arguments into the
795 replacement list, and store the result in DEST.
796
797 If it is necessary to expand macro invocations in one of the
798 arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
799 definitions, and don't expand invocations of the macros listed in
800 NO_LOOP. */
801static void
802substitute_args (struct macro_buffer *dest,
803 struct macro_definition *def,
804 int argc, struct macro_buffer *argv,
805 struct macro_name_list *no_loop,
806 macro_lookup_ftype *lookup_func,
807 void *lookup_baton)
808{
809 /* A macro buffer for the macro's replacement list. */
810 struct macro_buffer replacement_list;
811
812 init_shared_buffer (&replacement_list, (char *) def->replacement,
813 strlen (def->replacement));
814
815 gdb_assert (dest->len == 0);
816 dest->last_token = 0;
817
818 for (;;)
819 {
820 struct macro_buffer tok;
821 char *original_rl_start = replacement_list.text;
822 int substituted = 0;
823
824 /* Find the next token in the replacement list. */
825 if (! get_token (&tok, &replacement_list))
826 break;
827
828 /* Just for aesthetics. If we skipped some whitespace, copy
829 that to DEST. */
830 if (tok.text > original_rl_start)
831 {
832 appendmem (dest, original_rl_start, tok.text - original_rl_start);
833 dest->last_token = dest->len;
834 }
835
836 /* Is this token the stringification operator? */
837 if (tok.len == 1
838 && tok.text[0] == '#')
8a3fe4f8 839 error (_("Stringification is not implemented yet."));
ec2bcbe7
JB
840
841 /* Is this token the splicing operator? */
842 if (tok.len == 2
843 && tok.text[0] == '#'
844 && tok.text[1] == '#')
8a3fe4f8 845 error (_("Token splicing is not implemented yet."));
ec2bcbe7
JB
846
847 /* Is this token an identifier? */
848 if (tok.is_identifier)
849 {
850 int i;
851
852 /* Is it the magic varargs parameter? */
853 if (tok.len == 11
854 && ! memcmp (tok.text, "__VA_ARGS__", 11))
8a3fe4f8 855 error (_("Variable-arity macros not implemented yet."));
ec2bcbe7
JB
856
857 /* Is it one of the parameters? */
858 for (i = 0; i < def->argc; i++)
859 if (tok.len == strlen (def->argv[i])
860 && ! memcmp (tok.text, def->argv[i], tok.len))
861 {
862 struct macro_buffer arg_src;
863
864 /* Expand any macro invocations in the argument text,
865 and append the result to dest. Remember that scan
866 mutates its source, so we need to scan a new buffer
867 referring to the argument's text, not the argument
868 itself. */
869 init_shared_buffer (&arg_src, argv[i].text, argv[i].len);
870 scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
871 substituted = 1;
872 break;
873 }
874 }
875
876 /* If it wasn't a parameter, then just copy it across. */
877 if (! substituted)
878 append_tokens_without_splicing (dest, &tok);
879 }
880}
881
882
883/* Expand a call to a macro named ID, whose definition is DEF. Append
884 its expansion to DEST. SRC is the input text following the ID
885 token. We are currently rescanning the expansions of the macros
886 named in NO_LOOP; don't re-expand them. Use LOOKUP_FUNC and
887 LOOKUP_BATON to find definitions for any nested macro references.
888
889 Return 1 if we decided to expand it, zero otherwise. (If it's a
890 function-like macro name that isn't followed by an argument list,
891 we don't expand it.) If we return zero, leave SRC unchanged. */
892static int
893expand (const char *id,
894 struct macro_definition *def,
895 struct macro_buffer *dest,
896 struct macro_buffer *src,
897 struct macro_name_list *no_loop,
898 macro_lookup_ftype *lookup_func,
899 void *lookup_baton)
900{
901 struct macro_name_list new_no_loop;
902
903 /* Create a new node to be added to the front of the no-expand list.
904 This list is appropriate for re-scanning replacement lists, but
905 it is *not* appropriate for scanning macro arguments; invocations
906 of the macro whose arguments we are gathering *do* get expanded
907 there. */
908 new_no_loop.name = id;
909 new_no_loop.next = no_loop;
910
911 /* What kind of macro are we expanding? */
912 if (def->kind == macro_object_like)
913 {
914 struct macro_buffer replacement_list;
915
916 init_shared_buffer (&replacement_list, (char *) def->replacement,
917 strlen (def->replacement));
918
919 scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
920 return 1;
921 }
922 else if (def->kind == macro_function_like)
923 {
924 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
0a029df5 925 int argc = 0;
a86bc61c 926 struct macro_buffer *argv = NULL;
ec2bcbe7
JB
927 struct macro_buffer substituted;
928 struct macro_buffer substituted_src;
929
930 if (def->argc >= 1
a86bc61c 931 && strcmp (def->argv[def->argc - 1], "...") == 0)
8a3fe4f8 932 error (_("Varargs macros not implemented yet."));
ec2bcbe7
JB
933
934 make_cleanup (free_current_contents, &argv);
935 argv = gather_arguments (id, src, &argc);
936
937 /* If we couldn't find any argument list, then we don't expand
938 this macro. */
939 if (! argv)
940 {
941 do_cleanups (back_to);
942 return 0;
943 }
944
945 /* Check that we're passing an acceptable number of arguments for
946 this macro. */
947 if (argc != def->argc)
948 {
949 /* Remember that a sequence of tokens like "foo()" is a
950 valid invocation of a macro expecting either zero or one
951 arguments. */
952 if (! (argc == 1
953 && argv[0].len == 0
954 && def->argc == 0))
8a3fe4f8
AC
955 error (_("Wrong number of arguments to macro `%s' "
956 "(expected %d, got %d)."),
ec2bcbe7
JB
957 id, def->argc, argc);
958 }
959
960 /* Note that we don't expand macro invocations in the arguments
961 yet --- we let subst_args take care of that. Parameters that
962 appear as operands of the stringifying operator "#" or the
963 splicing operator "##" don't get macro references expanded,
964 so we can't really tell whether it's appropriate to macro-
965 expand an argument until we see how it's being used. */
966 init_buffer (&substituted, 0);
967 make_cleanup (cleanup_macro_buffer, &substituted);
968 substitute_args (&substituted, def, argc, argv, no_loop,
969 lookup_func, lookup_baton);
970
971 /* Now `substituted' is the macro's replacement list, with all
972 argument values substituted into it properly. Re-scan it for
973 macro references, but don't expand invocations of this macro.
974
975 We create a new buffer, `substituted_src', which points into
976 `substituted', and scan that. We can't scan `substituted'
977 itself, since the tokenization process moves the buffer's
978 text pointer around, and we still need to be able to find
979 `substituted's original text buffer after scanning it so we
980 can free it. */
981 init_shared_buffer (&substituted_src, substituted.text, substituted.len);
982 scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);
983
984 do_cleanups (back_to);
985
986 return 1;
987 }
988 else
e2e0b3e5 989 internal_error (__FILE__, __LINE__, _("bad macro definition kind"));
ec2bcbe7
JB
990}
991
992
993/* If the single token in SRC_FIRST followed by the tokens in SRC_REST
994 constitute a macro invokation not forbidden in NO_LOOP, append its
995 expansion to DEST and return non-zero. Otherwise, return zero, and
996 leave DEST unchanged.
997
998 SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
999 SRC_FIRST must be a string built by get_token. */
1000static int
1001maybe_expand (struct macro_buffer *dest,
1002 struct macro_buffer *src_first,
1003 struct macro_buffer *src_rest,
1004 struct macro_name_list *no_loop,
1005 macro_lookup_ftype *lookup_func,
1006 void *lookup_baton)
1007{
1008 gdb_assert (src_first->shared);
1009 gdb_assert (src_rest->shared);
1010 gdb_assert (! dest->shared);
1011
1012 /* Is this token an identifier? */
1013 if (src_first->is_identifier)
1014 {
1015 /* Make a null-terminated copy of it, since that's what our
1016 lookup function expects. */
1017 char *id = xmalloc (src_first->len + 1);
1018 struct cleanup *back_to = make_cleanup (xfree, id);
1019 memcpy (id, src_first->text, src_first->len);
1020 id[src_first->len] = 0;
1021
1022 /* If we're currently re-scanning the result of expanding
1023 this macro, don't expand it again. */
1024 if (! currently_rescanning (no_loop, id))
1025 {
1026 /* Does this identifier have a macro definition in scope? */
1027 struct macro_definition *def = lookup_func (id, lookup_baton);
1028
1029 if (def && expand (id, def, dest, src_rest, no_loop,
1030 lookup_func, lookup_baton))
1031 {
1032 do_cleanups (back_to);
1033 return 1;
1034 }
1035 }
1036
1037 do_cleanups (back_to);
1038 }
1039
1040 return 0;
1041}
1042
1043
1044/* Expand macro references in SRC, appending the results to DEST.
1045 Assume we are re-scanning the result of expanding the macros named
1046 in NO_LOOP, and don't try to re-expand references to them.
1047
1048 SRC must be a shared buffer; DEST must not be one. */
1049static void
1050scan (struct macro_buffer *dest,
1051 struct macro_buffer *src,
1052 struct macro_name_list *no_loop,
1053 macro_lookup_ftype *lookup_func,
1054 void *lookup_baton)
1055{
1056 gdb_assert (src->shared);
1057 gdb_assert (! dest->shared);
1058
1059 for (;;)
1060 {
1061 struct macro_buffer tok;
1062 char *original_src_start = src->text;
1063
1064 /* Find the next token in SRC. */
1065 if (! get_token (&tok, src))
1066 break;
1067
1068 /* Just for aesthetics. If we skipped some whitespace, copy
1069 that to DEST. */
1070 if (tok.text > original_src_start)
1071 {
1072 appendmem (dest, original_src_start, tok.text - original_src_start);
1073 dest->last_token = dest->len;
1074 }
1075
1076 if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton))
1077 /* We didn't end up expanding tok as a macro reference, so
1078 simply append it to dest. */
1079 append_tokens_without_splicing (dest, &tok);
1080 }
1081
1082 /* Just for aesthetics. If there was any trailing whitespace in
1083 src, copy it to dest. */
1084 if (src->len)
1085 {
1086 appendmem (dest, src->text, src->len);
1087 dest->last_token = dest->len;
1088 }
1089}
1090
1091
1092char *
1093macro_expand (const char *source,
1094 macro_lookup_ftype *lookup_func,
1095 void *lookup_func_baton)
1096{
1097 struct macro_buffer src, dest;
1098 struct cleanup *back_to;
1099
1100 init_shared_buffer (&src, (char *) source, strlen (source));
1101
1102 init_buffer (&dest, 0);
1103 dest.last_token = 0;
1104 back_to = make_cleanup (cleanup_macro_buffer, &dest);
1105
1106 scan (&dest, &src, 0, lookup_func, lookup_func_baton);
1107
1108 appendc (&dest, '\0');
1109
1110 discard_cleanups (back_to);
1111 return dest.text;
1112}
1113
1114
1115char *
1116macro_expand_once (const char *source,
1117 macro_lookup_ftype *lookup_func,
1118 void *lookup_func_baton)
1119{
8a3fe4f8 1120 error (_("Expand-once not implemented yet."));
ec2bcbe7
JB
1121}
1122
1123
1124char *
1125macro_expand_next (char **lexptr,
1126 macro_lookup_ftype *lookup_func,
1127 void *lookup_baton)
1128{
1129 struct macro_buffer src, dest, tok;
1130 struct cleanup *back_to;
1131
1132 /* Set up SRC to refer to the input text, pointed to by *lexptr. */
1133 init_shared_buffer (&src, *lexptr, strlen (*lexptr));
1134
1135 /* Set up DEST to receive the expansion, if there is one. */
1136 init_buffer (&dest, 0);
1137 dest.last_token = 0;
1138 back_to = make_cleanup (cleanup_macro_buffer, &dest);
1139
1140 /* Get the text's first preprocessing token. */
1141 if (! get_token (&tok, &src))
1142 {
1143 do_cleanups (back_to);
1144 return 0;
1145 }
1146
1147 /* If it's a macro invocation, expand it. */
1148 if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
1149 {
1150 /* It was a macro invocation! Package up the expansion as a
1151 null-terminated string and return it. Set *lexptr to the
1152 start of the next token in the input. */
1153 appendc (&dest, '\0');
1154 discard_cleanups (back_to);
1155 *lexptr = src.text;
1156 return dest.text;
1157 }
1158 else
1159 {
1160 /* It wasn't a macro invocation. */
1161 do_cleanups (back_to);
1162 return 0;
1163 }
1164}