GDB (API)
/home/stan/gdb/src/gdb/m2-typeprint.c
Go to the documentation of this file.
00001 /* Support for printing Modula 2 types for GDB, the GNU debugger.
00002    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00003 
00004    This file is part of GDB.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #include "defs.h"
00020 #include "gdb_obstack.h"
00021 #include "bfd.h"                /* Binary File Description */
00022 #include "symtab.h"
00023 #include "gdbtypes.h"
00024 #include "expression.h"
00025 #include "value.h"
00026 #include "gdbcore.h"
00027 #include "m2-lang.h"
00028 #include "target.h"
00029 #include "language.h"
00030 #include "demangle.h"
00031 #include "c-lang.h"
00032 #include "typeprint.h"
00033 #include "cp-abi.h"
00034 
00035 #include "gdb_string.h"
00036 #include <errno.h>
00037 
00038 static void m2_print_bounds (struct type *type,
00039                              struct ui_file *stream, int show, int level,
00040                              int print_high);
00041 
00042 static void m2_typedef (struct type *, struct ui_file *, int, int,
00043                         const struct type_print_options *);
00044 static void m2_array (struct type *, struct ui_file *, int, int,
00045                       const struct type_print_options *);
00046 static void m2_pointer (struct type *, struct ui_file *, int, int,
00047                         const struct type_print_options *);
00048 static void m2_ref (struct type *, struct ui_file *, int, int,
00049                     const struct type_print_options *);
00050 static void m2_procedure (struct type *, struct ui_file *, int, int,
00051                           const struct type_print_options *);
00052 static void m2_union (struct type *, struct ui_file *);
00053 static void m2_enum (struct type *, struct ui_file *, int, int);
00054 static void m2_range (struct type *, struct ui_file *, int, int,
00055                       const struct type_print_options *);
00056 static void m2_type_name (struct type *type, struct ui_file *stream);
00057 static void m2_short_set (struct type *type, struct ui_file *stream,
00058                           int show, int level);
00059 static int m2_long_set (struct type *type, struct ui_file *stream,
00060                         int show, int level, const struct type_print_options *flags);
00061 static int m2_unbounded_array (struct type *type, struct ui_file *stream,
00062                                int show, int level,
00063                                const struct type_print_options *flags);
00064 static void m2_record_fields (struct type *type, struct ui_file *stream,
00065                               int show, int level, const struct type_print_options *flags);
00066 static void m2_unknown (const char *s, struct type *type,
00067                         struct ui_file *stream, int show, int level);
00068 
00069 int m2_is_long_set (struct type *type);
00070 int m2_is_long_set_of_type (struct type *type, struct type **of_type);
00071 int m2_is_unbounded_array (struct type *type);
00072 
00073 
00074 void
00075 m2_print_type (struct type *type, const char *varstring,
00076                struct ui_file *stream,
00077                int show, int level,
00078                const struct type_print_options *flags)
00079 {
00080   CHECK_TYPEDEF (type);
00081 
00082   QUIT;
00083 
00084   wrap_here ("    ");
00085   if (type == NULL)
00086     {
00087       fputs_filtered (_("<type unknown>"), stream);
00088       return;
00089     }
00090 
00091   switch (TYPE_CODE (type))
00092     {
00093     case TYPE_CODE_SET:
00094       m2_short_set(type, stream, show, level);
00095       break;
00096 
00097     case TYPE_CODE_STRUCT:
00098       if (m2_long_set (type, stream, show, level, flags)
00099           || m2_unbounded_array (type, stream, show, level, flags))
00100         break;
00101       m2_record_fields (type, stream, show, level, flags);
00102       break;
00103 
00104     case TYPE_CODE_TYPEDEF:
00105       m2_typedef (type, stream, show, level, flags);
00106       break;
00107 
00108     case TYPE_CODE_ARRAY:
00109       m2_array (type, stream, show, level, flags);
00110       break;
00111 
00112     case TYPE_CODE_PTR:
00113       m2_pointer (type, stream, show, level, flags);
00114       break;
00115 
00116     case TYPE_CODE_REF:
00117       m2_ref (type, stream, show, level, flags);
00118       break;
00119 
00120     case TYPE_CODE_METHOD:
00121       m2_unknown (_("method"), type, stream, show, level);
00122       break;
00123 
00124     case TYPE_CODE_FUNC:
00125       m2_procedure (type, stream, show, level, flags);
00126       break;
00127 
00128     case TYPE_CODE_UNION:
00129       m2_union (type, stream);
00130       break;
00131 
00132     case TYPE_CODE_ENUM:
00133       m2_enum (type, stream, show, level);
00134       break;
00135 
00136     case TYPE_CODE_VOID:
00137       break;
00138 
00139     case TYPE_CODE_UNDEF:
00140       /* i18n: Do not translate the "struct" part!  */
00141       m2_unknown (_("undef"), type, stream, show, level);
00142       break;
00143 
00144     case TYPE_CODE_ERROR:
00145       m2_unknown (_("error"), type, stream, show, level);
00146       break;
00147 
00148     case TYPE_CODE_RANGE:
00149       m2_range (type, stream, show, level, flags);
00150       break;
00151 
00152     default:
00153       m2_type_name (type, stream);
00154       break;
00155     }
00156 }
00157 
00158 /* Print a typedef using M2 syntax.  TYPE is the underlying type.
00159    NEW_SYMBOL is the symbol naming the type.  STREAM is the stream on
00160    which to print.  */
00161 
00162 void
00163 m2_print_typedef (struct type *type, struct symbol *new_symbol,
00164                   struct ui_file *stream)
00165 {
00166   CHECK_TYPEDEF (type);
00167   fprintf_filtered (stream, "TYPE ");
00168   if (!TYPE_NAME (SYMBOL_TYPE (new_symbol))
00169       || strcmp (TYPE_NAME ((SYMBOL_TYPE (new_symbol))),
00170                  SYMBOL_LINKAGE_NAME (new_symbol)) != 0)
00171     fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new_symbol));
00172   else
00173     fprintf_filtered (stream, "<builtin> = ");
00174   type_print (type, "", stream, 0);
00175   fprintf_filtered (stream, ";\n");
00176 }
00177 
00178 /* m2_type_name - if a, type, has a name then print it.  */
00179 
00180 void
00181 m2_type_name (struct type *type, struct ui_file *stream)
00182 {
00183   if (TYPE_NAME (type) != NULL)
00184     fputs_filtered (TYPE_NAME (type), stream);
00185 }
00186 
00187 /* m2_range - displays a Modula-2 subrange type.  */
00188 
00189 void
00190 m2_range (struct type *type, struct ui_file *stream, int show,
00191           int level, const struct type_print_options *flags)
00192 {
00193   if (TYPE_HIGH_BOUND (type) == TYPE_LOW_BOUND (type))
00194     m2_print_type (TYPE_DOMAIN_TYPE (type), "", stream, show, level,
00195                    flags);
00196   else
00197     {
00198       struct type *target = TYPE_TARGET_TYPE (type);
00199 
00200       fprintf_filtered (stream, "[");
00201       print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
00202       fprintf_filtered (stream, "..");
00203       print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
00204       fprintf_filtered (stream, "]");
00205     }
00206 }
00207 
00208 static void
00209 m2_typedef (struct type *type, struct ui_file *stream, int show,
00210             int level, const struct type_print_options *flags)
00211 {
00212   if (TYPE_NAME (type) != NULL)
00213     {
00214       fputs_filtered (TYPE_NAME (type), stream);
00215       fputs_filtered (" = ", stream);
00216     }
00217   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
00218 }
00219 
00220 /* m2_array - prints out a Modula-2 ARRAY ... OF type.  */
00221 
00222 static void m2_array (struct type *type, struct ui_file *stream,
00223                       int show, int level, const struct type_print_options *flags)
00224 {
00225   fprintf_filtered (stream, "ARRAY [");
00226   if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
00227       && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
00228     {
00229       if (TYPE_INDEX_TYPE (type) != 0)
00230         {
00231           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 0);
00232           fprintf_filtered (stream, "..");
00233           m2_print_bounds (TYPE_INDEX_TYPE (type), stream, show, -1, 1);
00234         }
00235       else
00236         fprintf_filtered (stream, "%d",
00237                           (TYPE_LENGTH (type)
00238                            / TYPE_LENGTH (TYPE_TARGET_TYPE (type))));
00239     }
00240   fprintf_filtered (stream, "] OF ");
00241   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
00242 }
00243 
00244 static void
00245 m2_pointer (struct type *type, struct ui_file *stream, int show,
00246             int level, const struct type_print_options *flags)
00247 {
00248   if (TYPE_CONST (type))
00249     fprintf_filtered (stream, "[...] : ");
00250   else
00251     fprintf_filtered (stream, "POINTER TO ");
00252 
00253   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
00254 }
00255 
00256 static void
00257 m2_ref (struct type *type, struct ui_file *stream, int show,
00258         int level, const struct type_print_options *flags)
00259 {
00260   fprintf_filtered (stream, "VAR");
00261   m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
00262 }
00263 
00264 static void
00265 m2_unknown (const char *s, struct type *type, struct ui_file *stream,
00266             int show, int level)
00267 {
00268   fprintf_filtered (stream, "%s %s", s, _("is unknown"));
00269 }
00270 
00271 static void m2_union (struct type *type, struct ui_file *stream)
00272 {
00273   fprintf_filtered (stream, "union");
00274 }
00275 
00276 static void
00277 m2_procedure (struct type *type, struct ui_file *stream,
00278               int show, int level, const struct type_print_options *flags)
00279 {
00280   fprintf_filtered (stream, "PROCEDURE ");
00281   m2_type_name (type, stream);
00282   if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID)
00283     {
00284       int i, len = TYPE_NFIELDS (type);
00285 
00286       fprintf_filtered (stream, " (");
00287       for (i = 0; i < len; i++)
00288         {
00289           if (i > 0)
00290             {
00291               fputs_filtered (", ", stream);
00292               wrap_here ("    ");
00293             }
00294           m2_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, flags);
00295         }
00296       if (TYPE_TARGET_TYPE (type) != NULL)
00297         {
00298           fprintf_filtered (stream, " : ");
00299           m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
00300         }
00301     }
00302 }
00303 
00304 static void
00305 m2_print_bounds (struct type *type,
00306                  struct ui_file *stream, int show, int level,
00307                  int print_high)
00308 {
00309   struct type *target = TYPE_TARGET_TYPE (type);
00310 
00311   if (TYPE_NFIELDS(type) == 0)
00312     return;
00313 
00314   if (print_high)
00315     print_type_scalar (target, TYPE_HIGH_BOUND (type), stream);
00316   else
00317     print_type_scalar (target, TYPE_LOW_BOUND (type), stream);
00318 }
00319 
00320 static void
00321 m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
00322 {
00323   fprintf_filtered(stream, "SET [");
00324   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
00325                    show - 1, level, 0);
00326 
00327   fprintf_filtered(stream, "..");
00328   m2_print_bounds (TYPE_INDEX_TYPE (type), stream,
00329                    show - 1, level, 1);
00330   fprintf_filtered(stream, "]");
00331 }
00332 
00333 int
00334 m2_is_long_set (struct type *type)
00335 {
00336   LONGEST previous_high = 0;  /* Unnecessary initialization
00337                                  keeps gcc -Wall happy.  */
00338   int len, i;
00339   struct type *range;
00340 
00341   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00342     {
00343 
00344       /* check if all fields of the RECORD are consecutive sets.  */
00345 
00346       len = TYPE_NFIELDS (type);
00347       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
00348         {
00349           if (TYPE_FIELD_TYPE (type, i) == NULL)
00350             return 0;
00351           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) != TYPE_CODE_SET)
00352             return 0;
00353           if (TYPE_FIELD_NAME (type, i) != NULL
00354               && (strcmp (TYPE_FIELD_NAME (type, i), "") != 0))
00355             return 0;
00356           range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
00357           if ((i > TYPE_N_BASECLASSES (type))
00358               && previous_high + 1 != TYPE_LOW_BOUND (range))
00359             return 0;
00360           previous_high = TYPE_HIGH_BOUND (range);
00361         }
00362       return len>0;
00363     }
00364   return 0;
00365 }
00366 
00367 /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
00368                             understands that CHARs might be signed.
00369                             This should be integrated into gdbtypes.c
00370                             inside get_discrete_bounds.  */
00371 
00372 static int
00373 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
00374 {
00375   CHECK_TYPEDEF (type);
00376   switch (TYPE_CODE (type))
00377     {
00378     case TYPE_CODE_CHAR:
00379       if (TYPE_LENGTH (type) < sizeof (LONGEST))
00380         {
00381           if (!TYPE_UNSIGNED (type))
00382             {
00383               *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
00384               *highp = -*lowp - 1;
00385               return 0;
00386             }
00387         }
00388       /* fall through */
00389     default:
00390       return get_discrete_bounds (type, lowp, highp);
00391     }
00392 }
00393 
00394 /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
00395                             SET OF <oftype> of_type is assigned to the
00396                             subtype.  */
00397 
00398 int
00399 m2_is_long_set_of_type (struct type *type, struct type **of_type)
00400 {
00401   int len, i;
00402   struct type *range;
00403   struct type *target;
00404   LONGEST l1, l2;
00405   LONGEST h1, h2;
00406 
00407   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00408     {
00409       len = TYPE_NFIELDS (type);
00410       i = TYPE_N_BASECLASSES (type);
00411       if (len == 0)
00412         return 0;
00413       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i));
00414       target = TYPE_TARGET_TYPE (range);
00415 
00416       l1 = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
00417       h1 = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)));
00418       *of_type = target;
00419       if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
00420         return (l1 == l2 && h1 == h2);
00421       error (_("long_set failed to find discrete bounds for its subtype"));
00422       return 0;
00423     }
00424   error (_("expecting long_set"));
00425   return 0;
00426 }
00427 
00428 static int
00429 m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
00430              const struct type_print_options *flags)
00431 {
00432   struct type *of_type;
00433   int i;
00434   int len = TYPE_NFIELDS (type);
00435   LONGEST low;
00436   LONGEST high;
00437 
00438   if (m2_is_long_set (type))
00439     {
00440       if (TYPE_TAG_NAME (type) != NULL)
00441         {
00442           fputs_filtered (TYPE_TAG_NAME (type), stream);
00443           if (show == 0)
00444             return 1;
00445         }
00446       else if (TYPE_NAME (type) != NULL)
00447         {
00448           fputs_filtered (TYPE_NAME (type), stream);
00449           if (show == 0)
00450             return 1;
00451         }
00452 
00453       if (TYPE_TAG_NAME (type) != NULL || TYPE_NAME (type) != NULL)
00454         fputs_filtered (" = ", stream);
00455 
00456       if (get_long_set_bounds (type, &low, &high))
00457         {
00458           fprintf_filtered(stream, "SET OF ");
00459           i = TYPE_N_BASECLASSES (type);
00460           if (m2_is_long_set_of_type (type, &of_type))
00461             m2_print_type (of_type, "", stream, show - 1, level, flags);
00462           else
00463             {
00464               fprintf_filtered(stream, "[");
00465               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)),
00466                                stream, show - 1, level, 0);
00467 
00468               fprintf_filtered(stream, "..");
00469 
00470               m2_print_bounds (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, len-1)),
00471                                stream, show - 1, level, 1);
00472               fprintf_filtered(stream, "]");
00473             }
00474         }
00475       else
00476         /* i18n: Do not translate the "SET OF" part!  */
00477         fprintf_filtered(stream, _("SET OF <unknown>"));
00478 
00479       return 1;
00480     }
00481   return 0;
00482 }
00483 
00484 /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
00485                            as a Modula-2 unbounded ARRAY type.  */
00486 
00487 int
00488 m2_is_unbounded_array (struct type *type)
00489 {
00490   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00491     {
00492       /*
00493        *  check if we have a structure with exactly two fields named
00494        *  _m2_contents and _m2_high.  It also checks to see if the
00495        *  type of _m2_contents is a pointer.  The TYPE_TARGET_TYPE
00496        *  of the pointer determines the unbounded ARRAY OF type.
00497        */
00498       if (TYPE_NFIELDS (type) != 2)
00499         return 0;
00500       if (strcmp (TYPE_FIELD_NAME (type, 0), "_m2_contents") != 0)
00501         return 0;
00502       if (strcmp (TYPE_FIELD_NAME (type, 1), "_m2_high") != 0)
00503         return 0;
00504       if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) != TYPE_CODE_PTR)
00505         return 0;
00506       return 1;
00507     }
00508   return 0;
00509 }
00510 
00511 /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
00512                         parameter type then display the type as an
00513                         ARRAY OF type.  Returns TRUE if an unbounded
00514                         array type was detected.  */
00515 
00516 static int
00517 m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
00518                     int level, const struct type_print_options *flags)
00519 {
00520   if (m2_is_unbounded_array (type))
00521     {
00522       if (show > 0)
00523         {
00524           fputs_filtered ("ARRAY OF ", stream);
00525           m2_print_type (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
00526                          "", stream, 0, level, flags);
00527         }
00528       return 1;
00529     }
00530   return 0;
00531 }
00532 
00533 void
00534 m2_record_fields (struct type *type, struct ui_file *stream, int show,
00535                   int level, const struct type_print_options *flags)
00536 {
00537   /* Print the tag if it exists.  */
00538   if (TYPE_TAG_NAME (type) != NULL)
00539     {
00540       if (strncmp (TYPE_TAG_NAME (type), "$$", 2) != 0)
00541         {
00542           fputs_filtered (TYPE_TAG_NAME (type), stream);
00543           if (show > 0)
00544             fprintf_filtered (stream, " = ");
00545         }
00546     }
00547   wrap_here ("    ");
00548   if (show < 0)
00549     {
00550       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00551         fprintf_filtered (stream, "RECORD ... END ");
00552       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
00553         fprintf_filtered (stream, "CASE ... END ");
00554     }
00555   else if (show > 0)
00556     {
00557       int i;
00558       int len = TYPE_NFIELDS (type);
00559 
00560       if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00561         fprintf_filtered (stream, "RECORD\n");
00562       else if (TYPE_CODE (type) == TYPE_CODE_UNION)
00563         /* i18n: Do not translate "CASE" and "OF".  */
00564         fprintf_filtered (stream, _("CASE <variant> OF\n"));
00565 
00566       for (i = TYPE_N_BASECLASSES (type); i < len; i++)
00567         {
00568           QUIT;
00569 
00570           print_spaces_filtered (level + 4, stream);
00571           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
00572           fputs_filtered (" : ", stream);
00573           m2_print_type (TYPE_FIELD_TYPE (type, i),
00574                          "",
00575                          stream, 0, level + 4, flags);
00576           if (TYPE_FIELD_PACKED (type, i))
00577             {
00578               /* It is a bitfield.  This code does not attempt
00579                  to look at the bitpos and reconstruct filler,
00580                  unnamed fields.  This would lead to misleading
00581                  results if the compiler does not put out fields
00582                  for such things (I don't know what it does).  */
00583               fprintf_filtered (stream, " : %d",
00584                                 TYPE_FIELD_BITSIZE (type, i));
00585             }
00586           fprintf_filtered (stream, ";\n");
00587         }
00588       
00589       fprintfi_filtered (level, stream, "END ");
00590     }
00591 }
00592 
00593 void
00594 m2_enum (struct type *type, struct ui_file *stream, int show, int level)
00595 {
00596   LONGEST lastval;
00597   int i, len;
00598 
00599   if (show < 0)
00600     {
00601       /* If we just printed a tag name, no need to print anything else.  */
00602       if (TYPE_TAG_NAME (type) == NULL)
00603         fprintf_filtered (stream, "(...)");
00604     }
00605   else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
00606     {
00607       fprintf_filtered (stream, "(");
00608       len = TYPE_NFIELDS (type);
00609       lastval = 0;
00610       for (i = 0; i < len; i++)
00611         {
00612           QUIT;
00613           if (i > 0)
00614             fprintf_filtered (stream, ", ");
00615           wrap_here ("    ");
00616           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
00617           if (lastval != TYPE_FIELD_ENUMVAL (type, i))
00618             {
00619               fprintf_filtered (stream, " = %s",
00620                                 plongest (TYPE_FIELD_ENUMVAL (type, i)));
00621               lastval = TYPE_FIELD_ENUMVAL (type, i);
00622             }
00623           lastval++;
00624         }
00625       fprintf_filtered (stream, ")");
00626     }
00627 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines