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