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