* function.c (expand_function_end): If current_function_calls_alloca,
[official-gcc.git] / gcc / fortran / misc.c
blob13461b5befcecedacd9a2c70cdd786fce3bcfe25
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 /* Given a word, return the correct article. */
110 const char *
111 gfc_article (const char *word)
113 const char *p;
115 switch (*word)
117 case 'a':
118 case 'A':
119 case 'e':
120 case 'E':
121 case 'i':
122 case 'I':
123 case 'o':
124 case 'O':
125 case 'u':
126 case 'U':
127 p = "an";
128 break;
130 default:
131 p = "a";
134 return p;
138 /* Return a string for each type. */
140 const char *
141 gfc_basic_typename (bt type)
143 const char *p;
145 switch (type)
147 case BT_INTEGER:
148 p = "INTEGER";
149 break;
150 case BT_REAL:
151 p = "REAL";
152 break;
153 case BT_COMPLEX:
154 p = "COMPLEX";
155 break;
156 case BT_LOGICAL:
157 p = "LOGICAL";
158 break;
159 case BT_CHARACTER:
160 p = "CHARACTER";
161 break;
162 case BT_DERIVED:
163 p = "DERIVED";
164 break;
165 case BT_PROCEDURE:
166 p = "PROCEDURE";
167 break;
168 case BT_UNKNOWN:
169 p = "UNKNOWN";
170 break;
171 default:
172 gfc_internal_error ("gfc_basic_typename(): Undefined type");
175 return p;
179 /* Return a string describing the type and kind of a typespec. Because
180 we return alternating buffers, this subroutine can appear twice in
181 the argument list of a single statement. */
183 const char *
184 gfc_typename (gfc_typespec * ts)
186 static char buffer1[60], buffer2[60];
187 static int flag = 0;
188 char *buffer;
190 buffer = flag ? buffer1 : buffer2;
191 flag = !flag;
193 switch (ts->type)
195 case BT_INTEGER:
196 sprintf (buffer, "INTEGER(%d)", ts->kind);
197 break;
198 case BT_REAL:
199 sprintf (buffer, "REAL(%d)", ts->kind);
200 break;
201 case BT_COMPLEX:
202 sprintf (buffer, "COMPLEX(%d)", ts->kind);
203 break;
204 case BT_LOGICAL:
205 sprintf (buffer, "LOGICAL(%d)", ts->kind);
206 break;
207 case BT_CHARACTER:
208 sprintf (buffer, "CHARACTER(%d)", ts->kind);
209 break;
210 case BT_DERIVED:
211 sprintf (buffer, "TYPE(%s)", ts->derived->name);
212 break;
213 case BT_PROCEDURE:
214 strcpy (buffer, "PROCEDURE");
215 break;
216 case BT_UNKNOWN:
217 strcpy (buffer, "UNKNOWN");
218 break;
219 default:
220 gfc_internal_error ("gfc_typespec(): Undefined type");
223 return buffer;
227 /* Given an mstring array and a code, locate the code in the table,
228 returning a pointer to the string. */
230 const char *
231 gfc_code2string (const mstring * m, int code)
234 while (m->string != NULL)
236 if (m->tag == code)
237 return m->string;
238 m++;
241 gfc_internal_error ("gfc_code2string(): Bad code");
242 /* Not reached */
246 /* Given an mstring array and a string, returns the value of the tag
247 field. Returns the final tag if no matches to the string are
248 found. */
251 gfc_string2code (const mstring * m, const char *string)
254 for (; m->string != NULL; m++)
255 if (strcmp (m->string, string) == 0)
256 return m->tag;
258 return m->tag;
262 /* Convert an intent code to a string. */
263 /* TODO: move to gfortran.h as define. */
264 const char *
265 gfc_intent_string (sym_intent i)
268 return gfc_code2string (intents, i);
272 /***************** Initialization functions ****************/
274 /* Top level initialization. */
276 void
277 gfc_init_1 (void)
279 gfc_error_init_1 ();
280 gfc_scanner_init_1 ();
281 gfc_arith_init_1 ();
282 gfc_intrinsic_init_1 ();
283 gfc_simplify_init_1 ();
287 /* Per program unit initialization. */
289 void
290 gfc_init_2 (void)
293 gfc_symbol_init_2 ();
294 gfc_module_init_2 ();
298 /******************* Destructor functions ******************/
300 /* Call all of the top level destructors. */
302 void
303 gfc_done_1 (void)
305 gfc_scanner_done_1 ();
306 gfc_intrinsic_done_1 ();
307 gfc_arith_done_1 ();
311 /* Per program unit destructors. */
313 void
314 gfc_done_2 (void)
317 gfc_symbol_done_2 ();
318 gfc_module_done_2 ();