Debian Codegraph @

gcc/fortran/misc.cc

raw
1 /* Miscellaneous stuff that doesn't fit anywhere else.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "gfortran.h"
25 #include "spellcheck.h"
26 #include "tree.h"
27
28
29 /* Initialize a typespec to unknown. */
30
31 void
32 gfc_clear_ts (gfc_typespec *ts)
33 {
34 ts->type = BT_UNKNOWN;
35 ts->u.derived = NULL;
36 ts->kind = 0;
37 ts->u.cl = NULL;
38 ts->interface = NULL;
39 /* flag that says if the type is C interoperable */
40 ts->is_c_interop = 0;
41 /* says what f90 type the C kind interops with */
42 ts->f90_type = BT_UNKNOWN;
43 /* flag that says whether it's from iso_c_binding or not */
44 ts->is_iso_c = 0;
45 ts->deferred = false;
46 }
47
48
49 /* Open a file for reading. */
50
51 FILE *
52 gfc_open_file (const char *name)
53 {
54 if (!*name)
55 return stdin;
56
57 return fopen (name, "r");
58 }
59
60
61 /* Return a string for each type. */
62
63 const char *
64 gfc_basic_typename (bt type)
65 {
66 const char *p;
67
68 switch (type)
69 {
70 case BT_INTEGER:
71 p = "INTEGER";
72 break;
73 case BT_UNSIGNED:
74 p = "UNSIGNED";
75 break;
76 case BT_REAL:
77 p = "REAL";
78 break;
79 case BT_COMPLEX:
80 p = "COMPLEX";
81 break;
82 case BT_LOGICAL:
83 p = "LOGICAL";
84 break;
85 case BT_CHARACTER:
86 p = "CHARACTER";
87 break;
88 case BT_HOLLERITH:
89 p = "HOLLERITH";
90 break;
91 case BT_UNION:
92 p = "UNION";
93 break;
94 case BT_DERIVED:
95 p = "DERIVED";
96 break;
97 case BT_CLASS:
98 p = "CLASS";
99 break;
100 case BT_PROCEDURE:
101 p = "PROCEDURE";
102 break;
103 case BT_VOID:
104 p = "VOID";
105 break;
106 case BT_BOZ:
107 p = "BOZ";
108 break;
109 case BT_UNKNOWN:
110 p = "UNKNOWN";
111 break;
112 case BT_ASSUMED:
113 p = "TYPE(*)";
114 break;
115 default:
116 gfc_internal_error ("gfc_basic_typename(): Undefined type");
117 }
118
119 return p;
120 }
121
122
123 /* Return a string describing the type and kind of a typespec. Because
124 we return alternating buffers, this subroutine can appear twice in
125 the argument list of a single statement. */
126
127 const char *
128 gfc_typename (gfc_typespec *ts, bool for_hash)
129 {
130 /* Need to add sufficient padding for "TYPE()" + '\0', "UNION()" + '\0',
131 or "CLASS()" + '\0'. */
132 static char buffer1[GFC_MAX_SYMBOL_LEN + 8];
133 static char buffer2[GFC_MAX_SYMBOL_LEN + 8];
134 static int flag = 0;
135 char *buffer;
136 gfc_charlen_t length = 0;
137
138 buffer = flag ? buffer1 : buffer2;
139 flag = !flag;
140
141 switch (ts->type)
142 {
143 case BT_INTEGER:
144 if (ts->f90_type == BT_VOID
145 && ts->u.derived
146 && ts->u.derived->from_intmod == INTMOD_ISO_C_BINDING)
147 sprintf (buffer, "TYPE(%s)", ts->u.derived->name);
148 else
149 sprintf (buffer, "INTEGER(%d)", ts->kind);
150 break;
151 case BT_UNSIGNED:
152 sprintf (buffer, "UNSIGNED(%d)", ts->kind);
153 break;
154 case BT_REAL:
155 sprintf (buffer, "REAL(%d)", ts->kind);
156 break;
157 case BT_COMPLEX:
158 sprintf (buffer, "COMPLEX(%d)", ts->kind);
159 break;
160 case BT_LOGICAL:
161 sprintf (buffer, "LOGICAL(%d)", ts->kind);
162 break;
163 case BT_CHARACTER:
164 if (for_hash)
165 {
166 sprintf (buffer, "CHARACTER(%d)", ts->kind);
167 break;
168 }
169
170 if (ts->u.cl && ts->u.cl->length)
171 length = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
172 if (ts->kind == gfc_default_character_kind)
173 sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ")", length);
174 else
175 sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ",%d)", length,
176 ts->kind);
177 break;
178 case BT_HOLLERITH:
179 sprintf (buffer, "HOLLERITH");
180 break;
181 case BT_UNION:
182 sprintf (buffer, "UNION(%s)", ts->u.derived->name);
183 break;
184 case BT_DERIVED:
185 if (ts->u.derived == NULL)
186 {
187 sprintf (buffer, "invalid type");
188 break;
189 }
190 sprintf (buffer, "TYPE(%s)", ts->u.derived->name);
191 break;
192 case BT_CLASS:
193 if (!ts->u.derived || !ts->u.derived->components
194 || !ts->u.derived->components->ts.u.derived)
195 {
196 sprintf (buffer, "invalid class");
197 break;
198 }
199 if (ts->u.derived->components->ts.u.derived->attr.unlimited_polymorphic)
200 sprintf (buffer, "CLASS(*)");
201 else
202 sprintf (buffer, "CLASS(%s)",
203 ts->u.derived->components->ts.u.derived->name);
204 break;
205 case BT_ASSUMED:
206 sprintf (buffer, "TYPE(*)");
207 break;
208 case BT_PROCEDURE:
209 strcpy (buffer, "PROCEDURE");
210 break;
211 case BT_BOZ:
212 strcpy (buffer, "BOZ");
213 break;
214 case BT_UNKNOWN:
215 strcpy (buffer, "UNKNOWN");
216 break;
217 case BT_VOID:
218 strcpy (buffer, "VOID");
219 break;
220 default:
221 gfc_internal_error ("gfc_typename(): Undefined type");
222 }
223
224 return buffer;
225 }
226
227
228 const char *
229 gfc_typename (gfc_expr *ex)
230 {
231 /* 34 character buffer: 14 for "CHARACTER(n,4)", n can be upto 20 characters,
232 add 19 for the extra width and 1 for '\0' */
233 static char buffer1[34];
234 static char buffer2[34];
235 static bool flag = false;
236 char *buffer;
237 gfc_charlen_t length;
238 buffer = flag ? buffer1 : buffer2;
239 flag = !flag;
240
241 if (ex->ts.type == BT_CHARACTER)
242 {
243 if (ex->expr_type == EXPR_CONSTANT)
244 length = ex->value.character.length;
245 else if (ex->ts.deferred)
246 {
247 if (ex->ts.kind == gfc_default_character_kind)
248 return "CHARACTER(:)";
249 sprintf (buffer, "CHARACTER(:,%d)", ex->ts.kind);
250 return buffer;
251 }
252 else if (ex->ts.u.cl && ex->ts.u.cl->length == NULL)
253 {
254 if (ex->ts.kind == gfc_default_character_kind)
255 return "CHARACTER(*)";
256 sprintf (buffer, "CHARACTER(*,%d)", ex->ts.kind);
257 return buffer;
258 }
259 else if (ex->ts.u.cl == NULL
260 || ex->ts.u.cl->length->expr_type != EXPR_CONSTANT)
261 {
262 if (ex->ts.kind == gfc_default_character_kind)
263 return "CHARACTER";
264 sprintf (buffer, "CHARACTER(KIND=%d)", ex->ts.kind);
265 return buffer;
266 }
267 else
268 length = gfc_mpz_get_hwi (ex->ts.u.cl->length->value.integer);
269 if (ex->ts.kind == gfc_default_character_kind)
270 sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ")", length);
271 else
272 sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ",%d)", length,
273 ex->ts.kind);
274 return buffer;
275 }
276 return gfc_typename(&ex->ts);
277 }
278
279 /* The type of a dummy variable can also be CHARACTER(*). */
280
281 const char *
282 gfc_dummy_typename (gfc_typespec *ts)
283 {
284 static char buffer1[15]; /* 15 for "CHARACTER(*,4)" + '\0'. */
285 static char buffer2[15];
286 static bool flag = false;
287 char *buffer;
288
289 buffer = flag ? buffer1 : buffer2;
290 flag = !flag;
291
292 if (ts->type == BT_CHARACTER)
293 {
294 bool has_length = false;
295 if (ts->u.cl)
296 has_length = ts->u.cl->length != NULL;
297 if (!has_length)
298 {
299 if (ts->kind == gfc_default_character_kind)
300 sprintf(buffer, "CHARACTER(*)");
301 else if (ts->kind >= 0 && ts->kind < 10)
302 sprintf(buffer, "CHARACTER(*,%d)", ts->kind);
303 else
304 sprintf(buffer, "CHARACTER(*,?)");
305 return buffer;
306 }
307 }
308 return gfc_typename(ts);
309 }
310
311
312 /* Given an mstring array and a code, locate the code in the table,
313 returning a pointer to the string. */
314
315 const char *
316 gfc_code2string (const mstring *m, int code)
317 {
318 while (m->string != NULL)
319 {
320 if (m->tag == code)
321 return m->string;
322 m++;
323 }
324
325 gfc_internal_error ("gfc_code2string(): Bad code");
326 /* Not reached */
327 }
328
329
330 /* Given an mstring array and a string, returns the value of the tag
331 field. Returns the final tag if no matches to the string are found. */
332
333 int
334 gfc_string2code (const mstring *m, const char *string)
335 {
336 for (; m->string != NULL; m++)
337 if (strcmp (m->string, string) == 0)
338 return m->tag;
339
340 return m->tag;
341 }
342
343
344 /* Convert an intent code to a string. */
345 /* TODO: move to gfortran.h as define. */
346
347 const char *
348 gfc_intent_string (sym_intent i)
349 {
350 return gfc_code2string (intents, i);
351 }
352
353
354 /***************** Initialization functions ****************/
355
356 /* Top level initialization. */
357
358 void
359 gfc_init_1 (void)
360 {
361 gfc_error_init_1 ();
362 gfc_scanner_init_1 ();
363 gfc_arith_init_1 ();
364 gfc_intrinsic_init_1 ();
365 }
366
367
368 /* Per program unit initialization. */
369
370 void
371 gfc_init_2 (void)
372 {
373 gfc_symbol_init_2 ();
374 gfc_module_init_2 ();
375 }
376
377
378 /******************* Destructor functions ******************/
379
380 /* Call all of the top level destructors. */
381
382 void
383 gfc_done_1 (void)
384 {
385 gfc_scanner_done_1 ();
386 gfc_intrinsic_done_1 ();
387 gfc_arith_done_1 ();
388 }
389
390
391 /* Per program unit destructors. */
392
393 void
394 gfc_done_2 (void)
395 {
396 gfc_symbol_done_2 ();
397 gfc_module_done_2 ();
398 }
399
400
401 /* Returns the index into the table of C interoperable kinds where the
402 kind with the given name (c_kind_name) was found. */
403
404 int
405 get_c_kind(const char *c_kind_name, CInteropKind_t kinds_table[])
406 {
407 int index = 0;
408
409 for (index = 0; index < ISOCBINDING_LAST; index++)
410 if (strcmp (kinds_table[index].name, c_kind_name) == 0)
411 return index;
412
413 return ISOCBINDING_INVALID;
414 }
415
416
417 /* For a given name TYPO, determine the best candidate from CANDIDATES
418 using get_edit_distance. Frees CANDIDATES before returning. */
419
420 const char *
421 gfc_closest_fuzzy_match (const char *typo, char **candidates)
422 {
423 /* Determine closest match. */
424 const char *best = NULL;
425 char **cand = candidates;
426 edit_distance_t best_distance = MAX_EDIT_DISTANCE;
427 const size_t tl = strlen (typo);
428
429 while (cand && *cand)
430 {
431 edit_distance_t dist = get_edit_distance (typo, tl, *cand,
432 strlen (*cand));
433 if (dist < best_distance)
434 {
435 best_distance = dist;
436 best = *cand;
437 }
438 cand++;
439 }
440 /* If more than half of the letters were misspelled, the suggestion is
441 likely to be meaningless. */
442 if (best)
443 {
444 unsigned int cutoff = MAX (tl, strlen (best));
445
446 if (best_distance > cutoff)
447 {
448 XDELETEVEC (candidates);
449 return NULL;
450 }
451 XDELETEVEC (candidates);
452 }
453 return best;
454 }
455
456 /* Convert between GMP integers (mpz_t) and HOST_WIDE_INT. */
457
458 HOST_WIDE_INT
459 gfc_mpz_get_hwi (mpz_t op)
460 {
461 /* Using long_long_integer_type_node as that is the integer type
462 node that closest matches HOST_WIDE_INT; both are guaranteed to
463 be at least 64 bits. */
464 const wide_int w = wi::from_mpz (long_long_integer_type_node, op, true);
465 return w.to_shwi ();
466 }
467
468
469 void
470 gfc_mpz_set_hwi (mpz_t rop, const HOST_WIDE_INT op)
471 {
472 const wide_int w = wi::shwi (op, HOST_BITS_PER_WIDE_INT);
473 wi::to_mpz (w, rop, SIGNED);
474 }