gdb/f-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Fortran language support routines for GDB, the GNU debugger.

  2.    Copyright (C) 1993-2015 Free Software Foundation, Inc.

  3.    Contributed by Motorola.  Adapted from the C parser by Farooq Butt
  4.    (fmbutt@engage.sps.mot.com).

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "parser-defs.h"
  21. #include "language.h"
  22. #include "varobj.h"
  23. #include "f-lang.h"
  24. #include "valprint.h"
  25. #include "value.h"
  26. #include "cp-support.h"
  27. #include "charset.h"
  28. #include "c-lang.h"


  29. /* Local functions */

  30. extern void _initialize_f_language (void);

  31. static void f_printchar (int c, struct type *type, struct ui_file * stream);
  32. static void f_emit_char (int c, struct type *type,
  33.                          struct ui_file * stream, int quoter);

  34. /* Return the encoding that should be used for the character type
  35.    TYPE.  */

  36. static const char *
  37. f_get_encoding (struct type *type)
  38. {
  39.   const char *encoding;

  40.   switch (TYPE_LENGTH (type))
  41.     {
  42.     case 1:
  43.       encoding = target_charset (get_type_arch (type));
  44.       break;
  45.     case 4:
  46.       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_BIG)
  47.         encoding = "UTF-32BE";
  48.       else
  49.         encoding = "UTF-32LE";
  50.       break;

  51.     default:
  52.       error (_("unrecognized character type"));
  53.     }

  54.   return encoding;
  55. }

  56. /* Print the character C on STREAM as part of the contents of a literal
  57.    string whose delimiter is QUOTER.  Note that that format for printing
  58.    characters and strings is language specific.
  59.    FIXME:  This is a copy of the same function from c-exp.y.  It should
  60.    be replaced with a true F77 version.  */

  61. static void
  62. f_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
  63. {
  64.   const char *encoding = f_get_encoding (type);

  65.   generic_emit_char (c, type, stream, quoter, encoding);
  66. }

  67. /* Implementation of la_printchar.  */

  68. static void
  69. f_printchar (int c, struct type *type, struct ui_file *stream)
  70. {
  71.   fputs_filtered ("'", stream);
  72.   LA_EMIT_CHAR (c, type, stream, '\'');
  73.   fputs_filtered ("'", stream);
  74. }

  75. /* Print the character string STRING, printing at most LENGTH characters.
  76.    Printing stops early if the number hits print_max; repeat counts
  77.    are printed as appropriate.  Print ellipses at the end if we
  78.    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
  79.    FIXME:  This is a copy of the same function from c-exp.y.  It should
  80.    be replaced with a true F77 version.  */

  81. static void
  82. f_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
  83.             unsigned int length, const char *encoding, int force_ellipses,
  84.             const struct value_print_options *options)
  85. {
  86.   const char *type_encoding = f_get_encoding (type);

  87.   if (TYPE_LENGTH (type) == 4)
  88.     fputs_filtered ("4_", stream);

  89.   if (!encoding || !*encoding)
  90.     encoding = type_encoding;

  91.   generic_printstr (stream, type, string, length, encoding,
  92.                     force_ellipses, '\'', 0, options);
  93. }


  94. /* Table of operators and their precedences for printing expressions.  */

  95. static const struct op_print f_op_print_tab[] =
  96. {
  97.   {"+", BINOP_ADD, PREC_ADD, 0},
  98.   {"+", UNOP_PLUS, PREC_PREFIX, 0},
  99.   {"-", BINOP_SUB, PREC_ADD, 0},
  100.   {"-", UNOP_NEG, PREC_PREFIX, 0},
  101.   {"*", BINOP_MUL, PREC_MUL, 0},
  102.   {"/", BINOP_DIV, PREC_MUL, 0},
  103.   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
  104.   {"MOD", BINOP_REM, PREC_MUL, 0},
  105.   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
  106.   {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
  107.   {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
  108.   {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
  109.   {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
  110.   {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  111.   {".LE.", BINOP_LEQ, PREC_ORDER, 0},
  112.   {".GE.", BINOP_GEQ, PREC_ORDER, 0},
  113.   {".GT.", BINOP_GTR, PREC_ORDER, 0},
  114.   {".LT.", BINOP_LESS, PREC_ORDER, 0},
  115.   {"**", UNOP_IND, PREC_PREFIX, 0},
  116.   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
  117.   {NULL, 0, 0, 0}
  118. };

  119. enum f_primitive_types {
  120.   f_primitive_type_character,
  121.   f_primitive_type_logical,
  122.   f_primitive_type_logical_s1,
  123.   f_primitive_type_logical_s2,
  124.   f_primitive_type_logical_s8,
  125.   f_primitive_type_integer,
  126.   f_primitive_type_integer_s2,
  127.   f_primitive_type_real,
  128.   f_primitive_type_real_s8,
  129.   f_primitive_type_real_s16,
  130.   f_primitive_type_complex_s8,
  131.   f_primitive_type_complex_s16,
  132.   f_primitive_type_void,
  133.   nr_f_primitive_types
  134. };

  135. static void
  136. f_language_arch_info (struct gdbarch *gdbarch,
  137.                       struct language_arch_info *lai)
  138. {
  139.   const struct builtin_f_type *builtin = builtin_f_type (gdbarch);

  140.   lai->string_char_type = builtin->builtin_character;
  141.   lai->primitive_type_vector
  142.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_f_primitive_types + 1,
  143.                               struct type *);

  144.   lai->primitive_type_vector [f_primitive_type_character]
  145.     = builtin->builtin_character;
  146.   lai->primitive_type_vector [f_primitive_type_logical]
  147.     = builtin->builtin_logical;
  148.   lai->primitive_type_vector [f_primitive_type_logical_s1]
  149.     = builtin->builtin_logical_s1;
  150.   lai->primitive_type_vector [f_primitive_type_logical_s2]
  151.     = builtin->builtin_logical_s2;
  152.   lai->primitive_type_vector [f_primitive_type_logical_s8]
  153.     = builtin->builtin_logical_s8;
  154.   lai->primitive_type_vector [f_primitive_type_real]
  155.     = builtin->builtin_real;
  156.   lai->primitive_type_vector [f_primitive_type_real_s8]
  157.     = builtin->builtin_real_s8;
  158.   lai->primitive_type_vector [f_primitive_type_real_s16]
  159.     = builtin->builtin_real_s16;
  160.   lai->primitive_type_vector [f_primitive_type_complex_s8]
  161.     = builtin->builtin_complex_s8;
  162.   lai->primitive_type_vector [f_primitive_type_complex_s16]
  163.     = builtin->builtin_complex_s16;
  164.   lai->primitive_type_vector [f_primitive_type_void]
  165.     = builtin->builtin_void;

  166.   lai->bool_type_symbol = "logical";
  167.   lai->bool_type_default = builtin->builtin_logical_s2;
  168. }

  169. /* Remove the modules separator :: from the default break list.  */

  170. static char *
  171. f_word_break_characters (void)
  172. {
  173.   static char *retval;

  174.   if (!retval)
  175.     {
  176.       char *s;

  177.       retval = xstrdup (default_word_break_characters ());
  178.       s = strchr (retval, ':');
  179.       if (s)
  180.         {
  181.           char *last_char = &s[strlen (s) - 1];

  182.           *s = *last_char;
  183.           *last_char = 0;
  184.         }
  185.     }
  186.   return retval;
  187. }

  188. /* Consider the modules separator :: as a valid symbol name character
  189.    class.  */

  190. static VEC (char_ptr) *
  191. f_make_symbol_completion_list (const char *text, const char *word,
  192.                                enum type_code code)
  193. {
  194.   return default_make_symbol_completion_list_break_on (text, word, ":", code);
  195. }

  196. const struct language_defn f_language_defn =
  197. {
  198.   "fortran",
  199.   "Fortran",
  200.   language_fortran,
  201.   range_check_on,
  202.   case_sensitive_off,
  203.   array_column_major,
  204.   macro_expansion_no,
  205.   &exp_descriptor_standard,
  206.   f_parse,                        /* parser */
  207.   f_error,                        /* parser error function */
  208.   null_post_parser,
  209.   f_printchar,                        /* Print character constant */
  210.   f_printstr,                        /* function to print string constant */
  211.   f_emit_char,                        /* Function to print a single character */
  212.   f_print_type,                        /* Print a type using appropriate syntax */
  213.   default_print_typedef,        /* Print a typedef using appropriate syntax */
  214.   f_val_print,                        /* Print a value using appropriate syntax */
  215.   c_value_print,                /* FIXME */
  216.   default_read_var_value,        /* la_read_var_value */
  217.   NULL,                                /* Language specific skip_trampoline */
  218.   NULL,                            /* name_of_this */
  219.   cp_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  220.   basic_lookup_transparent_type,/* lookup_transparent_type */
  221.   NULL,                                /* Language specific symbol demangler */
  222.   NULL,                                /* Language specific
  223.                                    class_name_from_physname */
  224.   f_op_print_tab,                /* expression operators for printing */
  225.   0,                                /* arrays are first-class (not c-style) */
  226.   1,                                /* String lower bound */
  227.   f_word_break_characters,
  228.   f_make_symbol_completion_list,
  229.   f_language_arch_info,
  230.   default_print_array_index,
  231.   default_pass_by_reference,
  232.   default_get_string,
  233.   NULL,                                /* la_get_symbol_name_cmp */
  234.   iterate_over_symbols,
  235.   &default_varobj_ops,
  236.   NULL,
  237.   NULL,
  238.   LANG_MAGIC
  239. };

  240. static void *
  241. build_fortran_types (struct gdbarch *gdbarch)
  242. {
  243.   struct builtin_f_type *builtin_f_type
  244.     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);

  245.   builtin_f_type->builtin_void
  246.     = arch_type (gdbarch, TYPE_CODE_VOID, 1, "VOID");

  247.   builtin_f_type->builtin_character
  248.     = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");

  249.   builtin_f_type->builtin_logical_s1
  250.     = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");

  251.   builtin_f_type->builtin_integer_s2
  252.     = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
  253.                          "integer*2");

  254.   builtin_f_type->builtin_logical_s2
  255.     = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
  256.                          "logical*2");

  257.   builtin_f_type->builtin_logical_s8
  258.     = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
  259.                          "logical*8");

  260.   builtin_f_type->builtin_integer
  261.     = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
  262.                          "integer");

  263.   builtin_f_type->builtin_logical
  264.     = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
  265.                          "logical*4");

  266.   builtin_f_type->builtin_real
  267.     = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  268.                        "real", NULL);
  269.   builtin_f_type->builtin_real_s8
  270.     = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  271.                        "real*8", NULL);
  272.   builtin_f_type->builtin_real_s16
  273.     = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  274.                        "real*16", NULL);

  275.   builtin_f_type->builtin_complex_s8
  276.     = arch_complex_type (gdbarch, "complex*8",
  277.                          builtin_f_type->builtin_real);
  278.   builtin_f_type->builtin_complex_s16
  279.     = arch_complex_type (gdbarch, "complex*16",
  280.                          builtin_f_type->builtin_real_s8);
  281.   builtin_f_type->builtin_complex_s32
  282.     = arch_complex_type (gdbarch, "complex*32",
  283.                          builtin_f_type->builtin_real_s16);

  284.   return builtin_f_type;
  285. }

  286. static struct gdbarch_data *f_type_data;

  287. const struct builtin_f_type *
  288. builtin_f_type (struct gdbarch *gdbarch)
  289. {
  290.   return gdbarch_data (gdbarch, f_type_data);
  291. }

  292. void
  293. _initialize_f_language (void)
  294. {
  295.   f_type_data = gdbarch_data_register_post_init (build_fortran_types);

  296.   add_language (&f_language_defn);
  297. }