Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Multiple source language support for GDB. |
d9fcf2fb | 2 | Copyright 1991, 1992, 2000 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by the Department of Computer Science at the State University |
4 | of New York at Buffalo. | |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | /* This file contains functions that return things that are specific | |
24 | to languages. Each function should examine current_language if necessary, | |
25 | and return the appropriate result. */ | |
26 | ||
27 | /* FIXME: Most of these would be better organized as macros which | |
28 | return data out of a "language-specific" struct pointer that is set | |
29 | whenever the working language changes. That would be a lot faster. */ | |
30 | ||
31 | #include "defs.h" | |
32 | #include <ctype.h> | |
33 | #include "gdb_string.h" | |
34 | ||
35 | #include "symtab.h" | |
36 | #include "gdbtypes.h" | |
37 | #include "value.h" | |
38 | #include "gdbcmd.h" | |
39 | #include "frame.h" | |
40 | #include "expression.h" | |
41 | #include "language.h" | |
42 | #include "target.h" | |
43 | #include "parser-defs.h" | |
44 | ||
a14ed312 | 45 | extern void _initialize_language (void); |
392a587b | 46 | |
a14ed312 | 47 | static void show_language_command (char *, int); |
c906108c | 48 | |
a14ed312 | 49 | static void set_language_command (char *, int); |
c906108c | 50 | |
a14ed312 | 51 | static void show_type_command (char *, int); |
c906108c | 52 | |
a14ed312 | 53 | static void set_type_command (char *, int); |
c906108c | 54 | |
a14ed312 | 55 | static void show_range_command (char *, int); |
c906108c | 56 | |
a14ed312 | 57 | static void set_range_command (char *, int); |
c906108c | 58 | |
a14ed312 | 59 | static void set_range_str (void); |
c906108c | 60 | |
a14ed312 | 61 | static void set_type_str (void); |
c906108c | 62 | |
a14ed312 | 63 | static void set_lang_str (void); |
c906108c | 64 | |
a14ed312 | 65 | static void unk_lang_error (char *); |
c906108c | 66 | |
a14ed312 | 67 | static int unk_lang_parser (void); |
c906108c | 68 | |
a14ed312 | 69 | static void show_check (char *, int); |
c906108c | 70 | |
a14ed312 | 71 | static void set_check (char *, int); |
c906108c | 72 | |
a14ed312 | 73 | static void set_type_range (void); |
c906108c | 74 | |
d9fcf2fb | 75 | static void unk_lang_emit_char (int c, struct ui_file *stream, int quoter); |
c906108c | 76 | |
d9fcf2fb | 77 | static void unk_lang_printchar (int c, struct ui_file *stream); |
c906108c | 78 | |
d9fcf2fb JM |
79 | static void unk_lang_printstr (struct ui_file * stream, char *string, |
80 | unsigned int length, int width, | |
81 | int force_ellipses); | |
c906108c | 82 | |
a14ed312 | 83 | static struct type *unk_lang_create_fundamental_type (struct objfile *, int); |
c906108c | 84 | |
d9fcf2fb JM |
85 | static void unk_lang_print_type (struct type *, char *, struct ui_file *, |
86 | int, int); | |
c906108c | 87 | |
d9fcf2fb JM |
88 | static int unk_lang_val_print (struct type *, char *, int, CORE_ADDR, |
89 | struct ui_file *, int, int, int, | |
90 | enum val_prettyprint); | |
c906108c | 91 | |
d9fcf2fb | 92 | static int unk_lang_value_print (value_ptr, struct ui_file *, int, enum val_prettyprint); |
c906108c SS |
93 | |
94 | /* Forward declaration */ | |
95 | extern const struct language_defn unknown_language_defn; | |
96 | extern char *warning_pre_print; | |
c5aa993b | 97 | |
c906108c | 98 | /* The current (default at startup) state of type and range checking. |
c5aa993b JM |
99 | (If the modes are set to "auto", though, these are changed based |
100 | on the default language at startup, and then again based on the | |
101 | language of the first source file. */ | |
c906108c SS |
102 | |
103 | enum range_mode range_mode = range_mode_auto; | |
104 | enum range_check range_check = range_check_off; | |
105 | enum type_mode type_mode = type_mode_auto; | |
106 | enum type_check type_check = type_check_off; | |
107 | ||
108 | /* The current language and language_mode (see language.h) */ | |
109 | ||
110 | const struct language_defn *current_language = &unknown_language_defn; | |
111 | enum language_mode language_mode = language_mode_auto; | |
112 | ||
113 | /* The language that the user expects to be typing in (the language | |
114 | of main(), or the last language we notified them about, or C). */ | |
115 | ||
116 | const struct language_defn *expected_language; | |
117 | ||
118 | /* The list of supported languages. The list itself is malloc'd. */ | |
119 | ||
120 | static const struct language_defn **languages; | |
121 | static unsigned languages_size; | |
122 | static unsigned languages_allocsize; | |
123 | #define DEFAULT_ALLOCSIZE 4 | |
124 | ||
125 | /* The "set language/type/range" commands all put stuff in these | |
126 | buffers. This is to make them work as set/show commands. The | |
127 | user's string is copied here, then the set_* commands look at | |
128 | them and update them to something that looks nice when it is | |
129 | printed out. */ | |
130 | ||
131 | static char *language; | |
132 | static char *type; | |
133 | static char *range; | |
134 | ||
135 | /* Warning issued when current_language and the language of the current | |
136 | frame do not match. */ | |
137 | char lang_frame_mismatch_warn[] = | |
c5aa993b | 138 | "Warning: the current language does not match this frame."; |
c906108c | 139 | \f |
c5aa993b | 140 | |
c906108c SS |
141 | /* This page contains the functions corresponding to GDB commands |
142 | and their helpers. */ | |
143 | ||
144 | /* Show command. Display a warning if the language set | |
145 | does not match the frame. */ | |
146 | static void | |
147 | show_language_command (ignore, from_tty) | |
c5aa993b JM |
148 | char *ignore; |
149 | int from_tty; | |
c906108c | 150 | { |
c5aa993b | 151 | enum language flang; /* The language of the current frame */ |
c906108c | 152 | |
c5aa993b JM |
153 | flang = get_frame_language (); |
154 | if (flang != language_unknown && | |
155 | language_mode == language_mode_manual && | |
156 | current_language->la_language != flang) | |
157 | printf_filtered ("%s\n", lang_frame_mismatch_warn); | |
c906108c SS |
158 | } |
159 | ||
160 | /* Set command. Change the current working language. */ | |
161 | static void | |
162 | set_language_command (ignore, from_tty) | |
c5aa993b JM |
163 | char *ignore; |
164 | int from_tty; | |
c906108c SS |
165 | { |
166 | int i; | |
167 | enum language flang; | |
168 | char *err_lang; | |
169 | ||
170 | if (!language || !language[0]) | |
171 | { | |
c5aa993b | 172 | printf_unfiltered ("The currently understood settings are:\n\n"); |
c906108c SS |
173 | printf_unfiltered ("local or auto Automatic setting based on source file\n"); |
174 | ||
175 | for (i = 0; i < languages_size; ++i) | |
176 | { | |
177 | /* Already dealt with these above. */ | |
178 | if (languages[i]->la_language == language_unknown | |
179 | || languages[i]->la_language == language_auto) | |
180 | continue; | |
181 | ||
182 | /* FIXME for now assume that the human-readable name is just | |
183 | a capitalization of the internal name. */ | |
184 | printf_unfiltered ("%-16s Use the %c%s language\n", | |
185 | languages[i]->la_name, | |
c5aa993b JM |
186 | /* Capitalize first letter of language |
187 | name. */ | |
c906108c SS |
188 | toupper (languages[i]->la_name[0]), |
189 | languages[i]->la_name + 1); | |
190 | } | |
191 | /* Restore the silly string. */ | |
c5aa993b | 192 | set_language (current_language->la_language); |
c906108c SS |
193 | return; |
194 | } | |
195 | ||
196 | /* Search the list of languages for a match. */ | |
c5aa993b JM |
197 | for (i = 0; i < languages_size; i++) |
198 | { | |
199 | if (STREQ (languages[i]->la_name, language)) | |
200 | { | |
201 | /* Found it! Go into manual mode, and use this language. */ | |
202 | if (languages[i]->la_language == language_auto) | |
203 | { | |
204 | /* Enter auto mode. Set to the current frame's language, if known. */ | |
205 | language_mode = language_mode_auto; | |
206 | flang = get_frame_language (); | |
207 | if (flang != language_unknown) | |
208 | set_language (flang); | |
209 | expected_language = current_language; | |
210 | return; | |
211 | } | |
212 | else | |
213 | { | |
214 | /* Enter manual mode. Set the specified language. */ | |
215 | language_mode = language_mode_manual; | |
216 | current_language = languages[i]; | |
217 | set_type_range (); | |
218 | set_lang_str (); | |
219 | expected_language = current_language; | |
220 | return; | |
221 | } | |
222 | } | |
c906108c | 223 | } |
c906108c SS |
224 | |
225 | /* Reset the language (esp. the global string "language") to the | |
226 | correct values. */ | |
c5aa993b | 227 | err_lang = savestring (language, strlen (language)); |
c906108c | 228 | make_cleanup (free, err_lang); /* Free it after error */ |
c5aa993b JM |
229 | set_language (current_language->la_language); |
230 | error ("Unknown language `%s'.", err_lang); | |
c906108c SS |
231 | } |
232 | ||
233 | /* Show command. Display a warning if the type setting does | |
234 | not match the current language. */ | |
235 | static void | |
c5aa993b JM |
236 | show_type_command (ignore, from_tty) |
237 | char *ignore; | |
238 | int from_tty; | |
c906108c | 239 | { |
c5aa993b JM |
240 | if (type_check != current_language->la_type_check) |
241 | printf_unfiltered ( | |
242 | "Warning: the current type check setting does not match the language.\n"); | |
c906108c SS |
243 | } |
244 | ||
245 | /* Set command. Change the setting for type checking. */ | |
246 | static void | |
c5aa993b JM |
247 | set_type_command (ignore, from_tty) |
248 | char *ignore; | |
249 | int from_tty; | |
c906108c | 250 | { |
c5aa993b JM |
251 | if (STREQ (type, "on")) |
252 | { | |
c906108c SS |
253 | type_check = type_check_on; |
254 | type_mode = type_mode_manual; | |
c5aa993b JM |
255 | } |
256 | else if (STREQ (type, "warn")) | |
257 | { | |
c906108c SS |
258 | type_check = type_check_warn; |
259 | type_mode = type_mode_manual; | |
c5aa993b JM |
260 | } |
261 | else if (STREQ (type, "off")) | |
262 | { | |
c906108c SS |
263 | type_check = type_check_off; |
264 | type_mode = type_mode_manual; | |
c5aa993b JM |
265 | } |
266 | else if (STREQ (type, "auto")) | |
267 | { | |
c906108c | 268 | type_mode = type_mode_auto; |
c5aa993b | 269 | set_type_range (); |
c906108c SS |
270 | /* Avoid hitting the set_type_str call below. We |
271 | did it in set_type_range. */ | |
272 | return; | |
c5aa993b | 273 | } |
c4093a6a JM |
274 | else |
275 | { | |
276 | warning ("Unrecognized type check setting: \"%s\"", type); | |
277 | } | |
c5aa993b JM |
278 | set_type_str (); |
279 | show_type_command ((char *) NULL, from_tty); | |
c906108c SS |
280 | } |
281 | ||
282 | /* Show command. Display a warning if the range setting does | |
283 | not match the current language. */ | |
284 | static void | |
c5aa993b JM |
285 | show_range_command (ignore, from_tty) |
286 | char *ignore; | |
287 | int from_tty; | |
c906108c SS |
288 | { |
289 | ||
c5aa993b JM |
290 | if (range_check != current_language->la_range_check) |
291 | printf_unfiltered ( | |
292 | "Warning: the current range check setting does not match the language.\n"); | |
c906108c SS |
293 | } |
294 | ||
295 | /* Set command. Change the setting for range checking. */ | |
296 | static void | |
c5aa993b JM |
297 | set_range_command (ignore, from_tty) |
298 | char *ignore; | |
299 | int from_tty; | |
c906108c | 300 | { |
c5aa993b JM |
301 | if (STREQ (range, "on")) |
302 | { | |
c906108c SS |
303 | range_check = range_check_on; |
304 | range_mode = range_mode_manual; | |
c5aa993b JM |
305 | } |
306 | else if (STREQ (range, "warn")) | |
307 | { | |
c906108c SS |
308 | range_check = range_check_warn; |
309 | range_mode = range_mode_manual; | |
c5aa993b JM |
310 | } |
311 | else if (STREQ (range, "off")) | |
312 | { | |
c906108c SS |
313 | range_check = range_check_off; |
314 | range_mode = range_mode_manual; | |
c5aa993b JM |
315 | } |
316 | else if (STREQ (range, "auto")) | |
317 | { | |
c906108c | 318 | range_mode = range_mode_auto; |
c5aa993b | 319 | set_type_range (); |
c906108c | 320 | /* Avoid hitting the set_range_str call below. We |
c5aa993b | 321 | did it in set_type_range. */ |
c906108c | 322 | return; |
c5aa993b | 323 | } |
c4093a6a JM |
324 | else |
325 | { | |
326 | warning ("Unrecognized range check setting: \"%s\"", range); | |
327 | } | |
c5aa993b JM |
328 | set_range_str (); |
329 | show_range_command ((char *) 0, from_tty); | |
c906108c SS |
330 | } |
331 | ||
332 | /* Set the status of range and type checking based on | |
333 | the current modes and the current language. | |
334 | If SHOW is non-zero, then print out the current language, | |
335 | type and range checking status. */ | |
336 | static void | |
c5aa993b | 337 | set_type_range () |
c906108c SS |
338 | { |
339 | ||
340 | if (range_mode == range_mode_auto) | |
341 | range_check = current_language->la_range_check; | |
342 | ||
343 | if (type_mode == type_mode_auto) | |
344 | type_check = current_language->la_type_check; | |
345 | ||
c5aa993b JM |
346 | set_type_str (); |
347 | set_range_str (); | |
c906108c SS |
348 | } |
349 | ||
350 | /* Set current language to (enum language) LANG. Returns previous language. */ | |
351 | ||
352 | enum language | |
c5aa993b JM |
353 | set_language (lang) |
354 | enum language lang; | |
c906108c SS |
355 | { |
356 | int i; | |
357 | enum language prev_language; | |
358 | ||
359 | prev_language = current_language->la_language; | |
360 | ||
c5aa993b JM |
361 | for (i = 0; i < languages_size; i++) |
362 | { | |
363 | if (languages[i]->la_language == lang) | |
364 | { | |
365 | current_language = languages[i]; | |
366 | set_type_range (); | |
367 | set_lang_str (); | |
368 | break; | |
369 | } | |
c906108c | 370 | } |
c906108c SS |
371 | |
372 | return prev_language; | |
373 | } | |
374 | \f | |
375 | /* This page contains functions that update the global vars | |
376 | language, type and range. */ | |
377 | static void | |
c5aa993b | 378 | set_lang_str () |
c906108c | 379 | { |
c5aa993b | 380 | char *prefix = ""; |
c906108c | 381 | |
ccdaf797 PDM |
382 | if (language) |
383 | free (language); | |
c5aa993b JM |
384 | if (language_mode == language_mode_auto) |
385 | prefix = "auto; currently "; | |
c906108c | 386 | |
c5aa993b | 387 | language = concat (prefix, current_language->la_name, NULL); |
c906108c SS |
388 | } |
389 | ||
390 | static void | |
c5aa993b | 391 | set_type_str () |
c906108c | 392 | { |
c4093a6a | 393 | char *tmp = NULL, *prefix = ""; |
c906108c | 394 | |
ccdaf797 PDM |
395 | if (type) |
396 | free (type); | |
c5aa993b JM |
397 | if (type_mode == type_mode_auto) |
398 | prefix = "auto; currently "; | |
c906108c | 399 | |
c5aa993b JM |
400 | switch (type_check) |
401 | { | |
402 | case type_check_on: | |
c906108c SS |
403 | tmp = "on"; |
404 | break; | |
c5aa993b | 405 | case type_check_off: |
c906108c SS |
406 | tmp = "off"; |
407 | break; | |
c5aa993b | 408 | case type_check_warn: |
c906108c SS |
409 | tmp = "warn"; |
410 | break; | |
c5aa993b | 411 | default: |
c906108c | 412 | error ("Unrecognized type check setting."); |
c5aa993b | 413 | } |
c906108c | 414 | |
c5aa993b | 415 | type = concat (prefix, tmp, NULL); |
c906108c SS |
416 | } |
417 | ||
418 | static void | |
c5aa993b | 419 | set_range_str () |
c906108c | 420 | { |
c5aa993b | 421 | char *tmp, *pref = ""; |
c906108c | 422 | |
c5aa993b JM |
423 | if (range_mode == range_mode_auto) |
424 | pref = "auto; currently "; | |
c906108c | 425 | |
c5aa993b JM |
426 | switch (range_check) |
427 | { | |
428 | case range_check_on: | |
c906108c SS |
429 | tmp = "on"; |
430 | break; | |
c5aa993b | 431 | case range_check_off: |
c906108c SS |
432 | tmp = "off"; |
433 | break; | |
c5aa993b | 434 | case range_check_warn: |
c906108c SS |
435 | tmp = "warn"; |
436 | break; | |
c5aa993b | 437 | default: |
c906108c | 438 | error ("Unrecognized range check setting."); |
c5aa993b | 439 | } |
c906108c | 440 | |
ccdaf797 PDM |
441 | if (range) |
442 | free (range); | |
c5aa993b | 443 | range = concat (pref, tmp, NULL); |
c906108c SS |
444 | } |
445 | ||
446 | ||
447 | /* Print out the current language settings: language, range and | |
448 | type checking. If QUIETLY, print only what has changed. */ | |
449 | ||
450 | void | |
451 | language_info (quietly) | |
452 | int quietly; | |
453 | { | |
454 | if (quietly && expected_language == current_language) | |
455 | return; | |
456 | ||
457 | expected_language = current_language; | |
c5aa993b JM |
458 | printf_unfiltered ("Current language: %s\n", language); |
459 | show_language_command ((char *) 0, 1); | |
c906108c SS |
460 | |
461 | if (!quietly) | |
462 | { | |
c5aa993b JM |
463 | printf_unfiltered ("Type checking: %s\n", type); |
464 | show_type_command ((char *) 0, 1); | |
465 | printf_unfiltered ("Range checking: %s\n", range); | |
466 | show_range_command ((char *) 0, 1); | |
c906108c SS |
467 | } |
468 | } | |
469 | \f | |
470 | /* Return the result of a binary operation. */ | |
471 | ||
c5aa993b | 472 | #if 0 /* Currently unused */ |
c906108c SS |
473 | |
474 | struct type * | |
475 | binop_result_type (v1, v2) | |
c5aa993b | 476 | value_ptr v1, v2; |
c906108c | 477 | { |
c5aa993b JM |
478 | int size, uns; |
479 | struct type *t1 = check_typedef (VALUE_TYPE (v1)); | |
480 | struct type *t2 = check_typedef (VALUE_TYPE (v2)); | |
481 | ||
482 | int l1 = TYPE_LENGTH (t1); | |
483 | int l2 = TYPE_LENGTH (t2); | |
484 | ||
485 | switch (current_language->la_language) | |
486 | { | |
487 | case language_c: | |
488 | case language_cplus: | |
489 | if (TYPE_CODE (t1) == TYPE_CODE_FLT) | |
490 | return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ? | |
491 | VALUE_TYPE (v2) : VALUE_TYPE (v1); | |
492 | else if (TYPE_CODE (t2) == TYPE_CODE_FLT) | |
493 | return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ? | |
494 | VALUE_TYPE (v1) : VALUE_TYPE (v2); | |
495 | else if (TYPE_UNSIGNED (t1) && l1 > l2) | |
496 | return VALUE_TYPE (v1); | |
497 | else if (TYPE_UNSIGNED (t2) && l2 > l1) | |
498 | return VALUE_TYPE (v2); | |
499 | else /* Both are signed. Result is the longer type */ | |
500 | return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2); | |
c906108c | 501 | break; |
c5aa993b | 502 | case language_m2: |
c906108c | 503 | /* If we are doing type-checking, l1 should equal l2, so this is |
c5aa993b JM |
504 | not needed. */ |
505 | return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2); | |
c906108c | 506 | break; |
c5aa993b JM |
507 | case language_chill: |
508 | error ("Missing Chill support in function binop_result_check."); /*FIXME */ | |
509 | } | |
510 | abort (); | |
511 | return (struct type *) 0; /* For lint */ | |
c906108c SS |
512 | } |
513 | ||
c5aa993b | 514 | #endif /* 0 */ |
c906108c | 515 | \f |
c5aa993b | 516 | |
c906108c SS |
517 | /* This page contains functions that return format strings for |
518 | printf for printing out numbers in different formats */ | |
519 | ||
520 | /* Returns the appropriate printf format for hexadecimal | |
521 | numbers. */ | |
522 | char * | |
c5aa993b JM |
523 | local_hex_format_custom (pre) |
524 | char *pre; | |
c906108c | 525 | { |
c5aa993b JM |
526 | static char form[50]; |
527 | ||
528 | strcpy (form, local_hex_format_prefix ()); | |
529 | strcat (form, "%"); | |
530 | strcat (form, pre); | |
531 | strcat (form, local_hex_format_specifier ()); | |
532 | strcat (form, local_hex_format_suffix ()); | |
533 | return form; | |
c906108c SS |
534 | } |
535 | ||
7acb79c8 AC |
536 | #if 0 |
537 | /* FIXME: cagney/2000-03-04: This function does not appear to be used. | |
538 | It can be deleted once 5.0 has been released. */ | |
539 | /* FIXME: cagney/2000-03-04: This code assumes that the compiler | |
540 | supports ``long long''. */ | |
c4093a6a JM |
541 | /* Converts a number to hexadecimal (without leading "0x") and stores it in a |
542 | static string. Returns a pointer to this string. */ | |
543 | ||
544 | char * | |
545 | longest_raw_hex_string (num) | |
546 | LONGEST num; | |
547 | { | |
548 | static char res_longest_raw_hex_string[50]; | |
549 | long long ll = num; /* MERGEBUG ?? see below */ | |
550 | res_longest_raw_hex_string[0] = 0; | |
551 | /* MERGEBUG ?? As a quick fix I am replacing this with sprintf | |
552 | strcat_address_numeric (num, 0, res_longest_raw_hex_string, 50); | |
553 | */ | |
554 | ||
555 | sprintf (res_longest_raw_hex_string, "%llx", ll); | |
556 | return res_longest_raw_hex_string; | |
557 | } | |
7acb79c8 | 558 | #endif |
c4093a6a | 559 | |
c906108c SS |
560 | /* Converts a number to hexadecimal and stores it in a static |
561 | string. Returns a pointer to this string. */ | |
562 | char * | |
563 | local_hex_string (num) | |
c5aa993b | 564 | unsigned long num; |
c906108c | 565 | { |
c5aa993b | 566 | static char res[50]; |
c906108c | 567 | |
c5aa993b JM |
568 | sprintf (res, local_hex_format (), num); |
569 | return res; | |
c906108c SS |
570 | } |
571 | ||
c4093a6a JM |
572 | /* Converts a LONGEST number to hexadecimal and stores it in a static |
573 | string. Returns a pointer to this string. */ | |
574 | char * | |
575 | longest_local_hex_string (num) | |
576 | LONGEST num; | |
577 | { | |
578 | return longest_local_hex_string_custom (num, "l"); | |
579 | } | |
580 | ||
c906108c SS |
581 | /* Converts a number to custom hexadecimal and stores it in a static |
582 | string. Returns a pointer to this string. */ | |
583 | char * | |
c5aa993b JM |
584 | local_hex_string_custom (num, pre) |
585 | unsigned long num; | |
586 | char *pre; | |
c906108c | 587 | { |
c5aa993b | 588 | static char res[50]; |
c906108c | 589 | |
c5aa993b JM |
590 | sprintf (res, local_hex_format_custom (pre), num); |
591 | return res; | |
c906108c SS |
592 | } |
593 | ||
c4093a6a JM |
594 | /* Converts a LONGEST number to custom hexadecimal and stores it in a static |
595 | string. Returns a pointer to this string. Note that the width parameter | |
596 | should end with "l", e.g. "08l" as with calls to local_hex_string_custom */ | |
597 | ||
598 | char * | |
599 | longest_local_hex_string_custom (num, width) | |
600 | LONGEST num; | |
601 | char *width; | |
602 | { | |
603 | #define RESULT_BUF_LEN 50 | |
604 | static char res2[RESULT_BUF_LEN]; | |
605 | char format[RESULT_BUF_LEN]; | |
606 | #if !defined (PRINTF_HAS_LONG_LONG) | |
607 | int field_width; | |
608 | int num_len; | |
609 | int num_pad_chars; | |
610 | char *pad_char; /* string with one character */ | |
611 | int pad_on_left; | |
612 | char *parse_ptr; | |
613 | char temp_nbr_buf[RESULT_BUF_LEN]; | |
614 | #endif | |
615 | ||
616 | #ifndef CC_HAS_LONG_LONG | |
617 | /* If there is no long long, then LONGEST should be just long and we | |
618 | can use local_hex_string_custom | |
619 | */ | |
620 | return local_hex_string_custom ((unsigned long) num, width); | |
392746e5 | 621 | #elif defined (PRINTF_HAS_LONG_LONG) |
c4093a6a JM |
622 | /* Just use printf. */ |
623 | strcpy (format, local_hex_format_prefix ()); /* 0x */ | |
624 | strcat (format, "%"); | |
625 | strcat (format, width); /* e.g. "08l" */ | |
626 | strcat (format, "l"); /* need "ll" for long long */ | |
627 | strcat (format, local_hex_format_specifier ()); /* "x" */ | |
628 | strcat (format, local_hex_format_suffix ()); /* "" */ | |
629 | sprintf (res2, format, num); | |
630 | return res2; | |
631 | #else /* !defined (PRINTF_HAS_LONG_LONG) */ | |
632 | /* Use strcat_address_numeric to print the number into a string, then | |
633 | build the result string from local_hex_format_prefix, padding and | |
634 | the hex representation as indicated by "width". */ | |
635 | ||
636 | temp_nbr_buf[0] = 0; | |
637 | /* With use_local == 0, we don't get the leading "0x" prefix. */ | |
638 | /* MERGEBUG ?? As a quick fix I am replacing this call to | |
639 | strcat_address_numeric with sprintf | |
640 | strcat_address_numeric(num, 0, temp_nbr_buf, RESULT_BUF_LEN); | |
641 | */ | |
642 | ||
643 | { | |
644 | long long ll = num; | |
645 | sprintf (temp_nbr_buf, "%llx", ll); | |
646 | } | |
647 | /* parse width */ | |
648 | parse_ptr = width; | |
649 | pad_on_left = 1; | |
650 | pad_char = " "; | |
651 | if (*parse_ptr == '-') | |
652 | { | |
653 | parse_ptr++; | |
654 | pad_on_left = 0; | |
655 | } | |
656 | if (*parse_ptr == '0') | |
657 | { | |
658 | parse_ptr++; | |
659 | if (pad_on_left) | |
660 | pad_char = "0"; /* If padding is on the right, it is blank */ | |
661 | } | |
662 | field_width = atoi (parse_ptr); | |
663 | num_len = strlen (temp_nbr_buf); | |
664 | num_pad_chars = field_width - strlen (temp_nbr_buf); /* possibly negative */ | |
665 | ||
666 | if (strlen (local_hex_format_prefix ()) + num_len + num_pad_chars | |
667 | < RESULT_BUF_LEN) /* paranoia */ | |
668 | internal_error ("longest_local_hex_string_custom: insufficient space to store result"); | |
669 | ||
670 | strcpy (res2, local_hex_format_prefix ()); | |
671 | if (pad_on_left) | |
672 | { | |
673 | while (num_pad_chars > 0) | |
674 | { | |
675 | strcat (res2, pad_char); | |
676 | num_pad_chars--; | |
677 | } | |
678 | } | |
679 | strcat (res2, temp_nbr_buf); | |
680 | if (!pad_on_left) | |
681 | { | |
682 | while (num_pad_chars > 0) | |
683 | { | |
684 | strcat (res2, pad_char); | |
685 | num_pad_chars--; | |
686 | } | |
687 | } | |
688 | return res2; | |
689 | #endif | |
690 | ||
691 | } /* longest_local_hex_string_custom */ | |
692 | ||
c906108c SS |
693 | /* Returns the appropriate printf format for octal |
694 | numbers. */ | |
695 | char * | |
c5aa993b JM |
696 | local_octal_format_custom (pre) |
697 | char *pre; | |
c906108c | 698 | { |
c5aa993b JM |
699 | static char form[50]; |
700 | ||
701 | strcpy (form, local_octal_format_prefix ()); | |
702 | strcat (form, "%"); | |
703 | strcat (form, pre); | |
704 | strcat (form, local_octal_format_specifier ()); | |
705 | strcat (form, local_octal_format_suffix ()); | |
706 | return form; | |
c906108c SS |
707 | } |
708 | ||
709 | /* Returns the appropriate printf format for decimal numbers. */ | |
710 | char * | |
c5aa993b JM |
711 | local_decimal_format_custom (pre) |
712 | char *pre; | |
c906108c | 713 | { |
c5aa993b JM |
714 | static char form[50]; |
715 | ||
716 | strcpy (form, local_decimal_format_prefix ()); | |
717 | strcat (form, "%"); | |
718 | strcat (form, pre); | |
719 | strcat (form, local_decimal_format_specifier ()); | |
720 | strcat (form, local_decimal_format_suffix ()); | |
721 | return form; | |
c906108c SS |
722 | } |
723 | \f | |
724 | #if 0 | |
725 | /* This page contains functions that are used in type/range checking. | |
726 | They all return zero if the type/range check fails. | |
727 | ||
728 | It is hoped that these will make extending GDB to parse different | |
729 | languages a little easier. These are primarily used in eval.c when | |
730 | evaluating expressions and making sure that their types are correct. | |
731 | Instead of having a mess of conjucted/disjuncted expressions in an "if", | |
732 | the ideas of type can be wrapped up in the following functions. | |
733 | ||
734 | Note that some of them are not currently dependent upon which language | |
735 | is currently being parsed. For example, floats are the same in | |
736 | C and Modula-2 (ie. the only floating point type has TYPE_CODE of | |
737 | TYPE_CODE_FLT), while booleans are different. */ | |
738 | ||
739 | /* Returns non-zero if its argument is a simple type. This is the same for | |
740 | both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur, | |
741 | and thus will never cause the failure of the test. */ | |
742 | int | |
c5aa993b JM |
743 | simple_type (type) |
744 | struct type *type; | |
c906108c SS |
745 | { |
746 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
747 | switch (TYPE_CODE (type)) |
748 | { | |
749 | case TYPE_CODE_INT: | |
750 | case TYPE_CODE_CHAR: | |
751 | case TYPE_CODE_ENUM: | |
752 | case TYPE_CODE_FLT: | |
753 | case TYPE_CODE_RANGE: | |
754 | case TYPE_CODE_BOOL: | |
755 | return 1; | |
c906108c | 756 | |
c5aa993b JM |
757 | default: |
758 | return 0; | |
759 | } | |
c906108c SS |
760 | } |
761 | ||
762 | /* Returns non-zero if its argument is of an ordered type. | |
763 | An ordered type is one in which the elements can be tested for the | |
764 | properties of "greater than", "less than", etc, or for which the | |
765 | operations "increment" or "decrement" make sense. */ | |
766 | int | |
767 | ordered_type (type) | |
c5aa993b | 768 | struct type *type; |
c906108c SS |
769 | { |
770 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
771 | switch (TYPE_CODE (type)) |
772 | { | |
773 | case TYPE_CODE_INT: | |
774 | case TYPE_CODE_CHAR: | |
775 | case TYPE_CODE_ENUM: | |
776 | case TYPE_CODE_FLT: | |
777 | case TYPE_CODE_RANGE: | |
778 | return 1; | |
c906108c | 779 | |
c5aa993b JM |
780 | default: |
781 | return 0; | |
782 | } | |
c906108c SS |
783 | } |
784 | ||
785 | /* Returns non-zero if the two types are the same */ | |
786 | int | |
787 | same_type (arg1, arg2) | |
c5aa993b | 788 | struct type *arg1, *arg2; |
c906108c SS |
789 | { |
790 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
791 | if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2)) |
792 | /* One is structured and one isn't */ | |
793 | return 0; | |
794 | else if (structured_type (arg1) && structured_type (arg2)) | |
795 | return arg1 == arg2; | |
796 | else if (numeric_type (arg1) && numeric_type (arg2)) | |
797 | return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) && | |
798 | (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2)) | |
799 | ? 1 : 0; | |
800 | else | |
801 | return arg1 == arg2; | |
c906108c SS |
802 | } |
803 | ||
804 | /* Returns non-zero if the type is integral */ | |
805 | int | |
806 | integral_type (type) | |
c5aa993b | 807 | struct type *type; |
c906108c SS |
808 | { |
809 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
810 | switch (current_language->la_language) |
811 | { | |
812 | case language_c: | |
813 | case language_cplus: | |
814 | return (TYPE_CODE (type) != TYPE_CODE_INT) && | |
815 | (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1; | |
816 | case language_m2: | |
750ba382 | 817 | case language_pascal: |
c5aa993b JM |
818 | return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1; |
819 | case language_chill: | |
820 | error ("Missing Chill support in function integral_type."); /*FIXME */ | |
821 | default: | |
c906108c | 822 | error ("Language not supported."); |
c5aa993b | 823 | } |
c906108c SS |
824 | } |
825 | ||
826 | /* Returns non-zero if the value is numeric */ | |
827 | int | |
828 | numeric_type (type) | |
c5aa993b | 829 | struct type *type; |
c906108c SS |
830 | { |
831 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
832 | switch (TYPE_CODE (type)) |
833 | { | |
834 | case TYPE_CODE_INT: | |
835 | case TYPE_CODE_FLT: | |
836 | return 1; | |
c906108c | 837 | |
c5aa993b JM |
838 | default: |
839 | return 0; | |
840 | } | |
c906108c SS |
841 | } |
842 | ||
843 | /* Returns non-zero if the value is a character type */ | |
844 | int | |
845 | character_type (type) | |
c5aa993b | 846 | struct type *type; |
c906108c SS |
847 | { |
848 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
849 | switch (current_language->la_language) |
850 | { | |
851 | case language_chill: | |
852 | case language_m2: | |
750ba382 | 853 | case language_pascal: |
c5aa993b JM |
854 | return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1; |
855 | ||
856 | case language_c: | |
857 | case language_cplus: | |
858 | return (TYPE_CODE (type) == TYPE_CODE_INT) && | |
859 | TYPE_LENGTH (type) == sizeof (char) | |
860 | ? 1 : 0; | |
861 | default: | |
c906108c | 862 | return (0); |
c5aa993b | 863 | } |
c906108c SS |
864 | } |
865 | ||
866 | /* Returns non-zero if the value is a string type */ | |
867 | int | |
868 | string_type (type) | |
c5aa993b | 869 | struct type *type; |
c906108c SS |
870 | { |
871 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
872 | switch (current_language->la_language) |
873 | { | |
874 | case language_chill: | |
875 | case language_m2: | |
750ba382 | 876 | case language_pascal: |
c5aa993b JM |
877 | return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1; |
878 | ||
879 | case language_c: | |
880 | case language_cplus: | |
c906108c SS |
881 | /* C does not have distinct string type. */ |
882 | return (0); | |
c5aa993b | 883 | default: |
c906108c | 884 | return (0); |
c5aa993b | 885 | } |
c906108c SS |
886 | } |
887 | ||
888 | /* Returns non-zero if the value is a boolean type */ | |
889 | int | |
890 | boolean_type (type) | |
c5aa993b | 891 | struct type *type; |
c906108c SS |
892 | { |
893 | CHECK_TYPEDEF (type); | |
894 | if (TYPE_CODE (type) == TYPE_CODE_BOOL) | |
895 | return 1; | |
c5aa993b | 896 | switch (current_language->la_language) |
c906108c SS |
897 | { |
898 | case language_c: | |
899 | case language_cplus: | |
900 | /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL | |
c5aa993b | 901 | for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C. */ |
c906108c SS |
902 | if (TYPE_CODE (type) == TYPE_CODE_INT) |
903 | return 1; | |
c5aa993b | 904 | default: |
c906108c | 905 | break; |
c5aa993b | 906 | } |
c906108c SS |
907 | return 0; |
908 | } | |
909 | ||
910 | /* Returns non-zero if the value is a floating-point type */ | |
911 | int | |
912 | float_type (type) | |
c5aa993b | 913 | struct type *type; |
c906108c SS |
914 | { |
915 | CHECK_TYPEDEF (type); | |
c5aa993b | 916 | return TYPE_CODE (type) == TYPE_CODE_FLT; |
c906108c SS |
917 | } |
918 | ||
919 | /* Returns non-zero if the value is a pointer type */ | |
920 | int | |
c5aa993b JM |
921 | pointer_type (type) |
922 | struct type *type; | |
c906108c | 923 | { |
c5aa993b JM |
924 | return TYPE_CODE (type) == TYPE_CODE_PTR || |
925 | TYPE_CODE (type) == TYPE_CODE_REF; | |
c906108c SS |
926 | } |
927 | ||
928 | /* Returns non-zero if the value is a structured type */ | |
929 | int | |
c5aa993b JM |
930 | structured_type (type) |
931 | struct type *type; | |
c906108c SS |
932 | { |
933 | CHECK_TYPEDEF (type); | |
c5aa993b JM |
934 | switch (current_language->la_language) |
935 | { | |
936 | case language_c: | |
937 | case language_cplus: | |
938 | return (TYPE_CODE (type) == TYPE_CODE_STRUCT) || | |
939 | (TYPE_CODE (type) == TYPE_CODE_UNION) || | |
940 | (TYPE_CODE (type) == TYPE_CODE_ARRAY); | |
750ba382 PM |
941 | case language_pascal: |
942 | return (TYPE_CODE(type) == TYPE_CODE_STRUCT) || | |
943 | (TYPE_CODE(type) == TYPE_CODE_UNION) || | |
944 | (TYPE_CODE(type) == TYPE_CODE_SET) || | |
945 | (TYPE_CODE(type) == TYPE_CODE_ARRAY); | |
c5aa993b JM |
946 | case language_m2: |
947 | return (TYPE_CODE (type) == TYPE_CODE_STRUCT) || | |
948 | (TYPE_CODE (type) == TYPE_CODE_SET) || | |
949 | (TYPE_CODE (type) == TYPE_CODE_ARRAY); | |
950 | case language_chill: | |
951 | error ("Missing Chill support in function structured_type."); /*FIXME */ | |
952 | default: | |
c906108c | 953 | return (0); |
c5aa993b | 954 | } |
c906108c SS |
955 | } |
956 | #endif | |
957 | \f | |
958 | struct type * | |
959 | lang_bool_type () | |
960 | { | |
961 | struct symbol *sym; | |
962 | struct type *type; | |
c5aa993b | 963 | switch (current_language->la_language) |
c906108c SS |
964 | { |
965 | case language_chill: | |
966 | return builtin_type_chill_bool; | |
967 | case language_fortran: | |
968 | sym = lookup_symbol ("logical", NULL, VAR_NAMESPACE, NULL, NULL); | |
969 | if (sym) | |
970 | { | |
971 | type = SYMBOL_TYPE (sym); | |
972 | if (type && TYPE_CODE (type) == TYPE_CODE_BOOL) | |
973 | return type; | |
974 | } | |
975 | return builtin_type_f_logical_s2; | |
976 | case language_cplus: | |
750ba382 PM |
977 | case language_pascal: |
978 | if (current_language->la_language==language_cplus) | |
979 | {sym = lookup_symbol ("bool", NULL, VAR_NAMESPACE, NULL, NULL);} | |
980 | else | |
981 | {sym = lookup_symbol ("boolean", NULL, VAR_NAMESPACE, NULL, NULL);} | |
c906108c SS |
982 | if (sym) |
983 | { | |
984 | type = SYMBOL_TYPE (sym); | |
985 | if (type && TYPE_CODE (type) == TYPE_CODE_BOOL) | |
986 | return type; | |
987 | } | |
988 | return builtin_type_bool; | |
989 | default: | |
990 | return builtin_type_int; | |
991 | } | |
992 | } | |
993 | \f | |
994 | /* This page contains functions that return info about | |
995 | (struct value) values used in GDB. */ | |
996 | ||
997 | /* Returns non-zero if the value VAL represents a true value. */ | |
998 | int | |
999 | value_true (val) | |
1000 | value_ptr val; | |
1001 | { | |
1002 | /* It is possible that we should have some sort of error if a non-boolean | |
1003 | value is used in this context. Possibly dependent on some kind of | |
1004 | "boolean-checking" option like range checking. But it should probably | |
1005 | not depend on the language except insofar as is necessary to identify | |
1006 | a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean | |
1007 | should be an error, probably). */ | |
1008 | return !value_logical_not (val); | |
1009 | } | |
1010 | \f | |
1011 | /* Returns non-zero if the operator OP is defined on | |
1012 | the values ARG1 and ARG2. */ | |
1013 | ||
c5aa993b | 1014 | #if 0 /* Currently unused */ |
c906108c SS |
1015 | |
1016 | void | |
c5aa993b JM |
1017 | binop_type_check (arg1, arg2, op) |
1018 | value_ptr arg1, arg2; | |
1019 | int op; | |
c906108c | 1020 | { |
c5aa993b | 1021 | struct type *t1, *t2; |
c906108c | 1022 | |
c5aa993b JM |
1023 | /* If we're not checking types, always return success. */ |
1024 | if (!STRICT_TYPE) | |
1025 | return; | |
1026 | ||
1027 | t1 = VALUE_TYPE (arg1); | |
1028 | if (arg2 != NULL) | |
1029 | t2 = VALUE_TYPE (arg2); | |
1030 | else | |
1031 | t2 = NULL; | |
c906108c | 1032 | |
c5aa993b JM |
1033 | switch (op) |
1034 | { | |
1035 | case BINOP_ADD: | |
1036 | case BINOP_SUB: | |
1037 | if ((numeric_type (t1) && pointer_type (t2)) || | |
1038 | (pointer_type (t1) && numeric_type (t2))) | |
1039 | { | |
1040 | warning ("combining pointer and integer.\n"); | |
1041 | break; | |
1042 | } | |
1043 | case BINOP_MUL: | |
1044 | case BINOP_LSH: | |
1045 | case BINOP_RSH: | |
1046 | if (!numeric_type (t1) || !numeric_type (t2)) | |
1047 | type_op_error ("Arguments to %s must be numbers.", op); | |
1048 | else if (!same_type (t1, t2)) | |
1049 | type_op_error ("Arguments to %s must be of the same type.", op); | |
c906108c SS |
1050 | break; |
1051 | ||
c5aa993b JM |
1052 | case BINOP_LOGICAL_AND: |
1053 | case BINOP_LOGICAL_OR: | |
1054 | if (!boolean_type (t1) || !boolean_type (t2)) | |
1055 | type_op_error ("Arguments to %s must be of boolean type.", op); | |
c906108c SS |
1056 | break; |
1057 | ||
c5aa993b JM |
1058 | case BINOP_EQUAL: |
1059 | if ((pointer_type (t1) && !(pointer_type (t2) || integral_type (t2))) || | |
1060 | (pointer_type (t2) && !(pointer_type (t1) || integral_type (t1)))) | |
1061 | type_op_error ("A pointer can only be compared to an integer or pointer.", op); | |
1062 | else if ((pointer_type (t1) && integral_type (t2)) || | |
1063 | (integral_type (t1) && pointer_type (t2))) | |
1064 | { | |
1065 | warning ("combining integer and pointer.\n"); | |
1066 | break; | |
1067 | } | |
1068 | else if (!simple_type (t1) || !simple_type (t2)) | |
1069 | type_op_error ("Arguments to %s must be of simple type.", op); | |
1070 | else if (!same_type (t1, t2)) | |
1071 | type_op_error ("Arguments to %s must be of the same type.", op); | |
c906108c SS |
1072 | break; |
1073 | ||
c5aa993b JM |
1074 | case BINOP_REM: |
1075 | case BINOP_MOD: | |
1076 | if (!integral_type (t1) || !integral_type (t2)) | |
1077 | type_op_error ("Arguments to %s must be of integral type.", op); | |
c906108c SS |
1078 | break; |
1079 | ||
c5aa993b JM |
1080 | case BINOP_LESS: |
1081 | case BINOP_GTR: | |
1082 | case BINOP_LEQ: | |
1083 | case BINOP_GEQ: | |
1084 | if (!ordered_type (t1) || !ordered_type (t2)) | |
1085 | type_op_error ("Arguments to %s must be of ordered type.", op); | |
1086 | else if (!same_type (t1, t2)) | |
1087 | type_op_error ("Arguments to %s must be of the same type.", op); | |
c906108c SS |
1088 | break; |
1089 | ||
c5aa993b JM |
1090 | case BINOP_ASSIGN: |
1091 | if (pointer_type (t1) && !integral_type (t2)) | |
1092 | type_op_error ("A pointer can only be assigned an integer.", op); | |
1093 | else if (pointer_type (t1) && integral_type (t2)) | |
1094 | { | |
1095 | warning ("combining integer and pointer."); | |
1096 | break; | |
1097 | } | |
1098 | else if (!simple_type (t1) || !simple_type (t2)) | |
1099 | type_op_error ("Arguments to %s must be of simple type.", op); | |
1100 | else if (!same_type (t1, t2)) | |
1101 | type_op_error ("Arguments to %s must be of the same type.", op); | |
c906108c SS |
1102 | break; |
1103 | ||
1104 | case BINOP_CONCAT: | |
1105 | /* FIXME: Needs to handle bitstrings as well. */ | |
c5aa993b JM |
1106 | if (!(string_type (t1) || character_type (t1) || integral_type (t1)) |
1107 | || !(string_type (t2) || character_type (t2) || integral_type (t2))) | |
1108 | type_op_error ("Arguments to %s must be strings or characters.", op); | |
c906108c SS |
1109 | break; |
1110 | ||
c5aa993b | 1111 | /* Unary checks -- arg2 is null */ |
c906108c | 1112 | |
c5aa993b JM |
1113 | case UNOP_LOGICAL_NOT: |
1114 | if (!boolean_type (t1)) | |
1115 | type_op_error ("Argument to %s must be of boolean type.", op); | |
c906108c SS |
1116 | break; |
1117 | ||
c5aa993b JM |
1118 | case UNOP_PLUS: |
1119 | case UNOP_NEG: | |
1120 | if (!numeric_type (t1)) | |
1121 | type_op_error ("Argument to %s must be of numeric type.", op); | |
c906108c SS |
1122 | break; |
1123 | ||
c5aa993b JM |
1124 | case UNOP_IND: |
1125 | if (integral_type (t1)) | |
1126 | { | |
1127 | warning ("combining pointer and integer.\n"); | |
1128 | break; | |
1129 | } | |
1130 | else if (!pointer_type (t1)) | |
1131 | type_op_error ("Argument to %s must be a pointer.", op); | |
c906108c SS |
1132 | break; |
1133 | ||
c5aa993b JM |
1134 | case UNOP_PREINCREMENT: |
1135 | case UNOP_POSTINCREMENT: | |
1136 | case UNOP_PREDECREMENT: | |
1137 | case UNOP_POSTDECREMENT: | |
1138 | if (!ordered_type (t1)) | |
1139 | type_op_error ("Argument to %s must be of an ordered type.", op); | |
c906108c SS |
1140 | break; |
1141 | ||
c5aa993b | 1142 | default: |
c906108c | 1143 | /* Ok. The following operators have different meanings in |
c5aa993b JM |
1144 | different languages. */ |
1145 | switch (current_language->la_language) | |
1146 | { | |
c906108c | 1147 | #ifdef _LANG_c |
c5aa993b JM |
1148 | case language_c: |
1149 | case language_cplus: | |
1150 | switch (op) | |
1151 | { | |
1152 | case BINOP_DIV: | |
1153 | if (!numeric_type (t1) || !numeric_type (t2)) | |
1154 | type_op_error ("Arguments to %s must be numbers.", op); | |
1155 | break; | |
1156 | } | |
1157 | break; | |
c906108c SS |
1158 | #endif |
1159 | ||
1160 | #ifdef _LANG_m2 | |
c5aa993b JM |
1161 | case language_m2: |
1162 | switch (op) | |
1163 | { | |
1164 | case BINOP_DIV: | |
1165 | if (!float_type (t1) || !float_type (t2)) | |
1166 | type_op_error ("Arguments to %s must be floating point numbers.", op); | |
1167 | break; | |
1168 | case BINOP_INTDIV: | |
1169 | if (!integral_type (t1) || !integral_type (t2)) | |
1170 | type_op_error ("Arguments to %s must be of integral type.", op); | |
1171 | break; | |
1172 | } | |
c906108c SS |
1173 | #endif |
1174 | ||
750ba382 PM |
1175 | #ifdef _LANG_pascal |
1176 | case language_pascal: | |
1177 | switch(op) | |
1178 | { | |
1179 | case BINOP_DIV: | |
1180 | if (!float_type(t1) && !float_type(t2)) | |
1181 | type_op_error ("Arguments to %s must be floating point numbers.",op); | |
1182 | break; | |
1183 | case BINOP_INTDIV: | |
1184 | if (!integral_type(t1) || !integral_type(t2)) | |
1185 | type_op_error ("Arguments to %s must be of integral type.",op); | |
1186 | break; | |
1187 | } | |
1188 | #endif | |
1189 | ||
c906108c | 1190 | #ifdef _LANG_chill |
c5aa993b JM |
1191 | case language_chill: |
1192 | error ("Missing Chill support in function binop_type_check."); /*FIXME */ | |
c906108c SS |
1193 | #endif |
1194 | ||
c5aa993b JM |
1195 | } |
1196 | } | |
c906108c SS |
1197 | } |
1198 | ||
c5aa993b | 1199 | #endif /* 0 */ |
c906108c | 1200 | \f |
c5aa993b | 1201 | |
c906108c SS |
1202 | /* This page contains functions for the printing out of |
1203 | error messages that occur during type- and range- | |
1204 | checking. */ | |
1205 | ||
1206 | /* Prints the format string FMT with the operator as a string | |
1207 | corresponding to the opcode OP. If FATAL is non-zero, then | |
1208 | this is an error and error () is called. Otherwise, it is | |
1209 | a warning and printf() is called. */ | |
1210 | void | |
c5aa993b JM |
1211 | op_error (fmt, op, fatal) |
1212 | char *fmt; | |
1213 | enum exp_opcode op; | |
1214 | int fatal; | |
c906108c | 1215 | { |
c5aa993b JM |
1216 | if (fatal) |
1217 | error (fmt, op_string (op)); | |
1218 | else | |
1219 | { | |
1220 | warning (fmt, op_string (op)); | |
1221 | } | |
c906108c SS |
1222 | } |
1223 | ||
1224 | /* These are called when a language fails a type- or range-check. | |
1225 | The first argument should be a printf()-style format string, and | |
1226 | the rest of the arguments should be its arguments. If | |
1227 | [type|range]_check is [type|range]_check_on, then return_to_top_level() | |
1228 | is called in the style of error (). Otherwise, the message is prefixed | |
1229 | by the value of warning_pre_print and we do not return to the top level. */ | |
1230 | ||
1231 | void | |
c5aa993b | 1232 | type_error (char *string,...) |
c906108c | 1233 | { |
c5aa993b | 1234 | va_list args; |
c5aa993b | 1235 | va_start (args, string); |
c906108c | 1236 | |
c5aa993b JM |
1237 | if (type_check == type_check_warn) |
1238 | fprintf_filtered (gdb_stderr, warning_pre_print); | |
1239 | else | |
1240 | error_begin (); | |
c906108c | 1241 | |
c5aa993b JM |
1242 | vfprintf_filtered (gdb_stderr, string, args); |
1243 | fprintf_filtered (gdb_stderr, "\n"); | |
1244 | va_end (args); | |
1245 | if (type_check == type_check_on) | |
1246 | return_to_top_level (RETURN_ERROR); | |
c906108c SS |
1247 | } |
1248 | ||
1249 | void | |
c5aa993b | 1250 | range_error (char *string,...) |
c906108c | 1251 | { |
c5aa993b | 1252 | va_list args; |
c5aa993b | 1253 | va_start (args, string); |
c906108c | 1254 | |
c5aa993b JM |
1255 | if (range_check == range_check_warn) |
1256 | fprintf_filtered (gdb_stderr, warning_pre_print); | |
1257 | else | |
1258 | error_begin (); | |
c906108c | 1259 | |
c5aa993b JM |
1260 | vfprintf_filtered (gdb_stderr, string, args); |
1261 | fprintf_filtered (gdb_stderr, "\n"); | |
1262 | va_end (args); | |
1263 | if (range_check == range_check_on) | |
1264 | return_to_top_level (RETURN_ERROR); | |
c906108c | 1265 | } |
c906108c | 1266 | \f |
c5aa993b | 1267 | |
c906108c SS |
1268 | /* This page contains miscellaneous functions */ |
1269 | ||
1270 | /* Return the language enum for a given language string. */ | |
1271 | ||
1272 | enum language | |
1273 | language_enum (str) | |
1274 | char *str; | |
1275 | { | |
1276 | int i; | |
1277 | ||
c5aa993b | 1278 | for (i = 0; i < languages_size; i++) |
c906108c SS |
1279 | if (STREQ (languages[i]->la_name, str)) |
1280 | return languages[i]->la_language; | |
1281 | ||
1282 | return language_unknown; | |
1283 | } | |
1284 | ||
1285 | /* Return the language struct for a given language enum. */ | |
1286 | ||
1287 | const struct language_defn * | |
c5aa993b JM |
1288 | language_def (lang) |
1289 | enum language lang; | |
c906108c SS |
1290 | { |
1291 | int i; | |
1292 | ||
c5aa993b JM |
1293 | for (i = 0; i < languages_size; i++) |
1294 | { | |
1295 | if (languages[i]->la_language == lang) | |
1296 | { | |
1297 | return languages[i]; | |
1298 | } | |
c906108c | 1299 | } |
c906108c SS |
1300 | return NULL; |
1301 | } | |
1302 | ||
1303 | /* Return the language as a string */ | |
1304 | char * | |
c5aa993b JM |
1305 | language_str (lang) |
1306 | enum language lang; | |
c906108c SS |
1307 | { |
1308 | int i; | |
1309 | ||
c5aa993b JM |
1310 | for (i = 0; i < languages_size; i++) |
1311 | { | |
1312 | if (languages[i]->la_language == lang) | |
1313 | { | |
1314 | return languages[i]->la_name; | |
1315 | } | |
c906108c | 1316 | } |
c906108c SS |
1317 | return "Unknown"; |
1318 | } | |
1319 | ||
1320 | static void | |
1321 | set_check (ignore, from_tty) | |
c5aa993b JM |
1322 | char *ignore; |
1323 | int from_tty; | |
c906108c | 1324 | { |
c5aa993b JM |
1325 | printf_unfiltered ( |
1326 | "\"set check\" must be followed by the name of a check subcommand.\n"); | |
1327 | help_list (setchecklist, "set check ", -1, gdb_stdout); | |
c906108c SS |
1328 | } |
1329 | ||
1330 | static void | |
1331 | show_check (ignore, from_tty) | |
c5aa993b JM |
1332 | char *ignore; |
1333 | int from_tty; | |
c906108c | 1334 | { |
c5aa993b | 1335 | cmd_show_list (showchecklist, from_tty, ""); |
c906108c SS |
1336 | } |
1337 | \f | |
1338 | /* Add a language to the set of known languages. */ | |
1339 | ||
1340 | void | |
1341 | add_language (lang) | |
1342 | const struct language_defn *lang; | |
1343 | { | |
1344 | if (lang->la_magic != LANG_MAGIC) | |
1345 | { | |
c5aa993b JM |
1346 | fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n", |
1347 | lang->la_name); | |
1348 | abort (); | |
c906108c SS |
1349 | } |
1350 | ||
1351 | if (!languages) | |
1352 | { | |
1353 | languages_allocsize = DEFAULT_ALLOCSIZE; | |
1354 | languages = (const struct language_defn **) xmalloc | |
1355 | (languages_allocsize * sizeof (*languages)); | |
1356 | } | |
1357 | if (languages_size >= languages_allocsize) | |
1358 | { | |
1359 | languages_allocsize *= 2; | |
1360 | languages = (const struct language_defn **) xrealloc ((char *) languages, | |
c5aa993b | 1361 | languages_allocsize * sizeof (*languages)); |
c906108c SS |
1362 | } |
1363 | languages[languages_size++] = lang; | |
1364 | } | |
1365 | ||
1366 | /* Define the language that is no language. */ | |
1367 | ||
1368 | static int | |
1369 | unk_lang_parser () | |
1370 | { | |
1371 | return 1; | |
1372 | } | |
1373 | ||
1374 | static void | |
1375 | unk_lang_error (msg) | |
1376 | char *msg; | |
1377 | { | |
1378 | error ("Attempted to parse an expression with unknown language"); | |
1379 | } | |
1380 | ||
1381 | static void | |
1382 | unk_lang_emit_char (c, stream, quoter) | |
1383 | register int c; | |
d9fcf2fb | 1384 | struct ui_file *stream; |
c906108c SS |
1385 | int quoter; |
1386 | { | |
1387 | error ("internal error - unimplemented function unk_lang_emit_char called."); | |
1388 | } | |
1389 | ||
1390 | static void | |
1391 | unk_lang_printchar (c, stream) | |
1392 | register int c; | |
d9fcf2fb | 1393 | struct ui_file *stream; |
c906108c SS |
1394 | { |
1395 | error ("internal error - unimplemented function unk_lang_printchar called."); | |
1396 | } | |
1397 | ||
1398 | static void | |
1399 | unk_lang_printstr (stream, string, length, width, force_ellipses) | |
d9fcf2fb | 1400 | struct ui_file *stream; |
c906108c SS |
1401 | char *string; |
1402 | unsigned int length; | |
1403 | int width; | |
1404 | int force_ellipses; | |
1405 | { | |
1406 | error ("internal error - unimplemented function unk_lang_printstr called."); | |
1407 | } | |
1408 | ||
1409 | static struct type * | |
1410 | unk_lang_create_fundamental_type (objfile, typeid) | |
1411 | struct objfile *objfile; | |
1412 | int typeid; | |
1413 | { | |
1414 | error ("internal error - unimplemented function unk_lang_create_fundamental_type called."); | |
1415 | } | |
1416 | ||
1417 | static void | |
1418 | unk_lang_print_type (type, varstring, stream, show, level) | |
1419 | struct type *type; | |
1420 | char *varstring; | |
d9fcf2fb | 1421 | struct ui_file *stream; |
c906108c SS |
1422 | int show; |
1423 | int level; | |
1424 | { | |
1425 | error ("internal error - unimplemented function unk_lang_print_type called."); | |
1426 | } | |
1427 | ||
1428 | static int | |
c5aa993b | 1429 | unk_lang_val_print (type, valaddr, embedded_offset, address, stream, format, deref_ref, |
c906108c SS |
1430 | recurse, pretty) |
1431 | struct type *type; | |
1432 | char *valaddr; | |
1433 | int embedded_offset; | |
1434 | CORE_ADDR address; | |
d9fcf2fb | 1435 | struct ui_file *stream; |
c906108c SS |
1436 | int format; |
1437 | int deref_ref; | |
1438 | int recurse; | |
1439 | enum val_prettyprint pretty; | |
1440 | { | |
1441 | error ("internal error - unimplemented function unk_lang_val_print called."); | |
1442 | } | |
1443 | ||
1444 | static int | |
1445 | unk_lang_value_print (val, stream, format, pretty) | |
1446 | value_ptr val; | |
d9fcf2fb | 1447 | struct ui_file *stream; |
c906108c SS |
1448 | int format; |
1449 | enum val_prettyprint pretty; | |
1450 | { | |
1451 | error ("internal error - unimplemented function unk_lang_value_print called."); | |
1452 | } | |
1453 | ||
c5aa993b JM |
1454 | static struct type **CONST_PTR (unknown_builtin_types[]) = |
1455 | { | |
1456 | 0 | |
1457 | }; | |
1458 | static const struct op_print unk_op_print_tab[] = | |
1459 | { | |
1460 | {NULL, OP_NULL, PREC_NULL, 0} | |
c906108c SS |
1461 | }; |
1462 | ||
c5aa993b JM |
1463 | const struct language_defn unknown_language_defn = |
1464 | { | |
c906108c SS |
1465 | "unknown", |
1466 | language_unknown, | |
1467 | &unknown_builtin_types[0], | |
1468 | range_check_off, | |
1469 | type_check_off, | |
1470 | unk_lang_parser, | |
1471 | unk_lang_error, | |
1472 | evaluate_subexp_standard, | |
1473 | unk_lang_printchar, /* Print character constant */ | |
1474 | unk_lang_printstr, | |
1475 | unk_lang_emit_char, | |
1476 | unk_lang_create_fundamental_type, | |
1477 | unk_lang_print_type, /* Print a type using appropriate syntax */ | |
1478 | unk_lang_val_print, /* Print a value using appropriate syntax */ | |
1479 | unk_lang_value_print, /* Print a top-level value */ | |
c5aa993b JM |
1480 | {"", "", "", ""}, /* Binary format info */ |
1481 | {"0%lo", "0", "o", ""}, /* Octal format info */ | |
1482 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
1483 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
c906108c SS |
1484 | unk_op_print_tab, /* expression operators for printing */ |
1485 | 1, /* c-style arrays */ | |
1486 | 0, /* String lower bound */ | |
c5aa993b | 1487 | &builtin_type_char, /* Type of string elements */ |
c906108c SS |
1488 | LANG_MAGIC |
1489 | }; | |
1490 | ||
1491 | /* These two structs define fake entries for the "local" and "auto" options. */ | |
c5aa993b JM |
1492 | const struct language_defn auto_language_defn = |
1493 | { | |
c906108c SS |
1494 | "auto", |
1495 | language_auto, | |
1496 | &unknown_builtin_types[0], | |
1497 | range_check_off, | |
1498 | type_check_off, | |
1499 | unk_lang_parser, | |
1500 | unk_lang_error, | |
1501 | evaluate_subexp_standard, | |
1502 | unk_lang_printchar, /* Print character constant */ | |
1503 | unk_lang_printstr, | |
1504 | unk_lang_emit_char, | |
1505 | unk_lang_create_fundamental_type, | |
1506 | unk_lang_print_type, /* Print a type using appropriate syntax */ | |
1507 | unk_lang_val_print, /* Print a value using appropriate syntax */ | |
1508 | unk_lang_value_print, /* Print a top-level value */ | |
c5aa993b JM |
1509 | {"", "", "", ""}, /* Binary format info */ |
1510 | {"0%lo", "0", "o", ""}, /* Octal format info */ | |
1511 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
1512 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
c906108c SS |
1513 | unk_op_print_tab, /* expression operators for printing */ |
1514 | 1, /* c-style arrays */ | |
1515 | 0, /* String lower bound */ | |
c5aa993b | 1516 | &builtin_type_char, /* Type of string elements */ |
c906108c SS |
1517 | LANG_MAGIC |
1518 | }; | |
1519 | ||
c5aa993b JM |
1520 | const struct language_defn local_language_defn = |
1521 | { | |
c906108c SS |
1522 | "local", |
1523 | language_auto, | |
1524 | &unknown_builtin_types[0], | |
1525 | range_check_off, | |
1526 | type_check_off, | |
1527 | unk_lang_parser, | |
1528 | unk_lang_error, | |
1529 | evaluate_subexp_standard, | |
1530 | unk_lang_printchar, /* Print character constant */ | |
1531 | unk_lang_printstr, | |
1532 | unk_lang_emit_char, | |
1533 | unk_lang_create_fundamental_type, | |
1534 | unk_lang_print_type, /* Print a type using appropriate syntax */ | |
1535 | unk_lang_val_print, /* Print a value using appropriate syntax */ | |
1536 | unk_lang_value_print, /* Print a top-level value */ | |
c5aa993b JM |
1537 | {"", "", "", ""}, /* Binary format info */ |
1538 | {"0%lo", "0", "o", ""}, /* Octal format info */ | |
1539 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
1540 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
c906108c SS |
1541 | unk_op_print_tab, /* expression operators for printing */ |
1542 | 1, /* c-style arrays */ | |
1543 | 0, /* String lower bound */ | |
c5aa993b | 1544 | &builtin_type_char, /* Type of string elements */ |
c906108c SS |
1545 | LANG_MAGIC |
1546 | }; | |
1547 | \f | |
1548 | /* Initialize the language routines */ | |
1549 | ||
1550 | void | |
c5aa993b | 1551 | _initialize_language () |
c906108c | 1552 | { |
c5aa993b JM |
1553 | struct cmd_list_element *set, *show; |
1554 | ||
1555 | /* GDB commands for language specific stuff */ | |
1556 | ||
1557 | set = add_set_cmd ("language", class_support, var_string_noescape, | |
1558 | (char *) &language, | |
1559 | "Set the current source language.", | |
1560 | &setlist); | |
1561 | show = add_show_from_set (set, &showlist); | |
1562 | set->function.cfunc = set_language_command; | |
1563 | show->function.cfunc = show_language_command; | |
1564 | ||
1565 | add_prefix_cmd ("check", no_class, set_check, | |
1566 | "Set the status of the type/range checker", | |
1567 | &setchecklist, "set check ", 0, &setlist); | |
1568 | add_alias_cmd ("c", "check", no_class, 1, &setlist); | |
1569 | add_alias_cmd ("ch", "check", no_class, 1, &setlist); | |
1570 | ||
1571 | add_prefix_cmd ("check", no_class, show_check, | |
1572 | "Show the status of the type/range checker", | |
1573 | &showchecklist, "show check ", 0, &showlist); | |
1574 | add_alias_cmd ("c", "check", no_class, 1, &showlist); | |
1575 | add_alias_cmd ("ch", "check", no_class, 1, &showlist); | |
1576 | ||
1577 | set = add_set_cmd ("type", class_support, var_string_noescape, | |
1578 | (char *) &type, | |
1579 | "Set type checking. (on/warn/off/auto)", | |
1580 | &setchecklist); | |
1581 | show = add_show_from_set (set, &showchecklist); | |
1582 | set->function.cfunc = set_type_command; | |
1583 | show->function.cfunc = show_type_command; | |
1584 | ||
1585 | set = add_set_cmd ("range", class_support, var_string_noescape, | |
1586 | (char *) &range, | |
1587 | "Set range checking. (on/warn/off/auto)", | |
1588 | &setchecklist); | |
1589 | show = add_show_from_set (set, &showchecklist); | |
1590 | set->function.cfunc = set_range_command; | |
1591 | show->function.cfunc = show_range_command; | |
1592 | ||
1593 | add_language (&unknown_language_defn); | |
1594 | add_language (&local_language_defn); | |
1595 | add_language (&auto_language_defn); | |
1596 | ||
1597 | language = savestring ("auto", strlen ("auto")); | |
c5aa993b | 1598 | set_language_command (language, 0); |
ed9a39eb JM |
1599 | |
1600 | type = savestring ("auto", strlen ("auto")); | |
c5aa993b | 1601 | set_type_command (NULL, 0); |
ed9a39eb JM |
1602 | |
1603 | range = savestring ("auto", strlen ("auto")); | |
c5aa993b | 1604 | set_range_command (NULL, 0); |
c906108c | 1605 | } |