Commit | Line | Data |
---|---|---|
c8023e66 | 1 | /* Multiple source language support for GDB. |
7ed0f002 | 2 | Copyright 1991, 1992 Free Software Foundation, Inc. |
c8023e66 JG |
3 | Contributed by the Department of Computer Science at the State University |
4 | of New York at Buffalo. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | /* This file contains functions that return things that are specific | |
23 | to languages. Each function should examine current_language if necessary, | |
24 | and return the appropriate result. */ | |
25 | ||
26 | /* FIXME: Most of these would be better organized as macros which | |
27 | return data out of a "language-specific" struct pointer that is set | |
28 | whenever the working language changes. That would be a lot faster. */ | |
29 | ||
d747e0af | 30 | #include "defs.h" |
c8023e66 JG |
31 | #include <string.h> |
32 | #include <varargs.h> | |
33 | ||
c8023e66 | 34 | #include "symtab.h" |
1ab3bf1b | 35 | #include "gdbtypes.h" |
c8023e66 JG |
36 | #include "value.h" |
37 | #include "gdbcmd.h" | |
38 | #include "frame.h" | |
c8023e66 | 39 | #include "expression.h" |
7ed0f002 | 40 | #include "language.h" |
c8023e66 JG |
41 | #include "target.h" |
42 | #include "parser-defs.h" | |
43 | ||
7ed0f002 JG |
44 | static void |
45 | show_language_command PARAMS ((char *, int)); | |
46 | ||
47 | static void | |
48 | set_language_command PARAMS ((char *, int)); | |
49 | ||
50 | static void | |
51 | show_type_command PARAMS ((char *, int)); | |
c4668207 | 52 | |
7ed0f002 JG |
53 | static void |
54 | set_type_command PARAMS ((char *, int)); | |
55 | ||
56 | static void | |
57 | show_range_command PARAMS ((char *, int)); | |
58 | ||
59 | static void | |
60 | set_range_command PARAMS ((char *, int)); | |
61 | ||
62 | static void | |
63 | set_range_str PARAMS ((void)); | |
64 | ||
65 | static void | |
66 | set_type_str PARAMS ((void)); | |
67 | ||
68 | static void | |
69 | set_lang_str PARAMS ((void)); | |
70 | ||
71 | static void | |
72 | unk_lang_error PARAMS ((char *)); | |
73 | ||
74 | static int | |
75 | unk_lang_parser PARAMS ((void)); | |
76 | ||
77 | static void | |
78 | show_check PARAMS ((char *, int)); | |
79 | ||
80 | static void | |
81 | set_check PARAMS ((char *, int)); | |
82 | ||
83 | static void | |
84 | set_type_range PARAMS ((void)); | |
c8023e66 JG |
85 | |
86 | /* Forward declaration */ | |
0c6efbcc | 87 | extern const struct language_defn unknown_language_defn; |
318bf84f | 88 | extern char *warning_pre_print; |
7ed0f002 | 89 | |
c8023e66 JG |
90 | /* The current (default at startup) state of type and range checking. |
91 | (If the modes are set to "auto", though, these are changed based | |
92 | on the default language at startup, and then again based on the | |
93 | language of the first source file. */ | |
94 | ||
95 | enum range_mode range_mode = range_mode_auto; | |
96 | enum range_check range_check = range_check_off; | |
97 | enum type_mode type_mode = type_mode_auto; | |
98 | enum type_check type_check = type_check_off; | |
99 | ||
100 | /* The current language and language_mode (see language.h) */ | |
101 | ||
0c6efbcc | 102 | const struct language_defn *current_language = &unknown_language_defn; |
c8023e66 JG |
103 | enum language_mode language_mode = language_mode_auto; |
104 | ||
b5af69c3 JG |
105 | /* The language that the user expects to be typing in (the language |
106 | of main(), or the last language we notified them about, or C). */ | |
107 | ||
108 | const struct language_defn *expected_language; | |
109 | ||
c8023e66 JG |
110 | /* The list of supported languages. The list itself is malloc'd. */ |
111 | ||
7ed0f002 | 112 | static const struct language_defn **languages; |
c8023e66 JG |
113 | static unsigned languages_size; |
114 | static unsigned languages_allocsize; | |
115 | #define DEFAULT_ALLOCSIZE 3 | |
116 | ||
117 | /* The "set language/type/range" commands all put stuff in these | |
118 | buffers. This is to make them work as set/show commands. The | |
119 | user's string is copied here, then the set_* commands look at | |
120 | them and update them to something that looks nice when it is | |
121 | printed out. */ | |
122 | ||
123 | static char *language; | |
124 | static char *type; | |
125 | static char *range; | |
126 | ||
127 | /* Warning issued when current_language and the language of the current | |
128 | frame do not match. */ | |
129 | char lang_frame_mismatch_warn[] = | |
130 | "Warning: the current language does not match this frame."; | |
131 | ||
c8023e66 JG |
132 | \f |
133 | /* This page contains the functions corresponding to GDB commands | |
134 | and their helpers. */ | |
135 | ||
136 | /* Show command. Display a warning if the language set | |
137 | does not match the frame. */ | |
7ed0f002 | 138 | static void |
d8ce1326 JG |
139 | show_language_command (ignore, from_tty) |
140 | char *ignore; | |
c8023e66 JG |
141 | int from_tty; |
142 | { | |
143 | enum language flang; /* The language of the current frame */ | |
144 | ||
145 | flang = get_frame_language(); | |
146 | if (flang != language_unknown && | |
147 | language_mode == language_mode_manual && | |
148 | current_language->la_language != flang) | |
149 | printf_filtered("%s\n",lang_frame_mismatch_warn); | |
150 | } | |
151 | ||
152 | /* Set command. Change the current working language. */ | |
7ed0f002 | 153 | static void |
d8ce1326 JG |
154 | set_language_command (ignore, from_tty) |
155 | char *ignore; | |
c8023e66 JG |
156 | int from_tty; |
157 | { | |
158 | int i; | |
159 | enum language flang; | |
5f3d478e | 160 | char *err_lang; |
c8023e66 JG |
161 | |
162 | /* FIXME -- do this from the list, with HELP. */ | |
163 | if (!language || !language[0]) { | |
597dc86b | 164 | printf("The currently understood settings are:\n\n\ |
c8023e66 | 165 | local or auto Automatic setting based on source file\n\ |
0b798409 | 166 | c Use the C language\n\ |
545af6ce | 167 | c++ Use the C++ language\n\ |
0b798409 JG |
168 | modula-2 Use the Modula-2 language\n"); |
169 | /* Restore the silly string. */ | |
170 | set_language(current_language->la_language); | |
c8023e66 JG |
171 | return; |
172 | } | |
173 | ||
174 | /* Search the list of languages for a match. */ | |
175 | for (i = 0; i < languages_size; i++) { | |
176 | if (!strcmp (languages[i]->la_name, language)) { | |
177 | /* Found it! Go into manual mode, and use this language. */ | |
178 | if (languages[i]->la_language == language_auto) { | |
179 | /* Enter auto mode. Set to the current frame's language, if known. */ | |
180 | language_mode = language_mode_auto; | |
181 | flang = get_frame_language(); | |
182 | if (flang!=language_unknown) | |
183 | set_language(flang); | |
b5af69c3 | 184 | expected_language = current_language; |
c8023e66 JG |
185 | return; |
186 | } else { | |
187 | /* Enter manual mode. Set the specified language. */ | |
188 | language_mode = language_mode_manual; | |
189 | current_language = languages[i]; | |
190 | set_type_range (); | |
191 | set_lang_str(); | |
b5af69c3 | 192 | expected_language = current_language; |
c8023e66 JG |
193 | return; |
194 | } | |
195 | } | |
196 | } | |
197 | ||
5f3d478e JG |
198 | /* Reset the language (esp. the global string "language") to the |
199 | correct values. */ | |
200 | err_lang=savestring(language,strlen(language)); | |
201 | make_cleanup (free, err_lang); /* Free it after error */ | |
202 | set_language(current_language->la_language); | |
203 | error ("Unknown language `%s'.",err_lang); | |
c8023e66 JG |
204 | } |
205 | ||
206 | /* Show command. Display a warning if the type setting does | |
207 | not match the current language. */ | |
7ed0f002 | 208 | static void |
d8ce1326 JG |
209 | show_type_command(ignore, from_tty) |
210 | char *ignore; | |
c8023e66 JG |
211 | int from_tty; |
212 | { | |
213 | if (type_check != current_language->la_type_check) | |
597dc86b | 214 | printf( |
c8023e66 JG |
215 | "Warning: the current type check setting does not match the language.\n"); |
216 | } | |
217 | ||
218 | /* Set command. Change the setting for type checking. */ | |
7ed0f002 | 219 | static void |
d8ce1326 JG |
220 | set_type_command(ignore, from_tty) |
221 | char *ignore; | |
c8023e66 JG |
222 | int from_tty; |
223 | { | |
224 | if (!strcmp(type,"on")) | |
225 | { | |
226 | type_check = type_check_on; | |
227 | type_mode = type_mode_manual; | |
228 | } | |
229 | else if (!strcmp(type,"warn")) | |
230 | { | |
231 | type_check = type_check_warn; | |
232 | type_mode = type_mode_manual; | |
233 | } | |
234 | else if (!strcmp(type,"off")) | |
235 | { | |
236 | type_check = type_check_off; | |
237 | type_mode = type_mode_manual; | |
238 | } | |
239 | else if (!strcmp(type,"auto")) | |
240 | { | |
241 | type_mode = type_mode_auto; | |
242 | set_type_range(); | |
243 | /* Avoid hitting the set_type_str call below. We | |
244 | did it in set_type_range. */ | |
245 | return; | |
246 | } | |
247 | set_type_str(); | |
d8ce1326 | 248 | show_type_command((char *)NULL, from_tty); |
c8023e66 JG |
249 | } |
250 | ||
251 | /* Show command. Display a warning if the range setting does | |
252 | not match the current language. */ | |
7ed0f002 | 253 | static void |
d8ce1326 JG |
254 | show_range_command(ignore, from_tty) |
255 | char *ignore; | |
c8023e66 JG |
256 | int from_tty; |
257 | { | |
258 | ||
259 | if (range_check != current_language->la_range_check) | |
597dc86b | 260 | printf( |
c8023e66 JG |
261 | "Warning: the current range check setting does not match the language.\n"); |
262 | } | |
263 | ||
264 | /* Set command. Change the setting for range checking. */ | |
7ed0f002 | 265 | static void |
d8ce1326 JG |
266 | set_range_command(ignore, from_tty) |
267 | char *ignore; | |
c8023e66 JG |
268 | int from_tty; |
269 | { | |
270 | if (!strcmp(range,"on")) | |
271 | { | |
272 | range_check = range_check_on; | |
273 | range_mode = range_mode_manual; | |
274 | } | |
275 | else if (!strcmp(range,"warn")) | |
276 | { | |
277 | range_check = range_check_warn; | |
278 | range_mode = range_mode_manual; | |
279 | } | |
280 | else if (!strcmp(range,"off")) | |
281 | { | |
282 | range_check = range_check_off; | |
283 | range_mode = range_mode_manual; | |
284 | } | |
285 | else if (!strcmp(range,"auto")) | |
286 | { | |
287 | range_mode = range_mode_auto; | |
288 | set_type_range(); | |
289 | /* Avoid hitting the set_range_str call below. We | |
290 | did it in set_type_range. */ | |
291 | return; | |
292 | } | |
293 | set_range_str(); | |
d8ce1326 | 294 | show_range_command((char *)0, from_tty); |
c8023e66 JG |
295 | } |
296 | ||
297 | /* Set the status of range and type checking based on | |
298 | the current modes and the current language. | |
299 | If SHOW is non-zero, then print out the current language, | |
300 | type and range checking status. */ | |
301 | static void | |
302 | set_type_range() | |
303 | { | |
c8023e66 JG |
304 | |
305 | if (range_mode == range_mode_auto) | |
306 | range_check = current_language->la_range_check; | |
307 | ||
308 | if (type_mode == type_mode_auto) | |
309 | type_check = current_language->la_type_check; | |
310 | ||
311 | set_type_str(); | |
312 | set_range_str(); | |
313 | } | |
314 | ||
315 | /* Set current language to (enum language) LANG. */ | |
316 | ||
317 | void | |
318 | set_language(lang) | |
319 | enum language lang; | |
320 | { | |
321 | int i; | |
322 | ||
323 | for (i = 0; i < languages_size; i++) { | |
324 | if (languages[i]->la_language == lang) { | |
325 | current_language = languages[i]; | |
326 | set_type_range (); | |
327 | set_lang_str(); | |
545af6ce | 328 | break; |
c8023e66 JG |
329 | } |
330 | } | |
331 | } | |
332 | \f | |
333 | /* This page contains functions that update the global vars | |
334 | language, type and range. */ | |
7ed0f002 | 335 | static void |
c8023e66 JG |
336 | set_lang_str() |
337 | { | |
d8ce1326 | 338 | char *prefix = ""; |
c8023e66 JG |
339 | |
340 | free (language); | |
341 | if (language_mode == language_mode_auto) | |
342 | prefix = "auto; currently "; | |
343 | ||
58ae87f6 | 344 | language = concat(prefix, current_language->la_name, NULL); |
c8023e66 JG |
345 | } |
346 | ||
7ed0f002 | 347 | static void |
c8023e66 JG |
348 | set_type_str() |
349 | { | |
350 | char *tmp, *prefix = ""; | |
351 | ||
352 | free (type); | |
353 | if (type_mode==type_mode_auto) | |
354 | prefix = "auto; currently "; | |
355 | ||
356 | switch(type_check) | |
357 | { | |
358 | case type_check_on: | |
359 | tmp = "on"; | |
360 | break; | |
361 | case type_check_off: | |
362 | tmp = "off"; | |
363 | break; | |
364 | case type_check_warn: | |
365 | tmp = "warn"; | |
366 | break; | |
367 | default: | |
368 | error ("Unrecognized type check setting."); | |
369 | } | |
370 | ||
58ae87f6 | 371 | type = concat(prefix,tmp,NULL); |
c8023e66 JG |
372 | } |
373 | ||
7ed0f002 | 374 | static void |
c8023e66 JG |
375 | set_range_str() |
376 | { | |
377 | char *tmp, *pref = ""; | |
378 | ||
379 | free (range); | |
380 | if (range_mode==range_mode_auto) | |
381 | pref = "auto; currently "; | |
382 | ||
383 | switch(range_check) | |
384 | { | |
385 | case range_check_on: | |
386 | tmp = "on"; | |
387 | break; | |
388 | case range_check_off: | |
389 | tmp = "off"; | |
390 | break; | |
391 | case range_check_warn: | |
392 | tmp = "warn"; | |
393 | break; | |
394 | default: | |
395 | error ("Unrecognized range check setting."); | |
396 | } | |
397 | ||
58ae87f6 | 398 | range = concat(pref,tmp,NULL); |
c8023e66 JG |
399 | } |
400 | ||
401 | ||
402 | /* Print out the current language settings: language, range and | |
7ed0f002 | 403 | type checking. If QUIETLY, print only what has changed. */ |
b5af69c3 | 404 | |
c8023e66 | 405 | void |
7ed0f002 JG |
406 | language_info (quietly) |
407 | int quietly; | |
c8023e66 | 408 | { |
b5af69c3 JG |
409 | if (quietly && expected_language == current_language) |
410 | return; | |
411 | ||
412 | expected_language = current_language; | |
413 | printf("Current language: %s\n",language); | |
414 | show_language_command((char *)0, 1); | |
415 | ||
416 | if (!quietly) | |
417 | { | |
418 | printf("Type checking: %s\n",type); | |
419 | show_type_command((char *)0, 1); | |
420 | printf("Range checking: %s\n",range); | |
421 | show_range_command((char *)0, 1); | |
422 | } | |
c8023e66 JG |
423 | } |
424 | \f | |
425 | /* Return the result of a binary operation. */ | |
7ed0f002 JG |
426 | |
427 | #if 0 /* Currently unused */ | |
428 | ||
c8023e66 JG |
429 | struct type * |
430 | binop_result_type(v1,v2) | |
431 | value v1,v2; | |
432 | { | |
433 | int l1,l2,size,uns; | |
434 | ||
d8ce1326 JG |
435 | l1 = TYPE_LENGTH(VALUE_TYPE(v1)); |
436 | l2 = TYPE_LENGTH(VALUE_TYPE(v2)); | |
c8023e66 JG |
437 | |
438 | switch(current_language->la_language) | |
439 | { | |
440 | case language_c: | |
545af6ce | 441 | case language_cplus: |
c8023e66 JG |
442 | if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT) |
443 | return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ? | |
444 | VALUE_TYPE(v2) : VALUE_TYPE(v1); | |
445 | else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT) | |
446 | return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ? | |
447 | VALUE_TYPE(v1) : VALUE_TYPE(v2); | |
448 | else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2) | |
449 | return VALUE_TYPE(v1); | |
450 | else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1) | |
451 | return VALUE_TYPE(v2); | |
452 | else /* Both are signed. Result is the longer type */ | |
453 | return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2); | |
454 | break; | |
455 | case language_m2: | |
456 | /* If we are doing type-checking, l1 should equal l2, so this is | |
457 | not needed. */ | |
458 | return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2); | |
459 | break; | |
460 | } | |
d8ce1326 JG |
461 | abort(); |
462 | return (struct type *)0; /* For lint */ | |
c8023e66 | 463 | } |
7ed0f002 JG |
464 | |
465 | #endif /* 0 */ | |
466 | ||
c8023e66 JG |
467 | \f |
468 | /* This page contains functions that return format strings for | |
469 | printf for printing out numbers in different formats */ | |
470 | ||
471 | /* Returns the appropriate printf format for hexadecimal | |
472 | numbers. */ | |
473 | char * | |
474 | local_hex_format_custom(pre) | |
475 | char *pre; | |
476 | { | |
477 | static char form[50]; | |
478 | ||
479 | strcpy (form, current_language->la_hex_format_pre); | |
480 | strcat (form, pre); | |
481 | strcat (form, current_language->la_hex_format_suf); | |
482 | return form; | |
483 | } | |
484 | ||
485 | /* Converts a number to hexadecimal and stores it in a static | |
486 | string. Returns a pointer to this string. */ | |
487 | char * | |
488 | local_hex_string (num) | |
489 | int num; | |
490 | { | |
491 | static char res[50]; | |
492 | ||
493 | sprintf (res, current_language->la_hex_format, num); | |
494 | return res; | |
495 | } | |
496 | ||
497 | /* Converts a number to custom hexadecimal and stores it in a static | |
498 | string. Returns a pointer to this string. */ | |
499 | char * | |
500 | local_hex_string_custom(num,pre) | |
501 | int num; | |
502 | char *pre; | |
503 | { | |
504 | static char res[50]; | |
505 | ||
506 | sprintf (res, local_hex_format_custom(pre), num); | |
507 | return res; | |
508 | } | |
509 | ||
510 | /* Returns the appropriate printf format for octal | |
511 | numbers. */ | |
512 | char * | |
513 | local_octal_format_custom(pre) | |
514 | char *pre; | |
515 | { | |
516 | static char form[50]; | |
517 | ||
518 | strcpy (form, current_language->la_octal_format_pre); | |
519 | strcat (form, pre); | |
520 | strcat (form, current_language->la_octal_format_suf); | |
521 | return form; | |
522 | } | |
523 | \f | |
524 | /* This page contains functions that are used in type/range checking. | |
525 | They all return zero if the type/range check fails. | |
526 | ||
527 | It is hoped that these will make extending GDB to parse different | |
528 | languages a little easier. These are primarily used in eval.c when | |
529 | evaluating expressions and making sure that their types are correct. | |
530 | Instead of having a mess of conjucted/disjuncted expressions in an "if", | |
531 | the ideas of type can be wrapped up in the following functions. | |
532 | ||
533 | Note that some of them are not currently dependent upon which language | |
534 | is currently being parsed. For example, floats are the same in | |
535 | C and Modula-2 (ie. the only floating point type has TYPE_CODE of | |
536 | TYPE_CODE_FLT), while booleans are different. */ | |
537 | ||
538 | /* Returns non-zero if its argument is a simple type. This is the same for | |
539 | both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur, | |
540 | and thus will never cause the failure of the test. */ | |
541 | int | |
542 | simple_type(type) | |
543 | struct type *type; | |
544 | { | |
545 | switch (TYPE_CODE (type)) { | |
546 | case TYPE_CODE_INT: | |
547 | case TYPE_CODE_CHAR: | |
548 | case TYPE_CODE_ENUM: | |
549 | case TYPE_CODE_FLT: | |
550 | case TYPE_CODE_RANGE: | |
551 | case TYPE_CODE_BOOL: | |
552 | return 1; | |
553 | ||
554 | default: | |
555 | return 0; | |
556 | } | |
557 | } | |
558 | ||
559 | /* Returns non-zero if its argument is of an ordered type. */ | |
560 | int | |
561 | ordered_type (type) | |
562 | struct type *type; | |
563 | { | |
564 | switch (TYPE_CODE (type)) { | |
565 | case TYPE_CODE_INT: | |
566 | case TYPE_CODE_CHAR: | |
567 | case TYPE_CODE_ENUM: | |
568 | case TYPE_CODE_FLT: | |
569 | case TYPE_CODE_RANGE: | |
570 | return 1; | |
571 | ||
572 | default: | |
573 | return 0; | |
574 | } | |
575 | } | |
576 | ||
577 | /* Returns non-zero if the two types are the same */ | |
578 | int | |
579 | same_type (arg1, arg2) | |
580 | struct type *arg1, *arg2; | |
581 | { | |
582 | if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2)) | |
583 | /* One is structured and one isn't */ | |
584 | return 0; | |
585 | else if (structured_type(arg1) && structured_type(arg2)) | |
586 | return arg1 == arg2; | |
587 | else if (numeric_type(arg1) && numeric_type(arg2)) | |
588 | return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) && | |
589 | (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2)) | |
590 | ? 1 : 0; | |
591 | else | |
592 | return arg1==arg2; | |
593 | } | |
594 | ||
595 | /* Returns non-zero if the type is integral */ | |
596 | int | |
597 | integral_type (type) | |
598 | struct type *type; | |
599 | { | |
600 | switch(current_language->la_language) | |
601 | { | |
602 | case language_c: | |
545af6ce | 603 | case language_cplus: |
c8023e66 JG |
604 | return (TYPE_CODE(type) != TYPE_CODE_INT) && |
605 | (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1; | |
606 | case language_m2: | |
607 | return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1; | |
608 | default: | |
609 | error ("Language not supported."); | |
610 | } | |
611 | } | |
612 | ||
613 | /* Returns non-zero if the value is numeric */ | |
614 | int | |
615 | numeric_type (type) | |
616 | struct type *type; | |
617 | { | |
618 | switch (TYPE_CODE (type)) { | |
619 | case TYPE_CODE_INT: | |
620 | case TYPE_CODE_FLT: | |
621 | return 1; | |
622 | ||
623 | default: | |
624 | return 0; | |
625 | } | |
626 | } | |
627 | ||
628 | /* Returns non-zero if the value is a character type */ | |
629 | int | |
630 | character_type (type) | |
631 | struct type *type; | |
632 | { | |
633 | switch(current_language->la_language) | |
634 | { | |
635 | case language_m2: | |
636 | return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1; | |
637 | ||
638 | case language_c: | |
545af6ce | 639 | case language_cplus: |
c8023e66 JG |
640 | return (TYPE_CODE(type) == TYPE_CODE_INT) && |
641 | TYPE_LENGTH(type) == sizeof(char) | |
642 | ? 1 : 0; | |
51b57ded FF |
643 | default: |
644 | return (0); | |
c8023e66 JG |
645 | } |
646 | } | |
647 | ||
648 | /* Returns non-zero if the value is a boolean type */ | |
649 | int | |
650 | boolean_type (type) | |
651 | struct type *type; | |
652 | { | |
653 | switch(current_language->la_language) | |
654 | { | |
655 | case language_m2: | |
656 | return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1; | |
657 | ||
658 | case language_c: | |
545af6ce | 659 | case language_cplus: |
c8023e66 | 660 | return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1; |
51b57ded FF |
661 | default: |
662 | return (0); | |
c8023e66 JG |
663 | } |
664 | } | |
665 | ||
666 | /* Returns non-zero if the value is a floating-point type */ | |
667 | int | |
668 | float_type (type) | |
669 | struct type *type; | |
670 | { | |
671 | return TYPE_CODE(type) == TYPE_CODE_FLT; | |
672 | } | |
673 | ||
674 | /* Returns non-zero if the value is a pointer type */ | |
675 | int | |
676 | pointer_type(type) | |
677 | struct type *type; | |
678 | { | |
679 | return TYPE_CODE(type) == TYPE_CODE_PTR || | |
680 | TYPE_CODE(type) == TYPE_CODE_REF; | |
681 | } | |
682 | ||
683 | /* Returns non-zero if the value is a structured type */ | |
684 | int | |
685 | structured_type(type) | |
686 | struct type *type; | |
687 | { | |
688 | switch(current_language->la_language) | |
689 | { | |
690 | case language_c: | |
545af6ce | 691 | case language_cplus: |
c8023e66 JG |
692 | return (TYPE_CODE(type) == TYPE_CODE_STRUCT) || |
693 | (TYPE_CODE(type) == TYPE_CODE_UNION) || | |
694 | (TYPE_CODE(type) == TYPE_CODE_ARRAY); | |
695 | case language_m2: | |
696 | return (TYPE_CODE(type) == TYPE_CODE_STRUCT) || | |
697 | (TYPE_CODE(type) == TYPE_CODE_SET) || | |
698 | (TYPE_CODE(type) == TYPE_CODE_ARRAY); | |
51b57ded FF |
699 | default: |
700 | return (0); | |
c8023e66 JG |
701 | } |
702 | } | |
703 | \f | |
704 | /* This page contains functions that return info about | |
705 | (struct value) values used in GDB. */ | |
706 | ||
707 | /* Returns non-zero if the value VAL represents a true value. */ | |
708 | int | |
709 | value_true(val) | |
710 | value val; | |
711 | { | |
712 | int len, i; | |
713 | struct type *type; | |
714 | LONGEST v; | |
715 | ||
716 | switch (current_language->la_language) { | |
717 | ||
718 | case language_c: | |
545af6ce | 719 | case language_cplus: |
c8023e66 JG |
720 | return !value_zerop (val); |
721 | ||
722 | case language_m2: | |
723 | type = VALUE_TYPE(val); | |
724 | if (TYPE_CODE (type) != TYPE_CODE_BOOL) | |
725 | return 0; /* Not a BOOLEAN at all */ | |
726 | /* Search the fields for one that matches the current value. */ | |
727 | len = TYPE_NFIELDS (type); | |
728 | v = value_as_long (val); | |
729 | for (i = 0; i < len; i++) | |
730 | { | |
731 | QUIT; | |
732 | if (v == TYPE_FIELD_BITPOS (type, i)) | |
733 | break; | |
734 | } | |
735 | if (i >= len) | |
736 | return 0; /* Not a valid BOOLEAN value */ | |
737 | if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i))) | |
738 | return 1; /* BOOLEAN with value TRUE */ | |
739 | else | |
740 | return 0; /* BOOLEAN with value FALSE */ | |
741 | break; | |
742 | ||
743 | default: | |
744 | error ("Language not supported."); | |
745 | } | |
746 | } | |
747 | \f | |
748 | /* Returns non-zero if the operator OP is defined on | |
749 | the values ARG1 and ARG2. */ | |
7ed0f002 JG |
750 | |
751 | #if 0 /* Currently unused */ | |
752 | ||
c8023e66 JG |
753 | void |
754 | binop_type_check(arg1,arg2,op) | |
755 | value arg1,arg2; | |
756 | int op; | |
757 | { | |
758 | struct type *t1, *t2; | |
759 | ||
760 | /* If we're not checking types, always return success. */ | |
761 | if (!STRICT_TYPE) | |
762 | return; | |
763 | ||
764 | t1=VALUE_TYPE(arg1); | |
765 | if (arg2!=(value)NULL) | |
766 | t2=VALUE_TYPE(arg2); | |
767 | else | |
768 | t2=NULL; | |
769 | ||
770 | switch(op) | |
771 | { | |
772 | case BINOP_ADD: | |
773 | case BINOP_SUB: | |
774 | if ((numeric_type(t1) && pointer_type(t2)) || | |
775 | (pointer_type(t1) && numeric_type(t2))) | |
776 | { | |
318bf84f | 777 | warning ("combining pointer and integer.\n"); |
c8023e66 JG |
778 | break; |
779 | } | |
780 | case BINOP_MUL: | |
781 | case BINOP_LSH: | |
782 | case BINOP_RSH: | |
783 | if (!numeric_type(t1) || !numeric_type(t2)) | |
784 | type_op_error ("Arguments to %s must be numbers.",op); | |
785 | else if (!same_type(t1,t2)) | |
786 | type_op_error ("Arguments to %s must be of the same type.",op); | |
787 | break; | |
788 | ||
789 | case BINOP_AND: | |
790 | case BINOP_OR: | |
791 | if (!boolean_type(t1) || !boolean_type(t2)) | |
792 | type_op_error ("Arguments to %s must be of boolean type.",op); | |
793 | break; | |
794 | ||
795 | case BINOP_EQUAL: | |
796 | if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) || | |
797 | (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1)))) | |
798 | type_op_error ("A pointer can only be compared to an integer or pointer.",op); | |
799 | else if ((pointer_type(t1) && integral_type(t2)) || | |
800 | (integral_type(t1) && pointer_type(t2))) | |
801 | { | |
318bf84f | 802 | warning ("combining integer and pointer.\n"); |
c8023e66 JG |
803 | break; |
804 | } | |
805 | else if (!simple_type(t1) || !simple_type(t2)) | |
806 | type_op_error ("Arguments to %s must be of simple type.",op); | |
807 | else if (!same_type(t1,t2)) | |
808 | type_op_error ("Arguments to %s must be of the same type.",op); | |
809 | break; | |
810 | ||
811 | case BINOP_REM: | |
812 | if (!integral_type(t1) || !integral_type(t2)) | |
813 | type_op_error ("Arguments to %s must be of integral type.",op); | |
814 | break; | |
815 | ||
816 | case BINOP_LESS: | |
817 | case BINOP_GTR: | |
818 | case BINOP_LEQ: | |
819 | case BINOP_GEQ: | |
820 | if (!ordered_type(t1) || !ordered_type(t2)) | |
821 | type_op_error ("Arguments to %s must be of ordered type.",op); | |
822 | else if (!same_type(t1,t2)) | |
823 | type_op_error ("Arguments to %s must be of the same type.",op); | |
824 | break; | |
825 | ||
826 | case BINOP_ASSIGN: | |
827 | if (pointer_type(t1) && !integral_type(t2)) | |
828 | type_op_error ("A pointer can only be assigned an integer.",op); | |
829 | else if (pointer_type(t1) && integral_type(t2)) | |
830 | { | |
318bf84f | 831 | warning ("combining integer and pointer."); |
c8023e66 JG |
832 | break; |
833 | } | |
834 | else if (!simple_type(t1) || !simple_type(t2)) | |
835 | type_op_error ("Arguments to %s must be of simple type.",op); | |
836 | else if (!same_type(t1,t2)) | |
837 | type_op_error ("Arguments to %s must be of the same type.",op); | |
838 | break; | |
839 | ||
840 | /* Unary checks -- arg2 is null */ | |
841 | ||
842 | case UNOP_ZEROP: | |
843 | if (!boolean_type(t1)) | |
844 | type_op_error ("Argument to %s must be of boolean type.",op); | |
845 | break; | |
846 | ||
847 | case UNOP_PLUS: | |
848 | case UNOP_NEG: | |
849 | if (!numeric_type(t1)) | |
850 | type_op_error ("Argument to %s must be of numeric type.",op); | |
851 | break; | |
852 | ||
853 | case UNOP_IND: | |
854 | if (integral_type(t1)) | |
855 | { | |
318bf84f | 856 | warning ("combining pointer and integer.\n"); |
c8023e66 JG |
857 | break; |
858 | } | |
859 | else if (!pointer_type(t1)) | |
860 | type_op_error ("Argument to %s must be a pointer.",op); | |
861 | break; | |
862 | ||
863 | case UNOP_PREINCREMENT: | |
864 | case UNOP_POSTINCREMENT: | |
865 | case UNOP_PREDECREMENT: | |
866 | case UNOP_POSTDECREMENT: | |
867 | if (!ordered_type(t1)) | |
868 | type_op_error ("Argument to %s must be of an ordered type.",op); | |
869 | break; | |
870 | ||
871 | default: | |
872 | /* Ok. The following operators have different meanings in | |
873 | different languages. */ | |
874 | switch(current_language->la_language) | |
875 | { | |
876 | #ifdef _LANG_c | |
877 | case language_c: | |
545af6ce | 878 | case language_cplus: |
c8023e66 JG |
879 | switch(op) |
880 | { | |
881 | case BINOP_DIV: | |
882 | if (!numeric_type(t1) || !numeric_type(t2)) | |
883 | type_op_error ("Arguments to %s must be numbers.",op); | |
884 | break; | |
885 | } | |
886 | break; | |
887 | #endif | |
888 | ||
889 | #ifdef _LANG_m2 | |
890 | case language_m2: | |
891 | switch(op) | |
892 | { | |
893 | case BINOP_DIV: | |
894 | if (!float_type(t1) || !float_type(t2)) | |
895 | type_op_error ("Arguments to %s must be floating point numbers.",op); | |
896 | break; | |
897 | case BINOP_INTDIV: | |
898 | if (!integral_type(t1) || !integral_type(t2)) | |
899 | type_op_error ("Arguments to %s must be of integral type.",op); | |
900 | break; | |
901 | } | |
902 | #endif | |
903 | } | |
904 | } | |
905 | } | |
7ed0f002 JG |
906 | |
907 | #endif /* 0 */ | |
908 | ||
c8023e66 JG |
909 | \f |
910 | /* This page contains functions for the printing out of | |
911 | error messages that occur during type- and range- | |
912 | checking. */ | |
913 | ||
914 | /* Prints the format string FMT with the operator as a string | |
915 | corresponding to the opcode OP. If FATAL is non-zero, then | |
916 | this is an error and error () is called. Otherwise, it is | |
917 | a warning and printf() is called. */ | |
918 | void | |
919 | op_error (fmt,op,fatal) | |
920 | char *fmt; | |
921 | enum exp_opcode op; | |
922 | int fatal; | |
923 | { | |
924 | if (fatal) | |
925 | error (fmt,op_string(op)); | |
926 | else | |
927 | { | |
318bf84f | 928 | warning (fmt,op_string(op)); |
c8023e66 JG |
929 | } |
930 | } | |
931 | ||
932 | /* These are called when a language fails a type- or range-check. | |
933 | The first argument should be a printf()-style format string, and | |
934 | the rest of the arguments should be its arguments. If | |
935 | [type|range]_check is [type|range]_check_on, then return_to_top_level() | |
936 | is called in the style of error (). Otherwise, the message is prefixed | |
318bf84f | 937 | by the value of warning_pre_print and we do not return to the top level. */ |
7ed0f002 | 938 | |
c8023e66 JG |
939 | void |
940 | type_error (va_alist) | |
7ed0f002 | 941 | va_dcl |
c8023e66 JG |
942 | { |
943 | va_list args; | |
944 | char *string; | |
945 | ||
946 | if (type_check==type_check_warn) | |
597dc86b | 947 | fprintf(stderr,warning_pre_print); |
c8023e66 JG |
948 | else |
949 | target_terminal_ours(); | |
950 | ||
951 | va_start (args); | |
952 | string = va_arg (args, char *); | |
597dc86b SG |
953 | vfprintf (stderr, string, args); |
954 | fprintf (stderr, "\n"); | |
c8023e66 JG |
955 | va_end (args); |
956 | if (type_check==type_check_on) | |
957 | return_to_top_level(); | |
958 | } | |
959 | ||
960 | void | |
961 | range_error (va_alist) | |
7ed0f002 | 962 | va_dcl |
c8023e66 JG |
963 | { |
964 | va_list args; | |
965 | char *string; | |
966 | ||
967 | if (range_check==range_check_warn) | |
597dc86b | 968 | fprintf(stderr,warning_pre_print); |
c8023e66 JG |
969 | else |
970 | target_terminal_ours(); | |
971 | ||
972 | va_start (args); | |
973 | string = va_arg (args, char *); | |
597dc86b SG |
974 | vfprintf (stderr, string, args); |
975 | fprintf (stderr, "\n"); | |
c8023e66 JG |
976 | va_end (args); |
977 | if (range_check==range_check_on) | |
978 | return_to_top_level(); | |
979 | } | |
980 | ||
981 | \f | |
982 | /* This page contains miscellaneous functions */ | |
983 | ||
984 | /* Return the language as a string */ | |
985 | char * | |
986 | language_str(lang) | |
987 | enum language lang; | |
988 | { | |
989 | int i; | |
990 | ||
991 | for (i = 0; i < languages_size; i++) { | |
992 | if (languages[i]->la_language == lang) { | |
993 | return languages[i]->la_name; | |
994 | } | |
995 | } | |
996 | return "Unknown"; | |
997 | } | |
998 | ||
c8023e66 | 999 | static void |
d8ce1326 JG |
1000 | set_check (ignore, from_tty) |
1001 | char *ignore; | |
c8023e66 JG |
1002 | int from_tty; |
1003 | { | |
597dc86b | 1004 | printf( |
c8023e66 JG |
1005 | "\"set check\" must be followed by the name of a check subcommand.\n"); |
1006 | help_list(setchecklist, "set check ", -1, stdout); | |
1007 | } | |
1008 | ||
1009 | static void | |
be772100 JG |
1010 | show_check (ignore, from_tty) |
1011 | char *ignore; | |
c8023e66 JG |
1012 | int from_tty; |
1013 | { | |
1014 | cmd_show_list(showchecklist, from_tty, ""); | |
1015 | } | |
1016 | \f | |
1017 | /* Add a language to the set of known languages. */ | |
1018 | ||
1019 | void | |
1020 | add_language (lang) | |
7ed0f002 | 1021 | const struct language_defn *lang; |
c8023e66 JG |
1022 | { |
1023 | if (lang->la_magic != LANG_MAGIC) | |
1024 | { | |
597dc86b | 1025 | fprintf(stderr, "Magic number of %s language struct wrong\n", |
c8023e66 JG |
1026 | lang->la_name); |
1027 | abort(); | |
1028 | } | |
1029 | ||
1030 | if (!languages) | |
1031 | { | |
1032 | languages_allocsize = DEFAULT_ALLOCSIZE; | |
7ed0f002 | 1033 | languages = (const struct language_defn **) xmalloc |
c8023e66 JG |
1034 | (languages_allocsize * sizeof (*languages)); |
1035 | } | |
1036 | if (languages_size >= languages_allocsize) | |
1037 | { | |
1038 | languages_allocsize *= 2; | |
7ed0f002 | 1039 | languages = (const struct language_defn **) xrealloc ((char *) languages, |
c8023e66 JG |
1040 | languages_allocsize * sizeof (*languages)); |
1041 | } | |
1042 | languages[languages_size++] = lang; | |
c8023e66 JG |
1043 | } |
1044 | ||
1045 | /* Define the language that is no language. */ | |
1046 | ||
7ed0f002 | 1047 | static int |
c8023e66 JG |
1048 | unk_lang_parser () |
1049 | { | |
1050 | return 1; | |
1051 | } | |
1052 | ||
7ed0f002 JG |
1053 | static void |
1054 | unk_lang_error (msg) | |
1055 | char *msg; | |
c8023e66 JG |
1056 | { |
1057 | error ("Attempted to parse an expression with unknown language"); | |
1058 | } | |
1059 | ||
1060 | static struct type ** const (unknown_builtin_types[]) = { 0 }; | |
1061 | static const struct op_print unk_op_print_tab[] = { 0 }; | |
1062 | ||
1063 | const struct language_defn unknown_language_defn = { | |
1064 | "unknown", | |
1065 | language_unknown, | |
1066 | &unknown_builtin_types[0], | |
1067 | range_check_off, | |
1068 | type_check_off, | |
1069 | unk_lang_parser, | |
1070 | unk_lang_error, | |
1071 | &builtin_type_error, /* longest signed integral type */ | |
1072 | &builtin_type_error, /* longest unsigned integral type */ | |
1073 | &builtin_type_error, /* longest floating point type */ | |
1074 | "0x%x", "0x%", "x", /* Hex format, prefix, suffix */ | |
1075 | "0%o", "0%", "o", /* Octal format, prefix, suffix */ | |
1076 | unk_op_print_tab, /* expression operators for printing */ | |
1077 | LANG_MAGIC | |
1078 | }; | |
1079 | ||
1080 | /* These two structs define fake entries for the "local" and "auto" options. */ | |
1081 | const struct language_defn auto_language_defn = { | |
1082 | "auto", | |
1083 | language_auto, | |
1084 | &unknown_builtin_types[0], | |
1085 | range_check_off, | |
1086 | type_check_off, | |
1087 | unk_lang_parser, | |
1088 | unk_lang_error, | |
1089 | &builtin_type_error, /* longest signed integral type */ | |
1090 | &builtin_type_error, /* longest unsigned integral type */ | |
1091 | &builtin_type_error, /* longest floating point type */ | |
1092 | "0x%x", "0x%", "x", /* Hex format, prefix, suffix */ | |
1093 | "0%o", "0%", "o", /* Octal format, prefix, suffix */ | |
1094 | unk_op_print_tab, /* expression operators for printing */ | |
1095 | LANG_MAGIC | |
1096 | }; | |
1097 | ||
1098 | const struct language_defn local_language_defn = { | |
1099 | "local", | |
1100 | language_auto, | |
1101 | &unknown_builtin_types[0], | |
1102 | range_check_off, | |
1103 | type_check_off, | |
1104 | unk_lang_parser, | |
1105 | unk_lang_error, | |
1106 | &builtin_type_error, /* longest signed integral type */ | |
1107 | &builtin_type_error, /* longest unsigned integral type */ | |
1108 | &builtin_type_error, /* longest floating point type */ | |
1109 | "0x%x", "0x%", "x", /* Hex format, prefix, suffix */ | |
1110 | "0%o", "0%", "o", /* Octal format, prefix, suffix */ | |
1111 | unk_op_print_tab, /* expression operators for printing */ | |
1112 | LANG_MAGIC | |
1113 | }; | |
1114 | \f | |
1115 | /* Initialize the language routines */ | |
1116 | ||
1117 | void | |
1118 | _initialize_language() | |
1119 | { | |
1120 | struct cmd_list_element *set, *show; | |
1121 | ||
1122 | /* GDB commands for language specific stuff */ | |
1123 | ||
1124 | set = add_set_cmd ("language", class_support, var_string_noescape, | |
1125 | (char *)&language, | |
0b798409 | 1126 | "Set the current source language.", |
c8023e66 JG |
1127 | &setlist); |
1128 | show = add_show_from_set (set, &showlist); | |
1ab3bf1b JG |
1129 | set->function.cfunc = set_language_command; |
1130 | show->function.cfunc = show_language_command; | |
c8023e66 JG |
1131 | |
1132 | add_prefix_cmd ("check", no_class, set_check, | |
1133 | "Set the status of the type/range checker", | |
1134 | &setchecklist, "set check ", 0, &setlist); | |
1135 | add_alias_cmd ("c", "check", no_class, 1, &setlist); | |
1136 | add_alias_cmd ("ch", "check", no_class, 1, &setlist); | |
1137 | ||
1138 | add_prefix_cmd ("check", no_class, show_check, | |
1139 | "Show the status of the type/range checker", | |
7cb83757 | 1140 | &showchecklist, "show check ", 0, &showlist); |
c8023e66 JG |
1141 | add_alias_cmd ("c", "check", no_class, 1, &showlist); |
1142 | add_alias_cmd ("ch", "check", no_class, 1, &showlist); | |
1143 | ||
1144 | set = add_set_cmd ("type", class_support, var_string_noescape, | |
1145 | (char *)&type, | |
7cb83757 | 1146 | "Set type checking. (on/warn/off/auto)", |
c8023e66 JG |
1147 | &setchecklist); |
1148 | show = add_show_from_set (set, &showchecklist); | |
1ab3bf1b JG |
1149 | set->function.cfunc = set_type_command; |
1150 | show->function.cfunc = show_type_command; | |
c8023e66 JG |
1151 | |
1152 | set = add_set_cmd ("range", class_support, var_string_noescape, | |
1153 | (char *)&range, | |
7cb83757 | 1154 | "Set range checking. (on/warn/off/auto)", |
c8023e66 JG |
1155 | &setchecklist); |
1156 | show = add_show_from_set (set, &showchecklist); | |
1ab3bf1b JG |
1157 | set->function.cfunc = set_range_command; |
1158 | show->function.cfunc = show_range_command; | |
c8023e66 JG |
1159 | |
1160 | add_language (&unknown_language_defn); | |
1161 | add_language (&local_language_defn); | |
1162 | add_language (&auto_language_defn); | |
1163 | ||
1164 | language = savestring ("auto",strlen("auto")); | |
1165 | range = savestring ("auto",strlen("auto")); | |
1166 | type = savestring ("auto",strlen("auto")); | |
1167 | ||
1168 | /* Have the above take effect */ | |
1169 | ||
5f3d478e | 1170 | set_language_command (language, 0); |
c8023e66 JG |
1171 | set_type_command (NULL, 0); |
1172 | set_range_command (NULL, 0); | |
1173 | } |