xref: /openbsd-src/gnu/usr.bin/binutils/gdb/c-lang.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* C language support routines for GDB, the GNU debugger.
2b725ae77Skettenis    Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003, 2004
3b725ae77Skettenis    Free Software Foundation, Inc.
4e93f7393Sniklas 
5e93f7393Sniklas    This file is part of GDB.
6e93f7393Sniklas 
7e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
8e93f7393Sniklas    it under the terms of the GNU General Public License as published by
9e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
10e93f7393Sniklas    (at your option) any later version.
11e93f7393Sniklas 
12e93f7393Sniklas    This program is distributed in the hope that it will be useful,
13e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
14e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15e93f7393Sniklas    GNU General Public License for more details.
16e93f7393Sniklas 
17e93f7393Sniklas    You should have received a copy of the GNU General Public License
18e93f7393Sniklas    along with this program; if not, write to the Free Software
19b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
20b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
21e93f7393Sniklas 
22e93f7393Sniklas #include "defs.h"
23e93f7393Sniklas #include "symtab.h"
24e93f7393Sniklas #include "gdbtypes.h"
25e93f7393Sniklas #include "expression.h"
26e93f7393Sniklas #include "parser-defs.h"
27e93f7393Sniklas #include "language.h"
28e93f7393Sniklas #include "c-lang.h"
29b725ae77Skettenis #include "valprint.h"
30b725ae77Skettenis #include "macroscope.h"
31b725ae77Skettenis #include "gdb_assert.h"
32b725ae77Skettenis #include "charset.h"
33b725ae77Skettenis #include "gdb_string.h"
34b725ae77Skettenis #include "demangle.h"
35b725ae77Skettenis #include "cp-support.h"
36e93f7393Sniklas 
37b725ae77Skettenis extern void _initialize_c_language (void);
38b725ae77Skettenis static void c_emit_char (int c, struct ui_file * stream, int quoter);
39e93f7393Sniklas 
40e93f7393Sniklas /* Print the character C on STREAM as part of the contents of a literal
41e93f7393Sniklas    string whose delimiter is QUOTER.  Note that that format for printing
42e93f7393Sniklas    characters and strings is language specific. */
43e93f7393Sniklas 
44e93f7393Sniklas static void
c_emit_char(int c,struct ui_file * stream,int quoter)45b725ae77Skettenis c_emit_char (int c, struct ui_file *stream, int quoter)
46e93f7393Sniklas {
47b725ae77Skettenis   const char *escape;
48b725ae77Skettenis   int host_char;
49e93f7393Sniklas 
50e93f7393Sniklas   c &= 0xFF;			/* Avoid sign bit follies */
51e93f7393Sniklas 
52b725ae77Skettenis   escape = c_target_char_has_backslash_escape (c);
53b725ae77Skettenis   if (escape)
54e93f7393Sniklas     {
55b725ae77Skettenis       if (quoter == '"' && strcmp (escape, "0") == 0)
56b725ae77Skettenis 	/* Print nulls embedded in double quoted strings as \000 to
57b725ae77Skettenis 	   prevent ambiguity.  */
58b725ae77Skettenis 	fprintf_filtered (stream, "\\000");
59b725ae77Skettenis       else
60b725ae77Skettenis 	fprintf_filtered (stream, "\\%s", escape);
61e93f7393Sniklas     }
62b725ae77Skettenis   else if (target_char_to_host (c, &host_char)
63b725ae77Skettenis            && host_char_print_literally (host_char))
64b725ae77Skettenis     {
65b725ae77Skettenis       if (host_char == '\\' || host_char == quoter)
66b725ae77Skettenis         fputs_filtered ("\\", stream);
67b725ae77Skettenis       fprintf_filtered (stream, "%c", host_char);
68e93f7393Sniklas     }
69e93f7393Sniklas   else
70e93f7393Sniklas     fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
71e93f7393Sniklas }
72e93f7393Sniklas 
73e93f7393Sniklas void
c_printchar(int c,struct ui_file * stream)74b725ae77Skettenis c_printchar (int c, struct ui_file *stream)
75e93f7393Sniklas {
76b725ae77Skettenis   fputc_filtered ('\'', stream);
77b725ae77Skettenis   LA_EMIT_CHAR (c, stream, '\'');
78b725ae77Skettenis   fputc_filtered ('\'', stream);
79e93f7393Sniklas }
80e93f7393Sniklas 
81e93f7393Sniklas /* Print the character string STRING, printing at most LENGTH characters.
82b725ae77Skettenis    LENGTH is -1 if the string is nul terminated.  Each character is WIDTH bytes
83b725ae77Skettenis    long.  Printing stops early if the number hits print_max; repeat counts are
84b725ae77Skettenis    printed as appropriate.  Print ellipses at the end if we had to stop before
85b725ae77Skettenis    printing LENGTH characters, or if FORCE_ELLIPSES.  */
86e93f7393Sniklas 
87e93f7393Sniklas void
c_printstr(struct ui_file * stream,char * string,unsigned int length,int width,int force_ellipses)88b725ae77Skettenis c_printstr (struct ui_file *stream, char *string, unsigned int length,
89b725ae77Skettenis 	    int width, int force_ellipses)
90e93f7393Sniklas {
91b725ae77Skettenis   unsigned int i;
92e93f7393Sniklas   unsigned int things_printed = 0;
93e93f7393Sniklas   int in_quotes = 0;
94e93f7393Sniklas   int need_comma = 0;
95e93f7393Sniklas 
96e93f7393Sniklas   /* If the string was not truncated due to `set print elements', and
97e93f7393Sniklas      the last byte of it is a null, we don't print that, in traditional C
98e93f7393Sniklas      style.  */
99b725ae77Skettenis   if (!force_ellipses
100b725ae77Skettenis       && length > 0
101b725ae77Skettenis       && (extract_unsigned_integer (string + (length - 1) * width, width)
102b725ae77Skettenis           == '\0'))
103e93f7393Sniklas     length--;
104e93f7393Sniklas 
105e93f7393Sniklas   if (length == 0)
106e93f7393Sniklas     {
107e93f7393Sniklas       fputs_filtered ("\"\"", stream);
108e93f7393Sniklas       return;
109e93f7393Sniklas     }
110e93f7393Sniklas 
111e93f7393Sniklas   for (i = 0; i < length && things_printed < print_max; ++i)
112e93f7393Sniklas     {
113e93f7393Sniklas       /* Position of the character we are examining
114e93f7393Sniklas          to see whether it is repeated.  */
115e93f7393Sniklas       unsigned int rep1;
116e93f7393Sniklas       /* Number of repetitions we have detected so far.  */
117e93f7393Sniklas       unsigned int reps;
118b725ae77Skettenis       unsigned long current_char;
119e93f7393Sniklas 
120e93f7393Sniklas       QUIT;
121e93f7393Sniklas 
122e93f7393Sniklas       if (need_comma)
123e93f7393Sniklas 	{
124e93f7393Sniklas 	  fputs_filtered (", ", stream);
125e93f7393Sniklas 	  need_comma = 0;
126e93f7393Sniklas 	}
127e93f7393Sniklas 
128b725ae77Skettenis       current_char = extract_unsigned_integer (string + i * width, width);
129b725ae77Skettenis 
130e93f7393Sniklas       rep1 = i + 1;
131e93f7393Sniklas       reps = 1;
132b725ae77Skettenis       while (rep1 < length
133b725ae77Skettenis 	     && extract_unsigned_integer (string + rep1 * width, width)
134b725ae77Skettenis 	     == current_char)
135e93f7393Sniklas 	{
136e93f7393Sniklas 	  ++rep1;
137e93f7393Sniklas 	  ++reps;
138e93f7393Sniklas 	}
139e93f7393Sniklas 
140e93f7393Sniklas       if (reps > repeat_count_threshold)
141e93f7393Sniklas 	{
142e93f7393Sniklas 	  if (in_quotes)
143e93f7393Sniklas 	    {
144e93f7393Sniklas 	      if (inspect_it)
145e93f7393Sniklas 		fputs_filtered ("\\\", ", stream);
146e93f7393Sniklas 	      else
147e93f7393Sniklas 		fputs_filtered ("\", ", stream);
148e93f7393Sniklas 	      in_quotes = 0;
149e93f7393Sniklas 	    }
150b725ae77Skettenis 	  LA_PRINT_CHAR (current_char, stream);
151e93f7393Sniklas 	  fprintf_filtered (stream, " <repeats %u times>", reps);
152e93f7393Sniklas 	  i = rep1 - 1;
153e93f7393Sniklas 	  things_printed += repeat_count_threshold;
154e93f7393Sniklas 	  need_comma = 1;
155e93f7393Sniklas 	}
156e93f7393Sniklas       else
157e93f7393Sniklas 	{
158e93f7393Sniklas 	  if (!in_quotes)
159e93f7393Sniklas 	    {
160e93f7393Sniklas 	      if (inspect_it)
161e93f7393Sniklas 		fputs_filtered ("\\\"", stream);
162e93f7393Sniklas 	      else
163e93f7393Sniklas 		fputs_filtered ("\"", stream);
164e93f7393Sniklas 	      in_quotes = 1;
165e93f7393Sniklas 	    }
166b725ae77Skettenis 	  LA_EMIT_CHAR (current_char, stream, '"');
167e93f7393Sniklas 	  ++things_printed;
168e93f7393Sniklas 	}
169e93f7393Sniklas     }
170e93f7393Sniklas 
171e93f7393Sniklas   /* Terminate the quotes if necessary.  */
172e93f7393Sniklas   if (in_quotes)
173e93f7393Sniklas     {
174e93f7393Sniklas       if (inspect_it)
175e93f7393Sniklas 	fputs_filtered ("\\\"", stream);
176e93f7393Sniklas       else
177e93f7393Sniklas 	fputs_filtered ("\"", stream);
178e93f7393Sniklas     }
179e93f7393Sniklas 
180e93f7393Sniklas   if (force_ellipses || i < length)
181e93f7393Sniklas     fputs_filtered ("...", stream);
182e93f7393Sniklas }
183e93f7393Sniklas 
184e93f7393Sniklas /* Create a fundamental C type using default reasonable for the current
185e93f7393Sniklas    target machine.
186e93f7393Sniklas 
187e93f7393Sniklas    Some object/debugging file formats (DWARF version 1, COFF, etc) do not
188e93f7393Sniklas    define fundamental types such as "int" or "double".  Others (stabs or
189e93f7393Sniklas    DWARF version 2, etc) do define fundamental types.  For the formats which
190e93f7393Sniklas    don't provide fundamental types, gdb can create such types using this
191e93f7393Sniklas    function.
192e93f7393Sniklas 
193e93f7393Sniklas    FIXME:  Some compilers distinguish explicitly signed integral types
194e93f7393Sniklas    (signed short, signed int, signed long) from "regular" integral types
195e93f7393Sniklas    (short, int, long) in the debugging information.  There is some dis-
196e93f7393Sniklas    agreement as to how useful this feature is.  In particular, gcc does
197e93f7393Sniklas    not support this.  Also, only some debugging formats allow the
198e93f7393Sniklas    distinction to be passed on to a debugger.  For now, we always just
199e93f7393Sniklas    use "short", "int", or "long" as the type name, for both the implicit
200e93f7393Sniklas    and explicitly signed types.  This also makes life easier for the
201e93f7393Sniklas    gdb test suite since we don't have to account for the differences
202e93f7393Sniklas    in output depending upon what the compiler and debugging format
203e93f7393Sniklas    support.  We will probably have to re-examine the issue when gdb
204e93f7393Sniklas    starts taking it's fundamental type information directly from the
205e93f7393Sniklas    debugging information supplied by the compiler.  fnf@cygnus.com */
206e93f7393Sniklas 
207e93f7393Sniklas struct type *
c_create_fundamental_type(struct objfile * objfile,int typeid)208b725ae77Skettenis c_create_fundamental_type (struct objfile *objfile, int typeid)
209e93f7393Sniklas {
210b725ae77Skettenis   struct type *type = NULL;
211e93f7393Sniklas 
212e93f7393Sniklas   switch (typeid)
213e93f7393Sniklas     {
214e93f7393Sniklas     default:
215e93f7393Sniklas       /* FIXME:  For now, if we are asked to produce a type not in this
216e93f7393Sniklas          language, create the equivalent of a C integer type with the
217e93f7393Sniklas          name "<?type?>".  When all the dust settles from the type
218e93f7393Sniklas          reconstruction work, this should probably become an error. */
219e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
220e93f7393Sniklas 			TARGET_INT_BIT / TARGET_CHAR_BIT,
221e93f7393Sniklas 			0, "<?type?>", objfile);
222e93f7393Sniklas       warning ("internal error: no C/C++ fundamental type %d", typeid);
223e93f7393Sniklas       break;
224e93f7393Sniklas     case FT_VOID:
225e93f7393Sniklas       type = init_type (TYPE_CODE_VOID,
226e93f7393Sniklas 			TARGET_CHAR_BIT / TARGET_CHAR_BIT,
227e93f7393Sniklas 			0, "void", objfile);
228e93f7393Sniklas       break;
229b725ae77Skettenis     case FT_BOOLEAN:
230b725ae77Skettenis       type = init_type (TYPE_CODE_BOOL,
231b725ae77Skettenis 			TARGET_CHAR_BIT / TARGET_CHAR_BIT,
232b725ae77Skettenis 			0, "bool", objfile);
233b725ae77Skettenis       break;
234e93f7393Sniklas     case FT_CHAR:
235e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
236e93f7393Sniklas 			TARGET_CHAR_BIT / TARGET_CHAR_BIT,
237b725ae77Skettenis 			TYPE_FLAG_NOSIGN, "char", objfile);
238e93f7393Sniklas       break;
239e93f7393Sniklas     case FT_SIGNED_CHAR:
240e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
241e93f7393Sniklas 			TARGET_CHAR_BIT / TARGET_CHAR_BIT,
242e93f7393Sniklas 			0, "signed char", objfile);
243e93f7393Sniklas       break;
244e93f7393Sniklas     case FT_UNSIGNED_CHAR:
245e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
246e93f7393Sniklas 			TARGET_CHAR_BIT / TARGET_CHAR_BIT,
247e93f7393Sniklas 			TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
248e93f7393Sniklas       break;
249e93f7393Sniklas     case FT_SHORT:
250e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
251e93f7393Sniklas 			TARGET_SHORT_BIT / TARGET_CHAR_BIT,
252e93f7393Sniklas 			0, "short", objfile);
253e93f7393Sniklas       break;
254e93f7393Sniklas     case FT_SIGNED_SHORT:
255e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
256e93f7393Sniklas 			TARGET_SHORT_BIT / TARGET_CHAR_BIT,
257e93f7393Sniklas 			0, "short", objfile);	/* FIXME-fnf */
258e93f7393Sniklas       break;
259e93f7393Sniklas     case FT_UNSIGNED_SHORT:
260e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
261e93f7393Sniklas 			TARGET_SHORT_BIT / TARGET_CHAR_BIT,
262e93f7393Sniklas 			TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
263e93f7393Sniklas       break;
264e93f7393Sniklas     case FT_INTEGER:
265e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
266e93f7393Sniklas 			TARGET_INT_BIT / TARGET_CHAR_BIT,
267e93f7393Sniklas 			0, "int", objfile);
268e93f7393Sniklas       break;
269e93f7393Sniklas     case FT_SIGNED_INTEGER:
270e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
271e93f7393Sniklas 			TARGET_INT_BIT / TARGET_CHAR_BIT,
272e93f7393Sniklas 			0, "int", objfile);	/* FIXME -fnf */
273e93f7393Sniklas       break;
274e93f7393Sniklas     case FT_UNSIGNED_INTEGER:
275e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
276e93f7393Sniklas 			TARGET_INT_BIT / TARGET_CHAR_BIT,
277e93f7393Sniklas 			TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
278e93f7393Sniklas       break;
279e93f7393Sniklas     case FT_LONG:
280e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
281e93f7393Sniklas 			TARGET_LONG_BIT / TARGET_CHAR_BIT,
282e93f7393Sniklas 			0, "long", objfile);
283e93f7393Sniklas       break;
284e93f7393Sniklas     case FT_SIGNED_LONG:
285e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
286e93f7393Sniklas 			TARGET_LONG_BIT / TARGET_CHAR_BIT,
287e93f7393Sniklas 			0, "long", objfile);	/* FIXME -fnf */
288e93f7393Sniklas       break;
289e93f7393Sniklas     case FT_UNSIGNED_LONG:
290e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
291e93f7393Sniklas 			TARGET_LONG_BIT / TARGET_CHAR_BIT,
292e93f7393Sniklas 			TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
293e93f7393Sniklas       break;
294e93f7393Sniklas     case FT_LONG_LONG:
295e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
296e93f7393Sniklas 			TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
297e93f7393Sniklas 			0, "long long", objfile);
298e93f7393Sniklas       break;
299e93f7393Sniklas     case FT_SIGNED_LONG_LONG:
300e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
301e93f7393Sniklas 			TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
302e93f7393Sniklas 			0, "signed long long", objfile);
303e93f7393Sniklas       break;
304e93f7393Sniklas     case FT_UNSIGNED_LONG_LONG:
305e93f7393Sniklas       type = init_type (TYPE_CODE_INT,
306e93f7393Sniklas 			TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
307e93f7393Sniklas 			TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
308e93f7393Sniklas       break;
309e93f7393Sniklas     case FT_FLOAT:
310e93f7393Sniklas       type = init_type (TYPE_CODE_FLT,
311e93f7393Sniklas 			TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
312e93f7393Sniklas 			0, "float", objfile);
313e93f7393Sniklas       break;
314e93f7393Sniklas     case FT_DBL_PREC_FLOAT:
315e93f7393Sniklas       type = init_type (TYPE_CODE_FLT,
316e93f7393Sniklas 			TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
317e93f7393Sniklas 			0, "double", objfile);
318e93f7393Sniklas       break;
319e93f7393Sniklas     case FT_EXT_PREC_FLOAT:
320e93f7393Sniklas       type = init_type (TYPE_CODE_FLT,
321e93f7393Sniklas 			TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
322e93f7393Sniklas 			0, "long double", objfile);
323e93f7393Sniklas       break;
324b725ae77Skettenis     case FT_COMPLEX:
325b725ae77Skettenis       type = init_type (TYPE_CODE_FLT,
326b725ae77Skettenis 			2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
327b725ae77Skettenis 			0, "complex float", objfile);
328b725ae77Skettenis       TYPE_TARGET_TYPE (type)
329b725ae77Skettenis 	= init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
330b725ae77Skettenis 		     0, "float", objfile);
331b725ae77Skettenis       break;
332b725ae77Skettenis     case FT_DBL_PREC_COMPLEX:
333b725ae77Skettenis       type = init_type (TYPE_CODE_FLT,
334b725ae77Skettenis 			2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
335b725ae77Skettenis 			0, "complex double", objfile);
336b725ae77Skettenis       TYPE_TARGET_TYPE (type)
337b725ae77Skettenis 	= init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
338b725ae77Skettenis 		     0, "double", objfile);
339b725ae77Skettenis       break;
340b725ae77Skettenis     case FT_EXT_PREC_COMPLEX:
341b725ae77Skettenis       type = init_type (TYPE_CODE_FLT,
342b725ae77Skettenis 			2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
343b725ae77Skettenis 			0, "complex long double", objfile);
344b725ae77Skettenis       TYPE_TARGET_TYPE (type)
345b725ae77Skettenis 	= init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
346b725ae77Skettenis 		     0, "long double", objfile);
347b725ae77Skettenis       break;
348b725ae77Skettenis     case FT_TEMPLATE_ARG:
349b725ae77Skettenis       type = init_type (TYPE_CODE_TEMPLATE_ARG,
350b725ae77Skettenis 			0,
351b725ae77Skettenis 			0, "<template arg>", objfile);
352b725ae77Skettenis       break;
353e93f7393Sniklas     }
354e93f7393Sniklas   return (type);
355e93f7393Sniklas }
356b725ae77Skettenis 
357b725ae77Skettenis /* Preprocessing and parsing C and C++ expressions.  */
358b725ae77Skettenis 
359b725ae77Skettenis 
360b725ae77Skettenis /* When we find that lexptr (the global var defined in parse.c) is
361b725ae77Skettenis    pointing at a macro invocation, we expand the invocation, and call
362b725ae77Skettenis    scan_macro_expansion to save the old lexptr here and point lexptr
363b725ae77Skettenis    into the expanded text.  When we reach the end of that, we call
364b725ae77Skettenis    end_macro_expansion to pop back to the value we saved here.  The
365b725ae77Skettenis    macro expansion code promises to return only fully-expanded text,
366b725ae77Skettenis    so we don't need to "push" more than one level.
367b725ae77Skettenis 
368b725ae77Skettenis    This is disgusting, of course.  It would be cleaner to do all macro
369b725ae77Skettenis    expansion beforehand, and then hand that to lexptr.  But we don't
370b725ae77Skettenis    really know where the expression ends.  Remember, in a command like
371b725ae77Skettenis 
372b725ae77Skettenis      (gdb) break *ADDRESS if CONDITION
373b725ae77Skettenis 
374b725ae77Skettenis    we evaluate ADDRESS in the scope of the current frame, but we
375b725ae77Skettenis    evaluate CONDITION in the scope of the breakpoint's location.  So
376b725ae77Skettenis    it's simply wrong to try to macro-expand the whole thing at once.  */
377b725ae77Skettenis static char *macro_original_text;
378b725ae77Skettenis static char *macro_expanded_text;
379b725ae77Skettenis 
380b725ae77Skettenis 
381b725ae77Skettenis void
scan_macro_expansion(char * expansion)382b725ae77Skettenis scan_macro_expansion (char *expansion)
383b725ae77Skettenis {
384b725ae77Skettenis   /* We'd better not be trying to push the stack twice.  */
385b725ae77Skettenis   gdb_assert (! macro_original_text);
386b725ae77Skettenis   gdb_assert (! macro_expanded_text);
387b725ae77Skettenis 
388b725ae77Skettenis   /* Save the old lexptr value, so we can return to it when we're done
389b725ae77Skettenis      parsing the expanded text.  */
390b725ae77Skettenis   macro_original_text = lexptr;
391b725ae77Skettenis   lexptr = expansion;
392b725ae77Skettenis 
393b725ae77Skettenis   /* Save the expanded text, so we can free it when we're finished.  */
394b725ae77Skettenis   macro_expanded_text = expansion;
395b725ae77Skettenis }
396b725ae77Skettenis 
397b725ae77Skettenis 
398b725ae77Skettenis int
scanning_macro_expansion(void)399b725ae77Skettenis scanning_macro_expansion (void)
400b725ae77Skettenis {
401b725ae77Skettenis   return macro_original_text != 0;
402b725ae77Skettenis }
403b725ae77Skettenis 
404b725ae77Skettenis 
405b725ae77Skettenis void
finished_macro_expansion(void)406b725ae77Skettenis finished_macro_expansion (void)
407b725ae77Skettenis {
408b725ae77Skettenis   /* There'd better be something to pop back to, and we better have
409b725ae77Skettenis      saved a pointer to the start of the expanded text.  */
410b725ae77Skettenis   gdb_assert (macro_original_text);
411b725ae77Skettenis   gdb_assert (macro_expanded_text);
412b725ae77Skettenis 
413b725ae77Skettenis   /* Pop back to the original text.  */
414b725ae77Skettenis   lexptr = macro_original_text;
415b725ae77Skettenis   macro_original_text = 0;
416b725ae77Skettenis 
417b725ae77Skettenis   /* Free the expanded text.  */
418b725ae77Skettenis   xfree (macro_expanded_text);
419b725ae77Skettenis   macro_expanded_text = 0;
420b725ae77Skettenis }
421b725ae77Skettenis 
422b725ae77Skettenis 
423b725ae77Skettenis static void
scan_macro_cleanup(void * dummy)424b725ae77Skettenis scan_macro_cleanup (void *dummy)
425b725ae77Skettenis {
426b725ae77Skettenis   if (macro_original_text)
427b725ae77Skettenis     finished_macro_expansion ();
428b725ae77Skettenis }
429b725ae77Skettenis 
430b725ae77Skettenis 
431b725ae77Skettenis /* We set these global variables before calling c_parse, to tell it
432b725ae77Skettenis    how it to find macro definitions for the expression at hand.  */
433b725ae77Skettenis macro_lookup_ftype *expression_macro_lookup_func;
434b725ae77Skettenis void *expression_macro_lookup_baton;
435b725ae77Skettenis 
436b725ae77Skettenis 
437b725ae77Skettenis static struct macro_definition *
null_macro_lookup(const char * name,void * baton)438b725ae77Skettenis null_macro_lookup (const char *name, void *baton)
439b725ae77Skettenis {
440b725ae77Skettenis   return 0;
441b725ae77Skettenis }
442b725ae77Skettenis 
443b725ae77Skettenis 
444b725ae77Skettenis static int
c_preprocess_and_parse(void)445b725ae77Skettenis c_preprocess_and_parse (void)
446b725ae77Skettenis {
447b725ae77Skettenis   /* Set up a lookup function for the macro expander.  */
448b725ae77Skettenis   struct macro_scope *scope = 0;
449b725ae77Skettenis   struct cleanup *back_to = make_cleanup (free_current_contents, &scope);
450b725ae77Skettenis 
451b725ae77Skettenis   if (expression_context_block)
452b725ae77Skettenis     scope = sal_macro_scope (find_pc_line (expression_context_pc, 0));
453b725ae77Skettenis   else
454b725ae77Skettenis     scope = default_macro_scope ();
455b725ae77Skettenis 
456b725ae77Skettenis   if (scope)
457b725ae77Skettenis     {
458b725ae77Skettenis       expression_macro_lookup_func = standard_macro_lookup;
459b725ae77Skettenis       expression_macro_lookup_baton = (void *) scope;
460b725ae77Skettenis     }
461b725ae77Skettenis   else
462b725ae77Skettenis     {
463b725ae77Skettenis       expression_macro_lookup_func = null_macro_lookup;
464b725ae77Skettenis       expression_macro_lookup_baton = 0;
465b725ae77Skettenis     }
466b725ae77Skettenis 
467b725ae77Skettenis   gdb_assert (! macro_original_text);
468b725ae77Skettenis   make_cleanup (scan_macro_cleanup, 0);
469b725ae77Skettenis 
470b725ae77Skettenis   {
471b725ae77Skettenis     int result = c_parse ();
472b725ae77Skettenis     do_cleanups (back_to);
473b725ae77Skettenis     return result;
474b725ae77Skettenis   }
475b725ae77Skettenis }
476b725ae77Skettenis 
477e93f7393Sniklas 
478e93f7393Sniklas 
479e93f7393Sniklas /* Table mapping opcodes into strings for printing operators
480e93f7393Sniklas    and precedences of the operators.  */
481e93f7393Sniklas 
482e93f7393Sniklas const struct op_print c_op_print_tab[] =
483e93f7393Sniklas {
484e93f7393Sniklas   {",", BINOP_COMMA, PREC_COMMA, 0},
485e93f7393Sniklas   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
486e93f7393Sniklas   {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
487e93f7393Sniklas   {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
488e93f7393Sniklas   {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
489e93f7393Sniklas   {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
490e93f7393Sniklas   {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
491e93f7393Sniklas   {"==", BINOP_EQUAL, PREC_EQUAL, 0},
492e93f7393Sniklas   {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
493e93f7393Sniklas   {"<=", BINOP_LEQ, PREC_ORDER, 0},
494e93f7393Sniklas   {">=", BINOP_GEQ, PREC_ORDER, 0},
495e93f7393Sniklas   {">", BINOP_GTR, PREC_ORDER, 0},
496e93f7393Sniklas   {"<", BINOP_LESS, PREC_ORDER, 0},
497e93f7393Sniklas   {">>", BINOP_RSH, PREC_SHIFT, 0},
498e93f7393Sniklas   {"<<", BINOP_LSH, PREC_SHIFT, 0},
499e93f7393Sniklas   {"+", BINOP_ADD, PREC_ADD, 0},
500e93f7393Sniklas   {"-", BINOP_SUB, PREC_ADD, 0},
501e93f7393Sniklas   {"*", BINOP_MUL, PREC_MUL, 0},
502e93f7393Sniklas   {"/", BINOP_DIV, PREC_MUL, 0},
503e93f7393Sniklas   {"%", BINOP_REM, PREC_MUL, 0},
504e93f7393Sniklas   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
505e93f7393Sniklas   {"-", UNOP_NEG, PREC_PREFIX, 0},
506e93f7393Sniklas   {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
507e93f7393Sniklas   {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
508e93f7393Sniklas   {"*", UNOP_IND, PREC_PREFIX, 0},
509e93f7393Sniklas   {"&", UNOP_ADDR, PREC_PREFIX, 0},
510e93f7393Sniklas   {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
511e93f7393Sniklas   {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
512e93f7393Sniklas   {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
513e93f7393Sniklas   {NULL, 0, 0, 0}
514e93f7393Sniklas };
515e93f7393Sniklas 
516*63addd46Skettenis enum c_primitive_types {
517*63addd46Skettenis   c_primitive_type_int,
518*63addd46Skettenis   c_primitive_type_long,
519*63addd46Skettenis   c_primitive_type_short,
520*63addd46Skettenis   c_primitive_type_char,
521*63addd46Skettenis   c_primitive_type_float,
522*63addd46Skettenis   c_primitive_type_double,
523*63addd46Skettenis   c_primitive_type_void,
524*63addd46Skettenis   c_primitive_type_long_long,
525*63addd46Skettenis   c_primitive_type_signed_char,
526*63addd46Skettenis   c_primitive_type_unsigned_char,
527*63addd46Skettenis   c_primitive_type_unsigned_short,
528*63addd46Skettenis   c_primitive_type_unsigned_int,
529*63addd46Skettenis   c_primitive_type_unsigned_long,
530*63addd46Skettenis   c_primitive_type_unsigned_long_long,
531*63addd46Skettenis   c_primitive_type_long_double,
532*63addd46Skettenis   c_primitive_type_complex,
533*63addd46Skettenis   c_primitive_type_double_complex,
534*63addd46Skettenis   nr_c_primitive_types
535*63addd46Skettenis };
536*63addd46Skettenis 
537*63addd46Skettenis void
c_language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)538*63addd46Skettenis c_language_arch_info (struct gdbarch *gdbarch,
539*63addd46Skettenis 		      struct language_arch_info *lai)
540e93f7393Sniklas {
541*63addd46Skettenis   const struct builtin_type *builtin = builtin_type (gdbarch);
542*63addd46Skettenis   lai->string_char_type = builtin->builtin_char;
543*63addd46Skettenis   lai->primitive_type_vector
544*63addd46Skettenis     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
545*63addd46Skettenis 			      struct type *);
546*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
547*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
548*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
549*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
550*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
551*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
552*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
553*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
554*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
555*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
556*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
557*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
558*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
559*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
560*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
561*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
562*63addd46Skettenis   lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
563e93f7393Sniklas };
564e93f7393Sniklas 
565b725ae77Skettenis const struct language_defn c_language_defn =
566b725ae77Skettenis {
567e93f7393Sniklas   "c",				/* Language name */
568e93f7393Sniklas   language_c,
569*63addd46Skettenis   NULL,
570e93f7393Sniklas   range_check_off,
571e93f7393Sniklas   type_check_off,
572b725ae77Skettenis   case_sensitive_on,
573*63addd46Skettenis   array_row_major,
574b725ae77Skettenis   &exp_descriptor_standard,
575b725ae77Skettenis   c_preprocess_and_parse,
576e93f7393Sniklas   c_error,
577*63addd46Skettenis   null_post_parser,
578e93f7393Sniklas   c_printchar,			/* Print a character constant */
579e93f7393Sniklas   c_printstr,			/* Function to print string constant */
580b725ae77Skettenis   c_emit_char,			/* Print a single char */
581e93f7393Sniklas   c_create_fundamental_type,	/* Create fundamental type in this language */
582e93f7393Sniklas   c_print_type,			/* Print a type using appropriate syntax */
583e93f7393Sniklas   c_val_print,			/* Print a value using appropriate syntax */
584e93f7393Sniklas   c_value_print,		/* Print a top-level value */
585b725ae77Skettenis   NULL,				/* Language specific skip_trampoline */
586b725ae77Skettenis   NULL,				/* value_of_this */
587b725ae77Skettenis   basic_lookup_symbol_nonlocal,	/* lookup_symbol_nonlocal */
588b725ae77Skettenis   basic_lookup_transparent_type,/* lookup_transparent_type */
589b725ae77Skettenis   NULL,				/* Language specific symbol demangler */
590*63addd46Skettenis   NULL,				/* Language specific class_name_from_physname */
591e93f7393Sniklas   c_op_print_tab,		/* expression operators for printing */
592e93f7393Sniklas   1,				/* c-style arrays */
593e93f7393Sniklas   0,				/* String lower bound */
594*63addd46Skettenis   NULL,
595b725ae77Skettenis   default_word_break_characters,
596*63addd46Skettenis   c_language_arch_info,
597e93f7393Sniklas   LANG_MAGIC
598e93f7393Sniklas };
599e93f7393Sniklas 
600b725ae77Skettenis struct type **const (cplus_builtin_types[]) =
601b725ae77Skettenis {
602b725ae77Skettenis   &builtin_type_int,
603b725ae77Skettenis   &builtin_type_long,
604b725ae77Skettenis   &builtin_type_short,
605b725ae77Skettenis   &builtin_type_char,
606b725ae77Skettenis   &builtin_type_float,
607b725ae77Skettenis   &builtin_type_double,
608b725ae77Skettenis   &builtin_type_void,
609b725ae77Skettenis   &builtin_type_long_long,
610b725ae77Skettenis   &builtin_type_signed_char,
611b725ae77Skettenis   &builtin_type_unsigned_char,
612b725ae77Skettenis   &builtin_type_unsigned_short,
613b725ae77Skettenis   &builtin_type_unsigned_int,
614b725ae77Skettenis   &builtin_type_unsigned_long,
615b725ae77Skettenis   &builtin_type_unsigned_long_long,
616b725ae77Skettenis   &builtin_type_long_double,
617b725ae77Skettenis   &builtin_type_complex,
618b725ae77Skettenis   &builtin_type_double_complex,
619b725ae77Skettenis   &builtin_type_bool,
620b725ae77Skettenis   0
621b725ae77Skettenis };
622b725ae77Skettenis 
62318f91e6fSdrahn /* without this stub function, alpha attempts to generate a
62418f91e6fSdrahn  * text relocation for the function pointer instead of a PLT reference.
62518f91e6fSdrahn  */
62618f91e6fSdrahn static char *call_cplus_demangle (const char *mangled, int options);
62718f91e6fSdrahn static char *
call_cplus_demangle(const char * mangled,int options)62818f91e6fSdrahn call_cplus_demangle (const char *mangled, int options)
62918f91e6fSdrahn {
63018f91e6fSdrahn 	return cplus_demangle(mangled, options);
63118f91e6fSdrahn }
63218f91e6fSdrahn 
633b725ae77Skettenis const struct language_defn cplus_language_defn =
634b725ae77Skettenis {
635e93f7393Sniklas   "c++",			/* Language name */
636e93f7393Sniklas   language_cplus,
637b725ae77Skettenis   cplus_builtin_types,
638e93f7393Sniklas   range_check_off,
639e93f7393Sniklas   type_check_off,
640b725ae77Skettenis   case_sensitive_on,
641*63addd46Skettenis   array_row_major,
642b725ae77Skettenis   &exp_descriptor_standard,
643b725ae77Skettenis   c_preprocess_and_parse,
644e93f7393Sniklas   c_error,
645*63addd46Skettenis   null_post_parser,
646e93f7393Sniklas   c_printchar,			/* Print a character constant */
647e93f7393Sniklas   c_printstr,			/* Function to print string constant */
648b725ae77Skettenis   c_emit_char,			/* Print a single char */
649e93f7393Sniklas   c_create_fundamental_type,	/* Create fundamental type in this language */
650e93f7393Sniklas   c_print_type,			/* Print a type using appropriate syntax */
651e93f7393Sniklas   c_val_print,			/* Print a value using appropriate syntax */
652e93f7393Sniklas   c_value_print,		/* Print a top-level value */
653b725ae77Skettenis   NULL,				/* Language specific skip_trampoline */
654b725ae77Skettenis   value_of_this,		/* value_of_this */
655b725ae77Skettenis   cp_lookup_symbol_nonlocal,	/* lookup_symbol_nonlocal */
656b725ae77Skettenis   cp_lookup_transparent_type,   /* lookup_transparent_type */
65718f91e6fSdrahn   call_cplus_demangle,		/* Language specific symbol demangler */
658*63addd46Skettenis   cp_class_name_from_physname,  /* Language specific class_name_from_physname */
659e93f7393Sniklas   c_op_print_tab,		/* expression operators for printing */
660e93f7393Sniklas   1,				/* c-style arrays */
661e93f7393Sniklas   0,				/* String lower bound */
662e93f7393Sniklas   &builtin_type_char,		/* Type of string elements */
663b725ae77Skettenis   default_word_break_characters,
664*63addd46Skettenis   NULL, /* FIXME: la_language_arch_info.  */
665e93f7393Sniklas   LANG_MAGIC
666e93f7393Sniklas };
667e93f7393Sniklas 
668b725ae77Skettenis const struct language_defn asm_language_defn =
669b725ae77Skettenis {
670e93f7393Sniklas   "asm",			/* Language name */
671e93f7393Sniklas   language_asm,
672*63addd46Skettenis   NULL,
673e93f7393Sniklas   range_check_off,
674e93f7393Sniklas   type_check_off,
675b725ae77Skettenis   case_sensitive_on,
676*63addd46Skettenis   array_row_major,
677b725ae77Skettenis   &exp_descriptor_standard,
678b725ae77Skettenis   c_preprocess_and_parse,
679e93f7393Sniklas   c_error,
680*63addd46Skettenis   null_post_parser,
681e93f7393Sniklas   c_printchar,			/* Print a character constant */
682e93f7393Sniklas   c_printstr,			/* Function to print string constant */
683b725ae77Skettenis   c_emit_char,			/* Print a single char */
684e93f7393Sniklas   c_create_fundamental_type,	/* Create fundamental type in this language */
685e93f7393Sniklas   c_print_type,			/* Print a type using appropriate syntax */
686e93f7393Sniklas   c_val_print,			/* Print a value using appropriate syntax */
687e93f7393Sniklas   c_value_print,		/* Print a top-level value */
688b725ae77Skettenis   NULL,				/* Language specific skip_trampoline */
689b725ae77Skettenis   NULL,				/* value_of_this */
690b725ae77Skettenis   basic_lookup_symbol_nonlocal,	/* lookup_symbol_nonlocal */
691b725ae77Skettenis   basic_lookup_transparent_type,/* lookup_transparent_type */
692b725ae77Skettenis   NULL,				/* Language specific symbol demangler */
693*63addd46Skettenis   NULL,				/* Language specific class_name_from_physname */
694e93f7393Sniklas   c_op_print_tab,		/* expression operators for printing */
695e93f7393Sniklas   1,				/* c-style arrays */
696e93f7393Sniklas   0,				/* String lower bound */
697*63addd46Skettenis   NULL,
698b725ae77Skettenis   default_word_break_characters,
699*63addd46Skettenis   c_language_arch_info, /* FIXME: la_language_arch_info.  */
700b725ae77Skettenis   LANG_MAGIC
701b725ae77Skettenis };
702b725ae77Skettenis 
703b725ae77Skettenis /* The following language_defn does not represent a real language.
704b725ae77Skettenis    It just provides a minimal support a-la-C that should allow users
705b725ae77Skettenis    to do some simple operations when debugging applications that use
706b725ae77Skettenis    a language currently not supported by GDB.  */
707b725ae77Skettenis 
708b725ae77Skettenis const struct language_defn minimal_language_defn =
709b725ae77Skettenis {
710b725ae77Skettenis   "minimal",			/* Language name */
711b725ae77Skettenis   language_minimal,
712*63addd46Skettenis   NULL,
713b725ae77Skettenis   range_check_off,
714b725ae77Skettenis   type_check_off,
715b725ae77Skettenis   case_sensitive_on,
716*63addd46Skettenis   array_row_major,
717b725ae77Skettenis   &exp_descriptor_standard,
718b725ae77Skettenis   c_preprocess_and_parse,
719b725ae77Skettenis   c_error,
720*63addd46Skettenis   null_post_parser,
721b725ae77Skettenis   c_printchar,			/* Print a character constant */
722b725ae77Skettenis   c_printstr,			/* Function to print string constant */
723b725ae77Skettenis   c_emit_char,			/* Print a single char */
724b725ae77Skettenis   c_create_fundamental_type,	/* Create fundamental type in this language */
725b725ae77Skettenis   c_print_type,			/* Print a type using appropriate syntax */
726b725ae77Skettenis   c_val_print,			/* Print a value using appropriate syntax */
727b725ae77Skettenis   c_value_print,		/* Print a top-level value */
728b725ae77Skettenis   NULL,				/* Language specific skip_trampoline */
729b725ae77Skettenis   NULL,				/* value_of_this */
730b725ae77Skettenis   basic_lookup_symbol_nonlocal,	/* lookup_symbol_nonlocal */
731b725ae77Skettenis   basic_lookup_transparent_type,/* lookup_transparent_type */
732b725ae77Skettenis   NULL,				/* Language specific symbol demangler */
733*63addd46Skettenis   NULL,				/* Language specific class_name_from_physname */
734b725ae77Skettenis   c_op_print_tab,		/* expression operators for printing */
735b725ae77Skettenis   1,				/* c-style arrays */
736b725ae77Skettenis   0,				/* String lower bound */
737*63addd46Skettenis   NULL,
738b725ae77Skettenis   default_word_break_characters,
739*63addd46Skettenis   c_language_arch_info,
740e93f7393Sniklas   LANG_MAGIC
741e93f7393Sniklas };
742e93f7393Sniklas 
743e93f7393Sniklas void
_initialize_c_language(void)744b725ae77Skettenis _initialize_c_language (void)
745e93f7393Sniklas {
746e93f7393Sniklas   add_language (&c_language_defn);
747e93f7393Sniklas   add_language (&cplus_language_defn);
748e93f7393Sniklas   add_language (&asm_language_defn);
749b725ae77Skettenis   add_language (&minimal_language_defn);
750e93f7393Sniklas }
751