]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Backend support for Fortran 95 basic types and derived types. |
8b791297 | 2 | Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
835aac92 | 3 | Free Software Foundation, Inc. |
6de9cd9a DN |
4 | Contributed by Paul Brook <paul@nowt.org> |
5 | and Steven Bosscher <s.bosscher@student.tudelft.nl> | |
6 | ||
9fc4d79b | 7 | This file is part of GCC. |
6de9cd9a | 8 | |
9fc4d79b TS |
9 | GCC is free software; you can redistribute it and/or modify it under |
10 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 11 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 12 | version. |
6de9cd9a | 13 | |
9fc4d79b TS |
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
6de9cd9a DN |
18 | |
19 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
22 | |
23 | /* trans-types.c -- gfortran backend types */ | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "coretypes.h" | |
28 | #include "tree.h" | |
a8b3b0b6 | 29 | #include "langhooks.h" |
5e8e542f RH |
30 | #include "tm.h" |
31 | #include "target.h" | |
6de9cd9a DN |
32 | #include "ggc.h" |
33 | #include "toplev.h" | |
6de9cd9a DN |
34 | #include "gfortran.h" |
35 | #include "trans.h" | |
36 | #include "trans-types.h" | |
37 | #include "trans-const.h" | |
5e8e542f | 38 | #include "real.h" |
08789087 | 39 | #include "flags.h" |
fad0afd7 | 40 | #include "dwarf2out.h" |
6de9cd9a DN |
41 | \f |
42 | ||
43 | #if (GFC_MAX_DIMENSIONS < 10) | |
44 | #define GFC_RANK_DIGITS 1 | |
45 | #define GFC_RANK_PRINTF_FORMAT "%01d" | |
46 | #elif (GFC_MAX_DIMENSIONS < 100) | |
47 | #define GFC_RANK_DIGITS 2 | |
48 | #define GFC_RANK_PRINTF_FORMAT "%02d" | |
49 | #else | |
50 | #error If you really need >99 dimensions, continue the sequence above... | |
51 | #endif | |
52 | ||
a8b3b0b6 CR |
53 | /* array of structs so we don't have to worry about xmalloc or free */ |
54 | CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER]; | |
55 | ||
6de9cd9a DN |
56 | static tree gfc_get_derived_type (gfc_symbol * derived); |
57 | ||
6de9cd9a | 58 | tree gfc_array_index_type; |
b4838d29 | 59 | tree gfc_array_range_type; |
7e6de2a5 | 60 | tree gfc_character1_type_node; |
6de9cd9a DN |
61 | tree pvoid_type_node; |
62 | tree ppvoid_type_node; | |
63 | tree pchar_type_node; | |
089db47d | 64 | tree pfunc_type_node; |
7e6de2a5 | 65 | |
d7177ab2 | 66 | tree gfc_charlen_type_node; |
6de9cd9a | 67 | |
e2cad04b | 68 | static GTY(()) tree gfc_desc_dim_type; |
6de9cd9a | 69 | static GTY(()) tree gfc_max_array_element_size; |
4c73896d | 70 | static GTY(()) tree gfc_array_descriptor_base[GFC_MAX_DIMENSIONS]; |
6de9cd9a | 71 | |
5e8e542f RH |
72 | /* Arrays for all integral and real kinds. We'll fill this in at runtime |
73 | after the target has a chance to process command-line options. */ | |
74 | ||
75 | #define MAX_INT_KINDS 5 | |
76 | gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1]; | |
77 | gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1]; | |
e2cad04b RH |
78 | static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1]; |
79 | static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1]; | |
5e8e542f | 80 | |
e8f35d4d | 81 | #define MAX_REAL_KINDS 5 |
5e8e542f | 82 | gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1]; |
e2cad04b RH |
83 | static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1]; |
84 | static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1]; | |
5e8e542f | 85 | |
374929b2 FXC |
86 | #define MAX_CHARACTER_KINDS 2 |
87 | gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1]; | |
88 | static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1]; | |
89 | static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1]; | |
90 | ||
66e4ab31 | 91 | |
5e8e542f RH |
92 | /* The integer kind to use for array indices. This will be set to the |
93 | proper value based on target information from the backend. */ | |
94 | ||
95 | int gfc_index_integer_kind; | |
96 | ||
97 | /* The default kinds of the various types. */ | |
98 | ||
9d64df18 | 99 | int gfc_default_integer_kind; |
f4e7375a | 100 | int gfc_max_integer_kind; |
9d64df18 TS |
101 | int gfc_default_real_kind; |
102 | int gfc_default_double_kind; | |
103 | int gfc_default_character_kind; | |
104 | int gfc_default_logical_kind; | |
105 | int gfc_default_complex_kind; | |
e8525382 | 106 | int gfc_c_int_kind; |
5e8e542f | 107 | |
4fec64b0 JD |
108 | /* The kind size used for record offsets. If the target system supports |
109 | kind=8, this will be set to 8, otherwise it is set to 4. */ | |
014ec6ee | 110 | int gfc_intio_kind; |
4fec64b0 | 111 | |
f1412ca5 FXC |
112 | /* The integer kind used to store character lengths. */ |
113 | int gfc_charlen_int_kind; | |
114 | ||
39f87c03 FXC |
115 | /* The size of the numeric storage unit and character storage unit. */ |
116 | int gfc_numeric_storage_size; | |
117 | int gfc_character_storage_size; | |
118 | ||
a8b3b0b6 | 119 | |
17b1d2a0 | 120 | gfc_try |
a8b3b0b6 CR |
121 | gfc_check_any_c_kind (gfc_typespec *ts) |
122 | { | |
123 | int i; | |
124 | ||
125 | for (i = 0; i < ISOCBINDING_NUMBER; i++) | |
126 | { | |
127 | /* Check for any C interoperable kind for the given type/kind in ts. | |
128 | This can be used after verify_c_interop to make sure that the | |
129 | Fortran kind being used exists in at least some form for C. */ | |
130 | if (c_interop_kinds_table[i].f90_type == ts->type && | |
131 | c_interop_kinds_table[i].value == ts->kind) | |
132 | return SUCCESS; | |
133 | } | |
134 | ||
135 | return FAILURE; | |
136 | } | |
137 | ||
138 | ||
139 | static int | |
140 | get_real_kind_from_node (tree type) | |
141 | { | |
142 | int i; | |
143 | ||
144 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
145 | if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type)) | |
146 | return gfc_real_kinds[i].kind; | |
147 | ||
148 | return -4; | |
149 | } | |
150 | ||
151 | static int | |
152 | get_int_kind_from_node (tree type) | |
153 | { | |
154 | int i; | |
155 | ||
156 | if (!type) | |
157 | return -2; | |
158 | ||
159 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
160 | if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type)) | |
161 | return gfc_integer_kinds[i].kind; | |
162 | ||
163 | return -1; | |
164 | } | |
165 | ||
166 | static int | |
167 | get_int_kind_from_width (int size) | |
168 | { | |
169 | int i; | |
170 | ||
171 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
172 | if (gfc_integer_kinds[i].bit_size == size) | |
173 | return gfc_integer_kinds[i].kind; | |
174 | ||
175 | return -2; | |
176 | } | |
177 | ||
178 | static int | |
179 | get_int_kind_from_minimal_width (int size) | |
180 | { | |
181 | int i; | |
182 | ||
183 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
184 | if (gfc_integer_kinds[i].bit_size >= size) | |
185 | return gfc_integer_kinds[i].kind; | |
186 | ||
187 | return -2; | |
188 | } | |
189 | ||
190 | ||
191 | /* Generate the CInteropKind_t objects for the C interoperable | |
192 | kinds. */ | |
193 | ||
194 | static | |
195 | void init_c_interop_kinds (void) | |
196 | { | |
197 | int i; | |
198 | tree intmax_type_node = INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE ? | |
199 | integer_type_node : | |
200 | (LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE ? | |
201 | long_integer_type_node : | |
202 | long_long_integer_type_node); | |
203 | ||
204 | /* init all pointers in the list to NULL */ | |
205 | for (i = 0; i < ISOCBINDING_NUMBER; i++) | |
206 | { | |
207 | /* Initialize the name and value fields. */ | |
208 | c_interop_kinds_table[i].name[0] = '\0'; | |
209 | c_interop_kinds_table[i].value = -100; | |
210 | c_interop_kinds_table[i].f90_type = BT_UNKNOWN; | |
211 | } | |
212 | ||
21684705 | 213 | #define NAMED_INTCST(a,b,c,d) \ |
a8b3b0b6 CR |
214 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
215 | c_interop_kinds_table[a].f90_type = BT_INTEGER; \ | |
216 | c_interop_kinds_table[a].value = c; | |
217 | #define NAMED_REALCST(a,b,c) \ | |
218 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
219 | c_interop_kinds_table[a].f90_type = BT_REAL; \ | |
220 | c_interop_kinds_table[a].value = c; | |
221 | #define NAMED_CMPXCST(a,b,c) \ | |
222 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
223 | c_interop_kinds_table[a].f90_type = BT_COMPLEX; \ | |
224 | c_interop_kinds_table[a].value = c; | |
225 | #define NAMED_LOGCST(a,b,c) \ | |
226 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
227 | c_interop_kinds_table[a].f90_type = BT_LOGICAL; \ | |
228 | c_interop_kinds_table[a].value = c; | |
229 | #define NAMED_CHARKNDCST(a,b,c) \ | |
230 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
231 | c_interop_kinds_table[a].f90_type = BT_CHARACTER; \ | |
232 | c_interop_kinds_table[a].value = c; | |
233 | #define NAMED_CHARCST(a,b,c) \ | |
234 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
235 | c_interop_kinds_table[a].f90_type = BT_CHARACTER; \ | |
236 | c_interop_kinds_table[a].value = c; | |
237 | #define DERIVED_TYPE(a,b,c) \ | |
238 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
239 | c_interop_kinds_table[a].f90_type = BT_DERIVED; \ | |
240 | c_interop_kinds_table[a].value = c; | |
241 | #define PROCEDURE(a,b) \ | |
242 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
243 | c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \ | |
244 | c_interop_kinds_table[a].value = 0; | |
245 | #include "iso-c-binding.def" | |
246 | } | |
247 | ||
248 | ||
5e8e542f RH |
249 | /* Query the target to determine which machine modes are available for |
250 | computation. Choose KIND numbers for them. */ | |
251 | ||
252 | void | |
253 | gfc_init_kinds (void) | |
254 | { | |
255 | enum machine_mode mode; | |
374929b2 | 256 | int i_index, r_index, kind; |
5e8e542f RH |
257 | bool saw_i4 = false, saw_i8 = false; |
258 | bool saw_r4 = false, saw_r8 = false, saw_r16 = false; | |
259 | ||
260 | for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++) | |
261 | { | |
262 | int kind, bitsize; | |
263 | ||
264 | if (!targetm.scalar_mode_supported_p (mode)) | |
265 | continue; | |
266 | ||
04204c2f RH |
267 | /* The middle end doesn't support constants larger than 2*HWI. |
268 | Perhaps the target hook shouldn't have accepted these either, | |
269 | but just to be safe... */ | |
270 | bitsize = GET_MODE_BITSIZE (mode); | |
271 | if (bitsize > 2*HOST_BITS_PER_WIDE_INT) | |
272 | continue; | |
273 | ||
6e45f57b | 274 | gcc_assert (i_index != MAX_INT_KINDS); |
5e8e542f RH |
275 | |
276 | /* Let the kind equal the bit size divided by 8. This insulates the | |
277 | programmer from the underlying byte size. */ | |
5e8e542f RH |
278 | kind = bitsize / 8; |
279 | ||
280 | if (kind == 4) | |
281 | saw_i4 = true; | |
282 | if (kind == 8) | |
283 | saw_i8 = true; | |
284 | ||
285 | gfc_integer_kinds[i_index].kind = kind; | |
286 | gfc_integer_kinds[i_index].radix = 2; | |
287 | gfc_integer_kinds[i_index].digits = bitsize - 1; | |
288 | gfc_integer_kinds[i_index].bit_size = bitsize; | |
289 | ||
290 | gfc_logical_kinds[i_index].kind = kind; | |
291 | gfc_logical_kinds[i_index].bit_size = bitsize; | |
292 | ||
293 | i_index += 1; | |
294 | } | |
295 | ||
014ec6ee | 296 | /* Set the kind used to match GFC_INT_IO in libgfortran. This is |
4fec64b0 JD |
297 | used for large file access. */ |
298 | ||
299 | if (saw_i8) | |
014ec6ee | 300 | gfc_intio_kind = 8; |
4fec64b0 | 301 | else |
014ec6ee | 302 | gfc_intio_kind = 4; |
4fec64b0 JD |
303 | |
304 | /* If we do not at least have kind = 4, everything is pointless. */ | |
305 | gcc_assert(saw_i4); | |
306 | ||
f4e7375a SK |
307 | /* Set the maximum integer kind. Used with at least BOZ constants. */ |
308 | gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind; | |
309 | ||
5e8e542f RH |
310 | for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++) |
311 | { | |
312 | const struct real_format *fmt = REAL_MODE_FORMAT (mode); | |
313 | int kind; | |
314 | ||
315 | if (fmt == NULL) | |
316 | continue; | |
317 | if (!targetm.scalar_mode_supported_p (mode)) | |
318 | continue; | |
319 | ||
216ac520 SE |
320 | /* Only let float/double/long double go through because the fortran |
321 | library assumes these are the only floating point types. */ | |
322 | ||
323 | if (mode != TYPE_MODE (float_type_node) | |
324 | && (mode != TYPE_MODE (double_type_node)) | |
325 | && (mode != TYPE_MODE (long_double_type_node))) | |
326 | continue; | |
327 | ||
5e8e542f RH |
328 | /* Let the kind equal the precision divided by 8, rounding up. Again, |
329 | this insulates the programmer from the underlying byte size. | |
330 | ||
331 | Also, it effectively deals with IEEE extended formats. There, the | |
332 | total size of the type may equal 16, but it's got 6 bytes of padding | |
333 | and the increased size can get in the way of a real IEEE quad format | |
334 | which may also be supported by the target. | |
335 | ||
336 | We round up so as to handle IA-64 __floatreg (RFmode), which is an | |
337 | 82 bit type. Not to be confused with __float80 (XFmode), which is | |
338 | an 80 bit type also supported by IA-64. So XFmode should come out | |
339 | to be kind=10, and RFmode should come out to be kind=11. Egads. */ | |
340 | ||
341 | kind = (GET_MODE_PRECISION (mode) + 7) / 8; | |
342 | ||
343 | if (kind == 4) | |
344 | saw_r4 = true; | |
345 | if (kind == 8) | |
346 | saw_r8 = true; | |
347 | if (kind == 16) | |
348 | saw_r16 = true; | |
349 | ||
df2fba9e | 350 | /* Careful we don't stumble a weird internal mode. */ |
6e45f57b | 351 | gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind); |
5e8e542f | 352 | /* Or have too many modes for the allocated space. */ |
6e45f57b | 353 | gcc_assert (r_index != MAX_REAL_KINDS); |
5e8e542f RH |
354 | |
355 | gfc_real_kinds[r_index].kind = kind; | |
356 | gfc_real_kinds[r_index].radix = fmt->b; | |
357 | gfc_real_kinds[r_index].digits = fmt->p; | |
358 | gfc_real_kinds[r_index].min_exponent = fmt->emin; | |
359 | gfc_real_kinds[r_index].max_exponent = fmt->emax; | |
c69cb92f AM |
360 | if (fmt->pnan < fmt->p) |
361 | /* This is an IBM extended double format (or the MIPS variant) | |
362 | made up of two IEEE doubles. The value of the long double is | |
363 | the sum of the values of the two parts. The most significant | |
364 | part is required to be the value of the long double rounded | |
365 | to the nearest double. If we use emax of 1024 then we can't | |
366 | represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because | |
367 | rounding will make the most significant part overflow. */ | |
368 | gfc_real_kinds[r_index].max_exponent = fmt->emax - 1; | |
e2cad04b | 369 | gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode); |
5e8e542f RH |
370 | r_index += 1; |
371 | } | |
372 | ||
373 | /* Choose the default integer kind. We choose 4 unless the user | |
374 | directs us otherwise. */ | |
3ae9eb27 | 375 | if (gfc_option.flag_default_integer) |
5e8e542f RH |
376 | { |
377 | if (!saw_i8) | |
3ae9eb27 | 378 | fatal_error ("integer kind=8 not available for -fdefault-integer-8 option"); |
9d64df18 | 379 | gfc_default_integer_kind = 8; |
39f87c03 FXC |
380 | |
381 | /* Even if the user specified that the default integer kind be 8, | |
df2fba9e | 382 | the numeric storage size isn't 64. In this case, a warning will |
39f87c03 FXC |
383 | be issued when NUMERIC_STORAGE_SIZE is used. */ |
384 | gfc_numeric_storage_size = 4 * 8; | |
5e8e542f RH |
385 | } |
386 | else if (saw_i4) | |
39f87c03 FXC |
387 | { |
388 | gfc_default_integer_kind = 4; | |
389 | gfc_numeric_storage_size = 4 * 8; | |
390 | } | |
5e8e542f | 391 | else |
39f87c03 FXC |
392 | { |
393 | gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind; | |
394 | gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size; | |
395 | } | |
5e8e542f RH |
396 | |
397 | /* Choose the default real kind. Again, we choose 4 when possible. */ | |
3ae9eb27 | 398 | if (gfc_option.flag_default_real) |
5e8e542f RH |
399 | { |
400 | if (!saw_r8) | |
3ae9eb27 | 401 | fatal_error ("real kind=8 not available for -fdefault-real-8 option"); |
9d64df18 | 402 | gfc_default_real_kind = 8; |
5e8e542f RH |
403 | } |
404 | else if (saw_r4) | |
9d64df18 | 405 | gfc_default_real_kind = 4; |
5e8e542f | 406 | else |
9d64df18 | 407 | gfc_default_real_kind = gfc_real_kinds[0].kind; |
5e8e542f | 408 | |
3ae9eb27 SK |
409 | /* Choose the default double kind. If -fdefault-real and -fdefault-double |
410 | are specified, we use kind=8, if it's available. If -fdefault-real is | |
411 | specified without -fdefault-double, we use kind=16, if it's available. | |
412 | Otherwise we do not change anything. */ | |
413 | if (gfc_option.flag_default_double && !gfc_option.flag_default_real) | |
414 | fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8"); | |
415 | ||
416 | if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8) | |
417 | gfc_default_double_kind = 8; | |
418 | else if (gfc_option.flag_default_real && saw_r16) | |
9d64df18 | 419 | gfc_default_double_kind = 16; |
5e8e542f | 420 | else if (saw_r4 && saw_r8) |
9d64df18 | 421 | gfc_default_double_kind = 8; |
5e8e542f RH |
422 | else |
423 | { | |
424 | /* F95 14.6.3.1: A nonpointer scalar object of type double precision | |
425 | real ... occupies two contiguous numeric storage units. | |
426 | ||
427 | Therefore we must be supplied a kind twice as large as we chose | |
428 | for single precision. There are loopholes, in that double | |
429 | precision must *occupy* two storage units, though it doesn't have | |
430 | to *use* two storage units. Which means that you can make this | |
431 | kind artificially wide by padding it. But at present there are | |
432 | no GCC targets for which a two-word type does not exist, so we | |
433 | just let gfc_validate_kind abort and tell us if something breaks. */ | |
434 | ||
9d64df18 TS |
435 | gfc_default_double_kind |
436 | = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false); | |
5e8e542f RH |
437 | } |
438 | ||
439 | /* The default logical kind is constrained to be the same as the | |
440 | default integer kind. Similarly with complex and real. */ | |
9d64df18 TS |
441 | gfc_default_logical_kind = gfc_default_integer_kind; |
442 | gfc_default_complex_kind = gfc_default_real_kind; | |
5e8e542f | 443 | |
374929b2 FXC |
444 | /* We only have two character kinds: ASCII and UCS-4. |
445 | ASCII corresponds to a 8-bit integer type, if one is available. | |
446 | UCS-4 corresponds to a 32-bit integer type, if one is available. */ | |
447 | i_index = 0; | |
448 | if ((kind = get_int_kind_from_width (8)) > 0) | |
449 | { | |
450 | gfc_character_kinds[i_index].kind = kind; | |
451 | gfc_character_kinds[i_index].bit_size = 8; | |
452 | gfc_character_kinds[i_index].name = "ascii"; | |
453 | i_index++; | |
454 | } | |
455 | if ((kind = get_int_kind_from_width (32)) > 0) | |
456 | { | |
457 | gfc_character_kinds[i_index].kind = kind; | |
458 | gfc_character_kinds[i_index].bit_size = 32; | |
459 | gfc_character_kinds[i_index].name = "iso_10646"; | |
460 | i_index++; | |
461 | } | |
462 | ||
5e8e542f | 463 | /* Choose the smallest integer kind for our default character. */ |
374929b2 | 464 | gfc_default_character_kind = gfc_character_kinds[0].kind; |
39f87c03 | 465 | gfc_character_storage_size = gfc_default_character_kind * 8; |
5e8e542f RH |
466 | |
467 | /* Choose the integer kind the same size as "void*" for our index kind. */ | |
468 | gfc_index_integer_kind = POINTER_SIZE / 8; | |
e8525382 SK |
469 | /* Pick a kind the same size as the C "int" type. */ |
470 | gfc_c_int_kind = INT_TYPE_SIZE / 8; | |
a8b3b0b6 CR |
471 | |
472 | /* initialize the C interoperable kinds */ | |
473 | init_c_interop_kinds(); | |
5e8e542f RH |
474 | } |
475 | ||
5e8e542f RH |
476 | /* Make sure that a valid kind is present. Returns an index into the |
477 | associated kinds array, -1 if the kind is not present. */ | |
478 | ||
479 | static int | |
480 | validate_integer (int kind) | |
481 | { | |
482 | int i; | |
483 | ||
484 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
485 | if (gfc_integer_kinds[i].kind == kind) | |
486 | return i; | |
487 | ||
488 | return -1; | |
489 | } | |
490 | ||
491 | static int | |
492 | validate_real (int kind) | |
493 | { | |
494 | int i; | |
495 | ||
496 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
497 | if (gfc_real_kinds[i].kind == kind) | |
498 | return i; | |
499 | ||
500 | return -1; | |
501 | } | |
502 | ||
503 | static int | |
504 | validate_logical (int kind) | |
505 | { | |
506 | int i; | |
507 | ||
508 | for (i = 0; gfc_logical_kinds[i].kind; i++) | |
509 | if (gfc_logical_kinds[i].kind == kind) | |
510 | return i; | |
511 | ||
512 | return -1; | |
513 | } | |
514 | ||
515 | static int | |
516 | validate_character (int kind) | |
517 | { | |
374929b2 FXC |
518 | int i; |
519 | ||
520 | for (i = 0; gfc_character_kinds[i].kind; i++) | |
521 | if (gfc_character_kinds[i].kind == kind) | |
522 | return i; | |
523 | ||
524 | return -1; | |
5e8e542f RH |
525 | } |
526 | ||
527 | /* Validate a kind given a basic type. The return value is the same | |
528 | for the child functions, with -1 indicating nonexistence of the | |
529 | type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */ | |
530 | ||
531 | int | |
532 | gfc_validate_kind (bt type, int kind, bool may_fail) | |
533 | { | |
534 | int rc; | |
535 | ||
536 | switch (type) | |
537 | { | |
538 | case BT_REAL: /* Fall through */ | |
539 | case BT_COMPLEX: | |
540 | rc = validate_real (kind); | |
541 | break; | |
542 | case BT_INTEGER: | |
543 | rc = validate_integer (kind); | |
544 | break; | |
545 | case BT_LOGICAL: | |
546 | rc = validate_logical (kind); | |
547 | break; | |
548 | case BT_CHARACTER: | |
549 | rc = validate_character (kind); | |
550 | break; | |
551 | ||
552 | default: | |
553 | gfc_internal_error ("gfc_validate_kind(): Got bad type"); | |
554 | } | |
555 | ||
556 | if (rc < 0 && !may_fail) | |
557 | gfc_internal_error ("gfc_validate_kind(): Got bad kind"); | |
558 | ||
559 | return rc; | |
560 | } | |
561 | ||
562 | ||
e2cad04b RH |
563 | /* Four subroutines of gfc_init_types. Create type nodes for the given kind. |
564 | Reuse common type nodes where possible. Recognize if the kind matches up | |
565 | with a C type. This will be used later in determining which routines may | |
566 | be scarfed from libm. */ | |
567 | ||
568 | static tree | |
569 | gfc_build_int_type (gfc_integer_info *info) | |
570 | { | |
571 | int mode_precision = info->bit_size; | |
572 | ||
573 | if (mode_precision == CHAR_TYPE_SIZE) | |
574 | info->c_char = 1; | |
575 | if (mode_precision == SHORT_TYPE_SIZE) | |
576 | info->c_short = 1; | |
577 | if (mode_precision == INT_TYPE_SIZE) | |
578 | info->c_int = 1; | |
579 | if (mode_precision == LONG_TYPE_SIZE) | |
580 | info->c_long = 1; | |
581 | if (mode_precision == LONG_LONG_TYPE_SIZE) | |
582 | info->c_long_long = 1; | |
583 | ||
584 | if (TYPE_PRECISION (intQI_type_node) == mode_precision) | |
585 | return intQI_type_node; | |
586 | if (TYPE_PRECISION (intHI_type_node) == mode_precision) | |
587 | return intHI_type_node; | |
588 | if (TYPE_PRECISION (intSI_type_node) == mode_precision) | |
589 | return intSI_type_node; | |
590 | if (TYPE_PRECISION (intDI_type_node) == mode_precision) | |
591 | return intDI_type_node; | |
592 | if (TYPE_PRECISION (intTI_type_node) == mode_precision) | |
593 | return intTI_type_node; | |
594 | ||
595 | return make_signed_type (mode_precision); | |
596 | } | |
597 | ||
374929b2 FXC |
598 | static tree |
599 | gfc_build_uint_type (int size) | |
600 | { | |
601 | if (size == CHAR_TYPE_SIZE) | |
602 | return unsigned_char_type_node; | |
603 | if (size == SHORT_TYPE_SIZE) | |
604 | return short_unsigned_type_node; | |
605 | if (size == INT_TYPE_SIZE) | |
606 | return unsigned_type_node; | |
607 | if (size == LONG_TYPE_SIZE) | |
608 | return long_unsigned_type_node; | |
609 | if (size == LONG_LONG_TYPE_SIZE) | |
610 | return long_long_unsigned_type_node; | |
611 | ||
612 | return make_unsigned_type (size); | |
613 | } | |
614 | ||
615 | ||
e2cad04b RH |
616 | static tree |
617 | gfc_build_real_type (gfc_real_info *info) | |
618 | { | |
619 | int mode_precision = info->mode_precision; | |
620 | tree new_type; | |
621 | ||
622 | if (mode_precision == FLOAT_TYPE_SIZE) | |
623 | info->c_float = 1; | |
624 | if (mode_precision == DOUBLE_TYPE_SIZE) | |
625 | info->c_double = 1; | |
626 | if (mode_precision == LONG_DOUBLE_TYPE_SIZE) | |
627 | info->c_long_double = 1; | |
628 | ||
629 | if (TYPE_PRECISION (float_type_node) == mode_precision) | |
630 | return float_type_node; | |
631 | if (TYPE_PRECISION (double_type_node) == mode_precision) | |
632 | return double_type_node; | |
633 | if (TYPE_PRECISION (long_double_type_node) == mode_precision) | |
634 | return long_double_type_node; | |
635 | ||
636 | new_type = make_node (REAL_TYPE); | |
637 | TYPE_PRECISION (new_type) = mode_precision; | |
638 | layout_type (new_type); | |
639 | return new_type; | |
640 | } | |
641 | ||
642 | static tree | |
643 | gfc_build_complex_type (tree scalar_type) | |
644 | { | |
645 | tree new_type; | |
646 | ||
647 | if (scalar_type == NULL) | |
648 | return NULL; | |
649 | if (scalar_type == float_type_node) | |
650 | return complex_float_type_node; | |
651 | if (scalar_type == double_type_node) | |
652 | return complex_double_type_node; | |
653 | if (scalar_type == long_double_type_node) | |
654 | return complex_long_double_type_node; | |
655 | ||
656 | new_type = make_node (COMPLEX_TYPE); | |
657 | TREE_TYPE (new_type) = scalar_type; | |
658 | layout_type (new_type); | |
659 | return new_type; | |
660 | } | |
661 | ||
662 | static tree | |
663 | gfc_build_logical_type (gfc_logical_info *info) | |
664 | { | |
665 | int bit_size = info->bit_size; | |
666 | tree new_type; | |
667 | ||
668 | if (bit_size == BOOL_TYPE_SIZE) | |
669 | { | |
670 | info->c_bool = 1; | |
671 | return boolean_type_node; | |
672 | } | |
673 | ||
674 | new_type = make_unsigned_type (bit_size); | |
675 | TREE_SET_CODE (new_type, BOOLEAN_TYPE); | |
676 | TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1); | |
677 | TYPE_PRECISION (new_type) = 1; | |
678 | ||
679 | return new_type; | |
680 | } | |
681 | ||
682 | #if 0 | |
683 | /* Return the bit size of the C "size_t". */ | |
684 | ||
685 | static unsigned int | |
686 | c_size_t_size (void) | |
687 | { | |
688 | #ifdef SIZE_TYPE | |
689 | if (strcmp (SIZE_TYPE, "unsigned int") == 0) | |
690 | return INT_TYPE_SIZE; | |
691 | if (strcmp (SIZE_TYPE, "long unsigned int") == 0) | |
692 | return LONG_TYPE_SIZE; | |
693 | if (strcmp (SIZE_TYPE, "short unsigned int") == 0) | |
694 | return SHORT_TYPE_SIZE; | |
6e45f57b | 695 | gcc_unreachable (); |
e2cad04b RH |
696 | #else |
697 | return LONG_TYPE_SIZE; | |
698 | #endif | |
699 | } | |
700 | #endif | |
701 | ||
6de9cd9a DN |
702 | /* Create the backend type nodes. We map them to their |
703 | equivalent C type, at least for now. We also give | |
704 | names to the types here, and we push them in the | |
705 | global binding level context.*/ | |
c3e8c6b8 | 706 | |
6de9cd9a DN |
707 | void |
708 | gfc_init_types (void) | |
709 | { | |
e5008df7 | 710 | char name_buf[18]; |
e2cad04b RH |
711 | int index; |
712 | tree type; | |
6de9cd9a DN |
713 | unsigned n; |
714 | unsigned HOST_WIDE_INT hi; | |
715 | unsigned HOST_WIDE_INT lo; | |
716 | ||
e2cad04b | 717 | /* Create and name the types. */ |
6de9cd9a DN |
718 | #define PUSH_TYPE(name, node) \ |
719 | pushdecl (build_decl (TYPE_DECL, get_identifier (name), node)) | |
720 | ||
e2cad04b RH |
721 | for (index = 0; gfc_integer_kinds[index].kind != 0; ++index) |
722 | { | |
723 | type = gfc_build_int_type (&gfc_integer_kinds[index]); | |
724 | gfc_integer_types[index] = type; | |
40373aa6 | 725 | snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)", |
e2cad04b RH |
726 | gfc_integer_kinds[index].kind); |
727 | PUSH_TYPE (name_buf, type); | |
728 | } | |
6de9cd9a | 729 | |
e2cad04b RH |
730 | for (index = 0; gfc_logical_kinds[index].kind != 0; ++index) |
731 | { | |
732 | type = gfc_build_logical_type (&gfc_logical_kinds[index]); | |
733 | gfc_logical_types[index] = type; | |
40373aa6 | 734 | snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)", |
e2cad04b RH |
735 | gfc_logical_kinds[index].kind); |
736 | PUSH_TYPE (name_buf, type); | |
737 | } | |
6de9cd9a | 738 | |
e2cad04b RH |
739 | for (index = 0; gfc_real_kinds[index].kind != 0; index++) |
740 | { | |
741 | type = gfc_build_real_type (&gfc_real_kinds[index]); | |
742 | gfc_real_types[index] = type; | |
40373aa6 | 743 | snprintf (name_buf, sizeof(name_buf), "real(kind=%d)", |
e2cad04b RH |
744 | gfc_real_kinds[index].kind); |
745 | PUSH_TYPE (name_buf, type); | |
746 | ||
747 | type = gfc_build_complex_type (type); | |
748 | gfc_complex_types[index] = type; | |
40373aa6 | 749 | snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)", |
e2cad04b RH |
750 | gfc_real_kinds[index].kind); |
751 | PUSH_TYPE (name_buf, type); | |
752 | } | |
6de9cd9a | 753 | |
374929b2 FXC |
754 | for (index = 0; gfc_character_kinds[index].kind != 0; ++index) |
755 | { | |
756 | type = gfc_build_uint_type (gfc_character_kinds[index].bit_size); | |
757 | type = build_qualified_type (type, TYPE_UNQUALIFIED); | |
758 | snprintf (name_buf, sizeof(name_buf), "character(kind=%d)", | |
759 | gfc_character_kinds[index].kind); | |
760 | PUSH_TYPE (name_buf, type); | |
761 | gfc_character_types[index] = type; | |
762 | gfc_pcharacter_types[index] = build_pointer_type (type); | |
763 | } | |
764 | gfc_character1_type_node = gfc_character_types[0]; | |
6de9cd9a DN |
765 | |
766 | PUSH_TYPE ("byte", unsigned_char_type_node); | |
767 | PUSH_TYPE ("void", void_type_node); | |
768 | ||
769 | /* DBX debugging output gets upset if these aren't set. */ | |
770 | if (!TYPE_NAME (integer_type_node)) | |
771 | PUSH_TYPE ("c_integer", integer_type_node); | |
772 | if (!TYPE_NAME (char_type_node)) | |
773 | PUSH_TYPE ("c_char", char_type_node); | |
e2cad04b | 774 | |
6de9cd9a DN |
775 | #undef PUSH_TYPE |
776 | ||
777 | pvoid_type_node = build_pointer_type (void_type_node); | |
778 | ppvoid_type_node = build_pointer_type (pvoid_type_node); | |
779 | pchar_type_node = build_pointer_type (gfc_character1_type_node); | |
089db47d CR |
780 | pfunc_type_node |
781 | = build_pointer_type (build_function_type (void_type_node, NULL_TREE)); | |
6de9cd9a | 782 | |
6de9cd9a | 783 | gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind); |
b4838d29 ZD |
784 | /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type, |
785 | since this function is called before gfc_init_constants. */ | |
786 | gfc_array_range_type | |
787 | = build_range_type (gfc_array_index_type, | |
788 | build_int_cst (gfc_array_index_type, 0), | |
789 | NULL_TREE); | |
6de9cd9a DN |
790 | |
791 | /* The maximum array element size that can be handled is determined | |
792 | by the number of bits available to store this field in the array | |
793 | descriptor. */ | |
794 | ||
e2cad04b RH |
795 | n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT; |
796 | lo = ~ (unsigned HOST_WIDE_INT) 0; | |
797 | if (n > HOST_BITS_PER_WIDE_INT) | |
798 | hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n); | |
6de9cd9a | 799 | else |
e2cad04b | 800 | hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n; |
7d60be94 NS |
801 | gfc_max_array_element_size |
802 | = build_int_cst_wide (long_unsigned_type_node, lo, hi); | |
6de9cd9a DN |
803 | |
804 | size_type_node = gfc_array_index_type; | |
6de9cd9a | 805 | |
e2cad04b | 806 | boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind); |
7d60be94 NS |
807 | boolean_true_node = build_int_cst (boolean_type_node, 1); |
808 | boolean_false_node = build_int_cst (boolean_type_node, 0); | |
e2cad04b RH |
809 | |
810 | /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */ | |
f1412ca5 FXC |
811 | gfc_charlen_int_kind = 4; |
812 | gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind); | |
6de9cd9a DN |
813 | } |
814 | ||
e2cad04b | 815 | /* Get the type node for the given type and kind. */ |
c3e8c6b8 | 816 | |
6de9cd9a DN |
817 | tree |
818 | gfc_get_int_type (int kind) | |
819 | { | |
644cb69f FXC |
820 | int index = gfc_validate_kind (BT_INTEGER, kind, true); |
821 | return index < 0 ? 0 : gfc_integer_types[index]; | |
6de9cd9a DN |
822 | } |
823 | ||
6de9cd9a DN |
824 | tree |
825 | gfc_get_real_type (int kind) | |
826 | { | |
644cb69f FXC |
827 | int index = gfc_validate_kind (BT_REAL, kind, true); |
828 | return index < 0 ? 0 : gfc_real_types[index]; | |
6de9cd9a DN |
829 | } |
830 | ||
6de9cd9a DN |
831 | tree |
832 | gfc_get_complex_type (int kind) | |
833 | { | |
644cb69f FXC |
834 | int index = gfc_validate_kind (BT_COMPLEX, kind, true); |
835 | return index < 0 ? 0 : gfc_complex_types[index]; | |
6de9cd9a DN |
836 | } |
837 | ||
6de9cd9a DN |
838 | tree |
839 | gfc_get_logical_type (int kind) | |
840 | { | |
644cb69f FXC |
841 | int index = gfc_validate_kind (BT_LOGICAL, kind, true); |
842 | return index < 0 ? 0 : gfc_logical_types[index]; | |
6de9cd9a | 843 | } |
374929b2 FXC |
844 | |
845 | tree | |
846 | gfc_get_char_type (int kind) | |
847 | { | |
848 | int index = gfc_validate_kind (BT_CHARACTER, kind, true); | |
849 | return index < 0 ? 0 : gfc_character_types[index]; | |
850 | } | |
851 | ||
852 | tree | |
853 | gfc_get_pchar_type (int kind) | |
854 | { | |
855 | int index = gfc_validate_kind (BT_CHARACTER, kind, true); | |
856 | return index < 0 ? 0 : gfc_pcharacter_types[index]; | |
857 | } | |
858 | ||
6de9cd9a | 859 | \f |
40f20186 | 860 | /* Create a character type with the given kind and length. */ |
c3e8c6b8 | 861 | |
6de9cd9a | 862 | tree |
d393bbd7 | 863 | gfc_get_character_type_len_for_eltype (tree eltype, tree len) |
6de9cd9a | 864 | { |
e2cad04b | 865 | tree bounds, type; |
6de9cd9a | 866 | |
5e3b8727 | 867 | bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len); |
d393bbd7 | 868 | type = build_array_type (eltype, bounds); |
6de9cd9a DN |
869 | TYPE_STRING_FLAG (type) = 1; |
870 | ||
871 | return type; | |
872 | } | |
40f20186 | 873 | |
d393bbd7 FXC |
874 | tree |
875 | gfc_get_character_type_len (int kind, tree len) | |
876 | { | |
877 | gfc_validate_kind (BT_CHARACTER, kind, false); | |
878 | return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len); | |
879 | } | |
880 | ||
40f20186 PB |
881 | |
882 | /* Get a type node for a character kind. */ | |
883 | ||
884 | tree | |
885 | gfc_get_character_type (int kind, gfc_charlen * cl) | |
886 | { | |
887 | tree len; | |
888 | ||
889 | len = (cl == NULL) ? NULL_TREE : cl->backend_decl; | |
890 | ||
891 | return gfc_get_character_type_len (kind, len); | |
892 | } | |
6de9cd9a DN |
893 | \f |
894 | /* Covert a basic type. This will be an array for character types. */ | |
c3e8c6b8 | 895 | |
6de9cd9a DN |
896 | tree |
897 | gfc_typenode_for_spec (gfc_typespec * spec) | |
898 | { | |
899 | tree basetype; | |
900 | ||
901 | switch (spec->type) | |
902 | { | |
903 | case BT_UNKNOWN: | |
6e45f57b | 904 | gcc_unreachable (); |
6de9cd9a DN |
905 | |
906 | case BT_INTEGER: | |
a8b3b0b6 CR |
907 | /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol |
908 | has been resolved. This is done so we can convert C_PTR and | |
909 | C_FUNPTR to simple variables that get translated to (void *). */ | |
910 | if (spec->f90_type == BT_VOID) | |
089db47d CR |
911 | { |
912 | if (spec->derived | |
913 | && spec->derived->intmod_sym_id == ISOCBINDING_PTR) | |
914 | basetype = ptr_type_node; | |
915 | else | |
916 | basetype = pfunc_type_node; | |
917 | } | |
a8b3b0b6 CR |
918 | else |
919 | basetype = gfc_get_int_type (spec->kind); | |
6de9cd9a DN |
920 | break; |
921 | ||
922 | case BT_REAL: | |
923 | basetype = gfc_get_real_type (spec->kind); | |
924 | break; | |
925 | ||
926 | case BT_COMPLEX: | |
927 | basetype = gfc_get_complex_type (spec->kind); | |
928 | break; | |
929 | ||
930 | case BT_LOGICAL: | |
931 | basetype = gfc_get_logical_type (spec->kind); | |
932 | break; | |
933 | ||
934 | case BT_CHARACTER: | |
935 | basetype = gfc_get_character_type (spec->kind, spec->cl); | |
936 | break; | |
937 | ||
938 | case BT_DERIVED: | |
939 | basetype = gfc_get_derived_type (spec->derived); | |
6de9cd9a | 940 | |
a8b3b0b6 CR |
941 | /* If we're dealing with either C_PTR or C_FUNPTR, we modified the |
942 | type and kind to fit a (void *) and the basetype returned was a | |
943 | ptr_type_node. We need to pass up this new information to the | |
944 | symbol that was declared of type C_PTR or C_FUNPTR. */ | |
945 | if (spec->derived->attr.is_iso_c) | |
946 | { | |
947 | spec->type = spec->derived->ts.type; | |
948 | spec->kind = spec->derived->ts.kind; | |
949 | spec->f90_type = spec->derived->ts.f90_type; | |
950 | } | |
951 | break; | |
952 | case BT_VOID: | |
089db47d CR |
953 | /* This is for the second arg to c_f_pointer and c_f_procpointer |
954 | of the iso_c_binding module, to accept any ptr type. */ | |
955 | basetype = ptr_type_node; | |
956 | if (spec->f90_type == BT_VOID) | |
957 | { | |
958 | if (spec->derived | |
959 | && spec->derived->intmod_sym_id == ISOCBINDING_PTR) | |
960 | basetype = ptr_type_node; | |
961 | else | |
962 | basetype = pfunc_type_node; | |
963 | } | |
a8b3b0b6 | 964 | break; |
6de9cd9a | 965 | default: |
6e45f57b | 966 | gcc_unreachable (); |
6de9cd9a DN |
967 | } |
968 | return basetype; | |
969 | } | |
970 | \f | |
971 | /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */ | |
c3e8c6b8 | 972 | |
6de9cd9a DN |
973 | static tree |
974 | gfc_conv_array_bound (gfc_expr * expr) | |
975 | { | |
976 | /* If expr is an integer constant, return that. */ | |
977 | if (expr != NULL && expr->expr_type == EXPR_CONSTANT) | |
978 | return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind); | |
979 | ||
980 | /* Otherwise return NULL. */ | |
981 | return NULL_TREE; | |
982 | } | |
983 | \f | |
984 | tree | |
985 | gfc_get_element_type (tree type) | |
986 | { | |
987 | tree element; | |
988 | ||
989 | if (GFC_ARRAY_TYPE_P (type)) | |
990 | { | |
991 | if (TREE_CODE (type) == POINTER_TYPE) | |
992 | type = TREE_TYPE (type); | |
6e45f57b | 993 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
6de9cd9a DN |
994 | element = TREE_TYPE (type); |
995 | } | |
996 | else | |
997 | { | |
6e45f57b | 998 | gcc_assert (GFC_DESCRIPTOR_TYPE_P (type)); |
4c73896d | 999 | element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type); |
6de9cd9a | 1000 | |
6e45f57b | 1001 | gcc_assert (TREE_CODE (element) == POINTER_TYPE); |
6de9cd9a DN |
1002 | element = TREE_TYPE (element); |
1003 | ||
6e45f57b | 1004 | gcc_assert (TREE_CODE (element) == ARRAY_TYPE); |
6de9cd9a DN |
1005 | element = TREE_TYPE (element); |
1006 | } | |
1007 | ||
1008 | return element; | |
1009 | } | |
1010 | \f | |
eb28fb7d | 1011 | /* Build an array. This function is called from gfc_sym_type(). |
c3e8c6b8 | 1012 | Actually returns array descriptor type. |
6de9cd9a DN |
1013 | |
1014 | Format of array descriptors is as follows: | |
1015 | ||
1016 | struct gfc_array_descriptor | |
1017 | { | |
1018 | array *data | |
1019 | index offset; | |
1020 | index dtype; | |
1021 | struct descriptor_dimension dimension[N_DIM]; | |
1022 | } | |
1023 | ||
1024 | struct descriptor_dimension | |
1025 | { | |
1026 | index stride; | |
1027 | index lbound; | |
1028 | index ubound; | |
1029 | } | |
1030 | ||
eb28fb7d TS |
1031 | Translation code should use gfc_conv_descriptor_* rather than |
1032 | accessing the descriptor directly. Any changes to the array | |
1033 | descriptor type will require changes in gfc_conv_descriptor_* and | |
1034 | gfc_build_array_initializer. | |
6de9cd9a | 1035 | |
eb28fb7d TS |
1036 | This is represented internally as a RECORD_TYPE. The index nodes |
1037 | are gfc_array_index_type and the data node is a pointer to the | |
1038 | data. See below for the handling of character types. | |
6de9cd9a DN |
1039 | |
1040 | The dtype member is formatted as follows: | |
1041 | rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits | |
1042 | type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits | |
1043 | size = dtype >> GFC_DTYPE_SIZE_SHIFT | |
1044 | ||
eb28fb7d TS |
1045 | I originally used nested ARRAY_TYPE nodes to represent arrays, but |
1046 | this generated poor code for assumed/deferred size arrays. These | |
1047 | require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part | |
1048 | of the GENERIC grammar. Also, there is no way to explicitly set | |
1049 | the array stride, so all data must be packed(1). I've tried to | |
1050 | mark all the functions which would require modification with a GCC | |
1051 | ARRAYS comment. | |
6de9cd9a | 1052 | |
eb28fb7d | 1053 | The data component points to the first element in the array. The |
df2fba9e RW |
1054 | offset field is the position of the origin of the array (i.e. element |
1055 | (0, 0 ...)). This may be outside the bounds of the array. | |
6de9cd9a DN |
1056 | |
1057 | An element is accessed by | |
eb28fb7d | 1058 | data[offset + index0*stride0 + index1*stride1 + index2*stride2] |
c3e8c6b8 | 1059 | This gives good performance as the computation does not involve the |
eb28fb7d TS |
1060 | bounds of the array. For packed arrays, this is optimized further |
1061 | by substituting the known strides. | |
6de9cd9a | 1062 | |
eb28fb7d TS |
1063 | This system has one problem: all array bounds must be within 2^31 |
1064 | elements of the origin (2^63 on 64-bit machines). For example | |
1065 | integer, dimension (80000:90000, 80000:90000, 2) :: array | |
1066 | may not work properly on 32-bit machines because 80000*80000 > | |
df2fba9e | 1067 | 2^31, so the calculation for stride2 would overflow. This may |
eb28fb7d TS |
1068 | still work, but I haven't checked, and it relies on the overflow |
1069 | doing the right thing. | |
6de9cd9a | 1070 | |
1f2959f0 | 1071 | The way to fix this problem is to access elements as follows: |
eb28fb7d TS |
1072 | data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1] |
1073 | Obviously this is much slower. I will make this a compile time | |
1074 | option, something like -fsmall-array-offsets. Mixing code compiled | |
1075 | with and without this switch will work. | |
1076 | ||
1077 | (1) This can be worked around by modifying the upper bound of the | |
1078 | previous dimension. This requires extra fields in the descriptor | |
1079 | (both real_ubound and fake_ubound). */ | |
6de9cd9a DN |
1080 | |
1081 | ||
1082 | /* Returns true if the array sym does not require a descriptor. */ | |
1083 | ||
1084 | int | |
1085 | gfc_is_nodesc_array (gfc_symbol * sym) | |
1086 | { | |
6e45f57b | 1087 | gcc_assert (sym->attr.dimension); |
6de9cd9a DN |
1088 | |
1089 | /* We only want local arrays. */ | |
1090 | if (sym->attr.pointer || sym->attr.allocatable) | |
1091 | return 0; | |
1092 | ||
1093 | if (sym->attr.dummy) | |
1094 | { | |
1095 | if (sym->as->type != AS_ASSUMED_SHAPE) | |
1096 | return 1; | |
1097 | else | |
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | if (sym->attr.result || sym->attr.function) | |
1102 | return 0; | |
1103 | ||
6e45f57b | 1104 | gcc_assert (sym->as->type == AS_EXPLICIT); |
6de9cd9a DN |
1105 | |
1106 | return 1; | |
1107 | } | |
1108 | ||
40f20186 PB |
1109 | |
1110 | /* Create an array descriptor type. */ | |
1111 | ||
6de9cd9a | 1112 | static tree |
fad0afd7 JJ |
1113 | gfc_build_array_type (tree type, gfc_array_spec * as, |
1114 | enum gfc_array_kind akind) | |
6de9cd9a DN |
1115 | { |
1116 | tree lbound[GFC_MAX_DIMENSIONS]; | |
1117 | tree ubound[GFC_MAX_DIMENSIONS]; | |
1118 | int n; | |
1119 | ||
1120 | for (n = 0; n < as->rank; n++) | |
1121 | { | |
1122 | /* Create expressions for the known bounds of the array. */ | |
1123 | if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL) | |
7ab92584 | 1124 | lbound[n] = gfc_index_one_node; |
6de9cd9a DN |
1125 | else |
1126 | lbound[n] = gfc_conv_array_bound (as->lower[n]); | |
1127 | ubound[n] = gfc_conv_array_bound (as->upper[n]); | |
1128 | } | |
1129 | ||
fad0afd7 JJ |
1130 | if (as->type == AS_ASSUMED_SHAPE) |
1131 | akind = GFC_ARRAY_ASSUMED_SHAPE; | |
1132 | return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0, akind); | |
6de9cd9a DN |
1133 | } |
1134 | \f | |
1135 | /* Returns the struct descriptor_dimension type. */ | |
c3e8c6b8 | 1136 | |
6de9cd9a DN |
1137 | static tree |
1138 | gfc_get_desc_dim_type (void) | |
1139 | { | |
1140 | tree type; | |
1141 | tree decl; | |
1142 | tree fieldlist; | |
1143 | ||
1144 | if (gfc_desc_dim_type) | |
1145 | return gfc_desc_dim_type; | |
1146 | ||
1147 | /* Build the type node. */ | |
1148 | type = make_node (RECORD_TYPE); | |
1149 | ||
1150 | TYPE_NAME (type) = get_identifier ("descriptor_dimension"); | |
1151 | TYPE_PACKED (type) = 1; | |
1152 | ||
1153 | /* Consists of the stride, lbound and ubound members. */ | |
1154 | decl = build_decl (FIELD_DECL, | |
1155 | get_identifier ("stride"), gfc_array_index_type); | |
1156 | DECL_CONTEXT (decl) = type; | |
d8eff1b8 | 1157 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a DN |
1158 | fieldlist = decl; |
1159 | ||
1160 | decl = build_decl (FIELD_DECL, | |
1161 | get_identifier ("lbound"), gfc_array_index_type); | |
1162 | DECL_CONTEXT (decl) = type; | |
d8eff1b8 | 1163 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a DN |
1164 | fieldlist = chainon (fieldlist, decl); |
1165 | ||
1166 | decl = build_decl (FIELD_DECL, | |
1167 | get_identifier ("ubound"), gfc_array_index_type); | |
1168 | DECL_CONTEXT (decl) = type; | |
d8eff1b8 | 1169 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a DN |
1170 | fieldlist = chainon (fieldlist, decl); |
1171 | ||
1172 | /* Finish off the type. */ | |
1173 | TYPE_FIELDS (type) = fieldlist; | |
1174 | ||
1175 | gfc_finish_type (type); | |
dfcf0b12 | 1176 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1; |
6de9cd9a DN |
1177 | |
1178 | gfc_desc_dim_type = type; | |
1179 | return type; | |
1180 | } | |
1181 | ||
40b026d8 | 1182 | |
43a5ef69 | 1183 | /* Return the DTYPE for an array. This describes the type and type parameters |
40b026d8 PB |
1184 | of the array. */ |
1185 | /* TODO: Only call this when the value is actually used, and make all the | |
1186 | unknown cases abort. */ | |
1187 | ||
1188 | tree | |
1189 | gfc_get_dtype (tree type) | |
6de9cd9a DN |
1190 | { |
1191 | tree size; | |
1192 | int n; | |
1193 | HOST_WIDE_INT i; | |
1194 | tree tmp; | |
1195 | tree dtype; | |
40b026d8 PB |
1196 | tree etype; |
1197 | int rank; | |
1198 | ||
1199 | gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type)); | |
1200 | ||
1201 | if (GFC_TYPE_ARRAY_DTYPE (type)) | |
1202 | return GFC_TYPE_ARRAY_DTYPE (type); | |
6de9cd9a | 1203 | |
40b026d8 PB |
1204 | rank = GFC_TYPE_ARRAY_RANK (type); |
1205 | etype = gfc_get_element_type (type); | |
6de9cd9a | 1206 | |
40b026d8 | 1207 | switch (TREE_CODE (etype)) |
6de9cd9a DN |
1208 | { |
1209 | case INTEGER_TYPE: | |
1210 | n = GFC_DTYPE_INTEGER; | |
1211 | break; | |
1212 | ||
1213 | case BOOLEAN_TYPE: | |
1214 | n = GFC_DTYPE_LOGICAL; | |
1215 | break; | |
1216 | ||
1217 | case REAL_TYPE: | |
1218 | n = GFC_DTYPE_REAL; | |
1219 | break; | |
1220 | ||
1221 | case COMPLEX_TYPE: | |
1222 | n = GFC_DTYPE_COMPLEX; | |
1223 | break; | |
1224 | ||
40b026d8 | 1225 | /* We will never have arrays of arrays. */ |
6de9cd9a DN |
1226 | case RECORD_TYPE: |
1227 | n = GFC_DTYPE_DERIVED; | |
1228 | break; | |
1229 | ||
1230 | case ARRAY_TYPE: | |
1231 | n = GFC_DTYPE_CHARACTER; | |
1232 | break; | |
1233 | ||
1234 | default: | |
40f20186 PB |
1235 | /* TODO: Don't do dtype for temporary descriptorless arrays. */ |
1236 | /* We can strange array types for temporary arrays. */ | |
1237 | return gfc_index_zero_node; | |
6de9cd9a DN |
1238 | } |
1239 | ||
6e45f57b | 1240 | gcc_assert (rank <= GFC_DTYPE_RANK_MASK); |
40b026d8 | 1241 | size = TYPE_SIZE_UNIT (etype); |
f676971a | 1242 | |
6de9cd9a DN |
1243 | i = rank | (n << GFC_DTYPE_TYPE_SHIFT); |
1244 | if (size && INTEGER_CST_P (size)) | |
1245 | { | |
1246 | if (tree_int_cst_lt (gfc_max_array_element_size, size)) | |
1247 | internal_error ("Array element size too big"); | |
1248 | ||
1249 | i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT; | |
1250 | } | |
7d60be94 | 1251 | dtype = build_int_cst (gfc_array_index_type, i); |
6de9cd9a DN |
1252 | |
1253 | if (size && !INTEGER_CST_P (size)) | |
1254 | { | |
7d60be94 | 1255 | tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT); |
ac816b02 RG |
1256 | tmp = fold_build2 (LSHIFT_EXPR, gfc_array_index_type, |
1257 | fold_convert (gfc_array_index_type, size), tmp); | |
10c7a96f | 1258 | dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype); |
6de9cd9a DN |
1259 | } |
1260 | /* If we don't know the size we leave it as zero. This should never happen | |
1261 | for anything that is actually used. */ | |
1262 | /* TODO: Check this is actually true, particularly when repacking | |
1263 | assumed size parameters. */ | |
1264 | ||
40b026d8 | 1265 | GFC_TYPE_ARRAY_DTYPE (type) = dtype; |
6de9cd9a DN |
1266 | return dtype; |
1267 | } | |
1268 | ||
1269 | ||
dcfef7d4 TS |
1270 | /* Build an array type for use without a descriptor, packed according |
1271 | to the value of PACKED. */ | |
6de9cd9a DN |
1272 | |
1273 | tree | |
dcfef7d4 | 1274 | gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed) |
6de9cd9a DN |
1275 | { |
1276 | tree range; | |
1277 | tree type; | |
1278 | tree tmp; | |
1279 | int n; | |
1280 | int known_stride; | |
1281 | int known_offset; | |
1282 | mpz_t offset; | |
1283 | mpz_t stride; | |
1284 | mpz_t delta; | |
1285 | gfc_expr *expr; | |
1286 | ||
1287 | mpz_init_set_ui (offset, 0); | |
1288 | mpz_init_set_ui (stride, 1); | |
1289 | mpz_init (delta); | |
1290 | ||
1291 | /* We don't use build_array_type because this does not include include | |
13795658 | 1292 | lang-specific information (i.e. the bounds of the array) when checking |
6de9cd9a DN |
1293 | for duplicates. */ |
1294 | type = make_node (ARRAY_TYPE); | |
1295 | ||
1296 | GFC_ARRAY_TYPE_P (type) = 1; | |
1297 | TYPE_LANG_SPECIFIC (type) = (struct lang_type *) | |
1298 | ggc_alloc_cleared (sizeof (struct lang_type)); | |
1299 | ||
dcfef7d4 | 1300 | known_stride = (packed != PACKED_NO); |
6de9cd9a DN |
1301 | known_offset = 1; |
1302 | for (n = 0; n < as->rank; n++) | |
1303 | { | |
1304 | /* Fill in the stride and bound components of the type. */ | |
1305 | if (known_stride) | |
08789087 | 1306 | tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); |
6de9cd9a DN |
1307 | else |
1308 | tmp = NULL_TREE; | |
1309 | GFC_TYPE_ARRAY_STRIDE (type, n) = tmp; | |
1310 | ||
1311 | expr = as->lower[n]; | |
1312 | if (expr->expr_type == EXPR_CONSTANT) | |
1313 | { | |
1314 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
fad0afd7 | 1315 | gfc_index_integer_kind); |
6de9cd9a DN |
1316 | } |
1317 | else | |
1318 | { | |
1319 | known_stride = 0; | |
1320 | tmp = NULL_TREE; | |
1321 | } | |
1322 | GFC_TYPE_ARRAY_LBOUND (type, n) = tmp; | |
1323 | ||
1324 | if (known_stride) | |
1325 | { | |
1326 | /* Calculate the offset. */ | |
1327 | mpz_mul (delta, stride, as->lower[n]->value.integer); | |
1328 | mpz_sub (offset, offset, delta); | |
1329 | } | |
1330 | else | |
1331 | known_offset = 0; | |
1332 | ||
1333 | expr = as->upper[n]; | |
1334 | if (expr && expr->expr_type == EXPR_CONSTANT) | |
1335 | { | |
1336 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
1337 | gfc_index_integer_kind); | |
1338 | } | |
1339 | else | |
1340 | { | |
1341 | tmp = NULL_TREE; | |
1342 | known_stride = 0; | |
1343 | } | |
1344 | GFC_TYPE_ARRAY_UBOUND (type, n) = tmp; | |
1345 | ||
1346 | if (known_stride) | |
1347 | { | |
1348 | /* Calculate the stride. */ | |
1349 | mpz_sub (delta, as->upper[n]->value.integer, | |
1350 | as->lower[n]->value.integer); | |
1351 | mpz_add_ui (delta, delta, 1); | |
1352 | mpz_mul (stride, stride, delta); | |
1353 | } | |
1354 | ||
1355 | /* Only the first stride is known for partial packed arrays. */ | |
dcfef7d4 | 1356 | if (packed == PACKED_NO || packed == PACKED_PARTIAL) |
6de9cd9a DN |
1357 | known_stride = 0; |
1358 | } | |
1359 | ||
1360 | if (known_offset) | |
1361 | { | |
1362 | GFC_TYPE_ARRAY_OFFSET (type) = | |
1363 | gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind); | |
1364 | } | |
1365 | else | |
1366 | GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE; | |
1367 | ||
1368 | if (known_stride) | |
1369 | { | |
1370 | GFC_TYPE_ARRAY_SIZE (type) = | |
1371 | gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); | |
1372 | } | |
1373 | else | |
1374 | GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE; | |
1375 | ||
6de9cd9a | 1376 | GFC_TYPE_ARRAY_RANK (type) = as->rank; |
40b026d8 | 1377 | GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE; |
7ab92584 | 1378 | range = build_range_type (gfc_array_index_type, gfc_index_zero_node, |
6de9cd9a DN |
1379 | NULL_TREE); |
1380 | /* TODO: use main type if it is unbounded. */ | |
1381 | GFC_TYPE_ARRAY_DATAPTR_TYPE (type) = | |
1382 | build_pointer_type (build_array_type (etype, range)); | |
1383 | ||
1384 | if (known_stride) | |
1385 | { | |
1386 | mpz_sub_ui (stride, stride, 1); | |
1387 | range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); | |
1388 | } | |
1389 | else | |
1390 | range = NULL_TREE; | |
1391 | ||
7ab92584 | 1392 | range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range); |
6de9cd9a DN |
1393 | TYPE_DOMAIN (type) = range; |
1394 | ||
1395 | build_pointer_type (etype); | |
1396 | TREE_TYPE (type) = etype; | |
1397 | ||
1398 | layout_type (type); | |
1399 | ||
1400 | mpz_clear (offset); | |
1401 | mpz_clear (stride); | |
1402 | mpz_clear (delta); | |
1403 | ||
09775c40 AO |
1404 | /* Represent packed arrays as multi-dimensional if they have rank > |
1405 | 1 and with proper bounds, instead of flat arrays. This makes for | |
1406 | better debug info. */ | |
1407 | if (known_offset) | |
08789087 JJ |
1408 | { |
1409 | tree gtype = etype, rtype, type_decl; | |
1410 | ||
1411 | for (n = as->rank - 1; n >= 0; n--) | |
1412 | { | |
1413 | rtype = build_range_type (gfc_array_index_type, | |
1414 | GFC_TYPE_ARRAY_LBOUND (type, n), | |
1415 | GFC_TYPE_ARRAY_UBOUND (type, n)); | |
1416 | gtype = build_array_type (gtype, rtype); | |
1417 | } | |
1418 | TYPE_NAME (type) = type_decl = build_decl (TYPE_DECL, NULL, gtype); | |
1419 | DECL_ORIGINAL_TYPE (type_decl) = gtype; | |
1420 | } | |
1421 | ||
dcfef7d4 | 1422 | if (packed != PACKED_STATIC || !known_stride) |
6de9cd9a | 1423 | { |
841b0c1f PB |
1424 | /* For dummy arrays and automatic (heap allocated) arrays we |
1425 | want a pointer to the array. */ | |
6de9cd9a DN |
1426 | type = build_pointer_type (type); |
1427 | GFC_ARRAY_TYPE_P (type) = 1; | |
1428 | TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type)); | |
1429 | } | |
1430 | return type; | |
1431 | } | |
1432 | ||
4c73896d RH |
1433 | /* Return or create the base type for an array descriptor. */ |
1434 | ||
1435 | static tree | |
1436 | gfc_get_array_descriptor_base (int dimen) | |
1437 | { | |
1438 | tree fat_type, fieldlist, decl, arraytype; | |
1439 | char name[16 + GFC_RANK_DIGITS + 1]; | |
1440 | ||
1441 | gcc_assert (dimen >= 1 && dimen <= GFC_MAX_DIMENSIONS); | |
1442 | if (gfc_array_descriptor_base[dimen - 1]) | |
1443 | return gfc_array_descriptor_base[dimen - 1]; | |
1444 | ||
1445 | /* Build the type node. */ | |
1446 | fat_type = make_node (RECORD_TYPE); | |
1447 | ||
1448 | sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen); | |
1449 | TYPE_NAME (fat_type) = get_identifier (name); | |
1450 | ||
1451 | /* Add the data member as the first element of the descriptor. */ | |
1452 | decl = build_decl (FIELD_DECL, get_identifier ("data"), ptr_type_node); | |
1453 | ||
1454 | DECL_CONTEXT (decl) = fat_type; | |
1455 | fieldlist = decl; | |
1456 | ||
1457 | /* Add the base component. */ | |
1458 | decl = build_decl (FIELD_DECL, get_identifier ("offset"), | |
1459 | gfc_array_index_type); | |
1460 | DECL_CONTEXT (decl) = fat_type; | |
d8eff1b8 | 1461 | TREE_NO_WARNING (decl) = 1; |
4c73896d RH |
1462 | fieldlist = chainon (fieldlist, decl); |
1463 | ||
1464 | /* Add the dtype component. */ | |
1465 | decl = build_decl (FIELD_DECL, get_identifier ("dtype"), | |
1466 | gfc_array_index_type); | |
1467 | DECL_CONTEXT (decl) = fat_type; | |
d8eff1b8 | 1468 | TREE_NO_WARNING (decl) = 1; |
4c73896d RH |
1469 | fieldlist = chainon (fieldlist, decl); |
1470 | ||
1471 | /* Build the array type for the stride and bound components. */ | |
1472 | arraytype = | |
1473 | build_array_type (gfc_get_desc_dim_type (), | |
1474 | build_range_type (gfc_array_index_type, | |
1475 | gfc_index_zero_node, | |
1476 | gfc_rank_cst[dimen - 1])); | |
1477 | ||
1478 | decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype); | |
1479 | DECL_CONTEXT (decl) = fat_type; | |
d8eff1b8 | 1480 | TREE_NO_WARNING (decl) = 1; |
4c73896d RH |
1481 | fieldlist = chainon (fieldlist, decl); |
1482 | ||
1483 | /* Finish off the type. */ | |
1484 | TYPE_FIELDS (fat_type) = fieldlist; | |
1485 | ||
1486 | gfc_finish_type (fat_type); | |
dfcf0b12 | 1487 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1; |
4c73896d RH |
1488 | |
1489 | gfc_array_descriptor_base[dimen - 1] = fat_type; | |
1490 | return fat_type; | |
1491 | } | |
6de9cd9a DN |
1492 | |
1493 | /* Build an array (descriptor) type with given bounds. */ | |
1494 | ||
1495 | tree | |
1496 | gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound, | |
fad0afd7 JJ |
1497 | tree * ubound, int packed, |
1498 | enum gfc_array_kind akind) | |
6de9cd9a | 1499 | { |
6de9cd9a | 1500 | char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN]; |
416a8af4 | 1501 | tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype; |
9aa433c2 | 1502 | const char *type_name; |
4c73896d | 1503 | int n; |
6de9cd9a | 1504 | |
4c73896d RH |
1505 | base_type = gfc_get_array_descriptor_base (dimen); |
1506 | fat_type = build_variant_type_copy (base_type); | |
6de9cd9a DN |
1507 | |
1508 | tmp = TYPE_NAME (etype); | |
1509 | if (tmp && TREE_CODE (tmp) == TYPE_DECL) | |
1510 | tmp = DECL_NAME (tmp); | |
1511 | if (tmp) | |
9aa433c2 | 1512 | type_name = IDENTIFIER_POINTER (tmp); |
6de9cd9a | 1513 | else |
9aa433c2 | 1514 | type_name = "unknown"; |
6de9cd9a | 1515 | sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen, |
9aa433c2 | 1516 | GFC_MAX_SYMBOL_LEN, type_name); |
6de9cd9a | 1517 | TYPE_NAME (fat_type) = get_identifier (name); |
6de9cd9a | 1518 | |
4c73896d RH |
1519 | GFC_DESCRIPTOR_TYPE_P (fat_type) = 1; |
1520 | TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *) | |
1521 | ggc_alloc_cleared (sizeof (struct lang_type)); | |
1522 | ||
1523 | GFC_TYPE_ARRAY_RANK (fat_type) = dimen; | |
1524 | GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE; | |
fad0afd7 | 1525 | GFC_TYPE_ARRAY_AKIND (fat_type) = akind; |
6de9cd9a DN |
1526 | |
1527 | /* Build an array descriptor record type. */ | |
1528 | if (packed != 0) | |
7ab92584 | 1529 | stride = gfc_index_one_node; |
6de9cd9a DN |
1530 | else |
1531 | stride = NULL_TREE; | |
6de9cd9a DN |
1532 | for (n = 0; n < dimen; n++) |
1533 | { | |
1534 | GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride; | |
1535 | ||
1536 | if (lbound) | |
1537 | lower = lbound[n]; | |
1538 | else | |
1539 | lower = NULL_TREE; | |
1540 | ||
1541 | if (lower != NULL_TREE) | |
1542 | { | |
1543 | if (INTEGER_CST_P (lower)) | |
1544 | GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower; | |
1545 | else | |
1546 | lower = NULL_TREE; | |
1547 | } | |
1548 | ||
1549 | upper = ubound[n]; | |
1550 | if (upper != NULL_TREE) | |
1551 | { | |
1552 | if (INTEGER_CST_P (upper)) | |
1553 | GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper; | |
1554 | else | |
1555 | upper = NULL_TREE; | |
1556 | } | |
1557 | ||
1558 | if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE) | |
1559 | { | |
10c7a96f SB |
1560 | tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower); |
1561 | tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, | |
1562 | gfc_index_one_node); | |
6de9cd9a | 1563 | stride = |
10c7a96f | 1564 | fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride); |
6de9cd9a | 1565 | /* Check the folding worked. */ |
6e45f57b | 1566 | gcc_assert (INTEGER_CST_P (stride)); |
6de9cd9a DN |
1567 | } |
1568 | else | |
1569 | stride = NULL_TREE; | |
1570 | } | |
1571 | GFC_TYPE_ARRAY_SIZE (fat_type) = stride; | |
4c73896d | 1572 | |
6de9cd9a DN |
1573 | /* TODO: known offsets for descriptors. */ |
1574 | GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE; | |
1575 | ||
416a8af4 RG |
1576 | /* We define data as an array with the correct size if possible. |
1577 | Much better than doing pointer arithmetic. */ | |
1578 | if (stride) | |
1579 | rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node, | |
1580 | int_const_binop (MINUS_EXPR, stride, | |
1581 | integer_one_node, 0)); | |
1582 | else | |
1583 | rtype = gfc_array_range_type; | |
1584 | arraytype = build_array_type (etype, rtype); | |
6de9cd9a DN |
1585 | arraytype = build_pointer_type (arraytype); |
1586 | GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype; | |
1587 | ||
6de9cd9a DN |
1588 | return fat_type; |
1589 | } | |
1590 | \f | |
1591 | /* Build a pointer type. This function is called from gfc_sym_type(). */ | |
c3e8c6b8 | 1592 | |
6de9cd9a DN |
1593 | static tree |
1594 | gfc_build_pointer_type (gfc_symbol * sym, tree type) | |
1595 | { | |
436529ea | 1596 | /* Array pointer types aren't actually pointers. */ |
6de9cd9a DN |
1597 | if (sym->attr.dimension) |
1598 | return type; | |
1599 | else | |
1600 | return build_pointer_type (type); | |
1601 | } | |
1602 | \f | |
1603 | /* Return the type for a symbol. Special handling is required for character | |
1604 | types to get the correct level of indirection. | |
1605 | For functions return the return type. | |
ad6e2a18 TS |
1606 | For subroutines return void_type_node. |
1607 | Calling this multiple times for the same symbol should be avoided, | |
1608 | especially for character and array types. */ | |
c3e8c6b8 | 1609 | |
6de9cd9a DN |
1610 | tree |
1611 | gfc_sym_type (gfc_symbol * sym) | |
1612 | { | |
1613 | tree type; | |
1614 | int byref; | |
1615 | ||
726d8566 JW |
1616 | /* Procedure Pointers inside COMMON blocks or as function result. */ |
1617 | if (sym->attr.proc_pointer && (sym->attr.in_common || sym->attr.result)) | |
00625fae JW |
1618 | { |
1619 | /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */ | |
1620 | sym->attr.proc_pointer = 0; | |
1621 | type = build_pointer_type (gfc_get_function_type (sym)); | |
1622 | sym->attr.proc_pointer = 1; | |
1623 | return type; | |
1624 | } | |
1625 | ||
6de9cd9a DN |
1626 | if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function) |
1627 | return void_type_node; | |
1628 | ||
da4c6ed8 TS |
1629 | /* In the case of a function the fake result variable may have a |
1630 | type different from the function type, so don't return early in | |
1631 | that case. */ | |
1632 | if (sym->backend_decl && !sym->attr.function) | |
1633 | return TREE_TYPE (sym->backend_decl); | |
6de9cd9a | 1634 | |
665733c1 JJ |
1635 | if (sym->ts.type == BT_CHARACTER |
1636 | && ((sym->attr.function && sym->attr.is_bind_c) | |
1637 | || (sym->attr.result | |
1638 | && sym->ns->proc_name | |
1639 | && sym->ns->proc_name->attr.is_bind_c))) | |
06a54338 TB |
1640 | type = gfc_character1_type_node; |
1641 | else | |
1642 | type = gfc_typenode_for_spec (&sym->ts); | |
6de9cd9a | 1643 | |
06469efd | 1644 | if (sym->attr.dummy && !sym->attr.function && !sym->attr.value) |
6de9cd9a DN |
1645 | byref = 1; |
1646 | else | |
1647 | byref = 0; | |
1648 | ||
1649 | if (sym->attr.dimension) | |
1650 | { | |
1651 | if (gfc_is_nodesc_array (sym)) | |
1652 | { | |
1653 | /* If this is a character argument of unknown length, just use the | |
1654 | base type. */ | |
1655 | if (sym->ts.type != BT_CHARACTER | |
b49a3de7 | 1656 | || !(sym->attr.dummy || sym->attr.function) |
6de9cd9a DN |
1657 | || sym->ts.cl->backend_decl) |
1658 | { | |
1659 | type = gfc_get_nodesc_array_type (type, sym->as, | |
dcfef7d4 TS |
1660 | byref ? PACKED_FULL |
1661 | : PACKED_STATIC); | |
6de9cd9a DN |
1662 | byref = 0; |
1663 | } | |
1664 | } | |
1665 | else | |
fad0afd7 JJ |
1666 | { |
1667 | enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN; | |
1668 | if (sym->attr.pointer) | |
1669 | akind = GFC_ARRAY_POINTER; | |
1670 | else if (sym->attr.allocatable) | |
1671 | akind = GFC_ARRAY_ALLOCATABLE; | |
1672 | type = gfc_build_array_type (type, sym->as, akind); | |
1673 | } | |
a8b3b0b6 | 1674 | } |
6de9cd9a DN |
1675 | else |
1676 | { | |
1677 | if (sym->attr.allocatable || sym->attr.pointer) | |
1678 | type = gfc_build_pointer_type (sym, type); | |
e1c82219 JJ |
1679 | if (sym->attr.pointer) |
1680 | GFC_POINTER_TYPE_P (type) = 1; | |
6de9cd9a DN |
1681 | } |
1682 | ||
1683 | /* We currently pass all parameters by reference. | |
1684 | See f95_get_function_decl. For dummy function parameters return the | |
1685 | function type. */ | |
1686 | if (byref) | |
1619aa6f PB |
1687 | { |
1688 | /* We must use pointer types for potentially absent variables. The | |
1689 | optimizers assume a reference type argument is never NULL. */ | |
1690 | if (sym->attr.optional || sym->ns->proc_name->attr.entry_master) | |
1691 | type = build_pointer_type (type); | |
1692 | else | |
1693 | type = build_reference_type (type); | |
1694 | } | |
6de9cd9a DN |
1695 | |
1696 | return (type); | |
1697 | } | |
1698 | \f | |
1699 | /* Layout and output debug info for a record type. */ | |
c3e8c6b8 | 1700 | |
6de9cd9a DN |
1701 | void |
1702 | gfc_finish_type (tree type) | |
1703 | { | |
1704 | tree decl; | |
1705 | ||
1706 | decl = build_decl (TYPE_DECL, NULL_TREE, type); | |
1707 | TYPE_STUB_DECL (type) = decl; | |
1708 | layout_type (type); | |
1709 | rest_of_type_compilation (type, 1); | |
0e6df31e | 1710 | rest_of_decl_compilation (decl, 1, 0); |
6de9cd9a DN |
1711 | } |
1712 | \f | |
1713 | /* Add a field of given NAME and TYPE to the context of a UNION_TYPE | |
1714 | or RECORD_TYPE pointed to by STYPE. The new field is chained | |
1715 | to the fieldlist pointed to by FIELDLIST. | |
1716 | ||
1717 | Returns a pointer to the new field. */ | |
c3e8c6b8 | 1718 | |
6de9cd9a DN |
1719 | tree |
1720 | gfc_add_field_to_struct (tree *fieldlist, tree context, | |
1721 | tree name, tree type) | |
1722 | { | |
1723 | tree decl; | |
1724 | ||
1725 | decl = build_decl (FIELD_DECL, name, type); | |
1726 | ||
1727 | DECL_CONTEXT (decl) = context; | |
1728 | DECL_INITIAL (decl) = 0; | |
1729 | DECL_ALIGN (decl) = 0; | |
1730 | DECL_USER_ALIGN (decl) = 0; | |
1731 | TREE_CHAIN (decl) = NULL_TREE; | |
1732 | *fieldlist = chainon (*fieldlist, decl); | |
1733 | ||
1734 | return decl; | |
1735 | } | |
1736 | ||
1737 | ||
6b887797 PT |
1738 | /* Copy the backend_decl and component backend_decls if |
1739 | the two derived type symbols are "equal", as described | |
1740 | in 4.4.2 and resolved by gfc_compare_derived_types. */ | |
1741 | ||
1742 | static int | |
1743 | copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to) | |
1744 | { | |
1745 | gfc_component *to_cm; | |
1746 | gfc_component *from_cm; | |
1747 | ||
1748 | if (from->backend_decl == NULL | |
1749 | || !gfc_compare_derived_types (from, to)) | |
1750 | return 0; | |
1751 | ||
1752 | to->backend_decl = from->backend_decl; | |
1753 | ||
1754 | to_cm = to->components; | |
1755 | from_cm = from->components; | |
1756 | ||
1757 | /* Copy the component declarations. If a component is itself | |
1758 | a derived type, we need a copy of its component declarations. | |
1759 | This is done by recursing into gfc_get_derived_type and | |
1760 | ensures that the component's component declarations have | |
1761 | been built. If it is a character, we need the character | |
1762 | length, as well. */ | |
1763 | for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next) | |
1764 | { | |
1765 | to_cm->backend_decl = from_cm->backend_decl; | |
d4b7d0f0 | 1766 | if (!from_cm->attr.pointer && from_cm->ts.type == BT_DERIVED) |
6b887797 PT |
1767 | gfc_get_derived_type (to_cm->ts.derived); |
1768 | ||
1769 | else if (from_cm->ts.type == BT_CHARACTER) | |
1770 | to_cm->ts.cl->backend_decl = from_cm->ts.cl->backend_decl; | |
1771 | } | |
1772 | ||
1773 | return 1; | |
1774 | } | |
1775 | ||
1776 | ||
1777 | /* Build a tree node for a derived type. If there are equal | |
1778 | derived types, with different local names, these are built | |
1779 | at the same time. If an equal derived type has been built | |
1780 | in a parent namespace, this is used. */ | |
c3e8c6b8 | 1781 | |
6de9cd9a DN |
1782 | static tree |
1783 | gfc_get_derived_type (gfc_symbol * derived) | |
1784 | { | |
a8b3b0b6 | 1785 | tree typenode = NULL, field = NULL, field_type = NULL, fieldlist = NULL; |
6de9cd9a | 1786 | gfc_component *c; |
6b887797 | 1787 | gfc_dt_list *dt; |
6de9cd9a | 1788 | |
6b887797 | 1789 | gcc_assert (derived && derived->attr.flavor == FL_DERIVED); |
6de9cd9a | 1790 | |
a8b3b0b6 CR |
1791 | /* See if it's one of the iso_c_binding derived types. */ |
1792 | if (derived->attr.is_iso_c == 1) | |
1793 | { | |
9dc35956 CR |
1794 | if (derived->backend_decl) |
1795 | return derived->backend_decl; | |
1796 | ||
089db47d CR |
1797 | if (derived->intmod_sym_id == ISOCBINDING_PTR) |
1798 | derived->backend_decl = ptr_type_node; | |
1799 | else | |
1800 | derived->backend_decl = pfunc_type_node; | |
9dc35956 CR |
1801 | |
1802 | /* Create a backend_decl for the __c_ptr_c_address field. */ | |
1803 | derived->components->backend_decl = | |
1804 | gfc_add_field_to_struct (&(derived->backend_decl->type.values), | |
1805 | derived->backend_decl, | |
1806 | get_identifier (derived->components->name), | |
1807 | gfc_typenode_for_spec ( | |
1808 | &(derived->components->ts))); | |
1809 | ||
a8b3b0b6 CR |
1810 | derived->ts.kind = gfc_index_integer_kind; |
1811 | derived->ts.type = BT_INTEGER; | |
1812 | /* Set the f90_type to BT_VOID as a way to recognize something of type | |
1813 | BT_INTEGER that needs to fit a void * for the purpose of the | |
1814 | iso_c_binding derived types. */ | |
1815 | derived->ts.f90_type = BT_VOID; | |
9dc35956 | 1816 | |
a8b3b0b6 CR |
1817 | return derived->backend_decl; |
1818 | } | |
1819 | ||
6de9cd9a | 1820 | /* derived->backend_decl != 0 means we saw it before, but its |
436529ea | 1821 | components' backend_decl may have not been built. */ |
6de9cd9a DN |
1822 | if (derived->backend_decl) |
1823 | { | |
436529ea | 1824 | /* Its components' backend_decl have been built. */ |
6de9cd9a DN |
1825 | if (TYPE_FIELDS (derived->backend_decl)) |
1826 | return derived->backend_decl; | |
1827 | else | |
1828 | typenode = derived->backend_decl; | |
1829 | } | |
1830 | else | |
1831 | { | |
6b887797 | 1832 | |
6de9cd9a DN |
1833 | /* We see this derived type first time, so build the type node. */ |
1834 | typenode = make_node (RECORD_TYPE); | |
1835 | TYPE_NAME (typenode) = get_identifier (derived->name); | |
1836 | TYPE_PACKED (typenode) = gfc_option.flag_pack_derived; | |
1837 | derived->backend_decl = typenode; | |
1838 | } | |
1839 | ||
bce71376 PT |
1840 | /* Go through the derived type components, building them as |
1841 | necessary. The reason for doing this now is that it is | |
1842 | possible to recurse back to this derived type through a | |
1843 | pointer component (PR24092). If this happens, the fields | |
1844 | will be built and so we can return the type. */ | |
1845 | for (c = derived->components; c; c = c->next) | |
1846 | { | |
1847 | if (c->ts.type != BT_DERIVED) | |
1848 | continue; | |
1849 | ||
d4b7d0f0 | 1850 | if (!c->attr.pointer || c->ts.derived->backend_decl == NULL) |
bce71376 | 1851 | c->ts.derived->backend_decl = gfc_get_derived_type (c->ts.derived); |
a8b3b0b6 CR |
1852 | |
1853 | if (c->ts.derived && c->ts.derived->attr.is_iso_c) | |
1854 | { | |
1855 | /* Need to copy the modified ts from the derived type. The | |
1856 | typespec was modified because C_PTR/C_FUNPTR are translated | |
1857 | into (void *) from derived types. */ | |
1858 | c->ts.type = c->ts.derived->ts.type; | |
1859 | c->ts.kind = c->ts.derived->ts.kind; | |
1860 | c->ts.f90_type = c->ts.derived->ts.f90_type; | |
9dc35956 CR |
1861 | if (c->initializer) |
1862 | { | |
1863 | c->initializer->ts.type = c->ts.type; | |
1864 | c->initializer->ts.kind = c->ts.kind; | |
1865 | c->initializer->ts.f90_type = c->ts.f90_type; | |
1866 | c->initializer->expr_type = EXPR_NULL; | |
1867 | } | |
a8b3b0b6 | 1868 | } |
bce71376 PT |
1869 | } |
1870 | ||
1871 | if (TYPE_FIELDS (derived->backend_decl)) | |
1872 | return derived->backend_decl; | |
1873 | ||
6de9cd9a DN |
1874 | /* Build the type member list. Install the newly created RECORD_TYPE |
1875 | node as DECL_CONTEXT of each FIELD_DECL. */ | |
1876 | fieldlist = NULL_TREE; | |
1877 | for (c = derived->components; c; c = c->next) | |
1878 | { | |
bce71376 PT |
1879 | if (c->ts.type == BT_DERIVED) |
1880 | field_type = c->ts.derived->backend_decl; | |
6de9cd9a DN |
1881 | else |
1882 | { | |
1883 | if (c->ts.type == BT_CHARACTER) | |
1884 | { | |
1885 | /* Evaluate the string length. */ | |
1886 | gfc_conv_const_charlen (c->ts.cl); | |
6e45f57b | 1887 | gcc_assert (c->ts.cl->backend_decl); |
6de9cd9a DN |
1888 | } |
1889 | ||
1890 | field_type = gfc_typenode_for_spec (&c->ts); | |
1891 | } | |
1892 | ||
1f2959f0 | 1893 | /* This returns an array descriptor type. Initialization may be |
6de9cd9a | 1894 | required. */ |
d4b7d0f0 | 1895 | if (c->attr.dimension) |
6de9cd9a | 1896 | { |
d4b7d0f0 | 1897 | if (c->attr.pointer || c->attr.allocatable) |
6de9cd9a | 1898 | { |
fad0afd7 | 1899 | enum gfc_array_kind akind; |
d4b7d0f0 | 1900 | if (c->attr.pointer) |
fad0afd7 JJ |
1901 | akind = GFC_ARRAY_POINTER; |
1902 | else | |
1903 | akind = GFC_ARRAY_ALLOCATABLE; | |
1f2959f0 | 1904 | /* Pointers to arrays aren't actually pointer types. The |
e7dc5b4f | 1905 | descriptors are separate, but the data is common. */ |
fad0afd7 | 1906 | field_type = gfc_build_array_type (field_type, c->as, akind); |
6de9cd9a DN |
1907 | } |
1908 | else | |
dcfef7d4 TS |
1909 | field_type = gfc_get_nodesc_array_type (field_type, c->as, |
1910 | PACKED_STATIC); | |
6de9cd9a | 1911 | } |
d4b7d0f0 | 1912 | else if (c->attr.pointer) |
6de9cd9a DN |
1913 | field_type = build_pointer_type (field_type); |
1914 | ||
1915 | field = gfc_add_field_to_struct (&fieldlist, typenode, | |
1916 | get_identifier (c->name), | |
1917 | field_type); | |
dfcf0b12 FXC |
1918 | if (c->loc.lb) |
1919 | gfc_set_decl_location (field, &c->loc); | |
1920 | else if (derived->declared_at.lb) | |
1921 | gfc_set_decl_location (field, &derived->declared_at); | |
6de9cd9a DN |
1922 | |
1923 | DECL_PACKED (field) |= TYPE_PACKED (typenode); | |
1924 | ||
bce71376 PT |
1925 | gcc_assert (field); |
1926 | if (!c->backend_decl) | |
1927 | c->backend_decl = field; | |
6de9cd9a DN |
1928 | } |
1929 | ||
1930 | /* Now we have the final fieldlist. Record it, then lay out the | |
1931 | derived type, including the fields. */ | |
1932 | TYPE_FIELDS (typenode) = fieldlist; | |
1933 | ||
1934 | gfc_finish_type (typenode); | |
dfcf0b12 | 1935 | gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at); |
96ffc6cd TK |
1936 | if (derived->module && derived->ns->proc_name |
1937 | && derived->ns->proc_name->attr.flavor == FL_MODULE) | |
a64f5186 JJ |
1938 | { |
1939 | if (derived->ns->proc_name->backend_decl | |
1940 | && TREE_CODE (derived->ns->proc_name->backend_decl) | |
1941 | == NAMESPACE_DECL) | |
1942 | { | |
1943 | TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl; | |
1944 | DECL_CONTEXT (TYPE_STUB_DECL (typenode)) | |
1945 | = derived->ns->proc_name->backend_decl; | |
1946 | } | |
1947 | } | |
6de9cd9a DN |
1948 | |
1949 | derived->backend_decl = typenode; | |
1950 | ||
a64f5186 JJ |
1951 | /* Add this backend_decl to all the other, equal derived types. */ |
1952 | for (dt = gfc_derived_types; dt; dt = dt->next) | |
1953 | copy_dt_decls_ifequal (derived, dt->derived); | |
6b887797 | 1954 | |
e0e85e06 | 1955 | return derived->backend_decl; |
6de9cd9a | 1956 | } |
e0e85e06 PT |
1957 | |
1958 | ||
6de9cd9a DN |
1959 | int |
1960 | gfc_return_by_reference (gfc_symbol * sym) | |
1961 | { | |
1962 | if (!sym->attr.function) | |
1963 | return 0; | |
1964 | ||
b49a3de7 | 1965 | if (sym->attr.dimension) |
6de9cd9a DN |
1966 | return 1; |
1967 | ||
665733c1 JJ |
1968 | if (sym->ts.type == BT_CHARACTER |
1969 | && !sym->attr.is_bind_c | |
1970 | && (!sym->attr.result | |
1971 | || !sym->ns->proc_name | |
1972 | || !sym->ns->proc_name->attr.is_bind_c)) | |
6de9cd9a DN |
1973 | return 1; |
1974 | ||
973ff4c0 TS |
1975 | /* Possibly return complex numbers by reference for g77 compatibility. |
1976 | We don't do this for calls to intrinsics (as the library uses the | |
1977 | -fno-f2c calling convention), nor for calls to functions which always | |
1978 | require an explicit interface, as no compatibility problems can | |
1979 | arise there. */ | |
1980 | if (gfc_option.flag_f2c | |
b49a3de7 | 1981 | && sym->ts.type == BT_COMPLEX |
973ff4c0 TS |
1982 | && !sym->attr.intrinsic && !sym->attr.always_explicit) |
1983 | return 1; | |
66e4ab31 | 1984 | |
6de9cd9a DN |
1985 | return 0; |
1986 | } | |
1987 | \f | |
d198b59a JJ |
1988 | static tree |
1989 | gfc_get_mixed_entry_union (gfc_namespace *ns) | |
1990 | { | |
1991 | tree type; | |
1992 | tree decl; | |
1993 | tree fieldlist; | |
1994 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
1995 | gfc_entry_list *el, *el2; | |
1996 | ||
1997 | gcc_assert (ns->proc_name->attr.mixed_entry_master); | |
1998 | gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0); | |
1999 | ||
2000 | snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7); | |
2001 | ||
2002 | /* Build the type node. */ | |
2003 | type = make_node (UNION_TYPE); | |
2004 | ||
2005 | TYPE_NAME (type) = get_identifier (name); | |
2006 | fieldlist = NULL; | |
2007 | ||
2008 | for (el = ns->entries; el; el = el->next) | |
2009 | { | |
2010 | /* Search for duplicates. */ | |
2011 | for (el2 = ns->entries; el2 != el; el2 = el2->next) | |
2012 | if (el2->sym->result == el->sym->result) | |
2013 | break; | |
2014 | ||
2015 | if (el == el2) | |
2016 | { | |
2017 | decl = build_decl (FIELD_DECL, | |
2018 | get_identifier (el->sym->result->name), | |
2019 | gfc_sym_type (el->sym->result)); | |
2020 | DECL_CONTEXT (decl) = type; | |
2021 | fieldlist = chainon (fieldlist, decl); | |
2022 | } | |
2023 | } | |
2024 | ||
2025 | /* Finish off the type. */ | |
2026 | TYPE_FIELDS (type) = fieldlist; | |
2027 | ||
2028 | gfc_finish_type (type); | |
dfcf0b12 | 2029 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1; |
d198b59a JJ |
2030 | return type; |
2031 | } | |
2032 | \f | |
6de9cd9a DN |
2033 | tree |
2034 | gfc_get_function_type (gfc_symbol * sym) | |
2035 | { | |
2036 | tree type; | |
2037 | tree typelist; | |
2038 | gfc_formal_arglist *f; | |
2039 | gfc_symbol *arg; | |
2040 | int nstr; | |
2041 | int alternate_return; | |
2042 | ||
ecf24057 FXC |
2043 | /* Make sure this symbol is a function, a subroutine or the main |
2044 | program. */ | |
2045 | gcc_assert (sym->attr.flavor == FL_PROCEDURE | |
2046 | || sym->attr.flavor == FL_PROGRAM); | |
6de9cd9a DN |
2047 | |
2048 | if (sym->backend_decl) | |
2049 | return TREE_TYPE (sym->backend_decl); | |
2050 | ||
2051 | nstr = 0; | |
2052 | alternate_return = 0; | |
2053 | typelist = NULL_TREE; | |
3d79abbd PB |
2054 | |
2055 | if (sym->attr.entry_master) | |
2056 | { | |
2057 | /* Additional parameter for selecting an entry point. */ | |
2058 | typelist = gfc_chainon_list (typelist, gfc_array_index_type); | |
2059 | } | |
2060 | ||
bfd61955 TB |
2061 | if (sym->result) |
2062 | arg = sym->result; | |
2063 | else | |
2064 | arg = sym; | |
2065 | ||
2066 | if (arg->ts.type == BT_CHARACTER) | |
2067 | gfc_conv_const_charlen (arg->ts.cl); | |
2068 | ||
6de9cd9a DN |
2069 | /* Some functions we use an extra parameter for the return value. */ |
2070 | if (gfc_return_by_reference (sym)) | |
2071 | { | |
6de9cd9a | 2072 | type = gfc_sym_type (arg); |
973ff4c0 | 2073 | if (arg->ts.type == BT_COMPLEX |
6de9cd9a DN |
2074 | || arg->attr.dimension |
2075 | || arg->ts.type == BT_CHARACTER) | |
2076 | type = build_reference_type (type); | |
2077 | ||
2078 | typelist = gfc_chainon_list (typelist, type); | |
2079 | if (arg->ts.type == BT_CHARACTER) | |
d7177ab2 | 2080 | typelist = gfc_chainon_list (typelist, gfc_charlen_type_node); |
6de9cd9a DN |
2081 | } |
2082 | ||
436529ea | 2083 | /* Build the argument types for the function. */ |
6de9cd9a DN |
2084 | for (f = sym->formal; f; f = f->next) |
2085 | { | |
2086 | arg = f->sym; | |
2087 | if (arg) | |
2088 | { | |
2089 | /* Evaluate constant character lengths here so that they can be | |
2090 | included in the type. */ | |
2091 | if (arg->ts.type == BT_CHARACTER) | |
2092 | gfc_conv_const_charlen (arg->ts.cl); | |
2093 | ||
2094 | if (arg->attr.flavor == FL_PROCEDURE) | |
2095 | { | |
2096 | type = gfc_get_function_type (arg); | |
2097 | type = build_pointer_type (type); | |
2098 | } | |
2099 | else | |
2100 | type = gfc_sym_type (arg); | |
2101 | ||
2102 | /* Parameter Passing Convention | |
2103 | ||
2104 | We currently pass all parameters by reference. | |
2105 | Parameters with INTENT(IN) could be passed by value. | |
2106 | The problem arises if a function is called via an implicit | |
2107 | prototype. In this situation the INTENT is not known. | |
2108 | For this reason all parameters to global functions must be | |
aa9c57ec | 2109 | passed by reference. Passing by value would potentially |
6de9cd9a | 2110 | generate bad code. Worse there would be no way of telling that |
c3e8c6b8 | 2111 | this code was bad, except that it would give incorrect results. |
6de9cd9a DN |
2112 | |
2113 | Contained procedures could pass by value as these are never | |
e2ae1407 | 2114 | used without an explicit interface, and cannot be passed as |
c3e8c6b8 | 2115 | actual parameters for a dummy procedure. */ |
6de9cd9a DN |
2116 | if (arg->ts.type == BT_CHARACTER) |
2117 | nstr++; | |
2118 | typelist = gfc_chainon_list (typelist, type); | |
2119 | } | |
2120 | else | |
2121 | { | |
2122 | if (sym->attr.subroutine) | |
2123 | alternate_return = 1; | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | /* Add hidden string length parameters. */ | |
2128 | while (nstr--) | |
d7177ab2 | 2129 | typelist = gfc_chainon_list (typelist, gfc_charlen_type_node); |
6de9cd9a | 2130 | |
884d2e6b SE |
2131 | if (typelist) |
2132 | typelist = gfc_chainon_list (typelist, void_type_node); | |
6de9cd9a DN |
2133 | |
2134 | if (alternate_return) | |
2135 | type = integer_type_node; | |
2136 | else if (!sym->attr.function || gfc_return_by_reference (sym)) | |
2137 | type = void_type_node; | |
d198b59a JJ |
2138 | else if (sym->attr.mixed_entry_master) |
2139 | type = gfc_get_mixed_entry_union (sym->ns); | |
da4c6ed8 TS |
2140 | else if (gfc_option.flag_f2c |
2141 | && sym->ts.type == BT_REAL | |
2142 | && sym->ts.kind == gfc_default_real_kind | |
2143 | && !sym->attr.always_explicit) | |
2144 | { | |
2145 | /* Special case: f2c calling conventions require that (scalar) | |
2146 | default REAL functions return the C type double instead. f2c | |
2147 | compatibility is only an issue with functions that don't | |
2148 | require an explicit interface, as only these could be | |
2149 | implemented in Fortran 77. */ | |
2150 | sym->ts.kind = gfc_default_double_kind; | |
2151 | type = gfc_typenode_for_spec (&sym->ts); | |
2152 | sym->ts.kind = gfc_default_real_kind; | |
2153 | } | |
726d8566 JW |
2154 | else if (sym->result && sym->result->attr.proc_pointer) |
2155 | /* Procedure pointer return values. */ | |
2156 | type = gfc_sym_type (sym->result); | |
6de9cd9a DN |
2157 | else |
2158 | type = gfc_sym_type (sym); | |
2159 | ||
2160 | type = build_function_type (type, typelist); | |
2161 | ||
2162 | return type; | |
2163 | } | |
2164 | \f | |
e2cad04b | 2165 | /* Language hooks for middle-end access to type nodes. */ |
6de9cd9a DN |
2166 | |
2167 | /* Return an integer type with BITS bits of precision, | |
2168 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
2169 | ||
2170 | tree | |
2171 | gfc_type_for_size (unsigned bits, int unsignedp) | |
2172 | { | |
e2cad04b RH |
2173 | if (!unsignedp) |
2174 | { | |
2175 | int i; | |
2176 | for (i = 0; i <= MAX_INT_KINDS; ++i) | |
2177 | { | |
2178 | tree type = gfc_integer_types[i]; | |
2179 | if (type && bits == TYPE_PRECISION (type)) | |
2180 | return type; | |
2181 | } | |
5218394a PB |
2182 | |
2183 | /* Handle TImode as a special case because it is used by some backends | |
df2fba9e | 2184 | (e.g. ARM) even though it is not available for normal use. */ |
5218394a PB |
2185 | #if HOST_BITS_PER_WIDE_INT >= 64 |
2186 | if (bits == TYPE_PRECISION (intTI_type_node)) | |
2187 | return intTI_type_node; | |
2188 | #endif | |
e2cad04b RH |
2189 | } |
2190 | else | |
2191 | { | |
2192 | if (bits == TYPE_PRECISION (unsigned_intQI_type_node)) | |
2193 | return unsigned_intQI_type_node; | |
2194 | if (bits == TYPE_PRECISION (unsigned_intHI_type_node)) | |
2195 | return unsigned_intHI_type_node; | |
2196 | if (bits == TYPE_PRECISION (unsigned_intSI_type_node)) | |
2197 | return unsigned_intSI_type_node; | |
2198 | if (bits == TYPE_PRECISION (unsigned_intDI_type_node)) | |
2199 | return unsigned_intDI_type_node; | |
2200 | if (bits == TYPE_PRECISION (unsigned_intTI_type_node)) | |
2201 | return unsigned_intTI_type_node; | |
2202 | } | |
6de9cd9a | 2203 | |
e2cad04b | 2204 | return NULL_TREE; |
6de9cd9a DN |
2205 | } |
2206 | ||
e2cad04b RH |
2207 | /* Return a data type that has machine mode MODE. If the mode is an |
2208 | integer, then UNSIGNEDP selects between signed and unsigned types. */ | |
6de9cd9a DN |
2209 | |
2210 | tree | |
2211 | gfc_type_for_mode (enum machine_mode mode, int unsignedp) | |
2212 | { | |
e2cad04b RH |
2213 | int i; |
2214 | tree *base; | |
2215 | ||
2216 | if (GET_MODE_CLASS (mode) == MODE_FLOAT) | |
2217 | base = gfc_real_types; | |
2218 | else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT) | |
2219 | base = gfc_complex_types; | |
2220 | else if (SCALAR_INT_MODE_P (mode)) | |
2221 | return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp); | |
2222 | else if (VECTOR_MODE_P (mode)) | |
6de9cd9a | 2223 | { |
f676971a EC |
2224 | enum machine_mode inner_mode = GET_MODE_INNER (mode); |
2225 | tree inner_type = gfc_type_for_mode (inner_mode, unsignedp); | |
2226 | if (inner_type != NULL_TREE) | |
2227 | return build_vector_type_for_mode (inner_type, mode); | |
e2cad04b | 2228 | return NULL_TREE; |
6de9cd9a | 2229 | } |
e2cad04b | 2230 | else |
1a5ffec4 | 2231 | return NULL_TREE; |
6de9cd9a | 2232 | |
e2cad04b RH |
2233 | for (i = 0; i <= MAX_REAL_KINDS; ++i) |
2234 | { | |
2235 | tree type = base[i]; | |
2236 | if (type && mode == TYPE_MODE (type)) | |
2237 | return type; | |
2238 | } | |
2239 | ||
2240 | return NULL_TREE; | |
2241 | } | |
2242 | ||
fad0afd7 JJ |
2243 | /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO |
2244 | in that case. */ | |
2245 | ||
2246 | bool | |
2247 | gfc_get_array_descr_info (const_tree type, struct array_descr_info *info) | |
2248 | { | |
2249 | int rank, dim; | |
2250 | bool indirect = false; | |
2251 | tree etype, ptype, field, t, base_decl; | |
2252 | tree data_off, offset_off, dim_off, dim_size, elem_size; | |
2253 | tree lower_suboff, upper_suboff, stride_suboff; | |
2254 | ||
2255 | if (! GFC_DESCRIPTOR_TYPE_P (type)) | |
2256 | { | |
2257 | if (! POINTER_TYPE_P (type)) | |
2258 | return false; | |
2259 | type = TREE_TYPE (type); | |
2260 | if (! GFC_DESCRIPTOR_TYPE_P (type)) | |
2261 | return false; | |
2262 | indirect = true; | |
2263 | } | |
2264 | ||
2265 | rank = GFC_TYPE_ARRAY_RANK (type); | |
2266 | if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0]))) | |
2267 | return false; | |
2268 | ||
2269 | etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type); | |
2270 | gcc_assert (POINTER_TYPE_P (etype)); | |
2271 | etype = TREE_TYPE (etype); | |
2272 | gcc_assert (TREE_CODE (etype) == ARRAY_TYPE); | |
2273 | etype = TREE_TYPE (etype); | |
2274 | /* Can't handle variable sized elements yet. */ | |
2275 | if (int_size_in_bytes (etype) <= 0) | |
2276 | return false; | |
2277 | /* Nor non-constant lower bounds in assumed shape arrays. */ | |
2278 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE) | |
2279 | { | |
2280 | for (dim = 0; dim < rank; dim++) | |
2281 | if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE | |
2282 | || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST) | |
2283 | return false; | |
2284 | } | |
2285 | ||
2286 | memset (info, '\0', sizeof (*info)); | |
2287 | info->ndimensions = rank; | |
2288 | info->element_type = etype; | |
2289 | ptype = build_pointer_type (gfc_array_index_type); | |
2290 | if (indirect) | |
2291 | { | |
2292 | info->base_decl = build_decl (VAR_DECL, NULL_TREE, | |
2293 | build_pointer_type (ptype)); | |
2294 | base_decl = build1 (INDIRECT_REF, ptype, info->base_decl); | |
2295 | } | |
2296 | else | |
2297 | info->base_decl = base_decl = build_decl (VAR_DECL, NULL_TREE, ptype); | |
2298 | ||
de870512 JJ |
2299 | if (GFC_TYPE_ARRAY_SPAN (type)) |
2300 | elem_size = GFC_TYPE_ARRAY_SPAN (type); | |
2301 | else | |
2302 | elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype)); | |
fad0afd7 JJ |
2303 | field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type)); |
2304 | data_off = byte_position (field); | |
2305 | field = TREE_CHAIN (field); | |
2306 | offset_off = byte_position (field); | |
2307 | field = TREE_CHAIN (field); | |
2308 | field = TREE_CHAIN (field); | |
2309 | dim_off = byte_position (field); | |
2310 | dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field))); | |
2311 | field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field))); | |
2312 | stride_suboff = byte_position (field); | |
2313 | field = TREE_CHAIN (field); | |
2314 | lower_suboff = byte_position (field); | |
2315 | field = TREE_CHAIN (field); | |
2316 | upper_suboff = byte_position (field); | |
2317 | ||
2318 | t = base_decl; | |
2319 | if (!integer_zerop (data_off)) | |
2320 | t = build2 (POINTER_PLUS_EXPR, ptype, t, data_off); | |
2321 | t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t); | |
2322 | info->data_location = build1 (INDIRECT_REF, ptr_type_node, t); | |
2323 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE) | |
2324 | info->allocated = build2 (NE_EXPR, boolean_type_node, | |
2325 | info->data_location, null_pointer_node); | |
2326 | else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER) | |
2327 | info->associated = build2 (NE_EXPR, boolean_type_node, | |
2328 | info->data_location, null_pointer_node); | |
2329 | ||
2330 | for (dim = 0; dim < rank; dim++) | |
2331 | { | |
2332 | t = build2 (POINTER_PLUS_EXPR, ptype, base_decl, | |
2333 | size_binop (PLUS_EXPR, dim_off, lower_suboff)); | |
2334 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); | |
2335 | info->dimen[dim].lower_bound = t; | |
2336 | t = build2 (POINTER_PLUS_EXPR, ptype, base_decl, | |
2337 | size_binop (PLUS_EXPR, dim_off, upper_suboff)); | |
2338 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); | |
2339 | info->dimen[dim].upper_bound = t; | |
2340 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE) | |
2341 | { | |
2342 | /* Assumed shape arrays have known lower bounds. */ | |
2343 | info->dimen[dim].upper_bound | |
2344 | = build2 (MINUS_EXPR, gfc_array_index_type, | |
2345 | info->dimen[dim].upper_bound, | |
2346 | info->dimen[dim].lower_bound); | |
2347 | info->dimen[dim].lower_bound | |
2348 | = fold_convert (gfc_array_index_type, | |
2349 | GFC_TYPE_ARRAY_LBOUND (type, dim)); | |
2350 | info->dimen[dim].upper_bound | |
2351 | = build2 (PLUS_EXPR, gfc_array_index_type, | |
2352 | info->dimen[dim].lower_bound, | |
2353 | info->dimen[dim].upper_bound); | |
2354 | } | |
2355 | t = build2 (POINTER_PLUS_EXPR, ptype, base_decl, | |
2356 | size_binop (PLUS_EXPR, dim_off, stride_suboff)); | |
2357 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); | |
2358 | t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size); | |
2359 | info->dimen[dim].stride = t; | |
2360 | dim_off = size_binop (PLUS_EXPR, dim_off, dim_size); | |
2361 | } | |
2362 | ||
2363 | return true; | |
2364 | } | |
2365 | ||
6de9cd9a | 2366 | #include "gt-fortran-trans-types.h" |