]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/p-lang.c
2003-05-15 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / p-lang.c
1 /* Pascal language support routines for GDB, the GNU debugger.
2 Copyright 2000, 2002 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* This file is derived from c-lang.c */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "parser-defs.h"
28 #include "language.h"
29 #include "p-lang.h"
30 #include "valprint.h"
31 #include <ctype.h>
32
33 extern void _initialize_pascal_language (void);
34
35
36 /* Determines if type TYPE is a pascal string type.
37 Returns 1 if the type is a known pascal type
38 This function is used by p-valprint.c code to allow better string display.
39 If it is a pascal string type, then it also sets info needed
40 to get the length and the data of the string
41 length_pos, length_size and string_pos are given in bytes.
42 char_size gives the element size in bytes.
43 FIXME: if the position or the size of these fields
44 are not multiple of TARGET_CHAR_BIT then the results are wrong
45 but this does not happen for Free Pascal nor for GPC. */
46 int
47 is_pascal_string_type (struct type *type,int *length_pos,
48 int *length_size, int *string_pos, int *char_size,
49 char **arrayname)
50 {
51 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
52 {
53 /* Old Borland type pascal strings from Free Pascal Compiler. */
54 /* Two fields: length and st. */
55 if (TYPE_NFIELDS (type) == 2
56 && strcmp (TYPE_FIELDS (type)[0].name, "length") == 0
57 && strcmp (TYPE_FIELDS (type)[1].name, "st") == 0)
58 {
59 if (length_pos)
60 *length_pos = TYPE_FIELD_BITPOS (type, 0) / TARGET_CHAR_BIT;
61 if (length_size)
62 *length_size = TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
63 if (string_pos)
64 *string_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
65 if (char_size)
66 *char_size = 1;
67 if (arrayname)
68 *arrayname = TYPE_FIELDS (type)[1].name;
69 return 2;
70 };
71 /* GNU pascal strings. */
72 /* Three fields: Capacity, length and schema$ or _p_schema. */
73 if (TYPE_NFIELDS (type) == 3
74 && strcmp (TYPE_FIELDS (type)[0].name, "Capacity") == 0
75 && strcmp (TYPE_FIELDS (type)[1].name, "length") == 0)
76 {
77 if (length_pos)
78 *length_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
79 if (length_size)
80 *length_size = TYPE_LENGTH (TYPE_FIELD_TYPE (type, 1));
81 if (string_pos)
82 *string_pos = TYPE_FIELD_BITPOS (type, 2) / TARGET_CHAR_BIT;
83 /* FIXME: how can I detect wide chars in GPC ?? */
84 if (char_size)
85 *char_size = 1;
86 if (arrayname)
87 *arrayname = TYPE_FIELDS (type)[2].name;
88 return 3;
89 };
90 }
91 return 0;
92 }
93
94 static void pascal_one_char (int, struct ui_file *, int *);
95
96 /* Print the character C on STREAM as part of the contents of a literal
97 string.
98 In_quotes is reset to 0 if a char is written with #4 notation */
99
100 static void
101 pascal_one_char (register int c, struct ui_file *stream, int *in_quotes)
102 {
103
104 c &= 0xFF; /* Avoid sign bit follies */
105
106 if ((c == '\'') || (PRINT_LITERAL_FORM (c)))
107 {
108 if (!(*in_quotes))
109 fputs_filtered ("'", stream);
110 *in_quotes = 1;
111 if (c == '\'')
112 {
113 fputs_filtered ("''", stream);
114 }
115 else
116 fprintf_filtered (stream, "%c", c);
117 }
118 else
119 {
120 if (*in_quotes)
121 fputs_filtered ("'", stream);
122 *in_quotes = 0;
123 fprintf_filtered (stream, "#%d", (unsigned int) c);
124 }
125 }
126
127 static void pascal_emit_char (int c, struct ui_file *stream, int quoter);
128
129 /* Print the character C on STREAM as part of the contents of a literal
130 string whose delimiter is QUOTER. Note that that format for printing
131 characters and strings is language specific. */
132
133 static void
134 pascal_emit_char (register int c, struct ui_file *stream, int quoter)
135 {
136 int in_quotes = 0;
137 pascal_one_char (c, stream, &in_quotes);
138 if (in_quotes)
139 fputs_filtered ("'", stream);
140 }
141
142 void
143 pascal_printchar (int c, struct ui_file *stream)
144 {
145 int in_quotes = 0;
146 pascal_one_char (c, stream, &in_quotes);
147 if (in_quotes)
148 fputs_filtered ("'", stream);
149 }
150
151 /* Print the character string STRING, printing at most LENGTH characters.
152 Printing stops early if the number hits print_max; repeat counts
153 are printed as appropriate. Print ellipses at the end if we
154 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
155
156 void
157 pascal_printstr (struct ui_file *stream, char *string, unsigned int length,
158 int width, int force_ellipses)
159 {
160 register unsigned int i;
161 unsigned int things_printed = 0;
162 int in_quotes = 0;
163 int need_comma = 0;
164
165 /* If the string was not truncated due to `set print elements', and
166 the last byte of it is a null, we don't print that, in traditional C
167 style. */
168 if ((!force_ellipses) && length > 0 && string[length - 1] == '\0')
169 length--;
170
171 if (length == 0)
172 {
173 fputs_filtered ("''", stream);
174 return;
175 }
176
177 for (i = 0; i < length && things_printed < print_max; ++i)
178 {
179 /* Position of the character we are examining
180 to see whether it is repeated. */
181 unsigned int rep1;
182 /* Number of repetitions we have detected so far. */
183 unsigned int reps;
184
185 QUIT;
186
187 if (need_comma)
188 {
189 fputs_filtered (", ", stream);
190 need_comma = 0;
191 }
192
193 rep1 = i + 1;
194 reps = 1;
195 while (rep1 < length && string[rep1] == string[i])
196 {
197 ++rep1;
198 ++reps;
199 }
200
201 if (reps > repeat_count_threshold)
202 {
203 if (in_quotes)
204 {
205 if (inspect_it)
206 fputs_filtered ("\\', ", stream);
207 else
208 fputs_filtered ("', ", stream);
209 in_quotes = 0;
210 }
211 pascal_printchar (string[i], stream);
212 fprintf_filtered (stream, " <repeats %u times>", reps);
213 i = rep1 - 1;
214 things_printed += repeat_count_threshold;
215 need_comma = 1;
216 }
217 else
218 {
219 int c = string[i];
220 if ((!in_quotes) && (PRINT_LITERAL_FORM (c)))
221 {
222 if (inspect_it)
223 fputs_filtered ("\\'", stream);
224 else
225 fputs_filtered ("'", stream);
226 in_quotes = 1;
227 }
228 pascal_one_char (c, stream, &in_quotes);
229 ++things_printed;
230 }
231 }
232
233 /* Terminate the quotes if necessary. */
234 if (in_quotes)
235 {
236 if (inspect_it)
237 fputs_filtered ("\\'", stream);
238 else
239 fputs_filtered ("'", stream);
240 }
241
242 if (force_ellipses || i < length)
243 fputs_filtered ("...", stream);
244 }
245
246 /* Create a fundamental Pascal type using default reasonable for the current
247 target machine.
248
249 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
250 define fundamental types such as "int" or "double". Others (stabs or
251 DWARF version 2, etc) do define fundamental types. For the formats which
252 don't provide fundamental types, gdb can create such types using this
253 function.
254
255 FIXME: Some compilers distinguish explicitly signed integral types
256 (signed short, signed int, signed long) from "regular" integral types
257 (short, int, long) in the debugging information. There is some dis-
258 agreement as to how useful this feature is. In particular, gcc does
259 not support this. Also, only some debugging formats allow the
260 distinction to be passed on to a debugger. For now, we always just
261 use "short", "int", or "long" as the type name, for both the implicit
262 and explicitly signed types. This also makes life easier for the
263 gdb test suite since we don't have to account for the differences
264 in output depending upon what the compiler and debugging format
265 support. We will probably have to re-examine the issue when gdb
266 starts taking it's fundamental type information directly from the
267 debugging information supplied by the compiler. fnf@cygnus.com */
268
269 /* Note there might be some discussion about the choosen correspondance
270 because it mainly reflects Free Pascal Compiler setup for now PM */
271
272
273 struct type *
274 pascal_create_fundamental_type (struct objfile *objfile, int typeid)
275 {
276 register struct type *type = NULL;
277
278 switch (typeid)
279 {
280 default:
281 /* FIXME: For now, if we are asked to produce a type not in this
282 language, create the equivalent of a C integer type with the
283 name "<?type?>". When all the dust settles from the type
284 reconstruction work, this should probably become an error. */
285 type = init_type (TYPE_CODE_INT,
286 TARGET_INT_BIT / TARGET_CHAR_BIT,
287 0, "<?type?>", objfile);
288 warning ("internal error: no Pascal fundamental type %d", typeid);
289 break;
290 case FT_VOID:
291 type = init_type (TYPE_CODE_VOID,
292 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
293 0, "void", objfile);
294 break;
295 case FT_CHAR:
296 type = init_type (TYPE_CODE_CHAR,
297 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
298 0, "char", objfile);
299 break;
300 case FT_SIGNED_CHAR:
301 type = init_type (TYPE_CODE_INT,
302 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
303 0, "shortint", objfile);
304 break;
305 case FT_UNSIGNED_CHAR:
306 type = init_type (TYPE_CODE_INT,
307 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
308 TYPE_FLAG_UNSIGNED, "byte", objfile);
309 break;
310 case FT_SHORT:
311 type = init_type (TYPE_CODE_INT,
312 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
313 0, "integer", objfile);
314 break;
315 case FT_SIGNED_SHORT:
316 type = init_type (TYPE_CODE_INT,
317 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
318 0, "integer", objfile); /* FIXME-fnf */
319 break;
320 case FT_UNSIGNED_SHORT:
321 type = init_type (TYPE_CODE_INT,
322 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
323 TYPE_FLAG_UNSIGNED, "word", objfile);
324 break;
325 case FT_INTEGER:
326 type = init_type (TYPE_CODE_INT,
327 TARGET_INT_BIT / TARGET_CHAR_BIT,
328 0, "longint", objfile);
329 break;
330 case FT_SIGNED_INTEGER:
331 type = init_type (TYPE_CODE_INT,
332 TARGET_INT_BIT / TARGET_CHAR_BIT,
333 0, "longint", objfile); /* FIXME -fnf */
334 break;
335 case FT_UNSIGNED_INTEGER:
336 type = init_type (TYPE_CODE_INT,
337 TARGET_INT_BIT / TARGET_CHAR_BIT,
338 TYPE_FLAG_UNSIGNED, "cardinal", objfile);
339 break;
340 case FT_LONG:
341 type = init_type (TYPE_CODE_INT,
342 TARGET_LONG_BIT / TARGET_CHAR_BIT,
343 0, "long", objfile);
344 break;
345 case FT_SIGNED_LONG:
346 type = init_type (TYPE_CODE_INT,
347 TARGET_LONG_BIT / TARGET_CHAR_BIT,
348 0, "long", objfile); /* FIXME -fnf */
349 break;
350 case FT_UNSIGNED_LONG:
351 type = init_type (TYPE_CODE_INT,
352 TARGET_LONG_BIT / TARGET_CHAR_BIT,
353 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
354 break;
355 case FT_LONG_LONG:
356 type = init_type (TYPE_CODE_INT,
357 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
358 0, "long long", objfile);
359 break;
360 case FT_SIGNED_LONG_LONG:
361 type = init_type (TYPE_CODE_INT,
362 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
363 0, "signed long long", objfile);
364 break;
365 case FT_UNSIGNED_LONG_LONG:
366 type = init_type (TYPE_CODE_INT,
367 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
368 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
369 break;
370 case FT_FLOAT:
371 type = init_type (TYPE_CODE_FLT,
372 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
373 0, "float", objfile);
374 break;
375 case FT_DBL_PREC_FLOAT:
376 type = init_type (TYPE_CODE_FLT,
377 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
378 0, "double", objfile);
379 break;
380 case FT_EXT_PREC_FLOAT:
381 type = init_type (TYPE_CODE_FLT,
382 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
383 0, "extended", objfile);
384 break;
385 }
386 return (type);
387 }
388 \f
389
390 /* Table mapping opcodes into strings for printing operators
391 and precedences of the operators. */
392
393 const struct op_print pascal_op_print_tab[] =
394 {
395 {",", BINOP_COMMA, PREC_COMMA, 0},
396 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
397 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
398 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
399 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
400 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
401 {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
402 {"<=", BINOP_LEQ, PREC_ORDER, 0},
403 {">=", BINOP_GEQ, PREC_ORDER, 0},
404 {">", BINOP_GTR, PREC_ORDER, 0},
405 {"<", BINOP_LESS, PREC_ORDER, 0},
406 {"shr", BINOP_RSH, PREC_SHIFT, 0},
407 {"shl", BINOP_LSH, PREC_SHIFT, 0},
408 {"+", BINOP_ADD, PREC_ADD, 0},
409 {"-", BINOP_SUB, PREC_ADD, 0},
410 {"*", BINOP_MUL, PREC_MUL, 0},
411 {"/", BINOP_DIV, PREC_MUL, 0},
412 {"div", BINOP_INTDIV, PREC_MUL, 0},
413 {"mod", BINOP_REM, PREC_MUL, 0},
414 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
415 {"-", UNOP_NEG, PREC_PREFIX, 0},
416 {"not", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
417 {"^", UNOP_IND, PREC_SUFFIX, 1},
418 {"@", UNOP_ADDR, PREC_PREFIX, 0},
419 {"sizeof", UNOP_SIZEOF, PREC_PREFIX, 0},
420 {NULL, 0, 0, 0}
421 };
422 \f
423 struct type **const (pascal_builtin_types[]) =
424 {
425 &builtin_type_int,
426 &builtin_type_long,
427 &builtin_type_short,
428 &builtin_type_char,
429 &builtin_type_float,
430 &builtin_type_double,
431 &builtin_type_void,
432 &builtin_type_long_long,
433 &builtin_type_signed_char,
434 &builtin_type_unsigned_char,
435 &builtin_type_unsigned_short,
436 &builtin_type_unsigned_int,
437 &builtin_type_unsigned_long,
438 &builtin_type_unsigned_long_long,
439 &builtin_type_long_double,
440 &builtin_type_complex,
441 &builtin_type_double_complex,
442 0
443 };
444
445 const struct language_defn pascal_language_defn =
446 {
447 "pascal", /* Language name */
448 language_pascal,
449 pascal_builtin_types,
450 range_check_on,
451 type_check_on,
452 case_sensitive_on,
453 pascal_parse,
454 pascal_error,
455 evaluate_subexp_standard,
456 pascal_printchar, /* Print a character constant */
457 pascal_printstr, /* Function to print string constant */
458 pascal_emit_char, /* Print a single char */
459 pascal_create_fundamental_type, /* Create fundamental type in this language */
460 pascal_print_type, /* Print a type using appropriate syntax */
461 pascal_val_print, /* Print a value using appropriate syntax */
462 pascal_value_print, /* Print a top-level value */
463 NULL, /* Language specific skip_trampoline */
464 NULL, /* Language specific symbol demangler */
465 {"", "%", "b", ""}, /* Binary format info */
466 {"0%lo", "0", "o", ""}, /* Octal format info */
467 {"%ld", "", "d", ""}, /* Decimal format info */
468 {"$%lx", "$", "x", ""}, /* Hex format info */
469 pascal_op_print_tab, /* expression operators for printing */
470 1, /* c-style arrays */
471 0, /* String lower bound */
472 &builtin_type_char, /* Type of string elements */
473 LANG_MAGIC
474 };
475
476 void
477 _initialize_pascal_language (void)
478 {
479 add_language (&pascal_language_defn);
480 }