GDB (API)
/home/stan/gdb/src/gdb/f-lang.c
Go to the documentation of this file.
00001 /* Fortran language support routines for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1993-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Motorola.  Adapted from the C parser by Farooq Butt
00006    (fmbutt@engage.sps.mot.com).
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 #include "gdb_string.h"
00025 #include "symtab.h"
00026 #include "gdbtypes.h"
00027 #include "expression.h"
00028 #include "parser-defs.h"
00029 #include "language.h"
00030 #include "f-lang.h"
00031 #include "valprint.h"
00032 #include "value.h"
00033 #include "cp-support.h"
00034 #include "charset.h"
00035 #include "c-lang.h"
00036 
00037 
00038 /* Local functions */
00039 
00040 extern void _initialize_f_language (void);
00041 
00042 static void f_printchar (int c, struct type *type, struct ui_file * stream);
00043 static void f_emit_char (int c, struct type *type,
00044                          struct ui_file * stream, int quoter);
00045 
00046 /* Return the encoding that should be used for the character type
00047    TYPE.  */
00048 
00049 static const char *
00050 f_get_encoding (struct type *type)
00051 {
00052   const char *encoding;
00053 
00054   switch (TYPE_LENGTH (type))
00055     {
00056     case 1:
00057       encoding = target_charset (get_type_arch (type));
00058       break;
00059     case 4:
00060       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_BIG)
00061         encoding = "UTF-32BE";
00062       else
00063         encoding = "UTF-32LE";
00064       break;
00065 
00066     default:
00067       error (_("unrecognized character type"));
00068     }
00069 
00070   return encoding;
00071 }
00072 
00073 /* Print the character C on STREAM as part of the contents of a literal
00074    string whose delimiter is QUOTER.  Note that that format for printing
00075    characters and strings is language specific.
00076    FIXME:  This is a copy of the same function from c-exp.y.  It should
00077    be replaced with a true F77 version.  */
00078 
00079 static void
00080 f_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
00081 {
00082   const char *encoding = f_get_encoding (type);
00083 
00084   generic_emit_char (c, type, stream, quoter, encoding);
00085 }
00086 
00087 /* Implementation of la_printchar.  */
00088 
00089 static void
00090 f_printchar (int c, struct type *type, struct ui_file *stream)
00091 {
00092   fputs_filtered ("'", stream);
00093   LA_EMIT_CHAR (c, type, stream, '\'');
00094   fputs_filtered ("'", stream);
00095 }
00096 
00097 /* Print the character string STRING, printing at most LENGTH characters.
00098    Printing stops early if the number hits print_max; repeat counts
00099    are printed as appropriate.  Print ellipses at the end if we
00100    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
00101    FIXME:  This is a copy of the same function from c-exp.y.  It should
00102    be replaced with a true F77 version.  */
00103 
00104 static void
00105 f_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
00106             unsigned int length, const char *encoding, int force_ellipses,
00107             const struct value_print_options *options)
00108 {
00109   const char *type_encoding = f_get_encoding (type);
00110 
00111   if (TYPE_LENGTH (type) == 4)
00112     fputs_filtered ("4_", stream);
00113 
00114   if (!encoding || !*encoding)
00115     encoding = type_encoding;
00116 
00117   generic_printstr (stream, type, string, length, encoding,
00118                     force_ellipses, '\'', 0, options);
00119 }
00120 
00121 
00122 /* Table of operators and their precedences for printing expressions.  */
00123 
00124 static const struct op_print f_op_print_tab[] =
00125 {
00126   {"+", BINOP_ADD, PREC_ADD, 0},
00127   {"+", UNOP_PLUS, PREC_PREFIX, 0},
00128   {"-", BINOP_SUB, PREC_ADD, 0},
00129   {"-", UNOP_NEG, PREC_PREFIX, 0},
00130   {"*", BINOP_MUL, PREC_MUL, 0},
00131   {"/", BINOP_DIV, PREC_MUL, 0},
00132   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
00133   {"MOD", BINOP_REM, PREC_MUL, 0},
00134   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
00135   {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
00136   {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
00137   {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
00138   {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
00139   {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
00140   {".LE.", BINOP_LEQ, PREC_ORDER, 0},
00141   {".GE.", BINOP_GEQ, PREC_ORDER, 0},
00142   {".GT.", BINOP_GTR, PREC_ORDER, 0},
00143   {".LT.", BINOP_LESS, PREC_ORDER, 0},
00144   {"**", UNOP_IND, PREC_PREFIX, 0},
00145   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
00146   {NULL, 0, 0, 0}
00147 };
00148 
00149 enum f_primitive_types {
00150   f_primitive_type_character,
00151   f_primitive_type_logical,
00152   f_primitive_type_logical_s1,
00153   f_primitive_type_logical_s2,
00154   f_primitive_type_logical_s8,
00155   f_primitive_type_integer,
00156   f_primitive_type_integer_s2,
00157   f_primitive_type_real,
00158   f_primitive_type_real_s8,
00159   f_primitive_type_real_s16,
00160   f_primitive_type_complex_s8,
00161   f_primitive_type_complex_s16,
00162   f_primitive_type_void,
00163   nr_f_primitive_types
00164 };
00165 
00166 static void
00167 f_language_arch_info (struct gdbarch *gdbarch,
00168                       struct language_arch_info *lai)
00169 {
00170   const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
00171 
00172   lai->string_char_type = builtin->builtin_character;
00173   lai->primitive_type_vector
00174     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_f_primitive_types + 1,
00175                               struct type *);
00176 
00177   lai->primitive_type_vector [f_primitive_type_character]
00178     = builtin->builtin_character;
00179   lai->primitive_type_vector [f_primitive_type_logical]
00180     = builtin->builtin_logical;
00181   lai->primitive_type_vector [f_primitive_type_logical_s1]
00182     = builtin->builtin_logical_s1;
00183   lai->primitive_type_vector [f_primitive_type_logical_s2]
00184     = builtin->builtin_logical_s2;
00185   lai->primitive_type_vector [f_primitive_type_logical_s8]
00186     = builtin->builtin_logical_s8;
00187   lai->primitive_type_vector [f_primitive_type_real]
00188     = builtin->builtin_real;
00189   lai->primitive_type_vector [f_primitive_type_real_s8]
00190     = builtin->builtin_real_s8;
00191   lai->primitive_type_vector [f_primitive_type_real_s16]
00192     = builtin->builtin_real_s16;
00193   lai->primitive_type_vector [f_primitive_type_complex_s8]
00194     = builtin->builtin_complex_s8;
00195   lai->primitive_type_vector [f_primitive_type_complex_s16]
00196     = builtin->builtin_complex_s16;
00197   lai->primitive_type_vector [f_primitive_type_void]
00198     = builtin->builtin_void;
00199 
00200   lai->bool_type_symbol = "logical";
00201   lai->bool_type_default = builtin->builtin_logical_s2;
00202 }
00203 
00204 /* Remove the modules separator :: from the default break list.  */
00205 
00206 static char *
00207 f_word_break_characters (void)
00208 {
00209   static char *retval;
00210 
00211   if (!retval)
00212     {
00213       char *s;
00214 
00215       retval = xstrdup (default_word_break_characters ());
00216       s = strchr (retval, ':');
00217       if (s)
00218         {
00219           char *last_char = &s[strlen (s) - 1];
00220 
00221           *s = *last_char;
00222           *last_char = 0;
00223         }
00224     }
00225   return retval;
00226 }
00227 
00228 /* Consider the modules separator :: as a valid symbol name character
00229    class.  */
00230 
00231 static VEC (char_ptr) *
00232 f_make_symbol_completion_list (const char *text, const char *word,
00233                                enum type_code code)
00234 {
00235   return default_make_symbol_completion_list_break_on (text, word, ":", code);
00236 }
00237 
00238 const struct language_defn f_language_defn =
00239 {
00240   "fortran",
00241   language_fortran,
00242   range_check_on,
00243   case_sensitive_off,
00244   array_column_major,
00245   macro_expansion_no,
00246   &exp_descriptor_standard,
00247   f_parse,                      /* parser */
00248   f_error,                      /* parser error function */
00249   null_post_parser,
00250   f_printchar,                  /* Print character constant */
00251   f_printstr,                   /* function to print string constant */
00252   f_emit_char,                  /* Function to print a single character */
00253   f_print_type,                 /* Print a type using appropriate syntax */
00254   default_print_typedef,        /* Print a typedef using appropriate syntax */
00255   f_val_print,                  /* Print a value using appropriate syntax */
00256   c_value_print,                /* FIXME */
00257   default_read_var_value,       /* la_read_var_value */
00258   NULL,                         /* Language specific skip_trampoline */
00259   NULL,                         /* name_of_this */
00260   cp_lookup_symbol_nonlocal,    /* lookup_symbol_nonlocal */
00261   basic_lookup_transparent_type,/* lookup_transparent_type */
00262   NULL,                         /* Language specific symbol demangler */
00263   NULL,                         /* Language specific
00264                                    class_name_from_physname */
00265   f_op_print_tab,               /* expression operators for printing */
00266   0,                            /* arrays are first-class (not c-style) */
00267   1,                            /* String lower bound */
00268   f_word_break_characters,
00269   f_make_symbol_completion_list,
00270   f_language_arch_info,
00271   default_print_array_index,
00272   default_pass_by_reference,
00273   default_get_string,
00274   NULL,                         /* la_get_symbol_name_cmp */
00275   iterate_over_symbols,
00276   LANG_MAGIC
00277 };
00278 
00279 static void *
00280 build_fortran_types (struct gdbarch *gdbarch)
00281 {
00282   struct builtin_f_type *builtin_f_type
00283     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
00284 
00285   builtin_f_type->builtin_void
00286     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "VOID");
00287 
00288   builtin_f_type->builtin_character
00289     = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
00290 
00291   builtin_f_type->builtin_logical_s1
00292     = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
00293 
00294   builtin_f_type->builtin_integer_s2
00295     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
00296                          "integer*2");
00297 
00298   builtin_f_type->builtin_logical_s2
00299     = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
00300                          "logical*2");
00301 
00302   builtin_f_type->builtin_logical_s8
00303     = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
00304                          "logical*8");
00305 
00306   builtin_f_type->builtin_integer
00307     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
00308                          "integer");
00309 
00310   builtin_f_type->builtin_logical
00311     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
00312                          "logical*4");
00313 
00314   builtin_f_type->builtin_real
00315     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
00316                        "real", NULL);
00317   builtin_f_type->builtin_real_s8
00318     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
00319                        "real*8", NULL);
00320   builtin_f_type->builtin_real_s16
00321     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
00322                        "real*16", NULL);
00323 
00324   builtin_f_type->builtin_complex_s8
00325     = arch_complex_type (gdbarch, "complex*8",
00326                          builtin_f_type->builtin_real);
00327   builtin_f_type->builtin_complex_s16
00328     = arch_complex_type (gdbarch, "complex*16",
00329                          builtin_f_type->builtin_real_s8);
00330   builtin_f_type->builtin_complex_s32
00331     = arch_complex_type (gdbarch, "complex*32",
00332                          builtin_f_type->builtin_real_s16);
00333 
00334   return builtin_f_type;
00335 }
00336 
00337 static struct gdbarch_data *f_type_data;
00338 
00339 const struct builtin_f_type *
00340 builtin_f_type (struct gdbarch *gdbarch)
00341 {
00342   return gdbarch_data (gdbarch, f_type_data);
00343 }
00344 
00345 void
00346 _initialize_f_language (void)
00347 {
00348   f_type_data = gdbarch_data_register_post_init (build_fortran_types);
00349 
00350   add_language (&f_language_defn);
00351 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines