]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Declaration statement matcher |
5624e564 | 2 | Copyright (C) 2002-2015 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Andy Vaught |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 9 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 10 | version. |
6de9cd9a | 11 | |
9fc4d79b TS |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a | 20 | |
6de9cd9a | 21 | #include "config.h" |
d22e4895 | 22 | #include "system.h" |
953bee7c | 23 | #include "coretypes.h" |
6de9cd9a DN |
24 | #include "gfortran.h" |
25 | #include "match.h" | |
26 | #include "parse.h" | |
cab129d1 | 27 | #include "flags.h" |
b7e75771 | 28 | #include "constructor.h" |
40e23961 MC |
29 | #include "hash-set.h" |
30 | #include "machmode.h" | |
31 | #include "vec.h" | |
32 | #include "double-int.h" | |
33 | #include "input.h" | |
34 | #include "alias.h" | |
35 | #include "symtab.h" | |
36 | #include "wide-int.h" | |
37 | #include "inchash.h" | |
62603fae | 38 | #include "tree.h" |
d8a2d370 | 39 | #include "stringpool.h" |
ca39e6f2 FXC |
40 | |
41 | /* Macros to access allocate memory for gfc_data_variable, | |
42 | gfc_data_value and gfc_data. */ | |
ece3f663 KG |
43 | #define gfc_get_data_variable() XCNEW (gfc_data_variable) |
44 | #define gfc_get_data_value() XCNEW (gfc_data_value) | |
45 | #define gfc_get_data() XCNEW (gfc_data) | |
ca39e6f2 FXC |
46 | |
47 | ||
524af0d6 | 48 | static bool set_binding_label (const char **, const char *, int); |
62603fae JB |
49 | |
50 | ||
2054fc29 | 51 | /* This flag is set if an old-style length selector is matched |
6de9cd9a DN |
52 | during a type-declaration statement. */ |
53 | ||
54 | static int old_char_selector; | |
55 | ||
46fa431d | 56 | /* When variables acquire types and attributes from a declaration |
6de9cd9a DN |
57 | statement, they get them from the following static variables. The |
58 | first part of a declaration sets these variables and the second | |
59 | part copies these into symbol structures. */ | |
60 | ||
61 | static gfc_typespec current_ts; | |
62 | ||
63 | static symbol_attribute current_attr; | |
64 | static gfc_array_spec *current_as; | |
65 | static int colon_seen; | |
66 | ||
a8b3b0b6 | 67 | /* The current binding label (if any). */ |
9975a30b | 68 | static const char* curr_binding_label; |
a8b3b0b6 CR |
69 | /* Need to know how many identifiers are on the current data declaration |
70 | line in case we're given the BIND(C) attribute with a NAME= specifier. */ | |
71 | static int num_idents_on_line; | |
72 | /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we | |
73 | can supply a name if the curr_binding_label is nil and NAME= was not. */ | |
74 | static int has_name_equals = 0; | |
75 | ||
25d8f0a2 TS |
76 | /* Initializer of the previous enumerator. */ |
77 | ||
78 | static gfc_expr *last_initializer; | |
79 | ||
80 | /* History of all the enumerators is maintained, so that | |
81 | kind values of all the enumerators could be updated depending | |
82 | upon the maximum initialized value. */ | |
83 | ||
84 | typedef struct enumerator_history | |
85 | { | |
86 | gfc_symbol *sym; | |
87 | gfc_expr *initializer; | |
88 | struct enumerator_history *next; | |
89 | } | |
90 | enumerator_history; | |
91 | ||
92 | /* Header of enum history chain. */ | |
93 | ||
94 | static enumerator_history *enum_history = NULL; | |
95 | ||
96 | /* Pointer of enum history node containing largest initializer. */ | |
97 | ||
98 | static enumerator_history *max_enum = NULL; | |
99 | ||
6de9cd9a DN |
100 | /* gfc_new_block points to the symbol of a newly matched block. */ |
101 | ||
102 | gfc_symbol *gfc_new_block; | |
103 | ||
1c8bcdf7 | 104 | bool gfc_matching_function; |
e2d29968 | 105 | |
6de9cd9a | 106 | |
294fbfc8 TS |
107 | /********************* DATA statement subroutines *********************/ |
108 | ||
2220652d PT |
109 | static bool in_match_data = false; |
110 | ||
111 | bool | |
112 | gfc_in_match_data (void) | |
113 | { | |
114 | return in_match_data; | |
115 | } | |
116 | ||
ca39e6f2 FXC |
117 | static void |
118 | set_in_match_data (bool set_value) | |
2220652d PT |
119 | { |
120 | in_match_data = set_value; | |
121 | } | |
122 | ||
294fbfc8 TS |
123 | /* Free a gfc_data_variable structure and everything beneath it. */ |
124 | ||
125 | static void | |
636dff67 | 126 | free_variable (gfc_data_variable *p) |
294fbfc8 TS |
127 | { |
128 | gfc_data_variable *q; | |
129 | ||
130 | for (; p; p = q) | |
131 | { | |
132 | q = p->next; | |
133 | gfc_free_expr (p->expr); | |
134 | gfc_free_iterator (&p->iter, 0); | |
135 | free_variable (p->list); | |
cede9502 | 136 | free (p); |
294fbfc8 TS |
137 | } |
138 | } | |
139 | ||
140 | ||
141 | /* Free a gfc_data_value structure and everything beneath it. */ | |
142 | ||
143 | static void | |
636dff67 | 144 | free_value (gfc_data_value *p) |
294fbfc8 TS |
145 | { |
146 | gfc_data_value *q; | |
147 | ||
148 | for (; p; p = q) | |
149 | { | |
150 | q = p->next; | |
c9d75a48 | 151 | mpz_clear (p->repeat); |
294fbfc8 | 152 | gfc_free_expr (p->expr); |
cede9502 | 153 | free (p); |
294fbfc8 TS |
154 | } |
155 | } | |
156 | ||
157 | ||
158 | /* Free a list of gfc_data structures. */ | |
159 | ||
160 | void | |
636dff67 | 161 | gfc_free_data (gfc_data *p) |
294fbfc8 TS |
162 | { |
163 | gfc_data *q; | |
164 | ||
165 | for (; p; p = q) | |
166 | { | |
167 | q = p->next; | |
294fbfc8 TS |
168 | free_variable (p->var); |
169 | free_value (p->value); | |
cede9502 | 170 | free (p); |
294fbfc8 TS |
171 | } |
172 | } | |
173 | ||
174 | ||
a9f6f1f2 | 175 | /* Free all data in a namespace. */ |
636dff67 | 176 | |
a9f6f1f2 | 177 | static void |
66e4ab31 | 178 | gfc_free_data_all (gfc_namespace *ns) |
a9f6f1f2 JD |
179 | { |
180 | gfc_data *d; | |
181 | ||
182 | for (;ns->data;) | |
183 | { | |
184 | d = ns->data->next; | |
cede9502 | 185 | free (ns->data); |
a9f6f1f2 JD |
186 | ns->data = d; |
187 | } | |
188 | } | |
189 | ||
d5e2274d SB |
190 | /* Reject data parsed since the last restore point was marked. */ |
191 | ||
192 | void | |
193 | gfc_reject_data (gfc_namespace *ns) | |
194 | { | |
195 | gfc_data *d; | |
196 | ||
197 | while (ns->data && ns->data != ns->old_data) | |
198 | { | |
199 | d = ns->data->next; | |
200 | free (ns->data); | |
201 | ns->data = d; | |
202 | } | |
203 | } | |
a9f6f1f2 | 204 | |
294fbfc8 TS |
205 | static match var_element (gfc_data_variable *); |
206 | ||
207 | /* Match a list of variables terminated by an iterator and a right | |
208 | parenthesis. */ | |
209 | ||
210 | static match | |
636dff67 | 211 | var_list (gfc_data_variable *parent) |
294fbfc8 TS |
212 | { |
213 | gfc_data_variable *tail, var; | |
214 | match m; | |
215 | ||
216 | m = var_element (&var); | |
217 | if (m == MATCH_ERROR) | |
218 | return MATCH_ERROR; | |
219 | if (m == MATCH_NO) | |
220 | goto syntax; | |
221 | ||
222 | tail = gfc_get_data_variable (); | |
223 | *tail = var; | |
224 | ||
225 | parent->list = tail; | |
226 | ||
227 | for (;;) | |
228 | { | |
229 | if (gfc_match_char (',') != MATCH_YES) | |
230 | goto syntax; | |
231 | ||
232 | m = gfc_match_iterator (&parent->iter, 1); | |
233 | if (m == MATCH_YES) | |
234 | break; | |
235 | if (m == MATCH_ERROR) | |
236 | return MATCH_ERROR; | |
237 | ||
238 | m = var_element (&var); | |
239 | if (m == MATCH_ERROR) | |
240 | return MATCH_ERROR; | |
241 | if (m == MATCH_NO) | |
242 | goto syntax; | |
243 | ||
244 | tail->next = gfc_get_data_variable (); | |
245 | tail = tail->next; | |
246 | ||
247 | *tail = var; | |
248 | } | |
249 | ||
250 | if (gfc_match_char (')') != MATCH_YES) | |
251 | goto syntax; | |
252 | return MATCH_YES; | |
253 | ||
254 | syntax: | |
255 | gfc_syntax_error (ST_DATA); | |
256 | return MATCH_ERROR; | |
257 | } | |
258 | ||
259 | ||
260 | /* Match a single element in a data variable list, which can be a | |
261 | variable-iterator list. */ | |
262 | ||
263 | static match | |
7b901ac4 | 264 | var_element (gfc_data_variable *new_var) |
294fbfc8 TS |
265 | { |
266 | match m; | |
267 | gfc_symbol *sym; | |
268 | ||
7b901ac4 | 269 | memset (new_var, 0, sizeof (gfc_data_variable)); |
294fbfc8 TS |
270 | |
271 | if (gfc_match_char ('(') == MATCH_YES) | |
7b901ac4 | 272 | return var_list (new_var); |
294fbfc8 | 273 | |
7b901ac4 | 274 | m = gfc_match_variable (&new_var->expr, 0); |
294fbfc8 TS |
275 | if (m != MATCH_YES) |
276 | return m; | |
277 | ||
7b901ac4 | 278 | sym = new_var->expr->symtree->n.sym; |
294fbfc8 | 279 | |
f37e928c | 280 | /* Symbol should already have an associated type. */ |
524af0d6 | 281 | if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus)) |
f37e928c DK |
282 | return MATCH_ERROR; |
283 | ||
636dff67 SK |
284 | if (!sym->attr.function && gfc_current_ns->parent |
285 | && gfc_current_ns->parent == sym->ns) | |
294fbfc8 | 286 | { |
c4100eae | 287 | gfc_error ("Host associated variable %qs may not be in the DATA " |
e25a0da3 | 288 | "statement at %C", sym->name); |
294fbfc8 TS |
289 | return MATCH_ERROR; |
290 | } | |
291 | ||
4075a94e | 292 | if (gfc_current_state () != COMP_BLOCK_DATA |
636dff67 | 293 | && sym->attr.in_common |
524af0d6 | 294 | && !gfc_notify_std (GFC_STD_GNU, "initialization of " |
a4d9b221 | 295 | "common block variable %qs in DATA statement at %C", |
524af0d6 | 296 | sym->name)) |
4075a94e | 297 | return MATCH_ERROR; |
294fbfc8 | 298 | |
524af0d6 | 299 | if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where)) |
294fbfc8 TS |
300 | return MATCH_ERROR; |
301 | ||
302 | return MATCH_YES; | |
303 | } | |
304 | ||
305 | ||
306 | /* Match the top-level list of data variables. */ | |
307 | ||
308 | static match | |
636dff67 | 309 | top_var_list (gfc_data *d) |
294fbfc8 | 310 | { |
7b901ac4 | 311 | gfc_data_variable var, *tail, *new_var; |
294fbfc8 TS |
312 | match m; |
313 | ||
314 | tail = NULL; | |
315 | ||
316 | for (;;) | |
317 | { | |
318 | m = var_element (&var); | |
319 | if (m == MATCH_NO) | |
320 | goto syntax; | |
321 | if (m == MATCH_ERROR) | |
322 | return MATCH_ERROR; | |
323 | ||
7b901ac4 KG |
324 | new_var = gfc_get_data_variable (); |
325 | *new_var = var; | |
294fbfc8 TS |
326 | |
327 | if (tail == NULL) | |
7b901ac4 | 328 | d->var = new_var; |
294fbfc8 | 329 | else |
7b901ac4 | 330 | tail->next = new_var; |
294fbfc8 | 331 | |
7b901ac4 | 332 | tail = new_var; |
294fbfc8 TS |
333 | |
334 | if (gfc_match_char ('/') == MATCH_YES) | |
335 | break; | |
336 | if (gfc_match_char (',') != MATCH_YES) | |
337 | goto syntax; | |
338 | } | |
339 | ||
340 | return MATCH_YES; | |
341 | ||
342 | syntax: | |
343 | gfc_syntax_error (ST_DATA); | |
a9f6f1f2 | 344 | gfc_free_data_all (gfc_current_ns); |
294fbfc8 TS |
345 | return MATCH_ERROR; |
346 | } | |
347 | ||
348 | ||
349 | static match | |
636dff67 | 350 | match_data_constant (gfc_expr **result) |
294fbfc8 TS |
351 | { |
352 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
c3f34952 | 353 | gfc_symbol *sym, *dt_sym = NULL; |
294fbfc8 TS |
354 | gfc_expr *expr; |
355 | match m; | |
36d3fb4c | 356 | locus old_loc; |
294fbfc8 TS |
357 | |
358 | m = gfc_match_literal_constant (&expr, 1); | |
359 | if (m == MATCH_YES) | |
360 | { | |
361 | *result = expr; | |
362 | return MATCH_YES; | |
363 | } | |
364 | ||
365 | if (m == MATCH_ERROR) | |
366 | return MATCH_ERROR; | |
367 | ||
368 | m = gfc_match_null (result); | |
369 | if (m != MATCH_NO) | |
370 | return m; | |
371 | ||
36d3fb4c PT |
372 | old_loc = gfc_current_locus; |
373 | ||
374 | /* Should this be a structure component, try to match it | |
375 | before matching a name. */ | |
376 | m = gfc_match_rvalue (result); | |
377 | if (m == MATCH_ERROR) | |
378 | return m; | |
379 | ||
380 | if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE) | |
381 | { | |
524af0d6 | 382 | if (!gfc_simplify_expr (*result, 0)) |
36d3fb4c PT |
383 | m = MATCH_ERROR; |
384 | return m; | |
385 | } | |
46f4f794 TB |
386 | else if (m == MATCH_YES) |
387 | gfc_free_expr (*result); | |
36d3fb4c PT |
388 | |
389 | gfc_current_locus = old_loc; | |
390 | ||
294fbfc8 TS |
391 | m = gfc_match_name (name); |
392 | if (m != MATCH_YES) | |
393 | return m; | |
394 | ||
395 | if (gfc_find_symbol (name, NULL, 1, &sym)) | |
396 | return MATCH_ERROR; | |
397 | ||
c3f34952 TB |
398 | if (sym && sym->attr.generic) |
399 | dt_sym = gfc_find_dt_in_generic (sym); | |
400 | ||
294fbfc8 | 401 | if (sym == NULL |
c3f34952 TB |
402 | || (sym->attr.flavor != FL_PARAMETER |
403 | && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED))) | |
294fbfc8 | 404 | { |
c4100eae | 405 | gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C", |
294fbfc8 TS |
406 | name); |
407 | return MATCH_ERROR; | |
408 | } | |
c3f34952 TB |
409 | else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED) |
410 | return gfc_match_structure_constructor (dt_sym, result); | |
294fbfc8 | 411 | |
d46e0870 JD |
412 | /* Check to see if the value is an initialization array expression. */ |
413 | if (sym->value->expr_type == EXPR_ARRAY) | |
414 | { | |
415 | gfc_current_locus = old_loc; | |
416 | ||
417 | m = gfc_match_init_expr (result); | |
418 | if (m == MATCH_ERROR) | |
419 | return m; | |
420 | ||
421 | if (m == MATCH_YES) | |
422 | { | |
524af0d6 | 423 | if (!gfc_simplify_expr (*result, 0)) |
d46e0870 JD |
424 | m = MATCH_ERROR; |
425 | ||
426 | if ((*result)->expr_type == EXPR_CONSTANT) | |
427 | return m; | |
428 | else | |
429 | { | |
430 | gfc_error ("Invalid initializer %s in Data statement at %C", name); | |
431 | return MATCH_ERROR; | |
432 | } | |
433 | } | |
434 | } | |
435 | ||
294fbfc8 TS |
436 | *result = gfc_copy_expr (sym->value); |
437 | return MATCH_YES; | |
438 | } | |
439 | ||
440 | ||
441 | /* Match a list of values in a DATA statement. The leading '/' has | |
442 | already been seen at this point. */ | |
443 | ||
444 | static match | |
636dff67 | 445 | top_val_list (gfc_data *data) |
294fbfc8 | 446 | { |
7b901ac4 | 447 | gfc_data_value *new_val, *tail; |
294fbfc8 | 448 | gfc_expr *expr; |
294fbfc8 TS |
449 | match m; |
450 | ||
451 | tail = NULL; | |
452 | ||
453 | for (;;) | |
454 | { | |
455 | m = match_data_constant (&expr); | |
456 | if (m == MATCH_NO) | |
457 | goto syntax; | |
458 | if (m == MATCH_ERROR) | |
459 | return MATCH_ERROR; | |
460 | ||
7b901ac4 KG |
461 | new_val = gfc_get_data_value (); |
462 | mpz_init (new_val->repeat); | |
294fbfc8 TS |
463 | |
464 | if (tail == NULL) | |
7b901ac4 | 465 | data->value = new_val; |
294fbfc8 | 466 | else |
7b901ac4 | 467 | tail->next = new_val; |
294fbfc8 | 468 | |
7b901ac4 | 469 | tail = new_val; |
294fbfc8 TS |
470 | |
471 | if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES) | |
472 | { | |
473 | tail->expr = expr; | |
f2112868 | 474 | mpz_set_ui (tail->repeat, 1); |
294fbfc8 TS |
475 | } |
476 | else | |
477 | { | |
46f4f794 | 478 | mpz_set (tail->repeat, expr->value.integer); |
294fbfc8 | 479 | gfc_free_expr (expr); |
294fbfc8 TS |
480 | |
481 | m = match_data_constant (&tail->expr); | |
482 | if (m == MATCH_NO) | |
483 | goto syntax; | |
484 | if (m == MATCH_ERROR) | |
485 | return MATCH_ERROR; | |
486 | } | |
487 | ||
488 | if (gfc_match_char ('/') == MATCH_YES) | |
489 | break; | |
490 | if (gfc_match_char (',') == MATCH_NO) | |
491 | goto syntax; | |
492 | } | |
493 | ||
494 | return MATCH_YES; | |
495 | ||
496 | syntax: | |
497 | gfc_syntax_error (ST_DATA); | |
a9f6f1f2 | 498 | gfc_free_data_all (gfc_current_ns); |
294fbfc8 TS |
499 | return MATCH_ERROR; |
500 | } | |
501 | ||
502 | ||
503 | /* Matches an old style initialization. */ | |
504 | ||
505 | static match | |
506 | match_old_style_init (const char *name) | |
507 | { | |
508 | match m; | |
509 | gfc_symtree *st; | |
ed0e3607 | 510 | gfc_symbol *sym; |
294fbfc8 TS |
511 | gfc_data *newdata; |
512 | ||
513 | /* Set up data structure to hold initializers. */ | |
514 | gfc_find_sym_tree (name, NULL, 0, &st); | |
ed0e3607 AL |
515 | sym = st->n.sym; |
516 | ||
294fbfc8 TS |
517 | newdata = gfc_get_data (); |
518 | newdata->var = gfc_get_data_variable (); | |
519 | newdata->var->expr = gfc_get_variable_expr (st); | |
8c5c0b80 | 520 | newdata->where = gfc_current_locus; |
294fbfc8 | 521 | |
66e4ab31 | 522 | /* Match initial value list. This also eats the terminal '/'. */ |
294fbfc8 TS |
523 | m = top_val_list (newdata); |
524 | if (m != MATCH_YES) | |
525 | { | |
cede9502 | 526 | free (newdata); |
294fbfc8 TS |
527 | return m; |
528 | } | |
529 | ||
530 | if (gfc_pure (NULL)) | |
531 | { | |
532 | gfc_error ("Initialization at %C is not allowed in a PURE procedure"); | |
cede9502 | 533 | free (newdata); |
294fbfc8 TS |
534 | return MATCH_ERROR; |
535 | } | |
ccd7751b | 536 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
f1f39033 | 537 | |
ed0e3607 | 538 | /* Mark the variable as having appeared in a data statement. */ |
524af0d6 | 539 | if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at)) |
ed0e3607 | 540 | { |
cede9502 | 541 | free (newdata); |
ed0e3607 AL |
542 | return MATCH_ERROR; |
543 | } | |
544 | ||
294fbfc8 TS |
545 | /* Chain in namespace list of DATA initializers. */ |
546 | newdata->next = gfc_current_ns->data; | |
547 | gfc_current_ns->data = newdata; | |
548 | ||
549 | return m; | |
550 | } | |
551 | ||
636dff67 | 552 | |
294fbfc8 | 553 | /* Match the stuff following a DATA statement. If ERROR_FLAG is set, |
13795658 | 554 | we are matching a DATA statement and are therefore issuing an error |
d51347f9 | 555 | if we encounter something unexpected, if not, we're trying to match |
69de3b83 | 556 | an old-style initialization expression of the form INTEGER I /2/. */ |
294fbfc8 TS |
557 | |
558 | match | |
559 | gfc_match_data (void) | |
560 | { | |
7b901ac4 | 561 | gfc_data *new_data; |
294fbfc8 TS |
562 | match m; |
563 | ||
ca39e6f2 | 564 | set_in_match_data (true); |
2220652d | 565 | |
294fbfc8 TS |
566 | for (;;) |
567 | { | |
7b901ac4 KG |
568 | new_data = gfc_get_data (); |
569 | new_data->where = gfc_current_locus; | |
294fbfc8 | 570 | |
7b901ac4 | 571 | m = top_var_list (new_data); |
294fbfc8 TS |
572 | if (m != MATCH_YES) |
573 | goto cleanup; | |
574 | ||
7b901ac4 | 575 | m = top_val_list (new_data); |
294fbfc8 TS |
576 | if (m != MATCH_YES) |
577 | goto cleanup; | |
578 | ||
7b901ac4 KG |
579 | new_data->next = gfc_current_ns->data; |
580 | gfc_current_ns->data = new_data; | |
294fbfc8 TS |
581 | |
582 | if (gfc_match_eos () == MATCH_YES) | |
583 | break; | |
584 | ||
585 | gfc_match_char (','); /* Optional comma */ | |
586 | } | |
587 | ||
ca39e6f2 | 588 | set_in_match_data (false); |
2220652d | 589 | |
294fbfc8 TS |
590 | if (gfc_pure (NULL)) |
591 | { | |
592 | gfc_error ("DATA statement at %C is not allowed in a PURE procedure"); | |
593 | return MATCH_ERROR; | |
594 | } | |
ccd7751b | 595 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
f1f39033 | 596 | |
294fbfc8 TS |
597 | return MATCH_YES; |
598 | ||
599 | cleanup: | |
ca39e6f2 | 600 | set_in_match_data (false); |
7b901ac4 | 601 | gfc_free_data (new_data); |
294fbfc8 TS |
602 | return MATCH_ERROR; |
603 | } | |
604 | ||
605 | ||
606 | /************************ Declaration statements *********************/ | |
607 | ||
d3a9eea2 | 608 | |
eea58adb | 609 | /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */ |
d3a9eea2 | 610 | |
524af0d6 | 611 | static bool |
d3a9eea2 TB |
612 | merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy) |
613 | { | |
614 | int i; | |
615 | ||
63fbf586 TB |
616 | if ((from->type == AS_ASSUMED_RANK && to->corank) |
617 | || (to->type == AS_ASSUMED_RANK && from->corank)) | |
618 | { | |
619 | gfc_error ("The assumed-rank array at %C shall not have a codimension"); | |
524af0d6 | 620 | return false; |
63fbf586 | 621 | } |
c62c6622 | 622 | |
d3a9eea2 TB |
623 | if (to->rank == 0 && from->rank > 0) |
624 | { | |
625 | to->rank = from->rank; | |
626 | to->type = from->type; | |
627 | to->cray_pointee = from->cray_pointee; | |
628 | to->cp_was_assumed = from->cp_was_assumed; | |
629 | ||
630 | for (i = 0; i < to->corank; i++) | |
631 | { | |
632 | to->lower[from->rank + i] = to->lower[i]; | |
633 | to->upper[from->rank + i] = to->upper[i]; | |
634 | } | |
635 | for (i = 0; i < from->rank; i++) | |
636 | { | |
637 | if (copy) | |
638 | { | |
639 | to->lower[i] = gfc_copy_expr (from->lower[i]); | |
640 | to->upper[i] = gfc_copy_expr (from->upper[i]); | |
641 | } | |
642 | else | |
643 | { | |
644 | to->lower[i] = from->lower[i]; | |
645 | to->upper[i] = from->upper[i]; | |
646 | } | |
647 | } | |
648 | } | |
649 | else if (to->corank == 0 && from->corank > 0) | |
650 | { | |
651 | to->corank = from->corank; | |
652 | to->cotype = from->cotype; | |
653 | ||
654 | for (i = 0; i < from->corank; i++) | |
655 | { | |
656 | if (copy) | |
657 | { | |
658 | to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]); | |
659 | to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]); | |
660 | } | |
661 | else | |
662 | { | |
663 | to->lower[to->rank + i] = from->lower[i]; | |
664 | to->upper[to->rank + i] = from->upper[i]; | |
665 | } | |
666 | } | |
667 | } | |
63fbf586 | 668 | |
524af0d6 | 669 | return true; |
d3a9eea2 TB |
670 | } |
671 | ||
672 | ||
6de9cd9a DN |
673 | /* Match an intent specification. Since this can only happen after an |
674 | INTENT word, a legal intent-spec must follow. */ | |
675 | ||
676 | static sym_intent | |
677 | match_intent_spec (void) | |
678 | { | |
679 | ||
680 | if (gfc_match (" ( in out )") == MATCH_YES) | |
681 | return INTENT_INOUT; | |
682 | if (gfc_match (" ( in )") == MATCH_YES) | |
683 | return INTENT_IN; | |
684 | if (gfc_match (" ( out )") == MATCH_YES) | |
685 | return INTENT_OUT; | |
686 | ||
687 | gfc_error ("Bad INTENT specification at %C"); | |
688 | return INTENT_UNKNOWN; | |
689 | } | |
690 | ||
691 | ||
692 | /* Matches a character length specification, which is either a | |
e69afb29 | 693 | specification expression, '*', or ':'. */ |
6de9cd9a DN |
694 | |
695 | static match | |
e69afb29 | 696 | char_len_param_value (gfc_expr **expr, bool *deferred) |
6de9cd9a | 697 | { |
cba28dad JD |
698 | match m; |
699 | ||
e69afb29 SK |
700 | *expr = NULL; |
701 | *deferred = false; | |
702 | ||
6de9cd9a | 703 | if (gfc_match_char ('*') == MATCH_YES) |
e69afb29 SK |
704 | return MATCH_YES; |
705 | ||
706 | if (gfc_match_char (':') == MATCH_YES) | |
6de9cd9a | 707 | { |
524af0d6 JB |
708 | if (!gfc_notify_std (GFC_STD_F2003, "deferred type " |
709 | "parameter at %C")) | |
e69afb29 SK |
710 | return MATCH_ERROR; |
711 | ||
712 | *deferred = true; | |
713 | ||
6de9cd9a DN |
714 | return MATCH_YES; |
715 | } | |
716 | ||
cba28dad | 717 | m = gfc_match_expr (expr); |
f37e928c DK |
718 | |
719 | if (m == MATCH_YES | |
524af0d6 | 720 | && !gfc_expr_check_typed (*expr, gfc_current_ns, false)) |
f37e928c DK |
721 | return MATCH_ERROR; |
722 | ||
cba28dad JD |
723 | if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION) |
724 | { | |
725 | if ((*expr)->value.function.actual | |
726 | && (*expr)->value.function.actual->expr->symtree) | |
727 | { | |
728 | gfc_expr *e; | |
729 | e = (*expr)->value.function.actual->expr; | |
730 | if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE | |
731 | && e->expr_type == EXPR_VARIABLE) | |
732 | { | |
733 | if (e->symtree->n.sym->ts.type == BT_UNKNOWN) | |
734 | goto syntax; | |
735 | if (e->symtree->n.sym->ts.type == BT_CHARACTER | |
bc21d315 JW |
736 | && e->symtree->n.sym->ts.u.cl |
737 | && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN) | |
cba28dad JD |
738 | goto syntax; |
739 | } | |
740 | } | |
741 | } | |
742 | return m; | |
743 | ||
744 | syntax: | |
745 | gfc_error ("Conflict in attributes of function argument at %C"); | |
746 | return MATCH_ERROR; | |
6de9cd9a DN |
747 | } |
748 | ||
749 | ||
750 | /* A character length is a '*' followed by a literal integer or a | |
751 | char_len_param_value in parenthesis. */ | |
752 | ||
753 | static match | |
62732c30 | 754 | match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check) |
6de9cd9a | 755 | { |
5cf54585 | 756 | int length; |
6de9cd9a DN |
757 | match m; |
758 | ||
f5acf0f2 | 759 | *deferred = false; |
6de9cd9a DN |
760 | m = gfc_match_char ('*'); |
761 | if (m != MATCH_YES) | |
762 | return m; | |
763 | ||
5cf54585 | 764 | m = gfc_match_small_literal_int (&length, NULL); |
6de9cd9a DN |
765 | if (m == MATCH_ERROR) |
766 | return m; | |
767 | ||
768 | if (m == MATCH_YES) | |
769 | { | |
62732c30 | 770 | if (obsolescent_check |
524af0d6 | 771 | && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C")) |
e2ab8b09 | 772 | return MATCH_ERROR; |
b7e75771 | 773 | *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length); |
6de9cd9a DN |
774 | return m; |
775 | } | |
776 | ||
777 | if (gfc_match_char ('(') == MATCH_NO) | |
778 | goto syntax; | |
779 | ||
e69afb29 | 780 | m = char_len_param_value (expr, deferred); |
1c8bcdf7 PT |
781 | if (m != MATCH_YES && gfc_matching_function) |
782 | { | |
783 | gfc_undo_symbols (); | |
784 | m = MATCH_YES; | |
785 | } | |
786 | ||
6de9cd9a DN |
787 | if (m == MATCH_ERROR) |
788 | return m; | |
789 | if (m == MATCH_NO) | |
790 | goto syntax; | |
791 | ||
792 | if (gfc_match_char (')') == MATCH_NO) | |
793 | { | |
794 | gfc_free_expr (*expr); | |
795 | *expr = NULL; | |
796 | goto syntax; | |
797 | } | |
798 | ||
799 | return MATCH_YES; | |
800 | ||
801 | syntax: | |
802 | gfc_error ("Syntax error in character length specification at %C"); | |
803 | return MATCH_ERROR; | |
804 | } | |
805 | ||
806 | ||
9e35b386 EE |
807 | /* Special subroutine for finding a symbol. Check if the name is found |
808 | in the current name space. If not, and we're compiling a function or | |
809 | subroutine and the parent compilation unit is an interface, then check | |
810 | to see if the name we've been given is the name of the interface | |
811 | (located in another namespace). */ | |
6de9cd9a DN |
812 | |
813 | static int | |
08a6b8e0 | 814 | find_special (const char *name, gfc_symbol **result, bool allow_subroutine) |
6de9cd9a DN |
815 | { |
816 | gfc_state_data *s; | |
08a6b8e0 | 817 | gfc_symtree *st; |
9e35b386 | 818 | int i; |
6de9cd9a | 819 | |
08a6b8e0 | 820 | i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine); |
d51347f9 | 821 | if (i == 0) |
08a6b8e0 TB |
822 | { |
823 | *result = st ? st->n.sym : NULL; | |
824 | goto end; | |
825 | } | |
d51347f9 | 826 | |
6de9cd9a DN |
827 | if (gfc_current_state () != COMP_SUBROUTINE |
828 | && gfc_current_state () != COMP_FUNCTION) | |
9e35b386 | 829 | goto end; |
6de9cd9a DN |
830 | |
831 | s = gfc_state_stack->previous; | |
832 | if (s == NULL) | |
9e35b386 | 833 | goto end; |
6de9cd9a DN |
834 | |
835 | if (s->state != COMP_INTERFACE) | |
9e35b386 | 836 | goto end; |
6de9cd9a | 837 | if (s->sym == NULL) |
66e4ab31 | 838 | goto end; /* Nameless interface. */ |
6de9cd9a DN |
839 | |
840 | if (strcmp (name, s->sym->name) == 0) | |
841 | { | |
842 | *result = s->sym; | |
843 | return 0; | |
844 | } | |
845 | ||
9e35b386 EE |
846 | end: |
847 | return i; | |
6de9cd9a DN |
848 | } |
849 | ||
850 | ||
851 | /* Special subroutine for getting a symbol node associated with a | |
852 | procedure name, used in SUBROUTINE and FUNCTION statements. The | |
853 | symbol is created in the parent using with symtree node in the | |
854 | child unit pointing to the symbol. If the current namespace has no | |
855 | parent, then the symbol is just created in the current unit. */ | |
856 | ||
857 | static int | |
636dff67 | 858 | get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry) |
6de9cd9a DN |
859 | { |
860 | gfc_symtree *st; | |
861 | gfc_symbol *sym; | |
a7ca4d8d | 862 | int rc = 0; |
6de9cd9a | 863 | |
1a492601 PT |
864 | /* Module functions have to be left in their own namespace because |
865 | they have potentially (almost certainly!) already been referenced. | |
866 | In this sense, they are rather like external functions. This is | |
867 | fixed up in resolve.c(resolve_entries), where the symbol name- | |
868 | space is set to point to the master function, so that the fake | |
869 | result mechanism can work. */ | |
870 | if (module_fcn_entry) | |
6c12686b PT |
871 | { |
872 | /* Present if entry is declared to be a module procedure. */ | |
873 | rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result); | |
aa84a9a5 | 874 | |
6c12686b PT |
875 | if (*result == NULL) |
876 | rc = gfc_get_symbol (name, NULL, result); | |
2e32a71e | 877 | else if (!gfc_get_symbol (name, NULL, &sym) && sym |
aa84a9a5 PT |
878 | && (*result)->ts.type == BT_UNKNOWN |
879 | && sym->attr.flavor == FL_UNKNOWN) | |
880 | /* Pick up the typespec for the entry, if declared in the function | |
881 | body. Note that this symbol is FL_UNKNOWN because it will | |
882 | only have appeared in a type declaration. The local symtree | |
883 | is set to point to the module symbol and a unique symtree | |
884 | to the local version. This latter ensures a correct clearing | |
885 | of the symbols. */ | |
2e32a71e PT |
886 | { |
887 | /* If the ENTRY proceeds its specification, we need to ensure | |
888 | that this does not raise a "has no IMPLICIT type" error. */ | |
889 | if (sym->ts.type == BT_UNKNOWN) | |
0e5a218b | 890 | sym->attr.untyped = 1; |
2e32a71e | 891 | |
0e5a218b | 892 | (*result)->ts = sym->ts; |
2e32a71e PT |
893 | |
894 | /* Put the symbol in the procedure namespace so that, should | |
df2fba9e | 895 | the ENTRY precede its specification, the specification |
2e32a71e PT |
896 | can be applied. */ |
897 | (*result)->ns = gfc_current_ns; | |
898 | ||
899 | gfc_find_sym_tree (name, gfc_current_ns, 0, &st); | |
900 | st->n.sym = *result; | |
901 | st = gfc_get_unique_symtree (gfc_current_ns); | |
902 | st->n.sym = sym; | |
903 | } | |
6c12686b | 904 | } |
68ea355b PT |
905 | else |
906 | rc = gfc_get_symbol (name, gfc_current_ns->parent, result); | |
6de9cd9a | 907 | |
a7ca4d8d PT |
908 | if (rc) |
909 | return rc; | |
910 | ||
68ea355b | 911 | sym = *result; |
6de9cd9a | 912 | |
7b901ac4 | 913 | if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE) |
68ea355b | 914 | { |
cda7004b PT |
915 | /* Trap another encompassed procedure with the same name. All |
916 | these conditions are necessary to avoid picking up an entry | |
917 | whose name clashes with that of the encompassing procedure; | |
918 | this is handled using gsymbols to register unique,globally | |
919 | accessible names. */ | |
68ea355b | 920 | if (sym->attr.flavor != 0 |
636dff67 SK |
921 | && sym->attr.proc != 0 |
922 | && (sym->attr.subroutine || sym->attr.function) | |
923 | && sym->attr.if_source != IFSRC_UNKNOWN) | |
4daa149b TB |
924 | gfc_error_now_1 ("Procedure '%s' at %C is already defined at %L", |
925 | name, &sym->declared_at); | |
68ea355b | 926 | |
fd3e70af JD |
927 | /* Trap a procedure with a name the same as interface in the |
928 | encompassing scope. */ | |
929 | if (sym->attr.generic != 0 | |
2305fa31 JD |
930 | && (sym->attr.subroutine || sym->attr.function) |
931 | && !sym->attr.mod_proc) | |
4daa149b TB |
932 | gfc_error_now_1 ("Name '%s' at %C is already defined" |
933 | " as a generic interface at %L", | |
934 | name, &sym->declared_at); | |
fd3e70af | 935 | |
68ea355b PT |
936 | /* Trap declarations of attributes in encompassing scope. The |
937 | signature for this is that ts.kind is set. Legitimate | |
938 | references only set ts.type. */ | |
939 | if (sym->ts.kind != 0 | |
636dff67 SK |
940 | && !sym->attr.implicit_type |
941 | && sym->attr.proc == 0 | |
942 | && gfc_current_ns->parent != NULL | |
943 | && sym->attr.access == 0 | |
944 | && !module_fcn_entry) | |
4daa149b TB |
945 | gfc_error_now_1 ("Procedure '%s' at %C has an explicit interface " |
946 | "and must not have attributes declared at %L", | |
947 | name, &sym->declared_at); | |
68ea355b PT |
948 | } |
949 | ||
950 | if (gfc_current_ns->parent == NULL || *result == NULL) | |
951 | return rc; | |
6de9cd9a | 952 | |
1a492601 PT |
953 | /* Module function entries will already have a symtree in |
954 | the current namespace but will need one at module level. */ | |
955 | if (module_fcn_entry) | |
6c12686b PT |
956 | { |
957 | /* Present if entry is declared to be a module procedure. */ | |
958 | rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st); | |
959 | if (st == NULL) | |
960 | st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name); | |
961 | } | |
1a492601 PT |
962 | else |
963 | st = gfc_new_symtree (&gfc_current_ns->sym_root, name); | |
6de9cd9a | 964 | |
6de9cd9a DN |
965 | st->n.sym = sym; |
966 | sym->refs++; | |
967 | ||
66e4ab31 | 968 | /* See if the procedure should be a module procedure. */ |
6de9cd9a | 969 | |
1a492601 | 970 | if (((sym->ns->proc_name != NULL |
6c12686b PT |
971 | && sym->ns->proc_name->attr.flavor == FL_MODULE |
972 | && sym->attr.proc != PROC_MODULE) | |
973 | || (module_fcn_entry && sym->attr.proc != PROC_MODULE)) | |
524af0d6 | 974 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) |
6de9cd9a DN |
975 | rc = 2; |
976 | ||
977 | return rc; | |
978 | } | |
979 | ||
980 | ||
a8b3b0b6 CR |
981 | /* Verify that the given symbol representing a parameter is C |
982 | interoperable, by checking to see if it was marked as such after | |
983 | its declaration. If the given symbol is not interoperable, a | |
984 | warning is reported, thus removing the need to return the status to | |
985 | the calling function. The standard does not require the user use | |
986 | one of the iso_c_binding named constants to declare an | |
987 | interoperable parameter, but we can't be sure if the param is C | |
988 | interop or not if the user doesn't. For example, integer(4) may be | |
989 | legal Fortran, but doesn't have meaning in C. It may interop with | |
990 | a number of the C types, which causes a problem because the | |
991 | compiler can't know which one. This code is almost certainly not | |
992 | portable, and the user will get what they deserve if the C type | |
993 | across platforms isn't always interoperable with integer(4). If | |
994 | the user had used something like integer(c_int) or integer(c_long), | |
995 | the compiler could have automatically handled the varying sizes | |
996 | across platforms. */ | |
997 | ||
524af0d6 | 998 | bool |
00820a2a | 999 | gfc_verify_c_interop_param (gfc_symbol *sym) |
a8b3b0b6 CR |
1000 | { |
1001 | int is_c_interop = 0; | |
524af0d6 | 1002 | bool retval = true; |
a8b3b0b6 CR |
1003 | |
1004 | /* We check implicitly typed variables in symbol.c:gfc_set_default_type(). | |
1005 | Don't repeat the checks here. */ | |
1006 | if (sym->attr.implicit_type) | |
524af0d6 | 1007 | return true; |
f5acf0f2 | 1008 | |
a8b3b0b6 CR |
1009 | /* For subroutines or functions that are passed to a BIND(C) procedure, |
1010 | they're interoperable if they're BIND(C) and their params are all | |
1011 | interoperable. */ | |
1012 | if (sym->attr.flavor == FL_PROCEDURE) | |
1013 | { | |
1014 | if (sym->attr.is_bind_c == 0) | |
1015 | { | |
4daa149b TB |
1016 | gfc_error_now ("Procedure %qs at %L must have the BIND(C) " |
1017 | "attribute to be C interoperable", sym->name, | |
1018 | &(sym->declared_at)); | |
524af0d6 | 1019 | return false; |
a8b3b0b6 CR |
1020 | } |
1021 | else | |
1022 | { | |
1023 | if (sym->attr.is_c_interop == 1) | |
1024 | /* We've already checked this procedure; don't check it again. */ | |
524af0d6 | 1025 | return true; |
a8b3b0b6 CR |
1026 | else |
1027 | return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common, | |
1028 | sym->common_block); | |
1029 | } | |
1030 | } | |
f5acf0f2 | 1031 | |
a8b3b0b6 CR |
1032 | /* See if we've stored a reference to a procedure that owns sym. */ |
1033 | if (sym->ns != NULL && sym->ns->proc_name != NULL) | |
1034 | { | |
1035 | if (sym->ns->proc_name->attr.is_bind_c == 1) | |
1036 | { | |
524af0d6 | 1037 | is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0); |
a8b3b0b6 CR |
1038 | |
1039 | if (is_c_interop != 1) | |
1040 | { | |
1041 | /* Make personalized messages to give better feedback. */ | |
1042 | if (sym->ts.type == BT_DERIVED) | |
c4100eae MLI |
1043 | gfc_error ("Variable %qs at %L is a dummy argument to the " |
1044 | "BIND(C) procedure %qs but is not C interoperable " | |
1045 | "because derived type %qs is not C interoperable", | |
a8b3b0b6 | 1046 | sym->name, &(sym->declared_at), |
f5acf0f2 | 1047 | sym->ns->proc_name->name, |
bc21d315 | 1048 | sym->ts.u.derived->name); |
00820a2a | 1049 | else if (sym->ts.type == BT_CLASS) |
c4100eae MLI |
1050 | gfc_error ("Variable %qs at %L is a dummy argument to the " |
1051 | "BIND(C) procedure %qs but is not C interoperable " | |
00820a2a JW |
1052 | "because it is polymorphic", |
1053 | sym->name, &(sym->declared_at), | |
1054 | sym->ns->proc_name->name); | |
4daa149b | 1055 | else if (warn_c_binding_type) |
48749dbc MLI |
1056 | gfc_warning (OPT_Wc_binding_type, |
1057 | "Variable %qs at %L is a dummy argument of the " | |
1058 | "BIND(C) procedure %qs but may not be C " | |
a8b3b0b6 CR |
1059 | "interoperable", |
1060 | sym->name, &(sym->declared_at), | |
1061 | sym->ns->proc_name->name); | |
1062 | } | |
aa5e22f0 CR |
1063 | |
1064 | /* Character strings are only C interoperable if they have a | |
1065 | length of 1. */ | |
1066 | if (sym->ts.type == BT_CHARACTER) | |
1067 | { | |
bc21d315 | 1068 | gfc_charlen *cl = sym->ts.u.cl; |
aa5e22f0 CR |
1069 | if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT |
1070 | || mpz_cmp_si (cl->length->value.integer, 1) != 0) | |
1071 | { | |
c4100eae | 1072 | gfc_error ("Character argument %qs at %L " |
aa5e22f0 | 1073 | "must be length 1 because " |
c4100eae | 1074 | "procedure %qs is BIND(C)", |
aa5e22f0 CR |
1075 | sym->name, &sym->declared_at, |
1076 | sym->ns->proc_name->name); | |
524af0d6 | 1077 | retval = false; |
aa5e22f0 CR |
1078 | } |
1079 | } | |
1080 | ||
a8b3b0b6 CR |
1081 | /* We have to make sure that any param to a bind(c) routine does |
1082 | not have the allocatable, pointer, or optional attributes, | |
1083 | according to J3/04-007, section 5.1. */ | |
60f6ca95 | 1084 | if (sym->attr.allocatable == 1 |
a4d9b221 TB |
1085 | && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with " |
1086 | "ALLOCATABLE attribute in procedure %qs " | |
60f6ca95 TB |
1087 | "with BIND(C)", sym->name, |
1088 | &(sym->declared_at), | |
1089 | sym->ns->proc_name->name)) | |
1090 | retval = false; | |
1091 | ||
1092 | if (sym->attr.pointer == 1 | |
a4d9b221 TB |
1093 | && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs at %L with " |
1094 | "POINTER attribute in procedure %qs " | |
60f6ca95 TB |
1095 | "with BIND(C)", sym->name, |
1096 | &(sym->declared_at), | |
1097 | sym->ns->proc_name->name)) | |
1098 | retval = false; | |
1099 | ||
1100 | if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as) | |
a8b3b0b6 | 1101 | { |
c4100eae MLI |
1102 | gfc_error ("Scalar variable %qs at %L with POINTER or " |
1103 | "ALLOCATABLE in procedure %qs with BIND(C) is not yet" | |
60f6ca95 | 1104 | " supported", sym->name, &(sym->declared_at), |
a8b3b0b6 | 1105 | sym->ns->proc_name->name); |
524af0d6 | 1106 | retval = false; |
a8b3b0b6 CR |
1107 | } |
1108 | ||
2e8d9212 | 1109 | if (sym->attr.optional == 1 && sym->attr.value) |
a8b3b0b6 | 1110 | { |
c4100eae MLI |
1111 | gfc_error ("Variable %qs at %L cannot have both the OPTIONAL " |
1112 | "and the VALUE attribute because procedure %qs " | |
2e8d9212 | 1113 | "is BIND(C)", sym->name, &(sym->declared_at), |
a8b3b0b6 | 1114 | sym->ns->proc_name->name); |
524af0d6 | 1115 | retval = false; |
a8b3b0b6 | 1116 | } |
2e8d9212 | 1117 | else if (sym->attr.optional == 1 |
a4d9b221 | 1118 | && !gfc_notify_std (GFC_STD_F2008_TS, "Variable %qs " |
524af0d6 | 1119 | "at %L with OPTIONAL attribute in " |
a4d9b221 | 1120 | "procedure %qs which is BIND(C)", |
524af0d6 JB |
1121 | sym->name, &(sym->declared_at), |
1122 | sym->ns->proc_name->name)) | |
1123 | retval = false; | |
a8b3b0b6 CR |
1124 | |
1125 | /* Make sure that if it has the dimension attribute, that it is | |
95d47b8d TB |
1126 | either assumed size or explicit shape. Deferred shape is already |
1127 | covered by the pointer/allocatable attribute. */ | |
1128 | if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE | |
a4d9b221 | 1129 | && !gfc_notify_std_1 (GFC_STD_F2008_TS, "Assumed-shape array '%s' " |
524af0d6 JB |
1130 | "at %L as dummy argument to the BIND(C) " |
1131 | "procedure '%s' at %L", sym->name, | |
1132 | &(sym->declared_at), | |
1133 | sym->ns->proc_name->name, | |
1134 | &(sym->ns->proc_name->declared_at))) | |
1135 | retval = false; | |
a8b3b0b6 CR |
1136 | } |
1137 | } | |
1138 | ||
1139 | return retval; | |
1140 | } | |
1141 | ||
1142 | ||
cf2b3c22 | 1143 | |
a8b3b0b6 | 1144 | /* Function called by variable_decl() that adds a name to the symbol table. */ |
6de9cd9a | 1145 | |
524af0d6 | 1146 | static bool |
e69afb29 | 1147 | build_sym (const char *name, gfc_charlen *cl, bool cl_deferred, |
636dff67 | 1148 | gfc_array_spec **as, locus *var_locus) |
6de9cd9a DN |
1149 | { |
1150 | symbol_attribute attr; | |
1151 | gfc_symbol *sym; | |
1152 | ||
9e35b386 | 1153 | if (gfc_get_symbol (name, NULL, &sym)) |
524af0d6 | 1154 | return false; |
6de9cd9a | 1155 | |
66e4ab31 | 1156 | /* Start updating the symbol table. Add basic type attribute if present. */ |
6de9cd9a | 1157 | if (current_ts.type != BT_UNKNOWN |
636dff67 SK |
1158 | && (sym->attr.implicit_type == 0 |
1159 | || !gfc_compare_types (&sym->ts, ¤t_ts)) | |
524af0d6 JB |
1160 | && !gfc_add_type (sym, ¤t_ts, var_locus)) |
1161 | return false; | |
6de9cd9a DN |
1162 | |
1163 | if (sym->ts.type == BT_CHARACTER) | |
e69afb29 SK |
1164 | { |
1165 | sym->ts.u.cl = cl; | |
1166 | sym->ts.deferred = cl_deferred; | |
1167 | } | |
6de9cd9a DN |
1168 | |
1169 | /* Add dimension attribute if present. */ | |
524af0d6 JB |
1170 | if (!gfc_set_array_spec (sym, *as, var_locus)) |
1171 | return false; | |
6de9cd9a DN |
1172 | *as = NULL; |
1173 | ||
1174 | /* Add attribute to symbol. The copy is so that we can reset the | |
1175 | dimension attribute. */ | |
1176 | attr = current_attr; | |
1177 | attr.dimension = 0; | |
be59db2d | 1178 | attr.codimension = 0; |
6de9cd9a | 1179 | |
524af0d6 JB |
1180 | if (!gfc_copy_attr (&sym->attr, &attr, var_locus)) |
1181 | return false; | |
6de9cd9a | 1182 | |
a8b3b0b6 CR |
1183 | /* Finish any work that may need to be done for the binding label, |
1184 | if it's a bind(c). The bind(c) attr is found before the symbol | |
1185 | is made, and before the symbol name (for data decls), so the | |
1186 | current_ts is holding the binding label, or nothing if the | |
1187 | name= attr wasn't given. Therefore, test here if we're dealing | |
1188 | with a bind(c) and make sure the binding label is set correctly. */ | |
1189 | if (sym->attr.is_bind_c == 1) | |
1190 | { | |
62603fae | 1191 | if (!sym->binding_label) |
a8b3b0b6 | 1192 | { |
ad4a2f64 TB |
1193 | /* Set the binding label and verify that if a NAME= was specified |
1194 | then only one identifier was in the entity-decl-list. */ | |
524af0d6 JB |
1195 | if (!set_binding_label (&sym->binding_label, sym->name, |
1196 | num_idents_on_line)) | |
1197 | return false; | |
a8b3b0b6 CR |
1198 | } |
1199 | } | |
1200 | ||
1201 | /* See if we know we're in a common block, and if it's a bind(c) | |
1202 | common then we need to make sure we're an interoperable type. */ | |
1203 | if (sym->attr.in_common == 1) | |
1204 | { | |
1205 | /* Test the common block object. */ | |
1206 | if (sym->common_block != NULL && sym->common_block->is_bind_c == 1 | |
1207 | && sym->ts.is_c_interop != 1) | |
1208 | { | |
4daa149b | 1209 | gfc_error_now ("Variable %qs in common block %qs at %C " |
a8b3b0b6 | 1210 | "must be declared with a C interoperable " |
4daa149b | 1211 | "kind since common block %qs is BIND(C)", |
a8b3b0b6 CR |
1212 | sym->name, sym->common_block->name, |
1213 | sym->common_block->name); | |
1214 | gfc_clear_error (); | |
1215 | } | |
1216 | } | |
1217 | ||
9a3db5a3 PT |
1218 | sym->attr.implied_index = 0; |
1219 | ||
528622fd | 1220 | if (sym->ts.type == BT_CLASS) |
9b6da3c7 | 1221 | return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as); |
cf2b3c22 | 1222 | |
524af0d6 | 1223 | return true; |
6de9cd9a DN |
1224 | } |
1225 | ||
636dff67 | 1226 | |
df7cc9b5 | 1227 | /* Set character constant to the given length. The constant will be padded or |
d2848082 DK |
1228 | truncated. If we're inside an array constructor without a typespec, we |
1229 | additionally check that all elements have the same length; check_len -1 | |
1230 | means no checking. */ | |
df7cc9b5 FW |
1231 | |
1232 | void | |
d2848082 | 1233 | gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len) |
df7cc9b5 | 1234 | { |
00660189 | 1235 | gfc_char_t *s; |
df7cc9b5 FW |
1236 | int slen; |
1237 | ||
1238 | gcc_assert (expr->expr_type == EXPR_CONSTANT); | |
d393bbd7 | 1239 | gcc_assert (expr->ts.type == BT_CHARACTER); |
df7cc9b5 FW |
1240 | |
1241 | slen = expr->value.character.length; | |
1242 | if (len != slen) | |
1243 | { | |
00660189 FXC |
1244 | s = gfc_get_wide_string (len + 1); |
1245 | memcpy (s, expr->value.character.string, | |
1246 | MIN (len, slen) * sizeof (gfc_char_t)); | |
df7cc9b5 | 1247 | if (len > slen) |
00660189 | 1248 | gfc_wide_memset (&s[slen], ' ', len - slen); |
2220652d | 1249 | |
a96c39ea | 1250 | if (warn_character_truncation && slen > len) |
4daa149b TB |
1251 | gfc_warning_now (OPT_Wcharacter_truncation, |
1252 | "CHARACTER expression at %L is being truncated " | |
1253 | "(%d/%d)", &expr->where, slen, len); | |
2220652d PT |
1254 | |
1255 | /* Apply the standard by 'hand' otherwise it gets cleared for | |
1256 | initializers. */ | |
d2848082 DK |
1257 | if (check_len != -1 && slen != check_len |
1258 | && !(gfc_option.allow_std & GFC_STD_GNU)) | |
2220652d PT |
1259 | gfc_error_now ("The CHARACTER elements of the array constructor " |
1260 | "at %L must have the same length (%d/%d)", | |
d2848082 | 1261 | &expr->where, slen, check_len); |
2220652d | 1262 | |
150675a8 | 1263 | s[len] = '\0'; |
cede9502 | 1264 | free (expr->value.character.string); |
df7cc9b5 FW |
1265 | expr->value.character.string = s; |
1266 | expr->value.character.length = len; | |
1267 | } | |
1268 | } | |
6de9cd9a | 1269 | |
25d8f0a2 | 1270 | |
d51347f9 | 1271 | /* Function to create and update the enumerator history |
25d8f0a2 | 1272 | using the information passed as arguments. |
d51347f9 TB |
1273 | Pointer "max_enum" is also updated, to point to |
1274 | enum history node containing largest initializer. | |
25d8f0a2 TS |
1275 | |
1276 | SYM points to the symbol node of enumerator. | |
66e4ab31 | 1277 | INIT points to its enumerator value. */ |
25d8f0a2 | 1278 | |
d51347f9 | 1279 | static void |
636dff67 | 1280 | create_enum_history (gfc_symbol *sym, gfc_expr *init) |
25d8f0a2 TS |
1281 | { |
1282 | enumerator_history *new_enum_history; | |
1283 | gcc_assert (sym != NULL && init != NULL); | |
1284 | ||
ece3f663 | 1285 | new_enum_history = XCNEW (enumerator_history); |
25d8f0a2 TS |
1286 | |
1287 | new_enum_history->sym = sym; | |
1288 | new_enum_history->initializer = init; | |
1289 | new_enum_history->next = NULL; | |
1290 | ||
1291 | if (enum_history == NULL) | |
1292 | { | |
1293 | enum_history = new_enum_history; | |
1294 | max_enum = enum_history; | |
1295 | } | |
1296 | else | |
1297 | { | |
1298 | new_enum_history->next = enum_history; | |
1299 | enum_history = new_enum_history; | |
1300 | ||
d51347f9 | 1301 | if (mpz_cmp (max_enum->initializer->value.integer, |
25d8f0a2 | 1302 | new_enum_history->initializer->value.integer) < 0) |
636dff67 | 1303 | max_enum = new_enum_history; |
25d8f0a2 TS |
1304 | } |
1305 | } | |
1306 | ||
1307 | ||
d51347f9 | 1308 | /* Function to free enum kind history. */ |
25d8f0a2 | 1309 | |
d51347f9 | 1310 | void |
636dff67 | 1311 | gfc_free_enum_history (void) |
25d8f0a2 | 1312 | { |
d51347f9 TB |
1313 | enumerator_history *current = enum_history; |
1314 | enumerator_history *next; | |
25d8f0a2 TS |
1315 | |
1316 | while (current != NULL) | |
1317 | { | |
1318 | next = current->next; | |
cede9502 | 1319 | free (current); |
25d8f0a2 TS |
1320 | current = next; |
1321 | } | |
1322 | max_enum = NULL; | |
1323 | enum_history = NULL; | |
1324 | } | |
1325 | ||
1326 | ||
6de9cd9a DN |
1327 | /* Function called by variable_decl() that adds an initialization |
1328 | expression to a symbol. */ | |
1329 | ||
524af0d6 | 1330 | static bool |
66e4ab31 | 1331 | add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus) |
6de9cd9a DN |
1332 | { |
1333 | symbol_attribute attr; | |
1334 | gfc_symbol *sym; | |
1335 | gfc_expr *init; | |
1336 | ||
1337 | init = *initp; | |
08a6b8e0 | 1338 | if (find_special (name, &sym, false)) |
524af0d6 | 1339 | return false; |
6de9cd9a DN |
1340 | |
1341 | attr = sym->attr; | |
1342 | ||
1343 | /* If this symbol is confirming an implicit parameter type, | |
1344 | then an initialization expression is not allowed. */ | |
1345 | if (attr.flavor == FL_PARAMETER | |
1346 | && sym->value != NULL | |
1347 | && *initp != NULL) | |
1348 | { | |
c4100eae | 1349 | gfc_error ("Initializer not allowed for PARAMETER %qs at %C", |
6de9cd9a | 1350 | sym->name); |
524af0d6 | 1351 | return false; |
6de9cd9a DN |
1352 | } |
1353 | ||
1354 | if (init == NULL) | |
1355 | { | |
1356 | /* An initializer is required for PARAMETER declarations. */ | |
1357 | if (attr.flavor == FL_PARAMETER) | |
1358 | { | |
1359 | gfc_error ("PARAMETER at %L is missing an initializer", var_locus); | |
524af0d6 | 1360 | return false; |
6de9cd9a DN |
1361 | } |
1362 | } | |
1363 | else | |
1364 | { | |
1365 | /* If a variable appears in a DATA block, it cannot have an | |
1de8a836 | 1366 | initializer. */ |
6de9cd9a DN |
1367 | if (sym->attr.data) |
1368 | { | |
c4100eae | 1369 | gfc_error ("Variable %qs at %C with an initializer already " |
636dff67 | 1370 | "appears in a DATA statement", sym->name); |
524af0d6 | 1371 | return false; |
6de9cd9a DN |
1372 | } |
1373 | ||
75d17889 | 1374 | /* Check if the assignment can happen. This has to be put off |
80f95228 | 1375 | until later for derived type variables and procedure pointers. */ |
6de9cd9a | 1376 | if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED |
cf2b3c22 | 1377 | && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS |
f5acf0f2 | 1378 | && !sym->attr.proc_pointer |
524af0d6 JB |
1379 | && !gfc_check_assign_symbol (sym, NULL, init)) |
1380 | return false; | |
6de9cd9a | 1381 | |
bc21d315 | 1382 | if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl |
51b128a0 | 1383 | && init->ts.type == BT_CHARACTER) |
df7cc9b5 FW |
1384 | { |
1385 | /* Update symbol character length according initializer. */ | |
524af0d6 JB |
1386 | if (!gfc_check_assign_symbol (sym, NULL, init)) |
1387 | return false; | |
51b128a0 | 1388 | |
bc21d315 | 1389 | if (sym->ts.u.cl->length == NULL) |
df7cc9b5 | 1390 | { |
a99288e5 | 1391 | int clen; |
66e4ab31 SK |
1392 | /* If there are multiple CHARACTER variables declared on the |
1393 | same line, we don't want them to share the same length. */ | |
b76e28c6 | 1394 | sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
96f4873b | 1395 | |
a99288e5 PT |
1396 | if (sym->attr.flavor == FL_PARAMETER) |
1397 | { | |
1398 | if (init->expr_type == EXPR_CONSTANT) | |
1399 | { | |
1400 | clen = init->value.character.length; | |
b7e75771 JD |
1401 | sym->ts.u.cl->length |
1402 | = gfc_get_int_expr (gfc_default_integer_kind, | |
1403 | NULL, clen); | |
a99288e5 PT |
1404 | } |
1405 | else if (init->expr_type == EXPR_ARRAY) | |
1406 | { | |
b7e75771 JD |
1407 | gfc_constructor *c; |
1408 | c = gfc_constructor_first (init->value.constructor); | |
1409 | clen = c->expr->value.character.length; | |
1410 | sym->ts.u.cl->length | |
1411 | = gfc_get_int_expr (gfc_default_integer_kind, | |
1412 | NULL, clen); | |
a99288e5 | 1413 | } |
bc21d315 JW |
1414 | else if (init->ts.u.cl && init->ts.u.cl->length) |
1415 | sym->ts.u.cl->length = | |
1416 | gfc_copy_expr (sym->value->ts.u.cl->length); | |
a99288e5 | 1417 | } |
df7cc9b5 FW |
1418 | } |
1419 | /* Update initializer character length according symbol. */ | |
bc21d315 | 1420 | else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
df7cc9b5 | 1421 | { |
bc21d315 | 1422 | int len = mpz_get_si (sym->ts.u.cl->length->value.integer); |
df7cc9b5 FW |
1423 | |
1424 | if (init->expr_type == EXPR_CONSTANT) | |
d2848082 | 1425 | gfc_set_constant_character_len (len, init, -1); |
df7cc9b5 FW |
1426 | else if (init->expr_type == EXPR_ARRAY) |
1427 | { | |
b7e75771 JD |
1428 | gfc_constructor *c; |
1429 | ||
dcdc7b6c PT |
1430 | /* Build a new charlen to prevent simplification from |
1431 | deleting the length before it is resolved. */ | |
b76e28c6 | 1432 | init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
bc21d315 | 1433 | init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length); |
dcdc7b6c | 1434 | |
b7e75771 JD |
1435 | for (c = gfc_constructor_first (init->value.constructor); |
1436 | c; c = gfc_constructor_next (c)) | |
1437 | gfc_set_constant_character_len (len, c->expr, -1); | |
df7cc9b5 FW |
1438 | } |
1439 | } | |
1440 | } | |
1441 | ||
f5ca06e6 DK |
1442 | /* If sym is implied-shape, set its upper bounds from init. */ |
1443 | if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension | |
1444 | && sym->as->type == AS_IMPLIED_SHAPE) | |
1445 | { | |
1446 | int dim; | |
1447 | ||
1448 | if (init->rank == 0) | |
1449 | { | |
1450 | gfc_error ("Can't initialize implied-shape array at %L" | |
1451 | " with scalar", &sym->declared_at); | |
524af0d6 | 1452 | return false; |
f5ca06e6 DK |
1453 | } |
1454 | gcc_assert (sym->as->rank == init->rank); | |
1455 | ||
1456 | /* Shape should be present, we get an initialization expression. */ | |
1457 | gcc_assert (init->shape); | |
1458 | ||
1459 | for (dim = 0; dim < sym->as->rank; ++dim) | |
1460 | { | |
1461 | int k; | |
1462 | gfc_expr* lower; | |
1463 | gfc_expr* e; | |
f5acf0f2 | 1464 | |
f5ca06e6 DK |
1465 | lower = sym->as->lower[dim]; |
1466 | if (lower->expr_type != EXPR_CONSTANT) | |
1467 | { | |
1468 | gfc_error ("Non-constant lower bound in implied-shape" | |
1469 | " declaration at %L", &lower->where); | |
524af0d6 | 1470 | return false; |
f5ca06e6 DK |
1471 | } |
1472 | ||
1473 | /* All dimensions must be without upper bound. */ | |
1474 | gcc_assert (!sym->as->upper[dim]); | |
1475 | ||
1476 | k = lower->ts.kind; | |
1477 | e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at); | |
1478 | mpz_add (e->value.integer, | |
1479 | lower->value.integer, init->shape[dim]); | |
1480 | mpz_sub_ui (e->value.integer, e->value.integer, 1); | |
1481 | sym->as->upper[dim] = e; | |
1482 | } | |
1483 | ||
1484 | sym->as->type = AS_EXPLICIT; | |
1485 | } | |
1486 | ||
a8b3b0b6 CR |
1487 | /* Need to check if the expression we initialized this |
1488 | to was one of the iso_c_binding named constants. If so, | |
1489 | and we're a parameter (constant), let it be iso_c. | |
1490 | For example: | |
1491 | integer(c_int), parameter :: my_int = c_int | |
1492 | integer(my_int) :: my_int_2 | |
1493 | If we mark my_int as iso_c (since we can see it's value | |
1494 | is equal to one of the named constants), then my_int_2 | |
1495 | will be considered C interoperable. */ | |
1496 | if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED) | |
1497 | { | |
1498 | sym->ts.is_iso_c |= init->ts.is_iso_c; | |
1499 | sym->ts.is_c_interop |= init->ts.is_c_interop; | |
1500 | /* attr bits needed for module files. */ | |
1501 | sym->attr.is_iso_c |= init->ts.is_iso_c; | |
1502 | sym->attr.is_c_interop |= init->ts.is_c_interop; | |
1503 | if (init->ts.is_iso_c) | |
1504 | sym->ts.f90_type = init->ts.f90_type; | |
1505 | } | |
b7e75771 | 1506 | |
6de9cd9a DN |
1507 | /* Add initializer. Make sure we keep the ranks sane. */ |
1508 | if (sym->attr.dimension && init->rank == 0) | |
a9b43781 PT |
1509 | { |
1510 | mpz_t size; | |
1511 | gfc_expr *array; | |
a9b43781 PT |
1512 | int n; |
1513 | if (sym->attr.flavor == FL_PARAMETER | |
1514 | && init->expr_type == EXPR_CONSTANT | |
524af0d6 | 1515 | && spec_size (sym->as, &size) |
a9b43781 PT |
1516 | && mpz_cmp_si (size, 0) > 0) |
1517 | { | |
b7e75771 JD |
1518 | array = gfc_get_array_expr (init->ts.type, init->ts.kind, |
1519 | &init->where); | |
a9b43781 | 1520 | for (n = 0; n < (int)mpz_get_si (size); n++) |
b7e75771 JD |
1521 | gfc_constructor_append_expr (&array->value.constructor, |
1522 | n == 0 | |
1523 | ? init | |
1524 | : gfc_copy_expr (init), | |
1525 | &init->where); | |
f5acf0f2 | 1526 | |
a9b43781 PT |
1527 | array->shape = gfc_get_shape (sym->as->rank); |
1528 | for (n = 0; n < sym->as->rank; n++) | |
1529 | spec_dimen_size (sym->as, n, &array->shape[n]); | |
1530 | ||
1531 | init = array; | |
1532 | mpz_clear (size); | |
1533 | } | |
1534 | init->rank = sym->as->rank; | |
1535 | } | |
6de9cd9a DN |
1536 | |
1537 | sym->value = init; | |
ef7236d2 DF |
1538 | if (sym->attr.save == SAVE_NONE) |
1539 | sym->attr.save = SAVE_IMPLICIT; | |
6de9cd9a DN |
1540 | *initp = NULL; |
1541 | } | |
1542 | ||
524af0d6 | 1543 | return true; |
6de9cd9a DN |
1544 | } |
1545 | ||
1546 | ||
1547 | /* Function called by variable_decl() that adds a name to a structure | |
1548 | being built. */ | |
1549 | ||
524af0d6 | 1550 | static bool |
636dff67 SK |
1551 | build_struct (const char *name, gfc_charlen *cl, gfc_expr **init, |
1552 | gfc_array_spec **as) | |
6de9cd9a DN |
1553 | { |
1554 | gfc_component *c; | |
524af0d6 | 1555 | bool t = true; |
6de9cd9a | 1556 | |
619dd721 | 1557 | /* F03:C438/C439. If the current symbol is of the same derived type that we're |
6de9cd9a | 1558 | constructing, it must have the pointer attribute. */ |
619dd721 | 1559 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
bc21d315 | 1560 | && current_ts.u.derived == gfc_current_block () |
6de9cd9a DN |
1561 | && current_attr.pointer == 0) |
1562 | { | |
1563 | gfc_error ("Component at %C must have the POINTER attribute"); | |
524af0d6 | 1564 | return false; |
6de9cd9a DN |
1565 | } |
1566 | ||
636dff67 | 1567 | if (gfc_current_block ()->attr.pointer && (*as)->rank != 0) |
6de9cd9a DN |
1568 | { |
1569 | if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT) | |
1570 | { | |
1571 | gfc_error ("Array component of structure at %C must have explicit " | |
1572 | "or deferred shape"); | |
524af0d6 | 1573 | return false; |
6de9cd9a DN |
1574 | } |
1575 | } | |
1576 | ||
524af0d6 JB |
1577 | if (!gfc_add_component (gfc_current_block(), name, &c)) |
1578 | return false; | |
6de9cd9a DN |
1579 | |
1580 | c->ts = current_ts; | |
bc21d315 JW |
1581 | if (c->ts.type == BT_CHARACTER) |
1582 | c->ts.u.cl = cl; | |
d4b7d0f0 | 1583 | c->attr = current_attr; |
6de9cd9a DN |
1584 | |
1585 | c->initializer = *init; | |
1586 | *init = NULL; | |
1587 | ||
1588 | c->as = *as; | |
1589 | if (c->as != NULL) | |
be59db2d TB |
1590 | { |
1591 | if (c->as->corank) | |
1592 | c->attr.codimension = 1; | |
1593 | if (c->as->rank) | |
1594 | c->attr.dimension = 1; | |
1595 | } | |
6de9cd9a DN |
1596 | *as = NULL; |
1597 | ||
28d08315 PT |
1598 | /* Should this ever get more complicated, combine with similar section |
1599 | in add_init_expr_to_sym into a separate function. */ | |
f4347334 ZG |
1600 | if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer |
1601 | && c->ts.u.cl | |
bc21d315 | 1602 | && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
28d08315 | 1603 | { |
d2848082 DK |
1604 | int len; |
1605 | ||
bc21d315 JW |
1606 | gcc_assert (c->ts.u.cl && c->ts.u.cl->length); |
1607 | gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT); | |
1608 | gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER); | |
d2848082 | 1609 | |
bc21d315 | 1610 | len = mpz_get_si (c->ts.u.cl->length->value.integer); |
28d08315 PT |
1611 | |
1612 | if (c->initializer->expr_type == EXPR_CONSTANT) | |
d2848082 | 1613 | gfc_set_constant_character_len (len, c->initializer, -1); |
bc21d315 JW |
1614 | else if (mpz_cmp (c->ts.u.cl->length->value.integer, |
1615 | c->initializer->ts.u.cl->length->value.integer)) | |
28d08315 | 1616 | { |
b7e75771 JD |
1617 | gfc_constructor *ctor; |
1618 | ctor = gfc_constructor_first (c->initializer->value.constructor); | |
d2848082 | 1619 | |
2f4d1994 | 1620 | if (ctor) |
d2848082 | 1621 | { |
2f4d1994 | 1622 | int first_len; |
b7e75771 JD |
1623 | bool has_ts = (c->initializer->ts.u.cl |
1624 | && c->initializer->ts.u.cl->length_from_typespec); | |
2f4d1994 ILT |
1625 | |
1626 | /* Remember the length of the first element for checking | |
1627 | that all elements *in the constructor* have the same | |
1628 | length. This need not be the length of the LHS! */ | |
1629 | gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT); | |
1630 | gcc_assert (ctor->expr->ts.type == BT_CHARACTER); | |
1631 | first_len = ctor->expr->value.character.length; | |
1632 | ||
b7e75771 JD |
1633 | for ( ; ctor; ctor = gfc_constructor_next (ctor)) |
1634 | if (ctor->expr->expr_type == EXPR_CONSTANT) | |
d2848082 | 1635 | { |
b7e75771 JD |
1636 | gfc_set_constant_character_len (len, ctor->expr, |
1637 | has_ts ? -1 : first_len); | |
1638 | ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length); | |
d2848082 | 1639 | } |
d2848082 | 1640 | } |
28d08315 PT |
1641 | } |
1642 | } | |
1643 | ||
6de9cd9a | 1644 | /* Check array components. */ |
d4b7d0f0 | 1645 | if (!c->attr.dimension) |
2e23972e | 1646 | goto scalar; |
6de9cd9a | 1647 | |
d4b7d0f0 | 1648 | if (c->attr.pointer) |
6de9cd9a DN |
1649 | { |
1650 | if (c->as->type != AS_DEFERRED) | |
1651 | { | |
5046aff5 PT |
1652 | gfc_error ("Pointer array component of structure at %C must have a " |
1653 | "deferred shape"); | |
524af0d6 | 1654 | t = false; |
5046aff5 PT |
1655 | } |
1656 | } | |
d4b7d0f0 | 1657 | else if (c->attr.allocatable) |
5046aff5 PT |
1658 | { |
1659 | if (c->as->type != AS_DEFERRED) | |
1660 | { | |
1661 | gfc_error ("Allocatable component of structure at %C must have a " | |
1662 | "deferred shape"); | |
524af0d6 | 1663 | t = false; |
6de9cd9a DN |
1664 | } |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | if (c->as->type != AS_EXPLICIT) | |
1669 | { | |
636dff67 SK |
1670 | gfc_error ("Array component of structure at %C must have an " |
1671 | "explicit shape"); | |
524af0d6 | 1672 | t = false; |
6de9cd9a DN |
1673 | } |
1674 | } | |
1675 | ||
2e23972e JW |
1676 | scalar: |
1677 | if (c->ts.type == BT_CLASS) | |
17643884 | 1678 | { |
9b6da3c7 | 1679 | bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as); |
ea59b186 | 1680 | |
524af0d6 | 1681 | if (t) |
ea59b186 | 1682 | t = t2; |
17643884 JW |
1683 | } |
1684 | ||
2e23972e | 1685 | return t; |
6de9cd9a DN |
1686 | } |
1687 | ||
1688 | ||
1689 | /* Match a 'NULL()', and possibly take care of some side effects. */ | |
1690 | ||
1691 | match | |
636dff67 | 1692 | gfc_match_null (gfc_expr **result) |
6de9cd9a DN |
1693 | { |
1694 | gfc_symbol *sym; | |
576f6da6 | 1695 | match m, m2 = MATCH_NO; |
6de9cd9a | 1696 | |
576f6da6 TB |
1697 | if ((m = gfc_match (" null ( )")) == MATCH_ERROR) |
1698 | return MATCH_ERROR; | |
1699 | ||
1700 | if (m == MATCH_NO) | |
1701 | { | |
1702 | locus old_loc; | |
1703 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
1704 | ||
94241120 | 1705 | if ((m2 = gfc_match (" null (")) != MATCH_YES) |
576f6da6 TB |
1706 | return m2; |
1707 | ||
1708 | old_loc = gfc_current_locus; | |
1709 | if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR) | |
1710 | return MATCH_ERROR; | |
1711 | if (m2 != MATCH_YES | |
1712 | && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR)) | |
1713 | return MATCH_ERROR; | |
1714 | if (m2 == MATCH_NO) | |
1715 | { | |
1716 | gfc_current_locus = old_loc; | |
1717 | return MATCH_NO; | |
1718 | } | |
1719 | } | |
6de9cd9a DN |
1720 | |
1721 | /* The NULL symbol now has to be/become an intrinsic function. */ | |
1722 | if (gfc_get_symbol ("null", NULL, &sym)) | |
1723 | { | |
1724 | gfc_error ("NULL() initialization at %C is ambiguous"); | |
1725 | return MATCH_ERROR; | |
1726 | } | |
1727 | ||
1728 | gfc_intrinsic_symbol (sym); | |
1729 | ||
1730 | if (sym->attr.proc != PROC_INTRINSIC | |
07416986 | 1731 | && !(sym->attr.use_assoc && sym->attr.intrinsic) |
524af0d6 JB |
1732 | && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL) |
1733 | || !gfc_add_function (&sym->attr, sym->name, NULL))) | |
6de9cd9a DN |
1734 | return MATCH_ERROR; |
1735 | ||
b7e75771 | 1736 | *result = gfc_get_null_expr (&gfc_current_locus); |
6de9cd9a | 1737 | |
576f6da6 TB |
1738 | /* Invalid per F2008, C512. */ |
1739 | if (m2 == MATCH_YES) | |
1740 | { | |
1741 | gfc_error ("NULL() initialization at %C may not have MOLD"); | |
1742 | return MATCH_ERROR; | |
1743 | } | |
1744 | ||
6de9cd9a DN |
1745 | return MATCH_YES; |
1746 | } | |
1747 | ||
1748 | ||
80f95228 JW |
1749 | /* Match the initialization expr for a data pointer or procedure pointer. */ |
1750 | ||
1751 | static match | |
1752 | match_pointer_init (gfc_expr **init, int procptr) | |
1753 | { | |
1754 | match m; | |
1755 | ||
1756 | if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED) | |
1757 | { | |
1758 | gfc_error ("Initialization of pointer at %C is not allowed in " | |
1759 | "a PURE procedure"); | |
1760 | return MATCH_ERROR; | |
1761 | } | |
ccd7751b | 1762 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
80f95228 | 1763 | |
eea58adb | 1764 | /* Match NULL() initialization. */ |
80f95228 JW |
1765 | m = gfc_match_null (init); |
1766 | if (m != MATCH_NO) | |
1767 | return m; | |
1768 | ||
1769 | /* Match non-NULL initialization. */ | |
837c4b78 | 1770 | gfc_matching_ptr_assignment = !procptr; |
80f95228 JW |
1771 | gfc_matching_procptr_assignment = procptr; |
1772 | m = gfc_match_rvalue (init); | |
837c4b78 | 1773 | gfc_matching_ptr_assignment = 0; |
80f95228 JW |
1774 | gfc_matching_procptr_assignment = 0; |
1775 | if (m == MATCH_ERROR) | |
1776 | return MATCH_ERROR; | |
1777 | else if (m == MATCH_NO) | |
1778 | { | |
1779 | gfc_error ("Error in pointer initialization at %C"); | |
1780 | return MATCH_ERROR; | |
1781 | } | |
1782 | ||
dc9a54fa JW |
1783 | if (!procptr && !gfc_resolve_expr (*init)) |
1784 | return MATCH_ERROR; | |
f5acf0f2 | 1785 | |
524af0d6 JB |
1786 | if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer " |
1787 | "initialization at %C")) | |
80f95228 JW |
1788 | return MATCH_ERROR; |
1789 | ||
1790 | return MATCH_YES; | |
1791 | } | |
1792 | ||
1793 | ||
524af0d6 | 1794 | static bool |
bb9de0c4 JW |
1795 | check_function_name (char *name) |
1796 | { | |
1797 | /* In functions that have a RESULT variable defined, the function name always | |
1798 | refers to function calls. Therefore, the name is not allowed to appear in | |
1799 | specification statements. When checking this, be careful about | |
1800 | 'hidden' procedure pointer results ('ppr@'). */ | |
1801 | ||
1802 | if (gfc_current_state () == COMP_FUNCTION) | |
1803 | { | |
1804 | gfc_symbol *block = gfc_current_block (); | |
1805 | if (block && block->result && block->result != block | |
1806 | && strcmp (block->result->name, "ppr@") != 0 | |
1807 | && strcmp (block->name, name) == 0) | |
1808 | { | |
c4100eae | 1809 | gfc_error ("Function name %qs not allowed at %C", name); |
524af0d6 | 1810 | return false; |
bb9de0c4 JW |
1811 | } |
1812 | } | |
1813 | ||
524af0d6 | 1814 | return true; |
bb9de0c4 JW |
1815 | } |
1816 | ||
1817 | ||
6de9cd9a DN |
1818 | /* Match a variable name with an optional initializer. When this |
1819 | subroutine is called, a variable is expected to be parsed next. | |
1820 | Depending on what is happening at the moment, updates either the | |
1821 | symbol table or the current interface. */ | |
1822 | ||
1823 | static match | |
949d5b72 | 1824 | variable_decl (int elem) |
6de9cd9a DN |
1825 | { |
1826 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
1827 | gfc_expr *initializer, *char_len; | |
1828 | gfc_array_spec *as; | |
83d890b9 | 1829 | gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */ |
6de9cd9a | 1830 | gfc_charlen *cl; |
e69afb29 | 1831 | bool cl_deferred; |
6de9cd9a DN |
1832 | locus var_locus; |
1833 | match m; | |
524af0d6 | 1834 | bool t; |
83d890b9 | 1835 | gfc_symbol *sym; |
6de9cd9a DN |
1836 | |
1837 | initializer = NULL; | |
1838 | as = NULL; | |
83d890b9 | 1839 | cp_as = NULL; |
6de9cd9a DN |
1840 | |
1841 | /* When we get here, we've just matched a list of attributes and | |
1842 | maybe a type and a double colon. The next thing we expect to see | |
1843 | is the name of the symbol. */ | |
1844 | m = gfc_match_name (name); | |
1845 | if (m != MATCH_YES) | |
1846 | goto cleanup; | |
1847 | ||
63645982 | 1848 | var_locus = gfc_current_locus; |
6de9cd9a DN |
1849 | |
1850 | /* Now we could see the optional array spec. or character length. */ | |
be59db2d | 1851 | m = gfc_match_array_spec (&as, true, true); |
11126dc0 | 1852 | if (m == MATCH_ERROR) |
6de9cd9a | 1853 | goto cleanup; |
25d8f0a2 | 1854 | |
6de9cd9a DN |
1855 | if (m == MATCH_NO) |
1856 | as = gfc_copy_array_spec (current_as); | |
63fbf586 | 1857 | else if (current_as |
524af0d6 | 1858 | && !merge_array_spec (current_as, as, true)) |
63fbf586 TB |
1859 | { |
1860 | m = MATCH_ERROR; | |
1861 | goto cleanup; | |
1862 | } | |
6de9cd9a | 1863 | |
c61819ff | 1864 | if (flag_cray_pointer) |
11126dc0 AL |
1865 | cp_as = gfc_copy_array_spec (as); |
1866 | ||
f5ca06e6 DK |
1867 | /* At this point, we know for sure if the symbol is PARAMETER and can thus |
1868 | determine (and check) whether it can be implied-shape. If it | |
1869 | was parsed as assumed-size, change it because PARAMETERs can not | |
1870 | be assumed-size. */ | |
1871 | if (as) | |
1872 | { | |
1873 | if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER) | |
1874 | { | |
1875 | m = MATCH_ERROR; | |
c4100eae | 1876 | gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape", |
f5ca06e6 DK |
1877 | name, &var_locus); |
1878 | goto cleanup; | |
1879 | } | |
1880 | ||
1881 | if (as->type == AS_ASSUMED_SIZE && as->rank == 1 | |
1882 | && current_attr.flavor == FL_PARAMETER) | |
1883 | as->type = AS_IMPLIED_SHAPE; | |
1884 | ||
1885 | if (as->type == AS_IMPLIED_SHAPE | |
524af0d6 JB |
1886 | && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L", |
1887 | &var_locus)) | |
f5ca06e6 DK |
1888 | { |
1889 | m = MATCH_ERROR; | |
1890 | goto cleanup; | |
1891 | } | |
1892 | } | |
1893 | ||
6de9cd9a DN |
1894 | char_len = NULL; |
1895 | cl = NULL; | |
e69afb29 | 1896 | cl_deferred = false; |
6de9cd9a DN |
1897 | |
1898 | if (current_ts.type == BT_CHARACTER) | |
1899 | { | |
2767f2cc | 1900 | switch (match_char_length (&char_len, &cl_deferred, false)) |
6de9cd9a DN |
1901 | { |
1902 | case MATCH_YES: | |
b76e28c6 | 1903 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
6de9cd9a DN |
1904 | |
1905 | cl->length = char_len; | |
1906 | break; | |
1907 | ||
949d5b72 | 1908 | /* Non-constant lengths need to be copied after the first |
9b21a380 | 1909 | element. Also copy assumed lengths. */ |
6de9cd9a | 1910 | case MATCH_NO: |
9b21a380 | 1911 | if (elem > 1 |
bc21d315 JW |
1912 | && (current_ts.u.cl->length == NULL |
1913 | || current_ts.u.cl->length->expr_type != EXPR_CONSTANT)) | |
949d5b72 | 1914 | { |
b76e28c6 | 1915 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
bc21d315 | 1916 | cl->length = gfc_copy_expr (current_ts.u.cl->length); |
949d5b72 PT |
1917 | } |
1918 | else | |
bc21d315 | 1919 | cl = current_ts.u.cl; |
949d5b72 | 1920 | |
e69afb29 SK |
1921 | cl_deferred = current_ts.deferred; |
1922 | ||
6de9cd9a DN |
1923 | break; |
1924 | ||
1925 | case MATCH_ERROR: | |
1926 | goto cleanup; | |
1927 | } | |
1928 | } | |
1929 | ||
83d890b9 | 1930 | /* If this symbol has already shown up in a Cray Pointer declaration, |
88f7d6fb | 1931 | and this is not a component declaration, |
66e4ab31 | 1932 | then we want to set the type & bail out. */ |
c61819ff | 1933 | if (flag_cray_pointer && gfc_current_state () != COMP_DERIVED) |
83d890b9 AL |
1934 | { |
1935 | gfc_find_symbol (name, gfc_current_ns, 1, &sym); | |
1936 | if (sym != NULL && sym->attr.cray_pointee) | |
1937 | { | |
1938 | sym->ts.type = current_ts.type; | |
1939 | sym->ts.kind = current_ts.kind; | |
bc21d315 JW |
1940 | sym->ts.u.cl = cl; |
1941 | sym->ts.u.derived = current_ts.u.derived; | |
a8b3b0b6 CR |
1942 | sym->ts.is_c_interop = current_ts.is_c_interop; |
1943 | sym->ts.is_iso_c = current_ts.is_iso_c; | |
83d890b9 | 1944 | m = MATCH_YES; |
f5acf0f2 | 1945 | |
83d890b9 AL |
1946 | /* Check to see if we have an array specification. */ |
1947 | if (cp_as != NULL) | |
1948 | { | |
1949 | if (sym->as != NULL) | |
1950 | { | |
e25a0da3 | 1951 | gfc_error ("Duplicate array spec for Cray pointee at %C"); |
83d890b9 AL |
1952 | gfc_free_array_spec (cp_as); |
1953 | m = MATCH_ERROR; | |
1954 | goto cleanup; | |
1955 | } | |
1956 | else | |
1957 | { | |
524af0d6 | 1958 | if (!gfc_set_array_spec (sym, cp_as, &var_locus)) |
83d890b9 | 1959 | gfc_internal_error ("Couldn't set pointee array spec."); |
d51347f9 | 1960 | |
83d890b9 | 1961 | /* Fix the array spec. */ |
d51347f9 | 1962 | m = gfc_mod_pointee_as (sym->as); |
83d890b9 AL |
1963 | if (m == MATCH_ERROR) |
1964 | goto cleanup; | |
1965 | } | |
d51347f9 | 1966 | } |
83d890b9 AL |
1967 | goto cleanup; |
1968 | } | |
1969 | else | |
1970 | { | |
1971 | gfc_free_array_spec (cp_as); | |
1972 | } | |
1973 | } | |
d51347f9 | 1974 | |
3070bab4 JW |
1975 | /* Procedure pointer as function result. */ |
1976 | if (gfc_current_state () == COMP_FUNCTION | |
1977 | && strcmp ("ppr@", gfc_current_block ()->name) == 0 | |
1978 | && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0) | |
1979 | strcpy (name, "ppr@"); | |
1980 | ||
1981 | if (gfc_current_state () == COMP_FUNCTION | |
1982 | && strcmp (name, gfc_current_block ()->name) == 0 | |
1983 | && gfc_current_block ()->result | |
1984 | && strcmp ("ppr@", gfc_current_block ()->result->name) == 0) | |
1985 | strcpy (name, "ppr@"); | |
d51347f9 | 1986 | |
6de9cd9a DN |
1987 | /* OK, we've successfully matched the declaration. Now put the |
1988 | symbol in the current namespace, because it might be used in the | |
69de3b83 | 1989 | optional initialization expression for this symbol, e.g. this is |
6de9cd9a DN |
1990 | perfectly legal: |
1991 | ||
1992 | integer, parameter :: i = huge(i) | |
1993 | ||
1994 | This is only true for parameters or variables of a basic type. | |
1995 | For components of derived types, it is not true, so we don't | |
1996 | create a symbol for those yet. If we fail to create the symbol, | |
1997 | bail out. */ | |
1998 | if (gfc_current_state () != COMP_DERIVED | |
524af0d6 | 1999 | && !build_sym (name, cl, cl_deferred, &as, &var_locus)) |
6de9cd9a | 2000 | { |
72af9f0b PT |
2001 | m = MATCH_ERROR; |
2002 | goto cleanup; | |
2003 | } | |
2004 | ||
524af0d6 | 2005 | if (!check_function_name (name)) |
6de9cd9a | 2006 | { |
6de9cd9a DN |
2007 | m = MATCH_ERROR; |
2008 | goto cleanup; | |
2009 | } | |
2010 | ||
294fbfc8 TS |
2011 | /* We allow old-style initializations of the form |
2012 | integer i /2/, j(4) /3*3, 1/ | |
2013 | (if no colon has been seen). These are different from data | |
2014 | statements in that initializers are only allowed to apply to the | |
2015 | variable immediately preceding, i.e. | |
2016 | integer i, j /1, 2/ | |
2017 | is not allowed. Therefore we have to do some work manually, that | |
75d17889 | 2018 | could otherwise be left to the matchers for DATA statements. */ |
294fbfc8 TS |
2019 | |
2020 | if (!colon_seen && gfc_match (" /") == MATCH_YES) | |
2021 | { | |
524af0d6 JB |
2022 | if (!gfc_notify_std (GFC_STD_GNU, "Old-style " |
2023 | "initialization at %C")) | |
294fbfc8 | 2024 | return MATCH_ERROR; |
b18f1efc JJ |
2025 | else if (gfc_current_state () == COMP_DERIVED) |
2026 | { | |
2027 | gfc_error ("Invalid old style initialization for derived type " | |
2028 | "component at %C"); | |
2029 | m = MATCH_ERROR; | |
2030 | goto cleanup; | |
2031 | } | |
f5acf0f2 | 2032 | |
294fbfc8 TS |
2033 | return match_old_style_init (name); |
2034 | } | |
2035 | ||
6de9cd9a DN |
2036 | /* The double colon must be present in order to have initializers. |
2037 | Otherwise the statement is ambiguous with an assignment statement. */ | |
2038 | if (colon_seen) | |
2039 | { | |
2040 | if (gfc_match (" =>") == MATCH_YES) | |
2041 | { | |
6de9cd9a DN |
2042 | if (!current_attr.pointer) |
2043 | { | |
2044 | gfc_error ("Initialization at %C isn't for a pointer variable"); | |
2045 | m = MATCH_ERROR; | |
2046 | goto cleanup; | |
2047 | } | |
2048 | ||
80f95228 | 2049 | m = match_pointer_init (&initializer, 0); |
6de9cd9a DN |
2050 | if (m != MATCH_YES) |
2051 | goto cleanup; | |
6de9cd9a DN |
2052 | } |
2053 | else if (gfc_match_char ('=') == MATCH_YES) | |
2054 | { | |
2055 | if (current_attr.pointer) | |
2056 | { | |
a4d9b221 TB |
2057 | gfc_error ("Pointer initialization at %C requires %<=>%>, " |
2058 | "not %<=%>"); | |
6de9cd9a DN |
2059 | m = MATCH_ERROR; |
2060 | goto cleanup; | |
2061 | } | |
2062 | ||
2063 | m = gfc_match_init_expr (&initializer); | |
2064 | if (m == MATCH_NO) | |
2065 | { | |
2066 | gfc_error ("Expected an initialization expression at %C"); | |
2067 | m = MATCH_ERROR; | |
2068 | } | |
2069 | ||
ade20620 TB |
2070 | if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL) |
2071 | && gfc_state_stack->state != COMP_DERIVED) | |
6de9cd9a | 2072 | { |
636dff67 SK |
2073 | gfc_error ("Initialization of variable at %C is not allowed in " |
2074 | "a PURE procedure"); | |
6de9cd9a DN |
2075 | m = MATCH_ERROR; |
2076 | } | |
2077 | ||
ccd7751b TB |
2078 | if (current_attr.flavor != FL_PARAMETER |
2079 | && gfc_state_stack->state != COMP_DERIVED) | |
2080 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); | |
2081 | ||
6de9cd9a DN |
2082 | if (m != MATCH_YES) |
2083 | goto cleanup; | |
2084 | } | |
cb44ab82 VL |
2085 | } |
2086 | ||
5046aff5 PT |
2087 | if (initializer != NULL && current_attr.allocatable |
2088 | && gfc_current_state () == COMP_DERIVED) | |
2089 | { | |
636dff67 SK |
2090 | gfc_error ("Initialization of allocatable component at %C is not " |
2091 | "allowed"); | |
5046aff5 PT |
2092 | m = MATCH_ERROR; |
2093 | goto cleanup; | |
2094 | } | |
2095 | ||
54b4ba60 | 2096 | /* Add the initializer. Note that it is fine if initializer is |
6de9cd9a DN |
2097 | NULL here, because we sometimes also need to check if a |
2098 | declaration *must* have an initialization expression. */ | |
2099 | if (gfc_current_state () != COMP_DERIVED) | |
2100 | t = add_init_expr_to_sym (name, &initializer, &var_locus); | |
2101 | else | |
54b4ba60 | 2102 | { |
5046aff5 | 2103 | if (current_ts.type == BT_DERIVED |
636dff67 | 2104 | && !current_attr.pointer && !initializer) |
54b4ba60 PB |
2105 | initializer = gfc_default_initializer (¤t_ts); |
2106 | t = build_struct (name, cl, &initializer, &as); | |
2107 | } | |
6de9cd9a | 2108 | |
524af0d6 | 2109 | m = (t) ? MATCH_YES : MATCH_ERROR; |
6de9cd9a DN |
2110 | |
2111 | cleanup: | |
2112 | /* Free stuff up and return. */ | |
2113 | gfc_free_expr (initializer); | |
2114 | gfc_free_array_spec (as); | |
2115 | ||
2116 | return m; | |
2117 | } | |
2118 | ||
2119 | ||
b2b81a3f BM |
2120 | /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification. |
2121 | This assumes that the byte size is equal to the kind number for | |
2122 | non-COMPLEX types, and equal to twice the kind number for COMPLEX. */ | |
6de9cd9a DN |
2123 | |
2124 | match | |
636dff67 | 2125 | gfc_match_old_kind_spec (gfc_typespec *ts) |
6de9cd9a DN |
2126 | { |
2127 | match m; | |
5cf54585 | 2128 | int original_kind; |
6de9cd9a DN |
2129 | |
2130 | if (gfc_match_char ('*') != MATCH_YES) | |
2131 | return MATCH_NO; | |
2132 | ||
5cf54585 | 2133 | m = gfc_match_small_literal_int (&ts->kind, NULL); |
6de9cd9a DN |
2134 | if (m != MATCH_YES) |
2135 | return MATCH_ERROR; | |
2136 | ||
e45b3c75 ES |
2137 | original_kind = ts->kind; |
2138 | ||
6de9cd9a | 2139 | /* Massage the kind numbers for complex types. */ |
e45b3c75 ES |
2140 | if (ts->type == BT_COMPLEX) |
2141 | { | |
2142 | if (ts->kind % 2) | |
636dff67 SK |
2143 | { |
2144 | gfc_error ("Old-style type declaration %s*%d not supported at %C", | |
2145 | gfc_basic_typename (ts->type), original_kind); | |
2146 | return MATCH_ERROR; | |
2147 | } | |
e45b3c75 | 2148 | ts->kind /= 2; |
f4347334 ZG |
2149 | |
2150 | } | |
2151 | ||
203c7ebf | 2152 | if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8) |
f4347334 ZG |
2153 | ts->kind = 8; |
2154 | ||
2155 | if (ts->type == BT_REAL || ts->type == BT_COMPLEX) | |
2156 | { | |
2157 | if (ts->kind == 4) | |
2158 | { | |
203c7ebf | 2159 | if (flag_real4_kind == 8) |
f4347334 | 2160 | ts->kind = 8; |
203c7ebf | 2161 | if (flag_real4_kind == 10) |
f4347334 | 2162 | ts->kind = 10; |
203c7ebf | 2163 | if (flag_real4_kind == 16) |
f4347334 ZG |
2164 | ts->kind = 16; |
2165 | } | |
2166 | ||
2167 | if (ts->kind == 8) | |
2168 | { | |
203c7ebf | 2169 | if (flag_real8_kind == 4) |
f4347334 | 2170 | ts->kind = 4; |
203c7ebf | 2171 | if (flag_real8_kind == 10) |
f4347334 | 2172 | ts->kind = 10; |
203c7ebf | 2173 | if (flag_real8_kind == 16) |
f4347334 ZG |
2174 | ts->kind = 16; |
2175 | } | |
e45b3c75 | 2176 | } |
6de9cd9a | 2177 | |
e7a2d5fb | 2178 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a | 2179 | { |
e45b3c75 | 2180 | gfc_error ("Old-style type declaration %s*%d not supported at %C", |
636dff67 | 2181 | gfc_basic_typename (ts->type), original_kind); |
6de9cd9a DN |
2182 | return MATCH_ERROR; |
2183 | } | |
2184 | ||
524af0d6 JB |
2185 | if (!gfc_notify_std (GFC_STD_GNU, |
2186 | "Nonstandard type declaration %s*%d at %C", | |
2187 | gfc_basic_typename(ts->type), original_kind)) | |
df8652dc SK |
2188 | return MATCH_ERROR; |
2189 | ||
6de9cd9a DN |
2190 | return MATCH_YES; |
2191 | } | |
2192 | ||
2193 | ||
2194 | /* Match a kind specification. Since kinds are generally optional, we | |
2195 | usually return MATCH_NO if something goes wrong. If a "kind=" | |
2196 | string is found, then we know we have an error. */ | |
2197 | ||
2198 | match | |
e2d29968 | 2199 | gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only) |
6de9cd9a | 2200 | { |
e2d29968 | 2201 | locus where, loc; |
6de9cd9a DN |
2202 | gfc_expr *e; |
2203 | match m, n; | |
96ee3a4a | 2204 | char c; |
6de9cd9a DN |
2205 | const char *msg; |
2206 | ||
2207 | m = MATCH_NO; | |
e2d29968 | 2208 | n = MATCH_YES; |
6de9cd9a DN |
2209 | e = NULL; |
2210 | ||
e2d29968 PT |
2211 | where = loc = gfc_current_locus; |
2212 | ||
2213 | if (kind_expr_only) | |
2214 | goto kind_expr; | |
6de9cd9a DN |
2215 | |
2216 | if (gfc_match_char ('(') == MATCH_NO) | |
2217 | return MATCH_NO; | |
2218 | ||
2219 | /* Also gobbles optional text. */ | |
2220 | if (gfc_match (" kind = ") == MATCH_YES) | |
2221 | m = MATCH_ERROR; | |
2222 | ||
e2d29968 PT |
2223 | loc = gfc_current_locus; |
2224 | ||
2225 | kind_expr: | |
6de9cd9a | 2226 | n = gfc_match_init_expr (&e); |
e2d29968 | 2227 | |
6de9cd9a | 2228 | if (n != MATCH_YES) |
e2d29968 | 2229 | { |
1c8bcdf7 | 2230 | if (gfc_matching_function) |
e2d29968 | 2231 | { |
f5acf0f2 | 2232 | /* The function kind expression might include use associated or |
1c8bcdf7 PT |
2233 | imported parameters and try again after the specification |
2234 | expressions..... */ | |
e2d29968 PT |
2235 | if (gfc_match_char (')') != MATCH_YES) |
2236 | { | |
2237 | gfc_error ("Missing right parenthesis at %C"); | |
2238 | m = MATCH_ERROR; | |
2239 | goto no_match; | |
2240 | } | |
2241 | ||
2242 | gfc_free_expr (e); | |
e2d29968 PT |
2243 | gfc_undo_symbols (); |
2244 | return MATCH_YES; | |
2245 | } | |
2246 | else | |
2247 | { | |
2248 | /* ....or else, the match is real. */ | |
2249 | if (n == MATCH_NO) | |
2250 | gfc_error ("Expected initialization expression at %C"); | |
2251 | if (n != MATCH_YES) | |
2252 | return MATCH_ERROR; | |
2253 | } | |
2254 | } | |
6de9cd9a DN |
2255 | |
2256 | if (e->rank != 0) | |
2257 | { | |
2258 | gfc_error ("Expected scalar initialization expression at %C"); | |
2259 | m = MATCH_ERROR; | |
2260 | goto no_match; | |
2261 | } | |
2262 | ||
2263 | msg = gfc_extract_int (e, &ts->kind); | |
1c8bcdf7 | 2264 | |
6de9cd9a DN |
2265 | if (msg != NULL) |
2266 | { | |
2267 | gfc_error (msg); | |
2268 | m = MATCH_ERROR; | |
2269 | goto no_match; | |
2270 | } | |
2271 | ||
a8b3b0b6 CR |
2272 | /* Before throwing away the expression, let's see if we had a |
2273 | C interoperable kind (and store the fact). */ | |
2274 | if (e->ts.is_c_interop == 1) | |
2275 | { | |
eea58adb | 2276 | /* Mark this as C interoperable if being declared with one |
a8b3b0b6 CR |
2277 | of the named constants from iso_c_binding. */ |
2278 | ts->is_c_interop = e->ts.is_iso_c; | |
2279 | ts->f90_type = e->ts.f90_type; | |
2280 | } | |
f5acf0f2 | 2281 | |
6de9cd9a DN |
2282 | gfc_free_expr (e); |
2283 | e = NULL; | |
2284 | ||
a8b3b0b6 CR |
2285 | /* Ignore errors to this point, if we've gotten here. This means |
2286 | we ignore the m=MATCH_ERROR from above. */ | |
e7a2d5fb | 2287 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a DN |
2288 | { |
2289 | gfc_error ("Kind %d not supported for type %s at %C", ts->kind, | |
2290 | gfc_basic_typename (ts->type)); | |
96ee3a4a TB |
2291 | gfc_current_locus = where; |
2292 | return MATCH_ERROR; | |
6de9cd9a | 2293 | } |
96ee3a4a | 2294 | |
2ec855f1 TB |
2295 | /* Warn if, e.g., c_int is used for a REAL variable, but not |
2296 | if, e.g., c_double is used for COMPLEX as the standard | |
2297 | explicitly says that the kind type parameter for complex and real | |
2298 | variable is the same, i.e. c_float == c_float_complex. */ | |
2299 | if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type | |
2300 | && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX) | |
2301 | || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL))) | |
2be51762 TB |
2302 | gfc_warning_now ("C kind type parameter is for type %s but type at %L " |
2303 | "is %s", gfc_basic_typename (ts->f90_type), &where, | |
2304 | gfc_basic_typename (ts->type)); | |
2ec855f1 | 2305 | |
96ee3a4a | 2306 | gfc_gobble_whitespace (); |
8fc541d3 FXC |
2307 | if ((c = gfc_next_ascii_char ()) != ')' |
2308 | && (ts->type != BT_CHARACTER || c != ',')) | |
6de9cd9a | 2309 | { |
96ee3a4a TB |
2310 | if (ts->type == BT_CHARACTER) |
2311 | gfc_error ("Missing right parenthesis or comma at %C"); | |
2312 | else | |
2313 | gfc_error ("Missing right parenthesis at %C"); | |
e2d29968 | 2314 | m = MATCH_ERROR; |
6de9cd9a | 2315 | } |
a8b3b0b6 CR |
2316 | else |
2317 | /* All tests passed. */ | |
2318 | m = MATCH_YES; | |
6de9cd9a | 2319 | |
a8b3b0b6 CR |
2320 | if(m == MATCH_ERROR) |
2321 | gfc_current_locus = where; | |
f4347334 | 2322 | |
203c7ebf | 2323 | if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8) |
f4347334 ZG |
2324 | ts->kind = 8; |
2325 | ||
2326 | if (ts->type == BT_REAL || ts->type == BT_COMPLEX) | |
2327 | { | |
2328 | if (ts->kind == 4) | |
2329 | { | |
203c7ebf | 2330 | if (flag_real4_kind == 8) |
f4347334 | 2331 | ts->kind = 8; |
203c7ebf | 2332 | if (flag_real4_kind == 10) |
f4347334 | 2333 | ts->kind = 10; |
203c7ebf | 2334 | if (flag_real4_kind == 16) |
f4347334 ZG |
2335 | ts->kind = 16; |
2336 | } | |
2337 | ||
2338 | if (ts->kind == 8) | |
2339 | { | |
203c7ebf | 2340 | if (flag_real8_kind == 4) |
f4347334 | 2341 | ts->kind = 4; |
203c7ebf | 2342 | if (flag_real8_kind == 10) |
f4347334 | 2343 | ts->kind = 10; |
203c7ebf | 2344 | if (flag_real8_kind == 16) |
f4347334 ZG |
2345 | ts->kind = 16; |
2346 | } | |
2347 | } | |
2348 | ||
a8b3b0b6 CR |
2349 | /* Return what we know from the test(s). */ |
2350 | return m; | |
6de9cd9a DN |
2351 | |
2352 | no_match: | |
2353 | gfc_free_expr (e); | |
63645982 | 2354 | gfc_current_locus = where; |
6de9cd9a DN |
2355 | return m; |
2356 | } | |
2357 | ||
2358 | ||
187de1ed FXC |
2359 | static match |
2360 | match_char_kind (int * kind, int * is_iso_c) | |
2361 | { | |
2362 | locus where; | |
2363 | gfc_expr *e; | |
2364 | match m, n; | |
2365 | const char *msg; | |
2366 | ||
2367 | m = MATCH_NO; | |
2368 | e = NULL; | |
2369 | where = gfc_current_locus; | |
2370 | ||
2371 | n = gfc_match_init_expr (&e); | |
96ee3a4a | 2372 | |
1c8bcdf7 | 2373 | if (n != MATCH_YES && gfc_matching_function) |
96ee3a4a | 2374 | { |
1c8bcdf7 | 2375 | /* The expression might include use-associated or imported |
f5acf0f2 | 2376 | parameters and try again after the specification |
1c8bcdf7 | 2377 | expressions. */ |
96ee3a4a | 2378 | gfc_free_expr (e); |
96ee3a4a TB |
2379 | gfc_undo_symbols (); |
2380 | return MATCH_YES; | |
2381 | } | |
2382 | ||
187de1ed FXC |
2383 | if (n == MATCH_NO) |
2384 | gfc_error ("Expected initialization expression at %C"); | |
2385 | if (n != MATCH_YES) | |
2386 | return MATCH_ERROR; | |
2387 | ||
2388 | if (e->rank != 0) | |
2389 | { | |
2390 | gfc_error ("Expected scalar initialization expression at %C"); | |
2391 | m = MATCH_ERROR; | |
2392 | goto no_match; | |
2393 | } | |
2394 | ||
2395 | msg = gfc_extract_int (e, kind); | |
2396 | *is_iso_c = e->ts.is_iso_c; | |
2397 | if (msg != NULL) | |
2398 | { | |
2399 | gfc_error (msg); | |
2400 | m = MATCH_ERROR; | |
2401 | goto no_match; | |
2402 | } | |
2403 | ||
2404 | gfc_free_expr (e); | |
2405 | ||
2406 | /* Ignore errors to this point, if we've gotten here. This means | |
2407 | we ignore the m=MATCH_ERROR from above. */ | |
2408 | if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0) | |
2409 | { | |
2410 | gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind); | |
2411 | m = MATCH_ERROR; | |
2412 | } | |
2413 | else | |
2414 | /* All tests passed. */ | |
2415 | m = MATCH_YES; | |
2416 | ||
2417 | if (m == MATCH_ERROR) | |
2418 | gfc_current_locus = where; | |
f5acf0f2 | 2419 | |
187de1ed FXC |
2420 | /* Return what we know from the test(s). */ |
2421 | return m; | |
2422 | ||
2423 | no_match: | |
2424 | gfc_free_expr (e); | |
2425 | gfc_current_locus = where; | |
2426 | return m; | |
2427 | } | |
2428 | ||
8234e5e0 | 2429 | |
6de9cd9a DN |
2430 | /* Match the various kind/length specifications in a CHARACTER |
2431 | declaration. We don't return MATCH_NO. */ | |
2432 | ||
8234e5e0 SK |
2433 | match |
2434 | gfc_match_char_spec (gfc_typespec *ts) | |
6de9cd9a | 2435 | { |
187de1ed | 2436 | int kind, seen_length, is_iso_c; |
6de9cd9a DN |
2437 | gfc_charlen *cl; |
2438 | gfc_expr *len; | |
2439 | match m; | |
e69afb29 | 2440 | bool deferred; |
187de1ed | 2441 | |
6de9cd9a DN |
2442 | len = NULL; |
2443 | seen_length = 0; | |
187de1ed FXC |
2444 | kind = 0; |
2445 | is_iso_c = 0; | |
e69afb29 | 2446 | deferred = false; |
6de9cd9a DN |
2447 | |
2448 | /* Try the old-style specification first. */ | |
2449 | old_char_selector = 0; | |
2450 | ||
2767f2cc | 2451 | m = match_char_length (&len, &deferred, true); |
6de9cd9a DN |
2452 | if (m != MATCH_NO) |
2453 | { | |
2454 | if (m == MATCH_YES) | |
2455 | old_char_selector = 1; | |
2456 | seen_length = 1; | |
2457 | goto done; | |
2458 | } | |
2459 | ||
2460 | m = gfc_match_char ('('); | |
2461 | if (m != MATCH_YES) | |
2462 | { | |
a8b3b0b6 | 2463 | m = MATCH_YES; /* Character without length is a single char. */ |
6de9cd9a DN |
2464 | goto done; |
2465 | } | |
2466 | ||
a8b3b0b6 | 2467 | /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */ |
6de9cd9a DN |
2468 | if (gfc_match (" kind =") == MATCH_YES) |
2469 | { | |
187de1ed | 2470 | m = match_char_kind (&kind, &is_iso_c); |
f5acf0f2 | 2471 | |
6de9cd9a DN |
2472 | if (m == MATCH_ERROR) |
2473 | goto done; | |
2474 | if (m == MATCH_NO) | |
2475 | goto syntax; | |
2476 | ||
2477 | if (gfc_match (" , len =") == MATCH_NO) | |
2478 | goto rparen; | |
2479 | ||
e69afb29 | 2480 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
2481 | if (m == MATCH_NO) |
2482 | goto syntax; | |
2483 | if (m == MATCH_ERROR) | |
2484 | goto done; | |
2485 | seen_length = 1; | |
2486 | ||
2487 | goto rparen; | |
2488 | } | |
2489 | ||
66e4ab31 | 2490 | /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */ |
6de9cd9a DN |
2491 | if (gfc_match (" len =") == MATCH_YES) |
2492 | { | |
e69afb29 | 2493 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
2494 | if (m == MATCH_NO) |
2495 | goto syntax; | |
2496 | if (m == MATCH_ERROR) | |
2497 | goto done; | |
2498 | seen_length = 1; | |
2499 | ||
2500 | if (gfc_match_char (')') == MATCH_YES) | |
2501 | goto done; | |
2502 | ||
2503 | if (gfc_match (" , kind =") != MATCH_YES) | |
2504 | goto syntax; | |
2505 | ||
187de1ed FXC |
2506 | if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR) |
2507 | goto done; | |
6de9cd9a DN |
2508 | |
2509 | goto rparen; | |
2510 | } | |
2511 | ||
66e4ab31 | 2512 | /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */ |
e69afb29 | 2513 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
2514 | if (m == MATCH_NO) |
2515 | goto syntax; | |
2516 | if (m == MATCH_ERROR) | |
2517 | goto done; | |
2518 | seen_length = 1; | |
2519 | ||
2520 | m = gfc_match_char (')'); | |
2521 | if (m == MATCH_YES) | |
2522 | goto done; | |
2523 | ||
2524 | if (gfc_match_char (',') != MATCH_YES) | |
2525 | goto syntax; | |
2526 | ||
a8b3b0b6 | 2527 | gfc_match (" kind ="); /* Gobble optional text. */ |
6de9cd9a | 2528 | |
187de1ed | 2529 | m = match_char_kind (&kind, &is_iso_c); |
6de9cd9a DN |
2530 | if (m == MATCH_ERROR) |
2531 | goto done; | |
2532 | if (m == MATCH_NO) | |
2533 | goto syntax; | |
2534 | ||
2535 | rparen: | |
2536 | /* Require a right-paren at this point. */ | |
2537 | m = gfc_match_char (')'); | |
2538 | if (m == MATCH_YES) | |
2539 | goto done; | |
2540 | ||
2541 | syntax: | |
2542 | gfc_error ("Syntax error in CHARACTER declaration at %C"); | |
2543 | m = MATCH_ERROR; | |
16f8ffc8 JD |
2544 | gfc_free_expr (len); |
2545 | return m; | |
6de9cd9a DN |
2546 | |
2547 | done: | |
a99d95a2 PT |
2548 | /* Deal with character functions after USE and IMPORT statements. */ |
2549 | if (gfc_matching_function) | |
1c8bcdf7 | 2550 | { |
a99d95a2 | 2551 | gfc_free_expr (len); |
1c8bcdf7 PT |
2552 | gfc_undo_symbols (); |
2553 | return MATCH_YES; | |
2554 | } | |
2555 | ||
6de9cd9a DN |
2556 | if (m != MATCH_YES) |
2557 | { | |
2558 | gfc_free_expr (len); | |
2559 | return m; | |
2560 | } | |
2561 | ||
2562 | /* Do some final massaging of the length values. */ | |
b76e28c6 | 2563 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
6de9cd9a DN |
2564 | |
2565 | if (seen_length == 0) | |
b7e75771 | 2566 | cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
6de9cd9a | 2567 | else |
5cd09fac | 2568 | cl->length = len; |
6de9cd9a | 2569 | |
bc21d315 | 2570 | ts->u.cl = cl; |
187de1ed | 2571 | ts->kind = kind == 0 ? gfc_default_character_kind : kind; |
e69afb29 | 2572 | ts->deferred = deferred; |
6de9cd9a | 2573 | |
eea58adb | 2574 | /* We have to know if it was a C interoperable kind so we can |
a8b3b0b6 | 2575 | do accurate type checking of bind(c) procs, etc. */ |
187de1ed | 2576 | if (kind != 0) |
eea58adb | 2577 | /* Mark this as C interoperable if being declared with one |
187de1ed FXC |
2578 | of the named constants from iso_c_binding. */ |
2579 | ts->is_c_interop = is_iso_c; | |
a8b3b0b6 | 2580 | else if (len != NULL) |
187de1ed FXC |
2581 | /* Here, we might have parsed something such as: character(c_char) |
2582 | In this case, the parsing code above grabs the c_char when | |
2583 | looking for the length (line 1690, roughly). it's the last | |
2584 | testcase for parsing the kind params of a character variable. | |
2585 | However, it's not actually the length. this seems like it | |
f5acf0f2 | 2586 | could be an error. |
187de1ed FXC |
2587 | To see if the user used a C interop kind, test the expr |
2588 | of the so called length, and see if it's C interoperable. */ | |
2589 | ts->is_c_interop = len->ts.is_iso_c; | |
f5acf0f2 | 2590 | |
6de9cd9a DN |
2591 | return MATCH_YES; |
2592 | } | |
2593 | ||
2594 | ||
e74f1cc8 JW |
2595 | /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts |
2596 | structure to the matched specification. This is necessary for FUNCTION and | |
6de9cd9a DN |
2597 | IMPLICIT statements. |
2598 | ||
d51347f9 | 2599 | If implicit_flag is nonzero, then we don't check for the optional |
e5ddaa24 | 2600 | kind specification. Not doing so is needed for matching an IMPLICIT |
6de9cd9a DN |
2601 | statement correctly. */ |
2602 | ||
e2d29968 | 2603 | match |
e74f1cc8 | 2604 | gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag) |
6de9cd9a DN |
2605 | { |
2606 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
c3f34952 | 2607 | gfc_symbol *sym, *dt_sym; |
6de9cd9a | 2608 | match m; |
8fc541d3 | 2609 | char c; |
0fb56814 | 2610 | bool seen_deferred_kind, matched_type; |
c3f34952 | 2611 | const char *dt_name; |
6de9cd9a | 2612 | |
1c8bcdf7 PT |
2613 | /* A belt and braces check that the typespec is correctly being treated |
2614 | as a deferred characteristic association. */ | |
2615 | seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION) | |
a99d95a2 PT |
2616 | && (gfc_current_block ()->result->ts.kind == -1) |
2617 | && (ts->kind == -1); | |
6de9cd9a | 2618 | gfc_clear_ts (ts); |
1c8bcdf7 PT |
2619 | if (seen_deferred_kind) |
2620 | ts->kind = -1; | |
6de9cd9a | 2621 | |
a8b3b0b6 | 2622 | /* Clear the current binding label, in case one is given. */ |
62603fae | 2623 | curr_binding_label = NULL; |
a8b3b0b6 | 2624 | |
5f700e6d AL |
2625 | if (gfc_match (" byte") == MATCH_YES) |
2626 | { | |
524af0d6 | 2627 | if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C")) |
5f700e6d AL |
2628 | return MATCH_ERROR; |
2629 | ||
2630 | if (gfc_validate_kind (BT_INTEGER, 1, true) < 0) | |
2631 | { | |
2632 | gfc_error ("BYTE type used at %C " | |
2633 | "is not available on the target machine"); | |
2634 | return MATCH_ERROR; | |
2635 | } | |
d51347f9 | 2636 | |
5f700e6d AL |
2637 | ts->type = BT_INTEGER; |
2638 | ts->kind = 1; | |
2639 | return MATCH_YES; | |
2640 | } | |
2641 | ||
0fb56814 | 2642 | |
45a69325 | 2643 | m = gfc_match (" type ("); |
0fb56814 | 2644 | matched_type = (m == MATCH_YES); |
45a69325 TB |
2645 | if (matched_type) |
2646 | { | |
2647 | gfc_gobble_whitespace (); | |
2648 | if (gfc_peek_ascii_char () == '*') | |
2649 | { | |
2650 | if ((m = gfc_match ("*)")) != MATCH_YES) | |
2651 | return m; | |
2652 | if (gfc_current_state () == COMP_DERIVED) | |
2653 | { | |
2654 | gfc_error ("Assumed type at %C is not allowed for components"); | |
2655 | return MATCH_ERROR; | |
2656 | } | |
524af0d6 JB |
2657 | if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type " |
2658 | "at %C")) | |
45a69325 TB |
2659 | return MATCH_ERROR; |
2660 | ts->type = BT_ASSUMED; | |
2661 | return MATCH_YES; | |
2662 | } | |
2663 | ||
2664 | m = gfc_match ("%n", name); | |
2665 | matched_type = (m == MATCH_YES); | |
2666 | } | |
2667 | ||
0fb56814 TB |
2668 | if ((matched_type && strcmp ("integer", name) == 0) |
2669 | || (!matched_type && gfc_match (" integer") == MATCH_YES)) | |
6de9cd9a DN |
2670 | { |
2671 | ts->type = BT_INTEGER; | |
9d64df18 | 2672 | ts->kind = gfc_default_integer_kind; |
6de9cd9a DN |
2673 | goto get_kind; |
2674 | } | |
2675 | ||
0fb56814 TB |
2676 | if ((matched_type && strcmp ("character", name) == 0) |
2677 | || (!matched_type && gfc_match (" character") == MATCH_YES)) | |
6de9cd9a | 2678 | { |
0fb56814 | 2679 | if (matched_type |
524af0d6 JB |
2680 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
2681 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
2682 | return MATCH_ERROR; |
2683 | ||
6de9cd9a | 2684 | ts->type = BT_CHARACTER; |
e5ddaa24 | 2685 | if (implicit_flag == 0) |
0fb56814 | 2686 | m = gfc_match_char_spec (ts); |
e5ddaa24 | 2687 | else |
0fb56814 TB |
2688 | m = MATCH_YES; |
2689 | ||
2690 | if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES) | |
2691 | m = MATCH_ERROR; | |
2692 | ||
2693 | return m; | |
6de9cd9a DN |
2694 | } |
2695 | ||
0fb56814 TB |
2696 | if ((matched_type && strcmp ("real", name) == 0) |
2697 | || (!matched_type && gfc_match (" real") == MATCH_YES)) | |
6de9cd9a DN |
2698 | { |
2699 | ts->type = BT_REAL; | |
9d64df18 | 2700 | ts->kind = gfc_default_real_kind; |
6de9cd9a DN |
2701 | goto get_kind; |
2702 | } | |
2703 | ||
0fb56814 TB |
2704 | if ((matched_type |
2705 | && (strcmp ("doubleprecision", name) == 0 | |
2706 | || (strcmp ("double", name) == 0 | |
2707 | && gfc_match (" precision") == MATCH_YES))) | |
2708 | || (!matched_type && gfc_match (" double precision") == MATCH_YES)) | |
6de9cd9a | 2709 | { |
0fb56814 | 2710 | if (matched_type |
524af0d6 JB |
2711 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
2712 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
2713 | return MATCH_ERROR; |
2714 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
2715 | return MATCH_ERROR; | |
2716 | ||
6de9cd9a | 2717 | ts->type = BT_REAL; |
9d64df18 | 2718 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
2719 | return MATCH_YES; |
2720 | } | |
2721 | ||
0fb56814 TB |
2722 | if ((matched_type && strcmp ("complex", name) == 0) |
2723 | || (!matched_type && gfc_match (" complex") == MATCH_YES)) | |
6de9cd9a DN |
2724 | { |
2725 | ts->type = BT_COMPLEX; | |
9d64df18 | 2726 | ts->kind = gfc_default_complex_kind; |
6de9cd9a DN |
2727 | goto get_kind; |
2728 | } | |
2729 | ||
0fb56814 TB |
2730 | if ((matched_type |
2731 | && (strcmp ("doublecomplex", name) == 0 | |
2732 | || (strcmp ("double", name) == 0 | |
2733 | && gfc_match (" complex") == MATCH_YES))) | |
2734 | || (!matched_type && gfc_match (" double complex") == MATCH_YES)) | |
6de9cd9a | 2735 | { |
524af0d6 | 2736 | if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C")) |
0fb56814 TB |
2737 | return MATCH_ERROR; |
2738 | ||
2739 | if (matched_type | |
524af0d6 JB |
2740 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
2741 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
2742 | return MATCH_ERROR; |
2743 | ||
2744 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
df8652dc SK |
2745 | return MATCH_ERROR; |
2746 | ||
6de9cd9a | 2747 | ts->type = BT_COMPLEX; |
9d64df18 | 2748 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
2749 | return MATCH_YES; |
2750 | } | |
2751 | ||
0fb56814 TB |
2752 | if ((matched_type && strcmp ("logical", name) == 0) |
2753 | || (!matched_type && gfc_match (" logical") == MATCH_YES)) | |
6de9cd9a DN |
2754 | { |
2755 | ts->type = BT_LOGICAL; | |
9d64df18 | 2756 | ts->kind = gfc_default_logical_kind; |
6de9cd9a DN |
2757 | goto get_kind; |
2758 | } | |
2759 | ||
0fb56814 TB |
2760 | if (matched_type) |
2761 | m = gfc_match_char (')'); | |
2762 | ||
cf2b3c22 TB |
2763 | if (m == MATCH_YES) |
2764 | ts->type = BT_DERIVED; | |
2765 | else | |
727e8544 | 2766 | { |
528622fd JW |
2767 | /* Match CLASS declarations. */ |
2768 | m = gfc_match (" class ( * )"); | |
2769 | if (m == MATCH_ERROR) | |
2770 | return MATCH_ERROR; | |
2771 | else if (m == MATCH_YES) | |
2772 | { | |
8b704316 PT |
2773 | gfc_symbol *upe; |
2774 | gfc_symtree *st; | |
2775 | ts->type = BT_CLASS; | |
f5acf0f2 | 2776 | gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe); |
8b704316 PT |
2777 | if (upe == NULL) |
2778 | { | |
f5acf0f2 PT |
2779 | upe = gfc_new_symbol ("STAR", gfc_current_ns); |
2780 | st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR"); | |
8b704316 PT |
2781 | st->n.sym = upe; |
2782 | gfc_set_sym_referenced (upe); | |
2783 | upe->refs++; | |
2784 | upe->ts.type = BT_VOID; | |
2785 | upe->attr.unlimited_polymorphic = 1; | |
2786 | /* This is essential to force the construction of | |
2787 | unlimited polymorphic component class containers. */ | |
2788 | upe->attr.zero_comp = 1; | |
524af0d6 JB |
2789 | if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL, |
2790 | &gfc_current_locus)) | |
528622fd JW |
2791 | return MATCH_ERROR; |
2792 | } | |
8b704316 PT |
2793 | else |
2794 | { | |
f5acf0f2 | 2795 | st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR"); |
8b704316 | 2796 | if (st == NULL) |
f5acf0f2 | 2797 | st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR"); |
8b704316 PT |
2798 | st->n.sym = upe; |
2799 | upe->refs++; | |
2800 | } | |
2801 | ts->u.derived = upe; | |
2802 | return m; | |
2803 | } | |
528622fd | 2804 | |
727e8544 JW |
2805 | m = gfc_match (" class ( %n )", name); |
2806 | if (m != MATCH_YES) | |
2807 | return m; | |
cf2b3c22 | 2808 | ts->type = BT_CLASS; |
727e8544 | 2809 | |
524af0d6 | 2810 | if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C")) |
e74f1cc8 | 2811 | return MATCH_ERROR; |
727e8544 | 2812 | } |
6de9cd9a | 2813 | |
1c8bcdf7 PT |
2814 | /* Defer association of the derived type until the end of the |
2815 | specification block. However, if the derived type can be | |
f5acf0f2 | 2816 | found, add it to the typespec. */ |
1c8bcdf7 | 2817 | if (gfc_matching_function) |
e2d29968 | 2818 | { |
bc21d315 | 2819 | ts->u.derived = NULL; |
1c8bcdf7 PT |
2820 | if (gfc_current_state () != COMP_INTERFACE |
2821 | && !gfc_find_symbol (name, NULL, 1, &sym) && sym) | |
c3f34952 TB |
2822 | { |
2823 | sym = gfc_find_dt_in_generic (sym); | |
2824 | ts->u.derived = sym; | |
2825 | } | |
e2d29968 PT |
2826 | return MATCH_YES; |
2827 | } | |
2828 | ||
2829 | /* Search for the name but allow the components to be defined later. If | |
2830 | type = -1, this typespec has been seen in a function declaration but | |
c3f34952 | 2831 | the type could not be accessed at that point. The actual derived type is |
eea58adb | 2832 | stored in a symtree with the first letter of the name capitalized; the |
c3f34952 TB |
2833 | symtree with the all lower-case name contains the associated |
2834 | generic function. */ | |
2835 | dt_name = gfc_get_string ("%c%s", | |
2836 | (char) TOUPPER ((unsigned char) name[0]), | |
2837 | (const char*)&name[1]); | |
1c8bcdf7 | 2838 | sym = NULL; |
c3f34952 TB |
2839 | dt_sym = NULL; |
2840 | if (ts->kind != -1) | |
6de9cd9a | 2841 | { |
c3f34952 TB |
2842 | gfc_get_ha_symbol (name, &sym); |
2843 | if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym)) | |
2844 | { | |
c4100eae | 2845 | gfc_error ("Type name %qs at %C is ambiguous", name); |
c3f34952 TB |
2846 | return MATCH_ERROR; |
2847 | } | |
2848 | if (sym->generic && !dt_sym) | |
2849 | dt_sym = gfc_find_dt_in_generic (sym); | |
6de9cd9a | 2850 | } |
e2d29968 PT |
2851 | else if (ts->kind == -1) |
2852 | { | |
1c8bcdf7 PT |
2853 | int iface = gfc_state_stack->previous->state != COMP_INTERFACE |
2854 | || gfc_current_ns->has_import_set; | |
c3f34952 TB |
2855 | gfc_find_symbol (name, NULL, iface, &sym); |
2856 | if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym)) | |
f5acf0f2 | 2857 | { |
c4100eae | 2858 | gfc_error ("Type name %qs at %C is ambiguous", name); |
e2d29968 PT |
2859 | return MATCH_ERROR; |
2860 | } | |
c3f34952 TB |
2861 | if (sym && sym->generic && !dt_sym) |
2862 | dt_sym = gfc_find_dt_in_generic (sym); | |
e2d29968 | 2863 | |
1c8bcdf7 | 2864 | ts->kind = 0; |
e2d29968 PT |
2865 | if (sym == NULL) |
2866 | return MATCH_NO; | |
2867 | } | |
6de9cd9a | 2868 | |
c3f34952 TB |
2869 | if ((sym->attr.flavor != FL_UNKNOWN |
2870 | && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic)) | |
2871 | || sym->attr.subroutine) | |
2872 | { | |
c4100eae MLI |
2873 | gfc_error_1 ("Type name '%s' at %C conflicts with previously declared " |
2874 | "entity at %L, which has the same name", name, | |
2875 | &sym->declared_at); | |
c3f34952 TB |
2876 | return MATCH_ERROR; |
2877 | } | |
6de9cd9a | 2878 | |
1c8bcdf7 | 2879 | gfc_set_sym_referenced (sym); |
c3f34952 | 2880 | if (!sym->attr.generic |
524af0d6 | 2881 | && !gfc_add_generic (&sym->attr, sym->name, NULL)) |
c3f34952 TB |
2882 | return MATCH_ERROR; |
2883 | ||
2884 | if (!sym->attr.function | |
524af0d6 | 2885 | && !gfc_add_function (&sym->attr, sym->name, NULL)) |
c3f34952 TB |
2886 | return MATCH_ERROR; |
2887 | ||
2888 | if (!dt_sym) | |
2889 | { | |
2890 | gfc_interface *intr, *head; | |
2891 | ||
2892 | /* Use upper case to save the actual derived-type symbol. */ | |
2893 | gfc_get_symbol (dt_name, NULL, &dt_sym); | |
2894 | dt_sym->name = gfc_get_string (sym->name); | |
2895 | head = sym->generic; | |
2896 | intr = gfc_get_interface (); | |
2897 | intr->sym = dt_sym; | |
2898 | intr->where = gfc_current_locus; | |
2899 | intr->next = head; | |
2900 | sym->generic = intr; | |
2901 | sym->attr.if_source = IFSRC_DECL; | |
2902 | } | |
2903 | ||
2904 | gfc_set_sym_referenced (dt_sym); | |
2905 | ||
2906 | if (dt_sym->attr.flavor != FL_DERIVED | |
524af0d6 | 2907 | && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)) |
c3f34952 TB |
2908 | return MATCH_ERROR; |
2909 | ||
2910 | ts->u.derived = dt_sym; | |
6de9cd9a DN |
2911 | |
2912 | return MATCH_YES; | |
2913 | ||
2914 | get_kind: | |
0fb56814 | 2915 | if (matched_type |
524af0d6 JB |
2916 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
2917 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
2918 | return MATCH_ERROR; |
2919 | ||
6de9cd9a DN |
2920 | /* For all types except double, derived and character, look for an |
2921 | optional kind specifier. MATCH_NO is actually OK at this point. */ | |
e5ddaa24 | 2922 | if (implicit_flag == 1) |
0fb56814 TB |
2923 | { |
2924 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
2925 | return MATCH_ERROR; | |
2926 | ||
2927 | return MATCH_YES; | |
2928 | } | |
6de9cd9a | 2929 | |
0ff0dfbf TS |
2930 | if (gfc_current_form == FORM_FREE) |
2931 | { | |
0b3624f6 SK |
2932 | c = gfc_peek_ascii_char (); |
2933 | if (!gfc_is_whitespace (c) && c != '*' && c != '(' | |
636dff67 | 2934 | && c != ':' && c != ',') |
0fb56814 TB |
2935 | { |
2936 | if (matched_type && c == ')') | |
2937 | { | |
2938 | gfc_next_ascii_char (); | |
2939 | return MATCH_YES; | |
2940 | } | |
2941 | return MATCH_NO; | |
2942 | } | |
0ff0dfbf TS |
2943 | } |
2944 | ||
e2d29968 | 2945 | m = gfc_match_kind_spec (ts, false); |
6de9cd9a DN |
2946 | if (m == MATCH_NO && ts->type != BT_CHARACTER) |
2947 | m = gfc_match_old_kind_spec (ts); | |
2948 | ||
0fb56814 TB |
2949 | if (matched_type && gfc_match_char (')') != MATCH_YES) |
2950 | return MATCH_ERROR; | |
2951 | ||
1c8bcdf7 PT |
2952 | /* Defer association of the KIND expression of function results |
2953 | until after USE and IMPORT statements. */ | |
2954 | if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ()) | |
2955 | || gfc_matching_function) | |
2956 | return MATCH_YES; | |
2957 | ||
6de9cd9a DN |
2958 | if (m == MATCH_NO) |
2959 | m = MATCH_YES; /* No kind specifier found. */ | |
2960 | ||
2961 | return m; | |
2962 | } | |
2963 | ||
2964 | ||
e5ddaa24 TS |
2965 | /* Match an IMPLICIT NONE statement. Actually, this statement is |
2966 | already matched in parse.c, or we would not end up here in the | |
2967 | first place. So the only thing we need to check, is if there is | |
2968 | trailing garbage. If not, the match is successful. */ | |
2969 | ||
2970 | match | |
2971 | gfc_match_implicit_none (void) | |
2972 | { | |
8b7a967e TB |
2973 | char c; |
2974 | match m; | |
2975 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2976 | bool type = false; | |
2977 | bool external = false; | |
a6c63173 TB |
2978 | locus cur_loc = gfc_current_locus; |
2979 | ||
2980 | if (gfc_current_ns->seen_implicit_none | |
2981 | || gfc_current_ns->has_implicit_none_export) | |
2982 | { | |
2983 | gfc_error ("Duplicate IMPLICIT NONE statement at %C"); | |
2984 | return MATCH_ERROR; | |
2985 | } | |
8b7a967e TB |
2986 | |
2987 | gfc_gobble_whitespace (); | |
2988 | c = gfc_peek_ascii_char (); | |
2989 | if (c == '(') | |
2990 | { | |
2991 | (void) gfc_next_ascii_char (); | |
2992 | if (!gfc_notify_std (GFC_STD_F2015, "IMPORT NONE with spec list at %C")) | |
2993 | return MATCH_ERROR; | |
a6c63173 TB |
2994 | |
2995 | gfc_gobble_whitespace (); | |
2996 | if (gfc_peek_ascii_char () == ')') | |
8b7a967e | 2997 | { |
a6c63173 TB |
2998 | (void) gfc_next_ascii_char (); |
2999 | type = true; | |
3000 | } | |
3001 | else | |
3002 | for(;;) | |
3003 | { | |
3004 | m = gfc_match (" %n", name); | |
3005 | if (m != MATCH_YES) | |
3006 | return MATCH_ERROR; | |
8b7a967e | 3007 | |
a6c63173 TB |
3008 | if (strcmp (name, "type") == 0) |
3009 | type = true; | |
3010 | else if (strcmp (name, "external") == 0) | |
3011 | external = true; | |
3012 | else | |
3013 | return MATCH_ERROR; | |
8b7a967e | 3014 | |
a6c63173 TB |
3015 | gfc_gobble_whitespace (); |
3016 | c = gfc_next_ascii_char (); | |
3017 | if (c == ',') | |
3018 | continue; | |
3019 | if (c == ')') | |
3020 | break; | |
3021 | return MATCH_ERROR; | |
3022 | } | |
8b7a967e TB |
3023 | } |
3024 | else | |
3025 | type = true; | |
3026 | ||
3027 | if (gfc_match_eos () != MATCH_YES) | |
3028 | return MATCH_ERROR; | |
3029 | ||
a6c63173 | 3030 | gfc_set_implicit_none (type, external, &cur_loc); |
8b7a967e TB |
3031 | |
3032 | return MATCH_YES; | |
e5ddaa24 TS |
3033 | } |
3034 | ||
3035 | ||
3036 | /* Match the letter range(s) of an IMPLICIT statement. */ | |
3037 | ||
3038 | static match | |
1107b970 | 3039 | match_implicit_range (void) |
e5ddaa24 | 3040 | { |
8fc541d3 FXC |
3041 | char c, c1, c2; |
3042 | int inner; | |
e5ddaa24 TS |
3043 | locus cur_loc; |
3044 | ||
3045 | cur_loc = gfc_current_locus; | |
3046 | ||
3047 | gfc_gobble_whitespace (); | |
8fc541d3 | 3048 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
3049 | if (c != '(') |
3050 | { | |
3051 | gfc_error ("Missing character range in IMPLICIT at %C"); | |
3052 | goto bad; | |
3053 | } | |
3054 | ||
3055 | inner = 1; | |
3056 | while (inner) | |
3057 | { | |
3058 | gfc_gobble_whitespace (); | |
8fc541d3 | 3059 | c1 = gfc_next_ascii_char (); |
e5ddaa24 TS |
3060 | if (!ISALPHA (c1)) |
3061 | goto bad; | |
3062 | ||
3063 | gfc_gobble_whitespace (); | |
8fc541d3 | 3064 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
3065 | |
3066 | switch (c) | |
3067 | { | |
3068 | case ')': | |
66e4ab31 | 3069 | inner = 0; /* Fall through. */ |
e5ddaa24 TS |
3070 | |
3071 | case ',': | |
3072 | c2 = c1; | |
3073 | break; | |
3074 | ||
3075 | case '-': | |
3076 | gfc_gobble_whitespace (); | |
8fc541d3 | 3077 | c2 = gfc_next_ascii_char (); |
e5ddaa24 TS |
3078 | if (!ISALPHA (c2)) |
3079 | goto bad; | |
3080 | ||
3081 | gfc_gobble_whitespace (); | |
8fc541d3 | 3082 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
3083 | |
3084 | if ((c != ',') && (c != ')')) | |
3085 | goto bad; | |
3086 | if (c == ')') | |
3087 | inner = 0; | |
3088 | ||
3089 | break; | |
3090 | ||
3091 | default: | |
3092 | goto bad; | |
3093 | } | |
3094 | ||
3095 | if (c1 > c2) | |
3096 | { | |
3097 | gfc_error ("Letters must be in alphabetic order in " | |
3098 | "IMPLICIT statement at %C"); | |
3099 | goto bad; | |
3100 | } | |
3101 | ||
3102 | /* See if we can add the newly matched range to the pending | |
636dff67 SK |
3103 | implicits from this IMPLICIT statement. We do not check for |
3104 | conflicts with whatever earlier IMPLICIT statements may have | |
3105 | set. This is done when we've successfully finished matching | |
3106 | the current one. */ | |
524af0d6 | 3107 | if (!gfc_add_new_implicit_range (c1, c2)) |
e5ddaa24 TS |
3108 | goto bad; |
3109 | } | |
3110 | ||
3111 | return MATCH_YES; | |
3112 | ||
3113 | bad: | |
3114 | gfc_syntax_error (ST_IMPLICIT); | |
3115 | ||
3116 | gfc_current_locus = cur_loc; | |
3117 | return MATCH_ERROR; | |
3118 | } | |
3119 | ||
3120 | ||
3121 | /* Match an IMPLICIT statement, storing the types for | |
3122 | gfc_set_implicit() if the statement is accepted by the parser. | |
3123 | There is a strange looking, but legal syntactic construction | |
3124 | possible. It looks like: | |
3125 | ||
3126 | IMPLICIT INTEGER (a-b) (c-d) | |
3127 | ||
3128 | This is legal if "a-b" is a constant expression that happens to | |
3129 | equal one of the legal kinds for integers. The real problem | |
3130 | happens with an implicit specification that looks like: | |
3131 | ||
3132 | IMPLICIT INTEGER (a-b) | |
3133 | ||
3134 | In this case, a typespec matcher that is "greedy" (as most of the | |
3135 | matchers are) gobbles the character range as a kindspec, leaving | |
3136 | nothing left. We therefore have to go a bit more slowly in the | |
3137 | matching process by inhibiting the kindspec checking during | |
3138 | typespec matching and checking for a kind later. */ | |
3139 | ||
3140 | match | |
3141 | gfc_match_implicit (void) | |
3142 | { | |
3143 | gfc_typespec ts; | |
3144 | locus cur_loc; | |
8fc541d3 | 3145 | char c; |
e5ddaa24 TS |
3146 | match m; |
3147 | ||
8b7a967e TB |
3148 | if (gfc_current_ns->seen_implicit_none) |
3149 | { | |
3150 | gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) " | |
3151 | "statement"); | |
3152 | return MATCH_ERROR; | |
3153 | } | |
3154 | ||
44000dbb JD |
3155 | gfc_clear_ts (&ts); |
3156 | ||
e5ddaa24 TS |
3157 | /* We don't allow empty implicit statements. */ |
3158 | if (gfc_match_eos () == MATCH_YES) | |
3159 | { | |
3160 | gfc_error ("Empty IMPLICIT statement at %C"); | |
3161 | return MATCH_ERROR; | |
3162 | } | |
3163 | ||
e5ddaa24 TS |
3164 | do |
3165 | { | |
1107b970 PB |
3166 | /* First cleanup. */ |
3167 | gfc_clear_new_implicit (); | |
3168 | ||
e5ddaa24 | 3169 | /* A basic type is mandatory here. */ |
e74f1cc8 | 3170 | m = gfc_match_decl_type_spec (&ts, 1); |
e5ddaa24 TS |
3171 | if (m == MATCH_ERROR) |
3172 | goto error; | |
3173 | if (m == MATCH_NO) | |
3174 | goto syntax; | |
3175 | ||
3176 | cur_loc = gfc_current_locus; | |
1107b970 | 3177 | m = match_implicit_range (); |
e5ddaa24 TS |
3178 | |
3179 | if (m == MATCH_YES) | |
3180 | { | |
1107b970 | 3181 | /* We may have <TYPE> (<RANGE>). */ |
e5ddaa24 | 3182 | gfc_gobble_whitespace (); |
a6c63173 TB |
3183 | c = gfc_peek_ascii_char (); |
3184 | if (c == ',' || c == '\n' || c == ';' || c == '!') | |
1107b970 PB |
3185 | { |
3186 | /* Check for CHARACTER with no length parameter. */ | |
bc21d315 | 3187 | if (ts.type == BT_CHARACTER && !ts.u.cl) |
1107b970 | 3188 | { |
9d64df18 | 3189 | ts.kind = gfc_default_character_kind; |
b76e28c6 | 3190 | ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
b7e75771 JD |
3191 | ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind, |
3192 | NULL, 1); | |
1107b970 PB |
3193 | } |
3194 | ||
3195 | /* Record the Successful match. */ | |
524af0d6 | 3196 | if (!gfc_merge_new_implicit (&ts)) |
1107b970 | 3197 | return MATCH_ERROR; |
a6c63173 TB |
3198 | if (c == ',') |
3199 | c = gfc_next_ascii_char (); | |
3200 | else if (gfc_match_eos () == MATCH_ERROR) | |
3201 | goto error; | |
1107b970 PB |
3202 | continue; |
3203 | } | |
e5ddaa24 TS |
3204 | |
3205 | gfc_current_locus = cur_loc; | |
3206 | } | |
3207 | ||
1107b970 PB |
3208 | /* Discard the (incorrectly) matched range. */ |
3209 | gfc_clear_new_implicit (); | |
3210 | ||
3211 | /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */ | |
3212 | if (ts.type == BT_CHARACTER) | |
8234e5e0 | 3213 | m = gfc_match_char_spec (&ts); |
1107b970 | 3214 | else |
e5ddaa24 | 3215 | { |
e2d29968 | 3216 | m = gfc_match_kind_spec (&ts, false); |
e5ddaa24 | 3217 | if (m == MATCH_NO) |
1107b970 PB |
3218 | { |
3219 | m = gfc_match_old_kind_spec (&ts); | |
3220 | if (m == MATCH_ERROR) | |
3221 | goto error; | |
3222 | if (m == MATCH_NO) | |
3223 | goto syntax; | |
3224 | } | |
e5ddaa24 | 3225 | } |
1107b970 PB |
3226 | if (m == MATCH_ERROR) |
3227 | goto error; | |
e5ddaa24 | 3228 | |
1107b970 | 3229 | m = match_implicit_range (); |
e5ddaa24 TS |
3230 | if (m == MATCH_ERROR) |
3231 | goto error; | |
3232 | if (m == MATCH_NO) | |
3233 | goto syntax; | |
3234 | ||
3235 | gfc_gobble_whitespace (); | |
8fc541d3 | 3236 | c = gfc_next_ascii_char (); |
a6c63173 | 3237 | if (c != ',' && gfc_match_eos () != MATCH_YES) |
e5ddaa24 TS |
3238 | goto syntax; |
3239 | ||
524af0d6 | 3240 | if (!gfc_merge_new_implicit (&ts)) |
1107b970 | 3241 | return MATCH_ERROR; |
e5ddaa24 TS |
3242 | } |
3243 | while (c == ','); | |
3244 | ||
1107b970 | 3245 | return MATCH_YES; |
e5ddaa24 TS |
3246 | |
3247 | syntax: | |
3248 | gfc_syntax_error (ST_IMPLICIT); | |
3249 | ||
3250 | error: | |
3251 | return MATCH_ERROR; | |
3252 | } | |
3253 | ||
66e4ab31 | 3254 | |
8998be20 TB |
3255 | match |
3256 | gfc_match_import (void) | |
3257 | { | |
3258 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
3259 | match m; | |
3260 | gfc_symbol *sym; | |
3261 | gfc_symtree *st; | |
3262 | ||
66e4ab31 SK |
3263 | if (gfc_current_ns->proc_name == NULL |
3264 | || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY) | |
8998be20 TB |
3265 | { |
3266 | gfc_error ("IMPORT statement at %C only permitted in " | |
3267 | "an INTERFACE body"); | |
3268 | return MATCH_ERROR; | |
3269 | } | |
3270 | ||
524af0d6 | 3271 | if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C")) |
8998be20 TB |
3272 | return MATCH_ERROR; |
3273 | ||
3274 | if (gfc_match_eos () == MATCH_YES) | |
3275 | { | |
3276 | /* All host variables should be imported. */ | |
3277 | gfc_current_ns->has_import_set = 1; | |
3278 | return MATCH_YES; | |
3279 | } | |
3280 | ||
3281 | if (gfc_match (" ::") == MATCH_YES) | |
3282 | { | |
3283 | if (gfc_match_eos () == MATCH_YES) | |
636dff67 SK |
3284 | { |
3285 | gfc_error ("Expecting list of named entities at %C"); | |
3286 | return MATCH_ERROR; | |
3287 | } | |
8998be20 TB |
3288 | } |
3289 | ||
3290 | for(;;) | |
3291 | { | |
2e8d9212 | 3292 | sym = NULL; |
8998be20 TB |
3293 | m = gfc_match (" %n", name); |
3294 | switch (m) | |
3295 | { | |
3296 | case MATCH_YES: | |
36d3fb4c | 3297 | if (gfc_current_ns->parent != NULL |
66e4ab31 | 3298 | && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym)) |
36d3fb4c | 3299 | { |
c4100eae | 3300 | gfc_error ("Type name %qs at %C is ambiguous", name); |
36d3fb4c PT |
3301 | return MATCH_ERROR; |
3302 | } | |
4e2cf5f5 | 3303 | else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL |
66e4ab31 SK |
3304 | && gfc_find_symbol (name, |
3305 | gfc_current_ns->proc_name->ns->parent, | |
3306 | 1, &sym)) | |
636dff67 | 3307 | { |
c4100eae | 3308 | gfc_error ("Type name %qs at %C is ambiguous", name); |
636dff67 SK |
3309 | return MATCH_ERROR; |
3310 | } | |
3311 | ||
3312 | if (sym == NULL) | |
3313 | { | |
c4100eae | 3314 | gfc_error ("Cannot IMPORT %qs from host scoping unit " |
636dff67 SK |
3315 | "at %C - does not exist.", name); |
3316 | return MATCH_ERROR; | |
3317 | } | |
3318 | ||
dd8b9dde | 3319 | if (gfc_find_symtree (gfc_current_ns->sym_root, name)) |
636dff67 | 3320 | { |
48749dbc MLI |
3321 | gfc_warning ("%qs is already IMPORTed from host scoping unit " |
3322 | "at %C", name); | |
636dff67 SK |
3323 | goto next_item; |
3324 | } | |
3325 | ||
dd8b9dde | 3326 | st = gfc_new_symtree (&gfc_current_ns->sym_root, name); |
636dff67 SK |
3327 | st->n.sym = sym; |
3328 | sym->refs++; | |
5a8af0b4 | 3329 | sym->attr.imported = 1; |
8998be20 | 3330 | |
c3f34952 TB |
3331 | if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym))) |
3332 | { | |
3333 | /* The actual derived type is stored in a symtree with the first | |
eea58adb | 3334 | letter of the name capitalized; the symtree with the all |
1cc0e193 | 3335 | lower-case name contains the associated generic function. */ |
c3f34952 TB |
3336 | st = gfc_new_symtree (&gfc_current_ns->sym_root, |
3337 | gfc_get_string ("%c%s", | |
dd8b9dde TB |
3338 | (char) TOUPPER ((unsigned char) name[0]), |
3339 | &name[1])); | |
c3f34952 TB |
3340 | st->n.sym = sym; |
3341 | sym->refs++; | |
3342 | sym->attr.imported = 1; | |
3343 | } | |
3344 | ||
8998be20 TB |
3345 | goto next_item; |
3346 | ||
3347 | case MATCH_NO: | |
3348 | break; | |
3349 | ||
3350 | case MATCH_ERROR: | |
3351 | return MATCH_ERROR; | |
3352 | } | |
3353 | ||
3354 | next_item: | |
3355 | if (gfc_match_eos () == MATCH_YES) | |
3356 | break; | |
3357 | if (gfc_match_char (',') != MATCH_YES) | |
3358 | goto syntax; | |
3359 | } | |
3360 | ||
3361 | return MATCH_YES; | |
3362 | ||
3363 | syntax: | |
3364 | gfc_error ("Syntax error in IMPORT statement at %C"); | |
3365 | return MATCH_ERROR; | |
3366 | } | |
e5ddaa24 | 3367 | |
66e4ab31 | 3368 | |
f2449db4 RS |
3369 | /* A minimal implementation of gfc_match without whitespace, escape |
3370 | characters or variable arguments. Returns true if the next | |
3371 | characters match the TARGET template exactly. */ | |
3372 | ||
3373 | static bool | |
3374 | match_string_p (const char *target) | |
3375 | { | |
3376 | const char *p; | |
3377 | ||
3378 | for (p = target; *p; p++) | |
8fc541d3 | 3379 | if ((char) gfc_next_ascii_char () != *p) |
f2449db4 RS |
3380 | return false; |
3381 | return true; | |
3382 | } | |
3383 | ||
6de9cd9a DN |
3384 | /* Matches an attribute specification including array specs. If |
3385 | successful, leaves the variables current_attr and current_as | |
3386 | holding the specification. Also sets the colon_seen variable for | |
3387 | later use by matchers associated with initializations. | |
3388 | ||
3389 | This subroutine is a little tricky in the sense that we don't know | |
3390 | if we really have an attr-spec until we hit the double colon. | |
3391 | Until that time, we can only return MATCH_NO. This forces us to | |
3392 | check for duplicate specification at this level. */ | |
3393 | ||
3394 | static match | |
3395 | match_attr_spec (void) | |
3396 | { | |
6de9cd9a | 3397 | /* Modifiers that can exist in a type statement. */ |
d75d9546 | 3398 | enum |
6de9cd9a DN |
3399 | { GFC_DECL_BEGIN = 0, |
3400 | DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL, | |
3401 | DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL, | |
ee7e677f TB |
3402 | DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE, |
3403 | DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE, | |
fe4e525c TB |
3404 | DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS, |
3405 | DECL_NONE, GFC_DECL_END /* Sentinel */ | |
d75d9546 | 3406 | }; |
6de9cd9a DN |
3407 | |
3408 | /* GFC_DECL_END is the sentinel, index starts at 0. */ | |
3409 | #define NUM_DECL GFC_DECL_END | |
3410 | ||
6de9cd9a DN |
3411 | locus start, seen_at[NUM_DECL]; |
3412 | int seen[NUM_DECL]; | |
09639a83 | 3413 | unsigned int d; |
6de9cd9a DN |
3414 | const char *attr; |
3415 | match m; | |
524af0d6 | 3416 | bool t; |
6de9cd9a DN |
3417 | |
3418 | gfc_clear_attr (¤t_attr); | |
63645982 | 3419 | start = gfc_current_locus; |
6de9cd9a DN |
3420 | |
3421 | current_as = NULL; | |
3422 | colon_seen = 0; | |
3423 | ||
3424 | /* See if we get all of the keywords up to the final double colon. */ | |
3425 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
3426 | seen[d] = 0; | |
3427 | ||
3428 | for (;;) | |
3429 | { | |
8fc541d3 | 3430 | char ch; |
a8b3b0b6 | 3431 | |
f2449db4 RS |
3432 | d = DECL_NONE; |
3433 | gfc_gobble_whitespace (); | |
3434 | ||
8fc541d3 | 3435 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
3436 | if (ch == ':') |
3437 | { | |
3438 | /* This is the successful exit condition for the loop. */ | |
8fc541d3 | 3439 | if (gfc_next_ascii_char () == ':') |
f2449db4 RS |
3440 | break; |
3441 | } | |
3442 | else if (ch == ',') | |
a8b3b0b6 | 3443 | { |
a8b3b0b6 | 3444 | gfc_gobble_whitespace (); |
8fc541d3 | 3445 | switch (gfc_peek_ascii_char ()) |
a8b3b0b6 | 3446 | { |
f2449db4 | 3447 | case 'a': |
1eee5628 TB |
3448 | gfc_next_ascii_char (); |
3449 | switch (gfc_next_ascii_char ()) | |
3450 | { | |
3451 | case 'l': | |
3452 | if (match_string_p ("locatable")) | |
3453 | { | |
3454 | /* Matched "allocatable". */ | |
3455 | d = DECL_ALLOCATABLE; | |
3456 | } | |
3457 | break; | |
3458 | ||
3459 | case 's': | |
3460 | if (match_string_p ("ynchronous")) | |
3461 | { | |
3462 | /* Matched "asynchronous". */ | |
3463 | d = DECL_ASYNCHRONOUS; | |
3464 | } | |
3465 | break; | |
3466 | } | |
fe4e525c | 3467 | break; |
f2449db4 RS |
3468 | |
3469 | case 'b': | |
a8b3b0b6 | 3470 | /* Try and match the bind(c). */ |
1eabf70a | 3471 | m = gfc_match_bind_c (NULL, true); |
129d15a3 | 3472 | if (m == MATCH_YES) |
a8b3b0b6 | 3473 | d = DECL_IS_BIND_C; |
129d15a3 JW |
3474 | else if (m == MATCH_ERROR) |
3475 | goto cleanup; | |
f2449db4 RS |
3476 | break; |
3477 | ||
be59db2d | 3478 | case 'c': |
fe4e525c TB |
3479 | gfc_next_ascii_char (); |
3480 | if ('o' != gfc_next_ascii_char ()) | |
3481 | break; | |
3482 | switch (gfc_next_ascii_char ()) | |
3483 | { | |
3484 | case 'd': | |
3485 | if (match_string_p ("imension")) | |
3486 | { | |
3487 | d = DECL_CODIMENSION; | |
3488 | break; | |
3489 | } | |
3490 | case 'n': | |
3491 | if (match_string_p ("tiguous")) | |
3492 | { | |
3493 | d = DECL_CONTIGUOUS; | |
3494 | break; | |
3495 | } | |
3496 | } | |
be59db2d TB |
3497 | break; |
3498 | ||
f2449db4 RS |
3499 | case 'd': |
3500 | if (match_string_p ("dimension")) | |
3501 | d = DECL_DIMENSION; | |
3502 | break; | |
3503 | ||
3504 | case 'e': | |
3505 | if (match_string_p ("external")) | |
3506 | d = DECL_EXTERNAL; | |
3507 | break; | |
3508 | ||
3509 | case 'i': | |
3510 | if (match_string_p ("int")) | |
3511 | { | |
8fc541d3 | 3512 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
3513 | if (ch == 'e') |
3514 | { | |
3515 | if (match_string_p ("nt")) | |
3516 | { | |
3517 | /* Matched "intent". */ | |
3518 | /* TODO: Call match_intent_spec from here. */ | |
3519 | if (gfc_match (" ( in out )") == MATCH_YES) | |
3520 | d = DECL_INOUT; | |
3521 | else if (gfc_match (" ( in )") == MATCH_YES) | |
3522 | d = DECL_IN; | |
3523 | else if (gfc_match (" ( out )") == MATCH_YES) | |
3524 | d = DECL_OUT; | |
3525 | } | |
3526 | } | |
3527 | else if (ch == 'r') | |
3528 | { | |
3529 | if (match_string_p ("insic")) | |
3530 | { | |
3531 | /* Matched "intrinsic". */ | |
3532 | d = DECL_INTRINSIC; | |
3533 | } | |
3534 | } | |
3535 | } | |
3536 | break; | |
3537 | ||
3538 | case 'o': | |
3539 | if (match_string_p ("optional")) | |
3540 | d = DECL_OPTIONAL; | |
3541 | break; | |
3542 | ||
3543 | case 'p': | |
8fc541d3 FXC |
3544 | gfc_next_ascii_char (); |
3545 | switch (gfc_next_ascii_char ()) | |
f2449db4 RS |
3546 | { |
3547 | case 'a': | |
3548 | if (match_string_p ("rameter")) | |
3549 | { | |
3550 | /* Matched "parameter". */ | |
3551 | d = DECL_PARAMETER; | |
3552 | } | |
3553 | break; | |
3554 | ||
3555 | case 'o': | |
3556 | if (match_string_p ("inter")) | |
3557 | { | |
3558 | /* Matched "pointer". */ | |
3559 | d = DECL_POINTER; | |
3560 | } | |
3561 | break; | |
3562 | ||
3563 | case 'r': | |
8fc541d3 | 3564 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
3565 | if (ch == 'i') |
3566 | { | |
3567 | if (match_string_p ("vate")) | |
3568 | { | |
3569 | /* Matched "private". */ | |
3570 | d = DECL_PRIVATE; | |
3571 | } | |
3572 | } | |
3573 | else if (ch == 'o') | |
3574 | { | |
3575 | if (match_string_p ("tected")) | |
3576 | { | |
3577 | /* Matched "protected". */ | |
3578 | d = DECL_PROTECTED; | |
3579 | } | |
3580 | } | |
3581 | break; | |
3582 | ||
3583 | case 'u': | |
3584 | if (match_string_p ("blic")) | |
3585 | { | |
3586 | /* Matched "public". */ | |
3587 | d = DECL_PUBLIC; | |
3588 | } | |
3589 | break; | |
3590 | } | |
3591 | break; | |
3592 | ||
3593 | case 's': | |
3594 | if (match_string_p ("save")) | |
3595 | d = DECL_SAVE; | |
3596 | break; | |
3597 | ||
3598 | case 't': | |
3599 | if (match_string_p ("target")) | |
3600 | d = DECL_TARGET; | |
3601 | break; | |
3602 | ||
3603 | case 'v': | |
8fc541d3 FXC |
3604 | gfc_next_ascii_char (); |
3605 | ch = gfc_next_ascii_char (); | |
f2449db4 RS |
3606 | if (ch == 'a') |
3607 | { | |
3608 | if (match_string_p ("lue")) | |
3609 | { | |
3610 | /* Matched "value". */ | |
3611 | d = DECL_VALUE; | |
3612 | } | |
3613 | } | |
3614 | else if (ch == 'o') | |
3615 | { | |
3616 | if (match_string_p ("latile")) | |
3617 | { | |
3618 | /* Matched "volatile". */ | |
3619 | d = DECL_VOLATILE; | |
3620 | } | |
3621 | } | |
3622 | break; | |
a8b3b0b6 CR |
3623 | } |
3624 | } | |
d468bcdb | 3625 | |
f2449db4 RS |
3626 | /* No double colon and no recognizable decl_type, so assume that |
3627 | we've been looking at something else the whole time. */ | |
3628 | if (d == DECL_NONE) | |
3629 | { | |
3630 | m = MATCH_NO; | |
3631 | goto cleanup; | |
3632 | } | |
d51347f9 | 3633 | |
acb388a0 JD |
3634 | /* Check to make sure any parens are paired up correctly. */ |
3635 | if (gfc_match_parens () == MATCH_ERROR) | |
3636 | { | |
3637 | m = MATCH_ERROR; | |
3638 | goto cleanup; | |
3639 | } | |
3640 | ||
6de9cd9a | 3641 | seen[d]++; |
63645982 | 3642 | seen_at[d] = gfc_current_locus; |
6de9cd9a | 3643 | |
d3a9eea2 | 3644 | if (d == DECL_DIMENSION || d == DECL_CODIMENSION) |
6de9cd9a | 3645 | { |
d3a9eea2 | 3646 | gfc_array_spec *as = NULL; |
6de9cd9a | 3647 | |
d3a9eea2 TB |
3648 | m = gfc_match_array_spec (&as, d == DECL_DIMENSION, |
3649 | d == DECL_CODIMENSION); | |
3650 | ||
3651 | if (current_as == NULL) | |
3652 | current_as = as; | |
3653 | else if (m == MATCH_YES) | |
6de9cd9a | 3654 | { |
524af0d6 | 3655 | if (!merge_array_spec (as, current_as, false)) |
63fbf586 | 3656 | m = MATCH_ERROR; |
cede9502 | 3657 | free (as); |
6de9cd9a DN |
3658 | } |
3659 | ||
be59db2d TB |
3660 | if (m == MATCH_NO) |
3661 | { | |
d3a9eea2 TB |
3662 | if (d == DECL_CODIMENSION) |
3663 | gfc_error ("Missing codimension specification at %C"); | |
3664 | else | |
3665 | gfc_error ("Missing dimension specification at %C"); | |
be59db2d TB |
3666 | m = MATCH_ERROR; |
3667 | } | |
3668 | ||
3669 | if (m == MATCH_ERROR) | |
3670 | goto cleanup; | |
3671 | } | |
6de9cd9a DN |
3672 | } |
3673 | ||
6de9cd9a DN |
3674 | /* Since we've seen a double colon, we have to be looking at an |
3675 | attr-spec. This means that we can now issue errors. */ | |
3676 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
3677 | if (seen[d] > 1) | |
3678 | { | |
3679 | switch (d) | |
3680 | { | |
3681 | case DECL_ALLOCATABLE: | |
3682 | attr = "ALLOCATABLE"; | |
3683 | break; | |
1eee5628 TB |
3684 | case DECL_ASYNCHRONOUS: |
3685 | attr = "ASYNCHRONOUS"; | |
3686 | break; | |
be59db2d TB |
3687 | case DECL_CODIMENSION: |
3688 | attr = "CODIMENSION"; | |
3689 | break; | |
fe4e525c TB |
3690 | case DECL_CONTIGUOUS: |
3691 | attr = "CONTIGUOUS"; | |
3692 | break; | |
6de9cd9a DN |
3693 | case DECL_DIMENSION: |
3694 | attr = "DIMENSION"; | |
3695 | break; | |
3696 | case DECL_EXTERNAL: | |
3697 | attr = "EXTERNAL"; | |
3698 | break; | |
3699 | case DECL_IN: | |
3700 | attr = "INTENT (IN)"; | |
3701 | break; | |
3702 | case DECL_OUT: | |
3703 | attr = "INTENT (OUT)"; | |
3704 | break; | |
3705 | case DECL_INOUT: | |
3706 | attr = "INTENT (IN OUT)"; | |
3707 | break; | |
3708 | case DECL_INTRINSIC: | |
3709 | attr = "INTRINSIC"; | |
3710 | break; | |
3711 | case DECL_OPTIONAL: | |
3712 | attr = "OPTIONAL"; | |
3713 | break; | |
3714 | case DECL_PARAMETER: | |
3715 | attr = "PARAMETER"; | |
3716 | break; | |
3717 | case DECL_POINTER: | |
3718 | attr = "POINTER"; | |
3719 | break; | |
ee7e677f TB |
3720 | case DECL_PROTECTED: |
3721 | attr = "PROTECTED"; | |
3722 | break; | |
6de9cd9a DN |
3723 | case DECL_PRIVATE: |
3724 | attr = "PRIVATE"; | |
3725 | break; | |
3726 | case DECL_PUBLIC: | |
3727 | attr = "PUBLIC"; | |
3728 | break; | |
3729 | case DECL_SAVE: | |
3730 | attr = "SAVE"; | |
3731 | break; | |
3732 | case DECL_TARGET: | |
3733 | attr = "TARGET"; | |
3734 | break; | |
a8b3b0b6 CR |
3735 | case DECL_IS_BIND_C: |
3736 | attr = "IS_BIND_C"; | |
3737 | break; | |
3738 | case DECL_VALUE: | |
3739 | attr = "VALUE"; | |
3740 | break; | |
775e6c3a TB |
3741 | case DECL_VOLATILE: |
3742 | attr = "VOLATILE"; | |
3743 | break; | |
6de9cd9a | 3744 | default: |
66e4ab31 | 3745 | attr = NULL; /* This shouldn't happen. */ |
6de9cd9a DN |
3746 | } |
3747 | ||
3748 | gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]); | |
3749 | m = MATCH_ERROR; | |
3750 | goto cleanup; | |
3751 | } | |
3752 | ||
3753 | /* Now that we've dealt with duplicate attributes, add the attributes | |
3754 | to the current attribute. */ | |
3755 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
3756 | { | |
3757 | if (seen[d] == 0) | |
3758 | continue; | |
3759 | ||
3760 | if (gfc_current_state () == COMP_DERIVED | |
be59db2d TB |
3761 | && d != DECL_DIMENSION && d != DECL_CODIMENSION |
3762 | && d != DECL_POINTER && d != DECL_PRIVATE | |
fe4e525c | 3763 | && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE) |
6de9cd9a | 3764 | { |
5046aff5 PT |
3765 | if (d == DECL_ALLOCATABLE) |
3766 | { | |
524af0d6 JB |
3767 | if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE " |
3768 | "attribute at %C in a TYPE definition")) | |
5046aff5 PT |
3769 | { |
3770 | m = MATCH_ERROR; | |
3771 | goto cleanup; | |
3772 | } | |
636dff67 SK |
3773 | } |
3774 | else | |
5046aff5 PT |
3775 | { |
3776 | gfc_error ("Attribute at %L is not allowed in a TYPE definition", | |
d51347f9 | 3777 | &seen_at[d]); |
5046aff5 PT |
3778 | m = MATCH_ERROR; |
3779 | goto cleanup; | |
3780 | } | |
6de9cd9a DN |
3781 | } |
3782 | ||
4213f93b | 3783 | if ((d == DECL_PRIVATE || d == DECL_PUBLIC) |
636dff67 | 3784 | && gfc_current_state () != COMP_MODULE) |
4213f93b PT |
3785 | { |
3786 | if (d == DECL_PRIVATE) | |
3787 | attr = "PRIVATE"; | |
3788 | else | |
3789 | attr = "PUBLIC"; | |
d51347f9 TB |
3790 | if (gfc_current_state () == COMP_DERIVED |
3791 | && gfc_state_stack->previous | |
3792 | && gfc_state_stack->previous->state == COMP_MODULE) | |
3793 | { | |
524af0d6 JB |
3794 | if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s " |
3795 | "at %L in a TYPE definition", attr, | |
3796 | &seen_at[d])) | |
d51347f9 TB |
3797 | { |
3798 | m = MATCH_ERROR; | |
3799 | goto cleanup; | |
3800 | } | |
3801 | } | |
3802 | else | |
3803 | { | |
3804 | gfc_error ("%s attribute at %L is not allowed outside of the " | |
3805 | "specification part of a module", attr, &seen_at[d]); | |
3806 | m = MATCH_ERROR; | |
3807 | goto cleanup; | |
3808 | } | |
4213f93b PT |
3809 | } |
3810 | ||
6de9cd9a DN |
3811 | switch (d) |
3812 | { | |
3813 | case DECL_ALLOCATABLE: | |
3814 | t = gfc_add_allocatable (¤t_attr, &seen_at[d]); | |
3815 | break; | |
3816 | ||
1eee5628 | 3817 | case DECL_ASYNCHRONOUS: |
524af0d6 JB |
3818 | if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C")) |
3819 | t = false; | |
1eee5628 TB |
3820 | else |
3821 | t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]); | |
3822 | break; | |
3823 | ||
be59db2d TB |
3824 | case DECL_CODIMENSION: |
3825 | t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]); | |
3826 | break; | |
3827 | ||
fe4e525c | 3828 | case DECL_CONTIGUOUS: |
524af0d6 JB |
3829 | if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C")) |
3830 | t = false; | |
fe4e525c TB |
3831 | else |
3832 | t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]); | |
3833 | break; | |
3834 | ||
6de9cd9a | 3835 | case DECL_DIMENSION: |
231b2fcc | 3836 | t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]); |
6de9cd9a DN |
3837 | break; |
3838 | ||
3839 | case DECL_EXTERNAL: | |
3840 | t = gfc_add_external (¤t_attr, &seen_at[d]); | |
3841 | break; | |
3842 | ||
3843 | case DECL_IN: | |
3844 | t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]); | |
3845 | break; | |
3846 | ||
3847 | case DECL_OUT: | |
3848 | t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]); | |
3849 | break; | |
3850 | ||
3851 | case DECL_INOUT: | |
3852 | t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]); | |
3853 | break; | |
3854 | ||
3855 | case DECL_INTRINSIC: | |
3856 | t = gfc_add_intrinsic (¤t_attr, &seen_at[d]); | |
3857 | break; | |
3858 | ||
3859 | case DECL_OPTIONAL: | |
3860 | t = gfc_add_optional (¤t_attr, &seen_at[d]); | |
3861 | break; | |
3862 | ||
3863 | case DECL_PARAMETER: | |
231b2fcc | 3864 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]); |
6de9cd9a DN |
3865 | break; |
3866 | ||
3867 | case DECL_POINTER: | |
3868 | t = gfc_add_pointer (¤t_attr, &seen_at[d]); | |
3869 | break; | |
3870 | ||
ee7e677f TB |
3871 | case DECL_PROTECTED: |
3872 | if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE) | |
3873 | { | |
3874 | gfc_error ("PROTECTED at %C only allowed in specification " | |
3875 | "part of a module"); | |
524af0d6 | 3876 | t = false; |
ee7e677f TB |
3877 | break; |
3878 | } | |
3879 | ||
524af0d6 JB |
3880 | if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C")) |
3881 | t = false; | |
ee7e677f TB |
3882 | else |
3883 | t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]); | |
3884 | break; | |
3885 | ||
6de9cd9a | 3886 | case DECL_PRIVATE: |
231b2fcc TS |
3887 | t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL, |
3888 | &seen_at[d]); | |
6de9cd9a DN |
3889 | break; |
3890 | ||
3891 | case DECL_PUBLIC: | |
231b2fcc TS |
3892 | t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL, |
3893 | &seen_at[d]); | |
6de9cd9a DN |
3894 | break; |
3895 | ||
3896 | case DECL_SAVE: | |
80f95228 | 3897 | t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]); |
6de9cd9a DN |
3898 | break; |
3899 | ||
3900 | case DECL_TARGET: | |
3901 | t = gfc_add_target (¤t_attr, &seen_at[d]); | |
3902 | break; | |
3903 | ||
a8b3b0b6 CR |
3904 | case DECL_IS_BIND_C: |
3905 | t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0); | |
3906 | break; | |
f5acf0f2 | 3907 | |
06469efd | 3908 | case DECL_VALUE: |
524af0d6 JB |
3909 | if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C")) |
3910 | t = false; | |
06469efd PT |
3911 | else |
3912 | t = gfc_add_value (¤t_attr, NULL, &seen_at[d]); | |
3913 | break; | |
3914 | ||
775e6c3a | 3915 | case DECL_VOLATILE: |
524af0d6 JB |
3916 | if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C")) |
3917 | t = false; | |
775e6c3a TB |
3918 | else |
3919 | t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]); | |
3920 | break; | |
3921 | ||
6de9cd9a DN |
3922 | default: |
3923 | gfc_internal_error ("match_attr_spec(): Bad attribute"); | |
3924 | } | |
3925 | ||
524af0d6 | 3926 | if (!t) |
6de9cd9a DN |
3927 | { |
3928 | m = MATCH_ERROR; | |
3929 | goto cleanup; | |
3930 | } | |
3931 | } | |
3932 | ||
dab2cbf8 JW |
3933 | /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */ |
3934 | if (gfc_current_state () == COMP_MODULE && !current_attr.save | |
3935 | && (gfc_option.allow_std & GFC_STD_F2008) != 0) | |
80f95228 JW |
3936 | current_attr.save = SAVE_IMPLICIT; |
3937 | ||
6de9cd9a DN |
3938 | colon_seen = 1; |
3939 | return MATCH_YES; | |
3940 | ||
3941 | cleanup: | |
63645982 | 3942 | gfc_current_locus = start; |
6de9cd9a DN |
3943 | gfc_free_array_spec (current_as); |
3944 | current_as = NULL; | |
3945 | return m; | |
3946 | } | |
3947 | ||
3948 | ||
a8b3b0b6 CR |
3949 | /* Set the binding label, dest_label, either with the binding label |
3950 | stored in the given gfc_typespec, ts, or if none was provided, it | |
3951 | will be the symbol name in all lower case, as required by the draft | |
3952 | (J3/04-007, section 15.4.1). If a binding label was given and | |
3953 | there is more than one argument (num_idents), it is an error. */ | |
3954 | ||
524af0d6 | 3955 | static bool |
f5acf0f2 | 3956 | set_binding_label (const char **dest_label, const char *sym_name, |
9975a30b | 3957 | int num_idents) |
a8b3b0b6 | 3958 | { |
ad4a2f64 | 3959 | if (num_idents > 1 && has_name_equals) |
a8b3b0b6 | 3960 | { |
ad4a2f64 TB |
3961 | gfc_error ("Multiple identifiers provided with " |
3962 | "single NAME= specifier at %C"); | |
524af0d6 | 3963 | return false; |
ad4a2f64 | 3964 | } |
a8b3b0b6 | 3965 | |
62603fae | 3966 | if (curr_binding_label) |
eea58adb | 3967 | /* Binding label given; store in temp holder till have sym. */ |
62603fae | 3968 | *dest_label = curr_binding_label; |
a8b3b0b6 CR |
3969 | else |
3970 | { | |
3971 | /* No binding label given, and the NAME= specifier did not exist, | |
3972 | which means there was no NAME="". */ | |
3973 | if (sym_name != NULL && has_name_equals == 0) | |
62603fae | 3974 | *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name)); |
a8b3b0b6 | 3975 | } |
f5acf0f2 | 3976 | |
524af0d6 | 3977 | return true; |
a8b3b0b6 CR |
3978 | } |
3979 | ||
3980 | ||
3981 | /* Set the status of the given common block as being BIND(C) or not, | |
3982 | depending on the given parameter, is_bind_c. */ | |
3983 | ||
3984 | void | |
3985 | set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c) | |
3986 | { | |
3987 | com_block->is_bind_c = is_bind_c; | |
3988 | return; | |
3989 | } | |
3990 | ||
3991 | ||
3992 | /* Verify that the given gfc_typespec is for a C interoperable type. */ | |
3993 | ||
524af0d6 | 3994 | bool |
00820a2a | 3995 | gfc_verify_c_interop (gfc_typespec *ts) |
a8b3b0b6 | 3996 | { |
bc21d315 | 3997 | if (ts->type == BT_DERIVED && ts->u.derived != NULL) |
ba3721c1 | 3998 | return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c) |
524af0d6 | 3999 | ? true : false; |
00820a2a | 4000 | else if (ts->type == BT_CLASS) |
524af0d6 | 4001 | return false; |
45a69325 | 4002 | else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED) |
524af0d6 | 4003 | return false; |
45a69325 | 4004 | |
524af0d6 | 4005 | return true; |
a8b3b0b6 CR |
4006 | } |
4007 | ||
4008 | ||
4009 | /* Verify that the variables of a given common block, which has been | |
4010 | defined with the attribute specifier bind(c), to be of a C | |
4011 | interoperable type. Errors will be reported here, if | |
4012 | encountered. */ | |
4013 | ||
524af0d6 | 4014 | bool |
a8b3b0b6 CR |
4015 | verify_com_block_vars_c_interop (gfc_common_head *com_block) |
4016 | { | |
4017 | gfc_symbol *curr_sym = NULL; | |
524af0d6 | 4018 | bool retval = true; |
a8b3b0b6 CR |
4019 | |
4020 | curr_sym = com_block->head; | |
f5acf0f2 | 4021 | |
a8b3b0b6 CR |
4022 | /* Make sure we have at least one symbol. */ |
4023 | if (curr_sym == NULL) | |
4024 | return retval; | |
4025 | ||
4026 | /* Here we know we have a symbol, so we'll execute this loop | |
4027 | at least once. */ | |
4028 | do | |
4029 | { | |
4030 | /* The second to last param, 1, says this is in a common block. */ | |
4031 | retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block); | |
4032 | curr_sym = curr_sym->common_next; | |
f5acf0f2 | 4033 | } while (curr_sym != NULL); |
a8b3b0b6 CR |
4034 | |
4035 | return retval; | |
4036 | } | |
4037 | ||
4038 | ||
4039 | /* Verify that a given BIND(C) symbol is C interoperable. If it is not, | |
4040 | an appropriate error message is reported. */ | |
4041 | ||
524af0d6 | 4042 | bool |
a8b3b0b6 CR |
4043 | verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts, |
4044 | int is_in_common, gfc_common_head *com_block) | |
4045 | { | |
8327f9c2 | 4046 | bool bind_c_function = false; |
524af0d6 | 4047 | bool retval = true; |
d8fa96e0 | 4048 | |
8327f9c2 TB |
4049 | if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c) |
4050 | bind_c_function = true; | |
4051 | ||
d8fa96e0 CR |
4052 | if (tmp_sym->attr.function && tmp_sym->result != NULL) |
4053 | { | |
4054 | tmp_sym = tmp_sym->result; | |
4055 | /* Make sure it wasn't an implicitly typed result. */ | |
4daa149b | 4056 | if (tmp_sym->attr.implicit_type && warn_c_binding_type) |
d8fa96e0 | 4057 | { |
48749dbc MLI |
4058 | gfc_warning (OPT_Wc_binding_type, |
4059 | "Implicitly declared BIND(C) function %qs at " | |
d8fa96e0 CR |
4060 | "%L may not be C interoperable", tmp_sym->name, |
4061 | &tmp_sym->declared_at); | |
4062 | tmp_sym->ts.f90_type = tmp_sym->ts.type; | |
4063 | /* Mark it as C interoperable to prevent duplicate warnings. */ | |
4064 | tmp_sym->ts.is_c_interop = 1; | |
4065 | tmp_sym->attr.is_c_interop = 1; | |
4066 | } | |
4067 | } | |
8327f9c2 | 4068 | |
a8b3b0b6 CR |
4069 | /* Here, we know we have the bind(c) attribute, so if we have |
4070 | enough type info, then verify that it's a C interop kind. | |
4071 | The info could be in the symbol already, or possibly still in | |
4072 | the given ts (current_ts), so look in both. */ | |
f5acf0f2 | 4073 | if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN) |
a8b3b0b6 | 4074 | { |
524af0d6 | 4075 | if (!gfc_verify_c_interop (&(tmp_sym->ts))) |
a8b3b0b6 CR |
4076 | { |
4077 | /* See if we're dealing with a sym in a common block or not. */ | |
4daa149b | 4078 | if (is_in_common == 1 && warn_c_binding_type) |
a8b3b0b6 | 4079 | { |
48749dbc MLI |
4080 | gfc_warning (OPT_Wc_binding_type, |
4081 | "Variable %qs in common block %qs at %L " | |
a8b3b0b6 | 4082 | "may not be a C interoperable " |
48749dbc | 4083 | "kind though common block %qs is BIND(C)", |
a8b3b0b6 CR |
4084 | tmp_sym->name, com_block->name, |
4085 | &(tmp_sym->declared_at), com_block->name); | |
4086 | } | |
4087 | else | |
4088 | { | |
4089 | if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED) | |
c4100eae | 4090 | gfc_error ("Type declaration %qs at %L is not C " |
a8b3b0b6 CR |
4091 | "interoperable but it is BIND(C)", |
4092 | tmp_sym->name, &(tmp_sym->declared_at)); | |
4daa149b | 4093 | else if (warn_c_binding_type) |
48749dbc | 4094 | gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L " |
a8b3b0b6 | 4095 | "may not be a C interoperable " |
c4100eae | 4096 | "kind but it is BIND(C)", |
a8b3b0b6 CR |
4097 | tmp_sym->name, &(tmp_sym->declared_at)); |
4098 | } | |
4099 | } | |
f5acf0f2 | 4100 | |
a8b3b0b6 CR |
4101 | /* Variables declared w/in a common block can't be bind(c) |
4102 | since there's no way for C to see these variables, so there's | |
4103 | semantically no reason for the attribute. */ | |
4104 | if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1) | |
4105 | { | |
c4100eae | 4106 | gfc_error ("Variable %qs in common block %qs at " |
a8b3b0b6 CR |
4107 | "%L cannot be declared with BIND(C) " |
4108 | "since it is not a global", | |
4109 | tmp_sym->name, com_block->name, | |
4110 | &(tmp_sym->declared_at)); | |
524af0d6 | 4111 | retval = false; |
a8b3b0b6 | 4112 | } |
f5acf0f2 | 4113 | |
a8b3b0b6 CR |
4114 | /* Scalar variables that are bind(c) can not have the pointer |
4115 | or allocatable attributes. */ | |
4116 | if (tmp_sym->attr.is_bind_c == 1) | |
4117 | { | |
4118 | if (tmp_sym->attr.pointer == 1) | |
4119 | { | |
c4100eae | 4120 | gfc_error ("Variable %qs at %L cannot have both the " |
a8b3b0b6 CR |
4121 | "POINTER and BIND(C) attributes", |
4122 | tmp_sym->name, &(tmp_sym->declared_at)); | |
524af0d6 | 4123 | retval = false; |
a8b3b0b6 CR |
4124 | } |
4125 | ||
4126 | if (tmp_sym->attr.allocatable == 1) | |
4127 | { | |
c4100eae | 4128 | gfc_error ("Variable %qs at %L cannot have both the " |
a8b3b0b6 CR |
4129 | "ALLOCATABLE and BIND(C) attributes", |
4130 | tmp_sym->name, &(tmp_sym->declared_at)); | |
524af0d6 | 4131 | retval = false; |
a8b3b0b6 CR |
4132 | } |
4133 | ||
8327f9c2 TB |
4134 | } |
4135 | ||
4136 | /* If it is a BIND(C) function, make sure the return value is a | |
4137 | scalar value. The previous tests in this function made sure | |
4138 | the type is interoperable. */ | |
4139 | if (bind_c_function && tmp_sym->as != NULL) | |
c4100eae | 4140 | gfc_error ("Return type of BIND(C) function %qs at %L cannot " |
8327f9c2 TB |
4141 | "be an array", tmp_sym->name, &(tmp_sym->declared_at)); |
4142 | ||
4143 | /* BIND(C) functions can not return a character string. */ | |
4144 | if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER) | |
bc21d315 JW |
4145 | if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL |
4146 | || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT | |
4147 | || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0) | |
c4100eae | 4148 | gfc_error ("Return type of BIND(C) function %qs at %L cannot " |
a8b3b0b6 CR |
4149 | "be a character string", tmp_sym->name, |
4150 | &(tmp_sym->declared_at)); | |
a8b3b0b6 CR |
4151 | } |
4152 | ||
4153 | /* See if the symbol has been marked as private. If it has, make sure | |
4154 | there is no binding label and warn the user if there is one. */ | |
4155 | if (tmp_sym->attr.access == ACCESS_PRIVATE | |
62603fae | 4156 | && tmp_sym->binding_label) |
a8b3b0b6 CR |
4157 | /* Use gfc_warning_now because we won't say that the symbol fails |
4158 | just because of this. */ | |
4daa149b TB |
4159 | gfc_warning_now ("Symbol %qs at %L is marked PRIVATE but has been " |
4160 | "given the binding label %qs", tmp_sym->name, | |
a8b3b0b6 CR |
4161 | &(tmp_sym->declared_at), tmp_sym->binding_label); |
4162 | ||
4163 | return retval; | |
4164 | } | |
4165 | ||
4166 | ||
4167 | /* Set the appropriate fields for a symbol that's been declared as | |
4168 | BIND(C) (the is_bind_c flag and the binding label), and verify that | |
4169 | the type is C interoperable. Errors are reported by the functions | |
4170 | used to set/test these fields. */ | |
4171 | ||
524af0d6 | 4172 | bool |
a8b3b0b6 CR |
4173 | set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents) |
4174 | { | |
524af0d6 | 4175 | bool retval = true; |
f5acf0f2 | 4176 | |
a8b3b0b6 CR |
4177 | /* TODO: Do we need to make sure the vars aren't marked private? */ |
4178 | ||
4179 | /* Set the is_bind_c bit in symbol_attribute. */ | |
4180 | gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0); | |
4181 | ||
524af0d6 JB |
4182 | if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents)) |
4183 | return false; | |
a8b3b0b6 CR |
4184 | |
4185 | return retval; | |
4186 | } | |
4187 | ||
4188 | ||
4189 | /* Set the fields marking the given common block as BIND(C), including | |
4190 | a binding label, and report any errors encountered. */ | |
4191 | ||
524af0d6 | 4192 | bool |
a8b3b0b6 CR |
4193 | set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents) |
4194 | { | |
524af0d6 | 4195 | bool retval = true; |
f5acf0f2 | 4196 | |
a8b3b0b6 | 4197 | /* destLabel, common name, typespec (which may have binding label). */ |
524af0d6 JB |
4198 | if (!set_binding_label (&com_block->binding_label, com_block->name, |
4199 | num_idents)) | |
4200 | return false; | |
a8b3b0b6 CR |
4201 | |
4202 | /* Set the given common block (com_block) to being bind(c) (1). */ | |
4203 | set_com_block_bind_c (com_block, 1); | |
4204 | ||
4205 | return retval; | |
4206 | } | |
4207 | ||
4208 | ||
4209 | /* Retrieve the list of one or more identifiers that the given bind(c) | |
4210 | attribute applies to. */ | |
4211 | ||
524af0d6 | 4212 | bool |
a8b3b0b6 CR |
4213 | get_bind_c_idents (void) |
4214 | { | |
4215 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
4216 | int num_idents = 0; | |
4217 | gfc_symbol *tmp_sym = NULL; | |
4218 | match found_id; | |
4219 | gfc_common_head *com_block = NULL; | |
f5acf0f2 | 4220 | |
a8b3b0b6 CR |
4221 | if (gfc_match_name (name) == MATCH_YES) |
4222 | { | |
4223 | found_id = MATCH_YES; | |
4224 | gfc_get_ha_symbol (name, &tmp_sym); | |
4225 | } | |
4226 | else if (match_common_name (name) == MATCH_YES) | |
4227 | { | |
4228 | found_id = MATCH_YES; | |
4229 | com_block = gfc_get_common (name, 0); | |
4230 | } | |
4231 | else | |
4232 | { | |
4233 | gfc_error ("Need either entity or common block name for " | |
4234 | "attribute specification statement at %C"); | |
524af0d6 | 4235 | return false; |
a8b3b0b6 | 4236 | } |
f5acf0f2 | 4237 | |
a8b3b0b6 CR |
4238 | /* Save the current identifier and look for more. */ |
4239 | do | |
4240 | { | |
4241 | /* Increment the number of identifiers found for this spec stmt. */ | |
4242 | num_idents++; | |
4243 | ||
4244 | /* Make sure we have a sym or com block, and verify that it can | |
4245 | be bind(c). Set the appropriate field(s) and look for more | |
4246 | identifiers. */ | |
f5acf0f2 | 4247 | if (tmp_sym != NULL || com_block != NULL) |
a8b3b0b6 CR |
4248 | { |
4249 | if (tmp_sym != NULL) | |
4250 | { | |
524af0d6 JB |
4251 | if (!set_verify_bind_c_sym (tmp_sym, num_idents)) |
4252 | return false; | |
a8b3b0b6 CR |
4253 | } |
4254 | else | |
4255 | { | |
524af0d6 JB |
4256 | if (!set_verify_bind_c_com_block (com_block, num_idents)) |
4257 | return false; | |
a8b3b0b6 | 4258 | } |
f5acf0f2 | 4259 | |
a8b3b0b6 CR |
4260 | /* Look to see if we have another identifier. */ |
4261 | tmp_sym = NULL; | |
4262 | if (gfc_match_eos () == MATCH_YES) | |
4263 | found_id = MATCH_NO; | |
4264 | else if (gfc_match_char (',') != MATCH_YES) | |
4265 | found_id = MATCH_NO; | |
4266 | else if (gfc_match_name (name) == MATCH_YES) | |
4267 | { | |
4268 | found_id = MATCH_YES; | |
4269 | gfc_get_ha_symbol (name, &tmp_sym); | |
4270 | } | |
4271 | else if (match_common_name (name) == MATCH_YES) | |
4272 | { | |
4273 | found_id = MATCH_YES; | |
4274 | com_block = gfc_get_common (name, 0); | |
4275 | } | |
4276 | else | |
4277 | { | |
4278 | gfc_error ("Missing entity or common block name for " | |
4279 | "attribute specification statement at %C"); | |
524af0d6 | 4280 | return false; |
a8b3b0b6 CR |
4281 | } |
4282 | } | |
4283 | else | |
4284 | { | |
4285 | gfc_internal_error ("Missing symbol"); | |
4286 | } | |
4287 | } while (found_id == MATCH_YES); | |
4288 | ||
4289 | /* if we get here we were successful */ | |
524af0d6 | 4290 | return true; |
a8b3b0b6 CR |
4291 | } |
4292 | ||
4293 | ||
4294 | /* Try and match a BIND(C) attribute specification statement. */ | |
f5acf0f2 | 4295 | |
a8b3b0b6 CR |
4296 | match |
4297 | gfc_match_bind_c_stmt (void) | |
4298 | { | |
4299 | match found_match = MATCH_NO; | |
4300 | gfc_typespec *ts; | |
4301 | ||
4302 | ts = ¤t_ts; | |
f5acf0f2 | 4303 | |
a8b3b0b6 CR |
4304 | /* This may not be necessary. */ |
4305 | gfc_clear_ts (ts); | |
4306 | /* Clear the temporary binding label holder. */ | |
62603fae | 4307 | curr_binding_label = NULL; |
a8b3b0b6 CR |
4308 | |
4309 | /* Look for the bind(c). */ | |
1eabf70a | 4310 | found_match = gfc_match_bind_c (NULL, true); |
a8b3b0b6 CR |
4311 | |
4312 | if (found_match == MATCH_YES) | |
4313 | { | |
878cdb7b TB |
4314 | if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C")) |
4315 | return MATCH_ERROR; | |
4316 | ||
a8b3b0b6 CR |
4317 | /* Look for the :: now, but it is not required. */ |
4318 | gfc_match (" :: "); | |
4319 | ||
4320 | /* Get the identifier(s) that needs to be updated. This may need to | |
4321 | change to hand the flag(s) for the attr specified so all identifiers | |
4322 | found can have all appropriate parts updated (assuming that the same | |
4323 | spec stmt can have multiple attrs, such as both bind(c) and | |
4324 | allocatable...). */ | |
524af0d6 | 4325 | if (!get_bind_c_idents ()) |
a8b3b0b6 CR |
4326 | /* Error message should have printed already. */ |
4327 | return MATCH_ERROR; | |
4328 | } | |
4329 | ||
4330 | return found_match; | |
4331 | } | |
4332 | ||
4333 | ||
6de9cd9a DN |
4334 | /* Match a data declaration statement. */ |
4335 | ||
4336 | match | |
4337 | gfc_match_data_decl (void) | |
4338 | { | |
4339 | gfc_symbol *sym; | |
4340 | match m; | |
949d5b72 | 4341 | int elem; |
6de9cd9a | 4342 | |
a8b3b0b6 | 4343 | num_idents_on_line = 0; |
f5acf0f2 | 4344 | |
e74f1cc8 | 4345 | m = gfc_match_decl_type_spec (¤t_ts, 0); |
6de9cd9a DN |
4346 | if (m != MATCH_YES) |
4347 | return m; | |
4348 | ||
2e23972e JW |
4349 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
4350 | && gfc_current_state () != COMP_DERIVED) | |
6de9cd9a | 4351 | { |
bc21d315 | 4352 | sym = gfc_use_derived (current_ts.u.derived); |
6de9cd9a DN |
4353 | |
4354 | if (sym == NULL) | |
4355 | { | |
4356 | m = MATCH_ERROR; | |
4357 | goto cleanup; | |
4358 | } | |
4359 | ||
bc21d315 | 4360 | current_ts.u.derived = sym; |
6de9cd9a DN |
4361 | } |
4362 | ||
4363 | m = match_attr_spec (); | |
4364 | if (m == MATCH_ERROR) | |
4365 | { | |
4366 | m = MATCH_NO; | |
4367 | goto cleanup; | |
4368 | } | |
4369 | ||
8b704316 PT |
4370 | if (current_ts.type == BT_CLASS |
4371 | && current_ts.u.derived->attr.unlimited_polymorphic) | |
4372 | goto ok; | |
4373 | ||
2e23972e JW |
4374 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
4375 | && current_ts.u.derived->components == NULL | |
bc21d315 | 4376 | && !current_ts.u.derived->attr.zero_comp) |
6de9cd9a DN |
4377 | { |
4378 | ||
4379 | if (current_attr.pointer && gfc_current_state () == COMP_DERIVED) | |
4380 | goto ok; | |
4381 | ||
bc21d315 | 4382 | gfc_find_symbol (current_ts.u.derived->name, |
dd8b9dde | 4383 | current_ts.u.derived->ns, 1, &sym); |
6de9cd9a | 4384 | |
976e21f6 | 4385 | /* Any symbol that we find had better be a type definition |
636dff67 | 4386 | which has its components defined. */ |
976e21f6 | 4387 | if (sym != NULL && sym->attr.flavor == FL_DERIVED |
bc21d315 JW |
4388 | && (current_ts.u.derived->components != NULL |
4389 | || current_ts.u.derived->attr.zero_comp)) | |
6de9cd9a DN |
4390 | goto ok; |
4391 | ||
a1b80ec7 JW |
4392 | gfc_error ("Derived type at %C has not been previously defined " |
4393 | "and so cannot appear in a derived type definition"); | |
4394 | m = MATCH_ERROR; | |
4395 | goto cleanup; | |
6de9cd9a DN |
4396 | } |
4397 | ||
4398 | ok: | |
4399 | /* If we have an old-style character declaration, and no new-style | |
4400 | attribute specifications, then there a comma is optional between | |
4401 | the type specification and the variable list. */ | |
4402 | if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector) | |
4403 | gfc_match_char (','); | |
4404 | ||
949d5b72 PT |
4405 | /* Give the types/attributes to symbols that follow. Give the element |
4406 | a number so that repeat character length expressions can be copied. */ | |
4407 | elem = 1; | |
6de9cd9a DN |
4408 | for (;;) |
4409 | { | |
a8b3b0b6 | 4410 | num_idents_on_line++; |
949d5b72 | 4411 | m = variable_decl (elem++); |
6de9cd9a DN |
4412 | if (m == MATCH_ERROR) |
4413 | goto cleanup; | |
4414 | if (m == MATCH_NO) | |
4415 | break; | |
4416 | ||
4417 | if (gfc_match_eos () == MATCH_YES) | |
4418 | goto cleanup; | |
4419 | if (gfc_match_char (',') != MATCH_YES) | |
4420 | break; | |
4421 | } | |
4422 | ||
0f447a6e | 4423 | if (!gfc_error_flag_test ()) |
8f81c3c6 | 4424 | gfc_error ("Syntax error in data declaration at %C"); |
6de9cd9a DN |
4425 | m = MATCH_ERROR; |
4426 | ||
a9f6f1f2 JD |
4427 | gfc_free_data_all (gfc_current_ns); |
4428 | ||
6de9cd9a DN |
4429 | cleanup: |
4430 | gfc_free_array_spec (current_as); | |
4431 | current_as = NULL; | |
4432 | return m; | |
4433 | } | |
4434 | ||
4435 | ||
4436 | /* Match a prefix associated with a function or subroutine | |
4437 | declaration. If the typespec pointer is nonnull, then a typespec | |
4438 | can be matched. Note that if nothing matches, MATCH_YES is | |
4439 | returned (the null string was matched). */ | |
4440 | ||
1c8bcdf7 PT |
4441 | match |
4442 | gfc_match_prefix (gfc_typespec *ts) | |
6de9cd9a | 4443 | { |
7389bce6 | 4444 | bool seen_type; |
e6c14898 DK |
4445 | bool seen_impure; |
4446 | bool found_prefix; | |
6de9cd9a DN |
4447 | |
4448 | gfc_clear_attr (¤t_attr); | |
e6c14898 DK |
4449 | seen_type = false; |
4450 | seen_impure = false; | |
6de9cd9a | 4451 | |
3df684e2 DK |
4452 | gcc_assert (!gfc_matching_prefix); |
4453 | gfc_matching_prefix = true; | |
f37e928c | 4454 | |
e6c14898 | 4455 | do |
6de9cd9a | 4456 | { |
e6c14898 | 4457 | found_prefix = false; |
6de9cd9a | 4458 | |
e6c14898 DK |
4459 | if (!seen_type && ts != NULL |
4460 | && gfc_match_decl_type_spec (ts, 0) == MATCH_YES | |
4461 | && gfc_match_space () == MATCH_YES) | |
4462 | { | |
6de9cd9a | 4463 | |
e6c14898 DK |
4464 | seen_type = true; |
4465 | found_prefix = true; | |
4466 | } | |
4467 | ||
4468 | if (gfc_match ("elemental% ") == MATCH_YES) | |
4469 | { | |
524af0d6 | 4470 | if (!gfc_add_elemental (¤t_attr, NULL)) |
e6c14898 DK |
4471 | goto error; |
4472 | ||
4473 | found_prefix = true; | |
4474 | } | |
4475 | ||
4476 | if (gfc_match ("pure% ") == MATCH_YES) | |
4477 | { | |
524af0d6 | 4478 | if (!gfc_add_pure (¤t_attr, NULL)) |
e6c14898 DK |
4479 | goto error; |
4480 | ||
4481 | found_prefix = true; | |
4482 | } | |
6de9cd9a | 4483 | |
e6c14898 DK |
4484 | if (gfc_match ("recursive% ") == MATCH_YES) |
4485 | { | |
524af0d6 | 4486 | if (!gfc_add_recursive (¤t_attr, NULL)) |
e6c14898 DK |
4487 | goto error; |
4488 | ||
4489 | found_prefix = true; | |
4490 | } | |
4491 | ||
4492 | /* IMPURE is a somewhat special case, as it needs not set an actual | |
4493 | attribute but rather only prevents ELEMENTAL routines from being | |
4494 | automatically PURE. */ | |
4495 | if (gfc_match ("impure% ") == MATCH_YES) | |
4496 | { | |
524af0d6 | 4497 | if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C")) |
e6c14898 DK |
4498 | goto error; |
4499 | ||
4500 | seen_impure = true; | |
4501 | found_prefix = true; | |
4502 | } | |
6de9cd9a | 4503 | } |
e6c14898 | 4504 | while (found_prefix); |
6de9cd9a | 4505 | |
e6c14898 DK |
4506 | /* IMPURE and PURE must not both appear, of course. */ |
4507 | if (seen_impure && current_attr.pure) | |
6de9cd9a | 4508 | { |
e6c14898 DK |
4509 | gfc_error ("PURE and IMPURE must not appear both at %C"); |
4510 | goto error; | |
6de9cd9a DN |
4511 | } |
4512 | ||
e6c14898 DK |
4513 | /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */ |
4514 | if (!seen_impure && current_attr.elemental && !current_attr.pure) | |
6de9cd9a | 4515 | { |
524af0d6 | 4516 | if (!gfc_add_pure (¤t_attr, NULL)) |
f37e928c | 4517 | goto error; |
6de9cd9a DN |
4518 | } |
4519 | ||
4520 | /* At this point, the next item is not a prefix. */ | |
3df684e2 DK |
4521 | gcc_assert (gfc_matching_prefix); |
4522 | gfc_matching_prefix = false; | |
6de9cd9a | 4523 | return MATCH_YES; |
f37e928c DK |
4524 | |
4525 | error: | |
3df684e2 DK |
4526 | gcc_assert (gfc_matching_prefix); |
4527 | gfc_matching_prefix = false; | |
f37e928c | 4528 | return MATCH_ERROR; |
6de9cd9a DN |
4529 | } |
4530 | ||
4531 | ||
1c8bcdf7 | 4532 | /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */ |
6de9cd9a | 4533 | |
524af0d6 | 4534 | static bool |
636dff67 | 4535 | copy_prefix (symbol_attribute *dest, locus *where) |
6de9cd9a | 4536 | { |
524af0d6 JB |
4537 | if (current_attr.pure && !gfc_add_pure (dest, where)) |
4538 | return false; | |
6de9cd9a | 4539 | |
524af0d6 JB |
4540 | if (current_attr.elemental && !gfc_add_elemental (dest, where)) |
4541 | return false; | |
6de9cd9a | 4542 | |
524af0d6 JB |
4543 | if (current_attr.recursive && !gfc_add_recursive (dest, where)) |
4544 | return false; | |
6de9cd9a | 4545 | |
524af0d6 | 4546 | return true; |
6de9cd9a DN |
4547 | } |
4548 | ||
4549 | ||
4550 | /* Match a formal argument list. */ | |
4551 | ||
4552 | match | |
636dff67 | 4553 | gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag) |
6de9cd9a DN |
4554 | { |
4555 | gfc_formal_arglist *head, *tail, *p, *q; | |
4556 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
4557 | gfc_symbol *sym; | |
4558 | match m; | |
4559 | ||
4560 | head = tail = NULL; | |
4561 | ||
4562 | if (gfc_match_char ('(') != MATCH_YES) | |
4563 | { | |
4564 | if (null_flag) | |
4565 | goto ok; | |
4566 | return MATCH_NO; | |
4567 | } | |
4568 | ||
4569 | if (gfc_match_char (')') == MATCH_YES) | |
4570 | goto ok; | |
4571 | ||
4572 | for (;;) | |
4573 | { | |
4574 | if (gfc_match_char ('*') == MATCH_YES) | |
9362a03b JW |
4575 | { |
4576 | sym = NULL; | |
524af0d6 JB |
4577 | if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument " |
4578 | "at %C")) | |
9362a03b JW |
4579 | { |
4580 | m = MATCH_ERROR; | |
4581 | goto cleanup; | |
4582 | } | |
4583 | } | |
6de9cd9a DN |
4584 | else |
4585 | { | |
4586 | m = gfc_match_name (name); | |
4587 | if (m != MATCH_YES) | |
4588 | goto cleanup; | |
4589 | ||
4590 | if (gfc_get_symbol (name, NULL, &sym)) | |
4591 | goto cleanup; | |
4592 | } | |
4593 | ||
4594 | p = gfc_get_formal_arglist (); | |
4595 | ||
4596 | if (head == NULL) | |
4597 | head = tail = p; | |
4598 | else | |
4599 | { | |
4600 | tail->next = p; | |
4601 | tail = p; | |
4602 | } | |
4603 | ||
4604 | tail->sym = sym; | |
4605 | ||
4606 | /* We don't add the VARIABLE flavor because the name could be a | |
636dff67 SK |
4607 | dummy procedure. We don't apply these attributes to formal |
4608 | arguments of statement functions. */ | |
6de9cd9a | 4609 | if (sym != NULL && !st_flag |
524af0d6 JB |
4610 | && (!gfc_add_dummy(&sym->attr, sym->name, NULL) |
4611 | || !gfc_missing_attr (&sym->attr, NULL))) | |
6de9cd9a DN |
4612 | { |
4613 | m = MATCH_ERROR; | |
4614 | goto cleanup; | |
4615 | } | |
4616 | ||
4617 | /* The name of a program unit can be in a different namespace, | |
636dff67 SK |
4618 | so check for it explicitly. After the statement is accepted, |
4619 | the name is checked for especially in gfc_get_symbol(). */ | |
6de9cd9a DN |
4620 | if (gfc_new_block != NULL && sym != NULL |
4621 | && strcmp (sym->name, gfc_new_block->name) == 0) | |
4622 | { | |
c4100eae | 4623 | gfc_error ("Name %qs at %C is the name of the procedure", |
6de9cd9a DN |
4624 | sym->name); |
4625 | m = MATCH_ERROR; | |
4626 | goto cleanup; | |
4627 | } | |
4628 | ||
4629 | if (gfc_match_char (')') == MATCH_YES) | |
4630 | goto ok; | |
4631 | ||
4632 | m = gfc_match_char (','); | |
4633 | if (m != MATCH_YES) | |
4634 | { | |
4635 | gfc_error ("Unexpected junk in formal argument list at %C"); | |
4636 | goto cleanup; | |
4637 | } | |
4638 | } | |
4639 | ||
4640 | ok: | |
4641 | /* Check for duplicate symbols in the formal argument list. */ | |
4642 | if (head != NULL) | |
4643 | { | |
4644 | for (p = head; p->next; p = p->next) | |
4645 | { | |
4646 | if (p->sym == NULL) | |
4647 | continue; | |
4648 | ||
4649 | for (q = p->next; q; q = q->next) | |
4650 | if (p->sym == q->sym) | |
4651 | { | |
c4100eae | 4652 | gfc_error ("Duplicate symbol %qs in formal argument list " |
636dff67 | 4653 | "at %C", p->sym->name); |
6de9cd9a DN |
4654 | |
4655 | m = MATCH_ERROR; | |
4656 | goto cleanup; | |
4657 | } | |
4658 | } | |
4659 | } | |
4660 | ||
524af0d6 | 4661 | if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)) |
6de9cd9a DN |
4662 | { |
4663 | m = MATCH_ERROR; | |
4664 | goto cleanup; | |
4665 | } | |
4666 | ||
4667 | return MATCH_YES; | |
4668 | ||
4669 | cleanup: | |
4670 | gfc_free_formal_arglist (head); | |
4671 | return m; | |
4672 | } | |
4673 | ||
4674 | ||
4675 | /* Match a RESULT specification following a function declaration or | |
4676 | ENTRY statement. Also matches the end-of-statement. */ | |
4677 | ||
4678 | static match | |
66e4ab31 | 4679 | match_result (gfc_symbol *function, gfc_symbol **result) |
6de9cd9a DN |
4680 | { |
4681 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
4682 | gfc_symbol *r; | |
4683 | match m; | |
4684 | ||
4685 | if (gfc_match (" result (") != MATCH_YES) | |
4686 | return MATCH_NO; | |
4687 | ||
4688 | m = gfc_match_name (name); | |
4689 | if (m != MATCH_YES) | |
4690 | return m; | |
4691 | ||
a8b3b0b6 CR |
4692 | /* Get the right paren, and that's it because there could be the |
4693 | bind(c) attribute after the result clause. */ | |
524af0d6 | 4694 | if (gfc_match_char (')') != MATCH_YES) |
6de9cd9a | 4695 | { |
a8b3b0b6 | 4696 | /* TODO: should report the missing right paren here. */ |
6de9cd9a DN |
4697 | return MATCH_ERROR; |
4698 | } | |
4699 | ||
4700 | if (strcmp (function->name, name) == 0) | |
4701 | { | |
636dff67 | 4702 | gfc_error ("RESULT variable at %C must be different than function name"); |
6de9cd9a DN |
4703 | return MATCH_ERROR; |
4704 | } | |
4705 | ||
4706 | if (gfc_get_symbol (name, NULL, &r)) | |
4707 | return MATCH_ERROR; | |
4708 | ||
524af0d6 | 4709 | if (!gfc_add_result (&r->attr, r->name, NULL)) |
6de9cd9a DN |
4710 | return MATCH_ERROR; |
4711 | ||
4712 | *result = r; | |
4713 | ||
4714 | return MATCH_YES; | |
4715 | } | |
4716 | ||
4717 | ||
a8b3b0b6 CR |
4718 | /* Match a function suffix, which could be a combination of a result |
4719 | clause and BIND(C), either one, or neither. The draft does not | |
4720 | require them to come in a specific order. */ | |
4721 | ||
4722 | match | |
4723 | gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result) | |
4724 | { | |
4725 | match is_bind_c; /* Found bind(c). */ | |
4726 | match is_result; /* Found result clause. */ | |
4727 | match found_match; /* Status of whether we've found a good match. */ | |
8fc541d3 | 4728 | char peek_char; /* Character we're going to peek at. */ |
1eabf70a | 4729 | bool allow_binding_name; |
a8b3b0b6 CR |
4730 | |
4731 | /* Initialize to having found nothing. */ | |
4732 | found_match = MATCH_NO; | |
f5acf0f2 | 4733 | is_bind_c = MATCH_NO; |
a8b3b0b6 CR |
4734 | is_result = MATCH_NO; |
4735 | ||
4736 | /* Get the next char to narrow between result and bind(c). */ | |
4737 | gfc_gobble_whitespace (); | |
8fc541d3 | 4738 | peek_char = gfc_peek_ascii_char (); |
a8b3b0b6 | 4739 | |
1eabf70a TB |
4740 | /* C binding names are not allowed for internal procedures. */ |
4741 | if (gfc_current_state () == COMP_CONTAINS | |
4742 | && sym->ns->proc_name->attr.flavor != FL_MODULE) | |
4743 | allow_binding_name = false; | |
4744 | else | |
4745 | allow_binding_name = true; | |
4746 | ||
a8b3b0b6 CR |
4747 | switch (peek_char) |
4748 | { | |
4749 | case 'r': | |
4750 | /* Look for result clause. */ | |
4751 | is_result = match_result (sym, result); | |
4752 | if (is_result == MATCH_YES) | |
4753 | { | |
4754 | /* Now see if there is a bind(c) after it. */ | |
1eabf70a | 4755 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
4756 | /* We've found the result clause and possibly bind(c). */ |
4757 | found_match = MATCH_YES; | |
4758 | } | |
4759 | else | |
4760 | /* This should only be MATCH_ERROR. */ | |
f5acf0f2 | 4761 | found_match = is_result; |
a8b3b0b6 CR |
4762 | break; |
4763 | case 'b': | |
4764 | /* Look for bind(c) first. */ | |
1eabf70a | 4765 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
4766 | if (is_bind_c == MATCH_YES) |
4767 | { | |
4768 | /* Now see if a result clause followed it. */ | |
4769 | is_result = match_result (sym, result); | |
4770 | found_match = MATCH_YES; | |
4771 | } | |
4772 | else | |
4773 | { | |
4774 | /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */ | |
4775 | found_match = MATCH_ERROR; | |
4776 | } | |
4777 | break; | |
4778 | default: | |
4779 | gfc_error ("Unexpected junk after function declaration at %C"); | |
4780 | found_match = MATCH_ERROR; | |
4781 | break; | |
4782 | } | |
4783 | ||
a8b3b0b6 | 4784 | if (is_bind_c == MATCH_YES) |
01f4fff1 | 4785 | { |
1eabf70a | 4786 | /* Fortran 2008 draft allows BIND(C) for internal procedures. */ |
01f4fff1 | 4787 | if (gfc_current_state () == COMP_CONTAINS |
1eabf70a | 4788 | && sym->ns->proc_name->attr.flavor != FL_MODULE |
524af0d6 JB |
4789 | && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute " |
4790 | "at %L may not be specified for an internal " | |
4791 | "procedure", &gfc_current_locus)) | |
1eabf70a TB |
4792 | return MATCH_ERROR; |
4793 | ||
524af0d6 | 4794 | if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)) |
01f4fff1 TB |
4795 | return MATCH_ERROR; |
4796 | } | |
f5acf0f2 | 4797 | |
a8b3b0b6 CR |
4798 | return found_match; |
4799 | } | |
4800 | ||
4801 | ||
3070bab4 JW |
4802 | /* Procedure pointer return value without RESULT statement: |
4803 | Add "hidden" result variable named "ppr@". */ | |
4804 | ||
524af0d6 | 4805 | static bool |
3070bab4 JW |
4806 | add_hidden_procptr_result (gfc_symbol *sym) |
4807 | { | |
4808 | bool case1,case2; | |
4809 | ||
4810 | if (gfc_notification_std (GFC_STD_F2003) == ERROR) | |
524af0d6 | 4811 | return false; |
3070bab4 JW |
4812 | |
4813 | /* First usage case: PROCEDURE and EXTERNAL statements. */ | |
4814 | case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block () | |
4815 | && strcmp (gfc_current_block ()->name, sym->name) == 0 | |
4816 | && sym->attr.external; | |
4817 | /* Second usage case: INTERFACE statements. */ | |
4818 | case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous | |
4819 | && gfc_state_stack->previous->state == COMP_FUNCTION | |
4820 | && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0; | |
4821 | ||
4822 | if (case1 || case2) | |
4823 | { | |
4824 | gfc_symtree *stree; | |
4825 | if (case1) | |
08a6b8e0 | 4826 | gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false); |
3070bab4 | 4827 | else if (case2) |
c73b6478 JW |
4828 | { |
4829 | gfc_symtree *st2; | |
08a6b8e0 | 4830 | gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false); |
c73b6478 JW |
4831 | st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@"); |
4832 | st2->n.sym = stree->n.sym; | |
4833 | } | |
3070bab4 JW |
4834 | sym->result = stree->n.sym; |
4835 | ||
4836 | sym->result->attr.proc_pointer = sym->attr.proc_pointer; | |
4837 | sym->result->attr.pointer = sym->attr.pointer; | |
4838 | sym->result->attr.external = sym->attr.external; | |
4839 | sym->result->attr.referenced = sym->attr.referenced; | |
fc9c6e5d | 4840 | sym->result->ts = sym->ts; |
3070bab4 JW |
4841 | sym->attr.proc_pointer = 0; |
4842 | sym->attr.pointer = 0; | |
4843 | sym->attr.external = 0; | |
4844 | if (sym->result->attr.external && sym->result->attr.pointer) | |
4845 | { | |
4846 | sym->result->attr.pointer = 0; | |
4847 | sym->result->attr.proc_pointer = 1; | |
4848 | } | |
4849 | ||
4850 | return gfc_add_result (&sym->result->attr, sym->result->name, NULL); | |
4851 | } | |
4852 | /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */ | |
4853 | else if (sym->attr.function && !sym->attr.external && sym->attr.pointer | |
4854 | && sym->result && sym->result != sym && sym->result->attr.external | |
4855 | && sym == gfc_current_ns->proc_name | |
4856 | && sym == sym->result->ns->proc_name | |
4857 | && strcmp ("ppr@", sym->result->name) == 0) | |
4858 | { | |
4859 | sym->result->attr.proc_pointer = 1; | |
4860 | sym->attr.pointer = 0; | |
524af0d6 | 4861 | return true; |
3070bab4 JW |
4862 | } |
4863 | else | |
524af0d6 | 4864 | return false; |
3070bab4 JW |
4865 | } |
4866 | ||
4867 | ||
713485cc JW |
4868 | /* Match the interface for a PROCEDURE declaration, |
4869 | including brackets (R1212). */ | |
69773742 JW |
4870 | |
4871 | static match | |
713485cc | 4872 | match_procedure_interface (gfc_symbol **proc_if) |
69773742 JW |
4873 | { |
4874 | match m; | |
3276e0b3 | 4875 | gfc_symtree *st; |
69773742 | 4876 | locus old_loc, entry_loc; |
3276e0b3 PT |
4877 | gfc_namespace *old_ns = gfc_current_ns; |
4878 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
69773742 | 4879 | |
3276e0b3 | 4880 | old_loc = entry_loc = gfc_current_locus; |
69773742 JW |
4881 | gfc_clear_ts (¤t_ts); |
4882 | ||
4883 | if (gfc_match (" (") != MATCH_YES) | |
4884 | { | |
4885 | gfc_current_locus = entry_loc; | |
4886 | return MATCH_NO; | |
4887 | } | |
4888 | ||
4889 | /* Get the type spec. for the procedure interface. */ | |
4890 | old_loc = gfc_current_locus; | |
e74f1cc8 | 4891 | m = gfc_match_decl_type_spec (¤t_ts, 0); |
f4256439 | 4892 | gfc_gobble_whitespace (); |
8fc541d3 | 4893 | if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')')) |
69773742 JW |
4894 | goto got_ts; |
4895 | ||
4896 | if (m == MATCH_ERROR) | |
4897 | return m; | |
4898 | ||
3276e0b3 | 4899 | /* Procedure interface is itself a procedure. */ |
69773742 | 4900 | gfc_current_locus = old_loc; |
3276e0b3 | 4901 | m = gfc_match_name (name); |
69773742 | 4902 | |
3276e0b3 PT |
4903 | /* First look to see if it is already accessible in the current |
4904 | namespace because it is use associated or contained. */ | |
4905 | st = NULL; | |
4906 | if (gfc_find_sym_tree (name, NULL, 0, &st)) | |
4907 | return MATCH_ERROR; | |
4908 | ||
4909 | /* If it is still not found, then try the parent namespace, if it | |
4910 | exists and create the symbol there if it is still not found. */ | |
4911 | if (gfc_current_ns->parent) | |
4912 | gfc_current_ns = gfc_current_ns->parent; | |
4913 | if (st == NULL && gfc_get_ha_sym_tree (name, &st)) | |
4914 | return MATCH_ERROR; | |
4915 | ||
4916 | gfc_current_ns = old_ns; | |
4917 | *proc_if = st->n.sym; | |
69773742 | 4918 | |
713485cc | 4919 | if (*proc_if) |
69773742 | 4920 | { |
713485cc | 4921 | (*proc_if)->refs++; |
bb343a6c TB |
4922 | /* Resolve interface if possible. That way, attr.procedure is only set |
4923 | if it is declared by a later procedure-declaration-stmt, which is | |
0e8d854e | 4924 | invalid per F08:C1216 (cf. resolve_procedure_interface). */ |
713485cc JW |
4925 | while ((*proc_if)->ts.interface) |
4926 | *proc_if = (*proc_if)->ts.interface; | |
bb343a6c | 4927 | |
0e8d854e JW |
4928 | if ((*proc_if)->attr.flavor == FL_UNKNOWN |
4929 | && (*proc_if)->ts.type == BT_UNKNOWN | |
524af0d6 JB |
4930 | && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE, |
4931 | (*proc_if)->name, NULL)) | |
0e8d854e | 4932 | return MATCH_ERROR; |
69773742 JW |
4933 | } |
4934 | ||
4935 | got_ts: | |
69773742 JW |
4936 | if (gfc_match (" )") != MATCH_YES) |
4937 | { | |
4938 | gfc_current_locus = entry_loc; | |
4939 | return MATCH_NO; | |
4940 | } | |
4941 | ||
713485cc JW |
4942 | return MATCH_YES; |
4943 | } | |
4944 | ||
4945 | ||
4946 | /* Match a PROCEDURE declaration (R1211). */ | |
4947 | ||
4948 | static match | |
4949 | match_procedure_decl (void) | |
4950 | { | |
4951 | match m; | |
4952 | gfc_symbol *sym, *proc_if = NULL; | |
4953 | int num; | |
4954 | gfc_expr *initializer = NULL; | |
4955 | ||
1cc0e193 | 4956 | /* Parse interface (with brackets). */ |
713485cc JW |
4957 | m = match_procedure_interface (&proc_if); |
4958 | if (m != MATCH_YES) | |
4959 | return m; | |
4960 | ||
4961 | /* Parse attributes (with colons). */ | |
69773742 JW |
4962 | m = match_attr_spec(); |
4963 | if (m == MATCH_ERROR) | |
4964 | return MATCH_ERROR; | |
4965 | ||
0859be17 TB |
4966 | if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c) |
4967 | { | |
4968 | current_attr.is_bind_c = 1; | |
4969 | has_name_equals = 0; | |
4970 | curr_binding_label = NULL; | |
4971 | } | |
4972 | ||
69773742 JW |
4973 | /* Get procedure symbols. */ |
4974 | for(num=1;;num++) | |
4975 | { | |
69773742 JW |
4976 | m = gfc_match_symbol (&sym, 0); |
4977 | if (m == MATCH_NO) | |
4978 | goto syntax; | |
4979 | else if (m == MATCH_ERROR) | |
4980 | return m; | |
4981 | ||
4982 | /* Add current_attr to the symbol attributes. */ | |
524af0d6 | 4983 | if (!gfc_copy_attr (&sym->attr, ¤t_attr, NULL)) |
69773742 JW |
4984 | return MATCH_ERROR; |
4985 | ||
4986 | if (sym->attr.is_bind_c) | |
4987 | { | |
4988 | /* Check for C1218. */ | |
4989 | if (!proc_if || !proc_if->attr.is_bind_c) | |
4990 | { | |
4991 | gfc_error ("BIND(C) attribute at %C requires " | |
4992 | "an interface with BIND(C)"); | |
4993 | return MATCH_ERROR; | |
4994 | } | |
4995 | /* Check for C1217. */ | |
4996 | if (has_name_equals && sym->attr.pointer) | |
4997 | { | |
4998 | gfc_error ("BIND(C) procedure with NAME may not have " | |
4999 | "POINTER attribute at %C"); | |
5000 | return MATCH_ERROR; | |
5001 | } | |
5002 | if (has_name_equals && sym->attr.dummy) | |
5003 | { | |
5004 | gfc_error ("Dummy procedure at %C may not have " | |
5005 | "BIND(C) attribute with NAME"); | |
5006 | return MATCH_ERROR; | |
5007 | } | |
5008 | /* Set binding label for BIND(C). */ | |
524af0d6 | 5009 | if (!set_binding_label (&sym->binding_label, sym->name, num)) |
69773742 JW |
5010 | return MATCH_ERROR; |
5011 | } | |
5012 | ||
524af0d6 | 5013 | if (!gfc_add_external (&sym->attr, NULL)) |
69773742 | 5014 | return MATCH_ERROR; |
3070bab4 | 5015 | |
524af0d6 | 5016 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
5017 | sym = sym->result; |
5018 | ||
524af0d6 | 5019 | if (!gfc_add_proc (&sym->attr, sym->name, NULL)) |
69773742 JW |
5020 | return MATCH_ERROR; |
5021 | ||
5022 | /* Set interface. */ | |
5023 | if (proc_if != NULL) | |
6cc309c9 | 5024 | { |
1d146030 JW |
5025 | if (sym->ts.type != BT_UNKNOWN) |
5026 | { | |
c4100eae | 5027 | gfc_error ("Procedure %qs at %L already has basic type of %s", |
1d146030 JW |
5028 | sym->name, &gfc_current_locus, |
5029 | gfc_basic_typename (sym->ts.type)); | |
5030 | return MATCH_ERROR; | |
5031 | } | |
32d99e68 | 5032 | sym->ts.interface = proc_if; |
6cc309c9 | 5033 | sym->attr.untyped = 1; |
c73b6478 | 5034 | sym->attr.if_source = IFSRC_IFBODY; |
6cc309c9 | 5035 | } |
69773742 JW |
5036 | else if (current_ts.type != BT_UNKNOWN) |
5037 | { | |
524af0d6 | 5038 | if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus)) |
1d146030 | 5039 | return MATCH_ERROR; |
32d99e68 JW |
5040 | sym->ts.interface = gfc_new_symbol ("", gfc_current_ns); |
5041 | sym->ts.interface->ts = current_ts; | |
d91909c0 | 5042 | sym->ts.interface->attr.flavor = FL_PROCEDURE; |
32d99e68 | 5043 | sym->ts.interface->attr.function = 1; |
d91909c0 | 5044 | sym->attr.function = 1; |
c73b6478 | 5045 | sym->attr.if_source = IFSRC_UNKNOWN; |
69773742 JW |
5046 | } |
5047 | ||
8fb74da4 JW |
5048 | if (gfc_match (" =>") == MATCH_YES) |
5049 | { | |
5050 | if (!current_attr.pointer) | |
5051 | { | |
5052 | gfc_error ("Initialization at %C isn't for a pointer variable"); | |
5053 | m = MATCH_ERROR; | |
5054 | goto cleanup; | |
5055 | } | |
5056 | ||
80f95228 | 5057 | m = match_pointer_init (&initializer, 1); |
8fb74da4 JW |
5058 | if (m != MATCH_YES) |
5059 | goto cleanup; | |
5060 | ||
524af0d6 | 5061 | if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)) |
8fb74da4 JW |
5062 | goto cleanup; |
5063 | ||
5064 | } | |
5065 | ||
69773742 JW |
5066 | if (gfc_match_eos () == MATCH_YES) |
5067 | return MATCH_YES; | |
5068 | if (gfc_match_char (',') != MATCH_YES) | |
5069 | goto syntax; | |
5070 | } | |
5071 | ||
5072 | syntax: | |
5073 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
5074 | return MATCH_ERROR; | |
8fb74da4 JW |
5075 | |
5076 | cleanup: | |
5077 | /* Free stuff up and return. */ | |
5078 | gfc_free_expr (initializer); | |
5079 | return m; | |
69773742 JW |
5080 | } |
5081 | ||
5082 | ||
713485cc JW |
5083 | static match |
5084 | match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc); | |
5085 | ||
5086 | ||
5087 | /* Match a procedure pointer component declaration (R445). */ | |
5088 | ||
5089 | static match | |
5090 | match_ppc_decl (void) | |
5091 | { | |
5092 | match m; | |
5093 | gfc_symbol *proc_if = NULL; | |
5094 | gfc_typespec ts; | |
5095 | int num; | |
5096 | gfc_component *c; | |
5097 | gfc_expr *initializer = NULL; | |
5098 | gfc_typebound_proc* tb; | |
5099 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
5100 | ||
5101 | /* Parse interface (with brackets). */ | |
5102 | m = match_procedure_interface (&proc_if); | |
5103 | if (m != MATCH_YES) | |
5104 | goto syntax; | |
5105 | ||
5106 | /* Parse attributes. */ | |
5107 | tb = XCNEW (gfc_typebound_proc); | |
5108 | tb->where = gfc_current_locus; | |
5109 | m = match_binding_attributes (tb, false, true); | |
5110 | if (m == MATCH_ERROR) | |
5111 | return m; | |
5112 | ||
713485cc JW |
5113 | gfc_clear_attr (¤t_attr); |
5114 | current_attr.procedure = 1; | |
5115 | current_attr.proc_pointer = 1; | |
5116 | current_attr.access = tb->access; | |
5117 | current_attr.flavor = FL_PROCEDURE; | |
5118 | ||
5119 | /* Match the colons (required). */ | |
5120 | if (gfc_match (" ::") != MATCH_YES) | |
5121 | { | |
a4d9b221 | 5122 | gfc_error ("Expected %<::%> after binding-attributes at %C"); |
713485cc JW |
5123 | return MATCH_ERROR; |
5124 | } | |
5125 | ||
5126 | /* Check for C450. */ | |
5127 | if (!tb->nopass && proc_if == NULL) | |
5128 | { | |
5129 | gfc_error("NOPASS or explicit interface required at %C"); | |
5130 | return MATCH_ERROR; | |
5131 | } | |
5132 | ||
524af0d6 | 5133 | if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C")) |
3212c187 SK |
5134 | return MATCH_ERROR; |
5135 | ||
713485cc JW |
5136 | /* Match PPC names. */ |
5137 | ts = current_ts; | |
5138 | for(num=1;;num++) | |
5139 | { | |
5140 | m = gfc_match_name (name); | |
5141 | if (m == MATCH_NO) | |
5142 | goto syntax; | |
5143 | else if (m == MATCH_ERROR) | |
5144 | return m; | |
5145 | ||
524af0d6 | 5146 | if (!gfc_add_component (gfc_current_block(), name, &c)) |
713485cc JW |
5147 | return MATCH_ERROR; |
5148 | ||
5149 | /* Add current_attr to the symbol attributes. */ | |
524af0d6 | 5150 | if (!gfc_copy_attr (&c->attr, ¤t_attr, NULL)) |
713485cc JW |
5151 | return MATCH_ERROR; |
5152 | ||
524af0d6 | 5153 | if (!gfc_add_external (&c->attr, NULL)) |
713485cc JW |
5154 | return MATCH_ERROR; |
5155 | ||
524af0d6 | 5156 | if (!gfc_add_proc (&c->attr, name, NULL)) |
713485cc JW |
5157 | return MATCH_ERROR; |
5158 | ||
2be03814 TB |
5159 | if (num == 1) |
5160 | c->tb = tb; | |
5161 | else | |
5162 | { | |
5163 | c->tb = XCNEW (gfc_typebound_proc); | |
5164 | c->tb->where = gfc_current_locus; | |
5165 | *c->tb = *tb; | |
5166 | } | |
90661f26 | 5167 | |
713485cc JW |
5168 | /* Set interface. */ |
5169 | if (proc_if != NULL) | |
5170 | { | |
5171 | c->ts.interface = proc_if; | |
5172 | c->attr.untyped = 1; | |
5173 | c->attr.if_source = IFSRC_IFBODY; | |
5174 | } | |
5175 | else if (ts.type != BT_UNKNOWN) | |
5176 | { | |
5177 | c->ts = ts; | |
5178 | c->ts.interface = gfc_new_symbol ("", gfc_current_ns); | |
d7fee03d | 5179 | c->ts.interface->result = c->ts.interface; |
713485cc | 5180 | c->ts.interface->ts = ts; |
d91909c0 | 5181 | c->ts.interface->attr.flavor = FL_PROCEDURE; |
713485cc | 5182 | c->ts.interface->attr.function = 1; |
d91909c0 | 5183 | c->attr.function = 1; |
713485cc JW |
5184 | c->attr.if_source = IFSRC_UNKNOWN; |
5185 | } | |
5186 | ||
5187 | if (gfc_match (" =>") == MATCH_YES) | |
5188 | { | |
80f95228 | 5189 | m = match_pointer_init (&initializer, 1); |
713485cc JW |
5190 | if (m != MATCH_YES) |
5191 | { | |
5192 | gfc_free_expr (initializer); | |
5193 | return m; | |
5194 | } | |
5195 | c->initializer = initializer; | |
5196 | } | |
5197 | ||
5198 | if (gfc_match_eos () == MATCH_YES) | |
5199 | return MATCH_YES; | |
5200 | if (gfc_match_char (',') != MATCH_YES) | |
5201 | goto syntax; | |
5202 | } | |
5203 | ||
5204 | syntax: | |
5205 | gfc_error ("Syntax error in procedure pointer component at %C"); | |
5206 | return MATCH_ERROR; | |
5207 | } | |
5208 | ||
5209 | ||
69773742 JW |
5210 | /* Match a PROCEDURE declaration inside an interface (R1206). */ |
5211 | ||
5212 | static match | |
5213 | match_procedure_in_interface (void) | |
5214 | { | |
5215 | match m; | |
5216 | gfc_symbol *sym; | |
5217 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
a6fcd41a | 5218 | locus old_locus; |
69773742 JW |
5219 | |
5220 | if (current_interface.type == INTERFACE_NAMELESS | |
5221 | || current_interface.type == INTERFACE_ABSTRACT) | |
5222 | { | |
5223 | gfc_error ("PROCEDURE at %C must be in a generic interface"); | |
5224 | return MATCH_ERROR; | |
5225 | } | |
5226 | ||
a6fcd41a TB |
5227 | /* Check if the F2008 optional double colon appears. */ |
5228 | gfc_gobble_whitespace (); | |
5229 | old_locus = gfc_current_locus; | |
5230 | if (gfc_match ("::") == MATCH_YES) | |
5231 | { | |
524af0d6 JB |
5232 | if (!gfc_notify_std (GFC_STD_F2008, "double colon in " |
5233 | "MODULE PROCEDURE statement at %L", &old_locus)) | |
a6fcd41a TB |
5234 | return MATCH_ERROR; |
5235 | } | |
5236 | else | |
5237 | gfc_current_locus = old_locus; | |
5238 | ||
69773742 JW |
5239 | for(;;) |
5240 | { | |
5241 | m = gfc_match_name (name); | |
5242 | if (m == MATCH_NO) | |
5243 | goto syntax; | |
5244 | else if (m == MATCH_ERROR) | |
5245 | return m; | |
5246 | if (gfc_get_symbol (name, gfc_current_ns->parent, &sym)) | |
5247 | return MATCH_ERROR; | |
5248 | ||
524af0d6 | 5249 | if (!gfc_add_interface (sym)) |
69773742 JW |
5250 | return MATCH_ERROR; |
5251 | ||
69773742 JW |
5252 | if (gfc_match_eos () == MATCH_YES) |
5253 | break; | |
5254 | if (gfc_match_char (',') != MATCH_YES) | |
5255 | goto syntax; | |
5256 | } | |
5257 | ||
5258 | return MATCH_YES; | |
5259 | ||
5260 | syntax: | |
5261 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
5262 | return MATCH_ERROR; | |
5263 | } | |
5264 | ||
5265 | ||
5266 | /* General matcher for PROCEDURE declarations. */ | |
5267 | ||
30b608eb DK |
5268 | static match match_procedure_in_type (void); |
5269 | ||
69773742 JW |
5270 | match |
5271 | gfc_match_procedure (void) | |
5272 | { | |
5273 | match m; | |
5274 | ||
5275 | switch (gfc_current_state ()) | |
5276 | { | |
5277 | case COMP_NONE: | |
5278 | case COMP_PROGRAM: | |
5279 | case COMP_MODULE: | |
5280 | case COMP_SUBROUTINE: | |
5281 | case COMP_FUNCTION: | |
3547d57e | 5282 | case COMP_BLOCK: |
69773742 JW |
5283 | m = match_procedure_decl (); |
5284 | break; | |
5285 | case COMP_INTERFACE: | |
5286 | m = match_procedure_in_interface (); | |
5287 | break; | |
5288 | case COMP_DERIVED: | |
713485cc JW |
5289 | m = match_ppc_decl (); |
5290 | break; | |
30b608eb DK |
5291 | case COMP_DERIVED_CONTAINS: |
5292 | m = match_procedure_in_type (); | |
5293 | break; | |
69773742 JW |
5294 | default: |
5295 | return MATCH_NO; | |
5296 | } | |
5297 | ||
5298 | if (m != MATCH_YES) | |
5299 | return m; | |
5300 | ||
524af0d6 | 5301 | if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C")) |
69773742 JW |
5302 | return MATCH_ERROR; |
5303 | ||
5304 | return m; | |
5305 | } | |
5306 | ||
5307 | ||
c3005b0f DK |
5308 | /* Warn if a matched procedure has the same name as an intrinsic; this is |
5309 | simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current | |
5310 | parser-state-stack to find out whether we're in a module. */ | |
5311 | ||
5312 | static void | |
73e42eef | 5313 | do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func) |
c3005b0f DK |
5314 | { |
5315 | bool in_module; | |
5316 | ||
5317 | in_module = (gfc_state_stack->previous | |
5318 | && gfc_state_stack->previous->state == COMP_MODULE); | |
5319 | ||
5320 | gfc_warn_intrinsic_shadow (sym, in_module, func); | |
5321 | } | |
5322 | ||
5323 | ||
6de9cd9a DN |
5324 | /* Match a function declaration. */ |
5325 | ||
5326 | match | |
5327 | gfc_match_function_decl (void) | |
5328 | { | |
5329 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
5330 | gfc_symbol *sym, *result; | |
5331 | locus old_loc; | |
5332 | match m; | |
a8b3b0b6 | 5333 | match suffix_match; |
f5acf0f2 | 5334 | match found_match; /* Status returned by match func. */ |
6de9cd9a DN |
5335 | |
5336 | if (gfc_current_state () != COMP_NONE | |
5337 | && gfc_current_state () != COMP_INTERFACE | |
5338 | && gfc_current_state () != COMP_CONTAINS) | |
5339 | return MATCH_NO; | |
5340 | ||
5341 | gfc_clear_ts (¤t_ts); | |
5342 | ||
63645982 | 5343 | old_loc = gfc_current_locus; |
6de9cd9a | 5344 | |
1c8bcdf7 | 5345 | m = gfc_match_prefix (¤t_ts); |
6de9cd9a DN |
5346 | if (m != MATCH_YES) |
5347 | { | |
63645982 | 5348 | gfc_current_locus = old_loc; |
6de9cd9a DN |
5349 | return m; |
5350 | } | |
5351 | ||
5352 | if (gfc_match ("function% %n", name) != MATCH_YES) | |
5353 | { | |
63645982 | 5354 | gfc_current_locus = old_loc; |
6de9cd9a DN |
5355 | return MATCH_NO; |
5356 | } | |
1a492601 | 5357 | if (get_proc_name (name, &sym, false)) |
6de9cd9a | 5358 | return MATCH_ERROR; |
3070bab4 | 5359 | |
524af0d6 | 5360 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
5361 | sym = sym->result; |
5362 | ||
6de9cd9a DN |
5363 | gfc_new_block = sym; |
5364 | ||
5365 | m = gfc_match_formal_arglist (sym, 0, 0); | |
5366 | if (m == MATCH_NO) | |
2b9a33ae TS |
5367 | { |
5368 | gfc_error ("Expected formal argument list in function " | |
636dff67 | 5369 | "definition at %C"); |
2b9a33ae TS |
5370 | m = MATCH_ERROR; |
5371 | goto cleanup; | |
5372 | } | |
6de9cd9a DN |
5373 | else if (m == MATCH_ERROR) |
5374 | goto cleanup; | |
5375 | ||
5376 | result = NULL; | |
5377 | ||
a8b3b0b6 CR |
5378 | /* According to the draft, the bind(c) and result clause can |
5379 | come in either order after the formal_arg_list (i.e., either | |
5380 | can be first, both can exist together or by themselves or neither | |
5381 | one). Therefore, the match_result can't match the end of the | |
5382 | string, and check for the bind(c) or result clause in either order. */ | |
5383 | found_match = gfc_match_eos (); | |
5384 | ||
5385 | /* Make sure that it isn't already declared as BIND(C). If it is, it | |
5386 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
5387 | not allowed for procedures. */ | |
5388 | if (sym->attr.is_bind_c == 1) | |
5389 | { | |
5390 | sym->attr.is_bind_c = 0; | |
5391 | if (sym->old_symbol != NULL) | |
5392 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5393 | "variables or common blocks", | |
5394 | &(sym->old_symbol->declared_at)); | |
5395 | else | |
5396 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5397 | "variables or common blocks", &gfc_current_locus); | |
6de9cd9a DN |
5398 | } |
5399 | ||
a8b3b0b6 | 5400 | if (found_match != MATCH_YES) |
6de9cd9a | 5401 | { |
a8b3b0b6 CR |
5402 | /* If we haven't found the end-of-statement, look for a suffix. */ |
5403 | suffix_match = gfc_match_suffix (sym, &result); | |
5404 | if (suffix_match == MATCH_YES) | |
5405 | /* Need to get the eos now. */ | |
5406 | found_match = gfc_match_eos (); | |
5407 | else | |
5408 | found_match = suffix_match; | |
6de9cd9a DN |
5409 | } |
5410 | ||
a8b3b0b6 CR |
5411 | if(found_match != MATCH_YES) |
5412 | m = MATCH_ERROR; | |
6de9cd9a DN |
5413 | else |
5414 | { | |
a8b3b0b6 CR |
5415 | /* Make changes to the symbol. */ |
5416 | m = MATCH_ERROR; | |
f5acf0f2 | 5417 | |
524af0d6 | 5418 | if (!gfc_add_function (&sym->attr, sym->name, NULL)) |
a8b3b0b6 | 5419 | goto cleanup; |
f5acf0f2 | 5420 | |
524af0d6 JB |
5421 | if (!gfc_missing_attr (&sym->attr, NULL) |
5422 | || !copy_prefix (&sym->attr, &sym->declared_at)) | |
a8b3b0b6 | 5423 | goto cleanup; |
6de9cd9a | 5424 | |
a99d95a2 | 5425 | /* Delay matching the function characteristics until after the |
1c8bcdf7 | 5426 | specification block by signalling kind=-1. */ |
a99d95a2 PT |
5427 | sym->declared_at = old_loc; |
5428 | if (current_ts.type != BT_UNKNOWN) | |
5429 | current_ts.kind = -1; | |
5430 | else | |
5431 | current_ts.kind = 0; | |
1c8bcdf7 | 5432 | |
a8b3b0b6 CR |
5433 | if (result == NULL) |
5434 | { | |
6de7294f | 5435 | if (current_ts.type != BT_UNKNOWN |
524af0d6 | 5436 | && !gfc_add_type (sym, ¤t_ts, &gfc_current_locus)) |
6de7294f | 5437 | goto cleanup; |
a8b3b0b6 CR |
5438 | sym->result = sym; |
5439 | } | |
5440 | else | |
5441 | { | |
6de7294f | 5442 | if (current_ts.type != BT_UNKNOWN |
524af0d6 | 5443 | && !gfc_add_type (result, ¤t_ts, &gfc_current_locus)) |
6de7294f | 5444 | goto cleanup; |
a8b3b0b6 CR |
5445 | sym->result = result; |
5446 | } | |
5447 | ||
c3005b0f | 5448 | /* Warn if this procedure has the same name as an intrinsic. */ |
73e42eef | 5449 | do_warn_intrinsic_shadow (sym, true); |
c3005b0f | 5450 | |
a8b3b0b6 CR |
5451 | return MATCH_YES; |
5452 | } | |
6de9cd9a DN |
5453 | |
5454 | cleanup: | |
63645982 | 5455 | gfc_current_locus = old_loc; |
6de9cd9a DN |
5456 | return m; |
5457 | } | |
5458 | ||
636dff67 SK |
5459 | |
5460 | /* This is mostly a copy of parse.c(add_global_procedure) but modified to | |
5461 | pass the name of the entry, rather than the gfc_current_block name, and | |
5462 | to return false upon finding an existing global entry. */ | |
68ea355b PT |
5463 | |
5464 | static bool | |
3a43b5b3 TB |
5465 | add_global_entry (const char *name, const char *binding_label, bool sub, |
5466 | locus *where) | |
68ea355b PT |
5467 | { |
5468 | gfc_gsymbol *s; | |
32e8bb8e | 5469 | enum gfc_symbol_type type; |
68ea355b | 5470 | |
7389bce6 | 5471 | type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION; |
68ea355b | 5472 | |
f11de7c5 TB |
5473 | /* Only in Fortran 2003: For procedures with a binding label also the Fortran |
5474 | name is a global identifier. */ | |
5475 | if (!binding_label || gfc_notification_std (GFC_STD_F2008)) | |
68ea355b | 5476 | { |
f11de7c5 TB |
5477 | s = gfc_get_gsymbol (name); |
5478 | ||
5479 | if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type)) | |
5480 | { | |
3a43b5b3 | 5481 | gfc_global_used (s, where); |
f11de7c5 TB |
5482 | return false; |
5483 | } | |
5484 | else | |
5485 | { | |
5486 | s->type = type; | |
77f8682b | 5487 | s->sym_name = name; |
3a43b5b3 | 5488 | s->where = *where; |
f11de7c5 TB |
5489 | s->defined = 1; |
5490 | s->ns = gfc_current_ns; | |
5491 | } | |
68ea355b | 5492 | } |
f11de7c5 TB |
5493 | |
5494 | /* Don't add the symbol multiple times. */ | |
5495 | if (binding_label | |
5496 | && (!gfc_notification_std (GFC_STD_F2008) | |
5497 | || strcmp (name, binding_label) != 0)) | |
5498 | { | |
5499 | s = gfc_get_gsymbol (binding_label); | |
5500 | ||
5501 | if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type)) | |
5502 | { | |
3a43b5b3 | 5503 | gfc_global_used (s, where); |
f11de7c5 TB |
5504 | return false; |
5505 | } | |
5506 | else | |
5507 | { | |
5508 | s->type = type; | |
77f8682b | 5509 | s->sym_name = name; |
f11de7c5 | 5510 | s->binding_label = binding_label; |
3a43b5b3 | 5511 | s->where = *where; |
f11de7c5 TB |
5512 | s->defined = 1; |
5513 | s->ns = gfc_current_ns; | |
5514 | } | |
5515 | } | |
5516 | ||
5517 | return true; | |
68ea355b | 5518 | } |
6de9cd9a | 5519 | |
636dff67 | 5520 | |
6de9cd9a DN |
5521 | /* Match an ENTRY statement. */ |
5522 | ||
5523 | match | |
5524 | gfc_match_entry (void) | |
5525 | { | |
3d79abbd PB |
5526 | gfc_symbol *proc; |
5527 | gfc_symbol *result; | |
5528 | gfc_symbol *entry; | |
6de9cd9a DN |
5529 | char name[GFC_MAX_SYMBOL_LEN + 1]; |
5530 | gfc_compile_state state; | |
5531 | match m; | |
3d79abbd | 5532 | gfc_entry_list *el; |
c96cfa49 | 5533 | locus old_loc; |
1a492601 | 5534 | bool module_procedure; |
bc3e7a8c TB |
5535 | char peek_char; |
5536 | match is_bind_c; | |
6de9cd9a DN |
5537 | |
5538 | m = gfc_match_name (name); | |
5539 | if (m != MATCH_YES) | |
5540 | return m; | |
5541 | ||
524af0d6 | 5542 | if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C")) |
58fc89f6 TB |
5543 | return MATCH_ERROR; |
5544 | ||
3d79abbd | 5545 | state = gfc_current_state (); |
4c93c95a | 5546 | if (state != COMP_SUBROUTINE && state != COMP_FUNCTION) |
3d79abbd | 5547 | { |
4c93c95a FXC |
5548 | switch (state) |
5549 | { | |
5550 | case COMP_PROGRAM: | |
5551 | gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM"); | |
5552 | break; | |
5553 | case COMP_MODULE: | |
5554 | gfc_error ("ENTRY statement at %C cannot appear within a MODULE"); | |
5555 | break; | |
5556 | case COMP_BLOCK_DATA: | |
636dff67 SK |
5557 | gfc_error ("ENTRY statement at %C cannot appear within " |
5558 | "a BLOCK DATA"); | |
4c93c95a FXC |
5559 | break; |
5560 | case COMP_INTERFACE: | |
636dff67 SK |
5561 | gfc_error ("ENTRY statement at %C cannot appear within " |
5562 | "an INTERFACE"); | |
4c93c95a FXC |
5563 | break; |
5564 | case COMP_DERIVED: | |
636dff67 SK |
5565 | gfc_error ("ENTRY statement at %C cannot appear within " |
5566 | "a DERIVED TYPE block"); | |
4c93c95a FXC |
5567 | break; |
5568 | case COMP_IF: | |
636dff67 SK |
5569 | gfc_error ("ENTRY statement at %C cannot appear within " |
5570 | "an IF-THEN block"); | |
4c93c95a FXC |
5571 | break; |
5572 | case COMP_DO: | |
8c6a85e3 | 5573 | case COMP_DO_CONCURRENT: |
636dff67 SK |
5574 | gfc_error ("ENTRY statement at %C cannot appear within " |
5575 | "a DO block"); | |
4c93c95a FXC |
5576 | break; |
5577 | case COMP_SELECT: | |
636dff67 SK |
5578 | gfc_error ("ENTRY statement at %C cannot appear within " |
5579 | "a SELECT block"); | |
4c93c95a FXC |
5580 | break; |
5581 | case COMP_FORALL: | |
636dff67 SK |
5582 | gfc_error ("ENTRY statement at %C cannot appear within " |
5583 | "a FORALL block"); | |
4c93c95a FXC |
5584 | break; |
5585 | case COMP_WHERE: | |
636dff67 SK |
5586 | gfc_error ("ENTRY statement at %C cannot appear within " |
5587 | "a WHERE block"); | |
4c93c95a FXC |
5588 | break; |
5589 | case COMP_CONTAINS: | |
636dff67 SK |
5590 | gfc_error ("ENTRY statement at %C cannot appear within " |
5591 | "a contained subprogram"); | |
4c93c95a FXC |
5592 | break; |
5593 | default: | |
5594 | gfc_internal_error ("gfc_match_entry(): Bad state"); | |
5595 | } | |
3d79abbd PB |
5596 | return MATCH_ERROR; |
5597 | } | |
5598 | ||
1a492601 | 5599 | module_procedure = gfc_current_ns->parent != NULL |
636dff67 SK |
5600 | && gfc_current_ns->parent->proc_name |
5601 | && gfc_current_ns->parent->proc_name->attr.flavor | |
5602 | == FL_MODULE; | |
1a492601 | 5603 | |
3d79abbd PB |
5604 | if (gfc_current_ns->parent != NULL |
5605 | && gfc_current_ns->parent->proc_name | |
1a492601 | 5606 | && !module_procedure) |
3d79abbd PB |
5607 | { |
5608 | gfc_error("ENTRY statement at %C cannot appear in a " | |
5609 | "contained procedure"); | |
5610 | return MATCH_ERROR; | |
5611 | } | |
5612 | ||
1a492601 PT |
5613 | /* Module function entries need special care in get_proc_name |
5614 | because previous references within the function will have | |
5615 | created symbols attached to the current namespace. */ | |
5616 | if (get_proc_name (name, &entry, | |
5617 | gfc_current_ns->parent != NULL | |
ecd3b73c | 5618 | && module_procedure)) |
6de9cd9a DN |
5619 | return MATCH_ERROR; |
5620 | ||
3d79abbd PB |
5621 | proc = gfc_current_block (); |
5622 | ||
bc3e7a8c TB |
5623 | /* Make sure that it isn't already declared as BIND(C). If it is, it |
5624 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
5625 | not allowed for procedures. */ | |
5626 | if (entry->attr.is_bind_c == 1) | |
5627 | { | |
5628 | entry->attr.is_bind_c = 0; | |
5629 | if (entry->old_symbol != NULL) | |
5630 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5631 | "variables or common blocks", | |
5632 | &(entry->old_symbol->declared_at)); | |
5633 | else | |
5634 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5635 | "variables or common blocks", &gfc_current_locus); | |
5636 | } | |
f5acf0f2 | 5637 | |
bc3e7a8c TB |
5638 | /* Check what next non-whitespace character is so we can tell if there |
5639 | is the required parens if we have a BIND(C). */ | |
3a43b5b3 | 5640 | old_loc = gfc_current_locus; |
bc3e7a8c | 5641 | gfc_gobble_whitespace (); |
8fc541d3 | 5642 | peek_char = gfc_peek_ascii_char (); |
bc3e7a8c | 5643 | |
3d79abbd | 5644 | if (state == COMP_SUBROUTINE) |
6de9cd9a | 5645 | { |
6de9cd9a DN |
5646 | m = gfc_match_formal_arglist (entry, 0, 1); |
5647 | if (m != MATCH_YES) | |
5648 | return MATCH_ERROR; | |
5649 | ||
1eabf70a TB |
5650 | /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can |
5651 | never be an internal procedure. */ | |
5652 | is_bind_c = gfc_match_bind_c (entry, true); | |
bc3e7a8c TB |
5653 | if (is_bind_c == MATCH_ERROR) |
5654 | return MATCH_ERROR; | |
5655 | if (is_bind_c == MATCH_YES) | |
5656 | { | |
5657 | if (peek_char != '(') | |
5658 | { | |
5659 | gfc_error ("Missing required parentheses before BIND(C) at %C"); | |
5660 | return MATCH_ERROR; | |
5661 | } | |
524af0d6 JB |
5662 | if (!gfc_add_is_bind_c (&(entry->attr), entry->name, |
5663 | &(entry->declared_at), 1)) | |
bc3e7a8c TB |
5664 | return MATCH_ERROR; |
5665 | } | |
5666 | ||
f11de7c5 | 5667 | if (!gfc_current_ns->parent |
3a43b5b3 TB |
5668 | && !add_global_entry (name, entry->binding_label, true, |
5669 | &old_loc)) | |
f11de7c5 TB |
5670 | return MATCH_ERROR; |
5671 | ||
5672 | /* An entry in a subroutine. */ | |
524af0d6 JB |
5673 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
5674 | || !gfc_add_subroutine (&entry->attr, entry->name, NULL)) | |
6de9cd9a | 5675 | return MATCH_ERROR; |
3d79abbd PB |
5676 | } |
5677 | else | |
5678 | { | |
c96cfa49 | 5679 | /* An entry in a function. |
636dff67 SK |
5680 | We need to take special care because writing |
5681 | ENTRY f() | |
5682 | as | |
5683 | ENTRY f | |
5684 | is allowed, whereas | |
5685 | ENTRY f() RESULT (r) | |
5686 | can't be written as | |
5687 | ENTRY f RESULT (r). */ | |
c96cfa49 TS |
5688 | if (gfc_match_eos () == MATCH_YES) |
5689 | { | |
5690 | gfc_current_locus = old_loc; | |
5691 | /* Match the empty argument list, and add the interface to | |
5692 | the symbol. */ | |
5693 | m = gfc_match_formal_arglist (entry, 0, 1); | |
5694 | } | |
5695 | else | |
5696 | m = gfc_match_formal_arglist (entry, 0, 0); | |
5697 | ||
6de9cd9a DN |
5698 | if (m != MATCH_YES) |
5699 | return MATCH_ERROR; | |
5700 | ||
6de9cd9a DN |
5701 | result = NULL; |
5702 | ||
5703 | if (gfc_match_eos () == MATCH_YES) | |
5704 | { | |
524af0d6 JB |
5705 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
5706 | || !gfc_add_function (&entry->attr, entry->name, NULL)) | |
6de9cd9a DN |
5707 | return MATCH_ERROR; |
5708 | ||
d198b59a | 5709 | entry->result = entry; |
6de9cd9a DN |
5710 | } |
5711 | else | |
5712 | { | |
bc3e7a8c | 5713 | m = gfc_match_suffix (entry, &result); |
6de9cd9a DN |
5714 | if (m == MATCH_NO) |
5715 | gfc_syntax_error (ST_ENTRY); | |
5716 | if (m != MATCH_YES) | |
5717 | return MATCH_ERROR; | |
5718 | ||
bc3e7a8c TB |
5719 | if (result) |
5720 | { | |
524af0d6 JB |
5721 | if (!gfc_add_result (&result->attr, result->name, NULL) |
5722 | || !gfc_add_entry (&entry->attr, result->name, NULL) | |
5723 | || !gfc_add_function (&entry->attr, result->name, NULL)) | |
bc3e7a8c TB |
5724 | return MATCH_ERROR; |
5725 | entry->result = result; | |
5726 | } | |
5727 | else | |
5728 | { | |
524af0d6 JB |
5729 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
5730 | || !gfc_add_function (&entry->attr, entry->name, NULL)) | |
bc3e7a8c TB |
5731 | return MATCH_ERROR; |
5732 | entry->result = entry; | |
5733 | } | |
6de9cd9a | 5734 | } |
f11de7c5 TB |
5735 | |
5736 | if (!gfc_current_ns->parent | |
3a43b5b3 TB |
5737 | && !add_global_entry (name, entry->binding_label, false, |
5738 | &old_loc)) | |
f11de7c5 | 5739 | return MATCH_ERROR; |
6de9cd9a DN |
5740 | } |
5741 | ||
5742 | if (gfc_match_eos () != MATCH_YES) | |
5743 | { | |
5744 | gfc_syntax_error (ST_ENTRY); | |
5745 | return MATCH_ERROR; | |
5746 | } | |
5747 | ||
3d79abbd PB |
5748 | entry->attr.recursive = proc->attr.recursive; |
5749 | entry->attr.elemental = proc->attr.elemental; | |
5750 | entry->attr.pure = proc->attr.pure; | |
6de9cd9a | 5751 | |
3d79abbd PB |
5752 | el = gfc_get_entry_list (); |
5753 | el->sym = entry; | |
5754 | el->next = gfc_current_ns->entries; | |
5755 | gfc_current_ns->entries = el; | |
5756 | if (el->next) | |
5757 | el->id = el->next->id + 1; | |
5758 | else | |
5759 | el->id = 1; | |
6de9cd9a | 5760 | |
3d79abbd PB |
5761 | new_st.op = EXEC_ENTRY; |
5762 | new_st.ext.entry = el; | |
5763 | ||
5764 | return MATCH_YES; | |
6de9cd9a DN |
5765 | } |
5766 | ||
5767 | ||
5768 | /* Match a subroutine statement, including optional prefixes. */ | |
5769 | ||
5770 | match | |
5771 | gfc_match_subroutine (void) | |
5772 | { | |
5773 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
5774 | gfc_symbol *sym; | |
5775 | match m; | |
a8b3b0b6 CR |
5776 | match is_bind_c; |
5777 | char peek_char; | |
1eabf70a | 5778 | bool allow_binding_name; |
6de9cd9a DN |
5779 | |
5780 | if (gfc_current_state () != COMP_NONE | |
5781 | && gfc_current_state () != COMP_INTERFACE | |
5782 | && gfc_current_state () != COMP_CONTAINS) | |
5783 | return MATCH_NO; | |
5784 | ||
1c8bcdf7 | 5785 | m = gfc_match_prefix (NULL); |
6de9cd9a DN |
5786 | if (m != MATCH_YES) |
5787 | return m; | |
5788 | ||
5789 | m = gfc_match ("subroutine% %n", name); | |
5790 | if (m != MATCH_YES) | |
5791 | return m; | |
5792 | ||
1a492601 | 5793 | if (get_proc_name (name, &sym, false)) |
6de9cd9a | 5794 | return MATCH_ERROR; |
3070bab4 | 5795 | |
7fcd5ad5 | 5796 | /* Set declared_at as it might point to, e.g., a PUBLIC statement, if |
1cc0e193 | 5797 | the symbol existed before. */ |
7fcd5ad5 TB |
5798 | sym->declared_at = gfc_current_locus; |
5799 | ||
524af0d6 | 5800 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
5801 | sym = sym->result; |
5802 | ||
6de9cd9a DN |
5803 | gfc_new_block = sym; |
5804 | ||
a8b3b0b6 | 5805 | /* Check what next non-whitespace character is so we can tell if there |
bc3e7a8c | 5806 | is the required parens if we have a BIND(C). */ |
a8b3b0b6 | 5807 | gfc_gobble_whitespace (); |
8fc541d3 | 5808 | peek_char = gfc_peek_ascii_char (); |
f5acf0f2 | 5809 | |
524af0d6 | 5810 | if (!gfc_add_subroutine (&sym->attr, sym->name, NULL)) |
6de9cd9a DN |
5811 | return MATCH_ERROR; |
5812 | ||
5813 | if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES) | |
5814 | return MATCH_ERROR; | |
5815 | ||
a8b3b0b6 CR |
5816 | /* Make sure that it isn't already declared as BIND(C). If it is, it |
5817 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
5818 | not allowed for procedures. */ | |
5819 | if (sym->attr.is_bind_c == 1) | |
5820 | { | |
5821 | sym->attr.is_bind_c = 0; | |
5822 | if (sym->old_symbol != NULL) | |
5823 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5824 | "variables or common blocks", | |
5825 | &(sym->old_symbol->declared_at)); | |
5826 | else | |
5827 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
5828 | "variables or common blocks", &gfc_current_locus); | |
5829 | } | |
1eabf70a TB |
5830 | |
5831 | /* C binding names are not allowed for internal procedures. */ | |
5832 | if (gfc_current_state () == COMP_CONTAINS | |
5833 | && sym->ns->proc_name->attr.flavor != FL_MODULE) | |
5834 | allow_binding_name = false; | |
5835 | else | |
5836 | allow_binding_name = true; | |
5837 | ||
a8b3b0b6 CR |
5838 | /* Here, we are just checking if it has the bind(c) attribute, and if |
5839 | so, then we need to make sure it's all correct. If it doesn't, | |
5840 | we still need to continue matching the rest of the subroutine line. */ | |
1eabf70a | 5841 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
5842 | if (is_bind_c == MATCH_ERROR) |
5843 | { | |
5844 | /* There was an attempt at the bind(c), but it was wrong. An | |
5845 | error message should have been printed w/in the gfc_match_bind_c | |
5846 | so here we'll just return the MATCH_ERROR. */ | |
5847 | return MATCH_ERROR; | |
5848 | } | |
5849 | ||
5850 | if (is_bind_c == MATCH_YES) | |
5851 | { | |
1eabf70a | 5852 | /* The following is allowed in the Fortran 2008 draft. */ |
01f4fff1 | 5853 | if (gfc_current_state () == COMP_CONTAINS |
1eabf70a | 5854 | && sym->ns->proc_name->attr.flavor != FL_MODULE |
524af0d6 JB |
5855 | && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute " |
5856 | "at %L may not be specified for an internal " | |
5857 | "procedure", &gfc_current_locus)) | |
1eabf70a TB |
5858 | return MATCH_ERROR; |
5859 | ||
a8b3b0b6 CR |
5860 | if (peek_char != '(') |
5861 | { | |
5862 | gfc_error ("Missing required parentheses before BIND(C) at %C"); | |
5863 | return MATCH_ERROR; | |
5864 | } | |
524af0d6 JB |
5865 | if (!gfc_add_is_bind_c (&(sym->attr), sym->name, |
5866 | &(sym->declared_at), 1)) | |
a8b3b0b6 CR |
5867 | return MATCH_ERROR; |
5868 | } | |
f5acf0f2 | 5869 | |
6de9cd9a DN |
5870 | if (gfc_match_eos () != MATCH_YES) |
5871 | { | |
5872 | gfc_syntax_error (ST_SUBROUTINE); | |
5873 | return MATCH_ERROR; | |
5874 | } | |
5875 | ||
524af0d6 | 5876 | if (!copy_prefix (&sym->attr, &sym->declared_at)) |
6de9cd9a DN |
5877 | return MATCH_ERROR; |
5878 | ||
c3005b0f | 5879 | /* Warn if it has the same name as an intrinsic. */ |
73e42eef | 5880 | do_warn_intrinsic_shadow (sym, false); |
c3005b0f | 5881 | |
6de9cd9a DN |
5882 | return MATCH_YES; |
5883 | } | |
5884 | ||
5885 | ||
3b37ccd4 FXC |
5886 | /* Check that the NAME identifier in a BIND attribute or statement |
5887 | is conform to C identifier rules. */ | |
5888 | ||
5889 | match | |
5890 | check_bind_name_identifier (char **name) | |
5891 | { | |
5892 | char *n = *name, *p; | |
5893 | ||
5894 | /* Remove leading spaces. */ | |
5895 | while (*n == ' ') | |
5896 | n++; | |
5897 | ||
5898 | /* On an empty string, free memory and set name to NULL. */ | |
5899 | if (*n == '\0') | |
5900 | { | |
5901 | free (*name); | |
5902 | *name = NULL; | |
5903 | return MATCH_YES; | |
5904 | } | |
5905 | ||
5906 | /* Remove trailing spaces. */ | |
5907 | p = n + strlen(n) - 1; | |
5908 | while (*p == ' ') | |
5909 | *(p--) = '\0'; | |
5910 | ||
5911 | /* Insert the identifier into the symbol table. */ | |
5912 | p = xstrdup (n); | |
5913 | free (*name); | |
5914 | *name = p; | |
5915 | ||
5916 | /* Now check that identifier is valid under C rules. */ | |
5917 | if (ISDIGIT (*p)) | |
5918 | { | |
5919 | gfc_error ("Invalid C identifier in NAME= specifier at %C"); | |
5920 | return MATCH_ERROR; | |
5921 | } | |
5922 | ||
5923 | for (; *p; p++) | |
5924 | if (!(ISALNUM (*p) || *p == '_' || *p == '$')) | |
5925 | { | |
5926 | gfc_error ("Invalid C identifier in NAME= specifier at %C"); | |
5927 | return MATCH_ERROR; | |
5928 | } | |
5929 | ||
5930 | return MATCH_YES; | |
5931 | } | |
5932 | ||
5933 | ||
a8b3b0b6 CR |
5934 | /* Match a BIND(C) specifier, with the optional 'name=' specifier if |
5935 | given, and set the binding label in either the given symbol (if not | |
86bf520d | 5936 | NULL), or in the current_ts. The symbol may be NULL because we may |
a8b3b0b6 CR |
5937 | encounter the BIND(C) before the declaration itself. Return |
5938 | MATCH_NO if what we're looking at isn't a BIND(C) specifier, | |
5939 | MATCH_ERROR if it is a BIND(C) clause but an error was encountered, | |
5940 | or MATCH_YES if the specifier was correct and the binding label and | |
5941 | bind(c) fields were set correctly for the given symbol or the | |
1eabf70a TB |
5942 | current_ts. If allow_binding_name is false, no binding name may be |
5943 | given. */ | |
a8b3b0b6 CR |
5944 | |
5945 | match | |
1eabf70a | 5946 | gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name) |
a8b3b0b6 | 5947 | { |
3b37ccd4 FXC |
5948 | char *binding_label = NULL; |
5949 | gfc_expr *e = NULL; | |
a8b3b0b6 | 5950 | |
f5acf0f2 | 5951 | /* Initialize the flag that specifies whether we encountered a NAME= |
a8b3b0b6 CR |
5952 | specifier or not. */ |
5953 | has_name_equals = 0; | |
5954 | ||
a8b3b0b6 CR |
5955 | /* This much we have to be able to match, in this order, if |
5956 | there is a bind(c) label. */ | |
5957 | if (gfc_match (" bind ( c ") != MATCH_YES) | |
5958 | return MATCH_NO; | |
5959 | ||
5960 | /* Now see if there is a binding label, or if we've reached the | |
5961 | end of the bind(c) attribute without one. */ | |
5962 | if (gfc_match_char (',') == MATCH_YES) | |
5963 | { | |
5964 | if (gfc_match (" name = ") != MATCH_YES) | |
5965 | { | |
5966 | gfc_error ("Syntax error in NAME= specifier for binding label " | |
5967 | "at %C"); | |
5968 | /* should give an error message here */ | |
5969 | return MATCH_ERROR; | |
5970 | } | |
5971 | ||
5972 | has_name_equals = 1; | |
5973 | ||
3b37ccd4 FXC |
5974 | if (gfc_match_init_expr (&e) != MATCH_YES) |
5975 | { | |
5976 | gfc_free_expr (e); | |
5977 | return MATCH_ERROR; | |
5978 | } | |
f5acf0f2 | 5979 | |
3b37ccd4 | 5980 | if (!gfc_simplify_expr(e, 0)) |
a8b3b0b6 | 5981 | { |
3b37ccd4 FXC |
5982 | gfc_error ("NAME= specifier at %C should be a constant expression"); |
5983 | gfc_free_expr (e); | |
5984 | return MATCH_ERROR; | |
a8b3b0b6 | 5985 | } |
3b37ccd4 FXC |
5986 | |
5987 | if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER | |
5988 | || e->ts.kind != gfc_default_character_kind || e->rank != 0) | |
a8b3b0b6 | 5989 | { |
3b37ccd4 FXC |
5990 | gfc_error ("NAME= specifier at %C should be a scalar of " |
5991 | "default character kind"); | |
5992 | gfc_free_expr(e); | |
5993 | return MATCH_ERROR; | |
a8b3b0b6 | 5994 | } |
3b37ccd4 FXC |
5995 | |
5996 | // Get a C string from the Fortran string constant | |
5997 | binding_label = gfc_widechar_to_char (e->value.character.string, | |
5998 | e->value.character.length); | |
5999 | gfc_free_expr(e); | |
6000 | ||
6001 | // Check that it is valid (old gfc_match_name_C) | |
6002 | if (check_bind_name_identifier (&binding_label) != MATCH_YES) | |
6003 | return MATCH_ERROR; | |
6004 | } | |
a8b3b0b6 CR |
6005 | |
6006 | /* Get the required right paren. */ | |
6007 | if (gfc_match_char (')') != MATCH_YES) | |
6008 | { | |
6009 | gfc_error ("Missing closing paren for binding label at %C"); | |
6010 | return MATCH_ERROR; | |
6011 | } | |
6012 | ||
1eabf70a TB |
6013 | if (has_name_equals && !allow_binding_name) |
6014 | { | |
6015 | gfc_error ("No binding name is allowed in BIND(C) at %C"); | |
6016 | return MATCH_ERROR; | |
6017 | } | |
6018 | ||
6019 | if (has_name_equals && sym != NULL && sym->attr.dummy) | |
6020 | { | |
6021 | gfc_error ("For dummy procedure %s, no binding name is " | |
6022 | "allowed in BIND(C) at %C", sym->name); | |
6023 | return MATCH_ERROR; | |
6024 | } | |
6025 | ||
6026 | ||
a8b3b0b6 CR |
6027 | /* Save the binding label to the symbol. If sym is null, we're |
6028 | probably matching the typespec attributes of a declaration and | |
6029 | haven't gotten the name yet, and therefore, no symbol yet. */ | |
62603fae | 6030 | if (binding_label) |
a8b3b0b6 CR |
6031 | { |
6032 | if (sym != NULL) | |
62603fae | 6033 | sym->binding_label = binding_label; |
a8b3b0b6 | 6034 | else |
62603fae | 6035 | curr_binding_label = binding_label; |
a8b3b0b6 | 6036 | } |
1eabf70a | 6037 | else if (allow_binding_name) |
a8b3b0b6 CR |
6038 | { |
6039 | /* No binding label, but if symbol isn't null, we | |
1eabf70a TB |
6040 | can set the label for it here. |
6041 | If name="" or allow_binding_name is false, no C binding name is | |
1cc0e193 | 6042 | created. */ |
a8b3b0b6 | 6043 | if (sym != NULL && sym->name != NULL && has_name_equals == 0) |
62603fae | 6044 | sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name)); |
a8b3b0b6 | 6045 | } |
9e1d712c | 6046 | |
129d15a3 JW |
6047 | if (has_name_equals && gfc_current_state () == COMP_INTERFACE |
6048 | && current_interface.type == INTERFACE_ABSTRACT) | |
9e1d712c TB |
6049 | { |
6050 | gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C"); | |
6051 | return MATCH_ERROR; | |
6052 | } | |
6053 | ||
a8b3b0b6 CR |
6054 | return MATCH_YES; |
6055 | } | |
6056 | ||
6057 | ||
1f2959f0 | 6058 | /* Return nonzero if we're currently compiling a contained procedure. */ |
ddc9ce91 TS |
6059 | |
6060 | static int | |
6061 | contained_procedure (void) | |
6062 | { | |
083de129 | 6063 | gfc_state_data *s = gfc_state_stack; |
ddc9ce91 | 6064 | |
083de129 TB |
6065 | if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION) |
6066 | && s->previous != NULL && s->previous->state == COMP_CONTAINS) | |
6067 | return 1; | |
ddc9ce91 TS |
6068 | |
6069 | return 0; | |
6070 | } | |
6071 | ||
d51347f9 | 6072 | /* Set the kind of each enumerator. The kind is selected such that it is |
25d8f0a2 TS |
6073 | interoperable with the corresponding C enumeration type, making |
6074 | sure that -fshort-enums is honored. */ | |
6075 | ||
6076 | static void | |
6077 | set_enum_kind(void) | |
6078 | { | |
6079 | enumerator_history *current_history = NULL; | |
6080 | int kind; | |
6081 | int i; | |
6082 | ||
6083 | if (max_enum == NULL || enum_history == NULL) | |
6084 | return; | |
6085 | ||
cab129d1 | 6086 | if (!flag_short_enums) |
d51347f9 TB |
6087 | return; |
6088 | ||
25d8f0a2 TS |
6089 | i = 0; |
6090 | do | |
6091 | { | |
6092 | kind = gfc_integer_kinds[i++].kind; | |
6093 | } | |
d51347f9 | 6094 | while (kind < gfc_c_int_kind |
25d8f0a2 TS |
6095 | && gfc_check_integer_range (max_enum->initializer->value.integer, |
6096 | kind) != ARITH_OK); | |
6097 | ||
6098 | current_history = enum_history; | |
6099 | while (current_history != NULL) | |
6100 | { | |
6101 | current_history->sym->ts.kind = kind; | |
6102 | current_history = current_history->next; | |
6103 | } | |
6104 | } | |
6105 | ||
636dff67 | 6106 | |
6de9cd9a | 6107 | /* Match any of the various end-block statements. Returns the type of |
9abe5e56 DK |
6108 | END to the caller. The END INTERFACE, END IF, END DO, END SELECT |
6109 | and END BLOCK statements cannot be replaced by a single END statement. */ | |
6de9cd9a DN |
6110 | |
6111 | match | |
636dff67 | 6112 | gfc_match_end (gfc_statement *st) |
6de9cd9a DN |
6113 | { |
6114 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6115 | gfc_compile_state state; | |
6116 | locus old_loc; | |
6117 | const char *block_name; | |
6118 | const char *target; | |
ddc9ce91 | 6119 | int eos_ok; |
6de9cd9a | 6120 | match m; |
0cab6b73 TK |
6121 | gfc_namespace *parent_ns, *ns, *prev_ns; |
6122 | gfc_namespace **nsp; | |
6de9cd9a | 6123 | |
63645982 | 6124 | old_loc = gfc_current_locus; |
6de9cd9a DN |
6125 | if (gfc_match ("end") != MATCH_YES) |
6126 | return MATCH_NO; | |
6127 | ||
6128 | state = gfc_current_state (); | |
636dff67 SK |
6129 | block_name = gfc_current_block () == NULL |
6130 | ? NULL : gfc_current_block ()->name; | |
6de9cd9a | 6131 | |
03af1e4c | 6132 | switch (state) |
6de9cd9a | 6133 | { |
03af1e4c DK |
6134 | case COMP_ASSOCIATE: |
6135 | case COMP_BLOCK: | |
3a1fd30c | 6136 | if (!strncmp (block_name, "block@", strlen("block@"))) |
03af1e4c DK |
6137 | block_name = NULL; |
6138 | break; | |
6139 | ||
6140 | case COMP_CONTAINS: | |
6141 | case COMP_DERIVED_CONTAINS: | |
6de9cd9a | 6142 | state = gfc_state_stack->previous->state; |
636dff67 SK |
6143 | block_name = gfc_state_stack->previous->sym == NULL |
6144 | ? NULL : gfc_state_stack->previous->sym->name; | |
03af1e4c DK |
6145 | break; |
6146 | ||
6147 | default: | |
6148 | break; | |
6de9cd9a DN |
6149 | } |
6150 | ||
6151 | switch (state) | |
6152 | { | |
6153 | case COMP_NONE: | |
6154 | case COMP_PROGRAM: | |
6155 | *st = ST_END_PROGRAM; | |
6156 | target = " program"; | |
ddc9ce91 | 6157 | eos_ok = 1; |
6de9cd9a DN |
6158 | break; |
6159 | ||
6160 | case COMP_SUBROUTINE: | |
6161 | *st = ST_END_SUBROUTINE; | |
6162 | target = " subroutine"; | |
ddc9ce91 | 6163 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
6164 | break; |
6165 | ||
6166 | case COMP_FUNCTION: | |
6167 | *st = ST_END_FUNCTION; | |
6168 | target = " function"; | |
ddc9ce91 | 6169 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
6170 | break; |
6171 | ||
6172 | case COMP_BLOCK_DATA: | |
6173 | *st = ST_END_BLOCK_DATA; | |
6174 | target = " block data"; | |
ddc9ce91 | 6175 | eos_ok = 1; |
6de9cd9a DN |
6176 | break; |
6177 | ||
6178 | case COMP_MODULE: | |
6179 | *st = ST_END_MODULE; | |
6180 | target = " module"; | |
ddc9ce91 | 6181 | eos_ok = 1; |
6de9cd9a DN |
6182 | break; |
6183 | ||
6184 | case COMP_INTERFACE: | |
6185 | *st = ST_END_INTERFACE; | |
6186 | target = " interface"; | |
ddc9ce91 | 6187 | eos_ok = 0; |
6de9cd9a DN |
6188 | break; |
6189 | ||
6190 | case COMP_DERIVED: | |
30b608eb | 6191 | case COMP_DERIVED_CONTAINS: |
6de9cd9a DN |
6192 | *st = ST_END_TYPE; |
6193 | target = " type"; | |
ddc9ce91 | 6194 | eos_ok = 0; |
6de9cd9a DN |
6195 | break; |
6196 | ||
03af1e4c DK |
6197 | case COMP_ASSOCIATE: |
6198 | *st = ST_END_ASSOCIATE; | |
6199 | target = " associate"; | |
6200 | eos_ok = 0; | |
6201 | break; | |
6202 | ||
9abe5e56 DK |
6203 | case COMP_BLOCK: |
6204 | *st = ST_END_BLOCK; | |
6205 | target = " block"; | |
6206 | eos_ok = 0; | |
6207 | break; | |
6208 | ||
6de9cd9a DN |
6209 | case COMP_IF: |
6210 | *st = ST_ENDIF; | |
6211 | target = " if"; | |
ddc9ce91 | 6212 | eos_ok = 0; |
6de9cd9a DN |
6213 | break; |
6214 | ||
6215 | case COMP_DO: | |
8c6a85e3 | 6216 | case COMP_DO_CONCURRENT: |
6de9cd9a DN |
6217 | *st = ST_ENDDO; |
6218 | target = " do"; | |
ddc9ce91 | 6219 | eos_ok = 0; |
6de9cd9a DN |
6220 | break; |
6221 | ||
d0a4a61c TB |
6222 | case COMP_CRITICAL: |
6223 | *st = ST_END_CRITICAL; | |
6224 | target = " critical"; | |
6225 | eos_ok = 0; | |
6226 | break; | |
6227 | ||
6de9cd9a | 6228 | case COMP_SELECT: |
cf2b3c22 | 6229 | case COMP_SELECT_TYPE: |
6de9cd9a DN |
6230 | *st = ST_END_SELECT; |
6231 | target = " select"; | |
ddc9ce91 | 6232 | eos_ok = 0; |
6de9cd9a DN |
6233 | break; |
6234 | ||
6235 | case COMP_FORALL: | |
6236 | *st = ST_END_FORALL; | |
6237 | target = " forall"; | |
ddc9ce91 | 6238 | eos_ok = 0; |
6de9cd9a DN |
6239 | break; |
6240 | ||
6241 | case COMP_WHERE: | |
6242 | *st = ST_END_WHERE; | |
6243 | target = " where"; | |
ddc9ce91 | 6244 | eos_ok = 0; |
6de9cd9a DN |
6245 | break; |
6246 | ||
25d8f0a2 TS |
6247 | case COMP_ENUM: |
6248 | *st = ST_END_ENUM; | |
6249 | target = " enum"; | |
6250 | eos_ok = 0; | |
6251 | last_initializer = NULL; | |
6252 | set_enum_kind (); | |
6253 | gfc_free_enum_history (); | |
6254 | break; | |
6255 | ||
6de9cd9a DN |
6256 | default: |
6257 | gfc_error ("Unexpected END statement at %C"); | |
6258 | goto cleanup; | |
6259 | } | |
6260 | ||
3a43b5b3 | 6261 | old_loc = gfc_current_locus; |
6de9cd9a DN |
6262 | if (gfc_match_eos () == MATCH_YES) |
6263 | { | |
272001a2 TB |
6264 | if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION)) |
6265 | { | |
524af0d6 JB |
6266 | if (!gfc_notify_std (GFC_STD_F2008, "END statement " |
6267 | "instead of %s statement at %L", | |
6268 | gfc_ascii_statement(*st), &old_loc)) | |
272001a2 TB |
6269 | goto cleanup; |
6270 | } | |
6271 | else if (!eos_ok) | |
6de9cd9a | 6272 | { |
66e4ab31 | 6273 | /* We would have required END [something]. */ |
59ce85b5 TS |
6274 | gfc_error ("%s statement expected at %L", |
6275 | gfc_ascii_statement (*st), &old_loc); | |
6de9cd9a DN |
6276 | goto cleanup; |
6277 | } | |
6278 | ||
6279 | return MATCH_YES; | |
6280 | } | |
6281 | ||
6282 | /* Verify that we've got the sort of end-block that we're expecting. */ | |
6283 | if (gfc_match (target) != MATCH_YES) | |
6284 | { | |
3a43b5b3 TB |
6285 | gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st), |
6286 | &old_loc); | |
6de9cd9a DN |
6287 | goto cleanup; |
6288 | } | |
6289 | ||
3a43b5b3 | 6290 | old_loc = gfc_current_locus; |
6de9cd9a DN |
6291 | /* If we're at the end, make sure a block name wasn't required. */ |
6292 | if (gfc_match_eos () == MATCH_YES) | |
6293 | { | |
6294 | ||
690af379 | 6295 | if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT |
d0a4a61c | 6296 | && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK |
03af1e4c | 6297 | && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL) |
6de9cd9a DN |
6298 | return MATCH_YES; |
6299 | ||
9abe5e56 | 6300 | if (!block_name) |
6de9cd9a DN |
6301 | return MATCH_YES; |
6302 | ||
c4100eae | 6303 | gfc_error ("Expected block name of %qs in %s statement at %L", |
3a43b5b3 | 6304 | block_name, gfc_ascii_statement (*st), &old_loc); |
6de9cd9a DN |
6305 | |
6306 | return MATCH_ERROR; | |
6307 | } | |
6308 | ||
6309 | /* END INTERFACE has a special handler for its several possible endings. */ | |
6310 | if (*st == ST_END_INTERFACE) | |
6311 | return gfc_match_end_interface (); | |
6312 | ||
66e4ab31 SK |
6313 | /* We haven't hit the end of statement, so what is left must be an |
6314 | end-name. */ | |
6de9cd9a DN |
6315 | m = gfc_match_space (); |
6316 | if (m == MATCH_YES) | |
6317 | m = gfc_match_name (name); | |
6318 | ||
6319 | if (m == MATCH_NO) | |
6320 | gfc_error ("Expected terminating name at %C"); | |
6321 | if (m != MATCH_YES) | |
6322 | goto cleanup; | |
6323 | ||
6324 | if (block_name == NULL) | |
6325 | goto syntax; | |
6326 | ||
3070bab4 | 6327 | if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0) |
6de9cd9a | 6328 | { |
c4100eae | 6329 | gfc_error ("Expected label %qs for %s statement at %C", block_name, |
6de9cd9a DN |
6330 | gfc_ascii_statement (*st)); |
6331 | goto cleanup; | |
6332 | } | |
3070bab4 JW |
6333 | /* Procedure pointer as function result. */ |
6334 | else if (strcmp (block_name, "ppr@") == 0 | |
6335 | && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0) | |
6336 | { | |
c4100eae | 6337 | gfc_error ("Expected label %qs for %s statement at %C", |
3070bab4 JW |
6338 | gfc_current_block ()->ns->proc_name->name, |
6339 | gfc_ascii_statement (*st)); | |
6340 | goto cleanup; | |
6341 | } | |
6de9cd9a DN |
6342 | |
6343 | if (gfc_match_eos () == MATCH_YES) | |
6344 | return MATCH_YES; | |
6345 | ||
6346 | syntax: | |
6347 | gfc_syntax_error (*st); | |
6348 | ||
6349 | cleanup: | |
63645982 | 6350 | gfc_current_locus = old_loc; |
0cab6b73 TK |
6351 | |
6352 | /* If we are missing an END BLOCK, we created a half-ready namespace. | |
6353 | Remove it from the parent namespace's sibling list. */ | |
6354 | ||
6355 | if (state == COMP_BLOCK) | |
6356 | { | |
6357 | parent_ns = gfc_current_ns->parent; | |
6358 | ||
6359 | nsp = &(gfc_state_stack->previous->tail->ext.block.ns); | |
6360 | ||
6361 | prev_ns = NULL; | |
6362 | ns = *nsp; | |
6363 | while (ns) | |
6364 | { | |
6365 | if (ns == gfc_current_ns) | |
6366 | { | |
6367 | if (prev_ns == NULL) | |
6368 | *nsp = NULL; | |
6369 | else | |
6370 | prev_ns->sibling = ns->sibling; | |
6371 | } | |
6372 | prev_ns = ns; | |
6373 | ns = ns->sibling; | |
6374 | } | |
6375 | ||
6376 | gfc_free_namespace (gfc_current_ns); | |
6377 | gfc_current_ns = parent_ns; | |
6378 | } | |
6379 | ||
6de9cd9a DN |
6380 | return MATCH_ERROR; |
6381 | } | |
6382 | ||
6383 | ||
6384 | ||
6385 | /***************** Attribute declaration statements ****************/ | |
6386 | ||
6387 | /* Set the attribute of a single variable. */ | |
6388 | ||
6389 | static match | |
6390 | attr_decl1 (void) | |
6391 | { | |
6392 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6393 | gfc_array_spec *as; | |
97440db5 ML |
6394 | |
6395 | /* Workaround -Wmaybe-uninitialized false positive during | |
6396 | profiledbootstrap by initializing them. */ | |
6397 | gfc_symbol *sym = NULL; | |
6de9cd9a DN |
6398 | locus var_locus; |
6399 | match m; | |
6400 | ||
6401 | as = NULL; | |
6402 | ||
6403 | m = gfc_match_name (name); | |
6404 | if (m != MATCH_YES) | |
6405 | goto cleanup; | |
6406 | ||
08a6b8e0 | 6407 | if (find_special (name, &sym, false)) |
6de9cd9a DN |
6408 | return MATCH_ERROR; |
6409 | ||
524af0d6 | 6410 | if (!check_function_name (name)) |
bb9de0c4 JW |
6411 | { |
6412 | m = MATCH_ERROR; | |
6413 | goto cleanup; | |
6414 | } | |
f5acf0f2 | 6415 | |
63645982 | 6416 | var_locus = gfc_current_locus; |
6de9cd9a DN |
6417 | |
6418 | /* Deal with possible array specification for certain attributes. */ | |
6419 | if (current_attr.dimension | |
be59db2d | 6420 | || current_attr.codimension |
6de9cd9a DN |
6421 | || current_attr.allocatable |
6422 | || current_attr.pointer | |
6423 | || current_attr.target) | |
6424 | { | |
be59db2d TB |
6425 | m = gfc_match_array_spec (&as, !current_attr.codimension, |
6426 | !current_attr.dimension | |
6427 | && !current_attr.pointer | |
6428 | && !current_attr.target); | |
6de9cd9a DN |
6429 | if (m == MATCH_ERROR) |
6430 | goto cleanup; | |
6431 | ||
6432 | if (current_attr.dimension && m == MATCH_NO) | |
6433 | { | |
636dff67 SK |
6434 | gfc_error ("Missing array specification at %L in DIMENSION " |
6435 | "statement", &var_locus); | |
6de9cd9a DN |
6436 | m = MATCH_ERROR; |
6437 | goto cleanup; | |
6438 | } | |
6439 | ||
1283ab12 TB |
6440 | if (current_attr.dimension && sym->value) |
6441 | { | |
6442 | gfc_error ("Dimensions specified for %s at %L after its " | |
6443 | "initialisation", sym->name, &var_locus); | |
6444 | m = MATCH_ERROR; | |
6445 | goto cleanup; | |
6446 | } | |
6447 | ||
be59db2d TB |
6448 | if (current_attr.codimension && m == MATCH_NO) |
6449 | { | |
6450 | gfc_error ("Missing array specification at %L in CODIMENSION " | |
6451 | "statement", &var_locus); | |
6452 | m = MATCH_ERROR; | |
6453 | goto cleanup; | |
6454 | } | |
6455 | ||
6de9cd9a DN |
6456 | if ((current_attr.allocatable || current_attr.pointer) |
6457 | && (m == MATCH_YES) && (as->type != AS_DEFERRED)) | |
6458 | { | |
636dff67 | 6459 | gfc_error ("Array specification must be deferred at %L", &var_locus); |
6de9cd9a DN |
6460 | m = MATCH_ERROR; |
6461 | goto cleanup; | |
6462 | } | |
6463 | } | |
6464 | ||
2e23972e JW |
6465 | /* Update symbol table. DIMENSION attribute is set in |
6466 | gfc_set_array_spec(). For CLASS variables, this must be applied | |
b04533af | 6467 | to the first component, or '_data' field. */ |
d40477b4 | 6468 | if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class) |
6de9cd9a | 6469 | { |
524af0d6 | 6470 | if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, ¤t_attr, &var_locus)) |
2e23972e JW |
6471 | { |
6472 | m = MATCH_ERROR; | |
6473 | goto cleanup; | |
6474 | } | |
2e23972e JW |
6475 | } |
6476 | else | |
6477 | { | |
be59db2d | 6478 | if (current_attr.dimension == 0 && current_attr.codimension == 0 |
524af0d6 | 6479 | && !gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus)) |
2e23972e JW |
6480 | { |
6481 | m = MATCH_ERROR; | |
6482 | goto cleanup; | |
6483 | } | |
6de9cd9a | 6484 | } |
f5acf0f2 | 6485 | |
528622fd | 6486 | if (sym->ts.type == BT_CLASS |
9b6da3c7 | 6487 | && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as)) |
96d9b22c JW |
6488 | { |
6489 | m = MATCH_ERROR; | |
6490 | goto cleanup; | |
6491 | } | |
6de9cd9a | 6492 | |
524af0d6 | 6493 | if (!gfc_set_array_spec (sym, as, &var_locus)) |
6de9cd9a DN |
6494 | { |
6495 | m = MATCH_ERROR; | |
6496 | goto cleanup; | |
6497 | } | |
d51347f9 | 6498 | |
83d890b9 AL |
6499 | if (sym->attr.cray_pointee && sym->as != NULL) |
6500 | { | |
6501 | /* Fix the array spec. */ | |
f5acf0f2 | 6502 | m = gfc_mod_pointee_as (sym->as); |
83d890b9 AL |
6503 | if (m == MATCH_ERROR) |
6504 | goto cleanup; | |
6505 | } | |
6de9cd9a | 6506 | |
524af0d6 | 6507 | if (!gfc_add_attribute (&sym->attr, &var_locus)) |
1902704e PT |
6508 | { |
6509 | m = MATCH_ERROR; | |
6510 | goto cleanup; | |
6511 | } | |
6512 | ||
6de9cd9a DN |
6513 | if ((current_attr.external || current_attr.intrinsic) |
6514 | && sym->attr.flavor != FL_PROCEDURE | |
524af0d6 | 6515 | && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL)) |
6de9cd9a DN |
6516 | { |
6517 | m = MATCH_ERROR; | |
6518 | goto cleanup; | |
6519 | } | |
6520 | ||
3070bab4 JW |
6521 | add_hidden_procptr_result (sym); |
6522 | ||
6de9cd9a DN |
6523 | return MATCH_YES; |
6524 | ||
6525 | cleanup: | |
6526 | gfc_free_array_spec (as); | |
6527 | return m; | |
6528 | } | |
6529 | ||
6530 | ||
6531 | /* Generic attribute declaration subroutine. Used for attributes that | |
6532 | just have a list of names. */ | |
6533 | ||
6534 | static match | |
6535 | attr_decl (void) | |
6536 | { | |
6537 | match m; | |
6538 | ||
6539 | /* Gobble the optional double colon, by simply ignoring the result | |
6540 | of gfc_match(). */ | |
6541 | gfc_match (" ::"); | |
6542 | ||
6543 | for (;;) | |
6544 | { | |
6545 | m = attr_decl1 (); | |
6546 | if (m != MATCH_YES) | |
6547 | break; | |
6548 | ||
6549 | if (gfc_match_eos () == MATCH_YES) | |
6550 | { | |
6551 | m = MATCH_YES; | |
6552 | break; | |
6553 | } | |
6554 | ||
6555 | if (gfc_match_char (',') != MATCH_YES) | |
6556 | { | |
6557 | gfc_error ("Unexpected character in variable list at %C"); | |
6558 | m = MATCH_ERROR; | |
6559 | break; | |
6560 | } | |
6561 | } | |
6562 | ||
6563 | return m; | |
6564 | } | |
6565 | ||
6566 | ||
83d890b9 AL |
6567 | /* This routine matches Cray Pointer declarations of the form: |
6568 | pointer ( <pointer>, <pointee> ) | |
6569 | or | |
d51347f9 TB |
6570 | pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ... |
6571 | The pointer, if already declared, should be an integer. Otherwise, we | |
83d890b9 AL |
6572 | set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may |
6573 | be either a scalar, or an array declaration. No space is allocated for | |
d51347f9 | 6574 | the pointee. For the statement |
83d890b9 AL |
6575 | pointer (ipt, ar(10)) |
6576 | any subsequent uses of ar will be translated (in C-notation) as | |
d51347f9 | 6577 | ar(i) => ((<type> *) ipt)(i) |
b122dc6a | 6578 | After gimplification, pointee variable will disappear in the code. */ |
83d890b9 AL |
6579 | |
6580 | static match | |
6581 | cray_pointer_decl (void) | |
6582 | { | |
6583 | match m; | |
be59db2d | 6584 | gfc_array_spec *as = NULL; |
83d890b9 AL |
6585 | gfc_symbol *cptr; /* Pointer symbol. */ |
6586 | gfc_symbol *cpte; /* Pointee symbol. */ | |
6587 | locus var_locus; | |
6588 | bool done = false; | |
6589 | ||
6590 | while (!done) | |
6591 | { | |
6592 | if (gfc_match_char ('(') != MATCH_YES) | |
6593 | { | |
a4d9b221 | 6594 | gfc_error ("Expected %<(%> at %C"); |
d51347f9 | 6595 | return MATCH_ERROR; |
83d890b9 | 6596 | } |
d51347f9 | 6597 | |
83d890b9 AL |
6598 | /* Match pointer. */ |
6599 | var_locus = gfc_current_locus; | |
6600 | gfc_clear_attr (¤t_attr); | |
6601 | gfc_add_cray_pointer (¤t_attr, &var_locus); | |
6602 | current_ts.type = BT_INTEGER; | |
6603 | current_ts.kind = gfc_index_integer_kind; | |
6604 | ||
d51347f9 | 6605 | m = gfc_match_symbol (&cptr, 0); |
83d890b9 AL |
6606 | if (m != MATCH_YES) |
6607 | { | |
6608 | gfc_error ("Expected variable name at %C"); | |
6609 | return m; | |
6610 | } | |
d51347f9 | 6611 | |
524af0d6 | 6612 | if (!gfc_add_cray_pointer (&cptr->attr, &var_locus)) |
83d890b9 AL |
6613 | return MATCH_ERROR; |
6614 | ||
d51347f9 | 6615 | gfc_set_sym_referenced (cptr); |
83d890b9 AL |
6616 | |
6617 | if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */ | |
6618 | { | |
6619 | cptr->ts.type = BT_INTEGER; | |
d51347f9 | 6620 | cptr->ts.kind = gfc_index_integer_kind; |
83d890b9 AL |
6621 | } |
6622 | else if (cptr->ts.type != BT_INTEGER) | |
6623 | { | |
e25a0da3 | 6624 | gfc_error ("Cray pointer at %C must be an integer"); |
83d890b9 AL |
6625 | return MATCH_ERROR; |
6626 | } | |
6627 | else if (cptr->ts.kind < gfc_index_integer_kind) | |
6628 | gfc_warning ("Cray pointer at %C has %d bytes of precision;" | |
e25a0da3 | 6629 | " memory addresses require %d bytes", |
636dff67 | 6630 | cptr->ts.kind, gfc_index_integer_kind); |
83d890b9 AL |
6631 | |
6632 | if (gfc_match_char (',') != MATCH_YES) | |
6633 | { | |
6634 | gfc_error ("Expected \",\" at %C"); | |
d51347f9 | 6635 | return MATCH_ERROR; |
83d890b9 AL |
6636 | } |
6637 | ||
d51347f9 | 6638 | /* Match Pointee. */ |
83d890b9 AL |
6639 | var_locus = gfc_current_locus; |
6640 | gfc_clear_attr (¤t_attr); | |
6641 | gfc_add_cray_pointee (¤t_attr, &var_locus); | |
6642 | current_ts.type = BT_UNKNOWN; | |
6643 | current_ts.kind = 0; | |
6644 | ||
6645 | m = gfc_match_symbol (&cpte, 0); | |
6646 | if (m != MATCH_YES) | |
6647 | { | |
6648 | gfc_error ("Expected variable name at %C"); | |
6649 | return m; | |
6650 | } | |
d51347f9 | 6651 | |
83d890b9 | 6652 | /* Check for an optional array spec. */ |
be59db2d | 6653 | m = gfc_match_array_spec (&as, true, false); |
83d890b9 AL |
6654 | if (m == MATCH_ERROR) |
6655 | { | |
6656 | gfc_free_array_spec (as); | |
6657 | return m; | |
6658 | } | |
6659 | else if (m == MATCH_NO) | |
6660 | { | |
6661 | gfc_free_array_spec (as); | |
6662 | as = NULL; | |
f5acf0f2 | 6663 | } |
83d890b9 | 6664 | |
524af0d6 | 6665 | if (!gfc_add_cray_pointee (&cpte->attr, &var_locus)) |
83d890b9 AL |
6666 | return MATCH_ERROR; |
6667 | ||
6668 | gfc_set_sym_referenced (cpte); | |
6669 | ||
6670 | if (cpte->as == NULL) | |
6671 | { | |
524af0d6 | 6672 | if (!gfc_set_array_spec (cpte, as, &var_locus)) |
83d890b9 AL |
6673 | gfc_internal_error ("Couldn't set Cray pointee array spec."); |
6674 | } | |
6675 | else if (as != NULL) | |
6676 | { | |
e25a0da3 | 6677 | gfc_error ("Duplicate array spec for Cray pointee at %C"); |
83d890b9 AL |
6678 | gfc_free_array_spec (as); |
6679 | return MATCH_ERROR; | |
6680 | } | |
f5acf0f2 | 6681 | |
83d890b9 | 6682 | as = NULL; |
f5acf0f2 | 6683 | |
83d890b9 AL |
6684 | if (cpte->as != NULL) |
6685 | { | |
6686 | /* Fix array spec. */ | |
6687 | m = gfc_mod_pointee_as (cpte->as); | |
6688 | if (m == MATCH_ERROR) | |
6689 | return m; | |
f5acf0f2 PT |
6690 | } |
6691 | ||
83d890b9 | 6692 | /* Point the Pointee at the Pointer. */ |
b122dc6a | 6693 | cpte->cp_pointer = cptr; |
83d890b9 AL |
6694 | |
6695 | if (gfc_match_char (')') != MATCH_YES) | |
6696 | { | |
6697 | gfc_error ("Expected \")\" at %C"); | |
f5acf0f2 | 6698 | return MATCH_ERROR; |
83d890b9 AL |
6699 | } |
6700 | m = gfc_match_char (','); | |
6701 | if (m != MATCH_YES) | |
6702 | done = true; /* Stop searching for more declarations. */ | |
6703 | ||
6704 | } | |
f5acf0f2 | 6705 | |
83d890b9 AL |
6706 | if (m == MATCH_ERROR /* Failed when trying to find ',' above. */ |
6707 | || gfc_match_eos () != MATCH_YES) | |
6708 | { | |
a4d9b221 | 6709 | gfc_error ("Expected %<,%> or end of statement at %C"); |
83d890b9 AL |
6710 | return MATCH_ERROR; |
6711 | } | |
6712 | return MATCH_YES; | |
6713 | } | |
6714 | ||
6715 | ||
6de9cd9a DN |
6716 | match |
6717 | gfc_match_external (void) | |
6718 | { | |
6719 | ||
6720 | gfc_clear_attr (¤t_attr); | |
1902704e | 6721 | current_attr.external = 1; |
6de9cd9a DN |
6722 | |
6723 | return attr_decl (); | |
6724 | } | |
6725 | ||
6726 | ||
6de9cd9a DN |
6727 | match |
6728 | gfc_match_intent (void) | |
6729 | { | |
6730 | sym_intent intent; | |
6731 | ||
9abe5e56 DK |
6732 | /* This is not allowed within a BLOCK construct! */ |
6733 | if (gfc_current_state () == COMP_BLOCK) | |
6734 | { | |
6735 | gfc_error ("INTENT is not allowed inside of BLOCK at %C"); | |
6736 | return MATCH_ERROR; | |
6737 | } | |
6738 | ||
6de9cd9a DN |
6739 | intent = match_intent_spec (); |
6740 | if (intent == INTENT_UNKNOWN) | |
6741 | return MATCH_ERROR; | |
6742 | ||
6743 | gfc_clear_attr (¤t_attr); | |
1902704e | 6744 | current_attr.intent = intent; |
6de9cd9a DN |
6745 | |
6746 | return attr_decl (); | |
6747 | } | |
6748 | ||
6749 | ||
6750 | match | |
6751 | gfc_match_intrinsic (void) | |
6752 | { | |
6753 | ||
6754 | gfc_clear_attr (¤t_attr); | |
1902704e | 6755 | current_attr.intrinsic = 1; |
6de9cd9a DN |
6756 | |
6757 | return attr_decl (); | |
6758 | } | |
6759 | ||
6760 | ||
6761 | match | |
6762 | gfc_match_optional (void) | |
6763 | { | |
9abe5e56 DK |
6764 | /* This is not allowed within a BLOCK construct! */ |
6765 | if (gfc_current_state () == COMP_BLOCK) | |
6766 | { | |
6767 | gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C"); | |
6768 | return MATCH_ERROR; | |
6769 | } | |
6de9cd9a DN |
6770 | |
6771 | gfc_clear_attr (¤t_attr); | |
1902704e | 6772 | current_attr.optional = 1; |
6de9cd9a DN |
6773 | |
6774 | return attr_decl (); | |
6775 | } | |
6776 | ||
6777 | ||
6778 | match | |
6779 | gfc_match_pointer (void) | |
6780 | { | |
83d890b9 | 6781 | gfc_gobble_whitespace (); |
8fc541d3 | 6782 | if (gfc_peek_ascii_char () == '(') |
83d890b9 | 6783 | { |
c61819ff | 6784 | if (!flag_cray_pointer) |
83d890b9 | 6785 | { |
636dff67 SK |
6786 | gfc_error ("Cray pointer declaration at %C requires -fcray-pointer " |
6787 | "flag"); | |
83d890b9 AL |
6788 | return MATCH_ERROR; |
6789 | } | |
6790 | return cray_pointer_decl (); | |
6791 | } | |
6792 | else | |
6793 | { | |
6794 | gfc_clear_attr (¤t_attr); | |
1902704e | 6795 | current_attr.pointer = 1; |
f5acf0f2 | 6796 | |
83d890b9 AL |
6797 | return attr_decl (); |
6798 | } | |
6de9cd9a DN |
6799 | } |
6800 | ||
6801 | ||
6802 | match | |
6803 | gfc_match_allocatable (void) | |
6804 | { | |
6de9cd9a | 6805 | gfc_clear_attr (¤t_attr); |
1902704e | 6806 | current_attr.allocatable = 1; |
6de9cd9a DN |
6807 | |
6808 | return attr_decl (); | |
6809 | } | |
6810 | ||
6811 | ||
be59db2d TB |
6812 | match |
6813 | gfc_match_codimension (void) | |
6814 | { | |
6815 | gfc_clear_attr (¤t_attr); | |
6816 | current_attr.codimension = 1; | |
6817 | ||
6818 | return attr_decl (); | |
6819 | } | |
6820 | ||
6821 | ||
fe4e525c TB |
6822 | match |
6823 | gfc_match_contiguous (void) | |
6824 | { | |
524af0d6 | 6825 | if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C")) |
fe4e525c TB |
6826 | return MATCH_ERROR; |
6827 | ||
6828 | gfc_clear_attr (¤t_attr); | |
6829 | current_attr.contiguous = 1; | |
6830 | ||
6831 | return attr_decl (); | |
6832 | } | |
6833 | ||
6834 | ||
6de9cd9a DN |
6835 | match |
6836 | gfc_match_dimension (void) | |
6837 | { | |
6de9cd9a | 6838 | gfc_clear_attr (¤t_attr); |
1902704e | 6839 | current_attr.dimension = 1; |
6de9cd9a DN |
6840 | |
6841 | return attr_decl (); | |
6842 | } | |
6843 | ||
6844 | ||
6845 | match | |
6846 | gfc_match_target (void) | |
6847 | { | |
6de9cd9a | 6848 | gfc_clear_attr (¤t_attr); |
1902704e | 6849 | current_attr.target = 1; |
6de9cd9a DN |
6850 | |
6851 | return attr_decl (); | |
6852 | } | |
6853 | ||
6854 | ||
6855 | /* Match the list of entities being specified in a PUBLIC or PRIVATE | |
6856 | statement. */ | |
6857 | ||
6858 | static match | |
6859 | access_attr_decl (gfc_statement st) | |
6860 | { | |
6861 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6862 | interface_type type; | |
6863 | gfc_user_op *uop; | |
c3f34952 | 6864 | gfc_symbol *sym, *dt_sym; |
a1ee985f | 6865 | gfc_intrinsic_op op; |
6de9cd9a DN |
6866 | match m; |
6867 | ||
6868 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
6869 | goto done; | |
6870 | ||
6871 | for (;;) | |
6872 | { | |
a1ee985f | 6873 | m = gfc_match_generic_spec (&type, name, &op); |
6de9cd9a DN |
6874 | if (m == MATCH_NO) |
6875 | goto syntax; | |
6876 | if (m == MATCH_ERROR) | |
6877 | return MATCH_ERROR; | |
6878 | ||
6879 | switch (type) | |
6880 | { | |
6881 | case INTERFACE_NAMELESS: | |
9e1d712c | 6882 | case INTERFACE_ABSTRACT: |
6de9cd9a DN |
6883 | goto syntax; |
6884 | ||
6885 | case INTERFACE_GENERIC: | |
6886 | if (gfc_get_symbol (name, NULL, &sym)) | |
6887 | goto done; | |
6888 | ||
524af0d6 JB |
6889 | if (!gfc_add_access (&sym->attr, |
6890 | (st == ST_PUBLIC) | |
6891 | ? ACCESS_PUBLIC : ACCESS_PRIVATE, | |
6892 | sym->name, NULL)) | |
6de9cd9a DN |
6893 | return MATCH_ERROR; |
6894 | ||
c3f34952 | 6895 | if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym)) |
524af0d6 JB |
6896 | && !gfc_add_access (&dt_sym->attr, |
6897 | (st == ST_PUBLIC) | |
6898 | ? ACCESS_PUBLIC : ACCESS_PRIVATE, | |
6899 | sym->name, NULL)) | |
c3f34952 TB |
6900 | return MATCH_ERROR; |
6901 | ||
6de9cd9a DN |
6902 | break; |
6903 | ||
6904 | case INTERFACE_INTRINSIC_OP: | |
a1ee985f | 6905 | if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN) |
6de9cd9a | 6906 | { |
fb03a37e TK |
6907 | gfc_intrinsic_op other_op; |
6908 | ||
a1ee985f | 6909 | gfc_current_ns->operator_access[op] = |
6de9cd9a | 6910 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; |
fb03a37e TK |
6911 | |
6912 | /* Handle the case if there is another op with the same | |
6913 | function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */ | |
6914 | other_op = gfc_equivalent_op (op); | |
6915 | ||
6916 | if (other_op != INTRINSIC_NONE) | |
6917 | gfc_current_ns->operator_access[other_op] = | |
6918 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
6919 | ||
6de9cd9a DN |
6920 | } |
6921 | else | |
6922 | { | |
6923 | gfc_error ("Access specification of the %s operator at %C has " | |
a1ee985f | 6924 | "already been specified", gfc_op2string (op)); |
6de9cd9a DN |
6925 | goto done; |
6926 | } | |
6927 | ||
6928 | break; | |
6929 | ||
6930 | case INTERFACE_USER_OP: | |
6931 | uop = gfc_get_uop (name); | |
6932 | ||
6933 | if (uop->access == ACCESS_UNKNOWN) | |
6934 | { | |
636dff67 SK |
6935 | uop->access = (st == ST_PUBLIC) |
6936 | ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
6de9cd9a DN |
6937 | } |
6938 | else | |
6939 | { | |
636dff67 SK |
6940 | gfc_error ("Access specification of the .%s. operator at %C " |
6941 | "has already been specified", sym->name); | |
6de9cd9a DN |
6942 | goto done; |
6943 | } | |
6944 | ||
6945 | break; | |
6946 | } | |
6947 | ||
6948 | if (gfc_match_char (',') == MATCH_NO) | |
6949 | break; | |
6950 | } | |
6951 | ||
6952 | if (gfc_match_eos () != MATCH_YES) | |
6953 | goto syntax; | |
6954 | return MATCH_YES; | |
6955 | ||
6956 | syntax: | |
6957 | gfc_syntax_error (st); | |
6958 | ||
6959 | done: | |
6960 | return MATCH_ERROR; | |
6961 | } | |
6962 | ||
6963 | ||
ee7e677f TB |
6964 | match |
6965 | gfc_match_protected (void) | |
6966 | { | |
6967 | gfc_symbol *sym; | |
6968 | match m; | |
6969 | ||
6970 | if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE) | |
6971 | { | |
6972 | gfc_error ("PROTECTED at %C only allowed in specification " | |
6973 | "part of a module"); | |
6974 | return MATCH_ERROR; | |
6975 | ||
6976 | } | |
6977 | ||
524af0d6 | 6978 | if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C")) |
ee7e677f TB |
6979 | return MATCH_ERROR; |
6980 | ||
6981 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
6982 | { | |
6983 | return MATCH_ERROR; | |
6984 | } | |
6985 | ||
6986 | if (gfc_match_eos () == MATCH_YES) | |
6987 | goto syntax; | |
6988 | ||
6989 | for(;;) | |
6990 | { | |
6991 | m = gfc_match_symbol (&sym, 0); | |
6992 | switch (m) | |
6993 | { | |
6994 | case MATCH_YES: | |
524af0d6 | 6995 | if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)) |
ee7e677f TB |
6996 | return MATCH_ERROR; |
6997 | goto next_item; | |
6998 | ||
6999 | case MATCH_NO: | |
7000 | break; | |
7001 | ||
7002 | case MATCH_ERROR: | |
7003 | return MATCH_ERROR; | |
7004 | } | |
7005 | ||
7006 | next_item: | |
7007 | if (gfc_match_eos () == MATCH_YES) | |
7008 | break; | |
7009 | if (gfc_match_char (',') != MATCH_YES) | |
7010 | goto syntax; | |
7011 | } | |
7012 | ||
7013 | return MATCH_YES; | |
7014 | ||
7015 | syntax: | |
7016 | gfc_error ("Syntax error in PROTECTED statement at %C"); | |
7017 | return MATCH_ERROR; | |
7018 | } | |
7019 | ||
7020 | ||
86bf520d | 7021 | /* The PRIVATE statement is a bit weird in that it can be an attribute |
df2fba9e | 7022 | declaration, but also works as a standalone statement inside of a |
6de9cd9a DN |
7023 | type declaration or a module. */ |
7024 | ||
7025 | match | |
636dff67 | 7026 | gfc_match_private (gfc_statement *st) |
6de9cd9a DN |
7027 | { |
7028 | ||
7029 | if (gfc_match ("private") != MATCH_YES) | |
7030 | return MATCH_NO; | |
7031 | ||
d51347f9 | 7032 | if (gfc_current_state () != COMP_MODULE |
30b608eb DK |
7033 | && !(gfc_current_state () == COMP_DERIVED |
7034 | && gfc_state_stack->previous | |
7035 | && gfc_state_stack->previous->state == COMP_MODULE) | |
7036 | && !(gfc_current_state () == COMP_DERIVED_CONTAINS | |
7037 | && gfc_state_stack->previous && gfc_state_stack->previous->previous | |
7038 | && gfc_state_stack->previous->previous->state == COMP_MODULE)) | |
d51347f9 TB |
7039 | { |
7040 | gfc_error ("PRIVATE statement at %C is only allowed in the " | |
7041 | "specification part of a module"); | |
7042 | return MATCH_ERROR; | |
7043 | } | |
7044 | ||
6de9cd9a DN |
7045 | if (gfc_current_state () == COMP_DERIVED) |
7046 | { | |
7047 | if (gfc_match_eos () == MATCH_YES) | |
7048 | { | |
7049 | *st = ST_PRIVATE; | |
7050 | return MATCH_YES; | |
7051 | } | |
7052 | ||
7053 | gfc_syntax_error (ST_PRIVATE); | |
7054 | return MATCH_ERROR; | |
7055 | } | |
7056 | ||
7057 | if (gfc_match_eos () == MATCH_YES) | |
7058 | { | |
7059 | *st = ST_PRIVATE; | |
7060 | return MATCH_YES; | |
7061 | } | |
7062 | ||
7063 | *st = ST_ATTR_DECL; | |
7064 | return access_attr_decl (ST_PRIVATE); | |
7065 | } | |
7066 | ||
7067 | ||
7068 | match | |
636dff67 | 7069 | gfc_match_public (gfc_statement *st) |
6de9cd9a DN |
7070 | { |
7071 | ||
7072 | if (gfc_match ("public") != MATCH_YES) | |
7073 | return MATCH_NO; | |
7074 | ||
d51347f9 TB |
7075 | if (gfc_current_state () != COMP_MODULE) |
7076 | { | |
7077 | gfc_error ("PUBLIC statement at %C is only allowed in the " | |
7078 | "specification part of a module"); | |
7079 | return MATCH_ERROR; | |
7080 | } | |
7081 | ||
6de9cd9a DN |
7082 | if (gfc_match_eos () == MATCH_YES) |
7083 | { | |
7084 | *st = ST_PUBLIC; | |
7085 | return MATCH_YES; | |
7086 | } | |
7087 | ||
7088 | *st = ST_ATTR_DECL; | |
7089 | return access_attr_decl (ST_PUBLIC); | |
7090 | } | |
7091 | ||
7092 | ||
7093 | /* Workhorse for gfc_match_parameter. */ | |
7094 | ||
7095 | static match | |
7096 | do_parm (void) | |
7097 | { | |
7098 | gfc_symbol *sym; | |
7099 | gfc_expr *init; | |
7100 | match m; | |
524af0d6 | 7101 | bool t; |
6de9cd9a DN |
7102 | |
7103 | m = gfc_match_symbol (&sym, 0); | |
7104 | if (m == MATCH_NO) | |
7105 | gfc_error ("Expected variable name at %C in PARAMETER statement"); | |
7106 | ||
7107 | if (m != MATCH_YES) | |
7108 | return m; | |
7109 | ||
7110 | if (gfc_match_char ('=') == MATCH_NO) | |
7111 | { | |
7112 | gfc_error ("Expected = sign in PARAMETER statement at %C"); | |
7113 | return MATCH_ERROR; | |
7114 | } | |
7115 | ||
7116 | m = gfc_match_init_expr (&init); | |
7117 | if (m == MATCH_NO) | |
7118 | gfc_error ("Expected expression at %C in PARAMETER statement"); | |
7119 | if (m != MATCH_YES) | |
7120 | return m; | |
7121 | ||
7122 | if (sym->ts.type == BT_UNKNOWN | |
524af0d6 | 7123 | && !gfc_set_default_type (sym, 1, NULL)) |
6de9cd9a DN |
7124 | { |
7125 | m = MATCH_ERROR; | |
7126 | goto cleanup; | |
7127 | } | |
7128 | ||
524af0d6 JB |
7129 | if (!gfc_check_assign_symbol (sym, NULL, init) |
7130 | || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL)) | |
6de9cd9a DN |
7131 | { |
7132 | m = MATCH_ERROR; | |
7133 | goto cleanup; | |
7134 | } | |
7135 | ||
1283ab12 TB |
7136 | if (sym->value) |
7137 | { | |
7138 | gfc_error ("Initializing already initialized variable at %C"); | |
7139 | m = MATCH_ERROR; | |
7140 | goto cleanup; | |
7141 | } | |
7142 | ||
7919373d | 7143 | t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus); |
524af0d6 | 7144 | return (t) ? MATCH_YES : MATCH_ERROR; |
6de9cd9a DN |
7145 | |
7146 | cleanup: | |
7147 | gfc_free_expr (init); | |
7148 | return m; | |
7149 | } | |
7150 | ||
7151 | ||
7152 | /* Match a parameter statement, with the weird syntax that these have. */ | |
7153 | ||
7154 | match | |
7155 | gfc_match_parameter (void) | |
7156 | { | |
7157 | match m; | |
7158 | ||
7159 | if (gfc_match_char ('(') == MATCH_NO) | |
7160 | return MATCH_NO; | |
7161 | ||
7162 | for (;;) | |
7163 | { | |
7164 | m = do_parm (); | |
7165 | if (m != MATCH_YES) | |
7166 | break; | |
7167 | ||
7168 | if (gfc_match (" )%t") == MATCH_YES) | |
7169 | break; | |
7170 | ||
7171 | if (gfc_match_char (',') != MATCH_YES) | |
7172 | { | |
7173 | gfc_error ("Unexpected characters in PARAMETER statement at %C"); | |
7174 | m = MATCH_ERROR; | |
7175 | break; | |
7176 | } | |
7177 | } | |
7178 | ||
7179 | return m; | |
7180 | } | |
7181 | ||
7182 | ||
7183 | /* Save statements have a special syntax. */ | |
7184 | ||
7185 | match | |
7186 | gfc_match_save (void) | |
7187 | { | |
9056bd70 TS |
7188 | char n[GFC_MAX_SYMBOL_LEN+1]; |
7189 | gfc_common_head *c; | |
6de9cd9a DN |
7190 | gfc_symbol *sym; |
7191 | match m; | |
7192 | ||
7193 | if (gfc_match_eos () == MATCH_YES) | |
7194 | { | |
7195 | if (gfc_current_ns->seen_save) | |
7196 | { | |
524af0d6 JB |
7197 | if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C " |
7198 | "follows previous SAVE statement")) | |
09e87839 | 7199 | return MATCH_ERROR; |
6de9cd9a DN |
7200 | } |
7201 | ||
7202 | gfc_current_ns->save_all = gfc_current_ns->seen_save = 1; | |
7203 | return MATCH_YES; | |
7204 | } | |
7205 | ||
7206 | if (gfc_current_ns->save_all) | |
7207 | { | |
524af0d6 JB |
7208 | if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows " |
7209 | "blanket SAVE statement")) | |
09e87839 | 7210 | return MATCH_ERROR; |
6de9cd9a DN |
7211 | } |
7212 | ||
7213 | gfc_match (" ::"); | |
7214 | ||
7215 | for (;;) | |
7216 | { | |
7217 | m = gfc_match_symbol (&sym, 0); | |
7218 | switch (m) | |
7219 | { | |
7220 | case MATCH_YES: | |
524af0d6 JB |
7221 | if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, |
7222 | &gfc_current_locus)) | |
6de9cd9a DN |
7223 | return MATCH_ERROR; |
7224 | goto next_item; | |
7225 | ||
7226 | case MATCH_NO: | |
7227 | break; | |
7228 | ||
7229 | case MATCH_ERROR: | |
7230 | return MATCH_ERROR; | |
7231 | } | |
7232 | ||
9056bd70 | 7233 | m = gfc_match (" / %n /", &n); |
6de9cd9a DN |
7234 | if (m == MATCH_ERROR) |
7235 | return MATCH_ERROR; | |
7236 | if (m == MATCH_NO) | |
7237 | goto syntax; | |
7238 | ||
53814b8f | 7239 | c = gfc_get_common (n, 0); |
9056bd70 TS |
7240 | c->saved = 1; |
7241 | ||
6de9cd9a DN |
7242 | gfc_current_ns->seen_save = 1; |
7243 | ||
7244 | next_item: | |
7245 | if (gfc_match_eos () == MATCH_YES) | |
7246 | break; | |
7247 | if (gfc_match_char (',') != MATCH_YES) | |
7248 | goto syntax; | |
7249 | } | |
7250 | ||
7251 | return MATCH_YES; | |
7252 | ||
7253 | syntax: | |
7254 | gfc_error ("Syntax error in SAVE statement at %C"); | |
7255 | return MATCH_ERROR; | |
7256 | } | |
7257 | ||
7258 | ||
06469efd PT |
7259 | match |
7260 | gfc_match_value (void) | |
7261 | { | |
7262 | gfc_symbol *sym; | |
7263 | match m; | |
7264 | ||
9abe5e56 DK |
7265 | /* This is not allowed within a BLOCK construct! */ |
7266 | if (gfc_current_state () == COMP_BLOCK) | |
7267 | { | |
7268 | gfc_error ("VALUE is not allowed inside of BLOCK at %C"); | |
7269 | return MATCH_ERROR; | |
7270 | } | |
7271 | ||
524af0d6 | 7272 | if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C")) |
06469efd PT |
7273 | return MATCH_ERROR; |
7274 | ||
7275 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
7276 | { | |
7277 | return MATCH_ERROR; | |
7278 | } | |
7279 | ||
7280 | if (gfc_match_eos () == MATCH_YES) | |
7281 | goto syntax; | |
7282 | ||
7283 | for(;;) | |
7284 | { | |
7285 | m = gfc_match_symbol (&sym, 0); | |
7286 | switch (m) | |
7287 | { | |
7288 | case MATCH_YES: | |
524af0d6 | 7289 | if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)) |
06469efd PT |
7290 | return MATCH_ERROR; |
7291 | goto next_item; | |
7292 | ||
7293 | case MATCH_NO: | |
7294 | break; | |
7295 | ||
7296 | case MATCH_ERROR: | |
7297 | return MATCH_ERROR; | |
7298 | } | |
7299 | ||
7300 | next_item: | |
7301 | if (gfc_match_eos () == MATCH_YES) | |
7302 | break; | |
7303 | if (gfc_match_char (',') != MATCH_YES) | |
7304 | goto syntax; | |
7305 | } | |
7306 | ||
7307 | return MATCH_YES; | |
7308 | ||
7309 | syntax: | |
7310 | gfc_error ("Syntax error in VALUE statement at %C"); | |
7311 | return MATCH_ERROR; | |
7312 | } | |
7313 | ||
66e4ab31 | 7314 | |
775e6c3a TB |
7315 | match |
7316 | gfc_match_volatile (void) | |
7317 | { | |
7318 | gfc_symbol *sym; | |
7319 | match m; | |
7320 | ||
524af0d6 | 7321 | if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C")) |
775e6c3a TB |
7322 | return MATCH_ERROR; |
7323 | ||
7324 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
7325 | { | |
7326 | return MATCH_ERROR; | |
7327 | } | |
7328 | ||
7329 | if (gfc_match_eos () == MATCH_YES) | |
7330 | goto syntax; | |
7331 | ||
7332 | for(;;) | |
7333 | { | |
f5acf0f2 | 7334 | /* VOLATILE is special because it can be added to host-associated |
1cc0e193 | 7335 | symbols locally. Except for coarrays. */ |
9bce3c1c | 7336 | m = gfc_match_symbol (&sym, 1); |
775e6c3a TB |
7337 | switch (m) |
7338 | { | |
7339 | case MATCH_YES: | |
be59db2d TB |
7340 | /* F2008, C560+C561. VOLATILE for host-/use-associated variable or |
7341 | for variable in a BLOCK which is defined outside of the BLOCK. */ | |
7342 | if (sym->ns != gfc_current_ns && sym->attr.codimension) | |
7343 | { | |
c4100eae | 7344 | gfc_error ("Specifying VOLATILE for coarray variable %qs at " |
be59db2d TB |
7345 | "%C, which is use-/host-associated", sym->name); |
7346 | return MATCH_ERROR; | |
7347 | } | |
524af0d6 | 7348 | if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)) |
775e6c3a TB |
7349 | return MATCH_ERROR; |
7350 | goto next_item; | |
7351 | ||
7352 | case MATCH_NO: | |
7353 | break; | |
7354 | ||
7355 | case MATCH_ERROR: | |
7356 | return MATCH_ERROR; | |
7357 | } | |
7358 | ||
7359 | next_item: | |
7360 | if (gfc_match_eos () == MATCH_YES) | |
7361 | break; | |
7362 | if (gfc_match_char (',') != MATCH_YES) | |
7363 | goto syntax; | |
7364 | } | |
7365 | ||
7366 | return MATCH_YES; | |
7367 | ||
7368 | syntax: | |
7369 | gfc_error ("Syntax error in VOLATILE statement at %C"); | |
7370 | return MATCH_ERROR; | |
7371 | } | |
7372 | ||
7373 | ||
1eee5628 TB |
7374 | match |
7375 | gfc_match_asynchronous (void) | |
7376 | { | |
7377 | gfc_symbol *sym; | |
7378 | match m; | |
7379 | ||
524af0d6 | 7380 | if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C")) |
1eee5628 TB |
7381 | return MATCH_ERROR; |
7382 | ||
7383 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
7384 | { | |
7385 | return MATCH_ERROR; | |
7386 | } | |
7387 | ||
7388 | if (gfc_match_eos () == MATCH_YES) | |
7389 | goto syntax; | |
7390 | ||
7391 | for(;;) | |
7392 | { | |
f5acf0f2 | 7393 | /* ASYNCHRONOUS is special because it can be added to host-associated |
1eee5628 TB |
7394 | symbols locally. */ |
7395 | m = gfc_match_symbol (&sym, 1); | |
7396 | switch (m) | |
7397 | { | |
7398 | case MATCH_YES: | |
524af0d6 | 7399 | if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)) |
1eee5628 TB |
7400 | return MATCH_ERROR; |
7401 | goto next_item; | |
7402 | ||
7403 | case MATCH_NO: | |
7404 | break; | |
7405 | ||
7406 | case MATCH_ERROR: | |
7407 | return MATCH_ERROR; | |
7408 | } | |
7409 | ||
7410 | next_item: | |
7411 | if (gfc_match_eos () == MATCH_YES) | |
7412 | break; | |
7413 | if (gfc_match_char (',') != MATCH_YES) | |
7414 | goto syntax; | |
7415 | } | |
7416 | ||
7417 | return MATCH_YES; | |
7418 | ||
7419 | syntax: | |
7420 | gfc_error ("Syntax error in ASYNCHRONOUS statement at %C"); | |
7421 | return MATCH_ERROR; | |
7422 | } | |
7423 | ||
7424 | ||
6de9cd9a DN |
7425 | /* Match a module procedure statement. Note that we have to modify |
7426 | symbols in the parent's namespace because the current one was there | |
49de9e73 | 7427 | to receive symbols that are in an interface's formal argument list. */ |
6de9cd9a DN |
7428 | |
7429 | match | |
7430 | gfc_match_modproc (void) | |
7431 | { | |
7432 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7433 | gfc_symbol *sym; | |
7434 | match m; | |
162b5a21 | 7435 | locus old_locus; |
060fca4a | 7436 | gfc_namespace *module_ns; |
2b77e908 | 7437 | gfc_interface *old_interface_head, *interface; |
6de9cd9a DN |
7438 | |
7439 | if (gfc_state_stack->state != COMP_INTERFACE | |
7440 | || gfc_state_stack->previous == NULL | |
129d15a3 JW |
7441 | || current_interface.type == INTERFACE_NAMELESS |
7442 | || current_interface.type == INTERFACE_ABSTRACT) | |
6de9cd9a | 7443 | { |
636dff67 SK |
7444 | gfc_error ("MODULE PROCEDURE at %C must be in a generic module " |
7445 | "interface"); | |
6de9cd9a DN |
7446 | return MATCH_ERROR; |
7447 | } | |
7448 | ||
060fca4a PT |
7449 | module_ns = gfc_current_ns->parent; |
7450 | for (; module_ns; module_ns = module_ns->parent) | |
43dfd40c SK |
7451 | if (module_ns->proc_name->attr.flavor == FL_MODULE |
7452 | || module_ns->proc_name->attr.flavor == FL_PROGRAM | |
7453 | || (module_ns->proc_name->attr.flavor == FL_PROCEDURE | |
7454 | && !module_ns->proc_name->attr.contained)) | |
060fca4a PT |
7455 | break; |
7456 | ||
7457 | if (module_ns == NULL) | |
7458 | return MATCH_ERROR; | |
7459 | ||
2b77e908 FXC |
7460 | /* Store the current state of the interface. We will need it if we |
7461 | end up with a syntax error and need to recover. */ | |
7462 | old_interface_head = gfc_current_interface_head (); | |
7463 | ||
162b5a21 SK |
7464 | /* Check if the F2008 optional double colon appears. */ |
7465 | gfc_gobble_whitespace (); | |
7466 | old_locus = gfc_current_locus; | |
7467 | if (gfc_match ("::") == MATCH_YES) | |
7468 | { | |
524af0d6 JB |
7469 | if (!gfc_notify_std (GFC_STD_F2008, "double colon in " |
7470 | "MODULE PROCEDURE statement at %L", &old_locus)) | |
162b5a21 SK |
7471 | return MATCH_ERROR; |
7472 | } | |
7473 | else | |
7474 | gfc_current_locus = old_locus; | |
f5acf0f2 | 7475 | |
6de9cd9a DN |
7476 | for (;;) |
7477 | { | |
2b77e908 | 7478 | bool last = false; |
162b5a21 | 7479 | old_locus = gfc_current_locus; |
2b77e908 | 7480 | |
6de9cd9a DN |
7481 | m = gfc_match_name (name); |
7482 | if (m == MATCH_NO) | |
7483 | goto syntax; | |
7484 | if (m != MATCH_YES) | |
7485 | return MATCH_ERROR; | |
7486 | ||
2b77e908 FXC |
7487 | /* Check for syntax error before starting to add symbols to the |
7488 | current namespace. */ | |
7489 | if (gfc_match_eos () == MATCH_YES) | |
7490 | last = true; | |
162b5a21 | 7491 | |
2b77e908 FXC |
7492 | if (!last && gfc_match_char (',') != MATCH_YES) |
7493 | goto syntax; | |
7494 | ||
7495 | /* Now we're sure the syntax is valid, we process this item | |
7496 | further. */ | |
060fca4a | 7497 | if (gfc_get_symbol (name, module_ns, &sym)) |
6de9cd9a DN |
7498 | return MATCH_ERROR; |
7499 | ||
43dfd40c SK |
7500 | if (sym->attr.intrinsic) |
7501 | { | |
7502 | gfc_error ("Intrinsic procedure at %L cannot be a MODULE " | |
7503 | "PROCEDURE", &old_locus); | |
7504 | return MATCH_ERROR; | |
7505 | } | |
7506 | ||
6de9cd9a | 7507 | if (sym->attr.proc != PROC_MODULE |
524af0d6 | 7508 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) |
6de9cd9a DN |
7509 | return MATCH_ERROR; |
7510 | ||
524af0d6 | 7511 | if (!gfc_add_interface (sym)) |
6de9cd9a DN |
7512 | return MATCH_ERROR; |
7513 | ||
71f77fd7 | 7514 | sym->attr.mod_proc = 1; |
43dfd40c | 7515 | sym->declared_at = old_locus; |
71f77fd7 | 7516 | |
2b77e908 | 7517 | if (last) |
6de9cd9a | 7518 | break; |
6de9cd9a DN |
7519 | } |
7520 | ||
7521 | return MATCH_YES; | |
7522 | ||
7523 | syntax: | |
2b77e908 FXC |
7524 | /* Restore the previous state of the interface. */ |
7525 | interface = gfc_current_interface_head (); | |
7526 | gfc_set_current_interface_head (old_interface_head); | |
7527 | ||
7528 | /* Free the new interfaces. */ | |
7529 | while (interface != old_interface_head) | |
7530 | { | |
7531 | gfc_interface *i = interface->next; | |
cede9502 | 7532 | free (interface); |
2b77e908 FXC |
7533 | interface = i; |
7534 | } | |
7535 | ||
7536 | /* And issue a syntax error. */ | |
6de9cd9a DN |
7537 | gfc_syntax_error (ST_MODULE_PROC); |
7538 | return MATCH_ERROR; | |
7539 | } | |
7540 | ||
7541 | ||
7d1f1e61 | 7542 | /* Check a derived type that is being extended. */ |
42e3d759 | 7543 | |
7d1f1e61 PT |
7544 | static gfc_symbol* |
7545 | check_extended_derived_type (char *name) | |
7546 | { | |
7547 | gfc_symbol *extended; | |
7548 | ||
7549 | if (gfc_find_symbol (name, gfc_current_ns, 1, &extended)) | |
7550 | { | |
7551 | gfc_error ("Ambiguous symbol in TYPE definition at %C"); | |
7552 | return NULL; | |
7553 | } | |
7554 | ||
42e3d759 JW |
7555 | extended = gfc_find_dt_in_generic (extended); |
7556 | ||
7557 | /* F08:C428. */ | |
7d1f1e61 PT |
7558 | if (!extended) |
7559 | { | |
c4100eae | 7560 | gfc_error ("Symbol %qs at %C has not been previously defined", name); |
7d1f1e61 PT |
7561 | return NULL; |
7562 | } | |
7563 | ||
7564 | if (extended->attr.flavor != FL_DERIVED) | |
7565 | { | |
c4100eae | 7566 | gfc_error ("%qs in EXTENDS expression at %C is not a " |
7d1f1e61 PT |
7567 | "derived type", name); |
7568 | return NULL; | |
7569 | } | |
7570 | ||
7571 | if (extended->attr.is_bind_c) | |
7572 | { | |
c4100eae | 7573 | gfc_error ("%qs cannot be extended at %C because it " |
7d1f1e61 PT |
7574 | "is BIND(C)", extended->name); |
7575 | return NULL; | |
7576 | } | |
7577 | ||
7578 | if (extended->attr.sequence) | |
7579 | { | |
c4100eae | 7580 | gfc_error ("%qs cannot be extended at %C because it " |
7d1f1e61 PT |
7581 | "is a SEQUENCE type", extended->name); |
7582 | return NULL; | |
7583 | } | |
7584 | ||
7585 | return extended; | |
7586 | } | |
7587 | ||
7588 | ||
a8b3b0b6 CR |
7589 | /* Match the optional attribute specifiers for a type declaration. |
7590 | Return MATCH_ERROR if an error is encountered in one of the handled | |
7591 | attributes (public, private, bind(c)), MATCH_NO if what's found is | |
7592 | not a handled attribute, and MATCH_YES otherwise. TODO: More error | |
7593 | checking on attribute conflicts needs to be done. */ | |
6de9cd9a DN |
7594 | |
7595 | match | |
7d1f1e61 | 7596 | gfc_get_type_attr_spec (symbol_attribute *attr, char *name) |
6de9cd9a | 7597 | { |
a8b3b0b6 | 7598 | /* See if the derived type is marked as private. */ |
6de9cd9a DN |
7599 | if (gfc_match (" , private") == MATCH_YES) |
7600 | { | |
d51347f9 | 7601 | if (gfc_current_state () != COMP_MODULE) |
6de9cd9a | 7602 | { |
d51347f9 TB |
7603 | gfc_error ("Derived type at %C can only be PRIVATE in the " |
7604 | "specification part of a module"); | |
6de9cd9a DN |
7605 | return MATCH_ERROR; |
7606 | } | |
7607 | ||
524af0d6 | 7608 | if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL)) |
6de9cd9a | 7609 | return MATCH_ERROR; |
6de9cd9a | 7610 | } |
a8b3b0b6 | 7611 | else if (gfc_match (" , public") == MATCH_YES) |
6de9cd9a | 7612 | { |
d51347f9 | 7613 | if (gfc_current_state () != COMP_MODULE) |
6de9cd9a | 7614 | { |
d51347f9 TB |
7615 | gfc_error ("Derived type at %C can only be PUBLIC in the " |
7616 | "specification part of a module"); | |
6de9cd9a DN |
7617 | return MATCH_ERROR; |
7618 | } | |
7619 | ||
524af0d6 | 7620 | if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL)) |
6de9cd9a | 7621 | return MATCH_ERROR; |
6de9cd9a | 7622 | } |
52f49934 | 7623 | else if (gfc_match (" , bind ( c )") == MATCH_YES) |
a8b3b0b6 CR |
7624 | { |
7625 | /* If the type is defined to be bind(c) it then needs to make | |
7626 | sure that all fields are interoperable. This will | |
7627 | need to be a semantic check on the finished derived type. | |
7628 | See 15.2.3 (lines 9-12) of F2003 draft. */ | |
524af0d6 | 7629 | if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0)) |
a8b3b0b6 CR |
7630 | return MATCH_ERROR; |
7631 | ||
7632 | /* TODO: attr conflicts need to be checked, probably in symbol.c. */ | |
7633 | } | |
52f49934 DK |
7634 | else if (gfc_match (" , abstract") == MATCH_YES) |
7635 | { | |
524af0d6 | 7636 | if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C")) |
52f49934 DK |
7637 | return MATCH_ERROR; |
7638 | ||
524af0d6 | 7639 | if (!gfc_add_abstract (attr, &gfc_current_locus)) |
52f49934 DK |
7640 | return MATCH_ERROR; |
7641 | } | |
524af0d6 | 7642 | else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES) |
7d1f1e61 | 7643 | { |
524af0d6 | 7644 | if (!gfc_add_extension (attr, &gfc_current_locus)) |
7d1f1e61 PT |
7645 | return MATCH_ERROR; |
7646 | } | |
a8b3b0b6 CR |
7647 | else |
7648 | return MATCH_NO; | |
7649 | ||
7650 | /* If we get here, something matched. */ | |
7651 | return MATCH_YES; | |
7652 | } | |
7653 | ||
7654 | ||
7655 | /* Match the beginning of a derived type declaration. If a type name | |
7656 | was the result of a function, then it is possible to have a symbol | |
7657 | already to be known as a derived type yet have no components. */ | |
7658 | ||
7659 | match | |
7660 | gfc_match_derived_decl (void) | |
7661 | { | |
7662 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7d1f1e61 | 7663 | char parent[GFC_MAX_SYMBOL_LEN + 1]; |
a8b3b0b6 | 7664 | symbol_attribute attr; |
c3f34952 | 7665 | gfc_symbol *sym, *gensym; |
7d1f1e61 | 7666 | gfc_symbol *extended; |
a8b3b0b6 CR |
7667 | match m; |
7668 | match is_type_attr_spec = MATCH_NO; | |
e7303e85 | 7669 | bool seen_attr = false; |
c3f34952 | 7670 | gfc_interface *intr = NULL, *head; |
a8b3b0b6 CR |
7671 | |
7672 | if (gfc_current_state () == COMP_DERIVED) | |
7673 | return MATCH_NO; | |
7674 | ||
7d1f1e61 PT |
7675 | name[0] = '\0'; |
7676 | parent[0] = '\0'; | |
a8b3b0b6 | 7677 | gfc_clear_attr (&attr); |
7d1f1e61 | 7678 | extended = NULL; |
a8b3b0b6 CR |
7679 | |
7680 | do | |
7681 | { | |
7d1f1e61 | 7682 | is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent); |
a8b3b0b6 CR |
7683 | if (is_type_attr_spec == MATCH_ERROR) |
7684 | return MATCH_ERROR; | |
e7303e85 FXC |
7685 | if (is_type_attr_spec == MATCH_YES) |
7686 | seen_attr = true; | |
a8b3b0b6 | 7687 | } while (is_type_attr_spec == MATCH_YES); |
6de9cd9a | 7688 | |
63a3341a PT |
7689 | /* Deal with derived type extensions. The extension attribute has |
7690 | been added to 'attr' but now the parent type must be found and | |
7691 | checked. */ | |
7d1f1e61 PT |
7692 | if (parent[0]) |
7693 | extended = check_extended_derived_type (parent); | |
7694 | ||
7695 | if (parent[0] && !extended) | |
7696 | return MATCH_ERROR; | |
7697 | ||
e7303e85 | 7698 | if (gfc_match (" ::") != MATCH_YES && seen_attr) |
6de9cd9a DN |
7699 | { |
7700 | gfc_error ("Expected :: in TYPE definition at %C"); | |
7701 | return MATCH_ERROR; | |
7702 | } | |
7703 | ||
7704 | m = gfc_match (" %n%t", name); | |
7705 | if (m != MATCH_YES) | |
7706 | return m; | |
7707 | ||
e9c06563 TB |
7708 | /* Make sure the name is not the name of an intrinsic type. */ |
7709 | if (gfc_is_intrinsic_typename (name)) | |
6de9cd9a | 7710 | { |
c4100eae | 7711 | gfc_error ("Type name %qs at %C cannot be the same as an intrinsic " |
636dff67 | 7712 | "type", name); |
6de9cd9a DN |
7713 | return MATCH_ERROR; |
7714 | } | |
7715 | ||
c3f34952 | 7716 | if (gfc_get_symbol (name, NULL, &gensym)) |
6de9cd9a DN |
7717 | return MATCH_ERROR; |
7718 | ||
c3f34952 | 7719 | if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN) |
6de9cd9a | 7720 | { |
c4100eae | 7721 | gfc_error ("Derived type name %qs at %C already has a basic type " |
c3f34952 TB |
7722 | "of %s", gensym->name, gfc_typename (&gensym->ts)); |
7723 | return MATCH_ERROR; | |
7724 | } | |
7725 | ||
7726 | if (!gensym->attr.generic | |
524af0d6 | 7727 | && !gfc_add_generic (&gensym->attr, gensym->name, NULL)) |
c3f34952 TB |
7728 | return MATCH_ERROR; |
7729 | ||
7730 | if (!gensym->attr.function | |
524af0d6 | 7731 | && !gfc_add_function (&gensym->attr, gensym->name, NULL)) |
c3f34952 TB |
7732 | return MATCH_ERROR; |
7733 | ||
7734 | sym = gfc_find_dt_in_generic (gensym); | |
7735 | ||
7736 | if (sym && (sym->components != NULL || sym->attr.zero_comp)) | |
7737 | { | |
c4100eae | 7738 | gfc_error ("Derived type definition of %qs at %C has already been " |
c3f34952 | 7739 | "defined", sym->name); |
6de9cd9a DN |
7740 | return MATCH_ERROR; |
7741 | } | |
7742 | ||
c3f34952 TB |
7743 | if (!sym) |
7744 | { | |
7745 | /* Use upper case to save the actual derived-type symbol. */ | |
7746 | gfc_get_symbol (gfc_get_string ("%c%s", | |
7747 | (char) TOUPPER ((unsigned char) gensym->name[0]), | |
7748 | &gensym->name[1]), NULL, &sym); | |
7749 | sym->name = gfc_get_string (gensym->name); | |
7750 | head = gensym->generic; | |
7751 | intr = gfc_get_interface (); | |
7752 | intr->sym = sym; | |
7753 | intr->where = gfc_current_locus; | |
7754 | intr->sym->declared_at = gfc_current_locus; | |
7755 | intr->next = head; | |
7756 | gensym->generic = intr; | |
7757 | gensym->attr.if_source = IFSRC_DECL; | |
7758 | } | |
7759 | ||
6de9cd9a DN |
7760 | /* The symbol may already have the derived attribute without the |
7761 | components. The ways this can happen is via a function | |
7762 | definition, an INTRINSIC statement or a subtype in another | |
7763 | derived type that is a pointer. The first part of the AND clause | |
df2fba9e | 7764 | is true if the symbol is not the return value of a function. */ |
6de9cd9a | 7765 | if (sym->attr.flavor != FL_DERIVED |
524af0d6 | 7766 | && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL)) |
6de9cd9a DN |
7767 | return MATCH_ERROR; |
7768 | ||
6de9cd9a | 7769 | if (attr.access != ACCESS_UNKNOWN |
524af0d6 | 7770 | && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL)) |
6de9cd9a | 7771 | return MATCH_ERROR; |
c3f34952 TB |
7772 | else if (sym->attr.access == ACCESS_UNKNOWN |
7773 | && gensym->attr.access != ACCESS_UNKNOWN | |
524af0d6 JB |
7774 | && !gfc_add_access (&sym->attr, gensym->attr.access, |
7775 | sym->name, NULL)) | |
c3f34952 TB |
7776 | return MATCH_ERROR; |
7777 | ||
7778 | if (sym->attr.access != ACCESS_UNKNOWN | |
7779 | && gensym->attr.access == ACCESS_UNKNOWN) | |
7780 | gensym->attr.access = sym->attr.access; | |
6de9cd9a | 7781 | |
a8b3b0b6 CR |
7782 | /* See if the derived type was labeled as bind(c). */ |
7783 | if (attr.is_bind_c != 0) | |
7784 | sym->attr.is_bind_c = attr.is_bind_c; | |
7785 | ||
34523524 DK |
7786 | /* Construct the f2k_derived namespace if it is not yet there. */ |
7787 | if (!sym->f2k_derived) | |
7788 | sym->f2k_derived = gfc_get_namespace (NULL, 0); | |
f5acf0f2 | 7789 | |
7d1f1e61 PT |
7790 | if (extended && !sym->components) |
7791 | { | |
7792 | gfc_component *p; | |
7d1f1e61 PT |
7793 | |
7794 | /* Add the extended derived type as the first component. */ | |
7795 | gfc_add_component (sym, parent, &p); | |
7d1f1e61 PT |
7796 | extended->refs++; |
7797 | gfc_set_sym_referenced (extended); | |
7798 | ||
7799 | p->ts.type = BT_DERIVED; | |
bc21d315 | 7800 | p->ts.u.derived = extended; |
7d1f1e61 | 7801 | p->initializer = gfc_default_initializer (&p->ts); |
f5acf0f2 | 7802 | |
7c1dab0d JW |
7803 | /* Set extension level. */ |
7804 | if (extended->attr.extension == 255) | |
7805 | { | |
7806 | /* Since the extension field is 8 bit wide, we can only have | |
7807 | up to 255 extension levels. */ | |
c4100eae | 7808 | gfc_error ("Maximum extension level reached with type %qs at %L", |
7c1dab0d JW |
7809 | extended->name, &extended->declared_at); |
7810 | return MATCH_ERROR; | |
7811 | } | |
7812 | sym->attr.extension = extended->attr.extension + 1; | |
7d1f1e61 PT |
7813 | |
7814 | /* Provide the links between the extended type and its extension. */ | |
7815 | if (!extended->f2k_derived) | |
7816 | extended->f2k_derived = gfc_get_namespace (NULL, 0); | |
7d1f1e61 PT |
7817 | } |
7818 | ||
7c1dab0d JW |
7819 | if (!sym->hash_value) |
7820 | /* Set the hash for the compound name for this type. */ | |
4fa02692 | 7821 | sym->hash_value = gfc_hash_value (sym); |
cf2b3c22 | 7822 | |
52f49934 DK |
7823 | /* Take over the ABSTRACT attribute. */ |
7824 | sym->attr.abstract = attr.abstract; | |
7825 | ||
6de9cd9a DN |
7826 | gfc_new_block = sym; |
7827 | ||
7828 | return MATCH_YES; | |
7829 | } | |
83d890b9 AL |
7830 | |
7831 | ||
f5acf0f2 | 7832 | /* Cray Pointees can be declared as: |
b3aefde2 | 7833 | pointer (ipt, a (n,m,...,*)) */ |
83d890b9 | 7834 | |
32e8bb8e | 7835 | match |
83d890b9 AL |
7836 | gfc_mod_pointee_as (gfc_array_spec *as) |
7837 | { | |
7838 | as->cray_pointee = true; /* This will be useful to know later. */ | |
7839 | if (as->type == AS_ASSUMED_SIZE) | |
b3aefde2 | 7840 | as->cp_was_assumed = true; |
83d890b9 AL |
7841 | else if (as->type == AS_ASSUMED_SHAPE) |
7842 | { | |
7843 | gfc_error ("Cray Pointee at %C cannot be assumed shape array"); | |
7844 | return MATCH_ERROR; | |
7845 | } | |
7846 | return MATCH_YES; | |
7847 | } | |
25d8f0a2 TS |
7848 | |
7849 | ||
f5acf0f2 PT |
7850 | /* Match the enum definition statement, here we are trying to match |
7851 | the first line of enum definition statement. | |
25d8f0a2 TS |
7852 | Returns MATCH_YES if match is found. */ |
7853 | ||
7854 | match | |
7855 | gfc_match_enum (void) | |
7856 | { | |
7857 | match m; | |
f5acf0f2 | 7858 | |
25d8f0a2 TS |
7859 | m = gfc_match_eos (); |
7860 | if (m != MATCH_YES) | |
7861 | return m; | |
7862 | ||
524af0d6 | 7863 | if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C")) |
25d8f0a2 TS |
7864 | return MATCH_ERROR; |
7865 | ||
7866 | return MATCH_YES; | |
7867 | } | |
7868 | ||
7869 | ||
31224396 SK |
7870 | /* Returns an initializer whose value is one higher than the value of the |
7871 | LAST_INITIALIZER argument. If the argument is NULL, the | |
7872 | initializers value will be set to zero. The initializer's kind | |
7873 | will be set to gfc_c_int_kind. | |
7874 | ||
7875 | If -fshort-enums is given, the appropriate kind will be selected | |
7876 | later after all enumerators have been parsed. A warning is issued | |
7877 | here if an initializer exceeds gfc_c_int_kind. */ | |
7878 | ||
7879 | static gfc_expr * | |
7880 | enum_initializer (gfc_expr *last_initializer, locus where) | |
7881 | { | |
7882 | gfc_expr *result; | |
b7e75771 | 7883 | result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where); |
31224396 SK |
7884 | |
7885 | mpz_init (result->value.integer); | |
7886 | ||
7887 | if (last_initializer != NULL) | |
7888 | { | |
7889 | mpz_add_ui (result->value.integer, last_initializer->value.integer, 1); | |
7890 | result->where = last_initializer->where; | |
7891 | ||
7892 | if (gfc_check_integer_range (result->value.integer, | |
7893 | gfc_c_int_kind) != ARITH_OK) | |
7894 | { | |
7895 | gfc_error ("Enumerator exceeds the C integer type at %C"); | |
7896 | return NULL; | |
7897 | } | |
7898 | } | |
7899 | else | |
7900 | { | |
7901 | /* Control comes here, if it's the very first enumerator and no | |
7902 | initializer has been given. It will be initialized to zero. */ | |
7903 | mpz_set_si (result->value.integer, 0); | |
7904 | } | |
7905 | ||
7906 | return result; | |
7907 | } | |
7908 | ||
7909 | ||
6133c68a TS |
7910 | /* Match a variable name with an optional initializer. When this |
7911 | subroutine is called, a variable is expected to be parsed next. | |
7912 | Depending on what is happening at the moment, updates either the | |
7913 | symbol table or the current interface. */ | |
7914 | ||
7915 | static match | |
7916 | enumerator_decl (void) | |
7917 | { | |
7918 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7919 | gfc_expr *initializer; | |
7920 | gfc_array_spec *as = NULL; | |
7921 | gfc_symbol *sym; | |
7922 | locus var_locus; | |
7923 | match m; | |
524af0d6 | 7924 | bool t; |
6133c68a TS |
7925 | locus old_locus; |
7926 | ||
7927 | initializer = NULL; | |
7928 | old_locus = gfc_current_locus; | |
7929 | ||
7930 | /* When we get here, we've just matched a list of attributes and | |
7931 | maybe a type and a double colon. The next thing we expect to see | |
7932 | is the name of the symbol. */ | |
7933 | m = gfc_match_name (name); | |
7934 | if (m != MATCH_YES) | |
7935 | goto cleanup; | |
7936 | ||
7937 | var_locus = gfc_current_locus; | |
7938 | ||
7939 | /* OK, we've successfully matched the declaration. Now put the | |
7940 | symbol in the current namespace. If we fail to create the symbol, | |
7941 | bail out. */ | |
524af0d6 | 7942 | if (!build_sym (name, NULL, false, &as, &var_locus)) |
6133c68a TS |
7943 | { |
7944 | m = MATCH_ERROR; | |
7945 | goto cleanup; | |
7946 | } | |
7947 | ||
7948 | /* The double colon must be present in order to have initializers. | |
7949 | Otherwise the statement is ambiguous with an assignment statement. */ | |
7950 | if (colon_seen) | |
7951 | { | |
7952 | if (gfc_match_char ('=') == MATCH_YES) | |
7953 | { | |
7954 | m = gfc_match_init_expr (&initializer); | |
7955 | if (m == MATCH_NO) | |
7956 | { | |
7957 | gfc_error ("Expected an initialization expression at %C"); | |
7958 | m = MATCH_ERROR; | |
7959 | } | |
7960 | ||
7961 | if (m != MATCH_YES) | |
7962 | goto cleanup; | |
7963 | } | |
7964 | } | |
7965 | ||
7966 | /* If we do not have an initializer, the initialization value of the | |
7967 | previous enumerator (stored in last_initializer) is incremented | |
7968 | by 1 and is used to initialize the current enumerator. */ | |
7969 | if (initializer == NULL) | |
31224396 | 7970 | initializer = enum_initializer (last_initializer, old_locus); |
d51347f9 | 7971 | |
6133c68a TS |
7972 | if (initializer == NULL || initializer->ts.type != BT_INTEGER) |
7973 | { | |
01e64c3d JJ |
7974 | gfc_error ("ENUMERATOR %L not initialized with integer expression", |
7975 | &var_locus); | |
d51347f9 | 7976 | m = MATCH_ERROR; |
6133c68a TS |
7977 | goto cleanup; |
7978 | } | |
7979 | ||
7980 | /* Store this current initializer, for the next enumerator variable | |
7981 | to be parsed. add_init_expr_to_sym() zeros initializer, so we | |
7982 | use last_initializer below. */ | |
7983 | last_initializer = initializer; | |
7984 | t = add_init_expr_to_sym (name, &initializer, &var_locus); | |
7985 | ||
7986 | /* Maintain enumerator history. */ | |
7987 | gfc_find_symbol (name, NULL, 0, &sym); | |
7988 | create_enum_history (sym, last_initializer); | |
7989 | ||
524af0d6 | 7990 | return (t) ? MATCH_YES : MATCH_ERROR; |
6133c68a TS |
7991 | |
7992 | cleanup: | |
7993 | /* Free stuff up and return. */ | |
7994 | gfc_free_expr (initializer); | |
7995 | ||
7996 | return m; | |
7997 | } | |
7998 | ||
7999 | ||
66e4ab31 | 8000 | /* Match the enumerator definition statement. */ |
25d8f0a2 TS |
8001 | |
8002 | match | |
8003 | gfc_match_enumerator_def (void) | |
8004 | { | |
8005 | match m; | |
524af0d6 | 8006 | bool t; |
d51347f9 | 8007 | |
25d8f0a2 | 8008 | gfc_clear_ts (¤t_ts); |
d51347f9 | 8009 | |
25d8f0a2 TS |
8010 | m = gfc_match (" enumerator"); |
8011 | if (m != MATCH_YES) | |
8012 | return m; | |
6133c68a TS |
8013 | |
8014 | m = gfc_match (" :: "); | |
8015 | if (m == MATCH_ERROR) | |
8016 | return m; | |
8017 | ||
8018 | colon_seen = (m == MATCH_YES); | |
d51347f9 | 8019 | |
25d8f0a2 TS |
8020 | if (gfc_current_state () != COMP_ENUM) |
8021 | { | |
8022 | gfc_error ("ENUM definition statement expected before %C"); | |
8023 | gfc_free_enum_history (); | |
8024 | return MATCH_ERROR; | |
8025 | } | |
8026 | ||
8027 | (¤t_ts)->type = BT_INTEGER; | |
8028 | (¤t_ts)->kind = gfc_c_int_kind; | |
d51347f9 | 8029 | |
6133c68a TS |
8030 | gfc_clear_attr (¤t_attr); |
8031 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL); | |
524af0d6 | 8032 | if (!t) |
25d8f0a2 | 8033 | { |
6133c68a | 8034 | m = MATCH_ERROR; |
25d8f0a2 TS |
8035 | goto cleanup; |
8036 | } | |
8037 | ||
25d8f0a2 TS |
8038 | for (;;) |
8039 | { | |
6133c68a | 8040 | m = enumerator_decl (); |
25d8f0a2 | 8041 | if (m == MATCH_ERROR) |
01e64c3d JJ |
8042 | { |
8043 | gfc_free_enum_history (); | |
8044 | goto cleanup; | |
8045 | } | |
25d8f0a2 TS |
8046 | if (m == MATCH_NO) |
8047 | break; | |
8048 | ||
8049 | if (gfc_match_eos () == MATCH_YES) | |
8050 | goto cleanup; | |
8051 | if (gfc_match_char (',') != MATCH_YES) | |
8052 | break; | |
8053 | } | |
8054 | ||
8055 | if (gfc_current_state () == COMP_ENUM) | |
8056 | { | |
8057 | gfc_free_enum_history (); | |
8058 | gfc_error ("Syntax error in ENUMERATOR definition at %C"); | |
8059 | m = MATCH_ERROR; | |
8060 | } | |
8061 | ||
8062 | cleanup: | |
8063 | gfc_free_array_spec (current_as); | |
8064 | current_as = NULL; | |
8065 | return m; | |
8066 | ||
8067 | } | |
8068 | ||
f6fad28e | 8069 | |
30b608eb DK |
8070 | /* Match binding attributes. */ |
8071 | ||
8072 | static match | |
713485cc | 8073 | match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc) |
30b608eb DK |
8074 | { |
8075 | bool found_passing = false; | |
713485cc | 8076 | bool seen_ptr = false; |
90661f26 | 8077 | match m = MATCH_YES; |
30b608eb | 8078 | |
eea58adb | 8079 | /* Initialize to defaults. Do so even before the MATCH_NO check so that in |
30b608eb DK |
8080 | this case the defaults are in there. */ |
8081 | ba->access = ACCESS_UNKNOWN; | |
8082 | ba->pass_arg = NULL; | |
8083 | ba->pass_arg_num = 0; | |
8084 | ba->nopass = 0; | |
8085 | ba->non_overridable = 0; | |
b0e5fa94 | 8086 | ba->deferred = 0; |
90661f26 | 8087 | ba->ppc = ppc; |
30b608eb DK |
8088 | |
8089 | /* If we find a comma, we believe there are binding attributes. */ | |
90661f26 JW |
8090 | m = gfc_match_char (','); |
8091 | if (m == MATCH_NO) | |
8092 | goto done; | |
30b608eb DK |
8093 | |
8094 | do | |
8095 | { | |
e157f736 DK |
8096 | /* Access specifier. */ |
8097 | ||
8098 | m = gfc_match (" public"); | |
30b608eb DK |
8099 | if (m == MATCH_ERROR) |
8100 | goto error; | |
8101 | if (m == MATCH_YES) | |
8102 | { | |
e157f736 | 8103 | if (ba->access != ACCESS_UNKNOWN) |
30b608eb | 8104 | { |
e157f736 | 8105 | gfc_error ("Duplicate access-specifier at %C"); |
30b608eb DK |
8106 | goto error; |
8107 | } | |
8108 | ||
e157f736 | 8109 | ba->access = ACCESS_PUBLIC; |
30b608eb DK |
8110 | continue; |
8111 | } | |
8112 | ||
e157f736 | 8113 | m = gfc_match (" private"); |
30b608eb DK |
8114 | if (m == MATCH_ERROR) |
8115 | goto error; | |
8116 | if (m == MATCH_YES) | |
8117 | { | |
e157f736 | 8118 | if (ba->access != ACCESS_UNKNOWN) |
30b608eb | 8119 | { |
e157f736 | 8120 | gfc_error ("Duplicate access-specifier at %C"); |
30b608eb DK |
8121 | goto error; |
8122 | } | |
8123 | ||
e157f736 | 8124 | ba->access = ACCESS_PRIVATE; |
30b608eb DK |
8125 | continue; |
8126 | } | |
8127 | ||
e157f736 DK |
8128 | /* If inside GENERIC, the following is not allowed. */ |
8129 | if (!generic) | |
30b608eb | 8130 | { |
30b608eb | 8131 | |
e157f736 DK |
8132 | /* NOPASS flag. */ |
8133 | m = gfc_match (" nopass"); | |
8134 | if (m == MATCH_ERROR) | |
8135 | goto error; | |
8136 | if (m == MATCH_YES) | |
30b608eb | 8137 | { |
e157f736 DK |
8138 | if (found_passing) |
8139 | { | |
8140 | gfc_error ("Binding attributes already specify passing," | |
8141 | " illegal NOPASS at %C"); | |
8142 | goto error; | |
8143 | } | |
8144 | ||
8145 | found_passing = true; | |
8146 | ba->nopass = 1; | |
8147 | continue; | |
30b608eb DK |
8148 | } |
8149 | ||
e157f736 DK |
8150 | /* PASS possibly including argument. */ |
8151 | m = gfc_match (" pass"); | |
8152 | if (m == MATCH_ERROR) | |
8153 | goto error; | |
8154 | if (m == MATCH_YES) | |
30b608eb | 8155 | { |
e157f736 DK |
8156 | char arg[GFC_MAX_SYMBOL_LEN + 1]; |
8157 | ||
8158 | if (found_passing) | |
8159 | { | |
8160 | gfc_error ("Binding attributes already specify passing," | |
8161 | " illegal PASS at %C"); | |
8162 | goto error; | |
8163 | } | |
8164 | ||
8165 | m = gfc_match (" ( %n )", arg); | |
8166 | if (m == MATCH_ERROR) | |
8167 | goto error; | |
8168 | if (m == MATCH_YES) | |
90661f26 | 8169 | ba->pass_arg = gfc_get_string (arg); |
e157f736 DK |
8170 | gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL)); |
8171 | ||
8172 | found_passing = true; | |
8173 | ba->nopass = 0; | |
8174 | continue; | |
30b608eb DK |
8175 | } |
8176 | ||
713485cc JW |
8177 | if (ppc) |
8178 | { | |
8179 | /* POINTER flag. */ | |
8180 | m = gfc_match (" pointer"); | |
8181 | if (m == MATCH_ERROR) | |
8182 | goto error; | |
8183 | if (m == MATCH_YES) | |
8184 | { | |
8185 | if (seen_ptr) | |
8186 | { | |
8187 | gfc_error ("Duplicate POINTER attribute at %C"); | |
8188 | goto error; | |
8189 | } | |
8190 | ||
8191 | seen_ptr = true; | |
713485cc JW |
8192 | continue; |
8193 | } | |
8194 | } | |
8195 | else | |
8196 | { | |
8197 | /* NON_OVERRIDABLE flag. */ | |
8198 | m = gfc_match (" non_overridable"); | |
8199 | if (m == MATCH_ERROR) | |
8200 | goto error; | |
8201 | if (m == MATCH_YES) | |
8202 | { | |
8203 | if (ba->non_overridable) | |
8204 | { | |
8205 | gfc_error ("Duplicate NON_OVERRIDABLE at %C"); | |
8206 | goto error; | |
8207 | } | |
8208 | ||
8209 | ba->non_overridable = 1; | |
8210 | continue; | |
8211 | } | |
8212 | ||
8213 | /* DEFERRED flag. */ | |
8214 | m = gfc_match (" deferred"); | |
8215 | if (m == MATCH_ERROR) | |
8216 | goto error; | |
8217 | if (m == MATCH_YES) | |
8218 | { | |
8219 | if (ba->deferred) | |
8220 | { | |
8221 | gfc_error ("Duplicate DEFERRED at %C"); | |
8222 | goto error; | |
8223 | } | |
8224 | ||
8225 | ba->deferred = 1; | |
8226 | continue; | |
8227 | } | |
8228 | } | |
8229 | ||
30b608eb DK |
8230 | } |
8231 | ||
8232 | /* Nothing matching found. */ | |
e157f736 DK |
8233 | if (generic) |
8234 | gfc_error ("Expected access-specifier at %C"); | |
8235 | else | |
8236 | gfc_error ("Expected binding attribute at %C"); | |
30b608eb DK |
8237 | goto error; |
8238 | } | |
8239 | while (gfc_match_char (',') == MATCH_YES); | |
8240 | ||
b0e5fa94 DK |
8241 | /* NON_OVERRIDABLE and DEFERRED exclude themselves. */ |
8242 | if (ba->non_overridable && ba->deferred) | |
8243 | { | |
8244 | gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C"); | |
8245 | goto error; | |
8246 | } | |
8247 | ||
90661f26 JW |
8248 | m = MATCH_YES; |
8249 | ||
8250 | done: | |
e157f736 DK |
8251 | if (ba->access == ACCESS_UNKNOWN) |
8252 | ba->access = gfc_typebound_default_access; | |
8253 | ||
713485cc JW |
8254 | if (ppc && !seen_ptr) |
8255 | { | |
8256 | gfc_error ("POINTER attribute is required for procedure pointer component" | |
8257 | " at %C"); | |
8258 | goto error; | |
8259 | } | |
8260 | ||
90661f26 | 8261 | return m; |
30b608eb DK |
8262 | |
8263 | error: | |
30b608eb DK |
8264 | return MATCH_ERROR; |
8265 | } | |
8266 | ||
8267 | ||
8268 | /* Match a PROCEDURE specific binding inside a derived type. */ | |
8269 | ||
8270 | static match | |
8271 | match_procedure_in_type (void) | |
8272 | { | |
8273 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
8274 | char target_buf[GFC_MAX_SYMBOL_LEN + 1]; | |
1be17993 | 8275 | char* target = NULL, *ifc = NULL; |
3e15518b | 8276 | gfc_typebound_proc tb; |
30b608eb DK |
8277 | bool seen_colons; |
8278 | bool seen_attrs; | |
8279 | match m; | |
8280 | gfc_symtree* stree; | |
8281 | gfc_namespace* ns; | |
8282 | gfc_symbol* block; | |
1be17993 | 8283 | int num; |
30b608eb DK |
8284 | |
8285 | /* Check current state. */ | |
8286 | gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS); | |
8287 | block = gfc_state_stack->previous->sym; | |
8288 | gcc_assert (block); | |
8289 | ||
b0e5fa94 | 8290 | /* Try to match PROCEDURE(interface). */ |
30b608eb DK |
8291 | if (gfc_match (" (") == MATCH_YES) |
8292 | { | |
b0e5fa94 DK |
8293 | m = gfc_match_name (target_buf); |
8294 | if (m == MATCH_ERROR) | |
8295 | return m; | |
8296 | if (m != MATCH_YES) | |
8297 | { | |
a4d9b221 | 8298 | gfc_error ("Interface-name expected after %<(%> at %C"); |
b0e5fa94 DK |
8299 | return MATCH_ERROR; |
8300 | } | |
8301 | ||
8302 | if (gfc_match (" )") != MATCH_YES) | |
8303 | { | |
a4d9b221 | 8304 | gfc_error ("%<)%> expected at %C"); |
b0e5fa94 DK |
8305 | return MATCH_ERROR; |
8306 | } | |
8307 | ||
1be17993 | 8308 | ifc = target_buf; |
30b608eb DK |
8309 | } |
8310 | ||
8311 | /* Construct the data structure. */ | |
ff5b6492 | 8312 | memset (&tb, 0, sizeof (tb)); |
3e15518b | 8313 | tb.where = gfc_current_locus; |
30b608eb DK |
8314 | |
8315 | /* Match binding attributes. */ | |
3e15518b | 8316 | m = match_binding_attributes (&tb, false, false); |
30b608eb DK |
8317 | if (m == MATCH_ERROR) |
8318 | return m; | |
8319 | seen_attrs = (m == MATCH_YES); | |
8320 | ||
1be17993 | 8321 | /* Check that attribute DEFERRED is given if an interface is specified. */ |
3e15518b | 8322 | if (tb.deferred && !ifc) |
b0e5fa94 DK |
8323 | { |
8324 | gfc_error ("Interface must be specified for DEFERRED binding at %C"); | |
8325 | return MATCH_ERROR; | |
8326 | } | |
3e15518b | 8327 | if (ifc && !tb.deferred) |
b0e5fa94 DK |
8328 | { |
8329 | gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED"); | |
8330 | return MATCH_ERROR; | |
8331 | } | |
8332 | ||
30b608eb DK |
8333 | /* Match the colons. */ |
8334 | m = gfc_match (" ::"); | |
8335 | if (m == MATCH_ERROR) | |
8336 | return m; | |
8337 | seen_colons = (m == MATCH_YES); | |
8338 | if (seen_attrs && !seen_colons) | |
8339 | { | |
a4d9b221 | 8340 | gfc_error ("Expected %<::%> after binding-attributes at %C"); |
30b608eb DK |
8341 | return MATCH_ERROR; |
8342 | } | |
8343 | ||
f5acf0f2 | 8344 | /* Match the binding names. */ |
1be17993 | 8345 | for(num=1;;num++) |
30b608eb | 8346 | { |
1be17993 JW |
8347 | m = gfc_match_name (name); |
8348 | if (m == MATCH_ERROR) | |
8349 | return m; | |
8350 | if (m == MATCH_NO) | |
b0e5fa94 | 8351 | { |
1be17993 | 8352 | gfc_error ("Expected binding name at %C"); |
b0e5fa94 DK |
8353 | return MATCH_ERROR; |
8354 | } | |
8355 | ||
524af0d6 | 8356 | if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C")) |
1be17993 | 8357 | return MATCH_ERROR; |
30b608eb | 8358 | |
1be17993 JW |
8359 | /* Try to match the '=> target', if it's there. */ |
8360 | target = ifc; | |
8361 | m = gfc_match (" =>"); | |
30b608eb DK |
8362 | if (m == MATCH_ERROR) |
8363 | return m; | |
1be17993 | 8364 | if (m == MATCH_YES) |
30b608eb | 8365 | { |
3e15518b | 8366 | if (tb.deferred) |
1be17993 | 8367 | { |
a4d9b221 | 8368 | gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C"); |
1be17993 JW |
8369 | return MATCH_ERROR; |
8370 | } | |
8371 | ||
8372 | if (!seen_colons) | |
8373 | { | |
a4d9b221 | 8374 | gfc_error ("%<::%> needed in PROCEDURE binding with explicit target" |
1be17993 JW |
8375 | " at %C"); |
8376 | return MATCH_ERROR; | |
8377 | } | |
8378 | ||
8379 | m = gfc_match_name (target_buf); | |
8380 | if (m == MATCH_ERROR) | |
8381 | return m; | |
8382 | if (m == MATCH_NO) | |
8383 | { | |
a4d9b221 | 8384 | gfc_error ("Expected binding target after %<=>%> at %C"); |
1be17993 JW |
8385 | return MATCH_ERROR; |
8386 | } | |
8387 | target = target_buf; | |
30b608eb | 8388 | } |
30b608eb | 8389 | |
1be17993 JW |
8390 | /* If no target was found, it has the same name as the binding. */ |
8391 | if (!target) | |
8392 | target = name; | |
30b608eb | 8393 | |
1be17993 JW |
8394 | /* Get the namespace to insert the symbols into. */ |
8395 | ns = block->f2k_derived; | |
8396 | gcc_assert (ns); | |
30b608eb | 8397 | |
1be17993 | 8398 | /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */ |
3e15518b | 8399 | if (tb.deferred && !block->attr.abstract) |
1be17993 | 8400 | { |
c4100eae | 8401 | gfc_error ("Type %qs containing DEFERRED binding at %C " |
1be17993 JW |
8402 | "is not ABSTRACT", block->name); |
8403 | return MATCH_ERROR; | |
8404 | } | |
30b608eb | 8405 | |
1be17993 | 8406 | /* See if we already have a binding with this name in the symtree which |
6bd2c800 | 8407 | would be an error. If a GENERIC already targeted this binding, it may |
1be17993 JW |
8408 | be already there but then typebound is still NULL. */ |
8409 | stree = gfc_find_symtree (ns->tb_sym_root, name); | |
9f23af48 | 8410 | if (stree && stree->n.tb) |
1be17993 | 8411 | { |
c4100eae MLI |
8412 | gfc_error ("There is already a procedure with binding name %qs for " |
8413 | "the derived type %qs at %C", name, block->name); | |
1be17993 JW |
8414 | return MATCH_ERROR; |
8415 | } | |
b0e5fa94 | 8416 | |
1be17993 | 8417 | /* Insert it and set attributes. */ |
30b608eb | 8418 | |
9f23af48 MM |
8419 | if (!stree) |
8420 | { | |
8421 | stree = gfc_new_symtree (&ns->tb_sym_root, name); | |
8422 | gcc_assert (stree); | |
8423 | } | |
3e15518b | 8424 | stree->n.tb = gfc_get_typebound_proc (&tb); |
e34ccb4c | 8425 | |
3e15518b JW |
8426 | if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific, |
8427 | false)) | |
1be17993 | 8428 | return MATCH_ERROR; |
3e15518b | 8429 | gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym); |
f5acf0f2 | 8430 | |
1be17993 JW |
8431 | if (gfc_match_eos () == MATCH_YES) |
8432 | return MATCH_YES; | |
8433 | if (gfc_match_char (',') != MATCH_YES) | |
8434 | goto syntax; | |
e34ccb4c | 8435 | } |
30b608eb | 8436 | |
1be17993 JW |
8437 | syntax: |
8438 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
8439 | return MATCH_ERROR; | |
30b608eb DK |
8440 | } |
8441 | ||
8442 | ||
e157f736 DK |
8443 | /* Match a GENERIC procedure binding inside a derived type. */ |
8444 | ||
8445 | match | |
8446 | gfc_match_generic (void) | |
8447 | { | |
8448 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
94747289 | 8449 | char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */ |
e157f736 DK |
8450 | gfc_symbol* block; |
8451 | gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */ | |
8452 | gfc_typebound_proc* tb; | |
e157f736 | 8453 | gfc_namespace* ns; |
94747289 DK |
8454 | interface_type op_type; |
8455 | gfc_intrinsic_op op; | |
e157f736 DK |
8456 | match m; |
8457 | ||
8458 | /* Check current state. */ | |
8459 | if (gfc_current_state () == COMP_DERIVED) | |
8460 | { | |
8461 | gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS"); | |
8462 | return MATCH_ERROR; | |
8463 | } | |
8464 | if (gfc_current_state () != COMP_DERIVED_CONTAINS) | |
8465 | return MATCH_NO; | |
8466 | block = gfc_state_stack->previous->sym; | |
8467 | ns = block->f2k_derived; | |
8468 | gcc_assert (block && ns); | |
8469 | ||
ff5b6492 MM |
8470 | memset (&tbattr, 0, sizeof (tbattr)); |
8471 | tbattr.where = gfc_current_locus; | |
8472 | ||
e157f736 | 8473 | /* See if we get an access-specifier. */ |
713485cc | 8474 | m = match_binding_attributes (&tbattr, true, false); |
e157f736 DK |
8475 | if (m == MATCH_ERROR) |
8476 | goto error; | |
8477 | ||
8478 | /* Now the colons, those are required. */ | |
8479 | if (gfc_match (" ::") != MATCH_YES) | |
8480 | { | |
a4d9b221 | 8481 | gfc_error ("Expected %<::%> at %C"); |
e157f736 DK |
8482 | goto error; |
8483 | } | |
8484 | ||
94747289 DK |
8485 | /* Match the binding name; depending on type (operator / generic) format |
8486 | it for future error messages into bind_name. */ | |
f5acf0f2 | 8487 | |
94747289 | 8488 | m = gfc_match_generic_spec (&op_type, name, &op); |
e157f736 DK |
8489 | if (m == MATCH_ERROR) |
8490 | return MATCH_ERROR; | |
8491 | if (m == MATCH_NO) | |
8492 | { | |
94747289 | 8493 | gfc_error ("Expected generic name or operator descriptor at %C"); |
e157f736 DK |
8494 | goto error; |
8495 | } | |
8496 | ||
94747289 | 8497 | switch (op_type) |
e157f736 | 8498 | { |
94747289 DK |
8499 | case INTERFACE_GENERIC: |
8500 | snprintf (bind_name, sizeof (bind_name), "%s", name); | |
8501 | break; | |
f5acf0f2 | 8502 | |
94747289 DK |
8503 | case INTERFACE_USER_OP: |
8504 | snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name); | |
8505 | break; | |
f5acf0f2 | 8506 | |
94747289 DK |
8507 | case INTERFACE_INTRINSIC_OP: |
8508 | snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)", | |
8509 | gfc_op2string (op)); | |
8510 | break; | |
8511 | ||
8512 | default: | |
8513 | gcc_unreachable (); | |
8514 | } | |
e34ccb4c | 8515 | |
94747289 DK |
8516 | /* Match the required =>. */ |
8517 | if (gfc_match (" =>") != MATCH_YES) | |
8518 | { | |
a4d9b221 | 8519 | gfc_error ("Expected %<=>%> at %C"); |
94747289 DK |
8520 | goto error; |
8521 | } | |
f5acf0f2 | 8522 | |
94747289 DK |
8523 | /* Try to find existing GENERIC binding with this name / for this operator; |
8524 | if there is something, check that it is another GENERIC and then extend | |
8525 | it rather than building a new node. Otherwise, create it and put it | |
8526 | at the right position. */ | |
8527 | ||
8528 | switch (op_type) | |
8529 | { | |
8530 | case INTERFACE_USER_OP: | |
8531 | case INTERFACE_GENERIC: | |
8532 | { | |
8533 | const bool is_op = (op_type == INTERFACE_USER_OP); | |
8534 | gfc_symtree* st; | |
8535 | ||
8536 | st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name); | |
8537 | if (st) | |
8538 | { | |
8539 | tb = st->n.tb; | |
8540 | gcc_assert (tb); | |
8541 | } | |
8542 | else | |
8543 | tb = NULL; | |
8544 | ||
8545 | break; | |
8546 | } | |
8547 | ||
8548 | case INTERFACE_INTRINSIC_OP: | |
8549 | tb = ns->tb_op[op]; | |
8550 | break; | |
8551 | ||
8552 | default: | |
8553 | gcc_unreachable (); | |
8554 | } | |
8555 | ||
8556 | if (tb) | |
8557 | { | |
e34ccb4c | 8558 | if (!tb->is_generic) |
e157f736 | 8559 | { |
94747289 | 8560 | gcc_assert (op_type == INTERFACE_GENERIC); |
e157f736 | 8561 | gfc_error ("There's already a non-generic procedure with binding name" |
c4100eae | 8562 | " %qs for the derived type %qs at %C", |
94747289 | 8563 | bind_name, block->name); |
e157f736 DK |
8564 | goto error; |
8565 | } | |
8566 | ||
e157f736 DK |
8567 | if (tb->access != tbattr.access) |
8568 | { | |
8569 | gfc_error ("Binding at %C must have the same access as already" | |
c4100eae | 8570 | " defined binding %qs", bind_name); |
e157f736 DK |
8571 | goto error; |
8572 | } | |
8573 | } | |
8574 | else | |
8575 | { | |
3e15518b | 8576 | tb = gfc_get_typebound_proc (NULL); |
e157f736 DK |
8577 | tb->where = gfc_current_locus; |
8578 | tb->access = tbattr.access; | |
8579 | tb->is_generic = 1; | |
8580 | tb->u.generic = NULL; | |
94747289 DK |
8581 | |
8582 | switch (op_type) | |
8583 | { | |
8584 | case INTERFACE_GENERIC: | |
8585 | case INTERFACE_USER_OP: | |
8586 | { | |
8587 | const bool is_op = (op_type == INTERFACE_USER_OP); | |
8588 | gfc_symtree* st; | |
8589 | ||
8590 | st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root, | |
8591 | name); | |
8592 | gcc_assert (st); | |
8593 | st->n.tb = tb; | |
8594 | ||
8595 | break; | |
8596 | } | |
f5acf0f2 | 8597 | |
94747289 DK |
8598 | case INTERFACE_INTRINSIC_OP: |
8599 | ns->tb_op[op] = tb; | |
8600 | break; | |
8601 | ||
8602 | default: | |
8603 | gcc_unreachable (); | |
8604 | } | |
e157f736 DK |
8605 | } |
8606 | ||
8607 | /* Now, match all following names as specific targets. */ | |
8608 | do | |
8609 | { | |
8610 | gfc_symtree* target_st; | |
8611 | gfc_tbp_generic* target; | |
8612 | ||
8613 | m = gfc_match_name (name); | |
8614 | if (m == MATCH_ERROR) | |
8615 | goto error; | |
8616 | if (m == MATCH_NO) | |
8617 | { | |
8618 | gfc_error ("Expected specific binding name at %C"); | |
8619 | goto error; | |
8620 | } | |
8621 | ||
e34ccb4c | 8622 | target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name); |
e157f736 DK |
8623 | |
8624 | /* See if this is a duplicate specification. */ | |
8625 | for (target = tb->u.generic; target; target = target->next) | |
8626 | if (target_st == target->specific_st) | |
8627 | { | |
c4100eae MLI |
8628 | gfc_error ("%qs already defined as specific binding for the" |
8629 | " generic %qs at %C", name, bind_name); | |
e157f736 DK |
8630 | goto error; |
8631 | } | |
8632 | ||
e157f736 DK |
8633 | target = gfc_get_tbp_generic (); |
8634 | target->specific_st = target_st; | |
8635 | target->specific = NULL; | |
8636 | target->next = tb->u.generic; | |
218e1228 TB |
8637 | target->is_operator = ((op_type == INTERFACE_USER_OP) |
8638 | || (op_type == INTERFACE_INTRINSIC_OP)); | |
e157f736 DK |
8639 | tb->u.generic = target; |
8640 | } | |
8641 | while (gfc_match (" ,") == MATCH_YES); | |
8642 | ||
8643 | /* Here should be the end. */ | |
8644 | if (gfc_match_eos () != MATCH_YES) | |
8645 | { | |
8646 | gfc_error ("Junk after GENERIC binding at %C"); | |
8647 | goto error; | |
8648 | } | |
8649 | ||
8650 | return MATCH_YES; | |
8651 | ||
8652 | error: | |
8653 | return MATCH_ERROR; | |
8654 | } | |
8655 | ||
8656 | ||
34523524 DK |
8657 | /* Match a FINAL declaration inside a derived type. */ |
8658 | ||
8659 | match | |
8660 | gfc_match_final_decl (void) | |
8661 | { | |
8662 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
8663 | gfc_symbol* sym; | |
8664 | match m; | |
8665 | gfc_namespace* module_ns; | |
8666 | bool first, last; | |
30b608eb | 8667 | gfc_symbol* block; |
34523524 | 8668 | |
33344e0f JW |
8669 | if (gfc_current_form == FORM_FREE) |
8670 | { | |
8671 | char c = gfc_peek_ascii_char (); | |
8672 | if (!gfc_is_whitespace (c) && c != ':') | |
8673 | return MATCH_NO; | |
8674 | } | |
f5acf0f2 | 8675 | |
30b608eb | 8676 | if (gfc_state_stack->state != COMP_DERIVED_CONTAINS) |
34523524 | 8677 | { |
33344e0f JW |
8678 | if (gfc_current_form == FORM_FIXED) |
8679 | return MATCH_NO; | |
8680 | ||
34523524 | 8681 | gfc_error ("FINAL declaration at %C must be inside a derived type " |
30b608eb | 8682 | "CONTAINS section"); |
34523524 DK |
8683 | return MATCH_ERROR; |
8684 | } | |
8685 | ||
30b608eb DK |
8686 | block = gfc_state_stack->previous->sym; |
8687 | gcc_assert (block); | |
34523524 | 8688 | |
30b608eb DK |
8689 | if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous |
8690 | || gfc_state_stack->previous->previous->state != COMP_MODULE) | |
34523524 DK |
8691 | { |
8692 | gfc_error ("Derived type declaration with FINAL at %C must be in the" | |
8693 | " specification part of a MODULE"); | |
8694 | return MATCH_ERROR; | |
8695 | } | |
8696 | ||
8697 | module_ns = gfc_current_ns; | |
8698 | gcc_assert (module_ns); | |
8699 | gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE); | |
8700 | ||
8701 | /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */ | |
8702 | if (gfc_match (" ::") == MATCH_ERROR) | |
8703 | return MATCH_ERROR; | |
8704 | ||
8705 | /* Match the sequence of procedure names. */ | |
8706 | first = true; | |
8707 | last = false; | |
8708 | do | |
8709 | { | |
8710 | gfc_finalizer* f; | |
8711 | ||
8712 | if (first && gfc_match_eos () == MATCH_YES) | |
8713 | { | |
8714 | gfc_error ("Empty FINAL at %C"); | |
8715 | return MATCH_ERROR; | |
8716 | } | |
8717 | ||
8718 | m = gfc_match_name (name); | |
8719 | if (m == MATCH_NO) | |
8720 | { | |
8721 | gfc_error ("Expected module procedure name at %C"); | |
8722 | return MATCH_ERROR; | |
8723 | } | |
8724 | else if (m != MATCH_YES) | |
8725 | return MATCH_ERROR; | |
8726 | ||
8727 | if (gfc_match_eos () == MATCH_YES) | |
8728 | last = true; | |
8729 | if (!last && gfc_match_char (',') != MATCH_YES) | |
8730 | { | |
a4d9b221 | 8731 | gfc_error ("Expected %<,%> at %C"); |
34523524 DK |
8732 | return MATCH_ERROR; |
8733 | } | |
8734 | ||
8735 | if (gfc_get_symbol (name, module_ns, &sym)) | |
8736 | { | |
c4100eae | 8737 | gfc_error ("Unknown procedure name %qs at %C", name); |
34523524 DK |
8738 | return MATCH_ERROR; |
8739 | } | |
8740 | ||
8741 | /* Mark the symbol as module procedure. */ | |
8742 | if (sym->attr.proc != PROC_MODULE | |
524af0d6 | 8743 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) |
34523524 DK |
8744 | return MATCH_ERROR; |
8745 | ||
8746 | /* Check if we already have this symbol in the list, this is an error. */ | |
30b608eb | 8747 | for (f = block->f2k_derived->finalizers; f; f = f->next) |
f6fad28e | 8748 | if (f->proc_sym == sym) |
34523524 | 8749 | { |
c4100eae | 8750 | gfc_error ("%qs at %C is already defined as FINAL procedure!", |
34523524 DK |
8751 | name); |
8752 | return MATCH_ERROR; | |
8753 | } | |
8754 | ||
8755 | /* Add this symbol to the list of finalizers. */ | |
30b608eb | 8756 | gcc_assert (block->f2k_derived); |
34523524 | 8757 | ++sym->refs; |
ece3f663 | 8758 | f = XCNEW (gfc_finalizer); |
f6fad28e DK |
8759 | f->proc_sym = sym; |
8760 | f->proc_tree = NULL; | |
34523524 | 8761 | f->where = gfc_current_locus; |
30b608eb DK |
8762 | f->next = block->f2k_derived->finalizers; |
8763 | block->f2k_derived->finalizers = f; | |
34523524 DK |
8764 | |
8765 | first = false; | |
8766 | } | |
8767 | while (!last); | |
8768 | ||
8769 | return MATCH_YES; | |
8770 | } | |
08a6b8e0 TB |
8771 | |
8772 | ||
8773 | const ext_attr_t ext_attr_list[] = { | |
e7ac6a7c TB |
8774 | { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" }, |
8775 | { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" }, | |
8776 | { "cdecl", EXT_ATTR_CDECL, "cdecl" }, | |
8777 | { "stdcall", EXT_ATTR_STDCALL, "stdcall" }, | |
8778 | { "fastcall", EXT_ATTR_FASTCALL, "fastcall" }, | |
8779 | { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL }, | |
8780 | { NULL, EXT_ATTR_LAST, NULL } | |
08a6b8e0 TB |
8781 | }; |
8782 | ||
8783 | /* Match a !GCC$ ATTRIBUTES statement of the form: | |
8784 | !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ... | |
8785 | When we come here, we have already matched the !GCC$ ATTRIBUTES string. | |
8786 | ||
8787 | TODO: We should support all GCC attributes using the same syntax for | |
8788 | the attribute list, i.e. the list in C | |
8789 | __attributes(( attribute-list )) | |
8790 | matches then | |
8791 | !GCC$ ATTRIBUTES attribute-list :: | |
8792 | Cf. c-parser.c's c_parser_attributes; the data can then directly be | |
8793 | saved into a TREE. | |
8794 | ||
8795 | As there is absolutely no risk of confusion, we should never return | |
8796 | MATCH_NO. */ | |
8797 | match | |
8798 | gfc_match_gcc_attributes (void) | |
f5acf0f2 | 8799 | { |
08a6b8e0 TB |
8800 | symbol_attribute attr; |
8801 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
8802 | unsigned id; | |
8803 | gfc_symbol *sym; | |
8804 | match m; | |
8805 | ||
8806 | gfc_clear_attr (&attr); | |
8807 | for(;;) | |
8808 | { | |
8809 | char ch; | |
8810 | ||
8811 | if (gfc_match_name (name) != MATCH_YES) | |
8812 | return MATCH_ERROR; | |
8813 | ||
8814 | for (id = 0; id < EXT_ATTR_LAST; id++) | |
8815 | if (strcmp (name, ext_attr_list[id].name) == 0) | |
8816 | break; | |
8817 | ||
8818 | if (id == EXT_ATTR_LAST) | |
8819 | { | |
8820 | gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C"); | |
8821 | return MATCH_ERROR; | |
8822 | } | |
8823 | ||
524af0d6 | 8824 | if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus)) |
08a6b8e0 TB |
8825 | return MATCH_ERROR; |
8826 | ||
8827 | gfc_gobble_whitespace (); | |
8828 | ch = gfc_next_ascii_char (); | |
8829 | if (ch == ':') | |
8830 | { | |
8831 | /* This is the successful exit condition for the loop. */ | |
8832 | if (gfc_next_ascii_char () == ':') | |
8833 | break; | |
8834 | } | |
8835 | ||
8836 | if (ch == ',') | |
8837 | continue; | |
8838 | ||
8839 | goto syntax; | |
8840 | } | |
8841 | ||
8842 | if (gfc_match_eos () == MATCH_YES) | |
8843 | goto syntax; | |
8844 | ||
8845 | for(;;) | |
8846 | { | |
8847 | m = gfc_match_name (name); | |
8848 | if (m != MATCH_YES) | |
8849 | return m; | |
8850 | ||
8851 | if (find_special (name, &sym, true)) | |
8852 | return MATCH_ERROR; | |
f5acf0f2 | 8853 | |
08a6b8e0 TB |
8854 | sym->attr.ext_attr |= attr.ext_attr; |
8855 | ||
8856 | if (gfc_match_eos () == MATCH_YES) | |
8857 | break; | |
8858 | ||
8859 | if (gfc_match_char (',') != MATCH_YES) | |
8860 | goto syntax; | |
8861 | } | |
8862 | ||
8863 | return MATCH_YES; | |
8864 | ||
8865 | syntax: | |
8866 | gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C"); | |
8867 | return MATCH_ERROR; | |
8868 | } |