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