gdb: Convert language_data::string_lower_bound to a method
[binutils-gdb.git] / gdb / p-lang.c
1 /* Pascal language support routines for GDB, the GNU debugger.
2
3 Copyright (C) 2000-2020 Free Software Foundation, 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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* This file is derived from c-lang.c */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "parser-defs.h"
27 #include "language.h"
28 #include "varobj.h"
29 #include "p-lang.h"
30 #include "valprint.h"
31 #include "value.h"
32 #include <ctype.h>
33 #include "c-lang.h"
34 #include "gdbarch.h"
35 #include "cli/cli-style.h"
36
37 /* All GPC versions until now (2007-09-27) also define a symbol called
38 '_p_initialize'. Check for the presence of this symbol first. */
39 static const char GPC_P_INITIALIZE[] = "_p_initialize";
40
41 /* The name of the symbol that GPC uses as the name of the main
42 procedure (since version 20050212). */
43 static const char GPC_MAIN_PROGRAM_NAME_1[] = "_p__M0_main_program";
44
45 /* Older versions of GPC (versions older than 20050212) were using
46 a different name for the main procedure. */
47 static const char GPC_MAIN_PROGRAM_NAME_2[] = "pascal_main_program";
48
49 /* Function returning the special symbol name used
50 by GPC for the main procedure in the main program
51 if it is found in minimal symbol list.
52 This function tries to find minimal symbols generated by GPC
53 so that it finds the even if the program was compiled
54 without debugging information.
55 According to information supplied by Waldeck Hebisch,
56 this should work for all versions posterior to June 2000. */
57
58 const char *
59 pascal_main_name (void)
60 {
61 struct bound_minimal_symbol msym;
62
63 msym = lookup_minimal_symbol (GPC_P_INITIALIZE, NULL, NULL);
64
65 /* If '_p_initialize' was not found, the main program is likely not
66 written in Pascal. */
67 if (msym.minsym == NULL)
68 return NULL;
69
70 msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_1, NULL, NULL);
71 if (msym.minsym != NULL)
72 {
73 return GPC_MAIN_PROGRAM_NAME_1;
74 }
75
76 msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_2, NULL, NULL);
77 if (msym.minsym != NULL)
78 {
79 return GPC_MAIN_PROGRAM_NAME_2;
80 }
81
82 /* No known entry procedure found, the main program is probably
83 not compiled with GPC. */
84 return NULL;
85 }
86
87 /* Determines if type TYPE is a pascal string type.
88 Returns a positive value if the type is a known pascal string type.
89 This function is used by p-valprint.c code to allow better string display.
90 If it is a pascal string type, then it also sets info needed
91 to get the length and the data of the string
92 length_pos, length_size and string_pos are given in bytes.
93 char_size gives the element size in bytes.
94 FIXME: if the position or the size of these fields
95 are not multiple of TARGET_CHAR_BIT then the results are wrong
96 but this does not happen for Free Pascal nor for GPC. */
97 int
98 is_pascal_string_type (struct type *type,int *length_pos,
99 int *length_size, int *string_pos,
100 struct type **char_type,
101 const char **arrayname)
102 {
103 if (type != NULL && type->code () == TYPE_CODE_STRUCT)
104 {
105 /* Old Borland type pascal strings from Free Pascal Compiler. */
106 /* Two fields: length and st. */
107 if (type->num_fields () == 2
108 && TYPE_FIELD_NAME (type, 0)
109 && strcmp (TYPE_FIELD_NAME (type, 0), "length") == 0
110 && TYPE_FIELD_NAME (type, 1)
111 && strcmp (TYPE_FIELD_NAME (type, 1), "st") == 0)
112 {
113 if (length_pos)
114 *length_pos = TYPE_FIELD_BITPOS (type, 0) / TARGET_CHAR_BIT;
115 if (length_size)
116 *length_size = TYPE_LENGTH (type->field (0).type ());
117 if (string_pos)
118 *string_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
119 if (char_type)
120 *char_type = TYPE_TARGET_TYPE (type->field (1).type ());
121 if (arrayname)
122 *arrayname = TYPE_FIELD_NAME (type, 1);
123 return 2;
124 };
125 /* GNU pascal strings. */
126 /* Three fields: Capacity, length and schema$ or _p_schema. */
127 if (type->num_fields () == 3
128 && TYPE_FIELD_NAME (type, 0)
129 && strcmp (TYPE_FIELD_NAME (type, 0), "Capacity") == 0
130 && TYPE_FIELD_NAME (type, 1)
131 && strcmp (TYPE_FIELD_NAME (type, 1), "length") == 0)
132 {
133 if (length_pos)
134 *length_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
135 if (length_size)
136 *length_size = TYPE_LENGTH (type->field (1).type ());
137 if (string_pos)
138 *string_pos = TYPE_FIELD_BITPOS (type, 2) / TARGET_CHAR_BIT;
139 /* FIXME: how can I detect wide chars in GPC ?? */
140 if (char_type)
141 {
142 *char_type = TYPE_TARGET_TYPE (type->field (2).type ());
143
144 if ((*char_type)->code () == TYPE_CODE_ARRAY)
145 *char_type = TYPE_TARGET_TYPE (*char_type);
146 }
147 if (arrayname)
148 *arrayname = TYPE_FIELD_NAME (type, 2);
149 return 3;
150 };
151 }
152 return 0;
153 }
154
155 static void pascal_one_char (int, struct ui_file *, int *);
156
157 /* Print the character C on STREAM as part of the contents of a literal
158 string.
159 In_quotes is reset to 0 if a char is written with #4 notation. */
160
161 static void
162 pascal_one_char (int c, struct ui_file *stream, int *in_quotes)
163 {
164 if (c == '\'' || ((unsigned int) c <= 0xff && (PRINT_LITERAL_FORM (c))))
165 {
166 if (!(*in_quotes))
167 fputs_filtered ("'", stream);
168 *in_quotes = 1;
169 if (c == '\'')
170 {
171 fputs_filtered ("''", stream);
172 }
173 else
174 fprintf_filtered (stream, "%c", c);
175 }
176 else
177 {
178 if (*in_quotes)
179 fputs_filtered ("'", stream);
180 *in_quotes = 0;
181 fprintf_filtered (stream, "#%d", (unsigned int) c);
182 }
183 }
184
185 void
186 pascal_printchar (int c, struct type *type, struct ui_file *stream)
187 {
188 int in_quotes = 0;
189
190 pascal_one_char (c, stream, &in_quotes);
191 if (in_quotes)
192 fputs_filtered ("'", stream);
193 }
194
195 \f
196
197 /* Table mapping opcodes into strings for printing operators
198 and precedences of the operators. */
199
200 const struct op_print pascal_op_print_tab[] =
201 {
202 {",", BINOP_COMMA, PREC_COMMA, 0},
203 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
204 {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
205 {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
206 {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
207 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
208 {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
209 {"<=", BINOP_LEQ, PREC_ORDER, 0},
210 {">=", BINOP_GEQ, PREC_ORDER, 0},
211 {">", BINOP_GTR, PREC_ORDER, 0},
212 {"<", BINOP_LESS, PREC_ORDER, 0},
213 {"shr", BINOP_RSH, PREC_SHIFT, 0},
214 {"shl", BINOP_LSH, PREC_SHIFT, 0},
215 {"+", BINOP_ADD, PREC_ADD, 0},
216 {"-", BINOP_SUB, PREC_ADD, 0},
217 {"*", BINOP_MUL, PREC_MUL, 0},
218 {"/", BINOP_DIV, PREC_MUL, 0},
219 {"div", BINOP_INTDIV, PREC_MUL, 0},
220 {"mod", BINOP_REM, PREC_MUL, 0},
221 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
222 {"-", UNOP_NEG, PREC_PREFIX, 0},
223 {"not", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
224 {"^", UNOP_IND, PREC_SUFFIX, 1},
225 {"@", UNOP_ADDR, PREC_PREFIX, 0},
226 {"sizeof", UNOP_SIZEOF, PREC_PREFIX, 0},
227 {NULL, OP_NULL, PREC_PREFIX, 0}
228 };
229 \f
230 enum pascal_primitive_types {
231 pascal_primitive_type_int,
232 pascal_primitive_type_long,
233 pascal_primitive_type_short,
234 pascal_primitive_type_char,
235 pascal_primitive_type_float,
236 pascal_primitive_type_double,
237 pascal_primitive_type_void,
238 pascal_primitive_type_long_long,
239 pascal_primitive_type_signed_char,
240 pascal_primitive_type_unsigned_char,
241 pascal_primitive_type_unsigned_short,
242 pascal_primitive_type_unsigned_int,
243 pascal_primitive_type_unsigned_long,
244 pascal_primitive_type_unsigned_long_long,
245 pascal_primitive_type_long_double,
246 pascal_primitive_type_complex,
247 pascal_primitive_type_double_complex,
248 nr_pascal_primitive_types
249 };
250
251 /* Constant data representing the Pascal language. */
252
253 extern const struct language_data pascal_language_data =
254 {
255 range_check_on,
256 case_sensitive_on,
257 array_row_major,
258 macro_expansion_no,
259 &exp_descriptor_standard,
260 false, /* la_store_sym_names_in_linkage_form_p */
261 pascal_op_print_tab, /* expression operators for printing */
262 &default_varobj_ops,
263 };
264
265 /* Class representing the Pascal language. */
266
267 class pascal_language : public language_defn
268 {
269 public:
270 pascal_language ()
271 : language_defn (language_pascal, pascal_language_data)
272 { /* Nothing. */ }
273
274 /* See language.h. */
275
276 const char *name () const override
277 { return "pascal"; }
278
279 /* See language.h. */
280
281 const char *natural_name () const override
282 { return "Pascal"; }
283
284 /* See language.h. */
285
286 const std::vector<const char *> &filename_extensions () const override
287 {
288 static const std::vector<const char *> extensions
289 = { ".pas", ".p", ".pp" };
290 return extensions;
291 }
292
293 /* See language.h. */
294 void language_arch_info (struct gdbarch *gdbarch,
295 struct language_arch_info *lai) const override
296 {
297 const struct builtin_type *builtin = builtin_type (gdbarch);
298
299 lai->string_char_type = builtin->builtin_char;
300 lai->primitive_type_vector
301 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_pascal_primitive_types + 1,
302 struct type *);
303 lai->primitive_type_vector [pascal_primitive_type_int]
304 = builtin->builtin_int;
305 lai->primitive_type_vector [pascal_primitive_type_long]
306 = builtin->builtin_long;
307 lai->primitive_type_vector [pascal_primitive_type_short]
308 = builtin->builtin_short;
309 lai->primitive_type_vector [pascal_primitive_type_char]
310 = builtin->builtin_char;
311 lai->primitive_type_vector [pascal_primitive_type_float]
312 = builtin->builtin_float;
313 lai->primitive_type_vector [pascal_primitive_type_double]
314 = builtin->builtin_double;
315 lai->primitive_type_vector [pascal_primitive_type_void]
316 = builtin->builtin_void;
317 lai->primitive_type_vector [pascal_primitive_type_long_long]
318 = builtin->builtin_long_long;
319 lai->primitive_type_vector [pascal_primitive_type_signed_char]
320 = builtin->builtin_signed_char;
321 lai->primitive_type_vector [pascal_primitive_type_unsigned_char]
322 = builtin->builtin_unsigned_char;
323 lai->primitive_type_vector [pascal_primitive_type_unsigned_short]
324 = builtin->builtin_unsigned_short;
325 lai->primitive_type_vector [pascal_primitive_type_unsigned_int]
326 = builtin->builtin_unsigned_int;
327 lai->primitive_type_vector [pascal_primitive_type_unsigned_long]
328 = builtin->builtin_unsigned_long;
329 lai->primitive_type_vector [pascal_primitive_type_unsigned_long_long]
330 = builtin->builtin_unsigned_long_long;
331 lai->primitive_type_vector [pascal_primitive_type_long_double]
332 = builtin->builtin_long_double;
333 lai->primitive_type_vector [pascal_primitive_type_complex]
334 = builtin->builtin_complex;
335 lai->primitive_type_vector [pascal_primitive_type_double_complex]
336 = builtin->builtin_double_complex;
337
338 lai->bool_type_symbol = "boolean";
339 lai->bool_type_default = builtin->builtin_bool;
340 }
341
342 /* See language.h. */
343
344 void print_type (struct type *type, const char *varstring,
345 struct ui_file *stream, int show, int level,
346 const struct type_print_options *flags) const override
347 {
348 pascal_print_type (type, varstring, stream, show, level, flags);
349 }
350
351 /* See language.h. */
352
353 void value_print (struct value *val, struct ui_file *stream,
354 const struct value_print_options *options) const override
355 {
356 return pascal_value_print (val, stream, options);
357 }
358
359 /* See language.h. */
360
361 void value_print_inner
362 (struct value *val, struct ui_file *stream, int recurse,
363 const struct value_print_options *options) const override
364 {
365 return pascal_value_print_inner (val, stream, recurse, options);
366 }
367
368 /* See language.h. */
369
370 int parser (struct parser_state *ps) const override
371 {
372 return pascal_parse (ps);
373 }
374
375 /* See language.h. */
376
377 void emitchar (int ch, struct type *chtype,
378 struct ui_file *stream, int quoter) const override
379 {
380 int in_quotes = 0;
381
382 pascal_one_char (ch, stream, &in_quotes);
383 if (in_quotes)
384 fputs_filtered ("'", stream);
385 }
386
387 /* See language.h. */
388
389 void printchar (int ch, struct type *chtype,
390 struct ui_file *stream) const override
391 {
392 pascal_printchar (ch, chtype, stream);
393 }
394
395 /* See language.h. */
396
397 void printstr (struct ui_file *stream, struct type *elttype,
398 const gdb_byte *string, unsigned int length,
399 const char *encoding, int force_ellipses,
400 const struct value_print_options *options) const override
401 {
402 enum bfd_endian byte_order = type_byte_order (elttype);
403 unsigned int i;
404 unsigned int things_printed = 0;
405 int in_quotes = 0;
406 int need_comma = 0;
407 int width;
408
409 /* Preserve ELTTYPE's original type, just set its LENGTH. */
410 check_typedef (elttype);
411 width = TYPE_LENGTH (elttype);
412
413 /* If the string was not truncated due to `set print elements', and
414 the last byte of it is a null, we don't print that, in traditional C
415 style. */
416 if ((!force_ellipses) && length > 0
417 && extract_unsigned_integer (string + (length - 1) * width, width,
418 byte_order) == 0)
419 length--;
420
421 if (length == 0)
422 {
423 fputs_filtered ("''", stream);
424 return;
425 }
426
427 for (i = 0; i < length && things_printed < options->print_max; ++i)
428 {
429 /* Position of the character we are examining
430 to see whether it is repeated. */
431 unsigned int rep1;
432 /* Number of repetitions we have detected so far. */
433 unsigned int reps;
434 unsigned long int current_char;
435
436 QUIT;
437
438 if (need_comma)
439 {
440 fputs_filtered (", ", stream);
441 need_comma = 0;
442 }
443
444 current_char = extract_unsigned_integer (string + i * width, width,
445 byte_order);
446
447 rep1 = i + 1;
448 reps = 1;
449 while (rep1 < length
450 && extract_unsigned_integer (string + rep1 * width, width,
451 byte_order) == current_char)
452 {
453 ++rep1;
454 ++reps;
455 }
456
457 if (reps > options->repeat_count_threshold)
458 {
459 if (in_quotes)
460 {
461 fputs_filtered ("', ", stream);
462 in_quotes = 0;
463 }
464 pascal_printchar (current_char, elttype, stream);
465 fprintf_filtered (stream, " %p[<repeats %u times>%p]",
466 metadata_style.style ().ptr (),
467 reps, nullptr);
468 i = rep1 - 1;
469 things_printed += options->repeat_count_threshold;
470 need_comma = 1;
471 }
472 else
473 {
474 if ((!in_quotes) && (PRINT_LITERAL_FORM (current_char)))
475 {
476 fputs_filtered ("'", stream);
477 in_quotes = 1;
478 }
479 pascal_one_char (current_char, stream, &in_quotes);
480 ++things_printed;
481 }
482 }
483
484 /* Terminate the quotes if necessary. */
485 if (in_quotes)
486 fputs_filtered ("'", stream);
487
488 if (force_ellipses || i < length)
489 fputs_filtered ("...", stream);
490 }
491
492 /* See language.h. */
493
494 void print_typedef (struct type *type, struct symbol *new_symbol,
495 struct ui_file *stream) const override
496 {
497 pascal_print_typedef (type, new_symbol, stream);
498 }
499
500 /* See language.h. */
501
502 bool is_string_type_p (struct type *type) const override
503 {
504 return is_pascal_string_type (type, nullptr, nullptr, nullptr,
505 nullptr, nullptr) > 0;
506 }
507
508 /* See language.h. */
509
510 const char *name_of_this () const override
511 { return "this"; }
512 };
513
514 /* Single instance of the Pascal language class. */
515
516 static pascal_language pascal_language_defn;