xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/fortran/misc.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg /* Miscellaneous stuff that doesn't fit anywhere else.
2*4c3eb207Smrg    Copyright (C) 2000-2020 Free Software Foundation, Inc.
3627f7eb2Smrg    Contributed by Andy Vaught
4627f7eb2Smrg 
5627f7eb2Smrg This file is part of GCC.
6627f7eb2Smrg 
7627f7eb2Smrg GCC is free software; you can redistribute it and/or modify it under
8627f7eb2Smrg the terms of the GNU General Public License as published by the Free
9627f7eb2Smrg Software Foundation; either version 3, or (at your option) any later
10627f7eb2Smrg version.
11627f7eb2Smrg 
12627f7eb2Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13627f7eb2Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14627f7eb2Smrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15627f7eb2Smrg for more details.
16627f7eb2Smrg 
17627f7eb2Smrg You should have received a copy of the GNU General Public License
18627f7eb2Smrg along with GCC; see the file COPYING3.  If not see
19627f7eb2Smrg <http://www.gnu.org/licenses/>.  */
20627f7eb2Smrg 
21627f7eb2Smrg #include "config.h"
22627f7eb2Smrg #include "system.h"
23627f7eb2Smrg #include "coretypes.h"
24627f7eb2Smrg #include "gfortran.h"
25627f7eb2Smrg #include "spellcheck.h"
26627f7eb2Smrg #include "tree.h"
27627f7eb2Smrg 
28627f7eb2Smrg 
29627f7eb2Smrg /* Initialize a typespec to unknown.  */
30627f7eb2Smrg 
31627f7eb2Smrg void
gfc_clear_ts(gfc_typespec * ts)32627f7eb2Smrg gfc_clear_ts (gfc_typespec *ts)
33627f7eb2Smrg {
34627f7eb2Smrg   ts->type = BT_UNKNOWN;
35627f7eb2Smrg   ts->u.derived = NULL;
36627f7eb2Smrg   ts->kind = 0;
37627f7eb2Smrg   ts->u.cl = NULL;
38627f7eb2Smrg   ts->interface = NULL;
39627f7eb2Smrg   /* flag that says if the type is C interoperable */
40627f7eb2Smrg   ts->is_c_interop = 0;
41627f7eb2Smrg   /* says what f90 type the C kind interops with */
42627f7eb2Smrg   ts->f90_type = BT_UNKNOWN;
43627f7eb2Smrg   /* flag that says whether it's from iso_c_binding or not */
44627f7eb2Smrg   ts->is_iso_c = 0;
45627f7eb2Smrg   ts->deferred = false;
46627f7eb2Smrg }
47627f7eb2Smrg 
48627f7eb2Smrg 
49627f7eb2Smrg /* Open a file for reading.  */
50627f7eb2Smrg 
51627f7eb2Smrg FILE *
gfc_open_file(const char * name)52627f7eb2Smrg gfc_open_file (const char *name)
53627f7eb2Smrg {
54627f7eb2Smrg   if (!*name)
55627f7eb2Smrg     return stdin;
56627f7eb2Smrg 
57627f7eb2Smrg   return fopen (name, "r");
58627f7eb2Smrg }
59627f7eb2Smrg 
60627f7eb2Smrg 
61627f7eb2Smrg /* Return a string for each type.  */
62627f7eb2Smrg 
63627f7eb2Smrg const char *
gfc_basic_typename(bt type)64627f7eb2Smrg gfc_basic_typename (bt type)
65627f7eb2Smrg {
66627f7eb2Smrg   const char *p;
67627f7eb2Smrg 
68627f7eb2Smrg   switch (type)
69627f7eb2Smrg     {
70627f7eb2Smrg     case BT_INTEGER:
71627f7eb2Smrg       p = "INTEGER";
72627f7eb2Smrg       break;
73627f7eb2Smrg     case BT_REAL:
74627f7eb2Smrg       p = "REAL";
75627f7eb2Smrg       break;
76627f7eb2Smrg     case BT_COMPLEX:
77627f7eb2Smrg       p = "COMPLEX";
78627f7eb2Smrg       break;
79627f7eb2Smrg     case BT_LOGICAL:
80627f7eb2Smrg       p = "LOGICAL";
81627f7eb2Smrg       break;
82627f7eb2Smrg     case BT_CHARACTER:
83627f7eb2Smrg       p = "CHARACTER";
84627f7eb2Smrg       break;
85627f7eb2Smrg     case BT_HOLLERITH:
86627f7eb2Smrg       p = "HOLLERITH";
87627f7eb2Smrg       break;
88627f7eb2Smrg     case BT_UNION:
89627f7eb2Smrg       p = "UNION";
90627f7eb2Smrg       break;
91627f7eb2Smrg     case BT_DERIVED:
92627f7eb2Smrg       p = "DERIVED";
93627f7eb2Smrg       break;
94627f7eb2Smrg     case BT_CLASS:
95627f7eb2Smrg       p = "CLASS";
96627f7eb2Smrg       break;
97627f7eb2Smrg     case BT_PROCEDURE:
98627f7eb2Smrg       p = "PROCEDURE";
99627f7eb2Smrg       break;
100627f7eb2Smrg     case BT_VOID:
101627f7eb2Smrg       p = "VOID";
102627f7eb2Smrg       break;
103*4c3eb207Smrg     case BT_BOZ:
104*4c3eb207Smrg       p = "BOZ";
105*4c3eb207Smrg       break;
106627f7eb2Smrg     case BT_UNKNOWN:
107627f7eb2Smrg       p = "UNKNOWN";
108627f7eb2Smrg       break;
109627f7eb2Smrg     case BT_ASSUMED:
110627f7eb2Smrg       p = "TYPE(*)";
111627f7eb2Smrg       break;
112627f7eb2Smrg     default:
113627f7eb2Smrg       gfc_internal_error ("gfc_basic_typename(): Undefined type");
114627f7eb2Smrg     }
115627f7eb2Smrg 
116627f7eb2Smrg   return p;
117627f7eb2Smrg }
118627f7eb2Smrg 
119627f7eb2Smrg 
120627f7eb2Smrg /* Return a string describing the type and kind of a typespec.  Because
121627f7eb2Smrg    we return alternating buffers, this subroutine can appear twice in
122627f7eb2Smrg    the argument list of a single statement.  */
123627f7eb2Smrg 
124627f7eb2Smrg const char *
gfc_typename(gfc_typespec * ts,bool for_hash)125*4c3eb207Smrg gfc_typename (gfc_typespec *ts, bool for_hash)
126627f7eb2Smrg {
127627f7eb2Smrg   static char buffer1[GFC_MAX_SYMBOL_LEN + 7];  /* 7 for "TYPE()" + '\0'.  */
128627f7eb2Smrg   static char buffer2[GFC_MAX_SYMBOL_LEN + 7];
129627f7eb2Smrg   static int flag = 0;
130627f7eb2Smrg   char *buffer;
131627f7eb2Smrg   gfc_typespec *ts1;
132*4c3eb207Smrg   gfc_charlen_t length = 0;
133627f7eb2Smrg 
134627f7eb2Smrg   buffer = flag ? buffer1 : buffer2;
135627f7eb2Smrg   flag = !flag;
136627f7eb2Smrg 
137627f7eb2Smrg   switch (ts->type)
138627f7eb2Smrg     {
139627f7eb2Smrg     case BT_INTEGER:
140627f7eb2Smrg       sprintf (buffer, "INTEGER(%d)", ts->kind);
141627f7eb2Smrg       break;
142627f7eb2Smrg     case BT_REAL:
143627f7eb2Smrg       sprintf (buffer, "REAL(%d)", ts->kind);
144627f7eb2Smrg       break;
145627f7eb2Smrg     case BT_COMPLEX:
146627f7eb2Smrg       sprintf (buffer, "COMPLEX(%d)", ts->kind);
147627f7eb2Smrg       break;
148627f7eb2Smrg     case BT_LOGICAL:
149627f7eb2Smrg       sprintf (buffer, "LOGICAL(%d)", ts->kind);
150627f7eb2Smrg       break;
151627f7eb2Smrg     case BT_CHARACTER:
152*4c3eb207Smrg       if (for_hash)
153*4c3eb207Smrg 	{
154627f7eb2Smrg 	  sprintf (buffer, "CHARACTER(%d)", ts->kind);
155627f7eb2Smrg 	  break;
156*4c3eb207Smrg 	}
157*4c3eb207Smrg 
158*4c3eb207Smrg       if (ts->u.cl && ts->u.cl->length)
159*4c3eb207Smrg 	length = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
160*4c3eb207Smrg       if (ts->kind == gfc_default_character_kind)
161*4c3eb207Smrg 	sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ")", length);
162*4c3eb207Smrg       else
163*4c3eb207Smrg 	sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ",%d)", length,
164*4c3eb207Smrg 		 ts->kind);
165*4c3eb207Smrg       break;
166627f7eb2Smrg     case BT_HOLLERITH:
167627f7eb2Smrg       sprintf (buffer, "HOLLERITH");
168627f7eb2Smrg       break;
169627f7eb2Smrg     case BT_UNION:
170627f7eb2Smrg       sprintf (buffer, "UNION(%s)", ts->u.derived->name);
171627f7eb2Smrg       break;
172627f7eb2Smrg     case BT_DERIVED:
173*4c3eb207Smrg       if (ts->u.derived == NULL)
174*4c3eb207Smrg 	{
175*4c3eb207Smrg 	  sprintf (buffer, "invalid type");
176*4c3eb207Smrg 	  break;
177*4c3eb207Smrg 	}
178627f7eb2Smrg       sprintf (buffer, "TYPE(%s)", ts->u.derived->name);
179627f7eb2Smrg       break;
180627f7eb2Smrg     case BT_CLASS:
181*4c3eb207Smrg       if (ts->u.derived == NULL)
182*4c3eb207Smrg 	{
183*4c3eb207Smrg 	  sprintf (buffer, "invalid class");
184*4c3eb207Smrg 	  break;
185*4c3eb207Smrg 	}
186627f7eb2Smrg       ts1 = ts->u.derived->components ? &ts->u.derived->components->ts : NULL;
187627f7eb2Smrg       if (ts1 && ts1->u.derived && ts1->u.derived->attr.unlimited_polymorphic)
188627f7eb2Smrg 	sprintf (buffer, "CLASS(*)");
189627f7eb2Smrg       else
190627f7eb2Smrg 	sprintf (buffer, "CLASS(%s)", ts->u.derived->name);
191627f7eb2Smrg       break;
192627f7eb2Smrg     case BT_ASSUMED:
193627f7eb2Smrg       sprintf (buffer, "TYPE(*)");
194627f7eb2Smrg       break;
195627f7eb2Smrg     case BT_PROCEDURE:
196627f7eb2Smrg       strcpy (buffer, "PROCEDURE");
197627f7eb2Smrg       break;
198*4c3eb207Smrg     case BT_BOZ:
199*4c3eb207Smrg       strcpy (buffer, "BOZ");
200*4c3eb207Smrg       break;
201627f7eb2Smrg     case BT_UNKNOWN:
202627f7eb2Smrg       strcpy (buffer, "UNKNOWN");
203627f7eb2Smrg       break;
204627f7eb2Smrg     default:
205627f7eb2Smrg       gfc_internal_error ("gfc_typename(): Undefined type");
206627f7eb2Smrg     }
207627f7eb2Smrg 
208627f7eb2Smrg   return buffer;
209627f7eb2Smrg }
210627f7eb2Smrg 
211627f7eb2Smrg 
212*4c3eb207Smrg const char *
gfc_typename(gfc_expr * ex)213*4c3eb207Smrg gfc_typename (gfc_expr *ex)
214*4c3eb207Smrg {
215*4c3eb207Smrg   /* 34 character buffer: 14 for "CHARACTER(n,4)", n can be upto 20 characters,
216*4c3eb207Smrg      add 19 for the extra width and 1 for '\0' */
217*4c3eb207Smrg   static char buffer1[34];
218*4c3eb207Smrg   static char buffer2[34];
219*4c3eb207Smrg   static bool flag = false;
220*4c3eb207Smrg   char *buffer;
221*4c3eb207Smrg   gfc_charlen_t length;
222*4c3eb207Smrg   buffer = flag ? buffer1 : buffer2;
223*4c3eb207Smrg   flag = !flag;
224*4c3eb207Smrg 
225*4c3eb207Smrg   if (ex->ts.type == BT_CHARACTER)
226*4c3eb207Smrg     {
227*4c3eb207Smrg       if (ex->expr_type == EXPR_CONSTANT)
228*4c3eb207Smrg 	length = ex->value.character.length;
229*4c3eb207Smrg       else if (ex->ts.deferred)
230*4c3eb207Smrg 	{
231*4c3eb207Smrg 	  if (ex->ts.kind == gfc_default_character_kind)
232*4c3eb207Smrg 	    return "CHARACTER(:)";
233*4c3eb207Smrg 	  sprintf (buffer, "CHARACTER(:,%d)", ex->ts.kind);
234*4c3eb207Smrg 	  return buffer;
235*4c3eb207Smrg 	}
236*4c3eb207Smrg       else if (ex->ts.u.cl && ex->ts.u.cl->length == NULL)
237*4c3eb207Smrg 	{
238*4c3eb207Smrg 	  if (ex->ts.kind == gfc_default_character_kind)
239*4c3eb207Smrg 	    return "CHARACTER(*)";
240*4c3eb207Smrg 	  sprintf (buffer, "CHARACTER(*,%d)", ex->ts.kind);
241*4c3eb207Smrg 	  return buffer;
242*4c3eb207Smrg 	}
243*4c3eb207Smrg       else if (ex->ts.u.cl == NULL
244*4c3eb207Smrg 	       || ex->ts.u.cl->length->expr_type != EXPR_CONSTANT)
245*4c3eb207Smrg 	{
246*4c3eb207Smrg 	  if (ex->ts.kind == gfc_default_character_kind)
247*4c3eb207Smrg 	    return "CHARACTER";
248*4c3eb207Smrg 	  sprintf (buffer, "CHARACTER(KIND=%d)", ex->ts.kind);
249*4c3eb207Smrg 	  return buffer;
250*4c3eb207Smrg 	}
251*4c3eb207Smrg       else
252*4c3eb207Smrg 	length = gfc_mpz_get_hwi (ex->ts.u.cl->length->value.integer);
253*4c3eb207Smrg       if (ex->ts.kind == gfc_default_character_kind)
254*4c3eb207Smrg 	sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ")", length);
255*4c3eb207Smrg       else
256*4c3eb207Smrg 	sprintf (buffer, "CHARACTER(" HOST_WIDE_INT_PRINT_DEC ",%d)", length,
257*4c3eb207Smrg 		 ex->ts.kind);
258*4c3eb207Smrg       return buffer;
259*4c3eb207Smrg     }
260*4c3eb207Smrg   return gfc_typename(&ex->ts);
261*4c3eb207Smrg }
262*4c3eb207Smrg 
263*4c3eb207Smrg /* The type of a dummy variable can also be CHARACTER(*).  */
264*4c3eb207Smrg 
265*4c3eb207Smrg const char *
gfc_dummy_typename(gfc_typespec * ts)266*4c3eb207Smrg gfc_dummy_typename (gfc_typespec *ts)
267*4c3eb207Smrg {
268*4c3eb207Smrg   static char buffer1[15];  /* 15 for "CHARACTER(*,4)" + '\0'.  */
269*4c3eb207Smrg   static char buffer2[15];
270*4c3eb207Smrg   static bool flag = false;
271*4c3eb207Smrg   char *buffer;
272*4c3eb207Smrg 
273*4c3eb207Smrg   buffer = flag ? buffer1 : buffer2;
274*4c3eb207Smrg   flag = !flag;
275*4c3eb207Smrg 
276*4c3eb207Smrg   if (ts->type == BT_CHARACTER)
277*4c3eb207Smrg     {
278*4c3eb207Smrg       bool has_length = false;
279*4c3eb207Smrg       if (ts->u.cl)
280*4c3eb207Smrg 	has_length = ts->u.cl->length != NULL;
281*4c3eb207Smrg       if (!has_length)
282*4c3eb207Smrg 	{
283*4c3eb207Smrg 	  if (ts->kind == gfc_default_character_kind)
284*4c3eb207Smrg 	    sprintf(buffer, "CHARACTER(*)");
285*4c3eb207Smrg 	  else if (ts->kind < 10)
286*4c3eb207Smrg 	    sprintf(buffer, "CHARACTER(*,%d)", ts->kind);
287*4c3eb207Smrg 	  else
288*4c3eb207Smrg 	    sprintf(buffer, "CHARACTER(*,?)");
289*4c3eb207Smrg 	  return buffer;
290*4c3eb207Smrg 	}
291*4c3eb207Smrg     }
292*4c3eb207Smrg   return gfc_typename(ts);
293*4c3eb207Smrg }
294*4c3eb207Smrg 
295*4c3eb207Smrg 
296627f7eb2Smrg /* Given an mstring array and a code, locate the code in the table,
297627f7eb2Smrg    returning a pointer to the string.  */
298627f7eb2Smrg 
299627f7eb2Smrg const char *
gfc_code2string(const mstring * m,int code)300627f7eb2Smrg gfc_code2string (const mstring *m, int code)
301627f7eb2Smrg {
302627f7eb2Smrg   while (m->string != NULL)
303627f7eb2Smrg     {
304627f7eb2Smrg       if (m->tag == code)
305627f7eb2Smrg 	return m->string;
306627f7eb2Smrg       m++;
307627f7eb2Smrg     }
308627f7eb2Smrg 
309627f7eb2Smrg   gfc_internal_error ("gfc_code2string(): Bad code");
310627f7eb2Smrg   /* Not reached */
311627f7eb2Smrg }
312627f7eb2Smrg 
313627f7eb2Smrg 
314627f7eb2Smrg /* Given an mstring array and a string, returns the value of the tag
315627f7eb2Smrg    field.  Returns the final tag if no matches to the string are found.  */
316627f7eb2Smrg 
317627f7eb2Smrg int
gfc_string2code(const mstring * m,const char * string)318627f7eb2Smrg gfc_string2code (const mstring *m, const char *string)
319627f7eb2Smrg {
320627f7eb2Smrg   for (; m->string != NULL; m++)
321627f7eb2Smrg     if (strcmp (m->string, string) == 0)
322627f7eb2Smrg       return m->tag;
323627f7eb2Smrg 
324627f7eb2Smrg   return m->tag;
325627f7eb2Smrg }
326627f7eb2Smrg 
327627f7eb2Smrg 
328627f7eb2Smrg /* Convert an intent code to a string.  */
329627f7eb2Smrg /* TODO: move to gfortran.h as define.  */
330627f7eb2Smrg 
331627f7eb2Smrg const char *
gfc_intent_string(sym_intent i)332627f7eb2Smrg gfc_intent_string (sym_intent i)
333627f7eb2Smrg {
334627f7eb2Smrg   return gfc_code2string (intents, i);
335627f7eb2Smrg }
336627f7eb2Smrg 
337627f7eb2Smrg 
338627f7eb2Smrg /***************** Initialization functions ****************/
339627f7eb2Smrg 
340627f7eb2Smrg /* Top level initialization.  */
341627f7eb2Smrg 
342627f7eb2Smrg void
gfc_init_1(void)343627f7eb2Smrg gfc_init_1 (void)
344627f7eb2Smrg {
345627f7eb2Smrg   gfc_error_init_1 ();
346627f7eb2Smrg   gfc_scanner_init_1 ();
347627f7eb2Smrg   gfc_arith_init_1 ();
348627f7eb2Smrg   gfc_intrinsic_init_1 ();
349627f7eb2Smrg }
350627f7eb2Smrg 
351627f7eb2Smrg 
352627f7eb2Smrg /* Per program unit initialization.  */
353627f7eb2Smrg 
354627f7eb2Smrg void
gfc_init_2(void)355627f7eb2Smrg gfc_init_2 (void)
356627f7eb2Smrg {
357627f7eb2Smrg   gfc_symbol_init_2 ();
358627f7eb2Smrg   gfc_module_init_2 ();
359627f7eb2Smrg }
360627f7eb2Smrg 
361627f7eb2Smrg 
362627f7eb2Smrg /******************* Destructor functions ******************/
363627f7eb2Smrg 
364627f7eb2Smrg /* Call all of the top level destructors.  */
365627f7eb2Smrg 
366627f7eb2Smrg void
gfc_done_1(void)367627f7eb2Smrg gfc_done_1 (void)
368627f7eb2Smrg {
369627f7eb2Smrg   gfc_scanner_done_1 ();
370627f7eb2Smrg   gfc_intrinsic_done_1 ();
371627f7eb2Smrg   gfc_arith_done_1 ();
372627f7eb2Smrg }
373627f7eb2Smrg 
374627f7eb2Smrg 
375627f7eb2Smrg /* Per program unit destructors.  */
376627f7eb2Smrg 
377627f7eb2Smrg void
gfc_done_2(void)378627f7eb2Smrg gfc_done_2 (void)
379627f7eb2Smrg {
380627f7eb2Smrg   gfc_symbol_done_2 ();
381627f7eb2Smrg   gfc_module_done_2 ();
382627f7eb2Smrg }
383627f7eb2Smrg 
384627f7eb2Smrg 
385627f7eb2Smrg /* Returns the index into the table of C interoperable kinds where the
386627f7eb2Smrg    kind with the given name (c_kind_name) was found.  */
387627f7eb2Smrg 
388627f7eb2Smrg int
get_c_kind(const char * c_kind_name,CInteropKind_t kinds_table[])389627f7eb2Smrg get_c_kind(const char *c_kind_name, CInteropKind_t kinds_table[])
390627f7eb2Smrg {
391627f7eb2Smrg   int index = 0;
392627f7eb2Smrg 
393627f7eb2Smrg   for (index = 0; index < ISOCBINDING_LAST; index++)
394627f7eb2Smrg     if (strcmp (kinds_table[index].name, c_kind_name) == 0)
395627f7eb2Smrg       return index;
396627f7eb2Smrg 
397627f7eb2Smrg   return ISOCBINDING_INVALID;
398627f7eb2Smrg }
399627f7eb2Smrg 
400627f7eb2Smrg 
401627f7eb2Smrg /* For a given name TYPO, determine the best candidate from CANDIDATES
402627f7eb2Smrg    using get_edit_distance.  Frees CANDIDATES before returning.  */
403627f7eb2Smrg 
404627f7eb2Smrg const char *
gfc_closest_fuzzy_match(const char * typo,char ** candidates)405627f7eb2Smrg gfc_closest_fuzzy_match (const char *typo, char **candidates)
406627f7eb2Smrg {
407627f7eb2Smrg   /* Determine closest match.  */
408627f7eb2Smrg   const char *best = NULL;
409627f7eb2Smrg   char **cand = candidates;
410627f7eb2Smrg   edit_distance_t best_distance = MAX_EDIT_DISTANCE;
411627f7eb2Smrg   const size_t tl = strlen (typo);
412627f7eb2Smrg 
413627f7eb2Smrg   while (cand && *cand)
414627f7eb2Smrg     {
415627f7eb2Smrg       edit_distance_t dist = get_edit_distance (typo, tl, *cand,
416627f7eb2Smrg 	  strlen (*cand));
417627f7eb2Smrg       if (dist < best_distance)
418627f7eb2Smrg 	{
419627f7eb2Smrg 	   best_distance = dist;
420627f7eb2Smrg 	   best = *cand;
421627f7eb2Smrg 	}
422627f7eb2Smrg       cand++;
423627f7eb2Smrg     }
424627f7eb2Smrg   /* If more than half of the letters were misspelled, the suggestion is
425627f7eb2Smrg      likely to be meaningless.  */
426627f7eb2Smrg   if (best)
427627f7eb2Smrg     {
428627f7eb2Smrg       unsigned int cutoff = MAX (tl, strlen (best)) / 2;
429627f7eb2Smrg 
430627f7eb2Smrg       if (best_distance > cutoff)
431627f7eb2Smrg 	{
432627f7eb2Smrg 	  XDELETEVEC (candidates);
433627f7eb2Smrg 	  return NULL;
434627f7eb2Smrg 	}
435627f7eb2Smrg       XDELETEVEC (candidates);
436627f7eb2Smrg     }
437627f7eb2Smrg   return best;
438627f7eb2Smrg }
439627f7eb2Smrg 
440627f7eb2Smrg /* Convert between GMP integers (mpz_t) and HOST_WIDE_INT.  */
441627f7eb2Smrg 
442627f7eb2Smrg HOST_WIDE_INT
gfc_mpz_get_hwi(mpz_t op)443627f7eb2Smrg gfc_mpz_get_hwi (mpz_t op)
444627f7eb2Smrg {
445627f7eb2Smrg   /* Using long_long_integer_type_node as that is the integer type
446627f7eb2Smrg      node that closest matches HOST_WIDE_INT; both are guaranteed to
447627f7eb2Smrg      be at least 64 bits.  */
448627f7eb2Smrg   const wide_int w = wi::from_mpz (long_long_integer_type_node, op, true);
449627f7eb2Smrg   return w.to_shwi ();
450627f7eb2Smrg }
451627f7eb2Smrg 
452627f7eb2Smrg 
453627f7eb2Smrg void
gfc_mpz_set_hwi(mpz_t rop,const HOST_WIDE_INT op)454627f7eb2Smrg gfc_mpz_set_hwi (mpz_t rop, const HOST_WIDE_INT op)
455627f7eb2Smrg {
456627f7eb2Smrg   const wide_int w = wi::shwi (op, HOST_BITS_PER_WIDE_INT);
457627f7eb2Smrg   wi::to_mpz (w, rop, SIGNED);
458627f7eb2Smrg }
459