* Merge with edge-vector-mergepoint-20040918.
[official-gcc.git] / gcc / fortran / misc.c
blob431284ca7d4d26bc47eb4beb4f6a893164d0db0c
1 /* Miscellaneous stuff that doesn't fit anywhere else.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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 2, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
28 #include "gfortran.h"
31 /* Get a block of memory. Many callers assume that the memory we
32 return is zeroed. */
34 void *
35 gfc_getmem (size_t n)
37 void *p;
39 if (n == 0)
40 return NULL;
42 p = xmalloc (n);
43 if (p == NULL)
44 gfc_fatal_error ("Out of memory-- malloc() failed");
45 memset (p, 0, n);
46 return p;
50 /* gfortran.h defines free to something that triggers a syntax error,
51 but we need free() here. */
53 #define temp free
54 #undef free
56 void
57 gfc_free (void *p)
60 if (p != NULL)
61 free (p);
64 #define free temp
65 #undef temp
68 /* Get terminal width */
70 int
71 gfc_terminal_width(void)
73 return 80;
77 /* Initialize a typespec to unknown. */
79 void
80 gfc_clear_ts (gfc_typespec * ts)
83 ts->type = BT_UNKNOWN;
84 ts->kind = 0;
85 ts->derived = NULL;
86 ts->cl = NULL;
90 /* Open a file for reading. */
92 FILE *
93 gfc_open_file (const char *name)
95 struct stat statbuf;
97 if (!*name)
98 return stdin;
100 if (stat (name, &statbuf) < 0)
101 return NULL;
103 if (!S_ISREG (statbuf.st_mode))
104 return NULL;
106 return fopen (name, "r");
110 /* Given a word, return the correct article. */
112 const char *
113 gfc_article (const char *word)
115 const char *p;
117 switch (*word)
119 case 'a':
120 case 'A':
121 case 'e':
122 case 'E':
123 case 'i':
124 case 'I':
125 case 'o':
126 case 'O':
127 case 'u':
128 case 'U':
129 p = "an";
130 break;
132 default:
133 p = "a";
136 return p;
140 /* Return a string for each type. */
142 const char *
143 gfc_basic_typename (bt type)
145 const char *p;
147 switch (type)
149 case BT_INTEGER:
150 p = "INTEGER";
151 break;
152 case BT_REAL:
153 p = "REAL";
154 break;
155 case BT_COMPLEX:
156 p = "COMPLEX";
157 break;
158 case BT_LOGICAL:
159 p = "LOGICAL";
160 break;
161 case BT_CHARACTER:
162 p = "CHARACTER";
163 break;
164 case BT_DERIVED:
165 p = "DERIVED";
166 break;
167 case BT_PROCEDURE:
168 p = "PROCEDURE";
169 break;
170 case BT_UNKNOWN:
171 p = "UNKNOWN";
172 break;
173 default:
174 gfc_internal_error ("gfc_basic_typename(): Undefined type");
177 return p;
181 /* Return a string describing the type and kind of a typespec. Because
182 we return alternating buffers, this subroutine can appear twice in
183 the argument list of a single statement. */
185 const char *
186 gfc_typename (gfc_typespec * ts)
188 static char buffer1[60], buffer2[60];
189 static int flag = 0;
190 char *buffer;
192 buffer = flag ? buffer1 : buffer2;
193 flag = !flag;
195 switch (ts->type)
197 case BT_INTEGER:
198 sprintf (buffer, "INTEGER(%d)", ts->kind);
199 break;
200 case BT_REAL:
201 sprintf (buffer, "REAL(%d)", ts->kind);
202 break;
203 case BT_COMPLEX:
204 sprintf (buffer, "COMPLEX(%d)", ts->kind);
205 break;
206 case BT_LOGICAL:
207 sprintf (buffer, "LOGICAL(%d)", ts->kind);
208 break;
209 case BT_CHARACTER:
210 sprintf (buffer, "CHARACTER(%d)", ts->kind);
211 break;
212 case BT_DERIVED:
213 sprintf (buffer, "TYPE(%s)", ts->derived->name);
214 break;
215 case BT_PROCEDURE:
216 strcpy (buffer, "PROCEDURE");
217 break;
218 case BT_UNKNOWN:
219 strcpy (buffer, "UNKNOWN");
220 break;
221 default:
222 gfc_internal_error ("gfc_typespec(): Undefined type");
225 return buffer;
229 /* Given an mstring array and a code, locate the code in the table,
230 returning a pointer to the string. */
232 const char *
233 gfc_code2string (const mstring * m, int code)
236 while (m->string != NULL)
238 if (m->tag == code)
239 return m->string;
240 m++;
243 gfc_internal_error ("gfc_code2string(): Bad code");
244 /* Not reached */
248 /* Given an mstring array and a string, returns the value of the tag
249 field. Returns the final tag if no matches to the string are
250 found. */
253 gfc_string2code (const mstring * m, const char *string)
256 for (; m->string != NULL; m++)
257 if (strcmp (m->string, string) == 0)
258 return m->tag;
260 return m->tag;
264 /* Convert an intent code to a string. */
265 /* TODO: move to gfortran.h as define. */
266 const char *
267 gfc_intent_string (sym_intent i)
270 return gfc_code2string (intents, i);
274 /***************** Initialization functions ****************/
276 /* Top level initialization. */
278 void
279 gfc_init_1 (void)
282 gfc_error_init_1 ();
283 gfc_scanner_init_1 ();
284 gfc_arith_init_1 ();
285 gfc_intrinsic_init_1 ();
286 gfc_iresolve_init_1 ();
287 gfc_simplify_init_1 ();
291 /* Per program unit initialization. */
293 void
294 gfc_init_2 (void)
297 gfc_symbol_init_2 ();
298 gfc_module_init_2 ();
302 /******************* Destructor functions ******************/
304 /* Call all of the top level destructors. */
306 void
307 gfc_done_1 (void)
310 gfc_scanner_done_1 ();
311 gfc_intrinsic_done_1 ();
312 gfc_iresolve_done_1 ();
313 gfc_arith_done_1 ();
317 /* Per program unit destructors. */
319 void
320 gfc_done_2 (void)
323 gfc_symbol_done_2 ();
324 gfc_module_done_2 ();