f-lang.c revision 1.6 1 1.1 christos /* Fortran language support routines for GDB, the GNU debugger.
2 1.1 christos
3 1.6 christos Copyright (C) 1993-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Contributed by Motorola. Adapted from the C parser by Farooq Butt
6 1.1 christos (fmbutt (at) engage.sps.mot.com).
7 1.1 christos
8 1.1 christos This file is part of GDB.
9 1.1 christos
10 1.1 christos This program is free software; you can redistribute it and/or modify
11 1.1 christos it under the terms of the GNU General Public License as published by
12 1.1 christos the Free Software Foundation; either version 3 of the License, or
13 1.1 christos (at your option) any later version.
14 1.1 christos
15 1.1 christos This program is distributed in the hope that it will be useful,
16 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
17 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 1.1 christos GNU General Public License for more details.
19 1.1 christos
20 1.1 christos You should have received a copy of the GNU General Public License
21 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 1.1 christos
23 1.1 christos #include "defs.h"
24 1.1 christos #include "symtab.h"
25 1.1 christos #include "gdbtypes.h"
26 1.1 christos #include "expression.h"
27 1.1 christos #include "parser-defs.h"
28 1.1 christos #include "language.h"
29 1.1 christos #include "varobj.h"
30 1.1 christos #include "f-lang.h"
31 1.1 christos #include "valprint.h"
32 1.1 christos #include "value.h"
33 1.1 christos #include "cp-support.h"
34 1.1 christos #include "charset.h"
35 1.1 christos #include "c-lang.h"
36 1.1 christos
37 1.1 christos
38 1.1 christos /* Local functions */
39 1.1 christos
40 1.1 christos extern void _initialize_f_language (void);
41 1.1 christos
42 1.1 christos static void f_printchar (int c, struct type *type, struct ui_file * stream);
43 1.1 christos static void f_emit_char (int c, struct type *type,
44 1.1 christos struct ui_file * stream, int quoter);
45 1.1 christos
46 1.1 christos /* Return the encoding that should be used for the character type
47 1.1 christos TYPE. */
48 1.1 christos
49 1.1 christos static const char *
50 1.1 christos f_get_encoding (struct type *type)
51 1.1 christos {
52 1.1 christos const char *encoding;
53 1.1 christos
54 1.1 christos switch (TYPE_LENGTH (type))
55 1.1 christos {
56 1.1 christos case 1:
57 1.1 christos encoding = target_charset (get_type_arch (type));
58 1.1 christos break;
59 1.1 christos case 4:
60 1.1 christos if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_BIG)
61 1.1 christos encoding = "UTF-32BE";
62 1.1 christos else
63 1.1 christos encoding = "UTF-32LE";
64 1.1 christos break;
65 1.1 christos
66 1.1 christos default:
67 1.1 christos error (_("unrecognized character type"));
68 1.1 christos }
69 1.1 christos
70 1.1 christos return encoding;
71 1.1 christos }
72 1.1 christos
73 1.1 christos /* Print the character C on STREAM as part of the contents of a literal
74 1.1 christos string whose delimiter is QUOTER. Note that that format for printing
75 1.1 christos characters and strings is language specific.
76 1.1 christos FIXME: This is a copy of the same function from c-exp.y. It should
77 1.1 christos be replaced with a true F77 version. */
78 1.1 christos
79 1.1 christos static void
80 1.1 christos f_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
81 1.1 christos {
82 1.1 christos const char *encoding = f_get_encoding (type);
83 1.1 christos
84 1.1 christos generic_emit_char (c, type, stream, quoter, encoding);
85 1.1 christos }
86 1.1 christos
87 1.1 christos /* Implementation of la_printchar. */
88 1.1 christos
89 1.1 christos static void
90 1.1 christos f_printchar (int c, struct type *type, struct ui_file *stream)
91 1.1 christos {
92 1.1 christos fputs_filtered ("'", stream);
93 1.1 christos LA_EMIT_CHAR (c, type, stream, '\'');
94 1.1 christos fputs_filtered ("'", stream);
95 1.1 christos }
96 1.1 christos
97 1.1 christos /* Print the character string STRING, printing at most LENGTH characters.
98 1.1 christos Printing stops early if the number hits print_max; repeat counts
99 1.1 christos are printed as appropriate. Print ellipses at the end if we
100 1.1 christos had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
101 1.1 christos FIXME: This is a copy of the same function from c-exp.y. It should
102 1.1 christos be replaced with a true F77 version. */
103 1.1 christos
104 1.1 christos static void
105 1.1 christos f_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
106 1.1 christos unsigned int length, const char *encoding, int force_ellipses,
107 1.1 christos const struct value_print_options *options)
108 1.1 christos {
109 1.1 christos const char *type_encoding = f_get_encoding (type);
110 1.1 christos
111 1.1 christos if (TYPE_LENGTH (type) == 4)
112 1.1 christos fputs_filtered ("4_", stream);
113 1.1 christos
114 1.1 christos if (!encoding || !*encoding)
115 1.1 christos encoding = type_encoding;
116 1.1 christos
117 1.1 christos generic_printstr (stream, type, string, length, encoding,
118 1.1 christos force_ellipses, '\'', 0, options);
119 1.1 christos }
120 1.1 christos
121 1.1 christos
123 1.1 christos /* Table of operators and their precedences for printing expressions. */
124 1.1 christos
125 1.1 christos static const struct op_print f_op_print_tab[] =
126 1.1 christos {
127 1.1 christos {"+", BINOP_ADD, PREC_ADD, 0},
128 1.1 christos {"+", UNOP_PLUS, PREC_PREFIX, 0},
129 1.1 christos {"-", BINOP_SUB, PREC_ADD, 0},
130 1.1 christos {"-", UNOP_NEG, PREC_PREFIX, 0},
131 1.1 christos {"*", BINOP_MUL, PREC_MUL, 0},
132 1.1 christos {"/", BINOP_DIV, PREC_MUL, 0},
133 1.1 christos {"DIV", BINOP_INTDIV, PREC_MUL, 0},
134 1.1 christos {"MOD", BINOP_REM, PREC_MUL, 0},
135 1.1 christos {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
136 1.1 christos {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
137 1.1 christos {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
138 1.1 christos {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
139 1.1 christos {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
140 1.1 christos {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
141 1.1 christos {".LE.", BINOP_LEQ, PREC_ORDER, 0},
142 1.1 christos {".GE.", BINOP_GEQ, PREC_ORDER, 0},
143 1.1 christos {".GT.", BINOP_GTR, PREC_ORDER, 0},
144 1.1 christos {".LT.", BINOP_LESS, PREC_ORDER, 0},
145 1.1 christos {"**", UNOP_IND, PREC_PREFIX, 0},
146 1.6 christos {"@", BINOP_REPEAT, PREC_REPEAT, 0},
147 1.1 christos {NULL, OP_NULL, PREC_REPEAT, 0}
148 1.1 christos };
149 1.1 christos
150 1.1 christos enum f_primitive_types {
152 1.1 christos f_primitive_type_character,
153 1.1 christos f_primitive_type_logical,
154 1.1 christos f_primitive_type_logical_s1,
155 1.1 christos f_primitive_type_logical_s2,
156 1.1 christos f_primitive_type_logical_s8,
157 1.1 christos f_primitive_type_integer,
158 1.1 christos f_primitive_type_integer_s2,
159 1.1 christos f_primitive_type_real,
160 1.1 christos f_primitive_type_real_s8,
161 1.1 christos f_primitive_type_real_s16,
162 1.1 christos f_primitive_type_complex_s8,
163 1.1 christos f_primitive_type_complex_s16,
164 1.1 christos f_primitive_type_void,
165 1.1 christos nr_f_primitive_types
166 1.1 christos };
167 1.1 christos
168 1.1 christos static void
169 1.1 christos f_language_arch_info (struct gdbarch *gdbarch,
170 1.1 christos struct language_arch_info *lai)
171 1.1 christos {
172 1.1 christos const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
173 1.1 christos
174 1.1 christos lai->string_char_type = builtin->builtin_character;
175 1.1 christos lai->primitive_type_vector
176 1.1 christos = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_f_primitive_types + 1,
177 1.1 christos struct type *);
178 1.1 christos
179 1.1 christos lai->primitive_type_vector [f_primitive_type_character]
180 1.1 christos = builtin->builtin_character;
181 1.1 christos lai->primitive_type_vector [f_primitive_type_logical]
182 1.1 christos = builtin->builtin_logical;
183 1.1 christos lai->primitive_type_vector [f_primitive_type_logical_s1]
184 1.1 christos = builtin->builtin_logical_s1;
185 1.1 christos lai->primitive_type_vector [f_primitive_type_logical_s2]
186 1.1 christos = builtin->builtin_logical_s2;
187 1.1 christos lai->primitive_type_vector [f_primitive_type_logical_s8]
188 1.1 christos = builtin->builtin_logical_s8;
189 1.1 christos lai->primitive_type_vector [f_primitive_type_real]
190 1.1 christos = builtin->builtin_real;
191 1.1 christos lai->primitive_type_vector [f_primitive_type_real_s8]
192 1.1 christos = builtin->builtin_real_s8;
193 1.1 christos lai->primitive_type_vector [f_primitive_type_real_s16]
194 1.1 christos = builtin->builtin_real_s16;
195 1.1 christos lai->primitive_type_vector [f_primitive_type_complex_s8]
196 1.1 christos = builtin->builtin_complex_s8;
197 1.1 christos lai->primitive_type_vector [f_primitive_type_complex_s16]
198 1.1 christos = builtin->builtin_complex_s16;
199 1.1 christos lai->primitive_type_vector [f_primitive_type_void]
200 1.1 christos = builtin->builtin_void;
201 1.1 christos
202 1.1 christos lai->bool_type_symbol = "logical";
203 1.1 christos lai->bool_type_default = builtin->builtin_logical_s2;
204 1.1 christos }
205 1.1 christos
206 1.1 christos /* Remove the modules separator :: from the default break list. */
207 1.1 christos
208 1.1 christos static char *
209 1.1 christos f_word_break_characters (void)
210 1.1 christos {
211 1.1 christos static char *retval;
212 1.1 christos
213 1.1 christos if (!retval)
214 1.1 christos {
215 1.1 christos char *s;
216 1.1 christos
217 1.1 christos retval = xstrdup (default_word_break_characters ());
218 1.1 christos s = strchr (retval, ':');
219 1.1 christos if (s)
220 1.1 christos {
221 1.1 christos char *last_char = &s[strlen (s) - 1];
222 1.1 christos
223 1.1 christos *s = *last_char;
224 1.1 christos *last_char = 0;
225 1.1 christos }
226 1.1 christos }
227 1.1 christos return retval;
228 1.1 christos }
229 1.1 christos
230 1.1 christos /* Consider the modules separator :: as a valid symbol name character
231 1.1 christos class. */
232 1.1 christos
233 1.1 christos static VEC (char_ptr) *
234 1.1 christos f_make_symbol_completion_list (const char *text, const char *word,
235 1.1 christos enum type_code code)
236 1.1 christos {
237 1.1 christos return default_make_symbol_completion_list_break_on (text, word, ":", code);
238 1.6 christos }
239 1.6 christos
240 1.6 christos static const char *f_extensions[] =
241 1.6 christos {
242 1.6 christos ".f", ".F", ".for", ".FOR", ".ftn", ".FTN", ".fpp", ".FPP",
243 1.6 christos ".f90", ".F90", ".f95", ".F95", ".f03", ".F03", ".f08", ".F08",
244 1.6 christos NULL
245 1.1 christos };
246 1.1 christos
247 1.1 christos const struct language_defn f_language_defn =
248 1.1 christos {
249 1.1 christos "fortran",
250 1.1 christos "Fortran",
251 1.1 christos language_fortran,
252 1.1 christos range_check_on,
253 1.1 christos case_sensitive_off,
254 1.6 christos array_column_major,
255 1.1 christos macro_expansion_no,
256 1.1 christos f_extensions,
257 1.6 christos &exp_descriptor_standard,
258 1.1 christos f_parse, /* parser */
259 1.1 christos f_yyerror, /* parser error function */
260 1.1 christos null_post_parser,
261 1.1 christos f_printchar, /* Print character constant */
262 1.1 christos f_printstr, /* function to print string constant */
263 1.1 christos f_emit_char, /* Function to print a single character */
264 1.1 christos f_print_type, /* Print a type using appropriate syntax */
265 1.1 christos default_print_typedef, /* Print a typedef using appropriate syntax */
266 1.1 christos f_val_print, /* Print a value using appropriate syntax */
267 1.1 christos c_value_print, /* FIXME */
268 1.1 christos default_read_var_value, /* la_read_var_value */
269 1.1 christos NULL, /* Language specific skip_trampoline */
270 1.1 christos NULL, /* name_of_this */
271 1.6 christos cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
272 1.6 christos basic_lookup_transparent_type,/* lookup_transparent_type */
273 1.6 christos
274 1.6 christos /* We could support demangling here to provide module namespaces
275 1.6 christos also for inferiors with only minimal symbol table (ELF symbols).
276 1.6 christos Just the mangling standard is not standardized across compilers
277 1.1 christos and there is no DW_AT_producer available for inferiors with only
278 1.6 christos the ELF symbols to check the mangling kind. */
279 1.1 christos NULL, /* Language specific symbol demangler */
280 1.1 christos NULL,
281 1.1 christos NULL, /* Language specific
282 1.1 christos class_name_from_physname */
283 1.1 christos f_op_print_tab, /* expression operators for printing */
284 1.1 christos 0, /* arrays are first-class (not c-style) */
285 1.1 christos 1, /* String lower bound */
286 1.1 christos f_word_break_characters,
287 1.1 christos f_make_symbol_completion_list,
288 1.1 christos f_language_arch_info,
289 1.1 christos default_print_array_index,
290 1.1 christos default_pass_by_reference,
291 1.1 christos default_get_string,
292 1.1 christos NULL, /* la_get_symbol_name_cmp */
293 1.3 christos iterate_over_symbols,
294 1.3 christos &default_varobj_ops,
295 1.1 christos NULL,
296 1.1 christos NULL,
297 1.1 christos LANG_MAGIC
298 1.1 christos };
299 1.1 christos
300 1.1 christos static void *
301 1.1 christos build_fortran_types (struct gdbarch *gdbarch)
302 1.1 christos {
303 1.1 christos struct builtin_f_type *builtin_f_type
304 1.1 christos = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
305 1.1 christos
306 1.1 christos builtin_f_type->builtin_void
307 1.1 christos = arch_type (gdbarch, TYPE_CODE_VOID, 1, "VOID");
308 1.1 christos
309 1.1 christos builtin_f_type->builtin_character
310 1.1 christos = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
311 1.1 christos
312 1.1 christos builtin_f_type->builtin_logical_s1
313 1.1 christos = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
314 1.1 christos
315 1.1 christos builtin_f_type->builtin_integer_s2
316 1.1 christos = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
317 1.1 christos "integer*2");
318 1.1 christos
319 1.1 christos builtin_f_type->builtin_logical_s2
320 1.1 christos = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
321 1.1 christos "logical*2");
322 1.1 christos
323 1.1 christos builtin_f_type->builtin_logical_s8
324 1.1 christos = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
325 1.1 christos "logical*8");
326 1.1 christos
327 1.1 christos builtin_f_type->builtin_integer
328 1.1 christos = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
329 1.1 christos "integer");
330 1.1 christos
331 1.1 christos builtin_f_type->builtin_logical
332 1.1 christos = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
333 1.1 christos "logical*4");
334 1.1 christos
335 1.1 christos builtin_f_type->builtin_real
336 1.1 christos = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
337 1.1 christos "real", NULL);
338 1.1 christos builtin_f_type->builtin_real_s8
339 1.1 christos = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
340 1.1 christos "real*8", NULL);
341 1.1 christos builtin_f_type->builtin_real_s16
342 1.1 christos = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
343 1.1 christos "real*16", NULL);
344 1.1 christos
345 1.1 christos builtin_f_type->builtin_complex_s8
346 1.1 christos = arch_complex_type (gdbarch, "complex*8",
347 1.1 christos builtin_f_type->builtin_real);
348 1.1 christos builtin_f_type->builtin_complex_s16
349 1.1 christos = arch_complex_type (gdbarch, "complex*16",
350 1.1 christos builtin_f_type->builtin_real_s8);
351 1.1 christos builtin_f_type->builtin_complex_s32
352 1.1 christos = arch_complex_type (gdbarch, "complex*32",
353 1.1 christos builtin_f_type->builtin_real_s16);
354 1.1 christos
355 1.1 christos return builtin_f_type;
356 1.1 christos }
357 1.1 christos
358 1.1 christos static struct gdbarch_data *f_type_data;
359 1.1 christos
360 1.1 christos const struct builtin_f_type *
361 1.6 christos builtin_f_type (struct gdbarch *gdbarch)
362 1.1 christos {
363 1.1 christos return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
364 1.1 christos }
365 1.1 christos
366 1.1 christos void
367 1.1 christos _initialize_f_language (void)
368 1.1 christos {
369 1.1 christos f_type_data = gdbarch_data_register_post_init (build_fortran_types);
370 1.1 christos
371 add_language (&f_language_defn);
372 }
373