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