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