language.c revision 46283
1194676Sthompsa/* Multiple source language support for GDB.
2194676Sthompsa   Copyright 1991, 1992 Free Software Foundation, Inc.
3194676Sthompsa   Contributed by the Department of Computer Science at the State University
4195957Salfred   of New York at Buffalo.
5194676Sthompsa
6194676SthompsaThis file is part of GDB.
7194676Sthompsa
8194676SthompsaThis program is free software; you can redistribute it and/or modify
9194676Sthompsait under the terms of the GNU General Public License as published by
10194676Sthompsathe Free Software Foundation; either version 2 of the License, or
11194676Sthompsa(at your option) any later version.
12194676Sthompsa
13194676SthompsaThis program is distributed in the hope that it will be useful,
14194676Sthompsabut WITHOUT ANY WARRANTY; without even the implied warranty of
15194676SthompsaMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16194676SthompsaGNU General Public License for more details.
17194676Sthompsa
18194676SthompsaYou should have received a copy of the GNU General Public License
19194676Sthompsaalong with this program; if not, write to the Free Software
20194676SthompsaFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21194676Sthompsa
22194676Sthompsa/* This file contains functions that return things that are specific
23194676Sthompsa   to languages.  Each function should examine current_language if necessary,
24194676Sthompsa   and return the appropriate result. */
25194676Sthompsa
26194676Sthompsa/* FIXME:  Most of these would be better organized as macros which
27194676Sthompsa   return data out of a "language-specific" struct pointer that is set
28203815Swkoszek   whenever the working language changes.  That would be a lot faster.  */
29203815Swkoszek
30203815Swkoszek#include "defs.h"
31203815Swkoszek#include <ctype.h>
32203774Swkoszek#include "gdb_string.h"
33203815Swkoszek
34203815Swkoszek#include "symtab.h"
35203815Swkoszek#include "gdbtypes.h"
36203815Swkoszek#include "value.h"
37194676Sthompsa#include "gdbcmd.h"
38194676Sthompsa#include "frame.h"
39194676Sthompsa#include "expression.h"
40208020Sthompsa#include "language.h"
41208020Sthompsa#include "target.h"
42194676Sthompsa#include "parser-defs.h"
43194676Sthompsa
44194676Sthompsastatic void
45194676Sthompsashow_language_command PARAMS ((char *, int));
46194676Sthompsa
47194676Sthompsastatic void
48194676Sthompsaset_language_command PARAMS ((char *, int));
49194676Sthompsa
50194676Sthompsastatic void
51195957Salfredshow_type_command PARAMS ((char *, int));
52195957Salfred
53195957Salfredstatic void
54195957Salfredset_type_command PARAMS ((char *, int));
55195957Salfred
56195957Salfredstatic void
57195957Salfredshow_range_command PARAMS ((char *, int));
58195957Salfred
59195957Salfredstatic void
60195957Salfredset_range_command PARAMS ((char *, int));
61195957Salfred
62194676Sthompsastatic void
63194676Sthompsaset_range_str PARAMS ((void));
64194676Sthompsa
65195957Salfredstatic void
66194676Sthompsaset_type_str PARAMS ((void));
67195957Salfred
68194676Sthompsastatic void
69194676Sthompsaset_lang_str PARAMS ((void));
70194676Sthompsa
71194676Sthompsastatic void
72213853Shselaskyunk_lang_error PARAMS ((char *));
73213853Shselasky
74213853Shselaskystatic int
75213853Shselaskyunk_lang_parser PARAMS ((void));
76213853Shselasky
77213853Shselaskystatic void
78213853Shselaskyshow_check PARAMS ((char *, int));
79213853Shselasky
80213853Shselaskystatic void
81213853Shselaskyset_check PARAMS ((char *, int));
82213853Shselasky
83213853Shselaskystatic void
84213853Shselaskyset_type_range PARAMS ((void));
85213853Shselasky
86213853Shselaskystatic void
87213853Shselaskyunk_lang_emit_char PARAMS ((int c, GDB_FILE *stream, int quoter));
88213853Shselasky
89213853Shselaskystatic void
90213853Shselaskyunk_lang_printchar PARAMS ((int c, GDB_FILE *stream));
91194676Sthompsa
92195957Salfredstatic void
93194676Sthompsaunk_lang_printstr PARAMS ((GDB_FILE *stream, char *string, unsigned int length, int width, int force_ellipses));
94194676Sthompsa
95195957Salfredstatic struct type *
96194676Sthompsaunk_lang_create_fundamental_type PARAMS ((struct objfile *, int));
97194676Sthompsa
98194676Sthompsastatic void
99194676Sthompsaunk_lang_print_type PARAMS ((struct type *, char *, GDB_FILE *, int, int));
100194676Sthompsa
101194676Sthompsastatic int
102194676Sthompsaunk_lang_val_print PARAMS ((struct type *, char *, int, CORE_ADDR, GDB_FILE *,
103194676Sthompsa			    int, int, int, enum val_prettyprint));
104194676Sthompsa
105194676Sthompsastatic int
106194676Sthompsaunk_lang_value_print PARAMS ((value_ptr, GDB_FILE *, int, enum val_prettyprint));
107194676Sthompsa
108194676Sthompsa/* Forward declaration */
109194676Sthompsaextern const struct language_defn unknown_language_defn;
110195957Salfredextern char *warning_pre_print;
111195957Salfred
112194676Sthompsa/* The current (default at startup) state of type and range checking.
113195957Salfred    (If the modes are set to "auto", though, these are changed based
114195957Salfred    on the default language at startup, and then again based on the
115194676Sthompsa    language of the first source file.  */
116195957Salfred
117194676Sthompsaenum range_mode range_mode = range_mode_auto;
118194676Sthompsaenum range_check range_check = range_check_off;
119194676Sthompsaenum type_mode type_mode = type_mode_auto;
120195957Salfredenum type_check type_check = type_check_off;
121195957Salfred
122194676Sthompsa/* The current language and language_mode (see language.h) */
123194676Sthompsa
124194676Sthompsaconst struct language_defn *current_language = &unknown_language_defn;
125195957Salfredenum language_mode language_mode = language_mode_auto;
126213853Shselasky
127213853Shselasky/* The language that the user expects to be typing in (the language
128194676Sthompsa   of main(), or the last language we notified them about, or C).  */
129195957Salfred
130194676Sthompsaconst struct language_defn *expected_language;
131194676Sthompsa
132194676Sthompsa/* The list of supported languages.  The list itself is malloc'd.  */
133194676Sthompsa
134194676Sthompsastatic const struct language_defn **languages;
135194676Sthompsastatic unsigned languages_size;
136194676Sthompsastatic unsigned languages_allocsize;
137194676Sthompsa#define	DEFAULT_ALLOCSIZE 4
138194676Sthompsa
139194676Sthompsa/* The "set language/type/range" commands all put stuff in these
140195957Salfred   buffers.  This is to make them work as set/show commands.  The
141195957Salfred   user's string is copied here, then the set_* commands look at
142194676Sthompsa   them and update them to something that looks nice when it is
143194676Sthompsa   printed out. */
144194676Sthompsa
145194676Sthompsastatic char *language;
146195957Salfredstatic char *type;
147194676Sthompsastatic char *range;
148195957Salfred
149194676Sthompsa/* Warning issued when current_language and the language of the current
150195957Salfred   frame do not match. */
151195957Salfredchar lang_frame_mismatch_warn[] =
152195957Salfred	"Warning: the current language does not match this frame.";
153195957Salfred
154195957Salfred
155195957Salfred/* This page contains the functions corresponding to GDB commands
156194676Sthompsa   and their helpers. */
157194676Sthompsa
158195957Salfred/* Show command.  Display a warning if the language set
159195957Salfred   does not match the frame. */
160194676Sthompsastatic void
161194676Sthompsashow_language_command (ignore, from_tty)
162194676Sthompsa   char *ignore;
163194676Sthompsa   int from_tty;
164194676Sthompsa{
165194676Sthompsa   enum language flang;		/* The language of the current frame */
166194676Sthompsa
167194676Sthompsa   flang = get_frame_language();
168194676Sthompsa   if (flang != language_unknown &&
169194676Sthompsa       language_mode == language_mode_manual &&
170194676Sthompsa       current_language->la_language != flang)
171194676Sthompsa     printf_filtered("%s\n",lang_frame_mismatch_warn);
172194676Sthompsa}
173195957Salfred
174194676Sthompsa/* Set command.  Change the current working language. */
175195957Salfredstatic void
176194676Sthompsaset_language_command (ignore, from_tty)
177194676Sthompsa   char *ignore;
178194676Sthompsa   int from_tty;
179194676Sthompsa{
180195957Salfred  int i;
181194676Sthompsa  enum language flang;
182195957Salfred  char *err_lang;
183195957Salfred
184195957Salfred  if (!language || !language[0])
185195957Salfred    {
186195957Salfred      printf_unfiltered("The currently understood settings are:\n\n");
187195957Salfred      printf_unfiltered ("local or auto    Automatic setting based on source file\n");
188194676Sthompsa
189194676Sthompsa      for (i = 0; i < languages_size; ++i)
190195957Salfred	{
191194676Sthompsa	  /* Already dealt with these above.  */
192195957Salfred	  if (languages[i]->la_language == language_unknown
193194676Sthompsa	      || languages[i]->la_language == language_auto)
194194676Sthompsa	    continue;
195194676Sthompsa
196194676Sthompsa	  /* FIXME for now assume that the human-readable name is just
197194676Sthompsa	     a capitalization of the internal name.  */
198195957Salfred	  printf_unfiltered ("%-16s Use the %c%s language\n",
199194676Sthompsa			     languages[i]->la_name,
200194676Sthompsa			     /* Capitalize first letter of language
201194676Sthompsa				name.  */
202194676Sthompsa			     toupper (languages[i]->la_name[0]),
203194676Sthompsa			     languages[i]->la_name + 1);
204195957Salfred	}
205194676Sthompsa      /* Restore the silly string. */
206194676Sthompsa      set_language(current_language->la_language);
207194676Sthompsa      return;
208194676Sthompsa    }
209194676Sthompsa
210195560Sthompsa  /* Search the list of languages for a match.  */
211195560Sthompsa  for (i = 0; i < languages_size; i++) {
212195560Sthompsa    if (STREQ (languages[i]->la_name, language)) {
213195560Sthompsa      /* Found it!  Go into manual mode, and use this language.  */
214194676Sthompsa      if (languages[i]->la_language == language_auto) {
215195957Salfred	/* Enter auto mode.  Set to the current frame's language, if known.  */
216194676Sthompsa	language_mode = language_mode_auto;
217194676Sthompsa  	flang = get_frame_language();
218194676Sthompsa	if (flang!=language_unknown)
219199055Sthompsa	  set_language(flang);
220199055Sthompsa	expected_language = current_language;
221199055Sthompsa	return;
222194676Sthompsa      } else {
223194676Sthompsa	/* Enter manual mode.  Set the specified language.  */
224195957Salfred	language_mode = language_mode_manual;
225195957Salfred	current_language = languages[i];
226195957Salfred	set_type_range ();
227195957Salfred	set_lang_str();
228194676Sthompsa	expected_language = current_language;
229194676Sthompsa	return;
230194676Sthompsa      }
231194676Sthompsa    }
232195957Salfred  }
233194676Sthompsa
234194676Sthompsa  /* Reset the language (esp. the global string "language") to the
235194676Sthompsa     correct values. */
236194676Sthompsa  err_lang=savestring(language,strlen(language));
237194676Sthompsa  make_cleanup (free, err_lang);	/* Free it after error */
238194676Sthompsa  set_language(current_language->la_language);
239194676Sthompsa  error ("Unknown language `%s'.",err_lang);
240194676Sthompsa}
241194676Sthompsa
242194676Sthompsa/* Show command.  Display a warning if the type setting does
243194676Sthompsa   not match the current language. */
244194676Sthompsastatic void
245194676Sthompsashow_type_command(ignore, from_tty)
246194676Sthompsa   char *ignore;
247194676Sthompsa   int from_tty;
248194676Sthompsa{
249195957Salfred   if (type_check != current_language->la_type_check)
250194676Sthompsa      printf_unfiltered(
251194676Sthompsa"Warning: the current type check setting does not match the language.\n");
252194676Sthompsa}
253194676Sthompsa
254194676Sthompsa/* Set command.  Change the setting for type checking. */
255194676Sthompsastatic void
256194676Sthompsaset_type_command(ignore, from_tty)
257194676Sthompsa   char *ignore;
258194676Sthompsa   int from_tty;
259195957Salfred{
260194676Sthompsa   if (STREQ(type,"on"))
261194676Sthompsa   {
262195957Salfred      type_check = type_check_on;
263195957Salfred      type_mode = type_mode_manual;
264194676Sthompsa   }
265194676Sthompsa   else if (STREQ(type,"warn"))
266194676Sthompsa   {
267195957Salfred      type_check = type_check_warn;
268194676Sthompsa      type_mode = type_mode_manual;
269194676Sthompsa   }
270195957Salfred   else if (STREQ(type,"off"))
271195957Salfred   {
272194676Sthompsa      type_check = type_check_off;
273194676Sthompsa      type_mode = type_mode_manual;
274224903Shselasky   }
275224903Shselasky   else if (STREQ(type,"auto"))
276224903Shselasky   {
277224903Shselasky      type_mode = type_mode_auto;
278225035Shselasky      set_type_range();
279224903Shselasky      /* Avoid hitting the set_type_str call below.  We
280224903Shselasky         did it in set_type_range. */
281224903Shselasky      return;
282224903Shselasky   }
283224903Shselasky   set_type_str();
284224903Shselasky   show_type_command((char *)NULL, from_tty);
285224903Shselasky}
286224903Shselasky
287224903Shselasky/* Show command.  Display a warning if the range setting does
288224903Shselasky   not match the current language. */
289224903Shselaskystatic void
290224903Shselaskyshow_range_command(ignore, from_tty)
291224903Shselasky   char *ignore;
292224903Shselasky   int from_tty;
293224903Shselasky{
294224903Shselasky
295194676Sthompsa   if (range_check != current_language->la_range_check)
296195957Salfred      printf_unfiltered(
297194676Sthompsa"Warning: the current range check setting does not match the language.\n");
298194676Sthompsa}
299194676Sthompsa
300194676Sthompsa/* Set command.  Change the setting for range checking. */
301194676Sthompsastatic void
302195957Salfredset_range_command(ignore, from_tty)
303195957Salfred   char *ignore;
304195957Salfred   int from_tty;
305195957Salfred{
306194676Sthompsa   if (STREQ(range,"on"))
307194676Sthompsa   {
308194676Sthompsa      range_check = range_check_on;
309194676Sthompsa      range_mode = range_mode_manual;
310195957Salfred   }
311195957Salfred   else if (STREQ(range,"warn"))
312195957Salfred   {
313195957Salfred      range_check = range_check_warn;
314194676Sthompsa      range_mode = range_mode_manual;
315195957Salfred   }
316194676Sthompsa   else if (STREQ(range,"off"))
317195957Salfred   {
318194676Sthompsa      range_check = range_check_off;
319195957Salfred      range_mode = range_mode_manual;
320194676Sthompsa   }
321194676Sthompsa   else if (STREQ(range,"auto"))
322194676Sthompsa   {
323194676Sthompsa      range_mode = range_mode_auto;
324194676Sthompsa      set_type_range();
325194676Sthompsa      /* Avoid hitting the set_range_str call below.  We
326194676Sthompsa	 did it in set_type_range. */
327194676Sthompsa      return;
328194676Sthompsa   }
329194676Sthompsa   set_range_str();
330194676Sthompsa   show_range_command((char *)0, from_tty);
331194676Sthompsa}
332194676Sthompsa
333194676Sthompsa/* Set the status of range and type checking based on
334194676Sthompsa   the current modes and the current language.
335195957Salfred   If SHOW is non-zero, then print out the current language,
336194676Sthompsa   type and range checking status. */
337194676Sthompsastatic void
338195957Salfredset_type_range()
339194676Sthompsa{
340195957Salfred
341194676Sthompsa  if (range_mode == range_mode_auto)
342195957Salfred    range_check = current_language->la_range_check;
343194676Sthompsa
344194676Sthompsa  if (type_mode == type_mode_auto)
345194676Sthompsa    type_check = current_language->la_type_check;
346194676Sthompsa
347194676Sthompsa  set_type_str();
348195957Salfred  set_range_str();
349194676Sthompsa}
350194676Sthompsa
351195957Salfred/* Set current language to (enum language) LANG.  Returns previous language. */
352194676Sthompsa
353195957Salfredenum language
354194676Sthompsaset_language(lang)
355195957Salfred   enum language lang;
356194676Sthompsa{
357194676Sthompsa  int i;
358194676Sthompsa  enum language prev_language;
359194676Sthompsa
360194676Sthompsa  prev_language = current_language->la_language;
361194676Sthompsa
362194676Sthompsa  for (i = 0; i < languages_size; i++) {
363194676Sthompsa    if (languages[i]->la_language == lang) {
364195957Salfred      current_language = languages[i];
365194676Sthompsa      set_type_range ();
366194676Sthompsa      set_lang_str();
367194676Sthompsa      break;
368195957Salfred    }
369194676Sthompsa  }
370194676Sthompsa
371194676Sthompsa  return prev_language;
372194676Sthompsa}
373194676Sthompsa
374195957Salfred/* This page contains functions that update the global vars
375195957Salfred   language, type and range. */
376194676Sthompsastatic void
377195957Salfredset_lang_str()
378195957Salfred{
379195957Salfred   char *prefix = "";
380195957Salfred
381194676Sthompsa   free (language);
382194676Sthompsa   if (language_mode == language_mode_auto)
383195957Salfred      prefix = "auto; currently ";
384194676Sthompsa
385194676Sthompsa   language = concat(prefix, current_language->la_name, NULL);
386195957Salfred}
387194676Sthompsa
388194676Sthompsastatic void
389195957Salfredset_type_str()
390195957Salfred{
391194676Sthompsa   char *tmp, *prefix = "";
392213853Shselasky
393195957Salfred   free (type);
394195957Salfred   if (type_mode==type_mode_auto)
395194676Sthompsa      prefix = "auto; currently ";
396195957Salfred
397194676Sthompsa   switch(type_check)
398194676Sthompsa   {
399194676Sthompsa   case type_check_on:
400194676Sthompsa      tmp = "on";
401194676Sthompsa      break;
402195957Salfred   case type_check_off:
403194676Sthompsa      tmp = "off";
404194676Sthompsa      break;
405194676Sthompsa   case type_check_warn:
406194676Sthompsa      tmp = "warn";
407194676Sthompsa      break;
408195957Salfred      default:
409195957Salfred      error ("Unrecognized type check setting.");
410194676Sthompsa   }
411195957Salfred
412195957Salfred   type = concat(prefix,tmp,NULL);
413195957Salfred}
414195957Salfred
415195560Sthompsastatic void
416194676Sthompsaset_range_str()
417194676Sthompsa{
418194676Sthompsa   char *tmp, *pref = "";
419194676Sthompsa
420228236Shselasky   free (range);
421194676Sthompsa   if (range_mode==range_mode_auto)
422228236Shselasky      pref = "auto; currently ";
423228236Shselasky
424228236Shselasky   switch(range_check)
425228236Shselasky   {
426195957Salfred   case range_check_on:
427195957Salfred      tmp = "on";
428195957Salfred      break;
429195957Salfred   case range_check_off:
430195957Salfred      tmp = "off";
431194676Sthompsa      break;
432195560Sthompsa   case range_check_warn:
433228235Shselasky      tmp = "warn";
434195957Salfred      break;
435195560Sthompsa      default:
436194676Sthompsa      error ("Unrecognized range check setting.");
437194676Sthompsa   }
438194676Sthompsa
439195560Sthompsa   range = concat(pref,tmp,NULL);
440195957Salfred}
441194676Sthompsa
442194676Sthompsa
443194676Sthompsa/* Print out the current language settings: language, range and
444195957Salfred   type checking.  If QUIETLY, print only what has changed.  */
445194676Sthompsa
446194676Sthompsavoid
447195957Salfredlanguage_info (quietly)
448195957Salfred     int quietly;
449194676Sthompsa{
450194676Sthompsa  if (quietly && expected_language == current_language)
451195957Salfred    return;
452195957Salfred
453194676Sthompsa  expected_language = current_language;
454195957Salfred  printf_unfiltered("Current language:  %s\n",language);
455195957Salfred  show_language_command((char *)0, 1);
456194676Sthompsa
457195957Salfred  if (!quietly)
458194676Sthompsa    {
459195957Salfred       printf_unfiltered("Type checking:     %s\n",type);
460199055Sthompsa       show_type_command((char *)0, 1);
461199055Sthompsa       printf_unfiltered("Range checking:    %s\n",range);
462195957Salfred       show_range_command((char *)0, 1);
463194676Sthompsa    }
464195957Salfred}
465195957Salfred
466194676Sthompsa/* Return the result of a binary operation. */
467213853Shselasky
468195957Salfred#if 0	/* Currently unused */
469195957Salfred
470194676Sthompsastruct type *
471194676Sthompsabinop_result_type (v1, v2)
472194676Sthompsa   value_ptr v1, v2;
473194676Sthompsa{
474195957Salfred   int size,uns;
475194676Sthompsa   struct type *t1 = check_typedef (VALUE_TYPE (v1));
476195957Salfred   struct type *t2 = check_typedef (VALUE_TYPE (v2));
477194676Sthompsa
478195957Salfred   int l1 = TYPE_LENGTH (t1);
479194676Sthompsa   int l2 = TYPE_LENGTH (t2);
480194676Sthompsa
481194676Sthompsa   switch(current_language->la_language)
482195957Salfred   {
483194676Sthompsa   case language_c:
484195957Salfred   case language_cplus:
485194676Sthompsa      if (TYPE_CODE (t1)==TYPE_CODE_FLT)
486195957Salfred	 return TYPE_CODE(t2) == TYPE_CODE_FLT && l2 > l1 ?
487194676Sthompsa	    VALUE_TYPE(v2) : VALUE_TYPE(v1);
488194676Sthompsa      else if (TYPE_CODE(t2)==TYPE_CODE_FLT)
489195957Salfred	 return TYPE_CODE(t1)) == TYPE_CODE_FLT && l1 > l2 ?
490195957Salfred	    VALUE_TYPE(v1) : VALUE_TYPE(v2);
491195957Salfred      else if (TYPE_UNSIGNED(t1) && l1 > l2)
492194676Sthompsa	 return VALUE_TYPE(v1);
493195957Salfred      else if (TYPE_UNSIGNED(t2) && l2 > l1)
494195957Salfred	 return VALUE_TYPE(v2);
495195957Salfred      else  /* Both are signed.  Result is the longer type */
496195957Salfred	 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
497194676Sthompsa      break;
498194676Sthompsa   case language_m2:
499194676Sthompsa      /* If we are doing type-checking, l1 should equal l2, so this is
500194676Sthompsa	 not needed. */
501195957Salfred      return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
502194676Sthompsa      break;
503195957Salfred   case language_chill:
504195957Salfred      error ("Missing Chill support in function binop_result_check.");/*FIXME*/
505195957Salfred   }
506195957Salfred   abort();
507194676Sthompsa   return (struct type *)0;	/* For lint */
508195957Salfred}
509195957Salfred
510194676Sthompsa#endif	/* 0 */
511194676Sthompsa
512195957Salfred
513195957Salfred/* This page contains functions that return format strings for
514195957Salfred   printf for printing out numbers in different formats */
515195957Salfred
516195957Salfred/* Returns the appropriate printf format for hexadecimal
517195957Salfred   numbers. */
518194676Sthompsachar *
519195957Salfredlocal_hex_format_custom(pre)
520195957Salfred   char *pre;
521195957Salfred{
522195957Salfred   static char form[50];
523195957Salfred
524195957Salfred   strcpy (form, local_hex_format_prefix ());
525195957Salfred   strcat (form, "%");
526195957Salfred   strcat (form, pre);
527195957Salfred   strcat (form, local_hex_format_specifier ());
528195957Salfred   strcat (form, local_hex_format_suffix ());
529195957Salfred   return form;
530195957Salfred}
531195957Salfred
532195957Salfred/* Converts a number to hexadecimal and stores it in a static
533195957Salfred   string.  Returns a pointer to this string. */
534195957Salfredchar *
535195957Salfredlocal_hex_string (num)
536195957Salfred   unsigned long num;
537195957Salfred{
538195957Salfred   static char res[50];
539195957Salfred
540195957Salfred   sprintf (res, local_hex_format(), num);
541195957Salfred   return res;
542195957Salfred}
543195957Salfred
544194676Sthompsa/* Converts a number to custom hexadecimal and stores it in a static
545194676Sthompsa   string.  Returns a pointer to this string. */
546194676Sthompsachar *
547195957Salfredlocal_hex_string_custom(num,pre)
548194676Sthompsa   unsigned long num;
549195957Salfred   char *pre;
550195957Salfred{
551194676Sthompsa   static char res[50];
552195957Salfred
553194676Sthompsa   sprintf (res, local_hex_format_custom(pre), num);
554194676Sthompsa   return res;
555194676Sthompsa}
556195957Salfred
557194676Sthompsa/* Returns the appropriate printf format for octal
558194676Sthompsa   numbers. */
559195957Salfredchar *
560194676Sthompsalocal_octal_format_custom(pre)
561195957Salfred   char *pre;
562194676Sthompsa{
563195957Salfred   static char form[50];
564194676Sthompsa
565195957Salfred   strcpy (form, local_octal_format_prefix ());
566195957Salfred   strcat (form, "%");
567194676Sthompsa   strcat (form, pre);
568194676Sthompsa   strcat (form, local_octal_format_specifier ());
569194676Sthompsa   strcat (form, local_octal_format_suffix ());
570195957Salfred   return form;
571194676Sthompsa}
572195957Salfred
573195957Salfred/* Returns the appropriate printf format for decimal numbers. */
574194676Sthompsachar *
575195957Salfredlocal_decimal_format_custom(pre)
576194676Sthompsa   char *pre;
577194676Sthompsa{
578194676Sthompsa   static char form[50];
579195957Salfred
580194676Sthompsa   strcpy (form, local_decimal_format_prefix ());
581194676Sthompsa   strcat (form, "%");
582195957Salfred   strcat (form, pre);
583194676Sthompsa   strcat (form, local_decimal_format_specifier ());
584195957Salfred   strcat (form, local_decimal_format_suffix ());
585194676Sthompsa   return form;
586195957Salfred}
587194676Sthompsa
588195957Salfred#if 0
589195957Salfred/* This page contains functions that are used in type/range checking.
590194676Sthompsa   They all return zero if the type/range check fails.
591194676Sthompsa
592194676Sthompsa   It is hoped that these will make extending GDB to parse different
593195957Salfred   languages a little easier.  These are primarily used in eval.c when
594194676Sthompsa   evaluating expressions and making sure that their types are correct.
595194676Sthompsa   Instead of having a mess of conjucted/disjuncted expressions in an "if",
596195957Salfred   the ideas of type can be wrapped up in the following functions.
597195957Salfred
598194676Sthompsa   Note that some of them are not currently dependent upon which language
599195957Salfred   is currently being parsed.  For example, floats are the same in
600194676Sthompsa   C and Modula-2 (ie. the only floating point type has TYPE_CODE of
601194676Sthompsa   TYPE_CODE_FLT), while booleans are different. */
602194676Sthompsa
603195957Salfred/* Returns non-zero if its argument is a simple type.  This is the same for
604194676Sthompsa   both Modula-2 and for C.  In the C case, TYPE_CODE_CHAR will never occur,
605194676Sthompsa   and thus will never cause the failure of the test. */
606195957Salfredint
607195957Salfredsimple_type(type)
608195957Salfred    struct type *type;
609195957Salfred{
610194676Sthompsa  CHECK_TYPEDEF (type);
611195957Salfred  switch (TYPE_CODE (type)) {
612195957Salfred  case TYPE_CODE_INT:
613195957Salfred  case TYPE_CODE_CHAR:
614195957Salfred  case TYPE_CODE_ENUM:
615195957Salfred  case TYPE_CODE_FLT:
616195957Salfred  case TYPE_CODE_RANGE:
617195957Salfred  case TYPE_CODE_BOOL:
618195957Salfred    return 1;
619195957Salfred
620195957Salfred  default:
621195957Salfred    return 0;
622195957Salfred  }
623195957Salfred}
624195957Salfred
625195957Salfred/* Returns non-zero if its argument is of an ordered type.
626194676Sthompsa   An ordered type is one in which the elements can be tested for the
627194676Sthompsa   properties of "greater than", "less than", etc, or for which the
628195957Salfred   operations "increment" or "decrement" make sense. */
629195957Salfredint
630195957Salfredordered_type (type)
631195957Salfred   struct type *type;
632195957Salfred{
633195957Salfred  CHECK_TYPEDEF (type);
634195957Salfred  switch (TYPE_CODE (type)) {
635195957Salfred  case TYPE_CODE_INT:
636195957Salfred  case TYPE_CODE_CHAR:
637195957Salfred  case TYPE_CODE_ENUM:
638195957Salfred  case TYPE_CODE_FLT:
639195957Salfred  case TYPE_CODE_RANGE:
640195957Salfred    return 1;
641195957Salfred
642195957Salfred  default:
643194676Sthompsa    return 0;
644195957Salfred  }
645194676Sthompsa}
646194676Sthompsa
647195957Salfred/* Returns non-zero if the two types are the same */
648195957Salfredint
649194676Sthompsasame_type (arg1, arg2)
650195957Salfred   struct type *arg1, *arg2;
651195560Sthompsa{
652195957Salfred  CHECK_TYPEDEF (type);
653194676Sthompsa   if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
654195957Salfred      /* One is structured and one isn't */
655213853Shselasky      return 0;
656213853Shselasky   else if (structured_type(arg1) && structured_type(arg2))
657195957Salfred      return arg1 == arg2;
658195957Salfred   else if (numeric_type(arg1) && numeric_type(arg2))
659223642Shselasky      return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
660195957Salfred	 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
661195957Salfred	    ? 1 : 0;
662195957Salfred   else
663194676Sthompsa      return arg1==arg2;
664194676Sthompsa}
665194676Sthompsa
666195957Salfred/* Returns non-zero if the type is integral */
667195957Salfredint
668195957Salfredintegral_type (type)
669195957Salfred   struct type *type;
670194676Sthompsa{
671195957Salfred  CHECK_TYPEDEF (type);
672195957Salfred   switch(current_language->la_language)
673195957Salfred   {
674194676Sthompsa   case language_c:
675194676Sthompsa   case language_cplus:
676194676Sthompsa      return (TYPE_CODE(type) != TYPE_CODE_INT) &&
677195957Salfred	 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
678194676Sthompsa   case language_m2:
679195957Salfred      return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
680195957Salfred   case language_chill:
681194676Sthompsa      error ("Missing Chill support in function integral_type.");  /*FIXME*/
682195957Salfred   default:
683194676Sthompsa      error ("Language not supported.");
684213853Shselasky   }
685194676Sthompsa}
686195957Salfred
687195957Salfred/* Returns non-zero if the value is numeric */
688195957Salfredint
689195957Salfrednumeric_type (type)
690195957Salfred   struct type *type;
691195957Salfred{
692195957Salfred  CHECK_TYPEDEF (type);
693195957Salfred  switch (TYPE_CODE (type)) {
694195957Salfred  case TYPE_CODE_INT:
695195957Salfred  case TYPE_CODE_FLT:
696195957Salfred    return 1;
697194676Sthompsa
698194676Sthompsa  default:
699194676Sthompsa    return 0;
700213848Shselasky  }
701213848Shselasky}
702213848Shselasky
703213848Shselasky/* Returns non-zero if the value is a character type */
704213848Shselaskyint
705213848Shselaskycharacter_type (type)
706213848Shselasky   struct type *type;
707213848Shselasky{
708213848Shselasky  CHECK_TYPEDEF (type);
709213848Shselasky  switch(current_language->la_language)
710213848Shselasky   {
711213848Shselasky   case language_chill:
712213848Shselasky   case language_m2:
713213848Shselasky      return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
714213848Shselasky
715195957Salfred   case language_c:
716194676Sthompsa   case language_cplus:
717195957Salfred      return (TYPE_CODE(type) == TYPE_CODE_INT) &&
718194676Sthompsa	 TYPE_LENGTH(type) == sizeof(char)
719194676Sthompsa	 ? 1 : 0;
720226220Shselasky   default:
721226220Shselasky      return (0);
722226220Shselasky   }
723226220Shselasky}
724194676Sthompsa
725194676Sthompsa/* Returns non-zero if the value is a string type */
726194676Sthompsaint
727213853Shselaskystring_type (type)
728213853Shselasky   struct type *type;
729213853Shselasky{
730213853Shselasky  CHECK_TYPEDEF (type);
731213853Shselasky  switch(current_language->la_language)
732213853Shselasky   {
733213853Shselasky   case language_chill:
734213853Shselasky   case language_m2:
735213853Shselasky      return TYPE_CODE(type) != TYPE_CODE_STRING ? 0 : 1;
736213853Shselasky
737213853Shselasky   case language_c:
738213853Shselasky   case language_cplus:
739213853Shselasky      /* C does not have distinct string type. */
740213853Shselasky      return (0);
741213853Shselasky   default:
742213853Shselasky      return (0);
743213853Shselasky   }
744224085Shselasky}
745224085Shselasky
746213853Shselasky/* Returns non-zero if the value is a boolean type */
747213853Shselaskyint
748213853Shselaskyboolean_type (type)
749213853Shselasky   struct type *type;
750213853Shselasky{
751213853Shselasky  CHECK_TYPEDEF (type);
752213853Shselasky  if (TYPE_CODE (type) == TYPE_CODE_BOOL)
753213853Shselasky    return 1;
754213853Shselasky  switch(current_language->la_language)
755213853Shselasky    {
756213853Shselasky    case language_c:
757213853Shselasky    case language_cplus:
758213853Shselasky      /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL
759213853Shselasky	 for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C.  */
760213853Shselasky      if (TYPE_CODE (type) == TYPE_CODE_INT)
761213853Shselasky	return 1;
762213853Shselasky   default:
763213853Shselasky      break;
764213853Shselasky   }
765213853Shselasky  return 0;
766213853Shselasky}
767213853Shselasky
768195957Salfred/* Returns non-zero if the value is a floating-point type */
769194676Sthompsaint
770195957Salfredfloat_type (type)
771194676Sthompsa   struct type *type;
772195957Salfred{
773194676Sthompsa  CHECK_TYPEDEF (type);
774194676Sthompsa  return TYPE_CODE(type) == TYPE_CODE_FLT;
775195957Salfred}
776195957Salfred
777194676Sthompsa/* Returns non-zero if the value is a pointer type */
778213853Shselaskyint
779194676Sthompsapointer_type(type)
780194676Sthompsa   struct type *type;
781194676Sthompsa{
782195957Salfred   return TYPE_CODE(type) == TYPE_CODE_PTR ||
783194676Sthompsa      TYPE_CODE(type) == TYPE_CODE_REF;
784195957Salfred}
785194676Sthompsa
786195957Salfred/* Returns non-zero if the value is a structured type */
787194676Sthompsaint
788194676Sthompsastructured_type(type)
789194676Sthompsa   struct type *type;
790194676Sthompsa{
791194676Sthompsa  CHECK_TYPEDEF (type);
792194676Sthompsa   switch(current_language->la_language)
793194676Sthompsa   {
794194676Sthompsa   case language_c:
795195957Salfred   case language_cplus:
796195957Salfred      return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
797194676Sthompsa	 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
798194676Sthompsa	    (TYPE_CODE(type) == TYPE_CODE_ARRAY);
799194676Sthompsa   case language_m2:
800195957Salfred      return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
801194676Sthompsa	 (TYPE_CODE(type) == TYPE_CODE_SET) ||
802194676Sthompsa	    (TYPE_CODE(type) == TYPE_CODE_ARRAY);
803195957Salfred   case language_chill:
804195957Salfred      error ("Missing Chill support in function structured_type.");  /*FIXME*/
805194676Sthompsa   default:
806194676Sthompsa      return (0);
807195957Salfred   }
808194676Sthompsa}
809195957Salfred#endif
810195957Salfred
811194676Sthompsastruct type *
812195957Salfredlang_bool_type ()
813195957Salfred{
814195957Salfred  struct symbol *sym;
815195957Salfred  struct type *type;
816194676Sthompsa  switch(current_language->la_language)
817194676Sthompsa    {
818194676Sthompsa    case language_chill:
819195957Salfred      return builtin_type_chill_bool;
820194676Sthompsa    case language_fortran:
821195957Salfred      sym = lookup_symbol ("logical", NULL, VAR_NAMESPACE, NULL, NULL);
822194676Sthompsa      if (sym)
823195957Salfred	{
824195957Salfred	  type = SYMBOL_TYPE (sym);
825194676Sthompsa	  if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
826215253Shselasky	    return type;
827215253Shselasky	}
828215253Shselasky      return builtin_type_f_logical_s2;
829215253Shselasky    case language_cplus:
830195957Salfred      sym = lookup_symbol ("bool", NULL, VAR_NAMESPACE, NULL, NULL);
831195957Salfred      if (sym)
832194676Sthompsa	{
833195957Salfred	  type = SYMBOL_TYPE (sym);
834194676Sthompsa	  if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
835194676Sthompsa	    return type;
836219100Shselasky	}
837195957Salfred      return builtin_type_bool;
838195560Sthompsa    default:
839219100Shselasky      return builtin_type_int;
840195560Sthompsa    }
841195560Sthompsa}
842195560Sthompsa
843219100Shselasky/* This page contains functions that return info about
844195957Salfred   (struct value) values used in GDB. */
845195560Sthompsa
846195560Sthompsa/* Returns non-zero if the value VAL represents a true value. */
847195957Salfredint
848195560Sthompsavalue_true (val)
849195560Sthompsa     value_ptr val;
850195957Salfred{
851195560Sthompsa  /* It is possible that we should have some sort of error if a non-boolean
852195957Salfred     value is used in this context.  Possibly dependent on some kind of
853195560Sthompsa     "boolean-checking" option like range checking.  But it should probably
854195560Sthompsa     not depend on the language except insofar as is necessary to identify
855195560Sthompsa     a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
856195957Salfred     should be an error, probably).  */
857195560Sthompsa  return !value_logical_not (val);
858195560Sthompsa}
859195560Sthompsa
860195560Sthompsa/* Returns non-zero if the operator OP is defined on
861195560Sthompsa   the values ARG1 and ARG2. */
862195560Sthompsa
863195560Sthompsa#if 0	/* Currently unused */
864195560Sthompsa
865195957Salfredvoid
866195957Salfredbinop_type_check(arg1,arg2,op)
867195560Sthompsa   value_ptr arg1,arg2;
868195957Salfred   int op;
869195957Salfred{
870195957Salfred   struct type *t1, *t2;
871195560Sthompsa
872195957Salfred   /* If we're not checking types, always return success. */
873195957Salfred   if (!STRICT_TYPE)
874195957Salfred      return;
875195957Salfred
876195957Salfred   t1=VALUE_TYPE(arg1);
877195957Salfred   if (arg2 != NULL)
878195957Salfred      t2=VALUE_TYPE(arg2);
879195957Salfred   else
880195957Salfred      t2=NULL;
881195560Sthompsa
882195957Salfred   switch(op)
883195560Sthompsa   {
884195957Salfred   case BINOP_ADD:
885195560Sthompsa   case BINOP_SUB:
886195560Sthompsa      if ((numeric_type(t1) && pointer_type(t2)) ||
887195957Salfred	 (pointer_type(t1) && numeric_type(t2)))
888195957Salfred      {
889195957Salfred	 warning ("combining pointer and integer.\n");
890195957Salfred	 break;
891195957Salfred      }
892195957Salfred   case BINOP_MUL:
893195957Salfred   case BINOP_LSH:
894195957Salfred   case BINOP_RSH:
895195957Salfred      if (!numeric_type(t1) || !numeric_type(t2))
896195957Salfred	 type_op_error ("Arguments to %s must be numbers.",op);
897195957Salfred      else if (!same_type(t1,t2))
898195957Salfred	 type_op_error ("Arguments to %s must be of the same type.",op);
899195957Salfred      break;
900195957Salfred
901195957Salfred   case BINOP_LOGICAL_AND:
902195957Salfred   case BINOP_LOGICAL_OR:
903195957Salfred      if (!boolean_type(t1) || !boolean_type(t2))
904195957Salfred	 type_op_error ("Arguments to %s must be of boolean type.",op);
905195957Salfred      break;
906195957Salfred
907195957Salfred   case BINOP_EQUAL:
908195957Salfred      if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
909195957Salfred	 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
910195957Salfred	 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
911195957Salfred      else if ((pointer_type(t1) && integral_type(t2)) ||
912195957Salfred	 (integral_type(t1) && pointer_type(t2)))
913194676Sthompsa      {
914195957Salfred	 warning ("combining integer and pointer.\n");
915195957Salfred	 break;
916194676Sthompsa      }
917195957Salfred      else if (!simple_type(t1) || !simple_type(t2))
918195957Salfred	 type_op_error ("Arguments to %s must be of simple type.",op);
919195957Salfred      else if (!same_type(t1,t2))
920195957Salfred	 type_op_error ("Arguments to %s must be of the same type.",op);
921195957Salfred      break;
922195957Salfred
923195957Salfred   case BINOP_REM:
924195957Salfred   case BINOP_MOD:
925195957Salfred      if (!integral_type(t1) || !integral_type(t2))
926199575Sthompsa	 type_op_error ("Arguments to %s must be of integral type.",op);
927195957Salfred      break;
928195957Salfred
929199575Sthompsa   case BINOP_LESS:
930199575Sthompsa   case BINOP_GTR:
931199575Sthompsa   case BINOP_LEQ:
932195957Salfred   case BINOP_GEQ:
933195957Salfred      if (!ordered_type(t1) || !ordered_type(t2))
934195957Salfred	 type_op_error ("Arguments to %s must be of ordered type.",op);
935195957Salfred      else if (!same_type(t1,t2))
936195957Salfred	 type_op_error ("Arguments to %s must be of the same type.",op);
937195957Salfred      break;
938195957Salfred
939195957Salfred   case BINOP_ASSIGN:
940195957Salfred      if (pointer_type(t1) && !integral_type(t2))
941195957Salfred	 type_op_error ("A pointer can only be assigned an integer.",op);
942195957Salfred      else if (pointer_type(t1) && integral_type(t2))
943195957Salfred      {
944195957Salfred	 warning ("combining integer and pointer.");
945195957Salfred	 break;
946195957Salfred      }
947194676Sthompsa      else if (!simple_type(t1) || !simple_type(t2))
948195957Salfred	 type_op_error ("Arguments to %s must be of simple type.",op);
949194676Sthompsa      else if (!same_type(t1,t2))
950195957Salfred	 type_op_error ("Arguments to %s must be of the same type.",op);
951195957Salfred      break;
952195957Salfred
953195957Salfred    case BINOP_CONCAT:
954194676Sthompsa      /* FIXME:  Needs to handle bitstrings as well. */
955195957Salfred      if (!(string_type(t1) || character_type(t1) || integral_type(t1))
956195957Salfred	  || !(string_type(t2) || character_type(t2) || integral_type(t2)))
957195957Salfred	  type_op_error ("Arguments to %s must be strings or characters.", op);
958195957Salfred      break;
959195957Salfred
960195957Salfred   /* Unary checks -- arg2 is null */
961195957Salfred
962195957Salfred   case UNOP_LOGICAL_NOT:
963195957Salfred      if (!boolean_type(t1))
964195957Salfred	 type_op_error ("Argument to %s must be of boolean type.",op);
965195957Salfred      break;
966195957Salfred
967195957Salfred   case UNOP_PLUS:
968195957Salfred   case UNOP_NEG:
969195957Salfred      if (!numeric_type(t1))
970195957Salfred	 type_op_error ("Argument to %s must be of numeric type.",op);
971195957Salfred      break;
972194676Sthompsa
973194676Sthompsa   case UNOP_IND:
974195560Sthompsa      if (integral_type(t1))
975195957Salfred      {
976195957Salfred	 warning ("combining pointer and integer.\n");
977195957Salfred	 break;
978195957Salfred      }
979195957Salfred      else if (!pointer_type(t1))
980195957Salfred	 type_op_error ("Argument to %s must be a pointer.",op);
981195957Salfred      break;
982195957Salfred
983195560Sthompsa   case UNOP_PREINCREMENT:
984194676Sthompsa   case UNOP_POSTINCREMENT:
985195957Salfred   case UNOP_PREDECREMENT:
986195957Salfred   case UNOP_POSTDECREMENT:
987195957Salfred      if (!ordered_type(t1))
988195957Salfred	 type_op_error ("Argument to %s must be of an ordered type.",op);
989195957Salfred      break;
990195957Salfred
991195957Salfred   default:
992195957Salfred      /* Ok.  The following operators have different meanings in
993194676Sthompsa	 different languages. */
994195957Salfred      switch(current_language->la_language)
995195957Salfred      {
996195957Salfred#ifdef _LANG_c
997195957Salfred      case language_c:
998195957Salfred      case language_cplus:
999195957Salfred	 switch(op)
1000195957Salfred	 {
1001195957Salfred	 case BINOP_DIV:
1002195957Salfred	    if (!numeric_type(t1) || !numeric_type(t2))
1003195957Salfred	       type_op_error ("Arguments to %s must be numbers.",op);
1004195957Salfred	    break;
1005194676Sthompsa	 }
1006195957Salfred	 break;
1007195957Salfred#endif
1008194676Sthompsa
1009195957Salfred#ifdef _LANG_m2
1010194676Sthompsa      case language_m2:
1011195957Salfred	 switch(op)
1012195957Salfred	 {
1013195957Salfred	 case BINOP_DIV:
1014195957Salfred	    if (!float_type(t1) || !float_type(t2))
1015195957Salfred	       type_op_error ("Arguments to %s must be floating point numbers.",op);
1016195957Salfred	    break;
1017195957Salfred	 case BINOP_INTDIV:
1018195957Salfred	    if (!integral_type(t1) || !integral_type(t2))
1019195957Salfred	       type_op_error ("Arguments to %s must be of integral type.",op);
1020195957Salfred	    break;
1021195957Salfred	 }
1022195957Salfred#endif
1023195957Salfred
1024195957Salfred#ifdef _LANG_chill
1025195957Salfred       case language_chill:
1026195957Salfred	 error ("Missing Chill support in function binop_type_check.");/*FIXME*/
1027195957Salfred#endif
1028195957Salfred
1029195957Salfred      }
1030195957Salfred   }
1031195957Salfred}
1032195957Salfred
1033195957Salfred#endif	/* 0 */
1034195957Salfred
1035195957Salfred
1036194676Sthompsa/* This page contains functions for the printing out of
1037194676Sthompsa   error messages that occur during type- and range-
1038195957Salfred   checking. */
1039195957Salfred
1040195957Salfred/* Prints the format string FMT with the operator as a string
1041195957Salfred   corresponding to the opcode OP.  If FATAL is non-zero, then
1042195957Salfred   this is an error and error () is called.  Otherwise, it is
1043195957Salfred   a warning and printf() is called. */
1044195957Salfredvoid
1045195957Salfredop_error (fmt,op,fatal)
1046195957Salfred   char *fmt;
1047195957Salfred   enum exp_opcode op;
1048195957Salfred   int fatal;
1049195957Salfred{
1050195957Salfred   if (fatal)
1051195957Salfred      error (fmt,op_string(op));
1052195957Salfred   else
1053195957Salfred   {
1054195957Salfred      warning (fmt,op_string(op));
1055195957Salfred   }
1056195957Salfred}
1057195957Salfred
1058195957Salfred/* These are called when a language fails a type- or range-check.
1059195957Salfred   The first argument should be a printf()-style format string, and
1060195957Salfred   the rest of the arguments should be its arguments.  If
1061195957Salfred   [type|range]_check is [type|range]_check_on, then return_to_top_level()
1062195957Salfred   is called in the style of error ().  Otherwise, the message is prefixed
1063195957Salfred   by the value of warning_pre_print and we do not return to the top level. */
1064195957Salfred
1065195957Salfredvoid
1066195957Salfred#ifdef ANSI_PROTOTYPES
1067195957Salfredtype_error (char *string, ...)
1068195957Salfred#else
1069195957Salfredtype_error (va_alist)
1070195957Salfred     va_dcl
1071195957Salfred#endif
1072195957Salfred{
1073195957Salfred   va_list args;
1074195957Salfred#ifdef ANSI_PROTOTYPES
1075195957Salfred   va_start (args, string);
1076195957Salfred#else
1077195957Salfred   char *string;
1078195957Salfred   va_start (args);
1079195957Salfred   string = va_arg (args, char *);
1080194676Sthompsa#endif
1081194676Sthompsa
1082194676Sthompsa   if (type_check == type_check_warn)
1083195957Salfred     fprintf_filtered (gdb_stderr, warning_pre_print);
1084195957Salfred   else
1085195957Salfred     error_begin ();
1086195957Salfred
1087194676Sthompsa   vfprintf_filtered (gdb_stderr, string, args);
1088195957Salfred   fprintf_filtered (gdb_stderr, "\n");
1089195957Salfred   va_end (args);
1090195957Salfred   if (type_check == type_check_on)
1091195957Salfred     return_to_top_level (RETURN_ERROR);
1092195957Salfred}
1093195957Salfred
1094194676Sthompsavoid
1095195957Salfred#ifdef ANSI_PROTOTYPES
1096195957Salfredrange_error (char *string, ...)
1097195957Salfred#else
1098195957Salfredrange_error (va_alist)
1099194676Sthompsa     va_dcl
1100195957Salfred#endif
1101195957Salfred{
1102194676Sthompsa   va_list args;
1103195957Salfred#ifdef ANSI_PROTOTYPES
1104195957Salfred   va_start (args, string);
1105194676Sthompsa#else
1106195957Salfred   char *string;
1107194676Sthompsa   va_start (args);
1108195957Salfred   string = va_arg (args, char *);
1109195957Salfred#endif
1110194676Sthompsa
1111195957Salfred   if (range_check == range_check_warn)
1112195957Salfred     fprintf_filtered (gdb_stderr, warning_pre_print);
1113195957Salfred   else
1114195957Salfred     error_begin ();
1115195957Salfred
1116195957Salfred   vfprintf_filtered (gdb_stderr, string, args);
1117195957Salfred   fprintf_filtered (gdb_stderr, "\n");
1118195957Salfred   va_end (args);
1119195957Salfred   if (range_check == range_check_on)
1120195957Salfred     return_to_top_level (RETURN_ERROR);
1121195957Salfred}
1122195957Salfred
1123195957Salfred
1124194676Sthompsa/* This page contains miscellaneous functions */
1125195957Salfred
1126195957Salfred/* Return the language enum for a given language string. */
1127195957Salfred
1128195957Salfredenum language
1129195957Salfredlanguage_enum (str)
1130195957Salfred     char *str;
1131194676Sthompsa{
1132195957Salfred  int i;
1133195957Salfred
1134195957Salfred  for (i = 0; i < languages_size; i++)
1135195957Salfred    if (STREQ (languages[i]->la_name, str))
1136195957Salfred      return languages[i]->la_language;
1137195957Salfred
1138195957Salfred  return language_unknown;
1139195957Salfred}
1140195957Salfred
1141195957Salfred/* Return the language struct for a given language enum. */
1142195957Salfred
1143195957Salfredconst struct language_defn *
1144195957Salfredlanguage_def(lang)
1145195957Salfred   enum language lang;
1146195957Salfred{
1147195957Salfred  int i;
1148195957Salfred
1149195957Salfred  for (i = 0; i < languages_size; i++) {
1150195957Salfred    if (languages[i]->la_language == lang) {
1151195957Salfred      return languages[i];
1152195957Salfred    }
1153195957Salfred  }
1154195957Salfred  return NULL;
1155195957Salfred}
1156195957Salfred
1157195957Salfred/* Return the language as a string */
1158195957Salfredchar *
1159195957Salfredlanguage_str(lang)
1160195957Salfred   enum language lang;
1161195957Salfred{
1162195957Salfred  int i;
1163195957Salfred
1164195957Salfred  for (i = 0; i < languages_size; i++) {
1165195957Salfred    if (languages[i]->la_language == lang) {
1166195957Salfred      return languages[i]->la_name;
1167195957Salfred    }
1168195957Salfred  }
1169194676Sthompsa  return "Unknown";
1170195957Salfred}
1171195957Salfred
1172195957Salfredstatic void
1173195957Salfredset_check (ignore, from_tty)
1174195957Salfred   char *ignore;
1175195957Salfred   int from_tty;
1176195957Salfred{
1177195957Salfred   printf_unfiltered(
1178195957Salfred"\"set check\" must be followed by the name of a check subcommand.\n");
1179195957Salfred   help_list(setchecklist, "set check ", -1, gdb_stdout);
1180195957Salfred}
1181195957Salfred
1182195957Salfredstatic void
1183195957Salfredshow_check (ignore, from_tty)
1184195957Salfred   char *ignore;
1185195957Salfred   int from_tty;
1186195957Salfred{
1187195957Salfred   cmd_show_list(showchecklist, from_tty, "");
1188195957Salfred}
1189195957Salfred
1190195957Salfred/* Add a language to the set of known languages.  */
1191195957Salfred
1192195957Salfredvoid
1193195957Salfredadd_language (lang)
1194195957Salfred     const struct language_defn *lang;
1195195957Salfred{
1196195957Salfred  if (lang->la_magic != LANG_MAGIC)
1197195957Salfred    {
1198195957Salfred      fprintf_unfiltered(gdb_stderr, "Magic number of %s language struct wrong\n",
1199195957Salfred	lang->la_name);
1200195957Salfred      abort();
1201195957Salfred    }
1202195957Salfred
1203195957Salfred  if (!languages)
1204195957Salfred    {
1205195957Salfred      languages_allocsize = DEFAULT_ALLOCSIZE;
1206195957Salfred      languages = (const struct language_defn **) xmalloc
1207195957Salfred	(languages_allocsize * sizeof (*languages));
1208199575Sthompsa    }
1209199575Sthompsa  if (languages_size >= languages_allocsize)
1210195957Salfred    {
1211195957Salfred      languages_allocsize *= 2;
1212195957Salfred      languages = (const struct language_defn **) xrealloc ((char *) languages,
1213195957Salfred	languages_allocsize * sizeof (*languages));
1214195957Salfred    }
1215195957Salfred  languages[languages_size++] = lang;
1216195957Salfred}
1217199575Sthompsa
1218199575Sthompsa/* Define the language that is no language.  */
1219195957Salfred
1220195957Salfredstatic int
1221195957Salfredunk_lang_parser ()
1222195957Salfred{
1223195957Salfred  return 1;
1224195957Salfred}
1225195957Salfred
1226194676Sthompsastatic void
1227195957Salfredunk_lang_error (msg)
1228195957Salfred     char *msg;
1229195957Salfred{
1230195957Salfred  error ("Attempted to parse an expression with unknown language");
1231195957Salfred}
1232195957Salfred
1233195957Salfredstatic void
1234195957Salfredunk_lang_emit_char (c, stream, quoter)
1235195957Salfred     register int c;
1236195957Salfred     GDB_FILE *stream;
1237195957Salfred     int quoter;
1238194676Sthompsa{
1239195957Salfred  error ("internal error - unimplemented function unk_lang_emit_char called.");
1240195957Salfred}
1241194676Sthompsa
1242195957Salfredstatic void
1243194676Sthompsaunk_lang_printchar (c, stream)
1244195957Salfred     register int c;
1245195957Salfred     GDB_FILE *stream;
1246194676Sthompsa{
1247195957Salfred  error ("internal error - unimplemented function unk_lang_printchar called.");
1248195957Salfred}
1249195957Salfred
1250195957Salfredstatic void
1251194676Sthompsaunk_lang_printstr (stream, string, length, width, force_ellipses)
1252195957Salfred     GDB_FILE *stream;
1253195957Salfred     char *string;
1254195957Salfred     unsigned int length;
1255195957Salfred     int width;
1256195957Salfred     int force_ellipses;
1257195957Salfred{
1258195957Salfred  error ("internal error - unimplemented function unk_lang_printstr called.");
1259195957Salfred}
1260195957Salfred
1261195957Salfredstatic struct type *
1262195957Salfredunk_lang_create_fundamental_type (objfile, typeid)
1263195957Salfred     struct objfile *objfile;
1264194676Sthompsa     int typeid;
1265195957Salfred{
1266195957Salfred  error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1267195957Salfred}
1268195957Salfred
1269195957Salfredstatic void
1270195957Salfredunk_lang_print_type (type, varstring, stream, show, level)
1271195560Sthompsa     struct type *type;
1272195957Salfred     char *varstring;
1273195957Salfred     GDB_FILE *stream;
1274195957Salfred     int show;
1275195957Salfred     int level;
1276195957Salfred{
1277195957Salfred  error ("internal error - unimplemented function unk_lang_print_type called.");
1278195957Salfred}
1279195957Salfred
1280194676Sthompsastatic int
1281195957Salfredunk_lang_val_print (type, valaddr,  embedded_offset, address, stream, format, deref_ref,
1282195957Salfred		    recurse, pretty)
1283194676Sthompsa     struct type *type;
1284195957Salfred     char *valaddr;
1285195957Salfred     int embedded_offset;
1286194676Sthompsa     CORE_ADDR address;
1287195957Salfred     GDB_FILE *stream;
1288195957Salfred     int format;
1289195957Salfred     int deref_ref;
1290195957Salfred     int recurse;
1291194676Sthompsa     enum val_prettyprint pretty;
1292195957Salfred{
1293194676Sthompsa  error ("internal error - unimplemented function unk_lang_val_print called.");
1294195957Salfred}
1295195957Salfred
1296195957Salfredstatic int
1297195957Salfredunk_lang_value_print (val, stream, format, pretty)
1298195957Salfred     value_ptr val;
1299195957Salfred     GDB_FILE *stream;
1300195957Salfred     int format;
1301199055Sthompsa     enum val_prettyprint pretty;
1302195957Salfred{
1303195957Salfred  error ("internal error - unimplemented function unk_lang_value_print called.");
1304195957Salfred}
1305195957Salfred
1306195957Salfredstatic struct type ** CONST_PTR (unknown_builtin_types[]) = { 0 };
1307195957Salfredstatic const struct op_print unk_op_print_tab[] = {
1308195957Salfred    {NULL, OP_NULL, PREC_NULL, 0}
1309195957Salfred};
1310195957Salfred
1311195957Salfredconst struct language_defn unknown_language_defn = {
1312195957Salfred  "unknown",
1313195957Salfred  language_unknown,
1314195957Salfred  &unknown_builtin_types[0],
1315195957Salfred  range_check_off,
1316195957Salfred  type_check_off,
1317195957Salfred  unk_lang_parser,
1318195957Salfred  unk_lang_error,
1319195957Salfred  evaluate_subexp_standard,
1320195957Salfred  unk_lang_printchar,		/* Print character constant */
1321195957Salfred  unk_lang_printstr,
1322195957Salfred  unk_lang_emit_char,
1323195957Salfred  unk_lang_create_fundamental_type,
1324195957Salfred  unk_lang_print_type,		/* Print a type using appropriate syntax */
1325195957Salfred  unk_lang_val_print,		/* Print a value using appropriate syntax */
1326195957Salfred  unk_lang_value_print,		/* Print a top-level value */
1327195957Salfred  {"",      "",    "",   ""},	/* Binary format info */
1328195957Salfred  {"0%lo",   "0",   "o",  ""},	/* Octal format info */
1329195957Salfred  {"%ld",    "",    "d",  ""},	/* Decimal format info */
1330199575Sthompsa  {"0x%lx",  "0x",  "x",  ""},	/* Hex format info */
1331195957Salfred  unk_op_print_tab,		/* expression operators for printing */
1332195957Salfred  1,				/* c-style arrays */
1333195957Salfred  0,				/* String lower bound */
1334199575Sthompsa  &builtin_type_char,		/* Type of string elements */
1335199575Sthompsa  LANG_MAGIC
1336199575Sthompsa};
1337199575Sthompsa
1338199575Sthompsa/* These two structs define fake entries for the "local" and "auto" options. */
1339195957Salfredconst struct language_defn auto_language_defn = {
1340195957Salfred  "auto",
1341199575Sthompsa  language_auto,
1342195957Salfred  &unknown_builtin_types[0],
1343195957Salfred  range_check_off,
1344195957Salfred  type_check_off,
1345195957Salfred  unk_lang_parser,
1346195957Salfred  unk_lang_error,
1347195957Salfred  evaluate_subexp_standard,
1348195957Salfred  unk_lang_printchar,		/* Print character constant */
1349195957Salfred  unk_lang_printstr,
1350195957Salfred  unk_lang_emit_char,
1351195957Salfred  unk_lang_create_fundamental_type,
1352195957Salfred  unk_lang_print_type,		/* Print a type using appropriate syntax */
1353194676Sthompsa  unk_lang_val_print,		/* Print a value using appropriate syntax */
1354194676Sthompsa  unk_lang_value_print,		/* Print a top-level value */
1355195957Salfred  {"",      "",    "",   ""},	/* Binary format info */
1356195957Salfred  {"0%lo",   "0",   "o",  ""},	/* Octal format info */
1357194676Sthompsa  {"%ld",    "",    "d",  ""},	/* Decimal format info */
1358195957Salfred  {"0x%lx",  "0x",  "x",  ""},	/* Hex format info */
1359194676Sthompsa  unk_op_print_tab,		/* expression operators for printing */
1360195957Salfred  1,				/* c-style arrays */
1361195957Salfred  0,				/* String lower bound */
1362195957Salfred  &builtin_type_char,		/* Type of string elements */
1363195957Salfred  LANG_MAGIC
1364199055Sthompsa};
1365199575Sthompsa
1366194676Sthompsaconst struct language_defn local_language_defn = {
1367195957Salfred  "local",
1368195957Salfred  language_auto,
1369194676Sthompsa  &unknown_builtin_types[0],
1370199575Sthompsa  range_check_off,
1371195957Salfred  type_check_off,
1372199575Sthompsa  unk_lang_parser,
1373194676Sthompsa  unk_lang_error,
1374195957Salfred  evaluate_subexp_standard,
1375194676Sthompsa  unk_lang_printchar,		/* Print character constant */
1376195957Salfred  unk_lang_printstr,
1377195957Salfred  unk_lang_emit_char,
1378195957Salfred  unk_lang_create_fundamental_type,
1379195957Salfred  unk_lang_print_type,		/* Print a type using appropriate syntax */
1380195957Salfred  unk_lang_val_print,		/* Print a value using appropriate syntax */
1381195957Salfred  unk_lang_value_print,		/* Print a top-level value */
1382195957Salfred  {"",      "",    "",   ""},	/* Binary format info */
1383195957Salfred  {"0%lo",   "0",   "o",  ""},	/* Octal format info */
1384195957Salfred  {"%ld",    "",    "d",  ""},	/* Decimal format info */
1385195957Salfred  {"0x%lx",  "0x",  "x",  ""},	/* Hex format info */
1386199575Sthompsa  unk_op_print_tab,		/* expression operators for printing */
1387199575Sthompsa  1,				/* c-style arrays */
1388195957Salfred  0,				/* String lower bound */
1389195957Salfred  &builtin_type_char,		/* Type of string elements */
1390195957Salfred  LANG_MAGIC
1391195957Salfred};
1392195957Salfred
1393199575Sthompsa/* Initialize the language routines */
1394199575Sthompsa
1395199575Sthompsavoid
1396199575Sthompsa_initialize_language()
1397199575Sthompsa{
1398195957Salfred   struct cmd_list_element *set, *show;
1399195957Salfred
1400195957Salfred   /* GDB commands for language specific stuff */
1401199575Sthompsa
1402199575Sthompsa   set = add_set_cmd ("language", class_support, var_string_noescape,
1403195957Salfred		      (char *)&language,
1404195957Salfred		      "Set the current source language.",
1405199575Sthompsa		      &setlist);
1406195957Salfred   show = add_show_from_set (set, &showlist);
1407199575Sthompsa   set->function.cfunc = set_language_command;
1408199575Sthompsa   show->function.cfunc = show_language_command;
1409195957Salfred
1410195957Salfred   add_prefix_cmd ("check", no_class, set_check,
1411195957Salfred		   "Set the status of the type/range checker",
1412195957Salfred		   &setchecklist, "set check ", 0, &setlist);
1413195957Salfred   add_alias_cmd ("c", "check", no_class, 1, &setlist);
1414199575Sthompsa   add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1415199575Sthompsa
1416195957Salfred   add_prefix_cmd ("check", no_class, show_check,
1417195957Salfred		   "Show the status of the type/range checker",
1418195957Salfred		   &showchecklist, "show check ", 0, &showlist);
1419195957Salfred   add_alias_cmd ("c", "check", no_class, 1, &showlist);
1420195957Salfred   add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1421195957Salfred
1422199575Sthompsa   set = add_set_cmd ("type", class_support, var_string_noescape,
1423195957Salfred		      (char *)&type,
1424195957Salfred		      "Set type checking.  (on/warn/off/auto)",
1425195957Salfred		      &setchecklist);
1426195957Salfred   show = add_show_from_set (set, &showchecklist);
1427195957Salfred   set->function.cfunc = set_type_command;
1428195957Salfred   show->function.cfunc = show_type_command;
1429199575Sthompsa
1430194676Sthompsa   set = add_set_cmd ("range", class_support, var_string_noescape,
1431194676Sthompsa		      (char *)&range,
1432195957Salfred		      "Set range checking.  (on/warn/off/auto)",
1433195957Salfred		      &setchecklist);
1434195957Salfred   show = add_show_from_set (set, &showchecklist);
1435195957Salfred   set->function.cfunc = set_range_command;
1436195957Salfred   show->function.cfunc = show_range_command;
1437199055Sthompsa
1438199055Sthompsa   add_language (&unknown_language_defn);
1439199055Sthompsa   add_language (&local_language_defn);
1440199055Sthompsa   add_language (&auto_language_defn);
1441199055Sthompsa
1442199055Sthompsa   language = savestring ("auto",strlen("auto"));
1443199055Sthompsa   range = savestring ("auto",strlen("auto"));
1444199055Sthompsa   type = savestring ("auto",strlen("auto"));
1445199055Sthompsa
1446199055Sthompsa   /* Have the above take effect */
1447199055Sthompsa
1448199055Sthompsa   set_language_command (language, 0);
1449199055Sthompsa   set_type_command (NULL, 0);
1450213853Shselasky   set_range_command (NULL, 0);
1451213853Shselasky}
1452213853Shselasky