* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / fortran / misc.c
blob4d94d7fab72e6afb5bd9e43cd4b5b85d99a7a626
1 /* Miscellaneous stuff that doesn't fit anywhere else.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "gfortran.h"
29 /* Get a block of memory. Many callers assume that the memory we
30 return is zeroed. */
32 void *
33 gfc_getmem (size_t n)
35 void *p;
37 if (n == 0)
38 return NULL;
40 p = xmalloc (n);
41 if (p == NULL)
42 gfc_fatal_error ("Out of memory-- malloc() failed");
43 memset (p, 0, n);
44 return p;
48 /* gfortran.h defines free to something that triggers a syntax error,
49 but we need free() here. */
51 #define temp free
52 #undef free
54 void
55 gfc_free (void *p)
58 if (p != NULL)
59 free (p);
62 #define free temp
63 #undef temp
66 /* Get terminal width */
68 int
69 gfc_terminal_width(void)
71 return 80;
75 /* Initialize a typespec to unknown. */
77 void
78 gfc_clear_ts (gfc_typespec * ts)
81 ts->type = BT_UNKNOWN;
82 ts->kind = 0;
83 ts->derived = NULL;
84 ts->cl = NULL;
88 /* Open a file for reading. */
90 FILE *
91 gfc_open_file (const char *name)
93 struct stat statbuf;
95 if (!*name)
96 return stdin;
98 if (stat (name, &statbuf) < 0)
99 return NULL;
101 if (!S_ISREG (statbuf.st_mode))
102 return NULL;
104 return fopen (name, "r");
108 /* Return a string for each type. */
110 const char *
111 gfc_basic_typename (bt type)
113 const char *p;
115 switch (type)
117 case BT_INTEGER:
118 p = "INTEGER";
119 break;
120 case BT_REAL:
121 p = "REAL";
122 break;
123 case BT_COMPLEX:
124 p = "COMPLEX";
125 break;
126 case BT_LOGICAL:
127 p = "LOGICAL";
128 break;
129 case BT_CHARACTER:
130 p = "CHARACTER";
131 break;
132 case BT_HOLLERITH:
133 p = "HOLLERITH";
134 break;
135 case BT_DERIVED:
136 p = "DERIVED";
137 break;
138 case BT_PROCEDURE:
139 p = "PROCEDURE";
140 break;
141 case BT_UNKNOWN:
142 p = "UNKNOWN";
143 break;
144 default:
145 gfc_internal_error ("gfc_basic_typename(): Undefined type");
148 return p;
152 /* Return a string describing the type and kind of a typespec. Because
153 we return alternating buffers, this subroutine can appear twice in
154 the argument list of a single statement. */
156 const char *
157 gfc_typename (gfc_typespec * ts)
159 static char buffer1[60], buffer2[60];
160 static int flag = 0;
161 char *buffer;
163 buffer = flag ? buffer1 : buffer2;
164 flag = !flag;
166 switch (ts->type)
168 case BT_INTEGER:
169 sprintf (buffer, "INTEGER(%d)", ts->kind);
170 break;
171 case BT_REAL:
172 sprintf (buffer, "REAL(%d)", ts->kind);
173 break;
174 case BT_COMPLEX:
175 sprintf (buffer, "COMPLEX(%d)", ts->kind);
176 break;
177 case BT_LOGICAL:
178 sprintf (buffer, "LOGICAL(%d)", ts->kind);
179 break;
180 case BT_CHARACTER:
181 sprintf (buffer, "CHARACTER(%d)", ts->kind);
182 break;
183 case BT_HOLLERITH:
184 sprintf (buffer, "HOLLERITH");
185 break;
186 case BT_DERIVED:
187 sprintf (buffer, "TYPE(%s)", ts->derived->name);
188 break;
189 case BT_PROCEDURE:
190 strcpy (buffer, "PROCEDURE");
191 break;
192 case BT_UNKNOWN:
193 strcpy (buffer, "UNKNOWN");
194 break;
195 default:
196 gfc_internal_error ("gfc_typespec(): Undefined type");
199 return buffer;
203 /* Given an mstring array and a code, locate the code in the table,
204 returning a pointer to the string. */
206 const char *
207 gfc_code2string (const mstring * m, int code)
210 while (m->string != NULL)
212 if (m->tag == code)
213 return m->string;
214 m++;
217 gfc_internal_error ("gfc_code2string(): Bad code");
218 /* Not reached */
222 /* Given an mstring array and a string, returns the value of the tag
223 field. Returns the final tag if no matches to the string are
224 found. */
227 gfc_string2code (const mstring * m, const char *string)
230 for (; m->string != NULL; m++)
231 if (strcmp (m->string, string) == 0)
232 return m->tag;
234 return m->tag;
238 /* Convert an intent code to a string. */
239 /* TODO: move to gfortran.h as define. */
240 const char *
241 gfc_intent_string (sym_intent i)
244 return gfc_code2string (intents, i);
248 /***************** Initialization functions ****************/
250 /* Top level initialization. */
252 void
253 gfc_init_1 (void)
255 gfc_error_init_1 ();
256 gfc_scanner_init_1 ();
257 gfc_arith_init_1 ();
258 gfc_intrinsic_init_1 ();
259 gfc_simplify_init_1 ();
263 /* Per program unit initialization. */
265 void
266 gfc_init_2 (void)
269 gfc_symbol_init_2 ();
270 gfc_module_init_2 ();
274 /******************* Destructor functions ******************/
276 /* Call all of the top level destructors. */
278 void
279 gfc_done_1 (void)
281 gfc_scanner_done_1 ();
282 gfc_intrinsic_done_1 ();
283 gfc_arith_done_1 ();
287 /* Per program unit destructors. */
289 void
290 gfc_done_2 (void)
293 gfc_symbol_done_2 ();
294 gfc_module_done_2 ();