1 /* Pascal language support routines for GDB, the GNU debugger.
3 Copyright 2000, 2002, 2003, 2004, 2005 Free Software Foundation,
6 This file is part of GDB.
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.
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.
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
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is derived from c-lang.c */
25 #include "gdb_string.h"
28 #include "expression.h"
29 #include "parser-defs.h"
36 extern void _initialize_pascal_language (void);
39 /* Determines if type TYPE is a pascal string type.
40 Returns 1 if the type is a known pascal type
41 This function is used by p-valprint.c code to allow better string display.
42 If it is a pascal string type, then it also sets info needed
43 to get the length and the data of the string
44 length_pos, length_size and string_pos are given in bytes.
45 char_size gives the element size in bytes.
46 FIXME: if the position or the size of these fields
47 are not multiple of TARGET_CHAR_BIT then the results are wrong
48 but this does not happen for Free Pascal nor for GPC. */
50 is_pascal_string_type (struct type
*type
,int *length_pos
,
51 int *length_size
, int *string_pos
, int *char_size
,
54 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
56 /* Old Borland type pascal strings from Free Pascal Compiler. */
57 /* Two fields: length and st. */
58 if (TYPE_NFIELDS (type
) == 2
59 && strcmp (TYPE_FIELDS (type
)[0].name
, "length") == 0
60 && strcmp (TYPE_FIELDS (type
)[1].name
, "st") == 0)
63 *length_pos
= TYPE_FIELD_BITPOS (type
, 0) / TARGET_CHAR_BIT
;
65 *length_size
= TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 0));
67 *string_pos
= TYPE_FIELD_BITPOS (type
, 1) / TARGET_CHAR_BIT
;
71 *arrayname
= TYPE_FIELDS (type
)[1].name
;
74 /* GNU pascal strings. */
75 /* Three fields: Capacity, length and schema$ or _p_schema. */
76 if (TYPE_NFIELDS (type
) == 3
77 && strcmp (TYPE_FIELDS (type
)[0].name
, "Capacity") == 0
78 && strcmp (TYPE_FIELDS (type
)[1].name
, "length") == 0)
81 *length_pos
= TYPE_FIELD_BITPOS (type
, 1) / TARGET_CHAR_BIT
;
83 *length_size
= TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 1));
85 *string_pos
= TYPE_FIELD_BITPOS (type
, 2) / TARGET_CHAR_BIT
;
86 /* FIXME: how can I detect wide chars in GPC ?? */
90 *arrayname
= TYPE_FIELDS (type
)[2].name
;
97 static void pascal_one_char (int, struct ui_file
*, int *);
99 /* Print the character C on STREAM as part of the contents of a literal
101 In_quotes is reset to 0 if a char is written with #4 notation */
104 pascal_one_char (int c
, struct ui_file
*stream
, int *in_quotes
)
107 c
&= 0xFF; /* Avoid sign bit follies */
109 if ((c
== '\'') || (PRINT_LITERAL_FORM (c
)))
112 fputs_filtered ("'", stream
);
116 fputs_filtered ("''", stream
);
119 fprintf_filtered (stream
, "%c", c
);
124 fputs_filtered ("'", stream
);
126 fprintf_filtered (stream
, "#%d", (unsigned int) c
);
130 static void pascal_emit_char (int c
, struct ui_file
*stream
, int quoter
);
132 /* Print the character C on STREAM as part of the contents of a literal
133 string whose delimiter is QUOTER. Note that that format for printing
134 characters and strings is language specific. */
137 pascal_emit_char (int c
, struct ui_file
*stream
, int quoter
)
140 pascal_one_char (c
, stream
, &in_quotes
);
142 fputs_filtered ("'", stream
);
146 pascal_printchar (int c
, struct ui_file
*stream
)
149 pascal_one_char (c
, stream
, &in_quotes
);
151 fputs_filtered ("'", stream
);
154 /* Print the character string STRING, printing at most LENGTH characters.
155 Printing stops early if the number hits print_max; repeat counts
156 are printed as appropriate. Print ellipses at the end if we
157 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
160 pascal_printstr (struct ui_file
*stream
, const bfd_byte
*string
,
161 unsigned int length
, int width
, int force_ellipses
)
164 unsigned int things_printed
= 0;
168 /* If the string was not truncated due to `set print elements', and
169 the last byte of it is a null, we don't print that, in traditional C
171 if ((!force_ellipses
) && length
> 0 && string
[length
- 1] == '\0')
176 fputs_filtered ("''", stream
);
180 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
182 /* Position of the character we are examining
183 to see whether it is repeated. */
185 /* Number of repetitions we have detected so far. */
192 fputs_filtered (", ", stream
);
198 while (rep1
< length
&& string
[rep1
] == string
[i
])
204 if (reps
> repeat_count_threshold
)
209 fputs_filtered ("\\', ", stream
);
211 fputs_filtered ("', ", stream
);
214 pascal_printchar (string
[i
], stream
);
215 fprintf_filtered (stream
, " <repeats %u times>", reps
);
217 things_printed
+= repeat_count_threshold
;
223 if ((!in_quotes
) && (PRINT_LITERAL_FORM (c
)))
226 fputs_filtered ("\\'", stream
);
228 fputs_filtered ("'", stream
);
231 pascal_one_char (c
, stream
, &in_quotes
);
236 /* Terminate the quotes if necessary. */
240 fputs_filtered ("\\'", stream
);
242 fputs_filtered ("'", stream
);
245 if (force_ellipses
|| i
< length
)
246 fputs_filtered ("...", stream
);
249 /* Create a fundamental Pascal type using default reasonable for the current
252 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
253 define fundamental types such as "int" or "double". Others (stabs or
254 DWARF version 2, etc) do define fundamental types. For the formats which
255 don't provide fundamental types, gdb can create such types using this
258 FIXME: Some compilers distinguish explicitly signed integral types
259 (signed short, signed int, signed long) from "regular" integral types
260 (short, int, long) in the debugging information. There is some dis-
261 agreement as to how useful this feature is. In particular, gcc does
262 not support this. Also, only some debugging formats allow the
263 distinction to be passed on to a debugger. For now, we always just
264 use "short", "int", or "long" as the type name, for both the implicit
265 and explicitly signed types. This also makes life easier for the
266 gdb test suite since we don't have to account for the differences
267 in output depending upon what the compiler and debugging format
268 support. We will probably have to re-examine the issue when gdb
269 starts taking it's fundamental type information directly from the
270 debugging information supplied by the compiler. fnf@cygnus.com */
272 /* Note there might be some discussion about the choosen correspondance
273 because it mainly reflects Free Pascal Compiler setup for now PM */
277 pascal_create_fundamental_type (struct objfile
*objfile
, int typeid)
279 struct type
*type
= NULL
;
284 /* FIXME: For now, if we are asked to produce a type not in this
285 language, create the equivalent of a C integer type with the
286 name "<?type?>". When all the dust settles from the type
287 reconstruction work, this should probably become an error. */
288 type
= init_type (TYPE_CODE_INT
,
289 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
290 0, "<?type?>", objfile
);
291 warning (_("internal error: no Pascal fundamental type %d"), typeid);
294 type
= init_type (TYPE_CODE_VOID
,
295 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
299 type
= init_type (TYPE_CODE_CHAR
,
300 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
304 type
= init_type (TYPE_CODE_INT
,
305 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
306 0, "shortint", objfile
);
308 case FT_UNSIGNED_CHAR
:
309 type
= init_type (TYPE_CODE_INT
,
310 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
311 TYPE_FLAG_UNSIGNED
, "byte", objfile
);
314 type
= init_type (TYPE_CODE_INT
,
315 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
316 0, "integer", objfile
);
318 case FT_SIGNED_SHORT
:
319 type
= init_type (TYPE_CODE_INT
,
320 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
321 0, "integer", objfile
); /* FIXME-fnf */
323 case FT_UNSIGNED_SHORT
:
324 type
= init_type (TYPE_CODE_INT
,
325 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
326 TYPE_FLAG_UNSIGNED
, "word", objfile
);
329 type
= init_type (TYPE_CODE_INT
,
330 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
331 0, "longint", objfile
);
333 case FT_SIGNED_INTEGER
:
334 type
= init_type (TYPE_CODE_INT
,
335 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
336 0, "longint", objfile
); /* FIXME -fnf */
338 case FT_UNSIGNED_INTEGER
:
339 type
= init_type (TYPE_CODE_INT
,
340 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
341 TYPE_FLAG_UNSIGNED
, "cardinal", objfile
);
344 type
= init_type (TYPE_CODE_INT
,
345 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
349 type
= init_type (TYPE_CODE_INT
,
350 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
351 0, "long", objfile
); /* FIXME -fnf */
353 case FT_UNSIGNED_LONG
:
354 type
= init_type (TYPE_CODE_INT
,
355 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
356 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
359 type
= init_type (TYPE_CODE_INT
,
360 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
361 0, "long long", objfile
);
363 case FT_SIGNED_LONG_LONG
:
364 type
= init_type (TYPE_CODE_INT
,
365 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
366 0, "signed long long", objfile
);
368 case FT_UNSIGNED_LONG_LONG
:
369 type
= init_type (TYPE_CODE_INT
,
370 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
371 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
374 type
= init_type (TYPE_CODE_FLT
,
375 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
376 0, "float", objfile
);
378 case FT_DBL_PREC_FLOAT
:
379 type
= init_type (TYPE_CODE_FLT
,
380 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
381 0, "double", objfile
);
383 case FT_EXT_PREC_FLOAT
:
384 type
= init_type (TYPE_CODE_FLT
,
385 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
386 0, "extended", objfile
);
393 /* Table mapping opcodes into strings for printing operators
394 and precedences of the operators. */
396 const struct op_print pascal_op_print_tab
[] =
398 {",", BINOP_COMMA
, PREC_COMMA
, 0},
399 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
400 {"or", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
401 {"xor", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
402 {"and", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
403 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
404 {"<>", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
405 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
406 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
407 {">", BINOP_GTR
, PREC_ORDER
, 0},
408 {"<", BINOP_LESS
, PREC_ORDER
, 0},
409 {"shr", BINOP_RSH
, PREC_SHIFT
, 0},
410 {"shl", BINOP_LSH
, PREC_SHIFT
, 0},
411 {"+", BINOP_ADD
, PREC_ADD
, 0},
412 {"-", BINOP_SUB
, PREC_ADD
, 0},
413 {"*", BINOP_MUL
, PREC_MUL
, 0},
414 {"/", BINOP_DIV
, PREC_MUL
, 0},
415 {"div", BINOP_INTDIV
, PREC_MUL
, 0},
416 {"mod", BINOP_REM
, PREC_MUL
, 0},
417 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
418 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
419 {"not", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
420 {"^", UNOP_IND
, PREC_SUFFIX
, 1},
421 {"@", UNOP_ADDR
, PREC_PREFIX
, 0},
422 {"sizeof", UNOP_SIZEOF
, PREC_PREFIX
, 0},
426 struct type
**const (pascal_builtin_types
[]) =
433 &builtin_type_double
,
435 &builtin_type_long_long
,
436 &builtin_type_signed_char
,
437 &builtin_type_unsigned_char
,
438 &builtin_type_unsigned_short
,
439 &builtin_type_unsigned_int
,
440 &builtin_type_unsigned_long
,
441 &builtin_type_unsigned_long_long
,
442 &builtin_type_long_double
,
443 &builtin_type_complex
,
444 &builtin_type_double_complex
,
448 const struct language_defn pascal_language_defn
=
450 "pascal", /* Language name */
452 pascal_builtin_types
,
457 &exp_descriptor_standard
,
461 pascal_printchar
, /* Print a character constant */
462 pascal_printstr
, /* Function to print string constant */
463 pascal_emit_char
, /* Print a single char */
464 pascal_create_fundamental_type
, /* Create fundamental type in this language */
465 pascal_print_type
, /* Print a type using appropriate syntax */
466 pascal_val_print
, /* Print a value using appropriate syntax */
467 pascal_value_print
, /* Print a top-level value */
468 NULL
, /* Language specific skip_trampoline */
469 value_of_this
, /* value_of_this */
470 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
471 basic_lookup_transparent_type
,/* lookup_transparent_type */
472 NULL
, /* Language specific symbol demangler */
473 NULL
, /* Language specific class_name_from_physname */
474 pascal_op_print_tab
, /* expression operators for printing */
475 1, /* c-style arrays */
476 0, /* String lower bound */
477 &builtin_type_char
, /* Type of string elements */
478 default_word_break_characters
,
479 NULL
, /* FIXME: la_language_arch_info. */
484 _initialize_pascal_language (void)
486 add_language (&pascal_language_defn
);