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