]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Declaration statement matcher |
a5544970 | 2 | Copyright (C) 2002-2019 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" |
2adfab87 AM |
24 | #include "options.h" |
25 | #include "tree.h" | |
6de9cd9a | 26 | #include "gfortran.h" |
2adfab87 | 27 | #include "stringpool.h" |
6de9cd9a DN |
28 | #include "match.h" |
29 | #include "parse.h" | |
b7e75771 | 30 | #include "constructor.h" |
e8cecccc | 31 | #include "target.h" |
ca39e6f2 FXC |
32 | |
33 | /* Macros to access allocate memory for gfc_data_variable, | |
34 | gfc_data_value and gfc_data. */ | |
ece3f663 KG |
35 | #define gfc_get_data_variable() XCNEW (gfc_data_variable) |
36 | #define gfc_get_data_value() XCNEW (gfc_data_value) | |
37 | #define gfc_get_data() XCNEW (gfc_data) | |
ca39e6f2 FXC |
38 | |
39 | ||
524af0d6 | 40 | static bool set_binding_label (const char **, const char *, int); |
62603fae JB |
41 | |
42 | ||
2054fc29 | 43 | /* This flag is set if an old-style length selector is matched |
6de9cd9a DN |
44 | during a type-declaration statement. */ |
45 | ||
46 | static int old_char_selector; | |
47 | ||
46fa431d | 48 | /* When variables acquire types and attributes from a declaration |
6de9cd9a DN |
49 | statement, they get them from the following static variables. The |
50 | first part of a declaration sets these variables and the second | |
51 | part copies these into symbol structures. */ | |
52 | ||
53 | static gfc_typespec current_ts; | |
54 | ||
55 | static symbol_attribute current_attr; | |
56 | static gfc_array_spec *current_as; | |
57 | static int colon_seen; | |
6f855a26 | 58 | static int attr_seen; |
6de9cd9a | 59 | |
a8b3b0b6 | 60 | /* The current binding label (if any). */ |
9975a30b | 61 | static const char* curr_binding_label; |
a8b3b0b6 CR |
62 | /* Need to know how many identifiers are on the current data declaration |
63 | line in case we're given the BIND(C) attribute with a NAME= specifier. */ | |
64 | static int num_idents_on_line; | |
65 | /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we | |
66 | can supply a name if the curr_binding_label is nil and NAME= was not. */ | |
67 | static int has_name_equals = 0; | |
68 | ||
25d8f0a2 TS |
69 | /* Initializer of the previous enumerator. */ |
70 | ||
71 | static gfc_expr *last_initializer; | |
72 | ||
73 | /* History of all the enumerators is maintained, so that | |
74 | kind values of all the enumerators could be updated depending | |
75 | upon the maximum initialized value. */ | |
76 | ||
77 | typedef struct enumerator_history | |
78 | { | |
79 | gfc_symbol *sym; | |
80 | gfc_expr *initializer; | |
81 | struct enumerator_history *next; | |
82 | } | |
83 | enumerator_history; | |
84 | ||
85 | /* Header of enum history chain. */ | |
86 | ||
87 | static enumerator_history *enum_history = NULL; | |
88 | ||
89 | /* Pointer of enum history node containing largest initializer. */ | |
90 | ||
91 | static enumerator_history *max_enum = NULL; | |
92 | ||
6de9cd9a DN |
93 | /* gfc_new_block points to the symbol of a newly matched block. */ |
94 | ||
95 | gfc_symbol *gfc_new_block; | |
96 | ||
1c8bcdf7 | 97 | bool gfc_matching_function; |
e2d29968 | 98 | |
170a8bd6 EB |
99 | /* Set upon parsing a !GCC$ unroll n directive for use in the next loop. */ |
100 | int directive_unroll = -1; | |
101 | ||
facf0354 ML |
102 | /* Map of middle-end built-ins that should be vectorized. */ |
103 | hash_map<nofree_string_hash, int> *gfc_vectorized_builtins; | |
104 | ||
5bab4c96 PT |
105 | /* If a kind expression of a component of a parameterized derived type is |
106 | parameterized, temporarily store the expression here. */ | |
107 | static gfc_expr *saved_kind_expr = NULL; | |
108 | ||
109 | /* Used to store the parameter list arising in a PDT declaration and | |
110 | in the typespec of a PDT variable or component. */ | |
111 | static gfc_actual_arglist *decl_type_param_list; | |
112 | static gfc_actual_arglist *type_param_spec_list; | |
113 | ||
294fbfc8 TS |
114 | /********************* DATA statement subroutines *********************/ |
115 | ||
2220652d PT |
116 | static bool in_match_data = false; |
117 | ||
118 | bool | |
119 | gfc_in_match_data (void) | |
120 | { | |
121 | return in_match_data; | |
122 | } | |
123 | ||
ca39e6f2 FXC |
124 | static void |
125 | set_in_match_data (bool set_value) | |
2220652d PT |
126 | { |
127 | in_match_data = set_value; | |
128 | } | |
129 | ||
294fbfc8 TS |
130 | /* Free a gfc_data_variable structure and everything beneath it. */ |
131 | ||
132 | static void | |
636dff67 | 133 | free_variable (gfc_data_variable *p) |
294fbfc8 TS |
134 | { |
135 | gfc_data_variable *q; | |
136 | ||
137 | for (; p; p = q) | |
138 | { | |
139 | q = p->next; | |
140 | gfc_free_expr (p->expr); | |
141 | gfc_free_iterator (&p->iter, 0); | |
142 | free_variable (p->list); | |
cede9502 | 143 | free (p); |
294fbfc8 TS |
144 | } |
145 | } | |
146 | ||
147 | ||
148 | /* Free a gfc_data_value structure and everything beneath it. */ | |
149 | ||
150 | static void | |
636dff67 | 151 | free_value (gfc_data_value *p) |
294fbfc8 TS |
152 | { |
153 | gfc_data_value *q; | |
154 | ||
155 | for (; p; p = q) | |
156 | { | |
157 | q = p->next; | |
c9d75a48 | 158 | mpz_clear (p->repeat); |
294fbfc8 | 159 | gfc_free_expr (p->expr); |
cede9502 | 160 | free (p); |
294fbfc8 TS |
161 | } |
162 | } | |
163 | ||
164 | ||
165 | /* Free a list of gfc_data structures. */ | |
166 | ||
167 | void | |
636dff67 | 168 | gfc_free_data (gfc_data *p) |
294fbfc8 TS |
169 | { |
170 | gfc_data *q; | |
171 | ||
172 | for (; p; p = q) | |
173 | { | |
174 | q = p->next; | |
294fbfc8 TS |
175 | free_variable (p->var); |
176 | free_value (p->value); | |
cede9502 | 177 | free (p); |
294fbfc8 TS |
178 | } |
179 | } | |
180 | ||
181 | ||
a9f6f1f2 | 182 | /* Free all data in a namespace. */ |
636dff67 | 183 | |
a9f6f1f2 | 184 | static void |
66e4ab31 | 185 | gfc_free_data_all (gfc_namespace *ns) |
a9f6f1f2 JD |
186 | { |
187 | gfc_data *d; | |
188 | ||
189 | for (;ns->data;) | |
190 | { | |
191 | d = ns->data->next; | |
cede9502 | 192 | free (ns->data); |
a9f6f1f2 JD |
193 | ns->data = d; |
194 | } | |
195 | } | |
196 | ||
d5e2274d SB |
197 | /* Reject data parsed since the last restore point was marked. */ |
198 | ||
199 | void | |
200 | gfc_reject_data (gfc_namespace *ns) | |
201 | { | |
202 | gfc_data *d; | |
203 | ||
204 | while (ns->data && ns->data != ns->old_data) | |
205 | { | |
206 | d = ns->data->next; | |
207 | free (ns->data); | |
208 | ns->data = d; | |
209 | } | |
210 | } | |
a9f6f1f2 | 211 | |
294fbfc8 TS |
212 | static match var_element (gfc_data_variable *); |
213 | ||
214 | /* Match a list of variables terminated by an iterator and a right | |
215 | parenthesis. */ | |
216 | ||
217 | static match | |
636dff67 | 218 | var_list (gfc_data_variable *parent) |
294fbfc8 TS |
219 | { |
220 | gfc_data_variable *tail, var; | |
221 | match m; | |
222 | ||
223 | m = var_element (&var); | |
224 | if (m == MATCH_ERROR) | |
225 | return MATCH_ERROR; | |
226 | if (m == MATCH_NO) | |
227 | goto syntax; | |
228 | ||
229 | tail = gfc_get_data_variable (); | |
230 | *tail = var; | |
231 | ||
232 | parent->list = tail; | |
233 | ||
234 | for (;;) | |
235 | { | |
236 | if (gfc_match_char (',') != MATCH_YES) | |
237 | goto syntax; | |
238 | ||
239 | m = gfc_match_iterator (&parent->iter, 1); | |
240 | if (m == MATCH_YES) | |
241 | break; | |
242 | if (m == MATCH_ERROR) | |
243 | return MATCH_ERROR; | |
244 | ||
245 | m = var_element (&var); | |
246 | if (m == MATCH_ERROR) | |
247 | return MATCH_ERROR; | |
248 | if (m == MATCH_NO) | |
249 | goto syntax; | |
250 | ||
251 | tail->next = gfc_get_data_variable (); | |
252 | tail = tail->next; | |
253 | ||
254 | *tail = var; | |
255 | } | |
256 | ||
257 | if (gfc_match_char (')') != MATCH_YES) | |
258 | goto syntax; | |
259 | return MATCH_YES; | |
260 | ||
261 | syntax: | |
262 | gfc_syntax_error (ST_DATA); | |
263 | return MATCH_ERROR; | |
264 | } | |
265 | ||
266 | ||
267 | /* Match a single element in a data variable list, which can be a | |
268 | variable-iterator list. */ | |
269 | ||
270 | static match | |
7b901ac4 | 271 | var_element (gfc_data_variable *new_var) |
294fbfc8 TS |
272 | { |
273 | match m; | |
274 | gfc_symbol *sym; | |
275 | ||
7b901ac4 | 276 | memset (new_var, 0, sizeof (gfc_data_variable)); |
294fbfc8 TS |
277 | |
278 | if (gfc_match_char ('(') == MATCH_YES) | |
7b901ac4 | 279 | return var_list (new_var); |
294fbfc8 | 280 | |
7b901ac4 | 281 | m = gfc_match_variable (&new_var->expr, 0); |
294fbfc8 TS |
282 | if (m != MATCH_YES) |
283 | return m; | |
284 | ||
094a0ecc SK |
285 | if (new_var->expr->expr_type == EXPR_CONSTANT |
286 | && new_var->expr->symtree == NULL) | |
287 | { | |
288 | gfc_error ("Inquiry parameter cannot appear in a " | |
289 | "data-stmt-object-list at %C"); | |
290 | return MATCH_ERROR; | |
291 | } | |
292 | ||
7b901ac4 | 293 | sym = new_var->expr->symtree->n.sym; |
294fbfc8 | 294 | |
f37e928c | 295 | /* Symbol should already have an associated type. */ |
524af0d6 | 296 | if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus)) |
f37e928c DK |
297 | return MATCH_ERROR; |
298 | ||
636dff67 SK |
299 | if (!sym->attr.function && gfc_current_ns->parent |
300 | && gfc_current_ns->parent == sym->ns) | |
294fbfc8 | 301 | { |
c4100eae | 302 | gfc_error ("Host associated variable %qs may not be in the DATA " |
e25a0da3 | 303 | "statement at %C", sym->name); |
294fbfc8 TS |
304 | return MATCH_ERROR; |
305 | } | |
306 | ||
4075a94e | 307 | if (gfc_current_state () != COMP_BLOCK_DATA |
636dff67 | 308 | && sym->attr.in_common |
524af0d6 | 309 | && !gfc_notify_std (GFC_STD_GNU, "initialization of " |
a4d9b221 | 310 | "common block variable %qs in DATA statement at %C", |
524af0d6 | 311 | sym->name)) |
4075a94e | 312 | return MATCH_ERROR; |
294fbfc8 | 313 | |
524af0d6 | 314 | if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where)) |
294fbfc8 TS |
315 | return MATCH_ERROR; |
316 | ||
317 | return MATCH_YES; | |
318 | } | |
319 | ||
320 | ||
321 | /* Match the top-level list of data variables. */ | |
322 | ||
323 | static match | |
636dff67 | 324 | top_var_list (gfc_data *d) |
294fbfc8 | 325 | { |
7b901ac4 | 326 | gfc_data_variable var, *tail, *new_var; |
294fbfc8 TS |
327 | match m; |
328 | ||
329 | tail = NULL; | |
330 | ||
331 | for (;;) | |
332 | { | |
333 | m = var_element (&var); | |
334 | if (m == MATCH_NO) | |
335 | goto syntax; | |
336 | if (m == MATCH_ERROR) | |
337 | return MATCH_ERROR; | |
338 | ||
7b901ac4 KG |
339 | new_var = gfc_get_data_variable (); |
340 | *new_var = var; | |
bebf94af SK |
341 | if (new_var->expr) |
342 | new_var->expr->where = gfc_current_locus; | |
294fbfc8 TS |
343 | |
344 | if (tail == NULL) | |
7b901ac4 | 345 | d->var = new_var; |
294fbfc8 | 346 | else |
7b901ac4 | 347 | tail->next = new_var; |
294fbfc8 | 348 | |
7b901ac4 | 349 | tail = new_var; |
294fbfc8 TS |
350 | |
351 | if (gfc_match_char ('/') == MATCH_YES) | |
352 | break; | |
353 | if (gfc_match_char (',') != MATCH_YES) | |
354 | goto syntax; | |
355 | } | |
356 | ||
357 | return MATCH_YES; | |
358 | ||
359 | syntax: | |
360 | gfc_syntax_error (ST_DATA); | |
a9f6f1f2 | 361 | gfc_free_data_all (gfc_current_ns); |
294fbfc8 TS |
362 | return MATCH_ERROR; |
363 | } | |
364 | ||
365 | ||
366 | static match | |
636dff67 | 367 | match_data_constant (gfc_expr **result) |
294fbfc8 TS |
368 | { |
369 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
c3f34952 | 370 | gfc_symbol *sym, *dt_sym = NULL; |
294fbfc8 TS |
371 | gfc_expr *expr; |
372 | match m; | |
36d3fb4c | 373 | locus old_loc; |
294fbfc8 TS |
374 | |
375 | m = gfc_match_literal_constant (&expr, 1); | |
376 | if (m == MATCH_YES) | |
377 | { | |
378 | *result = expr; | |
379 | return MATCH_YES; | |
380 | } | |
381 | ||
382 | if (m == MATCH_ERROR) | |
383 | return MATCH_ERROR; | |
384 | ||
385 | m = gfc_match_null (result); | |
386 | if (m != MATCH_NO) | |
387 | return m; | |
388 | ||
36d3fb4c PT |
389 | old_loc = gfc_current_locus; |
390 | ||
391 | /* Should this be a structure component, try to match it | |
392 | before matching a name. */ | |
393 | m = gfc_match_rvalue (result); | |
394 | if (m == MATCH_ERROR) | |
395 | return m; | |
396 | ||
397 | if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE) | |
398 | { | |
524af0d6 | 399 | if (!gfc_simplify_expr (*result, 0)) |
36d3fb4c PT |
400 | m = MATCH_ERROR; |
401 | return m; | |
402 | } | |
46f4f794 | 403 | else if (m == MATCH_YES) |
b6e841a6 | 404 | { |
19adb97a SK |
405 | /* If a parameter inquiry ends up here, symtree is NULL but **result |
406 | contains the right constant expression. Check here. */ | |
407 | if ((*result)->symtree == NULL | |
408 | && (*result)->expr_type == EXPR_CONSTANT | |
0a524296 | 409 | && ((*result)->ts.type == BT_INTEGER |
19adb97a SK |
410 | || (*result)->ts.type == BT_REAL)) |
411 | return m; | |
412 | ||
b6e841a6 SK |
413 | /* F2018:R845 data-stmt-constant is initial-data-target. |
414 | A data-stmt-constant shall be ... initial-data-target if and | |
415 | only if the corresponding data-stmt-object has the POINTER | |
416 | attribute. ... If data-stmt-constant is initial-data-target | |
417 | the corresponding data statement object shall be | |
418 | data-pointer-initialization compatible (7.5.4.6) with the initial | |
419 | data target; the data statement object is initially associated | |
420 | with the target. */ | |
421 | if ((*result)->symtree->n.sym->attr.save | |
422 | && (*result)->symtree->n.sym->attr.target) | |
423 | return m; | |
424 | gfc_free_expr (*result); | |
425 | } | |
36d3fb4c PT |
426 | |
427 | gfc_current_locus = old_loc; | |
428 | ||
294fbfc8 TS |
429 | m = gfc_match_name (name); |
430 | if (m != MATCH_YES) | |
431 | return m; | |
432 | ||
433 | if (gfc_find_symbol (name, NULL, 1, &sym)) | |
434 | return MATCH_ERROR; | |
435 | ||
c3f34952 TB |
436 | if (sym && sym->attr.generic) |
437 | dt_sym = gfc_find_dt_in_generic (sym); | |
438 | ||
294fbfc8 | 439 | if (sym == NULL |
c3f34952 | 440 | || (sym->attr.flavor != FL_PARAMETER |
f6288c24 | 441 | && (!dt_sym || !gfc_fl_struct (dt_sym->attr.flavor)))) |
294fbfc8 | 442 | { |
c4100eae | 443 | gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C", |
294fbfc8 | 444 | name); |
89f1f37e | 445 | *result = NULL; |
294fbfc8 TS |
446 | return MATCH_ERROR; |
447 | } | |
f6288c24 | 448 | else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor)) |
c3f34952 | 449 | return gfc_match_structure_constructor (dt_sym, result); |
294fbfc8 | 450 | |
d46e0870 JD |
451 | /* Check to see if the value is an initialization array expression. */ |
452 | if (sym->value->expr_type == EXPR_ARRAY) | |
453 | { | |
454 | gfc_current_locus = old_loc; | |
455 | ||
456 | m = gfc_match_init_expr (result); | |
457 | if (m == MATCH_ERROR) | |
458 | return m; | |
459 | ||
460 | if (m == MATCH_YES) | |
461 | { | |
524af0d6 | 462 | if (!gfc_simplify_expr (*result, 0)) |
d46e0870 JD |
463 | m = MATCH_ERROR; |
464 | ||
465 | if ((*result)->expr_type == EXPR_CONSTANT) | |
466 | return m; | |
467 | else | |
468 | { | |
469 | gfc_error ("Invalid initializer %s in Data statement at %C", name); | |
470 | return MATCH_ERROR; | |
471 | } | |
472 | } | |
473 | } | |
474 | ||
294fbfc8 TS |
475 | *result = gfc_copy_expr (sym->value); |
476 | return MATCH_YES; | |
477 | } | |
478 | ||
479 | ||
480 | /* Match a list of values in a DATA statement. The leading '/' has | |
481 | already been seen at this point. */ | |
482 | ||
483 | static match | |
636dff67 | 484 | top_val_list (gfc_data *data) |
294fbfc8 | 485 | { |
7b901ac4 | 486 | gfc_data_value *new_val, *tail; |
294fbfc8 | 487 | gfc_expr *expr; |
294fbfc8 TS |
488 | match m; |
489 | ||
490 | tail = NULL; | |
491 | ||
492 | for (;;) | |
493 | { | |
494 | m = match_data_constant (&expr); | |
495 | if (m == MATCH_NO) | |
496 | goto syntax; | |
497 | if (m == MATCH_ERROR) | |
498 | return MATCH_ERROR; | |
499 | ||
7b901ac4 KG |
500 | new_val = gfc_get_data_value (); |
501 | mpz_init (new_val->repeat); | |
294fbfc8 TS |
502 | |
503 | if (tail == NULL) | |
7b901ac4 | 504 | data->value = new_val; |
294fbfc8 | 505 | else |
7b901ac4 | 506 | tail->next = new_val; |
294fbfc8 | 507 | |
7b901ac4 | 508 | tail = new_val; |
294fbfc8 TS |
509 | |
510 | if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES) | |
511 | { | |
512 | tail->expr = expr; | |
f2112868 | 513 | mpz_set_ui (tail->repeat, 1); |
294fbfc8 TS |
514 | } |
515 | else | |
516 | { | |
46f4f794 | 517 | mpz_set (tail->repeat, expr->value.integer); |
294fbfc8 | 518 | gfc_free_expr (expr); |
294fbfc8 TS |
519 | |
520 | m = match_data_constant (&tail->expr); | |
521 | if (m == MATCH_NO) | |
522 | goto syntax; | |
523 | if (m == MATCH_ERROR) | |
524 | return MATCH_ERROR; | |
525 | } | |
526 | ||
527 | if (gfc_match_char ('/') == MATCH_YES) | |
528 | break; | |
529 | if (gfc_match_char (',') == MATCH_NO) | |
530 | goto syntax; | |
531 | } | |
532 | ||
533 | return MATCH_YES; | |
534 | ||
535 | syntax: | |
536 | gfc_syntax_error (ST_DATA); | |
a9f6f1f2 | 537 | gfc_free_data_all (gfc_current_ns); |
294fbfc8 TS |
538 | return MATCH_ERROR; |
539 | } | |
540 | ||
541 | ||
542 | /* Matches an old style initialization. */ | |
543 | ||
544 | static match | |
545 | match_old_style_init (const char *name) | |
546 | { | |
547 | match m; | |
548 | gfc_symtree *st; | |
ed0e3607 | 549 | gfc_symbol *sym; |
294fbfc8 TS |
550 | gfc_data *newdata; |
551 | ||
552 | /* Set up data structure to hold initializers. */ | |
553 | gfc_find_sym_tree (name, NULL, 0, &st); | |
ed0e3607 AL |
554 | sym = st->n.sym; |
555 | ||
294fbfc8 TS |
556 | newdata = gfc_get_data (); |
557 | newdata->var = gfc_get_data_variable (); | |
558 | newdata->var->expr = gfc_get_variable_expr (st); | |
e11449d1 | 559 | newdata->var->expr->where = sym->declared_at; |
8c5c0b80 | 560 | newdata->where = gfc_current_locus; |
294fbfc8 | 561 | |
66e4ab31 | 562 | /* Match initial value list. This also eats the terminal '/'. */ |
294fbfc8 TS |
563 | m = top_val_list (newdata); |
564 | if (m != MATCH_YES) | |
565 | { | |
cede9502 | 566 | free (newdata); |
294fbfc8 TS |
567 | return m; |
568 | } | |
569 | ||
570 | if (gfc_pure (NULL)) | |
571 | { | |
572 | gfc_error ("Initialization at %C is not allowed in a PURE procedure"); | |
cede9502 | 573 | free (newdata); |
294fbfc8 TS |
574 | return MATCH_ERROR; |
575 | } | |
ccd7751b | 576 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
f1f39033 | 577 | |
ed0e3607 | 578 | /* Mark the variable as having appeared in a data statement. */ |
524af0d6 | 579 | if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at)) |
ed0e3607 | 580 | { |
cede9502 | 581 | free (newdata); |
ed0e3607 AL |
582 | return MATCH_ERROR; |
583 | } | |
584 | ||
294fbfc8 TS |
585 | /* Chain in namespace list of DATA initializers. */ |
586 | newdata->next = gfc_current_ns->data; | |
587 | gfc_current_ns->data = newdata; | |
588 | ||
589 | return m; | |
590 | } | |
591 | ||
636dff67 | 592 | |
294fbfc8 | 593 | /* Match the stuff following a DATA statement. If ERROR_FLAG is set, |
13795658 | 594 | we are matching a DATA statement and are therefore issuing an error |
d51347f9 | 595 | if we encounter something unexpected, if not, we're trying to match |
69de3b83 | 596 | an old-style initialization expression of the form INTEGER I /2/. */ |
294fbfc8 TS |
597 | |
598 | match | |
599 | gfc_match_data (void) | |
600 | { | |
7b901ac4 | 601 | gfc_data *new_data; |
02543f02 | 602 | gfc_expr *e; |
bebf94af | 603 | gfc_ref *ref; |
294fbfc8 TS |
604 | match m; |
605 | ||
5f0ba745 SK |
606 | /* Before parsing the rest of a DATA statement, check F2008:c1206. */ |
607 | if ((gfc_current_state () == COMP_FUNCTION | |
608 | || gfc_current_state () == COMP_SUBROUTINE) | |
609 | && gfc_state_stack->previous->state == COMP_INTERFACE) | |
610 | { | |
611 | gfc_error ("DATA statement at %C cannot appear within an INTERFACE"); | |
612 | return MATCH_ERROR; | |
613 | } | |
614 | ||
ca39e6f2 | 615 | set_in_match_data (true); |
2220652d | 616 | |
294fbfc8 TS |
617 | for (;;) |
618 | { | |
7b901ac4 KG |
619 | new_data = gfc_get_data (); |
620 | new_data->where = gfc_current_locus; | |
294fbfc8 | 621 | |
7b901ac4 | 622 | m = top_var_list (new_data); |
294fbfc8 TS |
623 | if (m != MATCH_YES) |
624 | goto cleanup; | |
625 | ||
c034c38f SK |
626 | if (new_data->var->iter.var |
627 | && new_data->var->iter.var->ts.type == BT_INTEGER | |
628 | && new_data->var->iter.var->symtree->n.sym->attr.implied_index == 1 | |
629 | && new_data->var->list | |
630 | && new_data->var->list->expr | |
631 | && new_data->var->list->expr->ts.type == BT_CHARACTER | |
632 | && new_data->var->list->expr->ref | |
633 | && new_data->var->list->expr->ref->type == REF_SUBSTRING) | |
634 | { | |
635 | gfc_error ("Invalid substring in data-implied-do at %L in DATA " | |
636 | "statement", &new_data->var->list->expr->where); | |
637 | goto cleanup; | |
638 | } | |
639 | ||
02543f02 SK |
640 | /* Check for an entity with an allocatable component, which is not |
641 | allowed. */ | |
642 | e = new_data->var->expr; | |
643 | if (e) | |
644 | { | |
645 | bool invalid; | |
646 | ||
647 | invalid = false; | |
bebf94af | 648 | for (ref = e->ref; ref; ref = ref->next) |
02543f02 SK |
649 | if ((ref->type == REF_COMPONENT |
650 | && ref->u.c.component->attr.allocatable) | |
651 | || (ref->type == REF_ARRAY | |
652 | && e->symtree->n.sym->attr.pointer != 1 | |
653 | && ref->u.ar.as && ref->u.ar.as->type == AS_DEFERRED)) | |
654 | invalid = true; | |
655 | ||
656 | if (invalid) | |
657 | { | |
658 | gfc_error ("Allocatable component or deferred-shaped array " | |
659 | "near %C in DATA statement"); | |
660 | goto cleanup; | |
661 | } | |
bebf94af SK |
662 | |
663 | /* F2008:C567 (R536) A data-i-do-object or a variable that appears | |
664 | as a data-stmt-object shall not be an object designator in which | |
665 | a pointer appears other than as the entire rightmost part-ref. */ | |
666 | ref = e->ref; | |
667 | if (e->symtree->n.sym->ts.type == BT_DERIVED | |
668 | && e->symtree->n.sym->attr.pointer | |
669 | && ref->type == REF_COMPONENT) | |
670 | goto partref; | |
671 | ||
672 | for (; ref; ref = ref->next) | |
673 | if (ref->type == REF_COMPONENT | |
674 | && ref->u.c.component->attr.pointer | |
675 | && ref->next) | |
676 | goto partref; | |
02543f02 SK |
677 | } |
678 | ||
7b901ac4 | 679 | m = top_val_list (new_data); |
294fbfc8 TS |
680 | if (m != MATCH_YES) |
681 | goto cleanup; | |
682 | ||
7b901ac4 KG |
683 | new_data->next = gfc_current_ns->data; |
684 | gfc_current_ns->data = new_data; | |
294fbfc8 TS |
685 | |
686 | if (gfc_match_eos () == MATCH_YES) | |
687 | break; | |
688 | ||
689 | gfc_match_char (','); /* Optional comma */ | |
690 | } | |
691 | ||
ca39e6f2 | 692 | set_in_match_data (false); |
2220652d | 693 | |
294fbfc8 TS |
694 | if (gfc_pure (NULL)) |
695 | { | |
696 | gfc_error ("DATA statement at %C is not allowed in a PURE procedure"); | |
697 | return MATCH_ERROR; | |
698 | } | |
ccd7751b | 699 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
f1f39033 | 700 | |
294fbfc8 TS |
701 | return MATCH_YES; |
702 | ||
bebf94af SK |
703 | partref: |
704 | ||
705 | gfc_error ("part-ref with pointer attribute near %L is not " | |
706 | "rightmost part-ref of data-stmt-object", | |
707 | &e->where); | |
708 | ||
294fbfc8 | 709 | cleanup: |
ca39e6f2 | 710 | set_in_match_data (false); |
7b901ac4 | 711 | gfc_free_data (new_data); |
294fbfc8 TS |
712 | return MATCH_ERROR; |
713 | } | |
714 | ||
715 | ||
716 | /************************ Declaration statements *********************/ | |
717 | ||
d3a9eea2 | 718 | |
f6288c24 FR |
719 | /* Like gfc_match_init_expr, but matches a 'clist' (old-style initialization |
720 | list). The difference here is the expression is a list of constants | |
6442a6f4 | 721 | and is surrounded by '/'. |
f6288c24 FR |
722 | The typespec ts must match the typespec of the variable which the |
723 | clist is initializing. | |
6442a6f4 | 724 | The arrayspec tells whether this should match a list of constants |
f6288c24 FR |
725 | corresponding to array elements or a scalar (as == NULL). */ |
726 | ||
727 | static match | |
728 | match_clist_expr (gfc_expr **result, gfc_typespec *ts, gfc_array_spec *as) | |
729 | { | |
730 | gfc_constructor_base array_head = NULL; | |
731 | gfc_expr *expr = NULL; | |
e11449d1 | 732 | match m = MATCH_ERROR; |
f6288c24 | 733 | locus where; |
9b24c104 | 734 | mpz_t repeat, cons_size, as_size; |
f6288c24 FR |
735 | bool scalar; |
736 | int cmp; | |
737 | ||
738 | gcc_assert (ts); | |
739 | ||
f6288c24 FR |
740 | /* We have already matched '/' - now look for a constant list, as with |
741 | top_val_list from decl.c, but append the result to an array. */ | |
742 | if (gfc_match ("/") == MATCH_YES) | |
743 | { | |
744 | gfc_error ("Empty old style initializer list at %C"); | |
e11449d1 | 745 | return MATCH_ERROR; |
f6288c24 FR |
746 | } |
747 | ||
748 | where = gfc_current_locus; | |
e11449d1 FR |
749 | scalar = !as || !as->rank; |
750 | ||
751 | if (!scalar && !spec_size (as, &as_size)) | |
752 | { | |
753 | gfc_error ("Array in initializer list at %L must have an explicit shape", | |
754 | as->type == AS_EXPLICIT ? &as->upper[0]->where : &where); | |
755 | /* Nothing to cleanup yet. */ | |
756 | return MATCH_ERROR; | |
757 | } | |
758 | ||
759 | mpz_init_set_ui (repeat, 0); | |
760 | ||
f6288c24 FR |
761 | for (;;) |
762 | { | |
763 | m = match_data_constant (&expr); | |
764 | if (m != MATCH_YES) | |
765 | expr = NULL; /* match_data_constant may set expr to garbage */ | |
766 | if (m == MATCH_NO) | |
767 | goto syntax; | |
768 | if (m == MATCH_ERROR) | |
769 | goto cleanup; | |
770 | ||
771 | /* Found r in repeat spec r*c; look for the constant to repeat. */ | |
772 | if ( gfc_match_char ('*') == MATCH_YES) | |
773 | { | |
774 | if (scalar) | |
775 | { | |
776 | gfc_error ("Repeat spec invalid in scalar initializer at %C"); | |
777 | goto cleanup; | |
778 | } | |
779 | if (expr->ts.type != BT_INTEGER) | |
780 | { | |
781 | gfc_error ("Repeat spec must be an integer at %C"); | |
782 | goto cleanup; | |
783 | } | |
784 | mpz_set (repeat, expr->value.integer); | |
785 | gfc_free_expr (expr); | |
786 | expr = NULL; | |
787 | ||
788 | m = match_data_constant (&expr); | |
789 | if (m == MATCH_NO) | |
e11449d1 FR |
790 | { |
791 | m = MATCH_ERROR; | |
792 | gfc_error ("Expected data constant after repeat spec at %C"); | |
793 | } | |
f6288c24 FR |
794 | if (m != MATCH_YES) |
795 | goto cleanup; | |
796 | } | |
797 | /* No repeat spec, we matched the data constant itself. */ | |
798 | else | |
799 | mpz_set_ui (repeat, 1); | |
800 | ||
801 | if (!scalar) | |
802 | { | |
803 | /* Add the constant initializer as many times as repeated. */ | |
804 | for (; mpz_cmp_ui (repeat, 0) > 0; mpz_sub_ui (repeat, repeat, 1)) | |
805 | { | |
806 | /* Make sure types of elements match */ | |
807 | if(ts && !gfc_compare_types (&expr->ts, ts) | |
808 | && !gfc_convert_type (expr, ts, 1)) | |
809 | goto cleanup; | |
810 | ||
811 | gfc_constructor_append_expr (&array_head, | |
812 | gfc_copy_expr (expr), &gfc_current_locus); | |
813 | } | |
814 | ||
815 | gfc_free_expr (expr); | |
816 | expr = NULL; | |
817 | } | |
818 | ||
819 | /* For scalar initializers quit after one element. */ | |
820 | else | |
821 | { | |
822 | if(gfc_match_char ('/') != MATCH_YES) | |
823 | { | |
824 | gfc_error ("End of scalar initializer expected at %C"); | |
825 | goto cleanup; | |
826 | } | |
827 | break; | |
828 | } | |
829 | ||
830 | if (gfc_match_char ('/') == MATCH_YES) | |
831 | break; | |
832 | if (gfc_match_char (',') == MATCH_NO) | |
833 | goto syntax; | |
834 | } | |
835 | ||
e11449d1 FR |
836 | /* If we break early from here out, we encountered an error. */ |
837 | m = MATCH_ERROR; | |
838 | ||
f6288c24 FR |
839 | /* Set up expr as an array constructor. */ |
840 | if (!scalar) | |
841 | { | |
842 | expr = gfc_get_array_expr (ts->type, ts->kind, &where); | |
843 | expr->ts = *ts; | |
844 | expr->value.constructor = array_head; | |
845 | ||
846 | expr->rank = as->rank; | |
847 | expr->shape = gfc_get_shape (expr->rank); | |
848 | ||
9b24c104 FR |
849 | /* Validate sizes. We built expr ourselves, so cons_size will be |
850 | constant (we fail above for non-constant expressions). | |
e11449d1 | 851 | We still need to verify that the sizes match. */ |
9b24c104 | 852 | gcc_assert (gfc_array_size (expr, &cons_size)); |
e11449d1 FR |
853 | cmp = mpz_cmp (cons_size, as_size); |
854 | if (cmp < 0) | |
855 | gfc_error ("Not enough elements in array initializer at %C"); | |
856 | else if (cmp > 0) | |
857 | gfc_error ("Too many elements in array initializer at %C"); | |
9b24c104 | 858 | mpz_clear (cons_size); |
f6288c24 | 859 | if (cmp) |
9b24c104 | 860 | goto cleanup; |
f6288c24 FR |
861 | } |
862 | ||
863 | /* Make sure scalar types match. */ | |
864 | else if (!gfc_compare_types (&expr->ts, ts) | |
865 | && !gfc_convert_type (expr, ts, 1)) | |
866 | goto cleanup; | |
867 | ||
868 | if (expr->ts.u.cl) | |
869 | expr->ts.u.cl->length_from_typespec = 1; | |
870 | ||
871 | *result = expr; | |
e11449d1 FR |
872 | m = MATCH_YES; |
873 | goto done; | |
f6288c24 FR |
874 | |
875 | syntax: | |
e11449d1 | 876 | m = MATCH_ERROR; |
f6288c24 FR |
877 | gfc_error ("Syntax error in old style initializer list at %C"); |
878 | ||
879 | cleanup: | |
880 | if (expr) | |
881 | expr->value.constructor = NULL; | |
882 | gfc_free_expr (expr); | |
883 | gfc_constructor_free (array_head); | |
e11449d1 FR |
884 | |
885 | done: | |
f6288c24 | 886 | mpz_clear (repeat); |
e11449d1 FR |
887 | if (!scalar) |
888 | mpz_clear (as_size); | |
889 | return m; | |
f6288c24 FR |
890 | } |
891 | ||
892 | ||
eea58adb | 893 | /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */ |
d3a9eea2 | 894 | |
524af0d6 | 895 | static bool |
d3a9eea2 TB |
896 | merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy) |
897 | { | |
93d1ab50 | 898 | int i, j; |
d3a9eea2 | 899 | |
63fbf586 TB |
900 | if ((from->type == AS_ASSUMED_RANK && to->corank) |
901 | || (to->type == AS_ASSUMED_RANK && from->corank)) | |
902 | { | |
903 | gfc_error ("The assumed-rank array at %C shall not have a codimension"); | |
524af0d6 | 904 | return false; |
63fbf586 | 905 | } |
c62c6622 | 906 | |
d3a9eea2 TB |
907 | if (to->rank == 0 && from->rank > 0) |
908 | { | |
909 | to->rank = from->rank; | |
910 | to->type = from->type; | |
911 | to->cray_pointee = from->cray_pointee; | |
912 | to->cp_was_assumed = from->cp_was_assumed; | |
913 | ||
914 | for (i = 0; i < to->corank; i++) | |
915 | { | |
93d1ab50 SK |
916 | /* Do not exceed the limits on lower[] and upper[]. gfortran |
917 | cleans up elsewhere. */ | |
918 | j = from->rank + i; | |
919 | if (j >= GFC_MAX_DIMENSIONS) | |
920 | break; | |
921 | ||
922 | to->lower[j] = to->lower[i]; | |
923 | to->upper[j] = to->upper[i]; | |
d3a9eea2 TB |
924 | } |
925 | for (i = 0; i < from->rank; i++) | |
926 | { | |
927 | if (copy) | |
928 | { | |
929 | to->lower[i] = gfc_copy_expr (from->lower[i]); | |
930 | to->upper[i] = gfc_copy_expr (from->upper[i]); | |
931 | } | |
932 | else | |
933 | { | |
934 | to->lower[i] = from->lower[i]; | |
935 | to->upper[i] = from->upper[i]; | |
936 | } | |
937 | } | |
938 | } | |
939 | else if (to->corank == 0 && from->corank > 0) | |
940 | { | |
941 | to->corank = from->corank; | |
942 | to->cotype = from->cotype; | |
943 | ||
944 | for (i = 0; i < from->corank; i++) | |
945 | { | |
93d1ab50 SK |
946 | /* Do not exceed the limits on lower[] and upper[]. gfortran |
947 | cleans up elsewhere. */ | |
948 | j = to->rank + i; | |
949 | if (j >= GFC_MAX_DIMENSIONS) | |
950 | break; | |
951 | ||
d3a9eea2 TB |
952 | if (copy) |
953 | { | |
93d1ab50 SK |
954 | to->lower[j] = gfc_copy_expr (from->lower[i]); |
955 | to->upper[j] = gfc_copy_expr (from->upper[i]); | |
d3a9eea2 TB |
956 | } |
957 | else | |
958 | { | |
93d1ab50 SK |
959 | to->lower[j] = from->lower[i]; |
960 | to->upper[j] = from->upper[i]; | |
d3a9eea2 TB |
961 | } |
962 | } | |
963 | } | |
63fbf586 | 964 | |
299ab1b2 | 965 | if (to->rank + to->corank > GFC_MAX_DIMENSIONS) |
93d1ab50 SK |
966 | { |
967 | gfc_error ("Sum of array rank %d and corank %d at %C exceeds maximum " | |
968 | "allowed dimensions of %d", | |
969 | to->rank, to->corank, GFC_MAX_DIMENSIONS); | |
970 | to->corank = GFC_MAX_DIMENSIONS - to->rank; | |
971 | return false; | |
972 | } | |
524af0d6 | 973 | return true; |
d3a9eea2 TB |
974 | } |
975 | ||
976 | ||
6de9cd9a DN |
977 | /* Match an intent specification. Since this can only happen after an |
978 | INTENT word, a legal intent-spec must follow. */ | |
979 | ||
980 | static sym_intent | |
981 | match_intent_spec (void) | |
982 | { | |
983 | ||
984 | if (gfc_match (" ( in out )") == MATCH_YES) | |
985 | return INTENT_INOUT; | |
986 | if (gfc_match (" ( in )") == MATCH_YES) | |
987 | return INTENT_IN; | |
988 | if (gfc_match (" ( out )") == MATCH_YES) | |
989 | return INTENT_OUT; | |
990 | ||
991 | gfc_error ("Bad INTENT specification at %C"); | |
992 | return INTENT_UNKNOWN; | |
993 | } | |
994 | ||
995 | ||
996 | /* Matches a character length specification, which is either a | |
e69afb29 | 997 | specification expression, '*', or ':'. */ |
6de9cd9a DN |
998 | |
999 | static match | |
e69afb29 | 1000 | char_len_param_value (gfc_expr **expr, bool *deferred) |
6de9cd9a | 1001 | { |
cba28dad JD |
1002 | match m; |
1003 | ||
e69afb29 SK |
1004 | *expr = NULL; |
1005 | *deferred = false; | |
1006 | ||
6de9cd9a | 1007 | if (gfc_match_char ('*') == MATCH_YES) |
e69afb29 SK |
1008 | return MATCH_YES; |
1009 | ||
1010 | if (gfc_match_char (':') == MATCH_YES) | |
6de9cd9a | 1011 | { |
98a819ea | 1012 | if (!gfc_notify_std (GFC_STD_F2003, "deferred type parameter at %C")) |
e69afb29 SK |
1013 | return MATCH_ERROR; |
1014 | ||
1015 | *deferred = true; | |
1016 | ||
6de9cd9a DN |
1017 | return MATCH_YES; |
1018 | } | |
1019 | ||
cba28dad | 1020 | m = gfc_match_expr (expr); |
f37e928c | 1021 | |
98a819ea SK |
1022 | if (m == MATCH_NO || m == MATCH_ERROR) |
1023 | return m; | |
1024 | ||
1025 | if (!gfc_expr_check_typed (*expr, gfc_current_ns, false)) | |
f37e928c DK |
1026 | return MATCH_ERROR; |
1027 | ||
98a819ea | 1028 | if ((*expr)->expr_type == EXPR_FUNCTION) |
cba28dad | 1029 | { |
8d48826b SK |
1030 | if ((*expr)->ts.type == BT_INTEGER |
1031 | || ((*expr)->ts.type == BT_UNKNOWN | |
1032 | && strcmp((*expr)->symtree->name, "null") != 0)) | |
1033 | return MATCH_YES; | |
1034 | ||
1035 | goto syntax; | |
1036 | } | |
1037 | else if ((*expr)->expr_type == EXPR_CONSTANT) | |
1038 | { | |
1039 | /* F2008, 4.4.3.1: The length is a type parameter; its kind is | |
1040 | processor dependent and its value is greater than or equal to zero. | |
1041 | F2008, 4.4.3.2: If the character length parameter value evaluates | |
1042 | to a negative value, the length of character entities declared | |
1043 | is zero. */ | |
1044 | ||
1045 | if ((*expr)->ts.type == BT_INTEGER) | |
cba28dad | 1046 | { |
8d48826b SK |
1047 | if (mpz_cmp_si ((*expr)->value.integer, 0) < 0) |
1048 | mpz_set_si ((*expr)->value.integer, 0); | |
cba28dad | 1049 | } |
8d48826b SK |
1050 | else |
1051 | goto syntax; | |
cba28dad | 1052 | } |
8d48826b SK |
1053 | else if ((*expr)->expr_type == EXPR_ARRAY) |
1054 | goto syntax; | |
1055 | else if ((*expr)->expr_type == EXPR_VARIABLE) | |
1056 | { | |
fb42421e | 1057 | bool t; |
8d48826b SK |
1058 | gfc_expr *e; |
1059 | ||
1060 | e = gfc_copy_expr (*expr); | |
1061 | ||
1062 | /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']", | |
1063 | which causes an ICE if gfc_reduce_init_expr() is called. */ | |
54b96a2d SK |
1064 | if (e->ref && e->ref->type == REF_ARRAY |
1065 | && e->ref->u.ar.type == AR_UNKNOWN | |
8d48826b SK |
1066 | && e->ref->u.ar.dimen_type[0] == DIMEN_RANGE) |
1067 | goto syntax; | |
1068 | ||
fb42421e SK |
1069 | t = gfc_reduce_init_expr (e); |
1070 | ||
8d987deb SK |
1071 | if (!t && e->ts.type == BT_UNKNOWN |
1072 | && e->symtree->n.sym->attr.untyped == 1 | |
63ac6251 TK |
1073 | && (flag_implicit_none |
1074 | || e->symtree->n.sym->ns->seen_implicit_none == 1 | |
8d987deb | 1075 | || e->symtree->n.sym->ns->parent->seen_implicit_none == 1)) |
fb42421e SK |
1076 | { |
1077 | gfc_free_expr (e); | |
1078 | goto syntax; | |
1079 | } | |
98a819ea | 1080 | |
54b96a2d | 1081 | if ((e->ref && e->ref->type == REF_ARRAY |
70112e2a | 1082 | && e->ref->u.ar.type != AR_ELEMENT) |
8d48826b SK |
1083 | || (!e->ref && e->expr_type == EXPR_ARRAY)) |
1084 | { | |
1085 | gfc_free_expr (e); | |
1086 | goto syntax; | |
1087 | } | |
1088 | ||
1089 | gfc_free_expr (e); | |
1090 | } | |
98a819ea | 1091 | |
cba28dad JD |
1092 | return m; |
1093 | ||
1094 | syntax: | |
8d48826b | 1095 | gfc_error ("Scalar INTEGER expression expected at %L", &(*expr)->where); |
cba28dad | 1096 | return MATCH_ERROR; |
6de9cd9a DN |
1097 | } |
1098 | ||
1099 | ||
1100 | /* A character length is a '*' followed by a literal integer or a | |
1101 | char_len_param_value in parenthesis. */ | |
1102 | ||
1103 | static match | |
62732c30 | 1104 | match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check) |
6de9cd9a | 1105 | { |
5cf54585 | 1106 | int length; |
6de9cd9a DN |
1107 | match m; |
1108 | ||
f5acf0f2 | 1109 | *deferred = false; |
6de9cd9a DN |
1110 | m = gfc_match_char ('*'); |
1111 | if (m != MATCH_YES) | |
1112 | return m; | |
1113 | ||
5cf54585 | 1114 | m = gfc_match_small_literal_int (&length, NULL); |
6de9cd9a DN |
1115 | if (m == MATCH_ERROR) |
1116 | return m; | |
1117 | ||
1118 | if (m == MATCH_YES) | |
1119 | { | |
62732c30 | 1120 | if (obsolescent_check |
524af0d6 | 1121 | && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C")) |
e2ab8b09 | 1122 | return MATCH_ERROR; |
f622221a | 1123 | *expr = gfc_get_int_expr (gfc_charlen_int_kind, NULL, length); |
6de9cd9a DN |
1124 | return m; |
1125 | } | |
1126 | ||
1127 | if (gfc_match_char ('(') == MATCH_NO) | |
1128 | goto syntax; | |
1129 | ||
e69afb29 | 1130 | m = char_len_param_value (expr, deferred); |
1c8bcdf7 PT |
1131 | if (m != MATCH_YES && gfc_matching_function) |
1132 | { | |
1133 | gfc_undo_symbols (); | |
1134 | m = MATCH_YES; | |
1135 | } | |
1136 | ||
6de9cd9a DN |
1137 | if (m == MATCH_ERROR) |
1138 | return m; | |
1139 | if (m == MATCH_NO) | |
1140 | goto syntax; | |
1141 | ||
1142 | if (gfc_match_char (')') == MATCH_NO) | |
1143 | { | |
1144 | gfc_free_expr (*expr); | |
1145 | *expr = NULL; | |
1146 | goto syntax; | |
1147 | } | |
1148 | ||
1149 | return MATCH_YES; | |
1150 | ||
1151 | syntax: | |
1152 | gfc_error ("Syntax error in character length specification at %C"); | |
1153 | return MATCH_ERROR; | |
1154 | } | |
1155 | ||
1156 | ||
9e35b386 EE |
1157 | /* Special subroutine for finding a symbol. Check if the name is found |
1158 | in the current name space. If not, and we're compiling a function or | |
1159 | subroutine and the parent compilation unit is an interface, then check | |
1160 | to see if the name we've been given is the name of the interface | |
1161 | (located in another namespace). */ | |
6de9cd9a DN |
1162 | |
1163 | static int | |
08a6b8e0 | 1164 | find_special (const char *name, gfc_symbol **result, bool allow_subroutine) |
6de9cd9a DN |
1165 | { |
1166 | gfc_state_data *s; | |
08a6b8e0 | 1167 | gfc_symtree *st; |
9e35b386 | 1168 | int i; |
6de9cd9a | 1169 | |
08a6b8e0 | 1170 | i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine); |
d51347f9 | 1171 | if (i == 0) |
08a6b8e0 TB |
1172 | { |
1173 | *result = st ? st->n.sym : NULL; | |
1174 | goto end; | |
1175 | } | |
d51347f9 | 1176 | |
6de9cd9a DN |
1177 | if (gfc_current_state () != COMP_SUBROUTINE |
1178 | && gfc_current_state () != COMP_FUNCTION) | |
9e35b386 | 1179 | goto end; |
6de9cd9a DN |
1180 | |
1181 | s = gfc_state_stack->previous; | |
1182 | if (s == NULL) | |
9e35b386 | 1183 | goto end; |
6de9cd9a DN |
1184 | |
1185 | if (s->state != COMP_INTERFACE) | |
9e35b386 | 1186 | goto end; |
6de9cd9a | 1187 | if (s->sym == NULL) |
66e4ab31 | 1188 | goto end; /* Nameless interface. */ |
6de9cd9a DN |
1189 | |
1190 | if (strcmp (name, s->sym->name) == 0) | |
1191 | { | |
1192 | *result = s->sym; | |
1193 | return 0; | |
1194 | } | |
1195 | ||
9e35b386 EE |
1196 | end: |
1197 | return i; | |
6de9cd9a DN |
1198 | } |
1199 | ||
1200 | ||
1201 | /* Special subroutine for getting a symbol node associated with a | |
1202 | procedure name, used in SUBROUTINE and FUNCTION statements. The | |
1203 | symbol is created in the parent using with symtree node in the | |
1204 | child unit pointing to the symbol. If the current namespace has no | |
1205 | parent, then the symbol is just created in the current unit. */ | |
1206 | ||
1207 | static int | |
636dff67 | 1208 | get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry) |
6de9cd9a DN |
1209 | { |
1210 | gfc_symtree *st; | |
1211 | gfc_symbol *sym; | |
a7ca4d8d | 1212 | int rc = 0; |
6de9cd9a | 1213 | |
1a492601 PT |
1214 | /* Module functions have to be left in their own namespace because |
1215 | they have potentially (almost certainly!) already been referenced. | |
1216 | In this sense, they are rather like external functions. This is | |
1217 | fixed up in resolve.c(resolve_entries), where the symbol name- | |
1218 | space is set to point to the master function, so that the fake | |
1219 | result mechanism can work. */ | |
1220 | if (module_fcn_entry) | |
6c12686b PT |
1221 | { |
1222 | /* Present if entry is declared to be a module procedure. */ | |
1223 | rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result); | |
aa84a9a5 | 1224 | |
6c12686b PT |
1225 | if (*result == NULL) |
1226 | rc = gfc_get_symbol (name, NULL, result); | |
2e32a71e | 1227 | else if (!gfc_get_symbol (name, NULL, &sym) && sym |
aa84a9a5 PT |
1228 | && (*result)->ts.type == BT_UNKNOWN |
1229 | && sym->attr.flavor == FL_UNKNOWN) | |
1230 | /* Pick up the typespec for the entry, if declared in the function | |
1231 | body. Note that this symbol is FL_UNKNOWN because it will | |
1232 | only have appeared in a type declaration. The local symtree | |
1233 | is set to point to the module symbol and a unique symtree | |
1234 | to the local version. This latter ensures a correct clearing | |
1235 | of the symbols. */ | |
2e32a71e PT |
1236 | { |
1237 | /* If the ENTRY proceeds its specification, we need to ensure | |
1238 | that this does not raise a "has no IMPLICIT type" error. */ | |
1239 | if (sym->ts.type == BT_UNKNOWN) | |
0e5a218b | 1240 | sym->attr.untyped = 1; |
2e32a71e | 1241 | |
0e5a218b | 1242 | (*result)->ts = sym->ts; |
2e32a71e PT |
1243 | |
1244 | /* Put the symbol in the procedure namespace so that, should | |
df2fba9e | 1245 | the ENTRY precede its specification, the specification |
2e32a71e PT |
1246 | can be applied. */ |
1247 | (*result)->ns = gfc_current_ns; | |
1248 | ||
1249 | gfc_find_sym_tree (name, gfc_current_ns, 0, &st); | |
1250 | st->n.sym = *result; | |
1251 | st = gfc_get_unique_symtree (gfc_current_ns); | |
2050626a | 1252 | sym->refs++; |
2e32a71e PT |
1253 | st->n.sym = sym; |
1254 | } | |
6c12686b | 1255 | } |
68ea355b PT |
1256 | else |
1257 | rc = gfc_get_symbol (name, gfc_current_ns->parent, result); | |
6de9cd9a | 1258 | |
a7ca4d8d PT |
1259 | if (rc) |
1260 | return rc; | |
1261 | ||
68ea355b | 1262 | sym = *result; |
79124116 PT |
1263 | if (sym->attr.proc == PROC_ST_FUNCTION) |
1264 | return rc; | |
6de9cd9a | 1265 | |
96c8b253 | 1266 | if (sym->attr.module_procedure && sym->attr.if_source == IFSRC_IFBODY) |
4668d6f9 PT |
1267 | { |
1268 | /* Create a partially populated interface symbol to carry the | |
1269 | characteristics of the procedure and the result. */ | |
c064374d | 1270 | sym->tlink = gfc_new_symbol (name, sym->ns); |
96c8b253 | 1271 | gfc_add_type (sym->tlink, &(sym->ts), &gfc_current_locus); |
c064374d | 1272 | gfc_copy_attr (&sym->tlink->attr, &sym->attr, NULL); |
4668d6f9 | 1273 | if (sym->attr.dimension) |
c064374d | 1274 | sym->tlink->as = gfc_copy_array_spec (sym->as); |
4668d6f9 PT |
1275 | |
1276 | /* Ideally, at this point, a copy would be made of the formal | |
1277 | arguments and their namespace. However, this does not appear | |
1278 | to be necessary, albeit at the expense of not being able to | |
1279 | use gfc_compare_interfaces directly. */ | |
1280 | ||
1281 | if (sym->result && sym->result != sym) | |
1282 | { | |
c064374d | 1283 | sym->tlink->result = sym->result; |
4668d6f9 PT |
1284 | sym->result = NULL; |
1285 | } | |
1286 | else if (sym->result) | |
1287 | { | |
c064374d | 1288 | sym->tlink->result = sym->tlink; |
4668d6f9 PT |
1289 | } |
1290 | } | |
1291 | else if (sym && !sym->gfc_new | |
1292 | && gfc_current_state () != COMP_INTERFACE) | |
68ea355b | 1293 | { |
cda7004b PT |
1294 | /* Trap another encompassed procedure with the same name. All |
1295 | these conditions are necessary to avoid picking up an entry | |
1296 | whose name clashes with that of the encompassing procedure; | |
2050626a | 1297 | this is handled using gsymbols to register unique, globally |
cda7004b | 1298 | accessible names. */ |
68ea355b | 1299 | if (sym->attr.flavor != 0 |
636dff67 | 1300 | && sym->attr.proc != 0 |
64300da7 | 1301 | && (sym->attr.subroutine || sym->attr.function || sym->attr.entry) |
636dff67 | 1302 | && sym->attr.if_source != IFSRC_UNKNOWN) |
b4439561 TB |
1303 | { |
1304 | gfc_error_now ("Procedure %qs at %C is already defined at %L", | |
1305 | name, &sym->declared_at); | |
1306 | return true; | |
1307 | } | |
64300da7 SK |
1308 | if (sym->attr.flavor != 0 |
1309 | && sym->attr.entry && sym->attr.if_source != IFSRC_UNKNOWN) | |
b4439561 TB |
1310 | { |
1311 | gfc_error_now ("Procedure %qs at %C is already defined at %L", | |
1312 | name, &sym->declared_at); | |
1313 | return true; | |
1314 | } | |
64300da7 | 1315 | |
81ea7c11 SK |
1316 | if (sym->attr.external && sym->attr.procedure |
1317 | && gfc_current_state () == COMP_CONTAINS) | |
b4439561 TB |
1318 | { |
1319 | gfc_error_now ("Contained procedure %qs at %C clashes with " | |
1320 | "procedure defined at %L", | |
1321 | name, &sym->declared_at); | |
1322 | return true; | |
1323 | } | |
81ea7c11 | 1324 | |
fd3e70af JD |
1325 | /* Trap a procedure with a name the same as interface in the |
1326 | encompassing scope. */ | |
1327 | if (sym->attr.generic != 0 | |
2305fa31 JD |
1328 | && (sym->attr.subroutine || sym->attr.function) |
1329 | && !sym->attr.mod_proc) | |
b4439561 TB |
1330 | { |
1331 | gfc_error_now ("Name %qs at %C is already defined" | |
1332 | " as a generic interface at %L", | |
1333 | name, &sym->declared_at); | |
1334 | return true; | |
1335 | } | |
fd3e70af | 1336 | |
68ea355b PT |
1337 | /* Trap declarations of attributes in encompassing scope. The |
1338 | signature for this is that ts.kind is set. Legitimate | |
1339 | references only set ts.type. */ | |
1340 | if (sym->ts.kind != 0 | |
636dff67 SK |
1341 | && !sym->attr.implicit_type |
1342 | && sym->attr.proc == 0 | |
1343 | && gfc_current_ns->parent != NULL | |
1344 | && sym->attr.access == 0 | |
1345 | && !module_fcn_entry) | |
b4439561 TB |
1346 | { |
1347 | gfc_error_now ("Procedure %qs at %C has an explicit interface " | |
96c8b253 | 1348 | "from a previous declaration", name); |
b4439561 TB |
1349 | return true; |
1350 | } | |
96c8b253 SK |
1351 | } |
1352 | ||
b74fa126 SK |
1353 | /* C1246 (R1225) MODULE shall appear only in the function-stmt or |
1354 | subroutine-stmt of a module subprogram or of a nonabstract interface | |
1355 | body that is declared in the scoping unit of a module or submodule. */ | |
1356 | if (sym->attr.external | |
1357 | && (sym->attr.subroutine || sym->attr.function) | |
1358 | && sym->attr.if_source == IFSRC_IFBODY | |
1359 | && !current_attr.module_procedure | |
1360 | && sym->attr.proc == PROC_MODULE | |
1361 | && gfc_state_stack->state == COMP_CONTAINS) | |
b4439561 TB |
1362 | { |
1363 | gfc_error_now ("Procedure %qs defined in interface body at %L " | |
1364 | "clashes with internal procedure defined at %C", | |
1365 | name, &sym->declared_at); | |
1366 | return true; | |
1367 | } | |
b74fa126 SK |
1368 | |
1369 | if (sym && !sym->gfc_new | |
1370 | && sym->attr.flavor != FL_UNKNOWN | |
1371 | && sym->attr.referenced == 0 && sym->attr.subroutine == 1 | |
1372 | && gfc_state_stack->state == COMP_CONTAINS | |
1373 | && gfc_state_stack->previous->state == COMP_SUBROUTINE) | |
b4439561 TB |
1374 | { |
1375 | gfc_error_now ("Procedure %qs at %C is already defined at %L", | |
1376 | name, &sym->declared_at); | |
1377 | return true; | |
1378 | } | |
68ea355b PT |
1379 | |
1380 | if (gfc_current_ns->parent == NULL || *result == NULL) | |
1381 | return rc; | |
6de9cd9a | 1382 | |
1a492601 PT |
1383 | /* Module function entries will already have a symtree in |
1384 | the current namespace but will need one at module level. */ | |
1385 | if (module_fcn_entry) | |
6c12686b PT |
1386 | { |
1387 | /* Present if entry is declared to be a module procedure. */ | |
1388 | rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st); | |
1389 | if (st == NULL) | |
1390 | st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name); | |
1391 | } | |
1a492601 PT |
1392 | else |
1393 | st = gfc_new_symtree (&gfc_current_ns->sym_root, name); | |
6de9cd9a | 1394 | |
6de9cd9a DN |
1395 | st->n.sym = sym; |
1396 | sym->refs++; | |
1397 | ||
66e4ab31 | 1398 | /* See if the procedure should be a module procedure. */ |
6de9cd9a | 1399 | |
1a492601 | 1400 | if (((sym->ns->proc_name != NULL |
96c8b253 SK |
1401 | && sym->ns->proc_name->attr.flavor == FL_MODULE |
1402 | && sym->attr.proc != PROC_MODULE) | |
1403 | || (module_fcn_entry && sym->attr.proc != PROC_MODULE)) | |
1404 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) | |
6de9cd9a DN |
1405 | rc = 2; |
1406 | ||
1407 | return rc; | |
1408 | } | |
1409 | ||
1410 | ||
a8b3b0b6 CR |
1411 | /* Verify that the given symbol representing a parameter is C |
1412 | interoperable, by checking to see if it was marked as such after | |
1413 | its declaration. If the given symbol is not interoperable, a | |
1414 | warning is reported, thus removing the need to return the status to | |
1415 | the calling function. The standard does not require the user use | |
1416 | one of the iso_c_binding named constants to declare an | |
1417 | interoperable parameter, but we can't be sure if the param is C | |
1418 | interop or not if the user doesn't. For example, integer(4) may be | |
1419 | legal Fortran, but doesn't have meaning in C. It may interop with | |
1420 | a number of the C types, which causes a problem because the | |
1421 | compiler can't know which one. This code is almost certainly not | |
1422 | portable, and the user will get what they deserve if the C type | |
1423 | across platforms isn't always interoperable with integer(4). If | |
1424 | the user had used something like integer(c_int) or integer(c_long), | |
1425 | the compiler could have automatically handled the varying sizes | |
1426 | across platforms. */ | |
1427 | ||
524af0d6 | 1428 | bool |
00820a2a | 1429 | gfc_verify_c_interop_param (gfc_symbol *sym) |
a8b3b0b6 CR |
1430 | { |
1431 | int is_c_interop = 0; | |
524af0d6 | 1432 | bool retval = true; |
a8b3b0b6 CR |
1433 | |
1434 | /* We check implicitly typed variables in symbol.c:gfc_set_default_type(). | |
1435 | Don't repeat the checks here. */ | |
1436 | if (sym->attr.implicit_type) | |
524af0d6 | 1437 | return true; |
f5acf0f2 | 1438 | |
a8b3b0b6 CR |
1439 | /* For subroutines or functions that are passed to a BIND(C) procedure, |
1440 | they're interoperable if they're BIND(C) and their params are all | |
1441 | interoperable. */ | |
1442 | if (sym->attr.flavor == FL_PROCEDURE) | |
1443 | { | |
1444 | if (sym->attr.is_bind_c == 0) | |
1445 | { | |
4daa149b TB |
1446 | gfc_error_now ("Procedure %qs at %L must have the BIND(C) " |
1447 | "attribute to be C interoperable", sym->name, | |
1448 | &(sym->declared_at)); | |
524af0d6 | 1449 | return false; |
a8b3b0b6 CR |
1450 | } |
1451 | else | |
1452 | { | |
1453 | if (sym->attr.is_c_interop == 1) | |
1454 | /* We've already checked this procedure; don't check it again. */ | |
524af0d6 | 1455 | return true; |
a8b3b0b6 CR |
1456 | else |
1457 | return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common, | |
1458 | sym->common_block); | |
1459 | } | |
1460 | } | |
f5acf0f2 | 1461 | |
a8b3b0b6 CR |
1462 | /* See if we've stored a reference to a procedure that owns sym. */ |
1463 | if (sym->ns != NULL && sym->ns->proc_name != NULL) | |
1464 | { | |
1465 | if (sym->ns->proc_name->attr.is_bind_c == 1) | |
1466 | { | |
524af0d6 | 1467 | is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0); |
a8b3b0b6 CR |
1468 | |
1469 | if (is_c_interop != 1) | |
1470 | { | |
1471 | /* Make personalized messages to give better feedback. */ | |
1472 | if (sym->ts.type == BT_DERIVED) | |
c4100eae MLI |
1473 | gfc_error ("Variable %qs at %L is a dummy argument to the " |
1474 | "BIND(C) procedure %qs but is not C interoperable " | |
1475 | "because derived type %qs is not C interoperable", | |
a8b3b0b6 | 1476 | sym->name, &(sym->declared_at), |
f5acf0f2 | 1477 | sym->ns->proc_name->name, |
bc21d315 | 1478 | sym->ts.u.derived->name); |
00820a2a | 1479 | else if (sym->ts.type == BT_CLASS) |
c4100eae MLI |
1480 | gfc_error ("Variable %qs at %L is a dummy argument to the " |
1481 | "BIND(C) procedure %qs but is not C interoperable " | |
00820a2a JW |
1482 | "because it is polymorphic", |
1483 | sym->name, &(sym->declared_at), | |
1484 | sym->ns->proc_name->name); | |
4daa149b | 1485 | else if (warn_c_binding_type) |
48749dbc MLI |
1486 | gfc_warning (OPT_Wc_binding_type, |
1487 | "Variable %qs at %L is a dummy argument of the " | |
1488 | "BIND(C) procedure %qs but may not be C " | |
a8b3b0b6 CR |
1489 | "interoperable", |
1490 | sym->name, &(sym->declared_at), | |
1491 | sym->ns->proc_name->name); | |
1492 | } | |
aa5e22f0 CR |
1493 | |
1494 | /* Character strings are only C interoperable if they have a | |
1495 | length of 1. */ | |
0a524296 | 1496 | if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension) |
aa5e22f0 | 1497 | { |
bc21d315 | 1498 | gfc_charlen *cl = sym->ts.u.cl; |
aa5e22f0 CR |
1499 | if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT |
1500 | || mpz_cmp_si (cl->length->value.integer, 1) != 0) | |
1501 | { | |
0a524296 PT |
1502 | gfc_error ("Character argument %qs at %L " |
1503 | "must be length 1 because " | |
1504 | "procedure %qs is BIND(C)", | |
1505 | sym->name, &sym->declared_at, | |
1506 | sym->ns->proc_name->name); | |
1507 | retval = false; | |
aa5e22f0 CR |
1508 | } |
1509 | } | |
1510 | ||
a8b3b0b6 CR |
1511 | /* We have to make sure that any param to a bind(c) routine does |
1512 | not have the allocatable, pointer, or optional attributes, | |
1513 | according to J3/04-007, section 5.1. */ | |
60f6ca95 | 1514 | if (sym->attr.allocatable == 1 |
286f737c | 1515 | && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with " |
a4d9b221 | 1516 | "ALLOCATABLE attribute in procedure %qs " |
60f6ca95 TB |
1517 | "with BIND(C)", sym->name, |
1518 | &(sym->declared_at), | |
1519 | sym->ns->proc_name->name)) | |
1520 | retval = false; | |
1521 | ||
1522 | if (sym->attr.pointer == 1 | |
286f737c | 1523 | && !gfc_notify_std (GFC_STD_F2018, "Variable %qs at %L with " |
a4d9b221 | 1524 | "POINTER attribute in procedure %qs " |
60f6ca95 TB |
1525 | "with BIND(C)", sym->name, |
1526 | &(sym->declared_at), | |
1527 | sym->ns->proc_name->name)) | |
1528 | retval = false; | |
1529 | ||
1530 | if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as) | |
a8b3b0b6 | 1531 | { |
c4100eae MLI |
1532 | gfc_error ("Scalar variable %qs at %L with POINTER or " |
1533 | "ALLOCATABLE in procedure %qs with BIND(C) is not yet" | |
60f6ca95 | 1534 | " supported", sym->name, &(sym->declared_at), |
a8b3b0b6 | 1535 | sym->ns->proc_name->name); |
524af0d6 | 1536 | retval = false; |
a8b3b0b6 CR |
1537 | } |
1538 | ||
2e8d9212 | 1539 | if (sym->attr.optional == 1 && sym->attr.value) |
a8b3b0b6 | 1540 | { |
c4100eae MLI |
1541 | gfc_error ("Variable %qs at %L cannot have both the OPTIONAL " |
1542 | "and the VALUE attribute because procedure %qs " | |
2e8d9212 | 1543 | "is BIND(C)", sym->name, &(sym->declared_at), |
a8b3b0b6 | 1544 | sym->ns->proc_name->name); |
524af0d6 | 1545 | retval = false; |
a8b3b0b6 | 1546 | } |
2e8d9212 | 1547 | else if (sym->attr.optional == 1 |
286f737c | 1548 | && !gfc_notify_std (GFC_STD_F2018, "Variable %qs " |
524af0d6 | 1549 | "at %L with OPTIONAL attribute in " |
70112e2a PT |
1550 | "procedure %qs which is BIND(C)", |
1551 | sym->name, &(sym->declared_at), | |
524af0d6 JB |
1552 | sym->ns->proc_name->name)) |
1553 | retval = false; | |
a8b3b0b6 CR |
1554 | |
1555 | /* Make sure that if it has the dimension attribute, that it is | |
95d47b8d TB |
1556 | either assumed size or explicit shape. Deferred shape is already |
1557 | covered by the pointer/allocatable attribute. */ | |
1558 | if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE | |
286f737c | 1559 | && !gfc_notify_std (GFC_STD_F2018, "Assumed-shape array %qs " |
524af0d6 | 1560 | "at %L as dummy argument to the BIND(C) " |
811582ec | 1561 | "procedure %qs at %L", sym->name, |
70112e2a PT |
1562 | &(sym->declared_at), |
1563 | sym->ns->proc_name->name, | |
524af0d6 JB |
1564 | &(sym->ns->proc_name->declared_at))) |
1565 | retval = false; | |
a8b3b0b6 CR |
1566 | } |
1567 | } | |
1568 | ||
1569 | return retval; | |
1570 | } | |
1571 | ||
1572 | ||
cf2b3c22 | 1573 | |
a8b3b0b6 | 1574 | /* Function called by variable_decl() that adds a name to the symbol table. */ |
6de9cd9a | 1575 | |
524af0d6 | 1576 | static bool |
e69afb29 | 1577 | build_sym (const char *name, gfc_charlen *cl, bool cl_deferred, |
636dff67 | 1578 | gfc_array_spec **as, locus *var_locus) |
6de9cd9a DN |
1579 | { |
1580 | symbol_attribute attr; | |
1581 | gfc_symbol *sym; | |
1e6025b6 | 1582 | int upper; |
bedee914 PT |
1583 | gfc_symtree *st; |
1584 | ||
1585 | /* Symbols in a submodule are host associated from the parent module or | |
1586 | submodules. Therefore, they can be overridden by declarations in the | |
1587 | submodule scope. Deal with this by attaching the existing symbol to | |
1588 | a new symtree and recycling the old symtree with a new symbol... */ | |
1589 | st = gfc_find_symtree (gfc_current_ns->sym_root, name); | |
1590 | if (st != NULL && gfc_state_stack->state == COMP_SUBMODULE | |
1591 | && st->n.sym != NULL | |
1592 | && st->n.sym->attr.host_assoc && st->n.sym->attr.used_in_submodule) | |
1593 | { | |
1594 | gfc_symtree *s = gfc_get_unique_symtree (gfc_current_ns); | |
1595 | s->n.sym = st->n.sym; | |
1596 | sym = gfc_new_symbol (name, gfc_current_ns); | |
6de9cd9a | 1597 | |
bedee914 PT |
1598 | |
1599 | st->n.sym = sym; | |
1600 | sym->refs++; | |
1601 | gfc_set_sym_referenced (sym); | |
1602 | } | |
1603 | /* ...Otherwise generate a new symtree and new symbol. */ | |
1604 | else if (gfc_get_symbol (name, NULL, &sym)) | |
524af0d6 | 1605 | return false; |
6de9cd9a | 1606 | |
1e6025b6 TK |
1607 | /* Check if the name has already been defined as a type. The |
1608 | first letter of the symtree will be in upper case then. Of | |
1609 | course, this is only necessary if the upper case letter is | |
1610 | actually different. */ | |
1611 | ||
1612 | upper = TOUPPER(name[0]); | |
1613 | if (upper != name[0]) | |
1614 | { | |
1615 | char u_name[GFC_MAX_SYMBOL_LEN + 1]; | |
1616 | gfc_symtree *st; | |
1e6025b6 | 1617 | |
025d57f0 MS |
1618 | gcc_assert (strlen(name) <= GFC_MAX_SYMBOL_LEN); |
1619 | strcpy (u_name, name); | |
1e6025b6 TK |
1620 | u_name[0] = upper; |
1621 | ||
1622 | st = gfc_find_symtree (gfc_current_ns->sym_root, u_name); | |
1623 | ||
f6288c24 FR |
1624 | /* STRUCTURE types can alias symbol names */ |
1625 | if (st != 0 && st->n.sym->attr.flavor != FL_STRUCT) | |
1e6025b6 TK |
1626 | { |
1627 | gfc_error ("Symbol %qs at %C also declared as a type at %L", name, | |
1628 | &st->n.sym->declared_at); | |
1629 | return false; | |
1630 | } | |
1631 | } | |
1632 | ||
66e4ab31 | 1633 | /* Start updating the symbol table. Add basic type attribute if present. */ |
6de9cd9a | 1634 | if (current_ts.type != BT_UNKNOWN |
636dff67 SK |
1635 | && (sym->attr.implicit_type == 0 |
1636 | || !gfc_compare_types (&sym->ts, ¤t_ts)) | |
524af0d6 JB |
1637 | && !gfc_add_type (sym, ¤t_ts, var_locus)) |
1638 | return false; | |
6de9cd9a DN |
1639 | |
1640 | if (sym->ts.type == BT_CHARACTER) | |
e69afb29 SK |
1641 | { |
1642 | sym->ts.u.cl = cl; | |
1643 | sym->ts.deferred = cl_deferred; | |
1644 | } | |
6de9cd9a DN |
1645 | |
1646 | /* Add dimension attribute if present. */ | |
524af0d6 JB |
1647 | if (!gfc_set_array_spec (sym, *as, var_locus)) |
1648 | return false; | |
6de9cd9a DN |
1649 | *as = NULL; |
1650 | ||
1651 | /* Add attribute to symbol. The copy is so that we can reset the | |
1652 | dimension attribute. */ | |
1653 | attr = current_attr; | |
1654 | attr.dimension = 0; | |
be59db2d | 1655 | attr.codimension = 0; |
6de9cd9a | 1656 | |
524af0d6 JB |
1657 | if (!gfc_copy_attr (&sym->attr, &attr, var_locus)) |
1658 | return false; | |
6de9cd9a | 1659 | |
a8b3b0b6 CR |
1660 | /* Finish any work that may need to be done for the binding label, |
1661 | if it's a bind(c). The bind(c) attr is found before the symbol | |
1662 | is made, and before the symbol name (for data decls), so the | |
1663 | current_ts is holding the binding label, or nothing if the | |
1664 | name= attr wasn't given. Therefore, test here if we're dealing | |
1665 | with a bind(c) and make sure the binding label is set correctly. */ | |
1666 | if (sym->attr.is_bind_c == 1) | |
1667 | { | |
62603fae | 1668 | if (!sym->binding_label) |
a8b3b0b6 | 1669 | { |
ad4a2f64 TB |
1670 | /* Set the binding label and verify that if a NAME= was specified |
1671 | then only one identifier was in the entity-decl-list. */ | |
70112e2a | 1672 | if (!set_binding_label (&sym->binding_label, sym->name, |
524af0d6 JB |
1673 | num_idents_on_line)) |
1674 | return false; | |
a8b3b0b6 CR |
1675 | } |
1676 | } | |
1677 | ||
1678 | /* See if we know we're in a common block, and if it's a bind(c) | |
1679 | common then we need to make sure we're an interoperable type. */ | |
1680 | if (sym->attr.in_common == 1) | |
1681 | { | |
1682 | /* Test the common block object. */ | |
1683 | if (sym->common_block != NULL && sym->common_block->is_bind_c == 1 | |
1684 | && sym->ts.is_c_interop != 1) | |
1685 | { | |
4daa149b | 1686 | gfc_error_now ("Variable %qs in common block %qs at %C " |
a8b3b0b6 | 1687 | "must be declared with a C interoperable " |
4daa149b | 1688 | "kind since common block %qs is BIND(C)", |
a8b3b0b6 CR |
1689 | sym->name, sym->common_block->name, |
1690 | sym->common_block->name); | |
1691 | gfc_clear_error (); | |
1692 | } | |
1693 | } | |
1694 | ||
9a3db5a3 PT |
1695 | sym->attr.implied_index = 0; |
1696 | ||
5bab4c96 PT |
1697 | /* Use the parameter expressions for a parameterized derived type. */ |
1698 | if ((sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS) | |
1699 | && sym->ts.u.derived->attr.pdt_type && type_param_spec_list) | |
1700 | sym->param_list = gfc_copy_actual_arglist (type_param_spec_list); | |
1701 | ||
528622fd | 1702 | if (sym->ts.type == BT_CLASS) |
9b6da3c7 | 1703 | return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as); |
cf2b3c22 | 1704 | |
524af0d6 | 1705 | return true; |
6de9cd9a DN |
1706 | } |
1707 | ||
636dff67 | 1708 | |
df7cc9b5 | 1709 | /* Set character constant to the given length. The constant will be padded or |
d2848082 DK |
1710 | truncated. If we're inside an array constructor without a typespec, we |
1711 | additionally check that all elements have the same length; check_len -1 | |
1712 | means no checking. */ | |
df7cc9b5 FW |
1713 | |
1714 | void | |
6b271a2e JB |
1715 | gfc_set_constant_character_len (gfc_charlen_t len, gfc_expr *expr, |
1716 | gfc_charlen_t check_len) | |
df7cc9b5 | 1717 | { |
00660189 | 1718 | gfc_char_t *s; |
6b271a2e | 1719 | gfc_charlen_t slen; |
df7cc9b5 | 1720 | |
834e9dbb SK |
1721 | if (expr->ts.type != BT_CHARACTER) |
1722 | return; | |
63af1586 | 1723 | |
b441ae1d SK |
1724 | if (expr->expr_type != EXPR_CONSTANT) |
1725 | { | |
1726 | gfc_error_now ("CHARACTER length must be a constant at %L", &expr->where); | |
1727 | return; | |
1728 | } | |
df7cc9b5 FW |
1729 | |
1730 | slen = expr->value.character.length; | |
1731 | if (len != slen) | |
1732 | { | |
00660189 FXC |
1733 | s = gfc_get_wide_string (len + 1); |
1734 | memcpy (s, expr->value.character.string, | |
1735 | MIN (len, slen) * sizeof (gfc_char_t)); | |
df7cc9b5 | 1736 | if (len > slen) |
00660189 | 1737 | gfc_wide_memset (&s[slen], ' ', len - slen); |
2220652d | 1738 | |
a96c39ea | 1739 | if (warn_character_truncation && slen > len) |
4daa149b TB |
1740 | gfc_warning_now (OPT_Wcharacter_truncation, |
1741 | "CHARACTER expression at %L is being truncated " | |
6b271a2e JB |
1742 | "(%ld/%ld)", &expr->where, |
1743 | (long) slen, (long) len); | |
2220652d PT |
1744 | |
1745 | /* Apply the standard by 'hand' otherwise it gets cleared for | |
1746 | initializers. */ | |
d2848082 DK |
1747 | if (check_len != -1 && slen != check_len |
1748 | && !(gfc_option.allow_std & GFC_STD_GNU)) | |
2220652d | 1749 | gfc_error_now ("The CHARACTER elements of the array constructor " |
6b271a2e JB |
1750 | "at %L must have the same length (%ld/%ld)", |
1751 | &expr->where, (long) slen, | |
1752 | (long) check_len); | |
2220652d | 1753 | |
150675a8 | 1754 | s[len] = '\0'; |
cede9502 | 1755 | free (expr->value.character.string); |
df7cc9b5 FW |
1756 | expr->value.character.string = s; |
1757 | expr->value.character.length = len; | |
e6ca33ba HA |
1758 | /* If explicit representation was given, clear it |
1759 | as it is no longer needed after padding. */ | |
1760 | if (expr->representation.length) | |
1761 | { | |
1762 | expr->representation.length = 0; | |
1763 | free (expr->representation.string); | |
1764 | expr->representation.string = NULL; | |
1765 | } | |
df7cc9b5 FW |
1766 | } |
1767 | } | |
6de9cd9a | 1768 | |
25d8f0a2 | 1769 | |
d51347f9 | 1770 | /* Function to create and update the enumerator history |
25d8f0a2 | 1771 | using the information passed as arguments. |
d51347f9 TB |
1772 | Pointer "max_enum" is also updated, to point to |
1773 | enum history node containing largest initializer. | |
25d8f0a2 TS |
1774 | |
1775 | SYM points to the symbol node of enumerator. | |
66e4ab31 | 1776 | INIT points to its enumerator value. */ |
25d8f0a2 | 1777 | |
d51347f9 | 1778 | static void |
636dff67 | 1779 | create_enum_history (gfc_symbol *sym, gfc_expr *init) |
25d8f0a2 TS |
1780 | { |
1781 | enumerator_history *new_enum_history; | |
1782 | gcc_assert (sym != NULL && init != NULL); | |
1783 | ||
ece3f663 | 1784 | new_enum_history = XCNEW (enumerator_history); |
25d8f0a2 TS |
1785 | |
1786 | new_enum_history->sym = sym; | |
1787 | new_enum_history->initializer = init; | |
1788 | new_enum_history->next = NULL; | |
1789 | ||
1790 | if (enum_history == NULL) | |
1791 | { | |
1792 | enum_history = new_enum_history; | |
1793 | max_enum = enum_history; | |
1794 | } | |
1795 | else | |
1796 | { | |
1797 | new_enum_history->next = enum_history; | |
1798 | enum_history = new_enum_history; | |
1799 | ||
d51347f9 | 1800 | if (mpz_cmp (max_enum->initializer->value.integer, |
25d8f0a2 | 1801 | new_enum_history->initializer->value.integer) < 0) |
636dff67 | 1802 | max_enum = new_enum_history; |
25d8f0a2 TS |
1803 | } |
1804 | } | |
1805 | ||
1806 | ||
d51347f9 | 1807 | /* Function to free enum kind history. */ |
25d8f0a2 | 1808 | |
d51347f9 | 1809 | void |
636dff67 | 1810 | gfc_free_enum_history (void) |
25d8f0a2 | 1811 | { |
d51347f9 TB |
1812 | enumerator_history *current = enum_history; |
1813 | enumerator_history *next; | |
25d8f0a2 TS |
1814 | |
1815 | while (current != NULL) | |
1816 | { | |
1817 | next = current->next; | |
cede9502 | 1818 | free (current); |
25d8f0a2 TS |
1819 | current = next; |
1820 | } | |
1821 | max_enum = NULL; | |
1822 | enum_history = NULL; | |
1823 | } | |
1824 | ||
1825 | ||
6de9cd9a DN |
1826 | /* Function called by variable_decl() that adds an initialization |
1827 | expression to a symbol. */ | |
1828 | ||
524af0d6 | 1829 | static bool |
66e4ab31 | 1830 | add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus) |
6de9cd9a DN |
1831 | { |
1832 | symbol_attribute attr; | |
1833 | gfc_symbol *sym; | |
1834 | gfc_expr *init; | |
1835 | ||
1836 | init = *initp; | |
08a6b8e0 | 1837 | if (find_special (name, &sym, false)) |
524af0d6 | 1838 | return false; |
6de9cd9a DN |
1839 | |
1840 | attr = sym->attr; | |
1841 | ||
1842 | /* If this symbol is confirming an implicit parameter type, | |
1843 | then an initialization expression is not allowed. */ | |
1844 | if (attr.flavor == FL_PARAMETER | |
1845 | && sym->value != NULL | |
1846 | && *initp != NULL) | |
1847 | { | |
c4100eae | 1848 | gfc_error ("Initializer not allowed for PARAMETER %qs at %C", |
6de9cd9a | 1849 | sym->name); |
524af0d6 | 1850 | return false; |
6de9cd9a DN |
1851 | } |
1852 | ||
1853 | if (init == NULL) | |
1854 | { | |
1855 | /* An initializer is required for PARAMETER declarations. */ | |
1856 | if (attr.flavor == FL_PARAMETER) | |
1857 | { | |
1858 | gfc_error ("PARAMETER at %L is missing an initializer", var_locus); | |
524af0d6 | 1859 | return false; |
6de9cd9a DN |
1860 | } |
1861 | } | |
1862 | else | |
1863 | { | |
1864 | /* If a variable appears in a DATA block, it cannot have an | |
1de8a836 | 1865 | initializer. */ |
6de9cd9a DN |
1866 | if (sym->attr.data) |
1867 | { | |
c4100eae | 1868 | gfc_error ("Variable %qs at %C with an initializer already " |
636dff67 | 1869 | "appears in a DATA statement", sym->name); |
524af0d6 | 1870 | return false; |
6de9cd9a DN |
1871 | } |
1872 | ||
75d17889 | 1873 | /* Check if the assignment can happen. This has to be put off |
80f95228 | 1874 | until later for derived type variables and procedure pointers. */ |
f6288c24 | 1875 | if (!gfc_bt_struct (sym->ts.type) && !gfc_bt_struct (init->ts.type) |
cf2b3c22 | 1876 | && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS |
f5acf0f2 | 1877 | && !sym->attr.proc_pointer |
524af0d6 JB |
1878 | && !gfc_check_assign_symbol (sym, NULL, init)) |
1879 | return false; | |
6de9cd9a | 1880 | |
bc21d315 | 1881 | if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl |
51b128a0 | 1882 | && init->ts.type == BT_CHARACTER) |
df7cc9b5 FW |
1883 | { |
1884 | /* Update symbol character length according initializer. */ | |
524af0d6 JB |
1885 | if (!gfc_check_assign_symbol (sym, NULL, init)) |
1886 | return false; | |
51b128a0 | 1887 | |
bc21d315 | 1888 | if (sym->ts.u.cl->length == NULL) |
df7cc9b5 | 1889 | { |
f622221a | 1890 | gfc_charlen_t clen; |
66e4ab31 SK |
1891 | /* If there are multiple CHARACTER variables declared on the |
1892 | same line, we don't want them to share the same length. */ | |
b76e28c6 | 1893 | sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
96f4873b | 1894 | |
a99288e5 PT |
1895 | if (sym->attr.flavor == FL_PARAMETER) |
1896 | { | |
1897 | if (init->expr_type == EXPR_CONSTANT) | |
1898 | { | |
1899 | clen = init->value.character.length; | |
b7e75771 | 1900 | sym->ts.u.cl->length |
f622221a | 1901 | = gfc_get_int_expr (gfc_charlen_int_kind, |
b7e75771 | 1902 | NULL, clen); |
a99288e5 PT |
1903 | } |
1904 | else if (init->expr_type == EXPR_ARRAY) | |
1905 | { | |
c004a341 | 1906 | if (init->ts.u.cl && init->ts.u.cl->length) |
39abef62 LK |
1907 | { |
1908 | const gfc_expr *length = init->ts.u.cl->length; | |
1909 | if (length->expr_type != EXPR_CONSTANT) | |
1910 | { | |
1911 | gfc_error ("Cannot initialize parameter array " | |
1912 | "at %L " | |
1913 | "with variable length elements", | |
1914 | &sym->declared_at); | |
1915 | return false; | |
1916 | } | |
1917 | clen = mpz_get_si (length->value.integer); | |
1918 | } | |
dc0f176a SK |
1919 | else if (init->value.constructor) |
1920 | { | |
1921 | gfc_constructor *c; | |
70112e2a | 1922 | c = gfc_constructor_first (init->value.constructor); |
dc0f176a SK |
1923 | clen = c->expr->value.character.length; |
1924 | } | |
1925 | else | |
1926 | gcc_unreachable (); | |
b7e75771 | 1927 | sym->ts.u.cl->length |
f622221a | 1928 | = gfc_get_int_expr (gfc_charlen_int_kind, |
b7e75771 | 1929 | NULL, clen); |
a99288e5 | 1930 | } |
bc21d315 JW |
1931 | else if (init->ts.u.cl && init->ts.u.cl->length) |
1932 | sym->ts.u.cl->length = | |
bc1efcb7 | 1933 | gfc_copy_expr (init->ts.u.cl->length); |
a99288e5 | 1934 | } |
df7cc9b5 FW |
1935 | } |
1936 | /* Update initializer character length according symbol. */ | |
bc21d315 | 1937 | else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
df7cc9b5 | 1938 | { |
d30ecc9c SK |
1939 | if (!gfc_specification_expr (sym->ts.u.cl->length)) |
1940 | return false; | |
1941 | ||
aeb8c028 JJ |
1942 | int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, |
1943 | false); | |
1944 | /* resolve_charlen will complain later on if the length | |
1945 | is too large. Just skeep the initialization in that case. */ | |
1946 | if (mpz_cmp (sym->ts.u.cl->length->value.integer, | |
1947 | gfc_integer_kinds[k].huge) <= 0) | |
df7cc9b5 | 1948 | { |
aeb8c028 JJ |
1949 | HOST_WIDE_INT len |
1950 | = gfc_mpz_get_hwi (sym->ts.u.cl->length->value.integer); | |
1951 | ||
1952 | if (init->expr_type == EXPR_CONSTANT) | |
1953 | gfc_set_constant_character_len (len, init, -1); | |
1954 | else if (init->expr_type == EXPR_ARRAY) | |
1955 | { | |
1956 | gfc_constructor *c; | |
b7e75771 | 1957 | |
aeb8c028 JJ |
1958 | /* Build a new charlen to prevent simplification from |
1959 | deleting the length before it is resolved. */ | |
1960 | init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); | |
1961 | init->ts.u.cl->length | |
1962 | = gfc_copy_expr (sym->ts.u.cl->length); | |
dcdc7b6c | 1963 | |
aeb8c028 JJ |
1964 | for (c = gfc_constructor_first (init->value.constructor); |
1965 | c; c = gfc_constructor_next (c)) | |
1966 | gfc_set_constant_character_len (len, c->expr, -1); | |
1967 | } | |
df7cc9b5 FW |
1968 | } |
1969 | } | |
1970 | } | |
1971 | ||
f5ca06e6 DK |
1972 | /* If sym is implied-shape, set its upper bounds from init. */ |
1973 | if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension | |
1974 | && sym->as->type == AS_IMPLIED_SHAPE) | |
1975 | { | |
1976 | int dim; | |
1977 | ||
1978 | if (init->rank == 0) | |
1979 | { | |
1fe61adf | 1980 | gfc_error ("Cannot initialize implied-shape array at %L" |
f5ca06e6 | 1981 | " with scalar", &sym->declared_at); |
524af0d6 | 1982 | return false; |
f5ca06e6 | 1983 | } |
f5ca06e6 | 1984 | |
8ed5ae52 TK |
1985 | /* The shape may be NULL for EXPR_ARRAY, set it. */ |
1986 | if (init->shape == NULL) | |
1987 | { | |
1988 | gcc_assert (init->expr_type == EXPR_ARRAY); | |
1989 | init->shape = gfc_get_shape (1); | |
1990 | if (!gfc_array_size (init, &init->shape[0])) | |
1991 | gfc_internal_error ("gfc_array_size failed"); | |
1992 | } | |
f5ca06e6 DK |
1993 | |
1994 | for (dim = 0; dim < sym->as->rank; ++dim) | |
1995 | { | |
1996 | int k; | |
cdffe788 | 1997 | gfc_expr *e, *lower; |
f5acf0f2 | 1998 | |
f5ca06e6 | 1999 | lower = sym->as->lower[dim]; |
cdffe788 | 2000 | |
70112e2a | 2001 | /* If the lower bound is an array element from another |
cdffe788 SK |
2002 | parameterized array, then it is marked with EXPR_VARIABLE and |
2003 | is an initialization expression. Try to reduce it. */ | |
2004 | if (lower->expr_type == EXPR_VARIABLE) | |
2005 | gfc_reduce_init_expr (lower); | |
2006 | ||
2007 | if (lower->expr_type == EXPR_CONSTANT) | |
2008 | { | |
2009 | /* All dimensions must be without upper bound. */ | |
2010 | gcc_assert (!sym->as->upper[dim]); | |
2011 | ||
2012 | k = lower->ts.kind; | |
2013 | e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at); | |
2014 | mpz_add (e->value.integer, lower->value.integer, | |
2015 | init->shape[dim]); | |
2016 | mpz_sub_ui (e->value.integer, e->value.integer, 1); | |
2017 | sym->as->upper[dim] = e; | |
2018 | } | |
2019 | else | |
f5ca06e6 DK |
2020 | { |
2021 | gfc_error ("Non-constant lower bound in implied-shape" | |
2022 | " declaration at %L", &lower->where); | |
524af0d6 | 2023 | return false; |
f5ca06e6 | 2024 | } |
f5ca06e6 DK |
2025 | } |
2026 | ||
2027 | sym->as->type = AS_EXPLICIT; | |
2028 | } | |
2029 | ||
a8b3b0b6 CR |
2030 | /* Need to check if the expression we initialized this |
2031 | to was one of the iso_c_binding named constants. If so, | |
2032 | and we're a parameter (constant), let it be iso_c. | |
2033 | For example: | |
2034 | integer(c_int), parameter :: my_int = c_int | |
2035 | integer(my_int) :: my_int_2 | |
2036 | If we mark my_int as iso_c (since we can see it's value | |
2037 | is equal to one of the named constants), then my_int_2 | |
2038 | will be considered C interoperable. */ | |
f6288c24 | 2039 | if (sym->ts.type != BT_CHARACTER && !gfc_bt_struct (sym->ts.type)) |
a8b3b0b6 CR |
2040 | { |
2041 | sym->ts.is_iso_c |= init->ts.is_iso_c; | |
2042 | sym->ts.is_c_interop |= init->ts.is_c_interop; | |
2043 | /* attr bits needed for module files. */ | |
2044 | sym->attr.is_iso_c |= init->ts.is_iso_c; | |
2045 | sym->attr.is_c_interop |= init->ts.is_c_interop; | |
2046 | if (init->ts.is_iso_c) | |
2047 | sym->ts.f90_type = init->ts.f90_type; | |
2048 | } | |
b7e75771 | 2049 | |
6de9cd9a DN |
2050 | /* Add initializer. Make sure we keep the ranks sane. */ |
2051 | if (sym->attr.dimension && init->rank == 0) | |
a9b43781 PT |
2052 | { |
2053 | mpz_t size; | |
2054 | gfc_expr *array; | |
a9b43781 PT |
2055 | int n; |
2056 | if (sym->attr.flavor == FL_PARAMETER | |
2057 | && init->expr_type == EXPR_CONSTANT | |
524af0d6 | 2058 | && spec_size (sym->as, &size) |
a9b43781 PT |
2059 | && mpz_cmp_si (size, 0) > 0) |
2060 | { | |
b7e75771 JD |
2061 | array = gfc_get_array_expr (init->ts.type, init->ts.kind, |
2062 | &init->where); | |
a9b43781 | 2063 | for (n = 0; n < (int)mpz_get_si (size); n++) |
b7e75771 JD |
2064 | gfc_constructor_append_expr (&array->value.constructor, |
2065 | n == 0 | |
2066 | ? init | |
2067 | : gfc_copy_expr (init), | |
2068 | &init->where); | |
f5acf0f2 | 2069 | |
a9b43781 PT |
2070 | array->shape = gfc_get_shape (sym->as->rank); |
2071 | for (n = 0; n < sym->as->rank; n++) | |
2072 | spec_dimen_size (sym->as, n, &array->shape[n]); | |
2073 | ||
2074 | init = array; | |
2075 | mpz_clear (size); | |
2076 | } | |
2077 | init->rank = sym->as->rank; | |
2078 | } | |
6de9cd9a DN |
2079 | |
2080 | sym->value = init; | |
ef7236d2 DF |
2081 | if (sym->attr.save == SAVE_NONE) |
2082 | sym->attr.save = SAVE_IMPLICIT; | |
6de9cd9a DN |
2083 | *initp = NULL; |
2084 | } | |
2085 | ||
524af0d6 | 2086 | return true; |
6de9cd9a DN |
2087 | } |
2088 | ||
2089 | ||
2090 | /* Function called by variable_decl() that adds a name to a structure | |
2091 | being built. */ | |
2092 | ||
524af0d6 | 2093 | static bool |
636dff67 SK |
2094 | build_struct (const char *name, gfc_charlen *cl, gfc_expr **init, |
2095 | gfc_array_spec **as) | |
6de9cd9a | 2096 | { |
f6288c24 | 2097 | gfc_state_data *s; |
6de9cd9a DN |
2098 | gfc_component *c; |
2099 | ||
619dd721 | 2100 | /* F03:C438/C439. If the current symbol is of the same derived type that we're |
6de9cd9a | 2101 | constructing, it must have the pointer attribute. */ |
619dd721 | 2102 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
bc21d315 | 2103 | && current_ts.u.derived == gfc_current_block () |
6de9cd9a DN |
2104 | && current_attr.pointer == 0) |
2105 | { | |
bf9f15ee PT |
2106 | if (current_attr.allocatable |
2107 | && !gfc_notify_std(GFC_STD_F2008, "Component at %C " | |
2108 | "must have the POINTER attribute")) | |
2109 | { | |
2110 | return false; | |
2111 | } | |
2112 | else if (current_attr.allocatable == 0) | |
2113 | { | |
9cbf8673 JW |
2114 | gfc_error ("Component at %C must have the POINTER attribute"); |
2115 | return false; | |
2116 | } | |
6de9cd9a | 2117 | } |
9cbf8673 JW |
2118 | |
2119 | /* F03:C437. */ | |
2120 | if (current_ts.type == BT_CLASS | |
2121 | && !(current_attr.pointer || current_attr.allocatable)) | |
2122 | { | |
2123 | gfc_error ("Component %qs with CLASS at %C must be allocatable " | |
2124 | "or pointer", name); | |
2125 | return false; | |
bf9f15ee | 2126 | } |
6de9cd9a | 2127 | |
636dff67 | 2128 | if (gfc_current_block ()->attr.pointer && (*as)->rank != 0) |
6de9cd9a DN |
2129 | { |
2130 | if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT) | |
2131 | { | |
2132 | gfc_error ("Array component of structure at %C must have explicit " | |
2133 | "or deferred shape"); | |
524af0d6 | 2134 | return false; |
6de9cd9a DN |
2135 | } |
2136 | } | |
2137 | ||
f6288c24 FR |
2138 | /* If we are in a nested union/map definition, gfc_add_component will not |
2139 | properly find repeated components because: | |
6442a6f4 | 2140 | (i) gfc_add_component does a flat search, where components of unions |
f6288c24 FR |
2141 | and maps are implicity chained so nested components may conflict. |
2142 | (ii) Unions and maps are not linked as components of their parent | |
2143 | structures until after they are parsed. | |
2144 | For (i) we use gfc_find_component which searches recursively, and for (ii) | |
2145 | we search each block directly from the parse stack until we find the top | |
2146 | level structure. */ | |
2147 | ||
2148 | s = gfc_state_stack; | |
2149 | if (s->state == COMP_UNION || s->state == COMP_MAP) | |
2150 | { | |
2151 | while (s->state == COMP_UNION || gfc_comp_struct (s->state)) | |
2152 | { | |
2153 | c = gfc_find_component (s->sym, name, true, true, NULL); | |
2154 | if (c != NULL) | |
2155 | { | |
2f029c08 | 2156 | gfc_error_now ("Component %qs at %C already declared at %L", |
f6288c24 FR |
2157 | name, &c->loc); |
2158 | return false; | |
2159 | } | |
2160 | /* Break after we've searched the entire chain. */ | |
2161 | if (s->state == COMP_DERIVED || s->state == COMP_STRUCTURE) | |
2162 | break; | |
2163 | s = s->previous; | |
2164 | } | |
2165 | } | |
2166 | ||
524af0d6 JB |
2167 | if (!gfc_add_component (gfc_current_block(), name, &c)) |
2168 | return false; | |
6de9cd9a DN |
2169 | |
2170 | c->ts = current_ts; | |
bc21d315 JW |
2171 | if (c->ts.type == BT_CHARACTER) |
2172 | c->ts.u.cl = cl; | |
5bab4c96 PT |
2173 | |
2174 | if (c->ts.type != BT_CLASS && c->ts.type != BT_DERIVED | |
276515e6 PT |
2175 | && (c->ts.kind == 0 || c->ts.type == BT_CHARACTER) |
2176 | && saved_kind_expr != NULL) | |
5bab4c96 PT |
2177 | c->kind_expr = gfc_copy_expr (saved_kind_expr); |
2178 | ||
d4b7d0f0 | 2179 | c->attr = current_attr; |
6de9cd9a DN |
2180 | |
2181 | c->initializer = *init; | |
2182 | *init = NULL; | |
2183 | ||
2184 | c->as = *as; | |
2185 | if (c->as != NULL) | |
be59db2d TB |
2186 | { |
2187 | if (c->as->corank) | |
2188 | c->attr.codimension = 1; | |
2189 | if (c->as->rank) | |
2190 | c->attr.dimension = 1; | |
2191 | } | |
6de9cd9a DN |
2192 | *as = NULL; |
2193 | ||
7fc61626 | 2194 | gfc_apply_init (&c->ts, &c->attr, c->initializer); |
28d08315 | 2195 | |
6de9cd9a | 2196 | /* Check array components. */ |
d4b7d0f0 | 2197 | if (!c->attr.dimension) |
2e23972e | 2198 | goto scalar; |
6de9cd9a | 2199 | |
d4b7d0f0 | 2200 | if (c->attr.pointer) |
6de9cd9a DN |
2201 | { |
2202 | if (c->as->type != AS_DEFERRED) | |
2203 | { | |
5046aff5 PT |
2204 | gfc_error ("Pointer array component of structure at %C must have a " |
2205 | "deferred shape"); | |
a4f15a7d | 2206 | return false; |
5046aff5 PT |
2207 | } |
2208 | } | |
d4b7d0f0 | 2209 | else if (c->attr.allocatable) |
5046aff5 PT |
2210 | { |
2211 | if (c->as->type != AS_DEFERRED) | |
2212 | { | |
2213 | gfc_error ("Allocatable component of structure at %C must have a " | |
2214 | "deferred shape"); | |
a4f15a7d | 2215 | return false; |
6de9cd9a DN |
2216 | } |
2217 | } | |
2218 | else | |
2219 | { | |
2220 | if (c->as->type != AS_EXPLICIT) | |
2221 | { | |
636dff67 SK |
2222 | gfc_error ("Array component of structure at %C must have an " |
2223 | "explicit shape"); | |
a4f15a7d | 2224 | return false; |
6de9cd9a DN |
2225 | } |
2226 | } | |
2227 | ||
2e23972e JW |
2228 | scalar: |
2229 | if (c->ts.type == BT_CLASS) | |
a4f15a7d | 2230 | return gfc_build_class_symbol (&c->ts, &c->attr, &c->as); |
ea59b186 | 2231 | |
5bab4c96 PT |
2232 | if (c->attr.pdt_kind || c->attr.pdt_len) |
2233 | { | |
2234 | gfc_symbol *sym; | |
2235 | gfc_find_symbol (c->name, gfc_current_block ()->f2k_derived, | |
2236 | 0, &sym); | |
2237 | if (sym == NULL) | |
2238 | { | |
2239 | gfc_error ("Type parameter %qs at %C has no corresponding entry " | |
2240 | "in the type parameter name list at %L", | |
2241 | c->name, &gfc_current_block ()->declared_at); | |
2242 | return false; | |
2243 | } | |
2244 | sym->ts = c->ts; | |
2245 | sym->attr.pdt_kind = c->attr.pdt_kind; | |
2246 | sym->attr.pdt_len = c->attr.pdt_len; | |
2247 | if (c->initializer) | |
2248 | sym->value = gfc_copy_expr (c->initializer); | |
2249 | sym->attr.flavor = FL_VARIABLE; | |
2250 | } | |
2251 | ||
2252 | if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) | |
2253 | && c->ts.u.derived && c->ts.u.derived->attr.pdt_template | |
2254 | && decl_type_param_list) | |
2255 | c->param_list = gfc_copy_actual_arglist (decl_type_param_list); | |
2256 | ||
a4f15a7d | 2257 | return true; |
6de9cd9a DN |
2258 | } |
2259 | ||
2260 | ||
2261 | /* Match a 'NULL()', and possibly take care of some side effects. */ | |
2262 | ||
2263 | match | |
636dff67 | 2264 | gfc_match_null (gfc_expr **result) |
6de9cd9a DN |
2265 | { |
2266 | gfc_symbol *sym; | |
576f6da6 | 2267 | match m, m2 = MATCH_NO; |
6de9cd9a | 2268 | |
576f6da6 TB |
2269 | if ((m = gfc_match (" null ( )")) == MATCH_ERROR) |
2270 | return MATCH_ERROR; | |
2271 | ||
2272 | if (m == MATCH_NO) | |
2273 | { | |
2274 | locus old_loc; | |
2275 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2276 | ||
94241120 | 2277 | if ((m2 = gfc_match (" null (")) != MATCH_YES) |
576f6da6 TB |
2278 | return m2; |
2279 | ||
2280 | old_loc = gfc_current_locus; | |
2281 | if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR) | |
2282 | return MATCH_ERROR; | |
2283 | if (m2 != MATCH_YES | |
2284 | && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR)) | |
2285 | return MATCH_ERROR; | |
2286 | if (m2 == MATCH_NO) | |
2287 | { | |
2288 | gfc_current_locus = old_loc; | |
2289 | return MATCH_NO; | |
2290 | } | |
2291 | } | |
6de9cd9a DN |
2292 | |
2293 | /* The NULL symbol now has to be/become an intrinsic function. */ | |
2294 | if (gfc_get_symbol ("null", NULL, &sym)) | |
2295 | { | |
2296 | gfc_error ("NULL() initialization at %C is ambiguous"); | |
2297 | return MATCH_ERROR; | |
2298 | } | |
2299 | ||
2300 | gfc_intrinsic_symbol (sym); | |
2301 | ||
2302 | if (sym->attr.proc != PROC_INTRINSIC | |
07416986 | 2303 | && !(sym->attr.use_assoc && sym->attr.intrinsic) |
524af0d6 JB |
2304 | && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL) |
2305 | || !gfc_add_function (&sym->attr, sym->name, NULL))) | |
6de9cd9a DN |
2306 | return MATCH_ERROR; |
2307 | ||
b7e75771 | 2308 | *result = gfc_get_null_expr (&gfc_current_locus); |
6de9cd9a | 2309 | |
576f6da6 TB |
2310 | /* Invalid per F2008, C512. */ |
2311 | if (m2 == MATCH_YES) | |
2312 | { | |
2313 | gfc_error ("NULL() initialization at %C may not have MOLD"); | |
2314 | return MATCH_ERROR; | |
2315 | } | |
2316 | ||
6de9cd9a DN |
2317 | return MATCH_YES; |
2318 | } | |
2319 | ||
2320 | ||
80f95228 JW |
2321 | /* Match the initialization expr for a data pointer or procedure pointer. */ |
2322 | ||
2323 | static match | |
2324 | match_pointer_init (gfc_expr **init, int procptr) | |
2325 | { | |
2326 | match m; | |
2327 | ||
f6288c24 | 2328 | if (gfc_pure (NULL) && !gfc_comp_struct (gfc_state_stack->state)) |
80f95228 JW |
2329 | { |
2330 | gfc_error ("Initialization of pointer at %C is not allowed in " | |
2331 | "a PURE procedure"); | |
2332 | return MATCH_ERROR; | |
2333 | } | |
ccd7751b | 2334 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
80f95228 | 2335 | |
eea58adb | 2336 | /* Match NULL() initialization. */ |
80f95228 JW |
2337 | m = gfc_match_null (init); |
2338 | if (m != MATCH_NO) | |
2339 | return m; | |
2340 | ||
2341 | /* Match non-NULL initialization. */ | |
837c4b78 | 2342 | gfc_matching_ptr_assignment = !procptr; |
80f95228 JW |
2343 | gfc_matching_procptr_assignment = procptr; |
2344 | m = gfc_match_rvalue (init); | |
837c4b78 | 2345 | gfc_matching_ptr_assignment = 0; |
80f95228 JW |
2346 | gfc_matching_procptr_assignment = 0; |
2347 | if (m == MATCH_ERROR) | |
2348 | return MATCH_ERROR; | |
2349 | else if (m == MATCH_NO) | |
2350 | { | |
2351 | gfc_error ("Error in pointer initialization at %C"); | |
2352 | return MATCH_ERROR; | |
2353 | } | |
2354 | ||
dc9a54fa JW |
2355 | if (!procptr && !gfc_resolve_expr (*init)) |
2356 | return MATCH_ERROR; | |
f5acf0f2 | 2357 | |
524af0d6 JB |
2358 | if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer " |
2359 | "initialization at %C")) | |
80f95228 JW |
2360 | return MATCH_ERROR; |
2361 | ||
2362 | return MATCH_YES; | |
2363 | } | |
2364 | ||
2365 | ||
524af0d6 | 2366 | static bool |
bb9de0c4 JW |
2367 | check_function_name (char *name) |
2368 | { | |
2369 | /* In functions that have a RESULT variable defined, the function name always | |
2370 | refers to function calls. Therefore, the name is not allowed to appear in | |
2371 | specification statements. When checking this, be careful about | |
2372 | 'hidden' procedure pointer results ('ppr@'). */ | |
2373 | ||
2374 | if (gfc_current_state () == COMP_FUNCTION) | |
2375 | { | |
2376 | gfc_symbol *block = gfc_current_block (); | |
2377 | if (block && block->result && block->result != block | |
2378 | && strcmp (block->result->name, "ppr@") != 0 | |
2379 | && strcmp (block->name, name) == 0) | |
2380 | { | |
ba77f7ba SK |
2381 | gfc_error ("RESULT variable %qs at %L prohibits FUNCTION name %qs at %C " |
2382 | "from appearing in a specification statement", | |
2383 | block->result->name, &block->result->declared_at, name); | |
524af0d6 | 2384 | return false; |
bb9de0c4 JW |
2385 | } |
2386 | } | |
2387 | ||
524af0d6 | 2388 | return true; |
bb9de0c4 JW |
2389 | } |
2390 | ||
2391 | ||
6de9cd9a DN |
2392 | /* Match a variable name with an optional initializer. When this |
2393 | subroutine is called, a variable is expected to be parsed next. | |
2394 | Depending on what is happening at the moment, updates either the | |
2395 | symbol table or the current interface. */ | |
2396 | ||
2397 | static match | |
949d5b72 | 2398 | variable_decl (int elem) |
6de9cd9a DN |
2399 | { |
2400 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6f855a26 | 2401 | static unsigned int fill_id = 0; |
6de9cd9a DN |
2402 | gfc_expr *initializer, *char_len; |
2403 | gfc_array_spec *as; | |
83d890b9 | 2404 | gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */ |
6de9cd9a | 2405 | gfc_charlen *cl; |
e69afb29 | 2406 | bool cl_deferred; |
6de9cd9a DN |
2407 | locus var_locus; |
2408 | match m; | |
524af0d6 | 2409 | bool t; |
83d890b9 | 2410 | gfc_symbol *sym; |
6de9cd9a DN |
2411 | |
2412 | initializer = NULL; | |
2413 | as = NULL; | |
83d890b9 | 2414 | cp_as = NULL; |
6de9cd9a DN |
2415 | |
2416 | /* When we get here, we've just matched a list of attributes and | |
2417 | maybe a type and a double colon. The next thing we expect to see | |
2418 | is the name of the symbol. */ | |
6f855a26 FR |
2419 | |
2420 | /* If we are parsing a structure with legacy support, we allow the symbol | |
2421 | name to be '%FILL' which gives it an anonymous (inaccessible) name. */ | |
2422 | m = MATCH_NO; | |
2423 | gfc_gobble_whitespace (); | |
2424 | if (gfc_peek_ascii_char () == '%') | |
2425 | { | |
2426 | gfc_next_ascii_char (); | |
2427 | m = gfc_match ("fill"); | |
2428 | } | |
2429 | ||
6de9cd9a | 2430 | if (m != MATCH_YES) |
6f855a26 FR |
2431 | { |
2432 | m = gfc_match_name (name); | |
2433 | if (m != MATCH_YES) | |
2434 | goto cleanup; | |
2435 | } | |
2436 | ||
2437 | else | |
2438 | { | |
2439 | m = MATCH_ERROR; | |
2440 | if (gfc_current_state () != COMP_STRUCTURE) | |
2441 | { | |
2442 | if (flag_dec_structure) | |
2443 | gfc_error ("%qs not allowed outside STRUCTURE at %C", "%FILL"); | |
2444 | else | |
2445 | gfc_error ("%qs at %C is a DEC extension, enable with " | |
2446 | "%<-fdec-structure%>", "%FILL"); | |
2447 | goto cleanup; | |
2448 | } | |
2449 | ||
2450 | if (attr_seen) | |
2451 | { | |
2452 | gfc_error ("%qs entity cannot have attributes at %C", "%FILL"); | |
2453 | goto cleanup; | |
2454 | } | |
2455 | ||
2456 | /* %FILL components are given invalid fortran names. */ | |
2457 | snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "%%FILL%u", fill_id++); | |
2458 | m = MATCH_YES; | |
2459 | } | |
6de9cd9a | 2460 | |
63645982 | 2461 | var_locus = gfc_current_locus; |
6de9cd9a DN |
2462 | |
2463 | /* Now we could see the optional array spec. or character length. */ | |
be59db2d | 2464 | m = gfc_match_array_spec (&as, true, true); |
11126dc0 | 2465 | if (m == MATCH_ERROR) |
6de9cd9a | 2466 | goto cleanup; |
25d8f0a2 | 2467 | |
6de9cd9a DN |
2468 | if (m == MATCH_NO) |
2469 | as = gfc_copy_array_spec (current_as); | |
63fbf586 | 2470 | else if (current_as |
524af0d6 | 2471 | && !merge_array_spec (current_as, as, true)) |
63fbf586 TB |
2472 | { |
2473 | m = MATCH_ERROR; | |
2474 | goto cleanup; | |
2475 | } | |
6de9cd9a | 2476 | |
c61819ff | 2477 | if (flag_cray_pointer) |
11126dc0 AL |
2478 | cp_as = gfc_copy_array_spec (as); |
2479 | ||
f5ca06e6 DK |
2480 | /* At this point, we know for sure if the symbol is PARAMETER and can thus |
2481 | determine (and check) whether it can be implied-shape. If it | |
67914693 | 2482 | was parsed as assumed-size, change it because PARAMETERs cannot |
09ef33c1 SK |
2483 | be assumed-size. |
2484 | ||
2485 | An explicit-shape-array cannot appear under several conditions. | |
2486 | That check is done here as well. */ | |
f5ca06e6 DK |
2487 | if (as) |
2488 | { | |
2489 | if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER) | |
2490 | { | |
2491 | m = MATCH_ERROR; | |
1fe61adf | 2492 | gfc_error ("Non-PARAMETER symbol %qs at %L cannot be implied-shape", |
f5ca06e6 DK |
2493 | name, &var_locus); |
2494 | goto cleanup; | |
2495 | } | |
2496 | ||
2497 | if (as->type == AS_ASSUMED_SIZE && as->rank == 1 | |
2498 | && current_attr.flavor == FL_PARAMETER) | |
2499 | as->type = AS_IMPLIED_SHAPE; | |
2500 | ||
2501 | if (as->type == AS_IMPLIED_SHAPE | |
70112e2a | 2502 | && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L", |
524af0d6 | 2503 | &var_locus)) |
f5ca06e6 DK |
2504 | { |
2505 | m = MATCH_ERROR; | |
2506 | goto cleanup; | |
2507 | } | |
09ef33c1 SK |
2508 | |
2509 | /* F2018:C830 (R816) An explicit-shape-spec whose bounds are not | |
2510 | constant expressions shall appear only in a subprogram, derived | |
2511 | type definition, BLOCK construct, or interface body. */ | |
2512 | if (as->type == AS_EXPLICIT | |
2513 | && gfc_current_state () != COMP_BLOCK | |
2514 | && gfc_current_state () != COMP_DERIVED | |
2515 | && gfc_current_state () != COMP_FUNCTION | |
2516 | && gfc_current_state () != COMP_INTERFACE | |
2517 | && gfc_current_state () != COMP_SUBROUTINE) | |
2518 | { | |
2519 | gfc_expr *e; | |
2520 | bool not_constant = false; | |
2521 | ||
2522 | for (int i = 0; i < as->rank; i++) | |
2523 | { | |
2524 | e = gfc_copy_expr (as->lower[i]); | |
2525 | gfc_resolve_expr (e); | |
2526 | gfc_simplify_expr (e, 0); | |
2527 | if (e && (e->expr_type != EXPR_CONSTANT)) | |
2528 | { | |
2529 | not_constant = true; | |
2530 | break; | |
2531 | } | |
2532 | gfc_free_expr (e); | |
2533 | ||
2534 | e = gfc_copy_expr (as->upper[i]); | |
2535 | gfc_resolve_expr (e); | |
2536 | gfc_simplify_expr (e, 0); | |
2537 | if (e && (e->expr_type != EXPR_CONSTANT)) | |
2538 | { | |
2539 | not_constant = true; | |
2540 | break; | |
2541 | } | |
2542 | gfc_free_expr (e); | |
2543 | } | |
2544 | ||
2545 | if (not_constant) | |
5b9a3332 | 2546 | { |
09ef33c1 SK |
2547 | gfc_error ("Explicit shaped array with nonconstant bounds at %C"); |
2548 | m = MATCH_ERROR; | |
2549 | goto cleanup; | |
2550 | } | |
2551 | } | |
078c5aff TK |
2552 | if (as->type == AS_EXPLICIT) |
2553 | { | |
2554 | for (int i = 0; i < as->rank; i++) | |
2555 | { | |
2556 | gfc_expr *e, *n; | |
2557 | e = as->lower[i]; | |
2558 | if (e->expr_type != EXPR_CONSTANT) | |
2559 | { | |
2560 | n = gfc_copy_expr (e); | |
2561 | gfc_simplify_expr (n, 1); | |
2562 | if (n->expr_type == EXPR_CONSTANT) | |
2563 | gfc_replace_expr (e, n); | |
2564 | else | |
2565 | gfc_free_expr (n); | |
2566 | } | |
2567 | e = as->upper[i]; | |
2568 | if (e->expr_type != EXPR_CONSTANT) | |
2569 | { | |
2570 | n = gfc_copy_expr (e); | |
2571 | gfc_simplify_expr (n, 1); | |
2572 | if (n->expr_type == EXPR_CONSTANT) | |
2573 | gfc_replace_expr (e, n); | |
2574 | else | |
2575 | gfc_free_expr (n); | |
2576 | } | |
2577 | } | |
2578 | } | |
f5ca06e6 DK |
2579 | } |
2580 | ||
6de9cd9a DN |
2581 | char_len = NULL; |
2582 | cl = NULL; | |
e69afb29 | 2583 | cl_deferred = false; |
6de9cd9a DN |
2584 | |
2585 | if (current_ts.type == BT_CHARACTER) | |
2586 | { | |
2767f2cc | 2587 | switch (match_char_length (&char_len, &cl_deferred, false)) |
6de9cd9a DN |
2588 | { |
2589 | case MATCH_YES: | |
b76e28c6 | 2590 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
6de9cd9a DN |
2591 | |
2592 | cl->length = char_len; | |
2593 | break; | |
2594 | ||
949d5b72 | 2595 | /* Non-constant lengths need to be copied after the first |
9b21a380 | 2596 | element. Also copy assumed lengths. */ |
6de9cd9a | 2597 | case MATCH_NO: |
9b21a380 | 2598 | if (elem > 1 |
bc21d315 JW |
2599 | && (current_ts.u.cl->length == NULL |
2600 | || current_ts.u.cl->length->expr_type != EXPR_CONSTANT)) | |
949d5b72 | 2601 | { |
b76e28c6 | 2602 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
bc21d315 | 2603 | cl->length = gfc_copy_expr (current_ts.u.cl->length); |
949d5b72 PT |
2604 | } |
2605 | else | |
bc21d315 | 2606 | cl = current_ts.u.cl; |
949d5b72 | 2607 | |
e69afb29 SK |
2608 | cl_deferred = current_ts.deferred; |
2609 | ||
6de9cd9a DN |
2610 | break; |
2611 | ||
2612 | case MATCH_ERROR: | |
2613 | goto cleanup; | |
2614 | } | |
2615 | } | |
2616 | ||
4668d6f9 PT |
2617 | /* The dummy arguments and result of the abreviated form of MODULE |
2618 | PROCEDUREs, used in SUBMODULES should not be redefined. */ | |
2619 | if (gfc_current_ns->proc_name | |
2620 | && gfc_current_ns->proc_name->abr_modproc_decl) | |
2621 | { | |
2622 | gfc_find_symbol (name, gfc_current_ns, 1, &sym); | |
2623 | if (sym != NULL && (sym->attr.dummy || sym->attr.result)) | |
2624 | { | |
2625 | m = MATCH_ERROR; | |
811582ec | 2626 | gfc_error ("%qs at %C is a redefinition of the declaration " |
4668d6f9 | 2627 | "in the corresponding interface for MODULE " |
811582ec | 2628 | "PROCEDURE %qs", sym->name, |
4668d6f9 PT |
2629 | gfc_current_ns->proc_name->name); |
2630 | goto cleanup; | |
2631 | } | |
2632 | } | |
2633 | ||
6f855a26 | 2634 | /* %FILL components may not have initializers. */ |
2eb3745a | 2635 | if (gfc_str_startswith (name, "%FILL") && gfc_match_eos () != MATCH_YES) |
6f855a26 FR |
2636 | { |
2637 | gfc_error ("%qs entity cannot have an initializer at %C", "%FILL"); | |
2638 | m = MATCH_ERROR; | |
2639 | goto cleanup; | |
2640 | } | |
2641 | ||
83d890b9 | 2642 | /* If this symbol has already shown up in a Cray Pointer declaration, |
88f7d6fb | 2643 | and this is not a component declaration, |
66e4ab31 | 2644 | then we want to set the type & bail out. */ |
f6288c24 | 2645 | if (flag_cray_pointer && !gfc_comp_struct (gfc_current_state ())) |
83d890b9 AL |
2646 | { |
2647 | gfc_find_symbol (name, gfc_current_ns, 1, &sym); | |
2648 | if (sym != NULL && sym->attr.cray_pointee) | |
2649 | { | |
83d890b9 | 2650 | m = MATCH_YES; |
5b9a3332 PT |
2651 | if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus)) |
2652 | { | |
2653 | m = MATCH_ERROR; | |
2654 | goto cleanup; | |
2655 | } | |
f5acf0f2 | 2656 | |
83d890b9 AL |
2657 | /* Check to see if we have an array specification. */ |
2658 | if (cp_as != NULL) | |
2659 | { | |
2660 | if (sym->as != NULL) | |
2661 | { | |
e25a0da3 | 2662 | gfc_error ("Duplicate array spec for Cray pointee at %C"); |
83d890b9 AL |
2663 | gfc_free_array_spec (cp_as); |
2664 | m = MATCH_ERROR; | |
2665 | goto cleanup; | |
2666 | } | |
2667 | else | |
2668 | { | |
524af0d6 | 2669 | if (!gfc_set_array_spec (sym, cp_as, &var_locus)) |
1fe61adf | 2670 | gfc_internal_error ("Cannot set pointee array spec."); |
d51347f9 | 2671 | |
83d890b9 | 2672 | /* Fix the array spec. */ |
d51347f9 | 2673 | m = gfc_mod_pointee_as (sym->as); |
83d890b9 AL |
2674 | if (m == MATCH_ERROR) |
2675 | goto cleanup; | |
2676 | } | |
d51347f9 | 2677 | } |
83d890b9 AL |
2678 | goto cleanup; |
2679 | } | |
2680 | else | |
2681 | { | |
2682 | gfc_free_array_spec (cp_as); | |
2683 | } | |
2684 | } | |
d51347f9 | 2685 | |
3070bab4 JW |
2686 | /* Procedure pointer as function result. */ |
2687 | if (gfc_current_state () == COMP_FUNCTION | |
2688 | && strcmp ("ppr@", gfc_current_block ()->name) == 0 | |
2689 | && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0) | |
2690 | strcpy (name, "ppr@"); | |
2691 | ||
2692 | if (gfc_current_state () == COMP_FUNCTION | |
2693 | && strcmp (name, gfc_current_block ()->name) == 0 | |
2694 | && gfc_current_block ()->result | |
2695 | && strcmp ("ppr@", gfc_current_block ()->result->name) == 0) | |
2696 | strcpy (name, "ppr@"); | |
d51347f9 | 2697 | |
6de9cd9a DN |
2698 | /* OK, we've successfully matched the declaration. Now put the |
2699 | symbol in the current namespace, because it might be used in the | |
69de3b83 | 2700 | optional initialization expression for this symbol, e.g. this is |
6de9cd9a DN |
2701 | perfectly legal: |
2702 | ||
2703 | integer, parameter :: i = huge(i) | |
2704 | ||
2705 | This is only true for parameters or variables of a basic type. | |
2706 | For components of derived types, it is not true, so we don't | |
2707 | create a symbol for those yet. If we fail to create the symbol, | |
2708 | bail out. */ | |
f6288c24 | 2709 | if (!gfc_comp_struct (gfc_current_state ()) |
524af0d6 | 2710 | && !build_sym (name, cl, cl_deferred, &as, &var_locus)) |
6de9cd9a | 2711 | { |
72af9f0b PT |
2712 | m = MATCH_ERROR; |
2713 | goto cleanup; | |
2714 | } | |
2715 | ||
524af0d6 | 2716 | if (!check_function_name (name)) |
6de9cd9a | 2717 | { |
6de9cd9a DN |
2718 | m = MATCH_ERROR; |
2719 | goto cleanup; | |
2720 | } | |
2721 | ||
294fbfc8 TS |
2722 | /* We allow old-style initializations of the form |
2723 | integer i /2/, j(4) /3*3, 1/ | |
2724 | (if no colon has been seen). These are different from data | |
2725 | statements in that initializers are only allowed to apply to the | |
2726 | variable immediately preceding, i.e. | |
2727 | integer i, j /1, 2/ | |
2728 | is not allowed. Therefore we have to do some work manually, that | |
75d17889 | 2729 | could otherwise be left to the matchers for DATA statements. */ |
294fbfc8 TS |
2730 | |
2731 | if (!colon_seen && gfc_match (" /") == MATCH_YES) | |
2732 | { | |
524af0d6 JB |
2733 | if (!gfc_notify_std (GFC_STD_GNU, "Old-style " |
2734 | "initialization at %C")) | |
294fbfc8 | 2735 | return MATCH_ERROR; |
f6288c24 FR |
2736 | |
2737 | /* Allow old style initializations for components of STRUCTUREs and MAPs | |
2738 | but not components of derived types. */ | |
b18f1efc JJ |
2739 | else if (gfc_current_state () == COMP_DERIVED) |
2740 | { | |
2741 | gfc_error ("Invalid old style initialization for derived type " | |
2742 | "component at %C"); | |
2743 | m = MATCH_ERROR; | |
2744 | goto cleanup; | |
2745 | } | |
f5acf0f2 | 2746 | |
f6288c24 FR |
2747 | /* For structure components, read the initializer as a special |
2748 | expression and let the rest of this function apply the initializer | |
2749 | as usual. */ | |
2750 | else if (gfc_comp_struct (gfc_current_state ())) | |
2751 | { | |
2752 | m = match_clist_expr (&initializer, ¤t_ts, as); | |
2753 | if (m == MATCH_NO) | |
2754 | gfc_error ("Syntax error in old style initialization of %s at %C", | |
2755 | name); | |
2756 | if (m != MATCH_YES) | |
2757 | goto cleanup; | |
2758 | } | |
2759 | ||
2760 | /* Otherwise we treat the old style initialization just like a | |
2761 | DATA declaration for the current variable. */ | |
2762 | else | |
2763 | return match_old_style_init (name); | |
294fbfc8 TS |
2764 | } |
2765 | ||
6de9cd9a DN |
2766 | /* The double colon must be present in order to have initializers. |
2767 | Otherwise the statement is ambiguous with an assignment statement. */ | |
2768 | if (colon_seen) | |
2769 | { | |
2770 | if (gfc_match (" =>") == MATCH_YES) | |
2771 | { | |
6de9cd9a DN |
2772 | if (!current_attr.pointer) |
2773 | { | |
2774 | gfc_error ("Initialization at %C isn't for a pointer variable"); | |
2775 | m = MATCH_ERROR; | |
2776 | goto cleanup; | |
2777 | } | |
2778 | ||
80f95228 | 2779 | m = match_pointer_init (&initializer, 0); |
6de9cd9a DN |
2780 | if (m != MATCH_YES) |
2781 | goto cleanup; | |
2cf4aa79 SK |
2782 | |
2783 | /* The target of a pointer initialization must have the SAVE | |
2784 | attribute. A variable in PROGRAM, MODULE, or SUBMODULE scope | |
2785 | is implicit SAVEd. Explicitly, set the SAVE_IMPLICIT value. */ | |
2786 | if (initializer->expr_type == EXPR_VARIABLE | |
2787 | && initializer->symtree->n.sym->attr.save == SAVE_NONE | |
2788 | && (gfc_current_state () == COMP_PROGRAM | |
2789 | || gfc_current_state () == COMP_MODULE | |
2790 | || gfc_current_state () == COMP_SUBMODULE)) | |
2791 | initializer->symtree->n.sym->attr.save = SAVE_IMPLICIT; | |
6de9cd9a DN |
2792 | } |
2793 | else if (gfc_match_char ('=') == MATCH_YES) | |
2794 | { | |
2795 | if (current_attr.pointer) | |
2796 | { | |
a4d9b221 TB |
2797 | gfc_error ("Pointer initialization at %C requires %<=>%>, " |
2798 | "not %<=%>"); | |
6de9cd9a DN |
2799 | m = MATCH_ERROR; |
2800 | goto cleanup; | |
2801 | } | |
2802 | ||
2803 | m = gfc_match_init_expr (&initializer); | |
2804 | if (m == MATCH_NO) | |
2805 | { | |
2806 | gfc_error ("Expected an initialization expression at %C"); | |
2807 | m = MATCH_ERROR; | |
2808 | } | |
2809 | ||
ade20620 | 2810 | if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL) |
f6288c24 | 2811 | && !gfc_comp_struct (gfc_state_stack->state)) |
6de9cd9a | 2812 | { |
636dff67 SK |
2813 | gfc_error ("Initialization of variable at %C is not allowed in " |
2814 | "a PURE procedure"); | |
6de9cd9a DN |
2815 | m = MATCH_ERROR; |
2816 | } | |
2817 | ||
ccd7751b | 2818 | if (current_attr.flavor != FL_PARAMETER |
f6288c24 | 2819 | && !gfc_comp_struct (gfc_state_stack->state)) |
ccd7751b TB |
2820 | gfc_unset_implicit_pure (gfc_current_ns->proc_name); |
2821 | ||
6de9cd9a DN |
2822 | if (m != MATCH_YES) |
2823 | goto cleanup; | |
2824 | } | |
cb44ab82 VL |
2825 | } |
2826 | ||
5046aff5 | 2827 | if (initializer != NULL && current_attr.allocatable |
f6288c24 | 2828 | && gfc_comp_struct (gfc_current_state ())) |
5046aff5 | 2829 | { |
636dff67 SK |
2830 | gfc_error ("Initialization of allocatable component at %C is not " |
2831 | "allowed"); | |
5046aff5 PT |
2832 | m = MATCH_ERROR; |
2833 | goto cleanup; | |
2834 | } | |
2835 | ||
18a4e7e3 PT |
2836 | if (gfc_current_state () == COMP_DERIVED |
2837 | && gfc_current_block ()->attr.pdt_template) | |
2838 | { | |
2839 | gfc_symbol *param; | |
2840 | gfc_find_symbol (name, gfc_current_block ()->f2k_derived, | |
2841 | 0, ¶m); | |
2842 | if (!param && (current_attr.pdt_kind || current_attr.pdt_len)) | |
2843 | { | |
2844 | gfc_error ("The component with KIND or LEN attribute at %C does not " | |
2845 | "not appear in the type parameter list at %L", | |
2846 | &gfc_current_block ()->declared_at); | |
2847 | m = MATCH_ERROR; | |
2848 | goto cleanup; | |
2849 | } | |
2850 | else if (param && !(current_attr.pdt_kind || current_attr.pdt_len)) | |
2851 | { | |
2852 | gfc_error ("The component at %C that appears in the type parameter " | |
2853 | "list at %L has neither the KIND nor LEN attribute", | |
2854 | &gfc_current_block ()->declared_at); | |
2855 | m = MATCH_ERROR; | |
2856 | goto cleanup; | |
2857 | } | |
2858 | else if (as && (current_attr.pdt_kind || current_attr.pdt_len)) | |
2859 | { | |
2860 | gfc_error ("The component at %C which is a type parameter must be " | |
2861 | "a scalar"); | |
2862 | m = MATCH_ERROR; | |
2863 | goto cleanup; | |
2864 | } | |
2865 | else if (param && initializer) | |
2866 | param->value = gfc_copy_expr (initializer); | |
2867 | } | |
2868 | ||
e310b381 | 2869 | /* Before adding a possible initilizer, do a simple check for compatibility |
26ca4e05 | 2870 | of lhs and rhs types. Assigning a REAL value to a derived type is not a |
e310b381 SK |
2871 | good thing. */ |
2872 | if (current_ts.type == BT_DERIVED && initializer | |
2873 | && (gfc_numeric_ts (&initializer->ts) | |
2874 | || initializer->ts.type == BT_LOGICAL | |
2875 | || initializer->ts.type == BT_CHARACTER)) | |
2876 | { | |
26ca4e05 | 2877 | gfc_error ("Incompatible initialization between a derived type " |
e310b381 SK |
2878 | "entity and an entity with %qs type at %C", |
2879 | gfc_typename (&initializer->ts)); | |
2880 | m = MATCH_ERROR; | |
2881 | goto cleanup; | |
2882 | } | |
2883 | ||
2884 | ||
54b4ba60 | 2885 | /* Add the initializer. Note that it is fine if initializer is |
6de9cd9a DN |
2886 | NULL here, because we sometimes also need to check if a |
2887 | declaration *must* have an initialization expression. */ | |
f6288c24 | 2888 | if (!gfc_comp_struct (gfc_current_state ())) |
6de9cd9a DN |
2889 | t = add_init_expr_to_sym (name, &initializer, &var_locus); |
2890 | else | |
54b4ba60 | 2891 | { |
5046aff5 | 2892 | if (current_ts.type == BT_DERIVED |
636dff67 | 2893 | && !current_attr.pointer && !initializer) |
54b4ba60 PB |
2894 | initializer = gfc_default_initializer (¤t_ts); |
2895 | t = build_struct (name, cl, &initializer, &as); | |
f6288c24 FR |
2896 | |
2897 | /* If we match a nested structure definition we expect to see the | |
2898 | * body even if the variable declarations blow up, so we need to keep | |
2899 | * the structure declaration around. */ | |
2900 | if (gfc_new_block && gfc_new_block->attr.flavor == FL_STRUCT) | |
2901 | gfc_commit_symbol (gfc_new_block); | |
54b4ba60 | 2902 | } |
6de9cd9a | 2903 | |
524af0d6 | 2904 | m = (t) ? MATCH_YES : MATCH_ERROR; |
6de9cd9a DN |
2905 | |
2906 | cleanup: | |
2907 | /* Free stuff up and return. */ | |
2908 | gfc_free_expr (initializer); | |
2909 | gfc_free_array_spec (as); | |
2910 | ||
2911 | return m; | |
2912 | } | |
2913 | ||
2914 | ||
b2b81a3f BM |
2915 | /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification. |
2916 | This assumes that the byte size is equal to the kind number for | |
2917 | non-COMPLEX types, and equal to twice the kind number for COMPLEX. */ | |
6de9cd9a DN |
2918 | |
2919 | match | |
636dff67 | 2920 | gfc_match_old_kind_spec (gfc_typespec *ts) |
6de9cd9a DN |
2921 | { |
2922 | match m; | |
5cf54585 | 2923 | int original_kind; |
6de9cd9a DN |
2924 | |
2925 | if (gfc_match_char ('*') != MATCH_YES) | |
2926 | return MATCH_NO; | |
2927 | ||
5cf54585 | 2928 | m = gfc_match_small_literal_int (&ts->kind, NULL); |
6de9cd9a DN |
2929 | if (m != MATCH_YES) |
2930 | return MATCH_ERROR; | |
2931 | ||
e45b3c75 ES |
2932 | original_kind = ts->kind; |
2933 | ||
6de9cd9a | 2934 | /* Massage the kind numbers for complex types. */ |
e45b3c75 ES |
2935 | if (ts->type == BT_COMPLEX) |
2936 | { | |
2937 | if (ts->kind % 2) | |
636dff67 SK |
2938 | { |
2939 | gfc_error ("Old-style type declaration %s*%d not supported at %C", | |
2940 | gfc_basic_typename (ts->type), original_kind); | |
2941 | return MATCH_ERROR; | |
2942 | } | |
e45b3c75 | 2943 | ts->kind /= 2; |
f4347334 ZG |
2944 | |
2945 | } | |
2946 | ||
203c7ebf | 2947 | if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8) |
f4347334 ZG |
2948 | ts->kind = 8; |
2949 | ||
2950 | if (ts->type == BT_REAL || ts->type == BT_COMPLEX) | |
2951 | { | |
2952 | if (ts->kind == 4) | |
2953 | { | |
203c7ebf | 2954 | if (flag_real4_kind == 8) |
f4347334 | 2955 | ts->kind = 8; |
203c7ebf | 2956 | if (flag_real4_kind == 10) |
f4347334 | 2957 | ts->kind = 10; |
203c7ebf | 2958 | if (flag_real4_kind == 16) |
f4347334 ZG |
2959 | ts->kind = 16; |
2960 | } | |
2961 | ||
2962 | if (ts->kind == 8) | |
2963 | { | |
203c7ebf | 2964 | if (flag_real8_kind == 4) |
f4347334 | 2965 | ts->kind = 4; |
203c7ebf | 2966 | if (flag_real8_kind == 10) |
f4347334 | 2967 | ts->kind = 10; |
203c7ebf | 2968 | if (flag_real8_kind == 16) |
f4347334 ZG |
2969 | ts->kind = 16; |
2970 | } | |
e45b3c75 | 2971 | } |
6de9cd9a | 2972 | |
e7a2d5fb | 2973 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a | 2974 | { |
e45b3c75 | 2975 | gfc_error ("Old-style type declaration %s*%d not supported at %C", |
636dff67 | 2976 | gfc_basic_typename (ts->type), original_kind); |
6de9cd9a DN |
2977 | return MATCH_ERROR; |
2978 | } | |
2979 | ||
70112e2a PT |
2980 | if (!gfc_notify_std (GFC_STD_GNU, |
2981 | "Nonstandard type declaration %s*%d at %C", | |
524af0d6 | 2982 | gfc_basic_typename(ts->type), original_kind)) |
df8652dc SK |
2983 | return MATCH_ERROR; |
2984 | ||
6de9cd9a DN |
2985 | return MATCH_YES; |
2986 | } | |
2987 | ||
2988 | ||
2989 | /* Match a kind specification. Since kinds are generally optional, we | |
2990 | usually return MATCH_NO if something goes wrong. If a "kind=" | |
2991 | string is found, then we know we have an error. */ | |
2992 | ||
2993 | match | |
e2d29968 | 2994 | gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only) |
6de9cd9a | 2995 | { |
e2d29968 | 2996 | locus where, loc; |
6de9cd9a DN |
2997 | gfc_expr *e; |
2998 | match m, n; | |
96ee3a4a | 2999 | char c; |
6de9cd9a DN |
3000 | |
3001 | m = MATCH_NO; | |
e2d29968 | 3002 | n = MATCH_YES; |
6de9cd9a | 3003 | e = NULL; |
5bab4c96 | 3004 | saved_kind_expr = NULL; |
6de9cd9a | 3005 | |
e2d29968 PT |
3006 | where = loc = gfc_current_locus; |
3007 | ||
3008 | if (kind_expr_only) | |
3009 | goto kind_expr; | |
6de9cd9a DN |
3010 | |
3011 | if (gfc_match_char ('(') == MATCH_NO) | |
3012 | return MATCH_NO; | |
3013 | ||
3014 | /* Also gobbles optional text. */ | |
3015 | if (gfc_match (" kind = ") == MATCH_YES) | |
3016 | m = MATCH_ERROR; | |
3017 | ||
e2d29968 PT |
3018 | loc = gfc_current_locus; |
3019 | ||
3020 | kind_expr: | |
5bab4c96 | 3021 | |
6de9cd9a | 3022 | n = gfc_match_init_expr (&e); |
e2d29968 | 3023 | |
5bab4c96 PT |
3024 | if (gfc_derived_parameter_expr (e)) |
3025 | { | |
3026 | ts->kind = 0; | |
3027 | saved_kind_expr = gfc_copy_expr (e); | |
3028 | goto close_brackets; | |
3029 | } | |
3030 | ||
6de9cd9a | 3031 | if (n != MATCH_YES) |
e2d29968 | 3032 | { |
1c8bcdf7 | 3033 | if (gfc_matching_function) |
e2d29968 | 3034 | { |
f5acf0f2 | 3035 | /* The function kind expression might include use associated or |
1c8bcdf7 PT |
3036 | imported parameters and try again after the specification |
3037 | expressions..... */ | |
e2d29968 PT |
3038 | if (gfc_match_char (')') != MATCH_YES) |
3039 | { | |
3040 | gfc_error ("Missing right parenthesis at %C"); | |
3041 | m = MATCH_ERROR; | |
3042 | goto no_match; | |
3043 | } | |
3044 | ||
3045 | gfc_free_expr (e); | |
e2d29968 PT |
3046 | gfc_undo_symbols (); |
3047 | return MATCH_YES; | |
3048 | } | |
3049 | else | |
3050 | { | |
3051 | /* ....or else, the match is real. */ | |
3052 | if (n == MATCH_NO) | |
3053 | gfc_error ("Expected initialization expression at %C"); | |
3054 | if (n != MATCH_YES) | |
3055 | return MATCH_ERROR; | |
3056 | } | |
3057 | } | |
6de9cd9a DN |
3058 | |
3059 | if (e->rank != 0) | |
3060 | { | |
3061 | gfc_error ("Expected scalar initialization expression at %C"); | |
3062 | m = MATCH_ERROR; | |
3063 | goto no_match; | |
3064 | } | |
3065 | ||
51f03c6b | 3066 | if (gfc_extract_int (e, &ts->kind, 1)) |
6de9cd9a | 3067 | { |
6de9cd9a DN |
3068 | m = MATCH_ERROR; |
3069 | goto no_match; | |
3070 | } | |
3071 | ||
a8b3b0b6 CR |
3072 | /* Before throwing away the expression, let's see if we had a |
3073 | C interoperable kind (and store the fact). */ | |
3074 | if (e->ts.is_c_interop == 1) | |
3075 | { | |
eea58adb | 3076 | /* Mark this as C interoperable if being declared with one |
a8b3b0b6 CR |
3077 | of the named constants from iso_c_binding. */ |
3078 | ts->is_c_interop = e->ts.is_iso_c; | |
3079 | ts->f90_type = e->ts.f90_type; | |
e655a6cc TK |
3080 | if (e->symtree) |
3081 | ts->interop_kind = e->symtree->n.sym; | |
a8b3b0b6 | 3082 | } |
f5acf0f2 | 3083 | |
6de9cd9a DN |
3084 | gfc_free_expr (e); |
3085 | e = NULL; | |
3086 | ||
a8b3b0b6 CR |
3087 | /* Ignore errors to this point, if we've gotten here. This means |
3088 | we ignore the m=MATCH_ERROR from above. */ | |
e7a2d5fb | 3089 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a DN |
3090 | { |
3091 | gfc_error ("Kind %d not supported for type %s at %C", ts->kind, | |
3092 | gfc_basic_typename (ts->type)); | |
96ee3a4a TB |
3093 | gfc_current_locus = where; |
3094 | return MATCH_ERROR; | |
6de9cd9a | 3095 | } |
96ee3a4a | 3096 | |
2ec855f1 TB |
3097 | /* Warn if, e.g., c_int is used for a REAL variable, but not |
3098 | if, e.g., c_double is used for COMPLEX as the standard | |
3099 | explicitly says that the kind type parameter for complex and real | |
3100 | variable is the same, i.e. c_float == c_float_complex. */ | |
3101 | if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type | |
3102 | && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX) | |
3103 | || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL))) | |
db30e21c | 3104 | gfc_warning_now (0, "C kind type parameter is for type %s but type at %L " |
2be51762 TB |
3105 | "is %s", gfc_basic_typename (ts->f90_type), &where, |
3106 | gfc_basic_typename (ts->type)); | |
2ec855f1 | 3107 | |
5bab4c96 PT |
3108 | close_brackets: |
3109 | ||
96ee3a4a | 3110 | gfc_gobble_whitespace (); |
8fc541d3 FXC |
3111 | if ((c = gfc_next_ascii_char ()) != ')' |
3112 | && (ts->type != BT_CHARACTER || c != ',')) | |
6de9cd9a | 3113 | { |
96ee3a4a TB |
3114 | if (ts->type == BT_CHARACTER) |
3115 | gfc_error ("Missing right parenthesis or comma at %C"); | |
3116 | else | |
3117 | gfc_error ("Missing right parenthesis at %C"); | |
e2d29968 | 3118 | m = MATCH_ERROR; |
6de9cd9a | 3119 | } |
a8b3b0b6 CR |
3120 | else |
3121 | /* All tests passed. */ | |
3122 | m = MATCH_YES; | |
6de9cd9a | 3123 | |
a8b3b0b6 CR |
3124 | if(m == MATCH_ERROR) |
3125 | gfc_current_locus = where; | |
f4347334 | 3126 | |
203c7ebf | 3127 | if (ts->type == BT_INTEGER && ts->kind == 4 && flag_integer4_kind == 8) |
f4347334 ZG |
3128 | ts->kind = 8; |
3129 | ||
3130 | if (ts->type == BT_REAL || ts->type == BT_COMPLEX) | |
3131 | { | |
3132 | if (ts->kind == 4) | |
3133 | { | |
203c7ebf | 3134 | if (flag_real4_kind == 8) |
f4347334 | 3135 | ts->kind = 8; |
203c7ebf | 3136 | if (flag_real4_kind == 10) |
f4347334 | 3137 | ts->kind = 10; |
203c7ebf | 3138 | if (flag_real4_kind == 16) |
f4347334 ZG |
3139 | ts->kind = 16; |
3140 | } | |
3141 | ||
3142 | if (ts->kind == 8) | |
3143 | { | |
203c7ebf | 3144 | if (flag_real8_kind == 4) |
f4347334 | 3145 | ts->kind = 4; |
203c7ebf | 3146 | if (flag_real8_kind == 10) |
f4347334 | 3147 | ts->kind = 10; |
203c7ebf | 3148 | if (flag_real8_kind == 16) |
f4347334 ZG |
3149 | ts->kind = 16; |
3150 | } | |
3151 | } | |
3152 | ||
a8b3b0b6 CR |
3153 | /* Return what we know from the test(s). */ |
3154 | return m; | |
6de9cd9a DN |
3155 | |
3156 | no_match: | |
3157 | gfc_free_expr (e); | |
63645982 | 3158 | gfc_current_locus = where; |
6de9cd9a DN |
3159 | return m; |
3160 | } | |
3161 | ||
3162 | ||
187de1ed FXC |
3163 | static match |
3164 | match_char_kind (int * kind, int * is_iso_c) | |
3165 | { | |
3166 | locus where; | |
3167 | gfc_expr *e; | |
3168 | match m, n; | |
51f03c6b | 3169 | bool fail; |
187de1ed FXC |
3170 | |
3171 | m = MATCH_NO; | |
3172 | e = NULL; | |
3173 | where = gfc_current_locus; | |
3174 | ||
3175 | n = gfc_match_init_expr (&e); | |
96ee3a4a | 3176 | |
1c8bcdf7 | 3177 | if (n != MATCH_YES && gfc_matching_function) |
96ee3a4a | 3178 | { |
1c8bcdf7 | 3179 | /* The expression might include use-associated or imported |
f5acf0f2 | 3180 | parameters and try again after the specification |
1c8bcdf7 | 3181 | expressions. */ |
96ee3a4a | 3182 | gfc_free_expr (e); |
96ee3a4a TB |
3183 | gfc_undo_symbols (); |
3184 | return MATCH_YES; | |
3185 | } | |
3186 | ||
187de1ed FXC |
3187 | if (n == MATCH_NO) |
3188 | gfc_error ("Expected initialization expression at %C"); | |
3189 | if (n != MATCH_YES) | |
3190 | return MATCH_ERROR; | |
3191 | ||
3192 | if (e->rank != 0) | |
3193 | { | |
3194 | gfc_error ("Expected scalar initialization expression at %C"); | |
3195 | m = MATCH_ERROR; | |
3196 | goto no_match; | |
3197 | } | |
3198 | ||
87f3a5cf PT |
3199 | if (gfc_derived_parameter_expr (e)) |
3200 | { | |
3201 | saved_kind_expr = e; | |
3202 | *kind = 0; | |
3203 | return MATCH_YES; | |
3204 | } | |
3205 | ||
51f03c6b | 3206 | fail = gfc_extract_int (e, kind, 1); |
187de1ed | 3207 | *is_iso_c = e->ts.is_iso_c; |
51f03c6b | 3208 | if (fail) |
187de1ed | 3209 | { |
187de1ed FXC |
3210 | m = MATCH_ERROR; |
3211 | goto no_match; | |
3212 | } | |
3213 | ||
3214 | gfc_free_expr (e); | |
3215 | ||
3216 | /* Ignore errors to this point, if we've gotten here. This means | |
3217 | we ignore the m=MATCH_ERROR from above. */ | |
3218 | if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0) | |
3219 | { | |
3220 | gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind); | |
3221 | m = MATCH_ERROR; | |
3222 | } | |
3223 | else | |
3224 | /* All tests passed. */ | |
3225 | m = MATCH_YES; | |
3226 | ||
3227 | if (m == MATCH_ERROR) | |
3228 | gfc_current_locus = where; | |
f5acf0f2 | 3229 | |
187de1ed FXC |
3230 | /* Return what we know from the test(s). */ |
3231 | return m; | |
3232 | ||
3233 | no_match: | |
3234 | gfc_free_expr (e); | |
3235 | gfc_current_locus = where; | |
3236 | return m; | |
3237 | } | |
3238 | ||
8234e5e0 | 3239 | |
6de9cd9a DN |
3240 | /* Match the various kind/length specifications in a CHARACTER |
3241 | declaration. We don't return MATCH_NO. */ | |
3242 | ||
8234e5e0 SK |
3243 | match |
3244 | gfc_match_char_spec (gfc_typespec *ts) | |
6de9cd9a | 3245 | { |
187de1ed | 3246 | int kind, seen_length, is_iso_c; |
6de9cd9a DN |
3247 | gfc_charlen *cl; |
3248 | gfc_expr *len; | |
3249 | match m; | |
e69afb29 | 3250 | bool deferred; |
187de1ed | 3251 | |
6de9cd9a DN |
3252 | len = NULL; |
3253 | seen_length = 0; | |
187de1ed FXC |
3254 | kind = 0; |
3255 | is_iso_c = 0; | |
e69afb29 | 3256 | deferred = false; |
6de9cd9a DN |
3257 | |
3258 | /* Try the old-style specification first. */ | |
3259 | old_char_selector = 0; | |
3260 | ||
2767f2cc | 3261 | m = match_char_length (&len, &deferred, true); |
6de9cd9a DN |
3262 | if (m != MATCH_NO) |
3263 | { | |
3264 | if (m == MATCH_YES) | |
3265 | old_char_selector = 1; | |
3266 | seen_length = 1; | |
3267 | goto done; | |
3268 | } | |
3269 | ||
3270 | m = gfc_match_char ('('); | |
3271 | if (m != MATCH_YES) | |
3272 | { | |
a8b3b0b6 | 3273 | m = MATCH_YES; /* Character without length is a single char. */ |
6de9cd9a DN |
3274 | goto done; |
3275 | } | |
3276 | ||
a8b3b0b6 | 3277 | /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */ |
6de9cd9a DN |
3278 | if (gfc_match (" kind =") == MATCH_YES) |
3279 | { | |
187de1ed | 3280 | m = match_char_kind (&kind, &is_iso_c); |
f5acf0f2 | 3281 | |
6de9cd9a DN |
3282 | if (m == MATCH_ERROR) |
3283 | goto done; | |
3284 | if (m == MATCH_NO) | |
3285 | goto syntax; | |
3286 | ||
3287 | if (gfc_match (" , len =") == MATCH_NO) | |
3288 | goto rparen; | |
3289 | ||
e69afb29 | 3290 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
3291 | if (m == MATCH_NO) |
3292 | goto syntax; | |
3293 | if (m == MATCH_ERROR) | |
3294 | goto done; | |
3295 | seen_length = 1; | |
3296 | ||
3297 | goto rparen; | |
3298 | } | |
3299 | ||
66e4ab31 | 3300 | /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */ |
6de9cd9a DN |
3301 | if (gfc_match (" len =") == MATCH_YES) |
3302 | { | |
e69afb29 | 3303 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
3304 | if (m == MATCH_NO) |
3305 | goto syntax; | |
3306 | if (m == MATCH_ERROR) | |
3307 | goto done; | |
3308 | seen_length = 1; | |
3309 | ||
3310 | if (gfc_match_char (')') == MATCH_YES) | |
3311 | goto done; | |
3312 | ||
3313 | if (gfc_match (" , kind =") != MATCH_YES) | |
3314 | goto syntax; | |
3315 | ||
187de1ed FXC |
3316 | if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR) |
3317 | goto done; | |
6de9cd9a DN |
3318 | |
3319 | goto rparen; | |
3320 | } | |
3321 | ||
66e4ab31 | 3322 | /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */ |
e69afb29 | 3323 | m = char_len_param_value (&len, &deferred); |
6de9cd9a DN |
3324 | if (m == MATCH_NO) |
3325 | goto syntax; | |
3326 | if (m == MATCH_ERROR) | |
3327 | goto done; | |
3328 | seen_length = 1; | |
3329 | ||
3330 | m = gfc_match_char (')'); | |
3331 | if (m == MATCH_YES) | |
3332 | goto done; | |
3333 | ||
3334 | if (gfc_match_char (',') != MATCH_YES) | |
3335 | goto syntax; | |
3336 | ||
a8b3b0b6 | 3337 | gfc_match (" kind ="); /* Gobble optional text. */ |
6de9cd9a | 3338 | |
187de1ed | 3339 | m = match_char_kind (&kind, &is_iso_c); |
6de9cd9a DN |
3340 | if (m == MATCH_ERROR) |
3341 | goto done; | |
3342 | if (m == MATCH_NO) | |
3343 | goto syntax; | |
3344 | ||
3345 | rparen: | |
3346 | /* Require a right-paren at this point. */ | |
3347 | m = gfc_match_char (')'); | |
3348 | if (m == MATCH_YES) | |
3349 | goto done; | |
3350 | ||
3351 | syntax: | |
3352 | gfc_error ("Syntax error in CHARACTER declaration at %C"); | |
3353 | m = MATCH_ERROR; | |
16f8ffc8 JD |
3354 | gfc_free_expr (len); |
3355 | return m; | |
6de9cd9a DN |
3356 | |
3357 | done: | |
a99d95a2 PT |
3358 | /* Deal with character functions after USE and IMPORT statements. */ |
3359 | if (gfc_matching_function) | |
1c8bcdf7 | 3360 | { |
a99d95a2 | 3361 | gfc_free_expr (len); |
1c8bcdf7 PT |
3362 | gfc_undo_symbols (); |
3363 | return MATCH_YES; | |
3364 | } | |
3365 | ||
6de9cd9a DN |
3366 | if (m != MATCH_YES) |
3367 | { | |
3368 | gfc_free_expr (len); | |
3369 | return m; | |
3370 | } | |
3371 | ||
3372 | /* Do some final massaging of the length values. */ | |
b76e28c6 | 3373 | cl = gfc_new_charlen (gfc_current_ns, NULL); |
6de9cd9a DN |
3374 | |
3375 | if (seen_length == 0) | |
f622221a | 3376 | cl->length = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1); |
6de9cd9a | 3377 | else |
00df7c36 | 3378 | { |
3cf89a7b SK |
3379 | /* If gfortran ends up here, then len may be reducible to a constant. |
3380 | Try to do that here. If it does not reduce, simply assign len to | |
3381 | charlen. A complication occurs with user-defined generic functions, | |
3382 | which are not resolved. Use a private namespace to deal with | |
3383 | generic functions. */ | |
3384 | ||
00df7c36 SK |
3385 | if (len && len->expr_type != EXPR_CONSTANT) |
3386 | { | |
3cf89a7b | 3387 | gfc_namespace *old_ns; |
00df7c36 | 3388 | gfc_expr *e; |
3cf89a7b SK |
3389 | |
3390 | old_ns = gfc_current_ns; | |
3391 | gfc_current_ns = gfc_get_namespace (NULL, 0); | |
3392 | ||
00df7c36 SK |
3393 | e = gfc_copy_expr (len); |
3394 | gfc_reduce_init_expr (e); | |
3395 | if (e->expr_type == EXPR_CONSTANT) | |
58da192e SK |
3396 | { |
3397 | gfc_replace_expr (len, e); | |
3398 | if (mpz_cmp_si (len->value.integer, 0) < 0) | |
3399 | mpz_set_ui (len->value.integer, 0); | |
3400 | } | |
00df7c36 SK |
3401 | else |
3402 | gfc_free_expr (e); | |
3cf89a7b SK |
3403 | |
3404 | gfc_free_namespace (gfc_current_ns); | |
3405 | gfc_current_ns = old_ns; | |
00df7c36 | 3406 | } |
3cf89a7b SK |
3407 | |
3408 | cl->length = len; | |
00df7c36 | 3409 | } |
6de9cd9a | 3410 | |
bc21d315 | 3411 | ts->u.cl = cl; |
187de1ed | 3412 | ts->kind = kind == 0 ? gfc_default_character_kind : kind; |
e69afb29 | 3413 | ts->deferred = deferred; |
6de9cd9a | 3414 | |
eea58adb | 3415 | /* We have to know if it was a C interoperable kind so we can |
a8b3b0b6 | 3416 | do accurate type checking of bind(c) procs, etc. */ |
187de1ed | 3417 | if (kind != 0) |
eea58adb | 3418 | /* Mark this as C interoperable if being declared with one |
187de1ed FXC |
3419 | of the named constants from iso_c_binding. */ |
3420 | ts->is_c_interop = is_iso_c; | |
a8b3b0b6 | 3421 | else if (len != NULL) |
187de1ed FXC |
3422 | /* Here, we might have parsed something such as: character(c_char) |
3423 | In this case, the parsing code above grabs the c_char when | |
3424 | looking for the length (line 1690, roughly). it's the last | |
3425 | testcase for parsing the kind params of a character variable. | |
3426 | However, it's not actually the length. this seems like it | |
f5acf0f2 | 3427 | could be an error. |
187de1ed FXC |
3428 | To see if the user used a C interop kind, test the expr |
3429 | of the so called length, and see if it's C interoperable. */ | |
3430 | ts->is_c_interop = len->ts.is_iso_c; | |
f5acf0f2 | 3431 | |
6de9cd9a DN |
3432 | return MATCH_YES; |
3433 | } | |
3434 | ||
3435 | ||
f6288c24 FR |
3436 | /* Matches a RECORD declaration. */ |
3437 | ||
3438 | static match | |
e79e6763 | 3439 | match_record_decl (char *name) |
f6288c24 FR |
3440 | { |
3441 | locus old_loc; | |
3442 | old_loc = gfc_current_locus; | |
e79e6763 | 3443 | match m; |
f6288c24 | 3444 | |
e79e6763 FR |
3445 | m = gfc_match (" record /"); |
3446 | if (m == MATCH_YES) | |
f6288c24 | 3447 | { |
f6d17ecd | 3448 | if (!flag_dec_structure) |
f6288c24 FR |
3449 | { |
3450 | gfc_current_locus = old_loc; | |
3451 | gfc_error ("RECORD at %C is an extension, enable it with " | |
a3f9f006 | 3452 | "%<-fdec-structure%>"); |
f6288c24 FR |
3453 | return MATCH_ERROR; |
3454 | } | |
e79e6763 FR |
3455 | m = gfc_match (" %n/", name); |
3456 | if (m == MATCH_YES) | |
3457 | return MATCH_YES; | |
f6288c24 FR |
3458 | } |
3459 | ||
e79e6763 | 3460 | gfc_current_locus = old_loc; |
f6d17ecd | 3461 | if (flag_dec_structure |
e79e6763 FR |
3462 | && (gfc_match (" record% ") == MATCH_YES |
3463 | || gfc_match (" record%t") == MATCH_YES)) | |
3464 | gfc_error ("Structure name expected after RECORD at %C"); | |
3465 | if (m == MATCH_NO) | |
f6288c24 | 3466 | return MATCH_NO; |
e79e6763 FR |
3467 | |
3468 | return MATCH_ERROR; | |
f6288c24 FR |
3469 | } |
3470 | ||
5bab4c96 PT |
3471 | |
3472 | /* This function uses the gfc_actual_arglist 'type_param_spec_list' as a source | |
3473 | of expressions to substitute into the possibly parameterized expression | |
3474 | 'e'. Using a list is inefficient but should not be too bad since the | |
3475 | number of type parameters is not likely to be large. */ | |
3476 | static bool | |
3477 | insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED, | |
3478 | int* f) | |
3479 | { | |
3480 | gfc_actual_arglist *param; | |
3481 | gfc_expr *copy; | |
3482 | ||
3483 | if (e->expr_type != EXPR_VARIABLE) | |
3484 | return false; | |
3485 | ||
3486 | gcc_assert (e->symtree); | |
3487 | if (e->symtree->n.sym->attr.pdt_kind | |
3488 | || (*f != 0 && e->symtree->n.sym->attr.pdt_len)) | |
3489 | { | |
3490 | for (param = type_param_spec_list; param; param = param->next) | |
3491 | if (strcmp (e->symtree->n.sym->name, param->name) == 0) | |
3492 | break; | |
3493 | ||
3494 | if (param) | |
3495 | { | |
3496 | copy = gfc_copy_expr (param->expr); | |
3497 | *e = *copy; | |
3498 | free (copy); | |
3499 | } | |
3500 | } | |
3501 | ||
3502 | return false; | |
3503 | } | |
3504 | ||
3505 | ||
3506 | bool | |
3507 | gfc_insert_kind_parameter_exprs (gfc_expr *e) | |
3508 | { | |
3509 | return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 0); | |
3510 | } | |
3511 | ||
3512 | ||
3513 | bool | |
3514 | gfc_insert_parameter_exprs (gfc_expr *e, gfc_actual_arglist *param_list) | |
3515 | { | |
3516 | gfc_actual_arglist *old_param_spec_list = type_param_spec_list; | |
3517 | type_param_spec_list = param_list; | |
3518 | return gfc_traverse_expr (e, NULL, &insert_parameter_exprs, 1); | |
3519 | type_param_spec_list = NULL; | |
3520 | type_param_spec_list = old_param_spec_list; | |
3521 | } | |
3522 | ||
3523 | /* Determines the instance of a parameterized derived type to be used by | |
3524 | matching determining the values of the kind parameters and using them | |
3525 | in the name of the instance. If the instance exists, it is used, otherwise | |
3526 | a new derived type is created. */ | |
3527 | match | |
3528 | gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym, | |
3529 | gfc_actual_arglist **ext_param_list) | |
3530 | { | |
3531 | /* The PDT template symbol. */ | |
3532 | gfc_symbol *pdt = *sym; | |
3533 | /* The symbol for the parameter in the template f2k_namespace. */ | |
3534 | gfc_symbol *param; | |
3535 | /* The hoped for instance of the PDT. */ | |
3536 | gfc_symbol *instance; | |
3537 | /* The list of parameters appearing in the PDT declaration. */ | |
3538 | gfc_formal_arglist *type_param_name_list; | |
3539 | /* Used to store the parameter specification list during recursive calls. */ | |
3540 | gfc_actual_arglist *old_param_spec_list; | |
3541 | /* Pointers to the parameter specification being used. */ | |
3542 | gfc_actual_arglist *actual_param; | |
3543 | gfc_actual_arglist *tail = NULL; | |
3544 | /* Used to build up the name of the PDT instance. The prefix uses 4 | |
3545 | characters and each KIND parameter 2 more. Allow 8 of the latter. */ | |
3546 | char name[GFC_MAX_SYMBOL_LEN + 21]; | |
3547 | ||
3548 | bool name_seen = (param_list == NULL); | |
3549 | bool assumed_seen = false; | |
3550 | bool deferred_seen = false; | |
3551 | bool spec_error = false; | |
3552 | int kind_value, i; | |
3553 | gfc_expr *kind_expr; | |
3554 | gfc_component *c1, *c2; | |
3555 | match m; | |
3556 | ||
3557 | type_param_spec_list = NULL; | |
3558 | ||
3559 | type_param_name_list = pdt->formal; | |
3560 | actual_param = param_list; | |
3561 | sprintf (name, "Pdt%s", pdt->name); | |
3562 | ||
3563 | /* Run through the parameter name list and pick up the actual | |
3564 | parameter values or use the default values in the PDT declaration. */ | |
3565 | for (; type_param_name_list; | |
3566 | type_param_name_list = type_param_name_list->next) | |
3567 | { | |
3568 | if (actual_param && actual_param->spec_type != SPEC_EXPLICIT) | |
3569 | { | |
3570 | if (actual_param->spec_type == SPEC_ASSUMED) | |
3571 | spec_error = deferred_seen; | |
3572 | else | |
3573 | spec_error = assumed_seen; | |
3574 | ||
3575 | if (spec_error) | |
3576 | { | |
3577 | gfc_error ("The type parameter spec list at %C cannot contain " | |
3578 | "both ASSUMED and DEFERRED parameters"); | |
18a4e7e3 | 3579 | goto error_return; |
5bab4c96 PT |
3580 | } |
3581 | } | |
3582 | ||
3583 | if (actual_param && actual_param->name) | |
3584 | name_seen = true; | |
3585 | param = type_param_name_list->sym; | |
3586 | ||
276515e6 PT |
3587 | if (!param || !param->name) |
3588 | continue; | |
3589 | ||
18a4e7e3 | 3590 | c1 = gfc_find_component (pdt, param->name, false, true, NULL); |
de624bee PT |
3591 | /* An error should already have been thrown in resolve.c |
3592 | (resolve_fl_derived0). */ | |
18a4e7e3 | 3593 | if (!pdt->attr.use_assoc && !c1) |
de624bee | 3594 | goto error_return; |
18a4e7e3 | 3595 | |
5bab4c96 PT |
3596 | kind_expr = NULL; |
3597 | if (!name_seen) | |
3598 | { | |
18a4e7e3 PT |
3599 | if (!actual_param && !(c1 && c1->initializer)) |
3600 | { | |
3601 | gfc_error ("The type parameter spec list at %C does not contain " | |
3602 | "enough parameter expressions"); | |
3603 | goto error_return; | |
3604 | } | |
3605 | else if (!actual_param && c1 && c1->initializer) | |
3606 | kind_expr = gfc_copy_expr (c1->initializer); | |
3607 | else if (actual_param && actual_param->spec_type == SPEC_EXPLICIT) | |
5bab4c96 PT |
3608 | kind_expr = gfc_copy_expr (actual_param->expr); |
3609 | } | |
3610 | else | |
3611 | { | |
3612 | actual_param = param_list; | |
3613 | for (;actual_param; actual_param = actual_param->next) | |
3614 | if (actual_param->name | |
3615 | && strcmp (actual_param->name, param->name) == 0) | |
3616 | break; | |
3617 | if (actual_param && actual_param->spec_type == SPEC_EXPLICIT) | |
3618 | kind_expr = gfc_copy_expr (actual_param->expr); | |
3619 | else | |
3620 | { | |
62d3c075 PT |
3621 | if (c1->initializer) |
3622 | kind_expr = gfc_copy_expr (c1->initializer); | |
5bab4c96 PT |
3623 | else if (!(actual_param && param->attr.pdt_len)) |
3624 | { | |
71a21b9e | 3625 | gfc_error ("The derived parameter %qs at %C does not " |
5bab4c96 | 3626 | "have a default value", param->name); |
18a4e7e3 | 3627 | goto error_return; |
5bab4c96 PT |
3628 | } |
3629 | } | |
3630 | } | |
3631 | ||
3632 | /* Store the current parameter expressions in a temporary actual | |
3633 | arglist 'list' so that they can be substituted in the corresponding | |
3634 | expressions in the PDT instance. */ | |
3635 | if (type_param_spec_list == NULL) | |
3636 | { | |
3637 | type_param_spec_list = gfc_get_actual_arglist (); | |
3638 | tail = type_param_spec_list; | |
3639 | } | |
3640 | else | |
3641 | { | |
3642 | tail->next = gfc_get_actual_arglist (); | |
3643 | tail = tail->next; | |
3644 | } | |
3645 | tail->name = param->name; | |
3646 | ||
3647 | if (kind_expr) | |
3648 | { | |
87f3a5cf PT |
3649 | /* Try simplification even for LEN expressions. */ |
3650 | gfc_resolve_expr (kind_expr); | |
3651 | gfc_simplify_expr (kind_expr, 1); | |
18a4e7e3 PT |
3652 | /* Variable expressions seem to default to BT_PROCEDURE. |
3653 | TODO find out why this is and fix it. */ | |
3654 | if (kind_expr->ts.type != BT_INTEGER | |
3655 | && kind_expr->ts.type != BT_PROCEDURE) | |
3656 | { | |
3657 | gfc_error ("The parameter expression at %C must be of " | |
3658 | "INTEGER type and not %s type", | |
3659 | gfc_basic_typename (kind_expr->ts.type)); | |
3660 | goto error_return; | |
3661 | } | |
3662 | ||
5bab4c96 | 3663 | tail->expr = gfc_copy_expr (kind_expr); |
5bab4c96 PT |
3664 | } |
3665 | ||
3666 | if (actual_param) | |
3667 | tail->spec_type = actual_param->spec_type; | |
3668 | ||
3669 | if (!param->attr.pdt_kind) | |
3670 | { | |
18a4e7e3 | 3671 | if (!name_seen && actual_param) |
5bab4c96 PT |
3672 | actual_param = actual_param->next; |
3673 | if (kind_expr) | |
3674 | { | |
3675 | gfc_free_expr (kind_expr); | |
3676 | kind_expr = NULL; | |
3677 | } | |
3678 | continue; | |
3679 | } | |
3680 | ||
3681 | if (actual_param | |
3682 | && (actual_param->spec_type == SPEC_ASSUMED | |
3683 | || actual_param->spec_type == SPEC_DEFERRED)) | |
3684 | { | |
71a21b9e | 3685 | gfc_error ("The KIND parameter %qs at %C cannot either be " |
5bab4c96 | 3686 | "ASSUMED or DEFERRED", param->name); |
18a4e7e3 | 3687 | goto error_return; |
5bab4c96 PT |
3688 | } |
3689 | ||
3690 | if (!kind_expr || !gfc_is_constant_expr (kind_expr)) | |
3691 | { | |
71a21b9e | 3692 | gfc_error ("The value for the KIND parameter %qs at %C does not " |
5bab4c96 | 3693 | "reduce to a constant expression", param->name); |
18a4e7e3 | 3694 | goto error_return; |
5bab4c96 PT |
3695 | } |
3696 | ||
3697 | gfc_extract_int (kind_expr, &kind_value); | |
8a302cb2 | 3698 | sprintf (name + strlen (name), "_%d", kind_value); |
5bab4c96 PT |
3699 | |
3700 | if (!name_seen && actual_param) | |
3701 | actual_param = actual_param->next; | |
3702 | gfc_free_expr (kind_expr); | |
3703 | } | |
3704 | ||
18a4e7e3 PT |
3705 | if (!name_seen && actual_param) |
3706 | { | |
3707 | gfc_error ("The type parameter spec list at %C contains too many " | |
3708 | "parameter expressions"); | |
3709 | goto error_return; | |
3710 | } | |
3711 | ||
5bab4c96 PT |
3712 | /* Now we search for the PDT instance 'name'. If it doesn't exist, we |
3713 | build it, using 'pdt' as a template. */ | |
3714 | if (gfc_get_symbol (name, pdt->ns, &instance)) | |
3715 | { | |
3716 | gfc_error ("Parameterized derived type at %C is ambiguous"); | |
18a4e7e3 | 3717 | goto error_return; |
5bab4c96 PT |
3718 | } |
3719 | ||
3720 | m = MATCH_YES; | |
3721 | ||
3722 | if (instance->attr.flavor == FL_DERIVED | |
3723 | && instance->attr.pdt_type) | |
3724 | { | |
3725 | instance->refs++; | |
3726 | if (ext_param_list) | |
3727 | *ext_param_list = type_param_spec_list; | |
3728 | *sym = instance; | |
3729 | gfc_commit_symbols (); | |
3730 | return m; | |
3731 | } | |
3732 | ||
3733 | /* Start building the new instance of the parameterized type. */ | |
3734 | gfc_copy_attr (&instance->attr, &pdt->attr, &pdt->declared_at); | |
3735 | instance->attr.pdt_template = 0; | |
3736 | instance->attr.pdt_type = 1; | |
3737 | instance->declared_at = gfc_current_locus; | |
3738 | ||
3739 | /* Add the components, replacing the parameters in all expressions | |
3740 | with the expressions for their values in 'type_param_spec_list'. */ | |
3741 | c1 = pdt->components; | |
3742 | tail = type_param_spec_list; | |
3743 | for (; c1; c1 = c1->next) | |
3744 | { | |
3745 | gfc_add_component (instance, c1->name, &c2); | |
276515e6 | 3746 | |
5bab4c96 PT |
3747 | c2->ts = c1->ts; |
3748 | c2->attr = c1->attr; | |
3749 | ||
276515e6 PT |
3750 | /* The order of declaration of the type_specs might not be the |
3751 | same as that of the components. */ | |
3752 | if (c1->attr.pdt_kind || c1->attr.pdt_len) | |
3753 | { | |
3754 | for (tail = type_param_spec_list; tail; tail = tail->next) | |
3755 | if (strcmp (c1->name, tail->name) == 0) | |
3756 | break; | |
3757 | } | |
3758 | ||
5bab4c96 PT |
3759 | /* Deal with type extension by recursively calling this function |
3760 | to obtain the instance of the extended type. */ | |
3761 | if (gfc_current_state () != COMP_DERIVED | |
3762 | && c1 == pdt->components | |
3763 | && (c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS) | |
3764 | && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template | |
3765 | && gfc_get_derived_super_type (*sym) == c2->ts.u.derived) | |
3766 | { | |
3767 | gfc_formal_arglist *f; | |
3768 | ||
3769 | old_param_spec_list = type_param_spec_list; | |
3770 | ||
3771 | /* Obtain a spec list appropriate to the extended type..*/ | |
3772 | actual_param = gfc_copy_actual_arglist (type_param_spec_list); | |
3773 | type_param_spec_list = actual_param; | |
3774 | for (f = c1->ts.u.derived->formal; f && f->next; f = f->next) | |
3775 | actual_param = actual_param->next; | |
3776 | if (actual_param) | |
3777 | { | |
3778 | gfc_free_actual_arglist (actual_param->next); | |
3779 | actual_param->next = NULL; | |
3780 | } | |
3781 | ||
3782 | /* Now obtain the PDT instance for the extended type. */ | |
3783 | c2->param_list = type_param_spec_list; | |
3784 | m = gfc_get_pdt_instance (type_param_spec_list, &c2->ts.u.derived, | |
3785 | NULL); | |
3786 | type_param_spec_list = old_param_spec_list; | |
3787 | ||
3788 | c2->ts.u.derived->refs++; | |
3789 | gfc_set_sym_referenced (c2->ts.u.derived); | |
3790 | ||
3791 | /* Set extension level. */ | |
3792 | if (c2->ts.u.derived->attr.extension == 255) | |
3793 | { | |
3794 | /* Since the extension field is 8 bit wide, we can only have | |
3795 | up to 255 extension levels. */ | |
3796 | gfc_error ("Maximum extension level reached with type %qs at %L", | |
3797 | c2->ts.u.derived->name, | |
3798 | &c2->ts.u.derived->declared_at); | |
18a4e7e3 | 3799 | goto error_return; |
5bab4c96 PT |
3800 | } |
3801 | instance->attr.extension = c2->ts.u.derived->attr.extension + 1; | |
3802 | ||
5bab4c96 PT |
3803 | continue; |
3804 | } | |
3805 | ||
3806 | /* Set the component kind using the parameterized expression. */ | |
276515e6 PT |
3807 | if ((c1->ts.kind == 0 || c1->ts.type == BT_CHARACTER) |
3808 | && c1->kind_expr != NULL) | |
5bab4c96 PT |
3809 | { |
3810 | gfc_expr *e = gfc_copy_expr (c1->kind_expr); | |
3811 | gfc_insert_kind_parameter_exprs (e); | |
87f3a5cf | 3812 | gfc_simplify_expr (e, 1); |
5bab4c96 PT |
3813 | gfc_extract_int (e, &c2->ts.kind); |
3814 | gfc_free_expr (e); | |
18a4e7e3 PT |
3815 | if (gfc_validate_kind (c2->ts.type, c2->ts.kind, true) < 0) |
3816 | { | |
3817 | gfc_error ("Kind %d not supported for type %s at %C", | |
3818 | c2->ts.kind, gfc_basic_typename (c2->ts.type)); | |
3819 | goto error_return; | |
3820 | } | |
5bab4c96 PT |
3821 | } |
3822 | ||
3823 | /* Similarly, set the string length if parameterized. */ | |
3824 | if (c1->ts.type == BT_CHARACTER | |
3825 | && c1->ts.u.cl->length | |
3826 | && gfc_derived_parameter_expr (c1->ts.u.cl->length)) | |
3827 | { | |
3828 | gfc_expr *e; | |
3829 | e = gfc_copy_expr (c1->ts.u.cl->length); | |
3830 | gfc_insert_kind_parameter_exprs (e); | |
3831 | gfc_simplify_expr (e, 1); | |
3832 | c2->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); | |
3833 | c2->ts.u.cl->length = e; | |
3834 | c2->attr.pdt_string = 1; | |
3835 | } | |
3836 | ||
3837 | /* Set up either the KIND/LEN initializer, if constant, | |
3838 | or the parameterized expression. Use the template | |
3839 | initializer if one is not already set in this instance. */ | |
3840 | if (c2->attr.pdt_kind || c2->attr.pdt_len) | |
3841 | { | |
3842 | if (tail && tail->expr && gfc_is_constant_expr (tail->expr)) | |
3843 | c2->initializer = gfc_copy_expr (tail->expr); | |
3844 | else if (tail && tail->expr) | |
3845 | { | |
3846 | c2->param_list = gfc_get_actual_arglist (); | |
3847 | c2->param_list->name = tail->name; | |
3848 | c2->param_list->expr = gfc_copy_expr (tail->expr); | |
3849 | c2->param_list->next = NULL; | |
3850 | } | |
3851 | ||
3852 | if (!c2->initializer && c1->initializer) | |
3853 | c2->initializer = gfc_copy_expr (c1->initializer); | |
5bab4c96 PT |
3854 | } |
3855 | ||
3856 | /* Copy the array spec. */ | |
3857 | c2->as = gfc_copy_array_spec (c1->as); | |
3858 | if (c1->ts.type == BT_CLASS) | |
3859 | CLASS_DATA (c2)->as = gfc_copy_array_spec (CLASS_DATA (c1)->as); | |
3860 | ||
3861 | /* Determine if an array spec is parameterized. If so, substitute | |
3862 | in the parameter expressions for the bounds and set the pdt_array | |
3863 | attribute. Notice that this attribute must be unconditionally set | |
3864 | if this is an array of parameterized character length. */ | |
3865 | if (c1->as && c1->as->type == AS_EXPLICIT) | |
3866 | { | |
3867 | bool pdt_array = false; | |
3868 | ||
3869 | /* Are the bounds of the array parameterized? */ | |
3870 | for (i = 0; i < c1->as->rank; i++) | |
3871 | { | |
3872 | if (gfc_derived_parameter_expr (c1->as->lower[i])) | |
3873 | pdt_array = true; | |
3874 | if (gfc_derived_parameter_expr (c1->as->upper[i])) | |
3875 | pdt_array = true; | |
3876 | } | |
3877 | ||
3878 | /* If they are, free the expressions for the bounds and | |
3879 | replace them with the template expressions with substitute | |
3880 | values. */ | |
3881 | for (i = 0; pdt_array && i < c1->as->rank; i++) | |
3882 | { | |
3883 | gfc_expr *e; | |
3884 | e = gfc_copy_expr (c1->as->lower[i]); | |
3885 | gfc_insert_kind_parameter_exprs (e); | |
3886 | gfc_simplify_expr (e, 1); | |
3887 | gfc_free_expr (c2->as->lower[i]); | |
3888 | c2->as->lower[i] = e; | |
3889 | e = gfc_copy_expr (c1->as->upper[i]); | |
3890 | gfc_insert_kind_parameter_exprs (e); | |
3891 | gfc_simplify_expr (e, 1); | |
3892 | gfc_free_expr (c2->as->upper[i]); | |
3893 | c2->as->upper[i] = e; | |
3894 | } | |
3895 | c2->attr.pdt_array = pdt_array ? 1 : c2->attr.pdt_string; | |
0b627b58 PT |
3896 | if (c1->initializer) |
3897 | { | |
3898 | c2->initializer = gfc_copy_expr (c1->initializer); | |
3899 | gfc_insert_kind_parameter_exprs (c2->initializer); | |
3900 | gfc_simplify_expr (c2->initializer, 1); | |
3901 | } | |
5bab4c96 PT |
3902 | } |
3903 | ||
3904 | /* Recurse into this function for PDT components. */ | |
3905 | if ((c1->ts.type == BT_DERIVED || c1->ts.type == BT_CLASS) | |
3906 | && c1->ts.u.derived && c1->ts.u.derived->attr.pdt_template) | |
3907 | { | |
3908 | gfc_actual_arglist *params; | |
3909 | /* The component in the template has a list of specification | |
3910 | expressions derived from its declaration. */ | |
3911 | params = gfc_copy_actual_arglist (c1->param_list); | |
3912 | actual_param = params; | |
3913 | /* Substitute the template parameters with the expressions | |
3914 | from the specification list. */ | |
3915 | for (;actual_param; actual_param = actual_param->next) | |
3916 | gfc_insert_parameter_exprs (actual_param->expr, | |
3917 | type_param_spec_list); | |
3918 | ||
3919 | /* Now obtain the PDT instance for the component. */ | |
3920 | old_param_spec_list = type_param_spec_list; | |
3921 | m = gfc_get_pdt_instance (params, &c2->ts.u.derived, NULL); | |
3922 | type_param_spec_list = old_param_spec_list; | |
3923 | ||
3924 | c2->param_list = params; | |
2fcd5884 PT |
3925 | if (!(c2->attr.pointer || c2->attr.allocatable)) |
3926 | c2->initializer = gfc_default_initializer (&c2->ts); | |
3927 | ||
3928 | if (c2->attr.allocatable) | |
3929 | instance->attr.alloc_comp = 1; | |
5bab4c96 PT |
3930 | } |
3931 | } | |
3932 | ||
3933 | gfc_commit_symbol (instance); | |
3934 | if (ext_param_list) | |
3935 | *ext_param_list = type_param_spec_list; | |
3936 | *sym = instance; | |
3937 | return m; | |
18a4e7e3 PT |
3938 | |
3939 | error_return: | |
3940 | gfc_free_actual_arglist (type_param_spec_list); | |
3941 | return MATCH_ERROR; | |
5bab4c96 PT |
3942 | } |
3943 | ||
3944 | ||
e74f1cc8 JW |
3945 | /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts |
3946 | structure to the matched specification. This is necessary for FUNCTION and | |
6de9cd9a DN |
3947 | IMPLICIT statements. |
3948 | ||
d51347f9 | 3949 | If implicit_flag is nonzero, then we don't check for the optional |
e5ddaa24 | 3950 | kind specification. Not doing so is needed for matching an IMPLICIT |
6de9cd9a DN |
3951 | statement correctly. */ |
3952 | ||
e2d29968 | 3953 | match |
e74f1cc8 | 3954 | gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag) |
6de9cd9a DN |
3955 | { |
3956 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
c3f34952 | 3957 | gfc_symbol *sym, *dt_sym; |
6de9cd9a | 3958 | match m; |
8fc541d3 | 3959 | char c; |
0fb56814 | 3960 | bool seen_deferred_kind, matched_type; |
c3f34952 | 3961 | const char *dt_name; |
6de9cd9a | 3962 | |
5bab4c96 PT |
3963 | decl_type_param_list = NULL; |
3964 | ||
1c8bcdf7 PT |
3965 | /* A belt and braces check that the typespec is correctly being treated |
3966 | as a deferred characteristic association. */ | |
3967 | seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION) | |
a99d95a2 PT |
3968 | && (gfc_current_block ()->result->ts.kind == -1) |
3969 | && (ts->kind == -1); | |
6de9cd9a | 3970 | gfc_clear_ts (ts); |
1c8bcdf7 PT |
3971 | if (seen_deferred_kind) |
3972 | ts->kind = -1; | |
6de9cd9a | 3973 | |
a8b3b0b6 | 3974 | /* Clear the current binding label, in case one is given. */ |
62603fae | 3975 | curr_binding_label = NULL; |
a8b3b0b6 | 3976 | |
5f700e6d AL |
3977 | if (gfc_match (" byte") == MATCH_YES) |
3978 | { | |
524af0d6 | 3979 | if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C")) |
5f700e6d AL |
3980 | return MATCH_ERROR; |
3981 | ||
3982 | if (gfc_validate_kind (BT_INTEGER, 1, true) < 0) | |
3983 | { | |
3984 | gfc_error ("BYTE type used at %C " | |
3985 | "is not available on the target machine"); | |
3986 | return MATCH_ERROR; | |
3987 | } | |
d51347f9 | 3988 | |
5f700e6d AL |
3989 | ts->type = BT_INTEGER; |
3990 | ts->kind = 1; | |
3991 | return MATCH_YES; | |
3992 | } | |
3993 | ||
0fb56814 | 3994 | |
45a69325 | 3995 | m = gfc_match (" type ("); |
0fb56814 | 3996 | matched_type = (m == MATCH_YES); |
45a69325 TB |
3997 | if (matched_type) |
3998 | { | |
3999 | gfc_gobble_whitespace (); | |
4000 | if (gfc_peek_ascii_char () == '*') | |
4001 | { | |
4002 | if ((m = gfc_match ("*)")) != MATCH_YES) | |
4003 | return m; | |
f6288c24 | 4004 | if (gfc_comp_struct (gfc_current_state ())) |
45a69325 TB |
4005 | { |
4006 | gfc_error ("Assumed type at %C is not allowed for components"); | |
4007 | return MATCH_ERROR; | |
4008 | } | |
286f737c | 4009 | if (!gfc_notify_std (GFC_STD_F2018, "Assumed type at %C")) |
45a69325 TB |
4010 | return MATCH_ERROR; |
4011 | ts->type = BT_ASSUMED; | |
4012 | return MATCH_YES; | |
4013 | } | |
4014 | ||
4015 | m = gfc_match ("%n", name); | |
4016 | matched_type = (m == MATCH_YES); | |
4017 | } | |
4018 | ||
0fb56814 TB |
4019 | if ((matched_type && strcmp ("integer", name) == 0) |
4020 | || (!matched_type && gfc_match (" integer") == MATCH_YES)) | |
6de9cd9a DN |
4021 | { |
4022 | ts->type = BT_INTEGER; | |
9d64df18 | 4023 | ts->kind = gfc_default_integer_kind; |
6de9cd9a DN |
4024 | goto get_kind; |
4025 | } | |
4026 | ||
0fb56814 TB |
4027 | if ((matched_type && strcmp ("character", name) == 0) |
4028 | || (!matched_type && gfc_match (" character") == MATCH_YES)) | |
6de9cd9a | 4029 | { |
0fb56814 | 4030 | if (matched_type |
524af0d6 JB |
4031 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
4032 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
4033 | return MATCH_ERROR; |
4034 | ||
6de9cd9a | 4035 | ts->type = BT_CHARACTER; |
e5ddaa24 | 4036 | if (implicit_flag == 0) |
0fb56814 | 4037 | m = gfc_match_char_spec (ts); |
e5ddaa24 | 4038 | else |
0fb56814 TB |
4039 | m = MATCH_YES; |
4040 | ||
4041 | if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES) | |
4042 | m = MATCH_ERROR; | |
4043 | ||
4044 | return m; | |
6de9cd9a DN |
4045 | } |
4046 | ||
0fb56814 TB |
4047 | if ((matched_type && strcmp ("real", name) == 0) |
4048 | || (!matched_type && gfc_match (" real") == MATCH_YES)) | |
6de9cd9a DN |
4049 | { |
4050 | ts->type = BT_REAL; | |
9d64df18 | 4051 | ts->kind = gfc_default_real_kind; |
6de9cd9a DN |
4052 | goto get_kind; |
4053 | } | |
4054 | ||
0fb56814 TB |
4055 | if ((matched_type |
4056 | && (strcmp ("doubleprecision", name) == 0 | |
4057 | || (strcmp ("double", name) == 0 | |
4058 | && gfc_match (" precision") == MATCH_YES))) | |
4059 | || (!matched_type && gfc_match (" double precision") == MATCH_YES)) | |
6de9cd9a | 4060 | { |
0fb56814 | 4061 | if (matched_type |
524af0d6 JB |
4062 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
4063 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
4064 | return MATCH_ERROR; |
4065 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
4066 | return MATCH_ERROR; | |
4067 | ||
6de9cd9a | 4068 | ts->type = BT_REAL; |
9d64df18 | 4069 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
4070 | return MATCH_YES; |
4071 | } | |
4072 | ||
0fb56814 TB |
4073 | if ((matched_type && strcmp ("complex", name) == 0) |
4074 | || (!matched_type && gfc_match (" complex") == MATCH_YES)) | |
6de9cd9a DN |
4075 | { |
4076 | ts->type = BT_COMPLEX; | |
9d64df18 | 4077 | ts->kind = gfc_default_complex_kind; |
6de9cd9a DN |
4078 | goto get_kind; |
4079 | } | |
4080 | ||
0fb56814 TB |
4081 | if ((matched_type |
4082 | && (strcmp ("doublecomplex", name) == 0 | |
4083 | || (strcmp ("double", name) == 0 | |
4084 | && gfc_match (" complex") == MATCH_YES))) | |
4085 | || (!matched_type && gfc_match (" double complex") == MATCH_YES)) | |
6de9cd9a | 4086 | { |
524af0d6 | 4087 | if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C")) |
0fb56814 TB |
4088 | return MATCH_ERROR; |
4089 | ||
4090 | if (matched_type | |
524af0d6 JB |
4091 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
4092 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
4093 | return MATCH_ERROR; |
4094 | ||
4095 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
df8652dc SK |
4096 | return MATCH_ERROR; |
4097 | ||
6de9cd9a | 4098 | ts->type = BT_COMPLEX; |
9d64df18 | 4099 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
4100 | return MATCH_YES; |
4101 | } | |
4102 | ||
0fb56814 TB |
4103 | if ((matched_type && strcmp ("logical", name) == 0) |
4104 | || (!matched_type && gfc_match (" logical") == MATCH_YES)) | |
6de9cd9a DN |
4105 | { |
4106 | ts->type = BT_LOGICAL; | |
9d64df18 | 4107 | ts->kind = gfc_default_logical_kind; |
6de9cd9a DN |
4108 | goto get_kind; |
4109 | } | |
4110 | ||
0fb56814 | 4111 | if (matched_type) |
5bab4c96 PT |
4112 | { |
4113 | m = gfc_match_actual_arglist (1, &decl_type_param_list, true); | |
4114 | if (m == MATCH_ERROR) | |
4115 | return m; | |
4116 | ||
0fb56814 | 4117 | m = gfc_match_char (')'); |
5bab4c96 | 4118 | } |
0fb56814 | 4119 | |
f6288c24 FR |
4120 | if (m != MATCH_YES) |
4121 | m = match_record_decl (name); | |
4122 | ||
4123 | if (matched_type || m == MATCH_YES) | |
4124 | { | |
4125 | ts->type = BT_DERIVED; | |
4126 | /* We accept record/s/ or type(s) where s is a structure, but we | |
4127 | * don't need all the extra derived-type stuff for structures. */ | |
4128 | if (gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &sym)) | |
4129 | { | |
2f029c08 | 4130 | gfc_error ("Type name %qs at %C is ambiguous", name); |
f6288c24 FR |
4131 | return MATCH_ERROR; |
4132 | } | |
5bab4c96 PT |
4133 | |
4134 | if (sym && sym->attr.flavor == FL_DERIVED | |
4135 | && sym->attr.pdt_template | |
4136 | && gfc_current_state () != COMP_DERIVED) | |
4137 | { | |
4138 | m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL); | |
4139 | if (m != MATCH_YES) | |
4140 | return m; | |
4141 | gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type); | |
4142 | ts->u.derived = sym; | |
4143 | strcpy (name, gfc_dt_lower_string (sym->name)); | |
4144 | } | |
4145 | ||
f6288c24 FR |
4146 | if (sym && sym->attr.flavor == FL_STRUCT) |
4147 | { | |
4148 | ts->u.derived = sym; | |
4149 | return MATCH_YES; | |
4150 | } | |
4151 | /* Actually a derived type. */ | |
4152 | } | |
4153 | ||
cf2b3c22 | 4154 | else |
727e8544 | 4155 | { |
f6288c24 | 4156 | /* Match nested STRUCTURE declarations; only valid within another |
e79e6763 | 4157 | structure declaration. */ |
f6d17ecd | 4158 | if (flag_dec_structure |
e79e6763 FR |
4159 | && (gfc_current_state () == COMP_STRUCTURE |
4160 | || gfc_current_state () == COMP_MAP)) | |
4161 | { | |
4162 | m = gfc_match (" structure"); | |
4163 | if (m == MATCH_YES) | |
4164 | { | |
4165 | m = gfc_match_structure_decl (); | |
4166 | if (m == MATCH_YES) | |
4167 | { | |
4168 | /* gfc_new_block is updated by match_structure_decl. */ | |
4169 | ts->type = BT_DERIVED; | |
4170 | ts->u.derived = gfc_new_block; | |
4171 | return MATCH_YES; | |
4172 | } | |
4173 | } | |
4174 | if (m == MATCH_ERROR) | |
4175 | return MATCH_ERROR; | |
4176 | } | |
f6288c24 | 4177 | |
528622fd JW |
4178 | /* Match CLASS declarations. */ |
4179 | m = gfc_match (" class ( * )"); | |
4180 | if (m == MATCH_ERROR) | |
4181 | return MATCH_ERROR; | |
4182 | else if (m == MATCH_YES) | |
4183 | { | |
8b704316 PT |
4184 | gfc_symbol *upe; |
4185 | gfc_symtree *st; | |
4186 | ts->type = BT_CLASS; | |
f5acf0f2 | 4187 | gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe); |
8b704316 PT |
4188 | if (upe == NULL) |
4189 | { | |
f5acf0f2 PT |
4190 | upe = gfc_new_symbol ("STAR", gfc_current_ns); |
4191 | st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR"); | |
8b704316 PT |
4192 | st->n.sym = upe; |
4193 | gfc_set_sym_referenced (upe); | |
4194 | upe->refs++; | |
4195 | upe->ts.type = BT_VOID; | |
4196 | upe->attr.unlimited_polymorphic = 1; | |
4197 | /* This is essential to force the construction of | |
4198 | unlimited polymorphic component class containers. */ | |
4199 | upe->attr.zero_comp = 1; | |
70112e2a | 4200 | if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL, |
524af0d6 | 4201 | &gfc_current_locus)) |
b93d8a3f JW |
4202 | return MATCH_ERROR; |
4203 | } | |
8b704316 PT |
4204 | else |
4205 | { | |
b93d8a3f | 4206 | st = gfc_get_tbp_symtree (&gfc_current_ns->sym_root, "STAR"); |
8b704316 PT |
4207 | st->n.sym = upe; |
4208 | upe->refs++; | |
4209 | } | |
4210 | ts->u.derived = upe; | |
4211 | return m; | |
4212 | } | |
528622fd | 4213 | |
5bab4c96 PT |
4214 | m = gfc_match (" class ("); |
4215 | ||
4216 | if (m == MATCH_YES) | |
4217 | m = gfc_match ("%n", name); | |
4218 | else | |
4219 | return m; | |
4220 | ||
727e8544 JW |
4221 | if (m != MATCH_YES) |
4222 | return m; | |
cf2b3c22 | 4223 | ts->type = BT_CLASS; |
727e8544 | 4224 | |
524af0d6 | 4225 | if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C")) |
e74f1cc8 | 4226 | return MATCH_ERROR; |
5bab4c96 PT |
4227 | |
4228 | m = gfc_match_actual_arglist (1, &decl_type_param_list, true); | |
4229 | if (m == MATCH_ERROR) | |
4230 | return m; | |
4231 | ||
4232 | m = gfc_match_char (')'); | |
4233 | if (m != MATCH_YES) | |
4234 | return m; | |
727e8544 | 4235 | } |
6de9cd9a | 4236 | |
1c8bcdf7 PT |
4237 | /* Defer association of the derived type until the end of the |
4238 | specification block. However, if the derived type can be | |
f5acf0f2 | 4239 | found, add it to the typespec. */ |
1c8bcdf7 | 4240 | if (gfc_matching_function) |
e2d29968 | 4241 | { |
bc21d315 | 4242 | ts->u.derived = NULL; |
1c8bcdf7 PT |
4243 | if (gfc_current_state () != COMP_INTERFACE |
4244 | && !gfc_find_symbol (name, NULL, 1, &sym) && sym) | |
c3f34952 TB |
4245 | { |
4246 | sym = gfc_find_dt_in_generic (sym); | |
4247 | ts->u.derived = sym; | |
4248 | } | |
e2d29968 PT |
4249 | return MATCH_YES; |
4250 | } | |
4251 | ||
4252 | /* Search for the name but allow the components to be defined later. If | |
4253 | type = -1, this typespec has been seen in a function declaration but | |
c3f34952 | 4254 | the type could not be accessed at that point. The actual derived type is |
eea58adb | 4255 | stored in a symtree with the first letter of the name capitalized; the |
c3f34952 TB |
4256 | symtree with the all lower-case name contains the associated |
4257 | generic function. */ | |
f6288c24 | 4258 | dt_name = gfc_dt_upper_string (name); |
1c8bcdf7 | 4259 | sym = NULL; |
c3f34952 TB |
4260 | dt_sym = NULL; |
4261 | if (ts->kind != -1) | |
6de9cd9a | 4262 | { |
c3f34952 TB |
4263 | gfc_get_ha_symbol (name, &sym); |
4264 | if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym)) | |
4265 | { | |
c4100eae | 4266 | gfc_error ("Type name %qs at %C is ambiguous", name); |
c3f34952 TB |
4267 | return MATCH_ERROR; |
4268 | } | |
4269 | if (sym->generic && !dt_sym) | |
4270 | dt_sym = gfc_find_dt_in_generic (sym); | |
18a4e7e3 PT |
4271 | |
4272 | /* Host associated PDTs can get confused with their constructors | |
4273 | because they ar instantiated in the template's namespace. */ | |
4274 | if (!dt_sym) | |
4275 | { | |
4276 | if (gfc_find_symbol (dt_name, NULL, 1, &dt_sym)) | |
4277 | { | |
4278 | gfc_error ("Type name %qs at %C is ambiguous", name); | |
4279 | return MATCH_ERROR; | |
4280 | } | |
4281 | if (dt_sym && !dt_sym->attr.pdt_type) | |
4282 | dt_sym = NULL; | |
4283 | } | |
6de9cd9a | 4284 | } |
e2d29968 PT |
4285 | else if (ts->kind == -1) |
4286 | { | |
1c8bcdf7 PT |
4287 | int iface = gfc_state_stack->previous->state != COMP_INTERFACE |
4288 | || gfc_current_ns->has_import_set; | |
c3f34952 TB |
4289 | gfc_find_symbol (name, NULL, iface, &sym); |
4290 | if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym)) | |
f5acf0f2 | 4291 | { |
c4100eae | 4292 | gfc_error ("Type name %qs at %C is ambiguous", name); |
e2d29968 PT |
4293 | return MATCH_ERROR; |
4294 | } | |
c3f34952 TB |
4295 | if (sym && sym->generic && !dt_sym) |
4296 | dt_sym = gfc_find_dt_in_generic (sym); | |
e2d29968 | 4297 | |
1c8bcdf7 | 4298 | ts->kind = 0; |
e2d29968 PT |
4299 | if (sym == NULL) |
4300 | return MATCH_NO; | |
4301 | } | |
6de9cd9a | 4302 | |
f6288c24 | 4303 | if ((sym->attr.flavor != FL_UNKNOWN && sym->attr.flavor != FL_STRUCT |
c3f34952 TB |
4304 | && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic)) |
4305 | || sym->attr.subroutine) | |
4306 | { | |
fea70c99 MLI |
4307 | gfc_error ("Type name %qs at %C conflicts with previously declared " |
4308 | "entity at %L, which has the same name", name, | |
4309 | &sym->declared_at); | |
c3f34952 TB |
4310 | return MATCH_ERROR; |
4311 | } | |
6de9cd9a | 4312 | |
5bab4c96 PT |
4313 | if (sym && sym->attr.flavor == FL_DERIVED |
4314 | && sym->attr.pdt_template | |
4315 | && gfc_current_state () != COMP_DERIVED) | |
18a4e7e3 PT |
4316 | { |
4317 | m = gfc_get_pdt_instance (decl_type_param_list, &sym, NULL); | |
4318 | if (m != MATCH_YES) | |
4319 | return m; | |
4320 | gcc_assert (!sym->attr.pdt_template && sym->attr.pdt_type); | |
4321 | ts->u.derived = sym; | |
4322 | strcpy (name, gfc_dt_lower_string (sym->name)); | |
4323 | } | |
5bab4c96 | 4324 | |
44c57c2f | 4325 | gfc_save_symbol_data (sym); |
1c8bcdf7 | 4326 | gfc_set_sym_referenced (sym); |
c3f34952 | 4327 | if (!sym->attr.generic |
524af0d6 | 4328 | && !gfc_add_generic (&sym->attr, sym->name, NULL)) |
c3f34952 TB |
4329 | return MATCH_ERROR; |
4330 | ||
4331 | if (!sym->attr.function | |
524af0d6 | 4332 | && !gfc_add_function (&sym->attr, sym->name, NULL)) |
c3f34952 TB |
4333 | return MATCH_ERROR; |
4334 | ||
5bab4c96 PT |
4335 | if (dt_sym && dt_sym->attr.flavor == FL_DERIVED |
4336 | && dt_sym->attr.pdt_template | |
4337 | && gfc_current_state () != COMP_DERIVED) | |
4338 | { | |
4339 | m = gfc_get_pdt_instance (decl_type_param_list, &dt_sym, NULL); | |
4340 | if (m != MATCH_YES) | |
4341 | return m; | |
4342 | gcc_assert (!dt_sym->attr.pdt_template && dt_sym->attr.pdt_type); | |
4343 | } | |
4344 | ||
c3f34952 TB |
4345 | if (!dt_sym) |
4346 | { | |
4347 | gfc_interface *intr, *head; | |
4348 | ||
4349 | /* Use upper case to save the actual derived-type symbol. */ | |
4350 | gfc_get_symbol (dt_name, NULL, &dt_sym); | |
51f03c6b | 4351 | dt_sym->name = gfc_get_string ("%s", sym->name); |
c3f34952 TB |
4352 | head = sym->generic; |
4353 | intr = gfc_get_interface (); | |
4354 | intr->sym = dt_sym; | |
4355 | intr->where = gfc_current_locus; | |
4356 | intr->next = head; | |
4357 | sym->generic = intr; | |
4358 | sym->attr.if_source = IFSRC_DECL; | |
4359 | } | |
44c57c2f MM |
4360 | else |
4361 | gfc_save_symbol_data (dt_sym); | |
c3f34952 TB |
4362 | |
4363 | gfc_set_sym_referenced (dt_sym); | |
4364 | ||
f6288c24 | 4365 | if (dt_sym->attr.flavor != FL_DERIVED && dt_sym->attr.flavor != FL_STRUCT |
524af0d6 | 4366 | && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)) |
c3f34952 TB |
4367 | return MATCH_ERROR; |
4368 | ||
4369 | ts->u.derived = dt_sym; | |
6de9cd9a DN |
4370 | |
4371 | return MATCH_YES; | |
4372 | ||
4373 | get_kind: | |
0fb56814 | 4374 | if (matched_type |
524af0d6 JB |
4375 | && !gfc_notify_std (GFC_STD_F2008, "TYPE with " |
4376 | "intrinsic-type-spec at %C")) | |
0fb56814 TB |
4377 | return MATCH_ERROR; |
4378 | ||
6de9cd9a DN |
4379 | /* For all types except double, derived and character, look for an |
4380 | optional kind specifier. MATCH_NO is actually OK at this point. */ | |
e5ddaa24 | 4381 | if (implicit_flag == 1) |
0fb56814 TB |
4382 | { |
4383 | if (matched_type && gfc_match_char (')') != MATCH_YES) | |
4384 | return MATCH_ERROR; | |
4385 | ||
4386 | return MATCH_YES; | |
4387 | } | |
6de9cd9a | 4388 | |
0ff0dfbf TS |
4389 | if (gfc_current_form == FORM_FREE) |
4390 | { | |
0b3624f6 SK |
4391 | c = gfc_peek_ascii_char (); |
4392 | if (!gfc_is_whitespace (c) && c != '*' && c != '(' | |
636dff67 | 4393 | && c != ':' && c != ',') |
0fb56814 TB |
4394 | { |
4395 | if (matched_type && c == ')') | |
4396 | { | |
4397 | gfc_next_ascii_char (); | |
4398 | return MATCH_YES; | |
4399 | } | |
4400 | return MATCH_NO; | |
4401 | } | |
0ff0dfbf TS |
4402 | } |
4403 | ||
e2d29968 | 4404 | m = gfc_match_kind_spec (ts, false); |
6de9cd9a | 4405 | if (m == MATCH_NO && ts->type != BT_CHARACTER) |
4381322d SK |
4406 | { |
4407 | m = gfc_match_old_kind_spec (ts); | |
4408 | if (gfc_validate_kind (ts->type, ts->kind, true) == -1) | |
4409 | return MATCH_ERROR; | |
4410 | } | |
6de9cd9a | 4411 | |
0fb56814 TB |
4412 | if (matched_type && gfc_match_char (')') != MATCH_YES) |
4413 | return MATCH_ERROR; | |
4414 | ||
1c8bcdf7 PT |
4415 | /* Defer association of the KIND expression of function results |
4416 | until after USE and IMPORT statements. */ | |
4417 | if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ()) | |
4418 | || gfc_matching_function) | |
4419 | return MATCH_YES; | |
4420 | ||
6de9cd9a DN |
4421 | if (m == MATCH_NO) |
4422 | m = MATCH_YES; /* No kind specifier found. */ | |
4423 | ||
4424 | return m; | |
4425 | } | |
4426 | ||
4427 | ||
e5ddaa24 TS |
4428 | /* Match an IMPLICIT NONE statement. Actually, this statement is |
4429 | already matched in parse.c, or we would not end up here in the | |
4430 | first place. So the only thing we need to check, is if there is | |
4431 | trailing garbage. If not, the match is successful. */ | |
4432 | ||
4433 | match | |
4434 | gfc_match_implicit_none (void) | |
4435 | { | |
8b7a967e TB |
4436 | char c; |
4437 | match m; | |
4438 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
4439 | bool type = false; | |
4440 | bool external = false; | |
a6c63173 TB |
4441 | locus cur_loc = gfc_current_locus; |
4442 | ||
4443 | if (gfc_current_ns->seen_implicit_none | |
4444 | || gfc_current_ns->has_implicit_none_export) | |
4445 | { | |
4446 | gfc_error ("Duplicate IMPLICIT NONE statement at %C"); | |
4447 | return MATCH_ERROR; | |
4448 | } | |
8b7a967e TB |
4449 | |
4450 | gfc_gobble_whitespace (); | |
4451 | c = gfc_peek_ascii_char (); | |
4452 | if (c == '(') | |
4453 | { | |
4454 | (void) gfc_next_ascii_char (); | |
8179b067 | 4455 | if (!gfc_notify_std (GFC_STD_F2018, "IMPORT NONE with spec list at %C")) |
8b7a967e | 4456 | return MATCH_ERROR; |
a6c63173 TB |
4457 | |
4458 | gfc_gobble_whitespace (); | |
4459 | if (gfc_peek_ascii_char () == ')') | |
8b7a967e | 4460 | { |
a6c63173 TB |
4461 | (void) gfc_next_ascii_char (); |
4462 | type = true; | |
4463 | } | |
4464 | else | |
4465 | for(;;) | |
4466 | { | |
4467 | m = gfc_match (" %n", name); | |
4468 | if (m != MATCH_YES) | |
4469 | return MATCH_ERROR; | |
8b7a967e | 4470 | |
a6c63173 TB |
4471 | if (strcmp (name, "type") == 0) |
4472 | type = true; | |
4473 | else if (strcmp (name, "external") == 0) | |
4474 | external = true; | |
4475 | else | |
4476 | return MATCH_ERROR; | |
8b7a967e | 4477 | |
a6c63173 TB |
4478 | gfc_gobble_whitespace (); |
4479 | c = gfc_next_ascii_char (); | |
4480 | if (c == ',') | |
4481 | continue; | |
4482 | if (c == ')') | |
4483 | break; | |
4484 | return MATCH_ERROR; | |
4485 | } | |
8b7a967e TB |
4486 | } |
4487 | else | |
4488 | type = true; | |
4489 | ||
4490 | if (gfc_match_eos () != MATCH_YES) | |
4491 | return MATCH_ERROR; | |
4492 | ||
a6c63173 | 4493 | gfc_set_implicit_none (type, external, &cur_loc); |
8b7a967e TB |
4494 | |
4495 | return MATCH_YES; | |
e5ddaa24 TS |
4496 | } |
4497 | ||
4498 | ||
4499 | /* Match the letter range(s) of an IMPLICIT statement. */ | |
4500 | ||
4501 | static match | |
1107b970 | 4502 | match_implicit_range (void) |
e5ddaa24 | 4503 | { |
8fc541d3 FXC |
4504 | char c, c1, c2; |
4505 | int inner; | |
e5ddaa24 TS |
4506 | locus cur_loc; |
4507 | ||
4508 | cur_loc = gfc_current_locus; | |
4509 | ||
4510 | gfc_gobble_whitespace (); | |
8fc541d3 | 4511 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
4512 | if (c != '(') |
4513 | { | |
4514 | gfc_error ("Missing character range in IMPLICIT at %C"); | |
4515 | goto bad; | |
4516 | } | |
4517 | ||
4518 | inner = 1; | |
4519 | while (inner) | |
4520 | { | |
4521 | gfc_gobble_whitespace (); | |
8fc541d3 | 4522 | c1 = gfc_next_ascii_char (); |
e5ddaa24 TS |
4523 | if (!ISALPHA (c1)) |
4524 | goto bad; | |
4525 | ||
4526 | gfc_gobble_whitespace (); | |
8fc541d3 | 4527 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
4528 | |
4529 | switch (c) | |
4530 | { | |
4531 | case ')': | |
66e4ab31 | 4532 | inner = 0; /* Fall through. */ |
e5ddaa24 TS |
4533 | |
4534 | case ',': | |
4535 | c2 = c1; | |
4536 | break; | |
4537 | ||
4538 | case '-': | |
4539 | gfc_gobble_whitespace (); | |
8fc541d3 | 4540 | c2 = gfc_next_ascii_char (); |
e5ddaa24 TS |
4541 | if (!ISALPHA (c2)) |
4542 | goto bad; | |
4543 | ||
4544 | gfc_gobble_whitespace (); | |
8fc541d3 | 4545 | c = gfc_next_ascii_char (); |
e5ddaa24 TS |
4546 | |
4547 | if ((c != ',') && (c != ')')) | |
4548 | goto bad; | |
4549 | if (c == ')') | |
4550 | inner = 0; | |
4551 | ||
4552 | break; | |
4553 | ||
4554 | default: | |
4555 | goto bad; | |
4556 | } | |
4557 | ||
4558 | if (c1 > c2) | |
4559 | { | |
4560 | gfc_error ("Letters must be in alphabetic order in " | |
4561 | "IMPLICIT statement at %C"); | |
4562 | goto bad; | |
4563 | } | |
4564 | ||
4565 | /* See if we can add the newly matched range to the pending | |
636dff67 SK |
4566 | implicits from this IMPLICIT statement. We do not check for |
4567 | conflicts with whatever earlier IMPLICIT statements may have | |
4568 | set. This is done when we've successfully finished matching | |
4569 | the current one. */ | |
524af0d6 | 4570 | if (!gfc_add_new_implicit_range (c1, c2)) |
e5ddaa24 TS |
4571 | goto bad; |
4572 | } | |
4573 | ||
4574 | return MATCH_YES; | |
4575 | ||
4576 | bad: | |
4577 | gfc_syntax_error (ST_IMPLICIT); | |
4578 | ||
4579 | gfc_current_locus = cur_loc; | |
4580 | return MATCH_ERROR; | |
4581 | } | |
4582 | ||
4583 | ||
4584 | /* Match an IMPLICIT statement, storing the types for | |
4585 | gfc_set_implicit() if the statement is accepted by the parser. | |
4586 | There is a strange looking, but legal syntactic construction | |
4587 | possible. It looks like: | |
4588 | ||
4589 | IMPLICIT INTEGER (a-b) (c-d) | |
4590 | ||
4591 | This is legal if "a-b" is a constant expression that happens to | |
4592 | equal one of the legal kinds for integers. The real problem | |
4593 | happens with an implicit specification that looks like: | |
4594 | ||
4595 | IMPLICIT INTEGER (a-b) | |
4596 | ||
4597 | In this case, a typespec matcher that is "greedy" (as most of the | |
4598 | matchers are) gobbles the character range as a kindspec, leaving | |
4599 | nothing left. We therefore have to go a bit more slowly in the | |
4600 | matching process by inhibiting the kindspec checking during | |
4601 | typespec matching and checking for a kind later. */ | |
4602 | ||
4603 | match | |
4604 | gfc_match_implicit (void) | |
4605 | { | |
4606 | gfc_typespec ts; | |
4607 | locus cur_loc; | |
8fc541d3 | 4608 | char c; |
e5ddaa24 TS |
4609 | match m; |
4610 | ||
8b7a967e TB |
4611 | if (gfc_current_ns->seen_implicit_none) |
4612 | { | |
4613 | gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) " | |
4614 | "statement"); | |
4615 | return MATCH_ERROR; | |
4616 | } | |
4617 | ||
44000dbb JD |
4618 | gfc_clear_ts (&ts); |
4619 | ||
e5ddaa24 TS |
4620 | /* We don't allow empty implicit statements. */ |
4621 | if (gfc_match_eos () == MATCH_YES) | |
4622 | { | |
4623 | gfc_error ("Empty IMPLICIT statement at %C"); | |
4624 | return MATCH_ERROR; | |
4625 | } | |
4626 | ||
e5ddaa24 TS |
4627 | do |
4628 | { | |
1107b970 PB |
4629 | /* First cleanup. */ |
4630 | gfc_clear_new_implicit (); | |
4631 | ||
e5ddaa24 | 4632 | /* A basic type is mandatory here. */ |
e74f1cc8 | 4633 | m = gfc_match_decl_type_spec (&ts, 1); |
e5ddaa24 TS |
4634 | if (m == MATCH_ERROR) |
4635 | goto error; | |
4636 | if (m == MATCH_NO) | |
4637 | goto syntax; | |
4638 | ||
4639 | cur_loc = gfc_current_locus; | |
1107b970 | 4640 | m = match_implicit_range (); |
e5ddaa24 TS |
4641 | |
4642 | if (m == MATCH_YES) | |
4643 | { | |
1107b970 | 4644 | /* We may have <TYPE> (<RANGE>). */ |
e5ddaa24 | 4645 | gfc_gobble_whitespace (); |
a6c63173 TB |
4646 | c = gfc_peek_ascii_char (); |
4647 | if (c == ',' || c == '\n' || c == ';' || c == '!') | |
1107b970 PB |
4648 | { |
4649 | /* Check for CHARACTER with no length parameter. */ | |
bc21d315 | 4650 | if (ts.type == BT_CHARACTER && !ts.u.cl) |
1107b970 | 4651 | { |
9d64df18 | 4652 | ts.kind = gfc_default_character_kind; |
b76e28c6 | 4653 | ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
f622221a | 4654 | ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind, |
b7e75771 | 4655 | NULL, 1); |
1107b970 PB |
4656 | } |
4657 | ||
4658 | /* Record the Successful match. */ | |
524af0d6 | 4659 | if (!gfc_merge_new_implicit (&ts)) |
1107b970 | 4660 | return MATCH_ERROR; |
a6c63173 TB |
4661 | if (c == ',') |
4662 | c = gfc_next_ascii_char (); | |
4663 | else if (gfc_match_eos () == MATCH_ERROR) | |
4664 | goto error; | |
1107b970 PB |
4665 | continue; |
4666 | } | |
e5ddaa24 TS |
4667 | |
4668 | gfc_current_locus = cur_loc; | |
4669 | } | |
4670 | ||
1107b970 PB |
4671 | /* Discard the (incorrectly) matched range. */ |
4672 | gfc_clear_new_implicit (); | |
4673 | ||
4674 | /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */ | |
4675 | if (ts.type == BT_CHARACTER) | |
8234e5e0 | 4676 | m = gfc_match_char_spec (&ts); |
1107b970 | 4677 | else |
e5ddaa24 | 4678 | { |
e2d29968 | 4679 | m = gfc_match_kind_spec (&ts, false); |
e5ddaa24 | 4680 | if (m == MATCH_NO) |
1107b970 PB |
4681 | { |
4682 | m = gfc_match_old_kind_spec (&ts); | |
4683 | if (m == MATCH_ERROR) | |
4684 | goto error; | |
4685 | if (m == MATCH_NO) | |
4686 | goto syntax; | |
4687 | } | |
e5ddaa24 | 4688 | } |
1107b970 PB |
4689 | if (m == MATCH_ERROR) |
4690 | goto error; | |
e5ddaa24 | 4691 | |
1107b970 | 4692 | m = match_implicit_range (); |
e5ddaa24 TS |
4693 | if (m == MATCH_ERROR) |
4694 | goto error; | |
4695 | if (m == MATCH_NO) | |
4696 | goto syntax; | |
4697 | ||
4698 | gfc_gobble_whitespace (); | |
8fc541d3 | 4699 | c = gfc_next_ascii_char (); |
a6c63173 | 4700 | if (c != ',' && gfc_match_eos () != MATCH_YES) |
e5ddaa24 TS |
4701 | goto syntax; |
4702 | ||
524af0d6 | 4703 | if (!gfc_merge_new_implicit (&ts)) |
1107b970 | 4704 | return MATCH_ERROR; |
e5ddaa24 TS |
4705 | } |
4706 | while (c == ','); | |
4707 | ||
1107b970 | 4708 | return MATCH_YES; |
e5ddaa24 TS |
4709 | |
4710 | syntax: | |
4711 | gfc_syntax_error (ST_IMPLICIT); | |
4712 | ||
4713 | error: | |
4714 | return MATCH_ERROR; | |
4715 | } | |
4716 | ||
66e4ab31 | 4717 | |
8998be20 TB |
4718 | match |
4719 | gfc_match_import (void) | |
4720 | { | |
4721 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
4722 | match m; | |
4723 | gfc_symbol *sym; | |
4724 | gfc_symtree *st; | |
4725 | ||
66e4ab31 SK |
4726 | if (gfc_current_ns->proc_name == NULL |
4727 | || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY) | |
8998be20 TB |
4728 | { |
4729 | gfc_error ("IMPORT statement at %C only permitted in " | |
4730 | "an INTERFACE body"); | |
4731 | return MATCH_ERROR; | |
4732 | } | |
4733 | ||
4668d6f9 PT |
4734 | if (gfc_current_ns->proc_name->attr.module_procedure) |
4735 | { | |
4736 | gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted " | |
4737 | "in a module procedure interface body"); | |
4738 | return MATCH_ERROR; | |
4739 | } | |
4740 | ||
524af0d6 | 4741 | if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C")) |
8998be20 TB |
4742 | return MATCH_ERROR; |
4743 | ||
4744 | if (gfc_match_eos () == MATCH_YES) | |
4745 | { | |
4746 | /* All host variables should be imported. */ | |
4747 | gfc_current_ns->has_import_set = 1; | |
4748 | return MATCH_YES; | |
4749 | } | |
4750 | ||
4751 | if (gfc_match (" ::") == MATCH_YES) | |
4752 | { | |
4753 | if (gfc_match_eos () == MATCH_YES) | |
636dff67 SK |
4754 | { |
4755 | gfc_error ("Expecting list of named entities at %C"); | |
4756 | return MATCH_ERROR; | |
4757 | } | |
8998be20 TB |
4758 | } |
4759 | ||
4760 | for(;;) | |
4761 | { | |
2e8d9212 | 4762 | sym = NULL; |
8998be20 TB |
4763 | m = gfc_match (" %n", name); |
4764 | switch (m) | |
4765 | { | |
4766 | case MATCH_YES: | |
36d3fb4c | 4767 | if (gfc_current_ns->parent != NULL |
66e4ab31 | 4768 | && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym)) |
36d3fb4c | 4769 | { |
c4100eae | 4770 | gfc_error ("Type name %qs at %C is ambiguous", name); |
36d3fb4c PT |
4771 | return MATCH_ERROR; |
4772 | } | |
4e2cf5f5 | 4773 | else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL |
66e4ab31 SK |
4774 | && gfc_find_symbol (name, |
4775 | gfc_current_ns->proc_name->ns->parent, | |
4776 | 1, &sym)) | |
636dff67 | 4777 | { |
c4100eae | 4778 | gfc_error ("Type name %qs at %C is ambiguous", name); |
636dff67 SK |
4779 | return MATCH_ERROR; |
4780 | } | |
4781 | ||
4782 | if (sym == NULL) | |
4783 | { | |
c4100eae | 4784 | gfc_error ("Cannot IMPORT %qs from host scoping unit " |
636dff67 SK |
4785 | "at %C - does not exist.", name); |
4786 | return MATCH_ERROR; | |
4787 | } | |
4788 | ||
dd8b9dde | 4789 | if (gfc_find_symtree (gfc_current_ns->sym_root, name)) |
636dff67 | 4790 | { |
db30e21c | 4791 | gfc_warning (0, "%qs is already IMPORTed from host scoping unit " |
48749dbc | 4792 | "at %C", name); |
636dff67 SK |
4793 | goto next_item; |
4794 | } | |
4795 | ||
dd8b9dde | 4796 | st = gfc_new_symtree (&gfc_current_ns->sym_root, name); |
636dff67 SK |
4797 | st->n.sym = sym; |
4798 | sym->refs++; | |
5a8af0b4 | 4799 | sym->attr.imported = 1; |
8998be20 | 4800 | |
c3f34952 TB |
4801 | if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym))) |
4802 | { | |
4803 | /* The actual derived type is stored in a symtree with the first | |
eea58adb | 4804 | letter of the name capitalized; the symtree with the all |
1cc0e193 | 4805 | lower-case name contains the associated generic function. */ |
c3f34952 | 4806 | st = gfc_new_symtree (&gfc_current_ns->sym_root, |
f6288c24 | 4807 | gfc_dt_upper_string (name)); |
c3f34952 TB |
4808 | st->n.sym = sym; |
4809 | sym->refs++; | |
4810 | sym->attr.imported = 1; | |
4811 | } | |
4812 | ||
8998be20 TB |
4813 | goto next_item; |
4814 | ||
4815 | case MATCH_NO: | |
4816 | break; | |
4817 | ||
4818 | case MATCH_ERROR: | |
4819 | return MATCH_ERROR; | |
4820 | } | |
4821 | ||
4822 | next_item: | |
4823 | if (gfc_match_eos () == MATCH_YES) | |
4824 | break; | |
4825 | if (gfc_match_char (',') != MATCH_YES) | |
4826 | goto syntax; | |
4827 | } | |
4828 | ||
4829 | return MATCH_YES; | |
4830 | ||
4831 | syntax: | |
4832 | gfc_error ("Syntax error in IMPORT statement at %C"); | |
4833 | return MATCH_ERROR; | |
4834 | } | |
e5ddaa24 | 4835 | |
66e4ab31 | 4836 | |
f2449db4 RS |
4837 | /* A minimal implementation of gfc_match without whitespace, escape |
4838 | characters or variable arguments. Returns true if the next | |
4839 | characters match the TARGET template exactly. */ | |
4840 | ||
4841 | static bool | |
4842 | match_string_p (const char *target) | |
4843 | { | |
4844 | const char *p; | |
4845 | ||
4846 | for (p = target; *p; p++) | |
8fc541d3 | 4847 | if ((char) gfc_next_ascii_char () != *p) |
f2449db4 RS |
4848 | return false; |
4849 | return true; | |
4850 | } | |
4851 | ||
6de9cd9a DN |
4852 | /* Matches an attribute specification including array specs. If |
4853 | successful, leaves the variables current_attr and current_as | |
4854 | holding the specification. Also sets the colon_seen variable for | |
4855 | later use by matchers associated with initializations. | |
4856 | ||
4857 | This subroutine is a little tricky in the sense that we don't know | |
4858 | if we really have an attr-spec until we hit the double colon. | |
4859 | Until that time, we can only return MATCH_NO. This forces us to | |
4860 | check for duplicate specification at this level. */ | |
4861 | ||
4862 | static match | |
4863 | match_attr_spec (void) | |
4864 | { | |
6de9cd9a | 4865 | /* Modifiers that can exist in a type statement. */ |
d75d9546 | 4866 | enum |
ea20e8be JW |
4867 | { GFC_DECL_BEGIN = 0, DECL_ALLOCATABLE = GFC_DECL_BEGIN, |
4868 | DECL_IN = INTENT_IN, DECL_OUT = INTENT_OUT, DECL_INOUT = INTENT_INOUT, | |
4869 | DECL_DIMENSION, DECL_EXTERNAL, | |
4870 | DECL_INTRINSIC, DECL_OPTIONAL, | |
ee7e677f | 4871 | DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE, |
34d567d1 | 4872 | DECL_STATIC, DECL_AUTOMATIC, |
ee7e677f | 4873 | DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE, |
fe4e525c | 4874 | DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS, |
5bab4c96 | 4875 | DECL_LEN, DECL_KIND, DECL_NONE, GFC_DECL_END /* Sentinel */ |
d75d9546 | 4876 | }; |
6de9cd9a DN |
4877 | |
4878 | /* GFC_DECL_END is the sentinel, index starts at 0. */ | |
4879 | #define NUM_DECL GFC_DECL_END | |
4880 | ||
ea20e8be JW |
4881 | /* Make sure that values from sym_intent are safe to be used here. */ |
4882 | gcc_assert (INTENT_IN > 0); | |
4883 | ||
6de9cd9a DN |
4884 | locus start, seen_at[NUM_DECL]; |
4885 | int seen[NUM_DECL]; | |
09639a83 | 4886 | unsigned int d; |
6de9cd9a DN |
4887 | const char *attr; |
4888 | match m; | |
524af0d6 | 4889 | bool t; |
6de9cd9a DN |
4890 | |
4891 | gfc_clear_attr (¤t_attr); | |
63645982 | 4892 | start = gfc_current_locus; |
6de9cd9a DN |
4893 | |
4894 | current_as = NULL; | |
4895 | colon_seen = 0; | |
6f855a26 | 4896 | attr_seen = 0; |
6de9cd9a DN |
4897 | |
4898 | /* See if we get all of the keywords up to the final double colon. */ | |
4899 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
4900 | seen[d] = 0; | |
4901 | ||
4902 | for (;;) | |
4903 | { | |
8fc541d3 | 4904 | char ch; |
a8b3b0b6 | 4905 | |
f2449db4 RS |
4906 | d = DECL_NONE; |
4907 | gfc_gobble_whitespace (); | |
4908 | ||
8fc541d3 | 4909 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
4910 | if (ch == ':') |
4911 | { | |
4912 | /* This is the successful exit condition for the loop. */ | |
8fc541d3 | 4913 | if (gfc_next_ascii_char () == ':') |
f2449db4 RS |
4914 | break; |
4915 | } | |
4916 | else if (ch == ',') | |
a8b3b0b6 | 4917 | { |
a8b3b0b6 | 4918 | gfc_gobble_whitespace (); |
8fc541d3 | 4919 | switch (gfc_peek_ascii_char ()) |
a8b3b0b6 | 4920 | { |
f2449db4 | 4921 | case 'a': |
1eee5628 TB |
4922 | gfc_next_ascii_char (); |
4923 | switch (gfc_next_ascii_char ()) | |
4924 | { | |
4925 | case 'l': | |
4926 | if (match_string_p ("locatable")) | |
4927 | { | |
4928 | /* Matched "allocatable". */ | |
4929 | d = DECL_ALLOCATABLE; | |
4930 | } | |
4931 | break; | |
4932 | ||
4933 | case 's': | |
4934 | if (match_string_p ("ynchronous")) | |
4935 | { | |
4936 | /* Matched "asynchronous". */ | |
4937 | d = DECL_ASYNCHRONOUS; | |
4938 | } | |
4939 | break; | |
34d567d1 FR |
4940 | |
4941 | case 'u': | |
4942 | if (match_string_p ("tomatic")) | |
4943 | { | |
4944 | /* Matched "automatic". */ | |
4945 | d = DECL_AUTOMATIC; | |
4946 | } | |
4947 | break; | |
1eee5628 | 4948 | } |
fe4e525c | 4949 | break; |
f2449db4 RS |
4950 | |
4951 | case 'b': | |
a8b3b0b6 | 4952 | /* Try and match the bind(c). */ |
1eabf70a | 4953 | m = gfc_match_bind_c (NULL, true); |
129d15a3 | 4954 | if (m == MATCH_YES) |
a8b3b0b6 | 4955 | d = DECL_IS_BIND_C; |
129d15a3 JW |
4956 | else if (m == MATCH_ERROR) |
4957 | goto cleanup; | |
f2449db4 RS |
4958 | break; |
4959 | ||
be59db2d | 4960 | case 'c': |
fe4e525c TB |
4961 | gfc_next_ascii_char (); |
4962 | if ('o' != gfc_next_ascii_char ()) | |
4963 | break; | |
4964 | switch (gfc_next_ascii_char ()) | |
4965 | { | |
4966 | case 'd': | |
4967 | if (match_string_p ("imension")) | |
4968 | { | |
4969 | d = DECL_CODIMENSION; | |
4970 | break; | |
4971 | } | |
191816a3 | 4972 | /* FALLTHRU */ |
fe4e525c TB |
4973 | case 'n': |
4974 | if (match_string_p ("tiguous")) | |
4975 | { | |
4976 | d = DECL_CONTIGUOUS; | |
4977 | break; | |
4978 | } | |
4979 | } | |
be59db2d TB |
4980 | break; |
4981 | ||
f2449db4 RS |
4982 | case 'd': |
4983 | if (match_string_p ("dimension")) | |
4984 | d = DECL_DIMENSION; | |
4985 | break; | |
4986 | ||
4987 | case 'e': | |
4988 | if (match_string_p ("external")) | |
4989 | d = DECL_EXTERNAL; | |
4990 | break; | |
4991 | ||
4992 | case 'i': | |
4993 | if (match_string_p ("int")) | |
4994 | { | |
8fc541d3 | 4995 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
4996 | if (ch == 'e') |
4997 | { | |
4998 | if (match_string_p ("nt")) | |
4999 | { | |
5000 | /* Matched "intent". */ | |
ea20e8be JW |
5001 | d = match_intent_spec (); |
5002 | if (d == INTENT_UNKNOWN) | |
5003 | { | |
5004 | m = MATCH_ERROR; | |
5005 | goto cleanup; | |
5006 | } | |
f2449db4 RS |
5007 | } |
5008 | } | |
5009 | else if (ch == 'r') | |
5010 | { | |
5011 | if (match_string_p ("insic")) | |
5012 | { | |
5013 | /* Matched "intrinsic". */ | |
5014 | d = DECL_INTRINSIC; | |
5015 | } | |
5016 | } | |
5017 | } | |
5018 | break; | |
5019 | ||
5bab4c96 PT |
5020 | case 'k': |
5021 | if (match_string_p ("kind")) | |
5022 | d = DECL_KIND; | |
5023 | break; | |
5024 | ||
5025 | case 'l': | |
5026 | if (match_string_p ("len")) | |
5027 | d = DECL_LEN; | |
5028 | break; | |
5029 | ||
f2449db4 RS |
5030 | case 'o': |
5031 | if (match_string_p ("optional")) | |
5032 | d = DECL_OPTIONAL; | |
5033 | break; | |
5034 | ||
5035 | case 'p': | |
8fc541d3 FXC |
5036 | gfc_next_ascii_char (); |
5037 | switch (gfc_next_ascii_char ()) | |
f2449db4 RS |
5038 | { |
5039 | case 'a': | |
5040 | if (match_string_p ("rameter")) | |
5041 | { | |
5042 | /* Matched "parameter". */ | |
5043 | d = DECL_PARAMETER; | |
5044 | } | |
5045 | break; | |
5046 | ||
5047 | case 'o': | |
5048 | if (match_string_p ("inter")) | |
5049 | { | |
5050 | /* Matched "pointer". */ | |
5051 | d = DECL_POINTER; | |
5052 | } | |
5053 | break; | |
5054 | ||
5055 | case 'r': | |
8fc541d3 | 5056 | ch = gfc_next_ascii_char (); |
f2449db4 RS |
5057 | if (ch == 'i') |
5058 | { | |
5059 | if (match_string_p ("vate")) | |
5060 | { | |
5061 | /* Matched "private". */ | |
5062 | d = DECL_PRIVATE; | |
5063 | } | |
5064 | } | |
5065 | else if (ch == 'o') | |
5066 | { | |
5067 | if (match_string_p ("tected")) | |
5068 | { | |
5069 | /* Matched "protected". */ | |
5070 | d = DECL_PROTECTED; | |
5071 | } | |
5072 | } | |
5073 | break; | |
5074 | ||
5075 | case 'u': | |
5076 | if (match_string_p ("blic")) | |
5077 | { | |
5078 | /* Matched "public". */ | |
5079 | d = DECL_PUBLIC; | |
5080 | } | |
5081 | break; | |
5082 | } | |
5083 | break; | |
5084 | ||
5085 | case 's': | |
34d567d1 FR |
5086 | gfc_next_ascii_char (); |
5087 | switch (gfc_next_ascii_char ()) | |
5088 | { | |
5089 | case 'a': | |
5090 | if (match_string_p ("ve")) | |
5091 | { | |
5092 | /* Matched "save". */ | |
5093 | d = DECL_SAVE; | |
5094 | } | |
5095 | break; | |
5096 | ||
5097 | case 't': | |
5098 | if (match_string_p ("atic")) | |
5099 | { | |
5100 | /* Matched "static". */ | |
5101 | d = DECL_STATIC; | |
5102 | } | |
5103 | break; | |
5104 | } | |
f2449db4 RS |
5105 | break; |
5106 | ||
5107 | case 't': | |
5108 | if (match_string_p ("target")) | |
5109 | d = DECL_TARGET; | |
5110 | break; | |
5111 | ||
5112 | case 'v': | |
8fc541d3 FXC |
5113 | gfc_next_ascii_char (); |
5114 | ch = gfc_next_ascii_char (); | |
f2449db4 RS |
5115 | if (ch == 'a') |
5116 | { | |
5117 | if (match_string_p ("lue")) | |
5118 | { | |
5119 | /* Matched "value". */ | |
5120 | d = DECL_VALUE; | |
5121 | } | |
5122 | } | |
5123 | else if (ch == 'o') | |
5124 | { | |
5125 | if (match_string_p ("latile")) | |
5126 | { | |
5127 | /* Matched "volatile". */ | |
5128 | d = DECL_VOLATILE; | |
5129 | } | |
5130 | } | |
5131 | break; | |
a8b3b0b6 CR |
5132 | } |
5133 | } | |
d468bcdb | 5134 | |
f2449db4 RS |
5135 | /* No double colon and no recognizable decl_type, so assume that |
5136 | we've been looking at something else the whole time. */ | |
5137 | if (d == DECL_NONE) | |
5138 | { | |
5139 | m = MATCH_NO; | |
5140 | goto cleanup; | |
5141 | } | |
d51347f9 | 5142 | |
acb388a0 JD |
5143 | /* Check to make sure any parens are paired up correctly. */ |
5144 | if (gfc_match_parens () == MATCH_ERROR) | |
5145 | { | |
5146 | m = MATCH_ERROR; | |
5147 | goto cleanup; | |
5148 | } | |
5149 | ||
6de9cd9a | 5150 | seen[d]++; |
63645982 | 5151 | seen_at[d] = gfc_current_locus; |
6de9cd9a | 5152 | |
d3a9eea2 | 5153 | if (d == DECL_DIMENSION || d == DECL_CODIMENSION) |
6de9cd9a | 5154 | { |
d3a9eea2 | 5155 | gfc_array_spec *as = NULL; |
6de9cd9a | 5156 | |
d3a9eea2 TB |
5157 | m = gfc_match_array_spec (&as, d == DECL_DIMENSION, |
5158 | d == DECL_CODIMENSION); | |
5159 | ||
5160 | if (current_as == NULL) | |
5161 | current_as = as; | |
5162 | else if (m == MATCH_YES) | |
6de9cd9a | 5163 | { |
524af0d6 | 5164 | if (!merge_array_spec (as, current_as, false)) |
63fbf586 | 5165 | m = MATCH_ERROR; |
cede9502 | 5166 | free (as); |
6de9cd9a DN |
5167 | } |
5168 | ||
be59db2d TB |
5169 | if (m == MATCH_NO) |
5170 | { | |
d3a9eea2 TB |
5171 | if (d == DECL_CODIMENSION) |
5172 | gfc_error ("Missing codimension specification at %C"); | |
5173 | else | |
5174 | gfc_error ("Missing dimension specification at %C"); | |
be59db2d TB |
5175 | m = MATCH_ERROR; |
5176 | } | |
5177 | ||
5178 | if (m == MATCH_ERROR) | |
5179 | goto cleanup; | |
5180 | } | |
6de9cd9a DN |
5181 | } |
5182 | ||
6de9cd9a DN |
5183 | /* Since we've seen a double colon, we have to be looking at an |
5184 | attr-spec. This means that we can now issue errors. */ | |
5185 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
5186 | if (seen[d] > 1) | |
5187 | { | |
5188 | switch (d) | |
5189 | { | |
5190 | case DECL_ALLOCATABLE: | |
5191 | attr = "ALLOCATABLE"; | |
5192 | break; | |
1eee5628 TB |
5193 | case DECL_ASYNCHRONOUS: |
5194 | attr = "ASYNCHRONOUS"; | |
5195 | break; | |
be59db2d TB |
5196 | case DECL_CODIMENSION: |
5197 | attr = "CODIMENSION"; | |
5198 | break; | |
fe4e525c TB |
5199 | case DECL_CONTIGUOUS: |
5200 | attr = "CONTIGUOUS"; | |
5201 | break; | |
6de9cd9a DN |
5202 | case DECL_DIMENSION: |
5203 | attr = "DIMENSION"; | |
5204 | break; | |
5205 | case DECL_EXTERNAL: | |
5206 | attr = "EXTERNAL"; | |
5207 | break; | |
5208 | case DECL_IN: | |
5209 | attr = "INTENT (IN)"; | |
5210 | break; | |
5211 | case DECL_OUT: | |
5212 | attr = "INTENT (OUT)"; | |
5213 | break; | |
5214 | case DECL_INOUT: | |
5215 | attr = "INTENT (IN OUT)"; | |
5216 | break; | |
5217 | case DECL_INTRINSIC: | |
5218 | attr = "INTRINSIC"; | |
5219 | break; | |
5220 | case DECL_OPTIONAL: | |
5221 | attr = "OPTIONAL"; | |
5222 | break; | |
5bab4c96 PT |
5223 | case DECL_KIND: |
5224 | attr = "KIND"; | |
5225 | break; | |
5226 | case DECL_LEN: | |
5227 | attr = "LEN"; | |
5228 | break; | |
6de9cd9a DN |
5229 | case DECL_PARAMETER: |
5230 | attr = "PARAMETER"; | |
5231 | break; | |
5232 | case DECL_POINTER: | |
5233 | attr = "POINTER"; | |
5234 | break; | |
ee7e677f TB |
5235 | case DECL_PROTECTED: |
5236 | attr = "PROTECTED"; | |
5237 | break; | |
6de9cd9a DN |
5238 | case DECL_PRIVATE: |
5239 | attr = "PRIVATE"; | |
5240 | break; | |
5241 | case DECL_PUBLIC: | |
5242 | attr = "PUBLIC"; | |
5243 | break; | |
5244 | case DECL_SAVE: | |
5245 | attr = "SAVE"; | |
5246 | break; | |
34d567d1 FR |
5247 | case DECL_STATIC: |
5248 | attr = "STATIC"; | |
5249 | break; | |
5250 | case DECL_AUTOMATIC: | |
5251 | attr = "AUTOMATIC"; | |
5252 | break; | |
6de9cd9a DN |
5253 | case DECL_TARGET: |
5254 | attr = "TARGET"; | |
5255 | break; | |
a8b3b0b6 CR |
5256 | case DECL_IS_BIND_C: |
5257 | attr = "IS_BIND_C"; | |
5258 | break; | |
5259 | case DECL_VALUE: | |
5260 | attr = "VALUE"; | |
5261 | break; | |
775e6c3a TB |
5262 | case DECL_VOLATILE: |
5263 | attr = "VOLATILE"; | |
5264 | break; | |
6de9cd9a | 5265 | default: |
66e4ab31 | 5266 | attr = NULL; /* This shouldn't happen. */ |
6de9cd9a DN |
5267 | } |
5268 | ||
5269 | gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]); | |
5270 | m = MATCH_ERROR; | |
5271 | goto cleanup; | |
5272 | } | |
5273 | ||
5274 | /* Now that we've dealt with duplicate attributes, add the attributes | |
5275 | to the current attribute. */ | |
5276 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
5277 | { | |
5278 | if (seen[d] == 0) | |
5279 | continue; | |
6f855a26 FR |
5280 | else |
5281 | attr_seen = 1; | |
6de9cd9a | 5282 | |
34d567d1 FR |
5283 | if ((d == DECL_STATIC || d == DECL_AUTOMATIC) |
5284 | && !flag_dec_static) | |
5285 | { | |
cf004230 FR |
5286 | gfc_error ("%s at %L is a DEC extension, enable with " |
5287 | "%<-fdec-static%>", | |
34d567d1 FR |
5288 | d == DECL_STATIC ? "STATIC" : "AUTOMATIC", &seen_at[d]); |
5289 | m = MATCH_ERROR; | |
5290 | goto cleanup; | |
5291 | } | |
5292 | /* Allow SAVE with STATIC, but don't complain. */ | |
5293 | if (d == DECL_STATIC && seen[DECL_SAVE]) | |
5294 | continue; | |
5295 | ||
6de9cd9a | 5296 | if (gfc_current_state () == COMP_DERIVED |
be59db2d TB |
5297 | && d != DECL_DIMENSION && d != DECL_CODIMENSION |
5298 | && d != DECL_POINTER && d != DECL_PRIVATE | |
fe4e525c | 5299 | && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE) |
6de9cd9a | 5300 | { |
5046aff5 PT |
5301 | if (d == DECL_ALLOCATABLE) |
5302 | { | |
524af0d6 JB |
5303 | if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE " |
5304 | "attribute at %C in a TYPE definition")) | |
5046aff5 PT |
5305 | { |
5306 | m = MATCH_ERROR; | |
5307 | goto cleanup; | |
5308 | } | |
636dff67 | 5309 | } |
5bab4c96 PT |
5310 | else if (d == DECL_KIND) |
5311 | { | |
5312 | if (!gfc_notify_std (GFC_STD_F2003, "KIND " | |
5313 | "attribute at %C in a TYPE definition")) | |
5314 | { | |
5315 | m = MATCH_ERROR; | |
5316 | goto cleanup; | |
5317 | } | |
5318 | if (current_ts.type != BT_INTEGER) | |
5319 | { | |
5320 | gfc_error ("Component with KIND attribute at %C must be " | |
5321 | "INTEGER"); | |
5322 | m = MATCH_ERROR; | |
5323 | goto cleanup; | |
5324 | } | |
5325 | if (current_ts.kind != gfc_default_integer_kind) | |
5326 | { | |
5327 | gfc_error ("Component with KIND attribute at %C must be " | |
5328 | "default integer kind (%d)", | |
5329 | gfc_default_integer_kind); | |
5330 | m = MATCH_ERROR; | |
5331 | goto cleanup; | |
5332 | } | |
5333 | } | |
5334 | else if (d == DECL_LEN) | |
5335 | { | |
5336 | if (!gfc_notify_std (GFC_STD_F2003, "LEN " | |
5337 | "attribute at %C in a TYPE definition")) | |
5338 | { | |
5339 | m = MATCH_ERROR; | |
5340 | goto cleanup; | |
5341 | } | |
5342 | if (current_ts.type != BT_INTEGER) | |
5343 | { | |
5344 | gfc_error ("Component with LEN attribute at %C must be " | |
5345 | "INTEGER"); | |
5346 | m = MATCH_ERROR; | |
5347 | goto cleanup; | |
5348 | } | |
5349 | if (current_ts.kind != gfc_default_integer_kind) | |
5350 | { | |
5351 | gfc_error ("Component with LEN attribute at %C must be " | |
5352 | "default integer kind (%d)", | |
5353 | gfc_default_integer_kind); | |
5354 | m = MATCH_ERROR; | |
5355 | goto cleanup; | |
5356 | } | |
5357 | } | |
636dff67 | 5358 | else |
5046aff5 PT |
5359 | { |
5360 | gfc_error ("Attribute at %L is not allowed in a TYPE definition", | |
d51347f9 | 5361 | &seen_at[d]); |
5046aff5 PT |
5362 | m = MATCH_ERROR; |
5363 | goto cleanup; | |
5364 | } | |
6de9cd9a DN |
5365 | } |
5366 | ||
4213f93b | 5367 | if ((d == DECL_PRIVATE || d == DECL_PUBLIC) |
636dff67 | 5368 | && gfc_current_state () != COMP_MODULE) |
4213f93b PT |
5369 | { |
5370 | if (d == DECL_PRIVATE) | |
5371 | attr = "PRIVATE"; | |
5372 | else | |
5373 | attr = "PUBLIC"; | |
d51347f9 TB |
5374 | if (gfc_current_state () == COMP_DERIVED |
5375 | && gfc_state_stack->previous | |
5376 | && gfc_state_stack->previous->state == COMP_MODULE) | |
5377 | { | |
524af0d6 | 5378 | if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s " |
70112e2a | 5379 | "at %L in a TYPE definition", attr, |
524af0d6 | 5380 | &seen_at[d])) |
d51347f9 TB |
5381 | { |
5382 | m = MATCH_ERROR; | |
5383 | goto cleanup; | |
5384 | } | |
5385 | } | |
5386 | else | |
5387 | { | |
5388 | gfc_error ("%s attribute at %L is not allowed outside of the " | |
5389 | "specification part of a module", attr, &seen_at[d]); | |
5390 | m = MATCH_ERROR; | |
5391 | goto cleanup; | |
5392 | } | |
4213f93b PT |
5393 | } |
5394 | ||
5bab4c96 PT |
5395 | if (gfc_current_state () != COMP_DERIVED |
5396 | && (d == DECL_KIND || d == DECL_LEN)) | |
5397 | { | |
5398 | gfc_error ("Attribute at %L is not allowed outside a TYPE " | |
5399 | "definition", &seen_at[d]); | |
5400 | m = MATCH_ERROR; | |
5401 | goto cleanup; | |
5402 | } | |
5403 | ||
6de9cd9a DN |
5404 | switch (d) |
5405 | { | |
5406 | case DECL_ALLOCATABLE: | |
5407 | t = gfc_add_allocatable (¤t_attr, &seen_at[d]); | |
5408 | break; | |
5409 | ||
1eee5628 | 5410 | case DECL_ASYNCHRONOUS: |
524af0d6 JB |
5411 | if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C")) |
5412 | t = false; | |
1eee5628 TB |
5413 | else |
5414 | t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]); | |
5415 | break; | |
5416 | ||
be59db2d TB |
5417 | case DECL_CODIMENSION: |
5418 | t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]); | |
5419 | break; | |
5420 | ||
fe4e525c | 5421 | case DECL_CONTIGUOUS: |
524af0d6 JB |
5422 | if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C")) |
5423 | t = false; | |
fe4e525c TB |
5424 | else |
5425 | t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]); | |
5426 | break; | |
5427 | ||
6de9cd9a | 5428 | case DECL_DIMENSION: |
231b2fcc | 5429 | t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]); |
6de9cd9a DN |
5430 | break; |
5431 | ||
5432 | case DECL_EXTERNAL: | |
5433 | t = gfc_add_external (¤t_attr, &seen_at[d]); | |
5434 | break; | |
5435 | ||
5436 | case DECL_IN: | |
5437 | t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]); | |
5438 | break; | |
5439 | ||
5440 | case DECL_OUT: | |
5441 | t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]); | |
5442 | break; | |
5443 | ||
5444 | case DECL_INOUT: | |
5445 | t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]); | |
5446 | break; | |
5447 | ||
5448 | case DECL_INTRINSIC: | |
5449 | t = gfc_add_intrinsic (¤t_attr, &seen_at[d]); | |
5450 | break; | |
5451 | ||
5452 | case DECL_OPTIONAL: | |
5453 | t = gfc_add_optional (¤t_attr, &seen_at[d]); | |
5454 | break; | |
5455 | ||
5bab4c96 PT |
5456 | case DECL_KIND: |
5457 | t = gfc_add_kind (¤t_attr, &seen_at[d]); | |
5458 | break; | |
5459 | ||
5460 | case DECL_LEN: | |
5461 | t = gfc_add_len (¤t_attr, &seen_at[d]); | |
5462 | break; | |
5463 | ||
6de9cd9a | 5464 | case DECL_PARAMETER: |
231b2fcc | 5465 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]); |
6de9cd9a DN |
5466 | break; |
5467 | ||
5468 | case DECL_POINTER: | |
5469 | t = gfc_add_pointer (¤t_attr, &seen_at[d]); | |
5470 | break; | |
5471 | ||
ee7e677f | 5472 | case DECL_PROTECTED: |
721be0f4 SK |
5473 | if (gfc_current_state () != COMP_MODULE |
5474 | || (gfc_current_ns->proc_name | |
5475 | && gfc_current_ns->proc_name->attr.flavor != FL_MODULE)) | |
ee7e677f TB |
5476 | { |
5477 | gfc_error ("PROTECTED at %C only allowed in specification " | |
5478 | "part of a module"); | |
524af0d6 | 5479 | t = false; |
ee7e677f TB |
5480 | break; |
5481 | } | |
5482 | ||
524af0d6 JB |
5483 | if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C")) |
5484 | t = false; | |
ee7e677f TB |
5485 | else |
5486 | t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]); | |
5487 | break; | |
5488 | ||
6de9cd9a | 5489 | case DECL_PRIVATE: |
231b2fcc TS |
5490 | t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL, |
5491 | &seen_at[d]); | |
6de9cd9a DN |
5492 | break; |
5493 | ||
5494 | case DECL_PUBLIC: | |
231b2fcc TS |
5495 | t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL, |
5496 | &seen_at[d]); | |
6de9cd9a DN |
5497 | break; |
5498 | ||
34d567d1 | 5499 | case DECL_STATIC: |
6de9cd9a | 5500 | case DECL_SAVE: |
80f95228 | 5501 | t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]); |
6de9cd9a DN |
5502 | break; |
5503 | ||
34d567d1 FR |
5504 | case DECL_AUTOMATIC: |
5505 | t = gfc_add_automatic (¤t_attr, NULL, &seen_at[d]); | |
5506 | break; | |
5507 | ||
6de9cd9a DN |
5508 | case DECL_TARGET: |
5509 | t = gfc_add_target (¤t_attr, &seen_at[d]); | |
5510 | break; | |
5511 | ||
a8b3b0b6 CR |
5512 | case DECL_IS_BIND_C: |
5513 | t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0); | |
5514 | break; | |
f5acf0f2 | 5515 | |
06469efd | 5516 | case DECL_VALUE: |
524af0d6 JB |
5517 | if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C")) |
5518 | t = false; | |
06469efd PT |
5519 | else |
5520 | t = gfc_add_value (¤t_attr, NULL, &seen_at[d]); | |
5521 | break; | |
5522 | ||
775e6c3a | 5523 | case DECL_VOLATILE: |
524af0d6 JB |
5524 | if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C")) |
5525 | t = false; | |
775e6c3a TB |
5526 | else |
5527 | t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]); | |
5528 | break; | |
5529 | ||
6de9cd9a DN |
5530 | default: |
5531 | gfc_internal_error ("match_attr_spec(): Bad attribute"); | |
5532 | } | |
5533 | ||
524af0d6 | 5534 | if (!t) |
6de9cd9a DN |
5535 | { |
5536 | m = MATCH_ERROR; | |
5537 | goto cleanup; | |
5538 | } | |
5539 | } | |
5540 | ||
dab2cbf8 | 5541 | /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */ |
4668d6f9 PT |
5542 | if ((gfc_current_state () == COMP_MODULE |
5543 | || gfc_current_state () == COMP_SUBMODULE) | |
5544 | && !current_attr.save | |
dab2cbf8 | 5545 | && (gfc_option.allow_std & GFC_STD_F2008) != 0) |
80f95228 JW |
5546 | current_attr.save = SAVE_IMPLICIT; |
5547 | ||
6de9cd9a DN |
5548 | colon_seen = 1; |
5549 | return MATCH_YES; | |
5550 | ||
5551 | cleanup: | |
63645982 | 5552 | gfc_current_locus = start; |
6de9cd9a DN |
5553 | gfc_free_array_spec (current_as); |
5554 | current_as = NULL; | |
6f855a26 | 5555 | attr_seen = 0; |
6de9cd9a DN |
5556 | return m; |
5557 | } | |
5558 | ||
5559 | ||
a8b3b0b6 CR |
5560 | /* Set the binding label, dest_label, either with the binding label |
5561 | stored in the given gfc_typespec, ts, or if none was provided, it | |
5562 | will be the symbol name in all lower case, as required by the draft | |
5563 | (J3/04-007, section 15.4.1). If a binding label was given and | |
5564 | there is more than one argument (num_idents), it is an error. */ | |
5565 | ||
524af0d6 | 5566 | static bool |
f5acf0f2 | 5567 | set_binding_label (const char **dest_label, const char *sym_name, |
9975a30b | 5568 | int num_idents) |
a8b3b0b6 | 5569 | { |
ad4a2f64 | 5570 | if (num_idents > 1 && has_name_equals) |
a8b3b0b6 | 5571 | { |
ad4a2f64 TB |
5572 | gfc_error ("Multiple identifiers provided with " |
5573 | "single NAME= specifier at %C"); | |
524af0d6 | 5574 | return false; |
ad4a2f64 | 5575 | } |
a8b3b0b6 | 5576 | |
62603fae | 5577 | if (curr_binding_label) |
eea58adb | 5578 | /* Binding label given; store in temp holder till have sym. */ |
62603fae | 5579 | *dest_label = curr_binding_label; |
a8b3b0b6 CR |
5580 | else |
5581 | { | |
5582 | /* No binding label given, and the NAME= specifier did not exist, | |
5583 | which means there was no NAME="". */ | |
5584 | if (sym_name != NULL && has_name_equals == 0) | |
62603fae | 5585 | *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name)); |
a8b3b0b6 | 5586 | } |
f5acf0f2 | 5587 | |
524af0d6 | 5588 | return true; |
a8b3b0b6 CR |
5589 | } |
5590 | ||
5591 | ||
5592 | /* Set the status of the given common block as being BIND(C) or not, | |
5593 | depending on the given parameter, is_bind_c. */ | |
5594 | ||
5595 | void | |
5596 | set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c) | |
5597 | { | |
5598 | com_block->is_bind_c = is_bind_c; | |
5599 | return; | |
5600 | } | |
5601 | ||
5602 | ||
5603 | /* Verify that the given gfc_typespec is for a C interoperable type. */ | |
5604 | ||
524af0d6 | 5605 | bool |
00820a2a | 5606 | gfc_verify_c_interop (gfc_typespec *ts) |
a8b3b0b6 | 5607 | { |
bc21d315 | 5608 | if (ts->type == BT_DERIVED && ts->u.derived != NULL) |
ba3721c1 | 5609 | return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c) |
524af0d6 | 5610 | ? true : false; |
00820a2a | 5611 | else if (ts->type == BT_CLASS) |
524af0d6 | 5612 | return false; |
45a69325 | 5613 | else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED) |
524af0d6 | 5614 | return false; |
45a69325 | 5615 | |
524af0d6 | 5616 | return true; |
a8b3b0b6 CR |
5617 | } |
5618 | ||
5619 | ||
5620 | /* Verify that the variables of a given common block, which has been | |
5621 | defined with the attribute specifier bind(c), to be of a C | |
5622 | interoperable type. Errors will be reported here, if | |
5623 | encountered. */ | |
5624 | ||
524af0d6 | 5625 | bool |
a8b3b0b6 CR |
5626 | verify_com_block_vars_c_interop (gfc_common_head *com_block) |
5627 | { | |
5628 | gfc_symbol *curr_sym = NULL; | |
524af0d6 | 5629 | bool retval = true; |
a8b3b0b6 CR |
5630 | |
5631 | curr_sym = com_block->head; | |
f5acf0f2 | 5632 | |
a8b3b0b6 CR |
5633 | /* Make sure we have at least one symbol. */ |
5634 | if (curr_sym == NULL) | |
5635 | return retval; | |
5636 | ||
5637 | /* Here we know we have a symbol, so we'll execute this loop | |
5638 | at least once. */ | |
5639 | do | |
5640 | { | |
5641 | /* The second to last param, 1, says this is in a common block. */ | |
5642 | retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block); | |
5643 | curr_sym = curr_sym->common_next; | |
f5acf0f2 | 5644 | } while (curr_sym != NULL); |
a8b3b0b6 CR |
5645 | |
5646 | return retval; | |
5647 | } | |
5648 | ||
5649 | ||
5650 | /* Verify that a given BIND(C) symbol is C interoperable. If it is not, | |
5651 | an appropriate error message is reported. */ | |
5652 | ||
524af0d6 | 5653 | bool |
a8b3b0b6 CR |
5654 | verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts, |
5655 | int is_in_common, gfc_common_head *com_block) | |
5656 | { | |
8327f9c2 | 5657 | bool bind_c_function = false; |
524af0d6 | 5658 | bool retval = true; |
d8fa96e0 | 5659 | |
8327f9c2 TB |
5660 | if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c) |
5661 | bind_c_function = true; | |
5662 | ||
d8fa96e0 CR |
5663 | if (tmp_sym->attr.function && tmp_sym->result != NULL) |
5664 | { | |
5665 | tmp_sym = tmp_sym->result; | |
5666 | /* Make sure it wasn't an implicitly typed result. */ | |
4daa149b | 5667 | if (tmp_sym->attr.implicit_type && warn_c_binding_type) |
d8fa96e0 | 5668 | { |
48749dbc MLI |
5669 | gfc_warning (OPT_Wc_binding_type, |
5670 | "Implicitly declared BIND(C) function %qs at " | |
d8fa96e0 CR |
5671 | "%L may not be C interoperable", tmp_sym->name, |
5672 | &tmp_sym->declared_at); | |
5673 | tmp_sym->ts.f90_type = tmp_sym->ts.type; | |
5674 | /* Mark it as C interoperable to prevent duplicate warnings. */ | |
5675 | tmp_sym->ts.is_c_interop = 1; | |
5676 | tmp_sym->attr.is_c_interop = 1; | |
5677 | } | |
5678 | } | |
8327f9c2 | 5679 | |
a8b3b0b6 CR |
5680 | /* Here, we know we have the bind(c) attribute, so if we have |
5681 | enough type info, then verify that it's a C interop kind. | |
5682 | The info could be in the symbol already, or possibly still in | |
5683 | the given ts (current_ts), so look in both. */ | |
f5acf0f2 | 5684 | if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN) |
a8b3b0b6 | 5685 | { |
524af0d6 | 5686 | if (!gfc_verify_c_interop (&(tmp_sym->ts))) |
a8b3b0b6 CR |
5687 | { |
5688 | /* See if we're dealing with a sym in a common block or not. */ | |
4daa149b | 5689 | if (is_in_common == 1 && warn_c_binding_type) |
a8b3b0b6 | 5690 | { |
48749dbc MLI |
5691 | gfc_warning (OPT_Wc_binding_type, |
5692 | "Variable %qs in common block %qs at %L " | |
a8b3b0b6 | 5693 | "may not be a C interoperable " |
48749dbc | 5694 | "kind though common block %qs is BIND(C)", |
a8b3b0b6 CR |
5695 | tmp_sym->name, com_block->name, |
5696 | &(tmp_sym->declared_at), com_block->name); | |
5697 | } | |
5698 | else | |
5699 | { | |
5700 | if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED) | |
c4100eae | 5701 | gfc_error ("Type declaration %qs at %L is not C " |
a8b3b0b6 CR |
5702 | "interoperable but it is BIND(C)", |
5703 | tmp_sym->name, &(tmp_sym->declared_at)); | |
4daa149b | 5704 | else if (warn_c_binding_type) |
48749dbc | 5705 | gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L " |
a8b3b0b6 | 5706 | "may not be a C interoperable " |
c4100eae | 5707 | "kind but it is BIND(C)", |
a8b3b0b6 CR |
5708 | tmp_sym->name, &(tmp_sym->declared_at)); |
5709 | } | |
5710 | } | |
f5acf0f2 | 5711 | |
a8b3b0b6 CR |
5712 | /* Variables declared w/in a common block can't be bind(c) |
5713 | since there's no way for C to see these variables, so there's | |
5714 | semantically no reason for the attribute. */ | |
5715 | if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1) | |
5716 | { | |
c4100eae | 5717 | gfc_error ("Variable %qs in common block %qs at " |
a8b3b0b6 CR |
5718 | "%L cannot be declared with BIND(C) " |
5719 | "since it is not a global", | |
5720 | tmp_sym->name, com_block->name, | |
5721 | &(tmp_sym->declared_at)); | |
524af0d6 | 5722 | retval = false; |
a8b3b0b6 | 5723 | } |
f5acf0f2 | 5724 | |
67914693 | 5725 | /* Scalar variables that are bind(c) cannot have the pointer |
a8b3b0b6 CR |
5726 | or allocatable attributes. */ |
5727 | if (tmp_sym->attr.is_bind_c == 1) | |
5728 | { | |
5729 | if (tmp_sym->attr.pointer == 1) | |
5730 | { | |
c4100eae | 5731 | gfc_error ("Variable %qs at %L cannot have both the " |
a8b3b0b6 CR |
5732 | "POINTER and BIND(C) attributes", |
5733 | tmp_sym->name, &(tmp_sym->declared_at)); | |
524af0d6 | 5734 | retval = false; |
a8b3b0b6 CR |
5735 | } |
5736 | ||
5737 | if (tmp_sym->attr.allocatable == 1) | |
5738 | { | |
c4100eae | 5739 | gfc_error ("Variable %qs at %L cannot have both the " |
a8b3b0b6 CR |
5740 | "ALLOCATABLE and BIND(C) attributes", |
5741 | tmp_sym->name, &(tmp_sym->declared_at)); | |
524af0d6 | 5742 | retval = false; |
a8b3b0b6 CR |
5743 | } |
5744 | ||
8327f9c2 TB |
5745 | } |
5746 | ||
5747 | /* If it is a BIND(C) function, make sure the return value is a | |
5748 | scalar value. The previous tests in this function made sure | |
5749 | the type is interoperable. */ | |
5750 | if (bind_c_function && tmp_sym->as != NULL) | |
c4100eae | 5751 | gfc_error ("Return type of BIND(C) function %qs at %L cannot " |
8327f9c2 TB |
5752 | "be an array", tmp_sym->name, &(tmp_sym->declared_at)); |
5753 | ||
67914693 | 5754 | /* BIND(C) functions cannot return a character string. */ |
8327f9c2 | 5755 | if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER) |
bc21d315 JW |
5756 | if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL |
5757 | || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT | |
5758 | || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0) | |
86ba9ce6 | 5759 | gfc_error ("Return type of BIND(C) function %qs of character " |
0f4f8561 | 5760 | "type at %L must have length 1", tmp_sym->name, |
a8b3b0b6 | 5761 | &(tmp_sym->declared_at)); |
a8b3b0b6 CR |
5762 | } |
5763 | ||
5764 | /* See if the symbol has been marked as private. If it has, make sure | |
5765 | there is no binding label and warn the user if there is one. */ | |
5766 | if (tmp_sym->attr.access == ACCESS_PRIVATE | |
62603fae | 5767 | && tmp_sym->binding_label) |
a8b3b0b6 CR |
5768 | /* Use gfc_warning_now because we won't say that the symbol fails |
5769 | just because of this. */ | |
db30e21c | 5770 | gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been " |
4daa149b | 5771 | "given the binding label %qs", tmp_sym->name, |
a8b3b0b6 CR |
5772 | &(tmp_sym->declared_at), tmp_sym->binding_label); |
5773 | ||
5774 | return retval; | |
5775 | } | |
5776 | ||
5777 | ||
5778 | /* Set the appropriate fields for a symbol that's been declared as | |
5779 | BIND(C) (the is_bind_c flag and the binding label), and verify that | |
5780 | the type is C interoperable. Errors are reported by the functions | |
5781 | used to set/test these fields. */ | |
5782 | ||
524af0d6 | 5783 | bool |
a8b3b0b6 CR |
5784 | set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents) |
5785 | { | |
524af0d6 | 5786 | bool retval = true; |
f5acf0f2 | 5787 | |
a8b3b0b6 CR |
5788 | /* TODO: Do we need to make sure the vars aren't marked private? */ |
5789 | ||
5790 | /* Set the is_bind_c bit in symbol_attribute. */ | |
5791 | gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0); | |
5792 | ||
524af0d6 JB |
5793 | if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents)) |
5794 | return false; | |
a8b3b0b6 CR |
5795 | |
5796 | return retval; | |
5797 | } | |
5798 | ||
5799 | ||
5800 | /* Set the fields marking the given common block as BIND(C), including | |
5801 | a binding label, and report any errors encountered. */ | |
5802 | ||
524af0d6 | 5803 | bool |
a8b3b0b6 CR |
5804 | set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents) |
5805 | { | |
524af0d6 | 5806 | bool retval = true; |
f5acf0f2 | 5807 | |
a8b3b0b6 | 5808 | /* destLabel, common name, typespec (which may have binding label). */ |
70112e2a | 5809 | if (!set_binding_label (&com_block->binding_label, com_block->name, |
524af0d6 JB |
5810 | num_idents)) |
5811 | return false; | |
a8b3b0b6 CR |
5812 | |
5813 | /* Set the given common block (com_block) to being bind(c) (1). */ | |
5814 | set_com_block_bind_c (com_block, 1); | |
5815 | ||
5816 | return retval; | |
5817 | } | |
5818 | ||
5819 | ||
5820 | /* Retrieve the list of one or more identifiers that the given bind(c) | |
5821 | attribute applies to. */ | |
5822 | ||
524af0d6 | 5823 | bool |
a8b3b0b6 CR |
5824 | get_bind_c_idents (void) |
5825 | { | |
5826 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
5827 | int num_idents = 0; | |
5828 | gfc_symbol *tmp_sym = NULL; | |
5829 | match found_id; | |
5830 | gfc_common_head *com_block = NULL; | |
f5acf0f2 | 5831 | |
a8b3b0b6 CR |
5832 | if (gfc_match_name (name) == MATCH_YES) |
5833 | { | |
5834 | found_id = MATCH_YES; | |
5835 | gfc_get_ha_symbol (name, &tmp_sym); | |
5836 | } | |
5837 | else if (match_common_name (name) == MATCH_YES) | |
5838 | { | |
5839 | found_id = MATCH_YES; | |
5840 | com_block = gfc_get_common (name, 0); | |
5841 | } | |
5842 | else | |
5843 | { | |
5844 | gfc_error ("Need either entity or common block name for " | |
5845 | "attribute specification statement at %C"); | |
524af0d6 | 5846 | return false; |
a8b3b0b6 | 5847 | } |
f5acf0f2 | 5848 | |
a8b3b0b6 CR |
5849 | /* Save the current identifier and look for more. */ |
5850 | do | |
5851 | { | |
5852 | /* Increment the number of identifiers found for this spec stmt. */ | |
5853 | num_idents++; | |
5854 | ||
5855 | /* Make sure we have a sym or com block, and verify that it can | |
5856 | be bind(c). Set the appropriate field(s) and look for more | |
5857 | identifiers. */ | |
f5acf0f2 | 5858 | if (tmp_sym != NULL || com_block != NULL) |
a8b3b0b6 CR |
5859 | { |
5860 | if (tmp_sym != NULL) | |
5861 | { | |
524af0d6 JB |
5862 | if (!set_verify_bind_c_sym (tmp_sym, num_idents)) |
5863 | return false; | |
a8b3b0b6 CR |
5864 | } |
5865 | else | |
5866 | { | |
524af0d6 JB |
5867 | if (!set_verify_bind_c_com_block (com_block, num_idents)) |
5868 | return false; | |
a8b3b0b6 | 5869 | } |
f5acf0f2 | 5870 | |
a8b3b0b6 CR |
5871 | /* Look to see if we have another identifier. */ |
5872 | tmp_sym = NULL; | |
5873 | if (gfc_match_eos () == MATCH_YES) | |
5874 | found_id = MATCH_NO; | |
5875 | else if (gfc_match_char (',') != MATCH_YES) | |
5876 | found_id = MATCH_NO; | |
5877 | else if (gfc_match_name (name) == MATCH_YES) | |
5878 | { | |
5879 | found_id = MATCH_YES; | |
5880 | gfc_get_ha_symbol (name, &tmp_sym); | |
5881 | } | |
5882 | else if (match_common_name (name) == MATCH_YES) | |
5883 | { | |
5884 | found_id = MATCH_YES; | |
5885 | com_block = gfc_get_common (name, 0); | |
5886 | } | |
5887 | else | |
5888 | { | |
5889 | gfc_error ("Missing entity or common block name for " | |
5890 | "attribute specification statement at %C"); | |
524af0d6 | 5891 | return false; |
a8b3b0b6 CR |
5892 | } |
5893 | } | |
5894 | else | |
5895 | { | |
5896 | gfc_internal_error ("Missing symbol"); | |
5897 | } | |
5898 | } while (found_id == MATCH_YES); | |
5899 | ||
5900 | /* if we get here we were successful */ | |
524af0d6 | 5901 | return true; |
a8b3b0b6 CR |
5902 | } |
5903 | ||
5904 | ||
5905 | /* Try and match a BIND(C) attribute specification statement. */ | |
f5acf0f2 | 5906 | |
a8b3b0b6 CR |
5907 | match |
5908 | gfc_match_bind_c_stmt (void) | |
5909 | { | |
5910 | match found_match = MATCH_NO; | |
5911 | gfc_typespec *ts; | |
5912 | ||
5913 | ts = ¤t_ts; | |
f5acf0f2 | 5914 | |
a8b3b0b6 CR |
5915 | /* This may not be necessary. */ |
5916 | gfc_clear_ts (ts); | |
5917 | /* Clear the temporary binding label holder. */ | |
62603fae | 5918 | curr_binding_label = NULL; |
a8b3b0b6 CR |
5919 | |
5920 | /* Look for the bind(c). */ | |
1eabf70a | 5921 | found_match = gfc_match_bind_c (NULL, true); |
a8b3b0b6 CR |
5922 | |
5923 | if (found_match == MATCH_YES) | |
5924 | { | |
878cdb7b TB |
5925 | if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C")) |
5926 | return MATCH_ERROR; | |
5927 | ||
a8b3b0b6 CR |
5928 | /* Look for the :: now, but it is not required. */ |
5929 | gfc_match (" :: "); | |
5930 | ||
5931 | /* Get the identifier(s) that needs to be updated. This may need to | |
5932 | change to hand the flag(s) for the attr specified so all identifiers | |
5933 | found can have all appropriate parts updated (assuming that the same | |
5934 | spec stmt can have multiple attrs, such as both bind(c) and | |
5935 | allocatable...). */ | |
524af0d6 | 5936 | if (!get_bind_c_idents ()) |
a8b3b0b6 CR |
5937 | /* Error message should have printed already. */ |
5938 | return MATCH_ERROR; | |
5939 | } | |
5940 | ||
5941 | return found_match; | |
5942 | } | |
5943 | ||
5944 | ||
6de9cd9a DN |
5945 | /* Match a data declaration statement. */ |
5946 | ||
5947 | match | |
5948 | gfc_match_data_decl (void) | |
5949 | { | |
5950 | gfc_symbol *sym; | |
5951 | match m; | |
949d5b72 | 5952 | int elem; |
6de9cd9a | 5953 | |
5bab4c96 PT |
5954 | type_param_spec_list = NULL; |
5955 | decl_type_param_list = NULL; | |
5956 | ||
a8b3b0b6 | 5957 | num_idents_on_line = 0; |
f5acf0f2 | 5958 | |
e74f1cc8 | 5959 | m = gfc_match_decl_type_spec (¤t_ts, 0); |
6de9cd9a DN |
5960 | if (m != MATCH_YES) |
5961 | return m; | |
5962 | ||
2e23972e | 5963 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
f6288c24 | 5964 | && !gfc_comp_struct (gfc_current_state ())) |
6de9cd9a | 5965 | { |
bc21d315 | 5966 | sym = gfc_use_derived (current_ts.u.derived); |
6de9cd9a DN |
5967 | |
5968 | if (sym == NULL) | |
5969 | { | |
5970 | m = MATCH_ERROR; | |
5971 | goto cleanup; | |
5972 | } | |
5973 | ||
bc21d315 | 5974 | current_ts.u.derived = sym; |
6de9cd9a DN |
5975 | } |
5976 | ||
5977 | m = match_attr_spec (); | |
5978 | if (m == MATCH_ERROR) | |
5979 | { | |
5980 | m = MATCH_NO; | |
5981 | goto cleanup; | |
5982 | } | |
5983 | ||
8b704316 PT |
5984 | if (current_ts.type == BT_CLASS |
5985 | && current_ts.u.derived->attr.unlimited_polymorphic) | |
5986 | goto ok; | |
5987 | ||
2e23972e JW |
5988 | if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS) |
5989 | && current_ts.u.derived->components == NULL | |
bc21d315 | 5990 | && !current_ts.u.derived->attr.zero_comp) |
6de9cd9a DN |
5991 | { |
5992 | ||
f6288c24 | 5993 | if (current_attr.pointer && gfc_comp_struct (gfc_current_state ())) |
6de9cd9a DN |
5994 | goto ok; |
5995 | ||
00cad178 | 5996 | if (current_attr.allocatable && gfc_current_state () == COMP_DERIVED) |
bf9f15ee PT |
5997 | goto ok; |
5998 | ||
bc21d315 | 5999 | gfc_find_symbol (current_ts.u.derived->name, |
dd8b9dde | 6000 | current_ts.u.derived->ns, 1, &sym); |
6de9cd9a | 6001 | |
976e21f6 | 6002 | /* Any symbol that we find had better be a type definition |
f6288c24 FR |
6003 | which has its components defined, or be a structure definition |
6004 | actively being parsed. */ | |
6005 | if (sym != NULL && gfc_fl_struct (sym->attr.flavor) | |
bc21d315 | 6006 | && (current_ts.u.derived->components != NULL |
f6288c24 FR |
6007 | || current_ts.u.derived->attr.zero_comp |
6008 | || current_ts.u.derived == gfc_new_block)) | |
6de9cd9a DN |
6009 | goto ok; |
6010 | ||
a1b80ec7 JW |
6011 | gfc_error ("Derived type at %C has not been previously defined " |
6012 | "and so cannot appear in a derived type definition"); | |
6013 | m = MATCH_ERROR; | |
6014 | goto cleanup; | |
6de9cd9a DN |
6015 | } |
6016 | ||
6017 | ok: | |
6018 | /* If we have an old-style character declaration, and no new-style | |
6019 | attribute specifications, then there a comma is optional between | |
6020 | the type specification and the variable list. */ | |
6021 | if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector) | |
6022 | gfc_match_char (','); | |
6023 | ||
949d5b72 PT |
6024 | /* Give the types/attributes to symbols that follow. Give the element |
6025 | a number so that repeat character length expressions can be copied. */ | |
6026 | elem = 1; | |
6de9cd9a DN |
6027 | for (;;) |
6028 | { | |
a8b3b0b6 | 6029 | num_idents_on_line++; |
949d5b72 | 6030 | m = variable_decl (elem++); |
6de9cd9a DN |
6031 | if (m == MATCH_ERROR) |
6032 | goto cleanup; | |
6033 | if (m == MATCH_NO) | |
6034 | break; | |
6035 | ||
6036 | if (gfc_match_eos () == MATCH_YES) | |
6037 | goto cleanup; | |
6038 | if (gfc_match_char (',') != MATCH_YES) | |
6039 | break; | |
6040 | } | |
6041 | ||
0f447a6e | 6042 | if (!gfc_error_flag_test ()) |
94903212 FR |
6043 | { |
6044 | /* An anonymous structure declaration is unambiguous; if we matched one | |
6045 | according to gfc_match_structure_decl, we need to return MATCH_YES | |
6046 | here to avoid confusing the remaining matchers, even if there was an | |
6047 | error during variable_decl. We must flush any such errors. Note this | |
6048 | causes the parser to gracefully continue parsing the remaining input | |
6049 | as a structure body, which likely follows. */ | |
6050 | if (current_ts.type == BT_DERIVED && current_ts.u.derived | |
6051 | && gfc_fl_struct (current_ts.u.derived->attr.flavor)) | |
6052 | { | |
6053 | gfc_error_now ("Syntax error in anonymous structure declaration" | |
6054 | " at %C"); | |
6055 | /* Skip the bad variable_decl and line up for the start of the | |
6056 | structure body. */ | |
6057 | gfc_error_recovery (); | |
6058 | m = MATCH_YES; | |
6059 | goto cleanup; | |
6060 | } | |
6061 | ||
6062 | gfc_error ("Syntax error in data declaration at %C"); | |
6063 | } | |
6064 | ||
6de9cd9a DN |
6065 | m = MATCH_ERROR; |
6066 | ||
a9f6f1f2 JD |
6067 | gfc_free_data_all (gfc_current_ns); |
6068 | ||
6de9cd9a | 6069 | cleanup: |
5bab4c96 PT |
6070 | if (saved_kind_expr) |
6071 | gfc_free_expr (saved_kind_expr); | |
6072 | if (type_param_spec_list) | |
6073 | gfc_free_actual_arglist (type_param_spec_list); | |
6074 | if (decl_type_param_list) | |
6075 | gfc_free_actual_arglist (decl_type_param_list); | |
6076 | saved_kind_expr = NULL; | |
6de9cd9a DN |
6077 | gfc_free_array_spec (current_as); |
6078 | current_as = NULL; | |
6079 | return m; | |
6080 | } | |
6081 | ||
2810dfab SK |
6082 | static bool |
6083 | in_module_or_interface(void) | |
6084 | { | |
6085 | if (gfc_current_state () == COMP_MODULE | |
0a524296 | 6086 | || gfc_current_state () == COMP_SUBMODULE |
2810dfab SK |
6087 | || gfc_current_state () == COMP_INTERFACE) |
6088 | return true; | |
6089 | ||
6090 | if (gfc_state_stack->state == COMP_CONTAINS | |
6091 | || gfc_state_stack->state == COMP_FUNCTION | |
6092 | || gfc_state_stack->state == COMP_SUBROUTINE) | |
6093 | { | |
6094 | gfc_state_data *p; | |
6095 | for (p = gfc_state_stack->previous; p ; p = p->previous) | |
6096 | { | |
0a524296 | 6097 | if (p->state == COMP_MODULE || p->state == COMP_SUBMODULE |
2810dfab SK |
6098 | || p->state == COMP_INTERFACE) |
6099 | return true; | |
6100 | } | |
6101 | } | |
6102 | return false; | |
6103 | } | |
6de9cd9a DN |
6104 | |
6105 | /* Match a prefix associated with a function or subroutine | |
6106 | declaration. If the typespec pointer is nonnull, then a typespec | |
6107 | can be matched. Note that if nothing matches, MATCH_YES is | |
6108 | returned (the null string was matched). */ | |
6109 | ||
1c8bcdf7 PT |
6110 | match |
6111 | gfc_match_prefix (gfc_typespec *ts) | |
6de9cd9a | 6112 | { |
7389bce6 | 6113 | bool seen_type; |
e6c14898 DK |
6114 | bool seen_impure; |
6115 | bool found_prefix; | |
6de9cd9a DN |
6116 | |
6117 | gfc_clear_attr (¤t_attr); | |
e6c14898 DK |
6118 | seen_type = false; |
6119 | seen_impure = false; | |
6de9cd9a | 6120 | |
3df684e2 DK |
6121 | gcc_assert (!gfc_matching_prefix); |
6122 | gfc_matching_prefix = true; | |
f37e928c | 6123 | |
e6c14898 | 6124 | do |
6de9cd9a | 6125 | { |
e6c14898 | 6126 | found_prefix = false; |
6de9cd9a | 6127 | |
70112e2a PT |
6128 | /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a |
6129 | corresponding attribute seems natural and distinguishes these | |
6130 | procedures from procedure types of PROC_MODULE, which these are | |
6131 | as well. */ | |
6132 | if (gfc_match ("module% ") == MATCH_YES) | |
6133 | { | |
6134 | if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C")) | |
6135 | goto error; | |
6136 | ||
2810dfab SK |
6137 | if (!in_module_or_interface ()) |
6138 | { | |
6139 | gfc_error ("MODULE prefix at %C found outside of a module, " | |
6140 | "submodule, or interface"); | |
6141 | goto error; | |
6142 | } | |
6143 | ||
70112e2a PT |
6144 | current_attr.module_procedure = 1; |
6145 | found_prefix = true; | |
6146 | } | |
6147 | ||
e6c14898 DK |
6148 | if (!seen_type && ts != NULL |
6149 | && gfc_match_decl_type_spec (ts, 0) == MATCH_YES | |
6150 | && gfc_match_space () == MATCH_YES) | |
6151 | { | |
6de9cd9a | 6152 | |
e6c14898 DK |
6153 | seen_type = true; |
6154 | found_prefix = true; | |
6155 | } | |
6156 | ||
6157 | if (gfc_match ("elemental% ") == MATCH_YES) | |
6158 | { | |
524af0d6 | 6159 | if (!gfc_add_elemental (¤t_attr, NULL)) |
e6c14898 DK |
6160 | goto error; |
6161 | ||
6162 | found_prefix = true; | |
6163 | } | |
6164 | ||
6165 | if (gfc_match ("pure% ") == MATCH_YES) | |
6166 | { | |
524af0d6 | 6167 | if (!gfc_add_pure (¤t_attr, NULL)) |
e6c14898 DK |
6168 | goto error; |
6169 | ||
6170 | found_prefix = true; | |
6171 | } | |
6de9cd9a | 6172 | |
e6c14898 DK |
6173 | if (gfc_match ("recursive% ") == MATCH_YES) |
6174 | { | |
524af0d6 | 6175 | if (!gfc_add_recursive (¤t_attr, NULL)) |
e6c14898 DK |
6176 | goto error; |
6177 | ||
6178 | found_prefix = true; | |
6179 | } | |
6180 | ||
6181 | /* IMPURE is a somewhat special case, as it needs not set an actual | |
6182 | attribute but rather only prevents ELEMENTAL routines from being | |
6183 | automatically PURE. */ | |
6184 | if (gfc_match ("impure% ") == MATCH_YES) | |
6185 | { | |
524af0d6 | 6186 | if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C")) |
e6c14898 DK |
6187 | goto error; |
6188 | ||
6189 | seen_impure = true; | |
6190 | found_prefix = true; | |
6191 | } | |
6de9cd9a | 6192 | } |
e6c14898 | 6193 | while (found_prefix); |
6de9cd9a | 6194 | |
e6c14898 DK |
6195 | /* IMPURE and PURE must not both appear, of course. */ |
6196 | if (seen_impure && current_attr.pure) | |
6de9cd9a | 6197 | { |
e6c14898 DK |
6198 | gfc_error ("PURE and IMPURE must not appear both at %C"); |
6199 | goto error; | |
6de9cd9a DN |
6200 | } |
6201 | ||
e6c14898 DK |
6202 | /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */ |
6203 | if (!seen_impure && current_attr.elemental && !current_attr.pure) | |
6de9cd9a | 6204 | { |
524af0d6 | 6205 | if (!gfc_add_pure (¤t_attr, NULL)) |
f37e928c | 6206 | goto error; |
6de9cd9a DN |
6207 | } |
6208 | ||
6209 | /* At this point, the next item is not a prefix. */ | |
3df684e2 | 6210 | gcc_assert (gfc_matching_prefix); |
4668d6f9 | 6211 | |
3df684e2 | 6212 | gfc_matching_prefix = false; |
6de9cd9a | 6213 | return MATCH_YES; |
f37e928c DK |
6214 | |
6215 | error: | |
3df684e2 DK |
6216 | gcc_assert (gfc_matching_prefix); |
6217 | gfc_matching_prefix = false; | |
f37e928c | 6218 | return MATCH_ERROR; |
6de9cd9a DN |
6219 | } |
6220 | ||
6221 | ||
1c8bcdf7 | 6222 | /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */ |
6de9cd9a | 6223 | |
524af0d6 | 6224 | static bool |
636dff67 | 6225 | copy_prefix (symbol_attribute *dest, locus *where) |
6de9cd9a | 6226 | { |
6442a6f4 PT |
6227 | if (dest->module_procedure) |
6228 | { | |
6229 | if (current_attr.elemental) | |
6230 | dest->elemental = 1; | |
6231 | ||
6232 | if (current_attr.pure) | |
6233 | dest->pure = 1; | |
6234 | ||
6235 | if (current_attr.recursive) | |
6236 | dest->recursive = 1; | |
6237 | ||
6238 | /* Module procedures are unusual in that the 'dest' is copied from | |
6239 | the interface declaration. However, this is an oportunity to | |
6240 | check that the submodule declaration is compliant with the | |
6241 | interface. */ | |
6242 | if (dest->elemental && !current_attr.elemental) | |
6243 | { | |
6244 | gfc_error ("ELEMENTAL prefix in MODULE PROCEDURE interface is " | |
6245 | "missing at %L", where); | |
6246 | return false; | |
6247 | } | |
6248 | ||
6249 | if (dest->pure && !current_attr.pure) | |
6250 | { | |
6251 | gfc_error ("PURE prefix in MODULE PROCEDURE interface is " | |
6252 | "missing at %L", where); | |
6253 | return false; | |
6254 | } | |
6255 | ||
6256 | if (dest->recursive && !current_attr.recursive) | |
6257 | { | |
6258 | gfc_error ("RECURSIVE prefix in MODULE PROCEDURE interface is " | |
6259 | "missing at %L", where); | |
6260 | return false; | |
6261 | } | |
6262 | ||
6263 | return true; | |
6264 | } | |
6de9cd9a | 6265 | |
524af0d6 JB |
6266 | if (current_attr.elemental && !gfc_add_elemental (dest, where)) |
6267 | return false; | |
6de9cd9a | 6268 | |
6442a6f4 PT |
6269 | if (current_attr.pure && !gfc_add_pure (dest, where)) |
6270 | return false; | |
6271 | ||
524af0d6 JB |
6272 | if (current_attr.recursive && !gfc_add_recursive (dest, where)) |
6273 | return false; | |
6de9cd9a | 6274 | |
524af0d6 | 6275 | return true; |
6de9cd9a DN |
6276 | } |
6277 | ||
6278 | ||
5bab4c96 PT |
6279 | /* Match a formal argument list or, if typeparam is true, a |
6280 | type_param_name_list. */ | |
6de9cd9a DN |
6281 | |
6282 | match | |
5bab4c96 PT |
6283 | gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, |
6284 | int null_flag, bool typeparam) | |
6de9cd9a DN |
6285 | { |
6286 | gfc_formal_arglist *head, *tail, *p, *q; | |
6287 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6288 | gfc_symbol *sym; | |
6289 | match m; | |
4668d6f9 | 6290 | gfc_formal_arglist *formal = NULL; |
6de9cd9a DN |
6291 | |
6292 | head = tail = NULL; | |
6293 | ||
4668d6f9 PT |
6294 | /* Keep the interface formal argument list and null it so that the |
6295 | matching for the new declaration can be done. The numbers and | |
6296 | names of the arguments are checked here. The interface formal | |
6297 | arguments are retained in formal_arglist and the characteristics | |
6298 | are compared in resolve.c(resolve_fl_procedure). See the remark | |
6299 | in get_proc_name about the eventual need to copy the formal_arglist | |
6300 | and populate the formal namespace of the interface symbol. */ | |
6301 | if (progname->attr.module_procedure | |
6302 | && progname->attr.host_assoc) | |
6303 | { | |
6304 | formal = progname->formal; | |
6305 | progname->formal = NULL; | |
6306 | } | |
6307 | ||
6de9cd9a DN |
6308 | if (gfc_match_char ('(') != MATCH_YES) |
6309 | { | |
6310 | if (null_flag) | |
6311 | goto ok; | |
6312 | return MATCH_NO; | |
6313 | } | |
6314 | ||
6315 | if (gfc_match_char (')') == MATCH_YES) | |
0a524296 | 6316 | { |
84083a71 JW |
6317 | if (typeparam) |
6318 | { | |
6319 | gfc_error_now ("A type parameter list is required at %C"); | |
6320 | m = MATCH_ERROR; | |
6321 | goto cleanup; | |
6322 | } | |
6323 | else | |
6324 | goto ok; | |
6325 | } | |
6de9cd9a DN |
6326 | |
6327 | for (;;) | |
6328 | { | |
6329 | if (gfc_match_char ('*') == MATCH_YES) | |
9362a03b JW |
6330 | { |
6331 | sym = NULL; | |
276515e6 PT |
6332 | if (!typeparam && !gfc_notify_std (GFC_STD_F95_OBS, |
6333 | "Alternate-return argument at %C")) | |
9362a03b JW |
6334 | { |
6335 | m = MATCH_ERROR; | |
6336 | goto cleanup; | |
6337 | } | |
276515e6 PT |
6338 | else if (typeparam) |
6339 | gfc_error_now ("A parameter name is required at %C"); | |
9362a03b | 6340 | } |
6de9cd9a DN |
6341 | else |
6342 | { | |
6343 | m = gfc_match_name (name); | |
6344 | if (m != MATCH_YES) | |
276515e6 PT |
6345 | { |
6346 | if(typeparam) | |
6347 | gfc_error_now ("A parameter name is required at %C"); | |
6348 | goto cleanup; | |
6349 | } | |
6de9cd9a | 6350 | |
5bab4c96 PT |
6351 | if (!typeparam && gfc_get_symbol (name, NULL, &sym)) |
6352 | goto cleanup; | |
6353 | else if (typeparam | |
6354 | && gfc_get_symbol (name, progname->f2k_derived, &sym)) | |
6de9cd9a DN |
6355 | goto cleanup; |
6356 | } | |
6357 | ||
6358 | p = gfc_get_formal_arglist (); | |
6359 | ||
6360 | if (head == NULL) | |
6361 | head = tail = p; | |
6362 | else | |
6363 | { | |
6364 | tail->next = p; | |
6365 | tail = p; | |
6366 | } | |
6367 | ||
6368 | tail->sym = sym; | |
6369 | ||
6370 | /* We don't add the VARIABLE flavor because the name could be a | |
636dff67 SK |
6371 | dummy procedure. We don't apply these attributes to formal |
6372 | arguments of statement functions. */ | |
6de9cd9a | 6373 | if (sym != NULL && !st_flag |
524af0d6 JB |
6374 | && (!gfc_add_dummy(&sym->attr, sym->name, NULL) |
6375 | || !gfc_missing_attr (&sym->attr, NULL))) | |
6de9cd9a DN |
6376 | { |
6377 | m = MATCH_ERROR; | |
6378 | goto cleanup; | |
6379 | } | |
6380 | ||
6381 | /* The name of a program unit can be in a different namespace, | |
636dff67 SK |
6382 | so check for it explicitly. After the statement is accepted, |
6383 | the name is checked for especially in gfc_get_symbol(). */ | |
de624bee | 6384 | if (gfc_new_block != NULL && sym != NULL && !typeparam |
6de9cd9a DN |
6385 | && strcmp (sym->name, gfc_new_block->name) == 0) |
6386 | { | |
c4100eae | 6387 | gfc_error ("Name %qs at %C is the name of the procedure", |
6de9cd9a DN |
6388 | sym->name); |
6389 | m = MATCH_ERROR; | |
6390 | goto cleanup; | |
6391 | } | |
6392 | ||
6393 | if (gfc_match_char (')') == MATCH_YES) | |
6394 | goto ok; | |
6395 | ||
6396 | m = gfc_match_char (','); | |
6397 | if (m != MATCH_YES) | |
6398 | { | |
de624bee PT |
6399 | if (typeparam) |
6400 | gfc_error_now ("Expected parameter list in type declaration " | |
6401 | "at %C"); | |
6402 | else | |
6403 | gfc_error ("Unexpected junk in formal argument list at %C"); | |
6de9cd9a DN |
6404 | goto cleanup; |
6405 | } | |
6406 | } | |
6407 | ||
6408 | ok: | |
6409 | /* Check for duplicate symbols in the formal argument list. */ | |
6410 | if (head != NULL) | |
6411 | { | |
6412 | for (p = head; p->next; p = p->next) | |
6413 | { | |
6414 | if (p->sym == NULL) | |
6415 | continue; | |
6416 | ||
6417 | for (q = p->next; q; q = q->next) | |
6418 | if (p->sym == q->sym) | |
6419 | { | |
de624bee PT |
6420 | if (typeparam) |
6421 | gfc_error_now ("Duplicate name %qs in parameter " | |
6422 | "list at %C", p->sym->name); | |
6423 | else | |
6424 | gfc_error ("Duplicate symbol %qs in formal argument " | |
6425 | "list at %C", p->sym->name); | |
6de9cd9a DN |
6426 | |
6427 | m = MATCH_ERROR; | |
6428 | goto cleanup; | |
6429 | } | |
6430 | } | |
6431 | } | |
6432 | ||
524af0d6 | 6433 | if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)) |
6de9cd9a DN |
6434 | { |
6435 | m = MATCH_ERROR; | |
6436 | goto cleanup; | |
6437 | } | |
6438 | ||
e9d9b48d PT |
6439 | /* gfc_error_now used in following and return with MATCH_YES because |
6440 | doing otherwise results in a cascade of extraneous errors and in | |
6441 | some cases an ICE in symbol.c(gfc_release_symbol). */ | |
0ef5fbc1 | 6442 | if (progname->attr.module_procedure && progname->attr.host_assoc) |
4668d6f9 | 6443 | { |
0ef5fbc1 PT |
6444 | bool arg_count_mismatch = false; |
6445 | ||
6446 | if (!formal && head) | |
6447 | arg_count_mismatch = true; | |
6448 | ||
6449 | /* Abbreviated module procedure declaration is not meant to have any | |
6450 | formal arguments! */ | |
e9d9b48d | 6451 | if (!progname->abr_modproc_decl && formal && !head) |
0ef5fbc1 PT |
6452 | arg_count_mismatch = true; |
6453 | ||
4668d6f9 PT |
6454 | for (p = formal, q = head; p && q; p = p->next, q = q->next) |
6455 | { | |
6456 | if ((p->next != NULL && q->next == NULL) | |
6457 | || (p->next == NULL && q->next != NULL)) | |
0ef5fbc1 | 6458 | arg_count_mismatch = true; |
4668d6f9 PT |
6459 | else if ((p->sym == NULL && q->sym == NULL) |
6460 | || strcmp (p->sym->name, q->sym->name) == 0) | |
6461 | continue; | |
6462 | else | |
6463 | gfc_error_now ("Mismatch in MODULE PROCEDURE formal " | |
6464 | "argument names (%s/%s) at %C", | |
6465 | p->sym->name, q->sym->name); | |
6466 | } | |
0ef5fbc1 PT |
6467 | |
6468 | if (arg_count_mismatch) | |
6469 | gfc_error_now ("Mismatch in number of MODULE PROCEDURE " | |
6470 | "formal arguments at %C"); | |
4668d6f9 PT |
6471 | } |
6472 | ||
6de9cd9a DN |
6473 | return MATCH_YES; |
6474 | ||
6475 | cleanup: | |
6476 | gfc_free_formal_arglist (head); | |
6477 | return m; | |
6478 | } | |
6479 | ||
6480 | ||
6481 | /* Match a RESULT specification following a function declaration or | |
6482 | ENTRY statement. Also matches the end-of-statement. */ | |
6483 | ||
6484 | static match | |
66e4ab31 | 6485 | match_result (gfc_symbol *function, gfc_symbol **result) |
6de9cd9a DN |
6486 | { |
6487 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6488 | gfc_symbol *r; | |
6489 | match m; | |
6490 | ||
6491 | if (gfc_match (" result (") != MATCH_YES) | |
6492 | return MATCH_NO; | |
6493 | ||
6494 | m = gfc_match_name (name); | |
6495 | if (m != MATCH_YES) | |
6496 | return m; | |
6497 | ||
a8b3b0b6 CR |
6498 | /* Get the right paren, and that's it because there could be the |
6499 | bind(c) attribute after the result clause. */ | |
524af0d6 | 6500 | if (gfc_match_char (')') != MATCH_YES) |
6de9cd9a | 6501 | { |
a8b3b0b6 | 6502 | /* TODO: should report the missing right paren here. */ |
6de9cd9a DN |
6503 | return MATCH_ERROR; |
6504 | } | |
6505 | ||
6506 | if (strcmp (function->name, name) == 0) | |
6507 | { | |
636dff67 | 6508 | gfc_error ("RESULT variable at %C must be different than function name"); |
6de9cd9a DN |
6509 | return MATCH_ERROR; |
6510 | } | |
6511 | ||
6512 | if (gfc_get_symbol (name, NULL, &r)) | |
6513 | return MATCH_ERROR; | |
6514 | ||
524af0d6 | 6515 | if (!gfc_add_result (&r->attr, r->name, NULL)) |
6de9cd9a DN |
6516 | return MATCH_ERROR; |
6517 | ||
6518 | *result = r; | |
6519 | ||
6520 | return MATCH_YES; | |
6521 | } | |
6522 | ||
6523 | ||
a8b3b0b6 CR |
6524 | /* Match a function suffix, which could be a combination of a result |
6525 | clause and BIND(C), either one, or neither. The draft does not | |
6526 | require them to come in a specific order. */ | |
6527 | ||
6528 | match | |
6529 | gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result) | |
6530 | { | |
6531 | match is_bind_c; /* Found bind(c). */ | |
6532 | match is_result; /* Found result clause. */ | |
6533 | match found_match; /* Status of whether we've found a good match. */ | |
8fc541d3 | 6534 | char peek_char; /* Character we're going to peek at. */ |
1eabf70a | 6535 | bool allow_binding_name; |
a8b3b0b6 CR |
6536 | |
6537 | /* Initialize to having found nothing. */ | |
6538 | found_match = MATCH_NO; | |
f5acf0f2 | 6539 | is_bind_c = MATCH_NO; |
a8b3b0b6 CR |
6540 | is_result = MATCH_NO; |
6541 | ||
6542 | /* Get the next char to narrow between result and bind(c). */ | |
6543 | gfc_gobble_whitespace (); | |
8fc541d3 | 6544 | peek_char = gfc_peek_ascii_char (); |
a8b3b0b6 | 6545 | |
1eabf70a TB |
6546 | /* C binding names are not allowed for internal procedures. */ |
6547 | if (gfc_current_state () == COMP_CONTAINS | |
6548 | && sym->ns->proc_name->attr.flavor != FL_MODULE) | |
6549 | allow_binding_name = false; | |
6550 | else | |
6551 | allow_binding_name = true; | |
6552 | ||
a8b3b0b6 CR |
6553 | switch (peek_char) |
6554 | { | |
6555 | case 'r': | |
6556 | /* Look for result clause. */ | |
6557 | is_result = match_result (sym, result); | |
6558 | if (is_result == MATCH_YES) | |
6559 | { | |
6560 | /* Now see if there is a bind(c) after it. */ | |
1eabf70a | 6561 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
6562 | /* We've found the result clause and possibly bind(c). */ |
6563 | found_match = MATCH_YES; | |
6564 | } | |
6565 | else | |
6566 | /* This should only be MATCH_ERROR. */ | |
f5acf0f2 | 6567 | found_match = is_result; |
a8b3b0b6 CR |
6568 | break; |
6569 | case 'b': | |
6570 | /* Look for bind(c) first. */ | |
1eabf70a | 6571 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
6572 | if (is_bind_c == MATCH_YES) |
6573 | { | |
6574 | /* Now see if a result clause followed it. */ | |
6575 | is_result = match_result (sym, result); | |
6576 | found_match = MATCH_YES; | |
6577 | } | |
6578 | else | |
6579 | { | |
6580 | /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */ | |
6581 | found_match = MATCH_ERROR; | |
6582 | } | |
6583 | break; | |
6584 | default: | |
6585 | gfc_error ("Unexpected junk after function declaration at %C"); | |
6586 | found_match = MATCH_ERROR; | |
6587 | break; | |
6588 | } | |
6589 | ||
a8b3b0b6 | 6590 | if (is_bind_c == MATCH_YES) |
01f4fff1 | 6591 | { |
1eabf70a | 6592 | /* Fortran 2008 draft allows BIND(C) for internal procedures. */ |
01f4fff1 | 6593 | if (gfc_current_state () == COMP_CONTAINS |
1eabf70a | 6594 | && sym->ns->proc_name->attr.flavor != FL_MODULE |
524af0d6 JB |
6595 | && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute " |
6596 | "at %L may not be specified for an internal " | |
6597 | "procedure", &gfc_current_locus)) | |
1eabf70a TB |
6598 | return MATCH_ERROR; |
6599 | ||
524af0d6 | 6600 | if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)) |
01f4fff1 TB |
6601 | return MATCH_ERROR; |
6602 | } | |
f5acf0f2 | 6603 | |
a8b3b0b6 CR |
6604 | return found_match; |
6605 | } | |
6606 | ||
6607 | ||
3070bab4 JW |
6608 | /* Procedure pointer return value without RESULT statement: |
6609 | Add "hidden" result variable named "ppr@". */ | |
6610 | ||
524af0d6 | 6611 | static bool |
3070bab4 JW |
6612 | add_hidden_procptr_result (gfc_symbol *sym) |
6613 | { | |
6614 | bool case1,case2; | |
6615 | ||
6616 | if (gfc_notification_std (GFC_STD_F2003) == ERROR) | |
524af0d6 | 6617 | return false; |
3070bab4 JW |
6618 | |
6619 | /* First usage case: PROCEDURE and EXTERNAL statements. */ | |
6620 | case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block () | |
6621 | && strcmp (gfc_current_block ()->name, sym->name) == 0 | |
6622 | && sym->attr.external; | |
6623 | /* Second usage case: INTERFACE statements. */ | |
6624 | case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous | |
6625 | && gfc_state_stack->previous->state == COMP_FUNCTION | |
6626 | && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0; | |
6627 | ||
6628 | if (case1 || case2) | |
6629 | { | |
6630 | gfc_symtree *stree; | |
6631 | if (case1) | |
08a6b8e0 | 6632 | gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false); |
5433e401 | 6633 | else |
c73b6478 JW |
6634 | { |
6635 | gfc_symtree *st2; | |
08a6b8e0 | 6636 | gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false); |
c73b6478 JW |
6637 | st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@"); |
6638 | st2->n.sym = stree->n.sym; | |
8c0f9dab | 6639 | stree->n.sym->refs++; |
c73b6478 | 6640 | } |
3070bab4 JW |
6641 | sym->result = stree->n.sym; |
6642 | ||
6643 | sym->result->attr.proc_pointer = sym->attr.proc_pointer; | |
6644 | sym->result->attr.pointer = sym->attr.pointer; | |
6645 | sym->result->attr.external = sym->attr.external; | |
6646 | sym->result->attr.referenced = sym->attr.referenced; | |
fc9c6e5d | 6647 | sym->result->ts = sym->ts; |
3070bab4 JW |
6648 | sym->attr.proc_pointer = 0; |
6649 | sym->attr.pointer = 0; | |
6650 | sym->attr.external = 0; | |
6651 | if (sym->result->attr.external && sym->result->attr.pointer) | |
6652 | { | |
6653 | sym->result->attr.pointer = 0; | |
6654 | sym->result->attr.proc_pointer = 1; | |
6655 | } | |
6656 | ||
6657 | return gfc_add_result (&sym->result->attr, sym->result->name, NULL); | |
6658 | } | |
6659 | /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */ | |
6660 | else if (sym->attr.function && !sym->attr.external && sym->attr.pointer | |
6661 | && sym->result && sym->result != sym && sym->result->attr.external | |
6662 | && sym == gfc_current_ns->proc_name | |
6663 | && sym == sym->result->ns->proc_name | |
6664 | && strcmp ("ppr@", sym->result->name) == 0) | |
6665 | { | |
6666 | sym->result->attr.proc_pointer = 1; | |
6667 | sym->attr.pointer = 0; | |
524af0d6 | 6668 | return true; |
3070bab4 JW |
6669 | } |
6670 | else | |
524af0d6 | 6671 | return false; |
3070bab4 JW |
6672 | } |
6673 | ||
6674 | ||
713485cc JW |
6675 | /* Match the interface for a PROCEDURE declaration, |
6676 | including brackets (R1212). */ | |
69773742 JW |
6677 | |
6678 | static match | |
713485cc | 6679 | match_procedure_interface (gfc_symbol **proc_if) |
69773742 JW |
6680 | { |
6681 | match m; | |
3276e0b3 | 6682 | gfc_symtree *st; |
69773742 | 6683 | locus old_loc, entry_loc; |
3276e0b3 PT |
6684 | gfc_namespace *old_ns = gfc_current_ns; |
6685 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
69773742 | 6686 | |
3276e0b3 | 6687 | old_loc = entry_loc = gfc_current_locus; |
69773742 JW |
6688 | gfc_clear_ts (¤t_ts); |
6689 | ||
6690 | if (gfc_match (" (") != MATCH_YES) | |
6691 | { | |
6692 | gfc_current_locus = entry_loc; | |
6693 | return MATCH_NO; | |
6694 | } | |
6695 | ||
6696 | /* Get the type spec. for the procedure interface. */ | |
6697 | old_loc = gfc_current_locus; | |
e74f1cc8 | 6698 | m = gfc_match_decl_type_spec (¤t_ts, 0); |
f4256439 | 6699 | gfc_gobble_whitespace (); |
8fc541d3 | 6700 | if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')')) |
69773742 JW |
6701 | goto got_ts; |
6702 | ||
6703 | if (m == MATCH_ERROR) | |
6704 | return m; | |
6705 | ||
3276e0b3 | 6706 | /* Procedure interface is itself a procedure. */ |
69773742 | 6707 | gfc_current_locus = old_loc; |
3276e0b3 | 6708 | m = gfc_match_name (name); |
69773742 | 6709 | |
3276e0b3 PT |
6710 | /* First look to see if it is already accessible in the current |
6711 | namespace because it is use associated or contained. */ | |
6712 | st = NULL; | |
6713 | if (gfc_find_sym_tree (name, NULL, 0, &st)) | |
6714 | return MATCH_ERROR; | |
6715 | ||
6716 | /* If it is still not found, then try the parent namespace, if it | |
6717 | exists and create the symbol there if it is still not found. */ | |
6718 | if (gfc_current_ns->parent) | |
6719 | gfc_current_ns = gfc_current_ns->parent; | |
6720 | if (st == NULL && gfc_get_ha_sym_tree (name, &st)) | |
6721 | return MATCH_ERROR; | |
6722 | ||
6723 | gfc_current_ns = old_ns; | |
6724 | *proc_if = st->n.sym; | |
69773742 | 6725 | |
713485cc | 6726 | if (*proc_if) |
69773742 | 6727 | { |
713485cc | 6728 | (*proc_if)->refs++; |
bb343a6c TB |
6729 | /* Resolve interface if possible. That way, attr.procedure is only set |
6730 | if it is declared by a later procedure-declaration-stmt, which is | |
0e8d854e | 6731 | invalid per F08:C1216 (cf. resolve_procedure_interface). */ |
d73e0ccf JD |
6732 | while ((*proc_if)->ts.interface |
6733 | && *proc_if != (*proc_if)->ts.interface) | |
713485cc | 6734 | *proc_if = (*proc_if)->ts.interface; |
bb343a6c | 6735 | |
0e8d854e JW |
6736 | if ((*proc_if)->attr.flavor == FL_UNKNOWN |
6737 | && (*proc_if)->ts.type == BT_UNKNOWN | |
70112e2a | 6738 | && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE, |
524af0d6 | 6739 | (*proc_if)->name, NULL)) |
0e8d854e | 6740 | return MATCH_ERROR; |
69773742 JW |
6741 | } |
6742 | ||
6743 | got_ts: | |
69773742 JW |
6744 | if (gfc_match (" )") != MATCH_YES) |
6745 | { | |
6746 | gfc_current_locus = entry_loc; | |
6747 | return MATCH_NO; | |
6748 | } | |
6749 | ||
713485cc JW |
6750 | return MATCH_YES; |
6751 | } | |
6752 | ||
6753 | ||
6754 | /* Match a PROCEDURE declaration (R1211). */ | |
6755 | ||
6756 | static match | |
6757 | match_procedure_decl (void) | |
6758 | { | |
6759 | match m; | |
6760 | gfc_symbol *sym, *proc_if = NULL; | |
6761 | int num; | |
6762 | gfc_expr *initializer = NULL; | |
6763 | ||
1cc0e193 | 6764 | /* Parse interface (with brackets). */ |
713485cc JW |
6765 | m = match_procedure_interface (&proc_if); |
6766 | if (m != MATCH_YES) | |
6767 | return m; | |
6768 | ||
6769 | /* Parse attributes (with colons). */ | |
69773742 JW |
6770 | m = match_attr_spec(); |
6771 | if (m == MATCH_ERROR) | |
6772 | return MATCH_ERROR; | |
6773 | ||
0859be17 TB |
6774 | if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c) |
6775 | { | |
6776 | current_attr.is_bind_c = 1; | |
6777 | has_name_equals = 0; | |
6778 | curr_binding_label = NULL; | |
6779 | } | |
6780 | ||
69773742 JW |
6781 | /* Get procedure symbols. */ |
6782 | for(num=1;;num++) | |
6783 | { | |
69773742 JW |
6784 | m = gfc_match_symbol (&sym, 0); |
6785 | if (m == MATCH_NO) | |
6786 | goto syntax; | |
6787 | else if (m == MATCH_ERROR) | |
6788 | return m; | |
6789 | ||
6790 | /* Add current_attr to the symbol attributes. */ | |
524af0d6 | 6791 | if (!gfc_copy_attr (&sym->attr, ¤t_attr, NULL)) |
69773742 JW |
6792 | return MATCH_ERROR; |
6793 | ||
6794 | if (sym->attr.is_bind_c) | |
6795 | { | |
6796 | /* Check for C1218. */ | |
6797 | if (!proc_if || !proc_if->attr.is_bind_c) | |
6798 | { | |
6799 | gfc_error ("BIND(C) attribute at %C requires " | |
6800 | "an interface with BIND(C)"); | |
6801 | return MATCH_ERROR; | |
6802 | } | |
6803 | /* Check for C1217. */ | |
6804 | if (has_name_equals && sym->attr.pointer) | |
6805 | { | |
6806 | gfc_error ("BIND(C) procedure with NAME may not have " | |
6807 | "POINTER attribute at %C"); | |
6808 | return MATCH_ERROR; | |
6809 | } | |
6810 | if (has_name_equals && sym->attr.dummy) | |
6811 | { | |
6812 | gfc_error ("Dummy procedure at %C may not have " | |
6813 | "BIND(C) attribute with NAME"); | |
6814 | return MATCH_ERROR; | |
6815 | } | |
6816 | /* Set binding label for BIND(C). */ | |
524af0d6 | 6817 | if (!set_binding_label (&sym->binding_label, sym->name, num)) |
69773742 JW |
6818 | return MATCH_ERROR; |
6819 | } | |
6820 | ||
524af0d6 | 6821 | if (!gfc_add_external (&sym->attr, NULL)) |
69773742 | 6822 | return MATCH_ERROR; |
3070bab4 | 6823 | |
524af0d6 | 6824 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
6825 | sym = sym->result; |
6826 | ||
524af0d6 | 6827 | if (!gfc_add_proc (&sym->attr, sym->name, NULL)) |
69773742 JW |
6828 | return MATCH_ERROR; |
6829 | ||
6830 | /* Set interface. */ | |
6831 | if (proc_if != NULL) | |
6cc309c9 | 6832 | { |
1d146030 JW |
6833 | if (sym->ts.type != BT_UNKNOWN) |
6834 | { | |
c4100eae | 6835 | gfc_error ("Procedure %qs at %L already has basic type of %s", |
1d146030 JW |
6836 | sym->name, &gfc_current_locus, |
6837 | gfc_basic_typename (sym->ts.type)); | |
6838 | return MATCH_ERROR; | |
6839 | } | |
32d99e68 | 6840 | sym->ts.interface = proc_if; |
6cc309c9 | 6841 | sym->attr.untyped = 1; |
c73b6478 | 6842 | sym->attr.if_source = IFSRC_IFBODY; |
6cc309c9 | 6843 | } |
69773742 JW |
6844 | else if (current_ts.type != BT_UNKNOWN) |
6845 | { | |
524af0d6 | 6846 | if (!gfc_add_type (sym, ¤t_ts, &gfc_current_locus)) |
1d146030 | 6847 | return MATCH_ERROR; |
32d99e68 JW |
6848 | sym->ts.interface = gfc_new_symbol ("", gfc_current_ns); |
6849 | sym->ts.interface->ts = current_ts; | |
d91909c0 | 6850 | sym->ts.interface->attr.flavor = FL_PROCEDURE; |
32d99e68 | 6851 | sym->ts.interface->attr.function = 1; |
d91909c0 | 6852 | sym->attr.function = 1; |
c73b6478 | 6853 | sym->attr.if_source = IFSRC_UNKNOWN; |
69773742 JW |
6854 | } |
6855 | ||
8fb74da4 JW |
6856 | if (gfc_match (" =>") == MATCH_YES) |
6857 | { | |
6858 | if (!current_attr.pointer) | |
6859 | { | |
6860 | gfc_error ("Initialization at %C isn't for a pointer variable"); | |
6861 | m = MATCH_ERROR; | |
6862 | goto cleanup; | |
6863 | } | |
6864 | ||
80f95228 | 6865 | m = match_pointer_init (&initializer, 1); |
8fb74da4 JW |
6866 | if (m != MATCH_YES) |
6867 | goto cleanup; | |
6868 | ||
524af0d6 | 6869 | if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)) |
8fb74da4 JW |
6870 | goto cleanup; |
6871 | ||
6872 | } | |
6873 | ||
69773742 JW |
6874 | if (gfc_match_eos () == MATCH_YES) |
6875 | return MATCH_YES; | |
6876 | if (gfc_match_char (',') != MATCH_YES) | |
6877 | goto syntax; | |
6878 | } | |
6879 | ||
6880 | syntax: | |
6881 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
6882 | return MATCH_ERROR; | |
8fb74da4 JW |
6883 | |
6884 | cleanup: | |
6885 | /* Free stuff up and return. */ | |
6886 | gfc_free_expr (initializer); | |
6887 | return m; | |
69773742 JW |
6888 | } |
6889 | ||
6890 | ||
713485cc JW |
6891 | static match |
6892 | match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc); | |
6893 | ||
6894 | ||
6895 | /* Match a procedure pointer component declaration (R445). */ | |
6896 | ||
6897 | static match | |
6898 | match_ppc_decl (void) | |
6899 | { | |
6900 | match m; | |
6901 | gfc_symbol *proc_if = NULL; | |
6902 | gfc_typespec ts; | |
6903 | int num; | |
6904 | gfc_component *c; | |
6905 | gfc_expr *initializer = NULL; | |
6906 | gfc_typebound_proc* tb; | |
6907 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
6908 | ||
6909 | /* Parse interface (with brackets). */ | |
6910 | m = match_procedure_interface (&proc_if); | |
6911 | if (m != MATCH_YES) | |
6912 | goto syntax; | |
6913 | ||
6914 | /* Parse attributes. */ | |
6915 | tb = XCNEW (gfc_typebound_proc); | |
6916 | tb->where = gfc_current_locus; | |
6917 | m = match_binding_attributes (tb, false, true); | |
6918 | if (m == MATCH_ERROR) | |
6919 | return m; | |
6920 | ||
713485cc JW |
6921 | gfc_clear_attr (¤t_attr); |
6922 | current_attr.procedure = 1; | |
6923 | current_attr.proc_pointer = 1; | |
6924 | current_attr.access = tb->access; | |
6925 | current_attr.flavor = FL_PROCEDURE; | |
6926 | ||
6927 | /* Match the colons (required). */ | |
6928 | if (gfc_match (" ::") != MATCH_YES) | |
6929 | { | |
a4d9b221 | 6930 | gfc_error ("Expected %<::%> after binding-attributes at %C"); |
713485cc JW |
6931 | return MATCH_ERROR; |
6932 | } | |
6933 | ||
6934 | /* Check for C450. */ | |
6935 | if (!tb->nopass && proc_if == NULL) | |
6936 | { | |
6937 | gfc_error("NOPASS or explicit interface required at %C"); | |
6938 | return MATCH_ERROR; | |
6939 | } | |
6940 | ||
524af0d6 | 6941 | if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C")) |
3212c187 SK |
6942 | return MATCH_ERROR; |
6943 | ||
713485cc JW |
6944 | /* Match PPC names. */ |
6945 | ts = current_ts; | |
6946 | for(num=1;;num++) | |
6947 | { | |
6948 | m = gfc_match_name (name); | |
6949 | if (m == MATCH_NO) | |
6950 | goto syntax; | |
6951 | else if (m == MATCH_ERROR) | |
6952 | return m; | |
6953 | ||
524af0d6 | 6954 | if (!gfc_add_component (gfc_current_block(), name, &c)) |
713485cc JW |
6955 | return MATCH_ERROR; |
6956 | ||
6957 | /* Add current_attr to the symbol attributes. */ | |
524af0d6 | 6958 | if (!gfc_copy_attr (&c->attr, ¤t_attr, NULL)) |
713485cc JW |
6959 | return MATCH_ERROR; |
6960 | ||
524af0d6 | 6961 | if (!gfc_add_external (&c->attr, NULL)) |
713485cc JW |
6962 | return MATCH_ERROR; |
6963 | ||
524af0d6 | 6964 | if (!gfc_add_proc (&c->attr, name, NULL)) |
713485cc JW |
6965 | return MATCH_ERROR; |
6966 | ||
2be03814 TB |
6967 | if (num == 1) |
6968 | c->tb = tb; | |
6969 | else | |
6970 | { | |
6971 | c->tb = XCNEW (gfc_typebound_proc); | |
6972 | c->tb->where = gfc_current_locus; | |
6973 | *c->tb = *tb; | |
6974 | } | |
90661f26 | 6975 | |
713485cc JW |
6976 | /* Set interface. */ |
6977 | if (proc_if != NULL) | |
6978 | { | |
6979 | c->ts.interface = proc_if; | |
6980 | c->attr.untyped = 1; | |
6981 | c->attr.if_source = IFSRC_IFBODY; | |
6982 | } | |
6983 | else if (ts.type != BT_UNKNOWN) | |
6984 | { | |
6985 | c->ts = ts; | |
6986 | c->ts.interface = gfc_new_symbol ("", gfc_current_ns); | |
d7fee03d | 6987 | c->ts.interface->result = c->ts.interface; |
713485cc | 6988 | c->ts.interface->ts = ts; |
d91909c0 | 6989 | c->ts.interface->attr.flavor = FL_PROCEDURE; |
713485cc | 6990 | c->ts.interface->attr.function = 1; |
d91909c0 | 6991 | c->attr.function = 1; |
713485cc JW |
6992 | c->attr.if_source = IFSRC_UNKNOWN; |
6993 | } | |
6994 | ||
6995 | if (gfc_match (" =>") == MATCH_YES) | |
6996 | { | |
80f95228 | 6997 | m = match_pointer_init (&initializer, 1); |
713485cc JW |
6998 | if (m != MATCH_YES) |
6999 | { | |
7000 | gfc_free_expr (initializer); | |
7001 | return m; | |
7002 | } | |
7003 | c->initializer = initializer; | |
7004 | } | |
7005 | ||
7006 | if (gfc_match_eos () == MATCH_YES) | |
7007 | return MATCH_YES; | |
7008 | if (gfc_match_char (',') != MATCH_YES) | |
7009 | goto syntax; | |
7010 | } | |
7011 | ||
7012 | syntax: | |
7013 | gfc_error ("Syntax error in procedure pointer component at %C"); | |
7014 | return MATCH_ERROR; | |
7015 | } | |
7016 | ||
7017 | ||
69773742 JW |
7018 | /* Match a PROCEDURE declaration inside an interface (R1206). */ |
7019 | ||
7020 | static match | |
7021 | match_procedure_in_interface (void) | |
7022 | { | |
7023 | match m; | |
7024 | gfc_symbol *sym; | |
7025 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
a6fcd41a | 7026 | locus old_locus; |
69773742 JW |
7027 | |
7028 | if (current_interface.type == INTERFACE_NAMELESS | |
7029 | || current_interface.type == INTERFACE_ABSTRACT) | |
7030 | { | |
7031 | gfc_error ("PROCEDURE at %C must be in a generic interface"); | |
7032 | return MATCH_ERROR; | |
7033 | } | |
7034 | ||
a6fcd41a TB |
7035 | /* Check if the F2008 optional double colon appears. */ |
7036 | gfc_gobble_whitespace (); | |
7037 | old_locus = gfc_current_locus; | |
7038 | if (gfc_match ("::") == MATCH_YES) | |
7039 | { | |
524af0d6 JB |
7040 | if (!gfc_notify_std (GFC_STD_F2008, "double colon in " |
7041 | "MODULE PROCEDURE statement at %L", &old_locus)) | |
a6fcd41a TB |
7042 | return MATCH_ERROR; |
7043 | } | |
7044 | else | |
7045 | gfc_current_locus = old_locus; | |
7046 | ||
69773742 JW |
7047 | for(;;) |
7048 | { | |
7049 | m = gfc_match_name (name); | |
7050 | if (m == MATCH_NO) | |
7051 | goto syntax; | |
7052 | else if (m == MATCH_ERROR) | |
7053 | return m; | |
7054 | if (gfc_get_symbol (name, gfc_current_ns->parent, &sym)) | |
7055 | return MATCH_ERROR; | |
7056 | ||
524af0d6 | 7057 | if (!gfc_add_interface (sym)) |
69773742 JW |
7058 | return MATCH_ERROR; |
7059 | ||
69773742 JW |
7060 | if (gfc_match_eos () == MATCH_YES) |
7061 | break; | |
7062 | if (gfc_match_char (',') != MATCH_YES) | |
7063 | goto syntax; | |
7064 | } | |
7065 | ||
7066 | return MATCH_YES; | |
7067 | ||
7068 | syntax: | |
7069 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
7070 | return MATCH_ERROR; | |
7071 | } | |
7072 | ||
7073 | ||
7074 | /* General matcher for PROCEDURE declarations. */ | |
7075 | ||
30b608eb DK |
7076 | static match match_procedure_in_type (void); |
7077 | ||
69773742 JW |
7078 | match |
7079 | gfc_match_procedure (void) | |
7080 | { | |
7081 | match m; | |
7082 | ||
7083 | switch (gfc_current_state ()) | |
7084 | { | |
7085 | case COMP_NONE: | |
7086 | case COMP_PROGRAM: | |
7087 | case COMP_MODULE: | |
4668d6f9 | 7088 | case COMP_SUBMODULE: |
69773742 JW |
7089 | case COMP_SUBROUTINE: |
7090 | case COMP_FUNCTION: | |
3547d57e | 7091 | case COMP_BLOCK: |
69773742 JW |
7092 | m = match_procedure_decl (); |
7093 | break; | |
7094 | case COMP_INTERFACE: | |
7095 | m = match_procedure_in_interface (); | |
7096 | break; | |
7097 | case COMP_DERIVED: | |
713485cc JW |
7098 | m = match_ppc_decl (); |
7099 | break; | |
30b608eb DK |
7100 | case COMP_DERIVED_CONTAINS: |
7101 | m = match_procedure_in_type (); | |
7102 | break; | |
69773742 JW |
7103 | default: |
7104 | return MATCH_NO; | |
7105 | } | |
7106 | ||
7107 | if (m != MATCH_YES) | |
7108 | return m; | |
7109 | ||
524af0d6 | 7110 | if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C")) |
69773742 JW |
7111 | return MATCH_ERROR; |
7112 | ||
7113 | return m; | |
7114 | } | |
7115 | ||
7116 | ||
c3005b0f DK |
7117 | /* Warn if a matched procedure has the same name as an intrinsic; this is |
7118 | simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current | |
7119 | parser-state-stack to find out whether we're in a module. */ | |
7120 | ||
7121 | static void | |
73e42eef | 7122 | do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func) |
c3005b0f DK |
7123 | { |
7124 | bool in_module; | |
7125 | ||
7126 | in_module = (gfc_state_stack->previous | |
4668d6f9 PT |
7127 | && (gfc_state_stack->previous->state == COMP_MODULE |
7128 | || gfc_state_stack->previous->state == COMP_SUBMODULE)); | |
c3005b0f DK |
7129 | |
7130 | gfc_warn_intrinsic_shadow (sym, in_module, func); | |
7131 | } | |
7132 | ||
7133 | ||
6de9cd9a DN |
7134 | /* Match a function declaration. */ |
7135 | ||
7136 | match | |
7137 | gfc_match_function_decl (void) | |
7138 | { | |
7139 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7140 | gfc_symbol *sym, *result; | |
7141 | locus old_loc; | |
7142 | match m; | |
a8b3b0b6 | 7143 | match suffix_match; |
f5acf0f2 | 7144 | match found_match; /* Status returned by match func. */ |
6de9cd9a DN |
7145 | |
7146 | if (gfc_current_state () != COMP_NONE | |
7147 | && gfc_current_state () != COMP_INTERFACE | |
7148 | && gfc_current_state () != COMP_CONTAINS) | |
7149 | return MATCH_NO; | |
7150 | ||
7151 | gfc_clear_ts (¤t_ts); | |
7152 | ||
63645982 | 7153 | old_loc = gfc_current_locus; |
6de9cd9a | 7154 | |
1c8bcdf7 | 7155 | m = gfc_match_prefix (¤t_ts); |
6de9cd9a DN |
7156 | if (m != MATCH_YES) |
7157 | { | |
63645982 | 7158 | gfc_current_locus = old_loc; |
6de9cd9a DN |
7159 | return m; |
7160 | } | |
7161 | ||
7162 | if (gfc_match ("function% %n", name) != MATCH_YES) | |
7163 | { | |
63645982 | 7164 | gfc_current_locus = old_loc; |
6de9cd9a DN |
7165 | return MATCH_NO; |
7166 | } | |
4668d6f9 | 7167 | |
1a492601 | 7168 | if (get_proc_name (name, &sym, false)) |
6de9cd9a | 7169 | return MATCH_ERROR; |
3070bab4 | 7170 | |
524af0d6 | 7171 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
7172 | sym = sym->result; |
7173 | ||
4668d6f9 PT |
7174 | if (current_attr.module_procedure) |
7175 | sym->attr.module_procedure = 1; | |
7176 | ||
6de9cd9a DN |
7177 | gfc_new_block = sym; |
7178 | ||
7179 | m = gfc_match_formal_arglist (sym, 0, 0); | |
7180 | if (m == MATCH_NO) | |
2b9a33ae TS |
7181 | { |
7182 | gfc_error ("Expected formal argument list in function " | |
636dff67 | 7183 | "definition at %C"); |
2b9a33ae TS |
7184 | m = MATCH_ERROR; |
7185 | goto cleanup; | |
7186 | } | |
6de9cd9a DN |
7187 | else if (m == MATCH_ERROR) |
7188 | goto cleanup; | |
7189 | ||
7190 | result = NULL; | |
7191 | ||
a8b3b0b6 CR |
7192 | /* According to the draft, the bind(c) and result clause can |
7193 | come in either order after the formal_arg_list (i.e., either | |
7194 | can be first, both can exist together or by themselves or neither | |
7195 | one). Therefore, the match_result can't match the end of the | |
7196 | string, and check for the bind(c) or result clause in either order. */ | |
7197 | found_match = gfc_match_eos (); | |
7198 | ||
7199 | /* Make sure that it isn't already declared as BIND(C). If it is, it | |
7200 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
7201 | not allowed for procedures. */ | |
7202 | if (sym->attr.is_bind_c == 1) | |
7203 | { | |
7204 | sym->attr.is_bind_c = 0; | |
7205 | if (sym->old_symbol != NULL) | |
7206 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7207 | "variables or common blocks", | |
7208 | &(sym->old_symbol->declared_at)); | |
7209 | else | |
7210 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7211 | "variables or common blocks", &gfc_current_locus); | |
6de9cd9a DN |
7212 | } |
7213 | ||
a8b3b0b6 | 7214 | if (found_match != MATCH_YES) |
6de9cd9a | 7215 | { |
a8b3b0b6 CR |
7216 | /* If we haven't found the end-of-statement, look for a suffix. */ |
7217 | suffix_match = gfc_match_suffix (sym, &result); | |
7218 | if (suffix_match == MATCH_YES) | |
7219 | /* Need to get the eos now. */ | |
7220 | found_match = gfc_match_eos (); | |
7221 | else | |
7222 | found_match = suffix_match; | |
6de9cd9a DN |
7223 | } |
7224 | ||
a8b3b0b6 CR |
7225 | if(found_match != MATCH_YES) |
7226 | m = MATCH_ERROR; | |
6de9cd9a DN |
7227 | else |
7228 | { | |
a8b3b0b6 CR |
7229 | /* Make changes to the symbol. */ |
7230 | m = MATCH_ERROR; | |
f5acf0f2 | 7231 | |
524af0d6 | 7232 | if (!gfc_add_function (&sym->attr, sym->name, NULL)) |
a8b3b0b6 | 7233 | goto cleanup; |
f5acf0f2 | 7234 | |
70112e2a | 7235 | if (!gfc_missing_attr (&sym->attr, NULL)) |
a8b3b0b6 | 7236 | goto cleanup; |
6de9cd9a | 7237 | |
70112e2a PT |
7238 | if (!copy_prefix (&sym->attr, &sym->declared_at)) |
7239 | { | |
7240 | if(!sym->attr.module_procedure) | |
7241 | goto cleanup; | |
7242 | else | |
7243 | gfc_error_check (); | |
7244 | } | |
7245 | ||
a99d95a2 | 7246 | /* Delay matching the function characteristics until after the |
1c8bcdf7 | 7247 | specification block by signalling kind=-1. */ |
a99d95a2 PT |
7248 | sym->declared_at = old_loc; |
7249 | if (current_ts.type != BT_UNKNOWN) | |
7250 | current_ts.kind = -1; | |
7251 | else | |
7252 | current_ts.kind = 0; | |
1c8bcdf7 | 7253 | |
a8b3b0b6 CR |
7254 | if (result == NULL) |
7255 | { | |
6de7294f | 7256 | if (current_ts.type != BT_UNKNOWN |
524af0d6 | 7257 | && !gfc_add_type (sym, ¤t_ts, &gfc_current_locus)) |
6de7294f | 7258 | goto cleanup; |
a8b3b0b6 CR |
7259 | sym->result = sym; |
7260 | } | |
7261 | else | |
7262 | { | |
6de7294f | 7263 | if (current_ts.type != BT_UNKNOWN |
524af0d6 | 7264 | && !gfc_add_type (result, ¤t_ts, &gfc_current_locus)) |
6de7294f | 7265 | goto cleanup; |
a8b3b0b6 CR |
7266 | sym->result = result; |
7267 | } | |
7268 | ||
c3005b0f | 7269 | /* Warn if this procedure has the same name as an intrinsic. */ |
73e42eef | 7270 | do_warn_intrinsic_shadow (sym, true); |
c3005b0f | 7271 | |
a8b3b0b6 CR |
7272 | return MATCH_YES; |
7273 | } | |
6de9cd9a DN |
7274 | |
7275 | cleanup: | |
63645982 | 7276 | gfc_current_locus = old_loc; |
6de9cd9a DN |
7277 | return m; |
7278 | } | |
7279 | ||
636dff67 SK |
7280 | |
7281 | /* This is mostly a copy of parse.c(add_global_procedure) but modified to | |
7282 | pass the name of the entry, rather than the gfc_current_block name, and | |
7283 | to return false upon finding an existing global entry. */ | |
68ea355b PT |
7284 | |
7285 | static bool | |
3a43b5b3 TB |
7286 | add_global_entry (const char *name, const char *binding_label, bool sub, |
7287 | locus *where) | |
68ea355b PT |
7288 | { |
7289 | gfc_gsymbol *s; | |
32e8bb8e | 7290 | enum gfc_symbol_type type; |
68ea355b | 7291 | |
7389bce6 | 7292 | type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION; |
68ea355b | 7293 | |
f11de7c5 TB |
7294 | /* Only in Fortran 2003: For procedures with a binding label also the Fortran |
7295 | name is a global identifier. */ | |
7296 | if (!binding_label || gfc_notification_std (GFC_STD_F2008)) | |
68ea355b | 7297 | { |
55b9c612 | 7298 | s = gfc_get_gsymbol (name, false); |
f11de7c5 TB |
7299 | |
7300 | if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type)) | |
7301 | { | |
3a43b5b3 | 7302 | gfc_global_used (s, where); |
f11de7c5 TB |
7303 | return false; |
7304 | } | |
7305 | else | |
7306 | { | |
7307 | s->type = type; | |
77f8682b | 7308 | s->sym_name = name; |
3a43b5b3 | 7309 | s->where = *where; |
f11de7c5 TB |
7310 | s->defined = 1; |
7311 | s->ns = gfc_current_ns; | |
7312 | } | |
68ea355b | 7313 | } |
f11de7c5 TB |
7314 | |
7315 | /* Don't add the symbol multiple times. */ | |
7316 | if (binding_label | |
7317 | && (!gfc_notification_std (GFC_STD_F2008) | |
7318 | || strcmp (name, binding_label) != 0)) | |
7319 | { | |
55b9c612 | 7320 | s = gfc_get_gsymbol (binding_label, true); |
f11de7c5 TB |
7321 | |
7322 | if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type)) | |
7323 | { | |
3a43b5b3 | 7324 | gfc_global_used (s, where); |
f11de7c5 TB |
7325 | return false; |
7326 | } | |
7327 | else | |
7328 | { | |
7329 | s->type = type; | |
77f8682b | 7330 | s->sym_name = name; |
f11de7c5 | 7331 | s->binding_label = binding_label; |
3a43b5b3 | 7332 | s->where = *where; |
f11de7c5 TB |
7333 | s->defined = 1; |
7334 | s->ns = gfc_current_ns; | |
7335 | } | |
7336 | } | |
7337 | ||
7338 | return true; | |
68ea355b | 7339 | } |
6de9cd9a | 7340 | |
636dff67 | 7341 | |
6de9cd9a DN |
7342 | /* Match an ENTRY statement. */ |
7343 | ||
7344 | match | |
7345 | gfc_match_entry (void) | |
7346 | { | |
3d79abbd PB |
7347 | gfc_symbol *proc; |
7348 | gfc_symbol *result; | |
7349 | gfc_symbol *entry; | |
6de9cd9a DN |
7350 | char name[GFC_MAX_SYMBOL_LEN + 1]; |
7351 | gfc_compile_state state; | |
7352 | match m; | |
3d79abbd | 7353 | gfc_entry_list *el; |
c96cfa49 | 7354 | locus old_loc; |
1a492601 | 7355 | bool module_procedure; |
bc3e7a8c TB |
7356 | char peek_char; |
7357 | match is_bind_c; | |
6de9cd9a DN |
7358 | |
7359 | m = gfc_match_name (name); | |
7360 | if (m != MATCH_YES) | |
7361 | return m; | |
7362 | ||
524af0d6 | 7363 | if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C")) |
58fc89f6 TB |
7364 | return MATCH_ERROR; |
7365 | ||
3d79abbd | 7366 | state = gfc_current_state (); |
4c93c95a | 7367 | if (state != COMP_SUBROUTINE && state != COMP_FUNCTION) |
3d79abbd | 7368 | { |
4c93c95a FXC |
7369 | switch (state) |
7370 | { | |
7371 | case COMP_PROGRAM: | |
7372 | gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM"); | |
7373 | break; | |
7374 | case COMP_MODULE: | |
7375 | gfc_error ("ENTRY statement at %C cannot appear within a MODULE"); | |
7376 | break; | |
4668d6f9 PT |
7377 | case COMP_SUBMODULE: |
7378 | gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE"); | |
7379 | break; | |
4c93c95a | 7380 | case COMP_BLOCK_DATA: |
636dff67 SK |
7381 | gfc_error ("ENTRY statement at %C cannot appear within " |
7382 | "a BLOCK DATA"); | |
4c93c95a FXC |
7383 | break; |
7384 | case COMP_INTERFACE: | |
636dff67 SK |
7385 | gfc_error ("ENTRY statement at %C cannot appear within " |
7386 | "an INTERFACE"); | |
4c93c95a | 7387 | break; |
f6288c24 FR |
7388 | case COMP_STRUCTURE: |
7389 | gfc_error ("ENTRY statement at %C cannot appear within " | |
7390 | "a STRUCTURE block"); | |
7391 | break; | |
4c93c95a | 7392 | case COMP_DERIVED: |
636dff67 SK |
7393 | gfc_error ("ENTRY statement at %C cannot appear within " |
7394 | "a DERIVED TYPE block"); | |
4c93c95a FXC |
7395 | break; |
7396 | case COMP_IF: | |
636dff67 SK |
7397 | gfc_error ("ENTRY statement at %C cannot appear within " |
7398 | "an IF-THEN block"); | |
4c93c95a FXC |
7399 | break; |
7400 | case COMP_DO: | |
8c6a85e3 | 7401 | case COMP_DO_CONCURRENT: |
636dff67 SK |
7402 | gfc_error ("ENTRY statement at %C cannot appear within " |
7403 | "a DO block"); | |
4c93c95a FXC |
7404 | break; |
7405 | case COMP_SELECT: | |
636dff67 SK |
7406 | gfc_error ("ENTRY statement at %C cannot appear within " |
7407 | "a SELECT block"); | |
4c93c95a FXC |
7408 | break; |
7409 | case COMP_FORALL: | |
636dff67 SK |
7410 | gfc_error ("ENTRY statement at %C cannot appear within " |
7411 | "a FORALL block"); | |
4c93c95a FXC |
7412 | break; |
7413 | case COMP_WHERE: | |
636dff67 SK |
7414 | gfc_error ("ENTRY statement at %C cannot appear within " |
7415 | "a WHERE block"); | |
4c93c95a FXC |
7416 | break; |
7417 | case COMP_CONTAINS: | |
636dff67 SK |
7418 | gfc_error ("ENTRY statement at %C cannot appear within " |
7419 | "a contained subprogram"); | |
4c93c95a FXC |
7420 | break; |
7421 | default: | |
fce523bf | 7422 | gfc_error ("Unexpected ENTRY statement at %C"); |
4c93c95a | 7423 | } |
3d79abbd PB |
7424 | return MATCH_ERROR; |
7425 | } | |
7426 | ||
5f0ba745 SK |
7427 | if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION) |
7428 | && gfc_state_stack->previous->state == COMP_INTERFACE) | |
7429 | { | |
7430 | gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE"); | |
7431 | return MATCH_ERROR; | |
7432 | } | |
7433 | ||
1a492601 | 7434 | module_procedure = gfc_current_ns->parent != NULL |
636dff67 SK |
7435 | && gfc_current_ns->parent->proc_name |
7436 | && gfc_current_ns->parent->proc_name->attr.flavor | |
7437 | == FL_MODULE; | |
1a492601 | 7438 | |
3d79abbd PB |
7439 | if (gfc_current_ns->parent != NULL |
7440 | && gfc_current_ns->parent->proc_name | |
1a492601 | 7441 | && !module_procedure) |
3d79abbd PB |
7442 | { |
7443 | gfc_error("ENTRY statement at %C cannot appear in a " | |
7444 | "contained procedure"); | |
7445 | return MATCH_ERROR; | |
7446 | } | |
7447 | ||
1a492601 PT |
7448 | /* Module function entries need special care in get_proc_name |
7449 | because previous references within the function will have | |
7450 | created symbols attached to the current namespace. */ | |
7451 | if (get_proc_name (name, &entry, | |
7452 | gfc_current_ns->parent != NULL | |
ecd3b73c | 7453 | && module_procedure)) |
6de9cd9a DN |
7454 | return MATCH_ERROR; |
7455 | ||
3d79abbd PB |
7456 | proc = gfc_current_block (); |
7457 | ||
bc3e7a8c TB |
7458 | /* Make sure that it isn't already declared as BIND(C). If it is, it |
7459 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
7460 | not allowed for procedures. */ | |
7461 | if (entry->attr.is_bind_c == 1) | |
7462 | { | |
7463 | entry->attr.is_bind_c = 0; | |
7464 | if (entry->old_symbol != NULL) | |
7465 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7466 | "variables or common blocks", | |
7467 | &(entry->old_symbol->declared_at)); | |
7468 | else | |
7469 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7470 | "variables or common blocks", &gfc_current_locus); | |
7471 | } | |
f5acf0f2 | 7472 | |
bc3e7a8c TB |
7473 | /* Check what next non-whitespace character is so we can tell if there |
7474 | is the required parens if we have a BIND(C). */ | |
3a43b5b3 | 7475 | old_loc = gfc_current_locus; |
bc3e7a8c | 7476 | gfc_gobble_whitespace (); |
8fc541d3 | 7477 | peek_char = gfc_peek_ascii_char (); |
bc3e7a8c | 7478 | |
3d79abbd | 7479 | if (state == COMP_SUBROUTINE) |
6de9cd9a | 7480 | { |
6de9cd9a DN |
7481 | m = gfc_match_formal_arglist (entry, 0, 1); |
7482 | if (m != MATCH_YES) | |
7483 | return MATCH_ERROR; | |
7484 | ||
1eabf70a TB |
7485 | /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can |
7486 | never be an internal procedure. */ | |
7487 | is_bind_c = gfc_match_bind_c (entry, true); | |
bc3e7a8c TB |
7488 | if (is_bind_c == MATCH_ERROR) |
7489 | return MATCH_ERROR; | |
7490 | if (is_bind_c == MATCH_YES) | |
7491 | { | |
7492 | if (peek_char != '(') | |
7493 | { | |
7494 | gfc_error ("Missing required parentheses before BIND(C) at %C"); | |
7495 | return MATCH_ERROR; | |
7496 | } | |
89508a3f SK |
7497 | |
7498 | if (!gfc_add_is_bind_c (&(entry->attr), entry->name, | |
7499 | &(entry->declared_at), 1)) | |
7500 | return MATCH_ERROR; | |
0a524296 | 7501 | |
bc3e7a8c TB |
7502 | } |
7503 | ||
f11de7c5 | 7504 | if (!gfc_current_ns->parent |
3a43b5b3 TB |
7505 | && !add_global_entry (name, entry->binding_label, true, |
7506 | &old_loc)) | |
f11de7c5 TB |
7507 | return MATCH_ERROR; |
7508 | ||
7509 | /* An entry in a subroutine. */ | |
524af0d6 JB |
7510 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
7511 | || !gfc_add_subroutine (&entry->attr, entry->name, NULL)) | |
6de9cd9a | 7512 | return MATCH_ERROR; |
3d79abbd PB |
7513 | } |
7514 | else | |
7515 | { | |
c96cfa49 | 7516 | /* An entry in a function. |
636dff67 SK |
7517 | We need to take special care because writing |
7518 | ENTRY f() | |
7519 | as | |
7520 | ENTRY f | |
7521 | is allowed, whereas | |
7522 | ENTRY f() RESULT (r) | |
7523 | can't be written as | |
7524 | ENTRY f RESULT (r). */ | |
c96cfa49 TS |
7525 | if (gfc_match_eos () == MATCH_YES) |
7526 | { | |
7527 | gfc_current_locus = old_loc; | |
7528 | /* Match the empty argument list, and add the interface to | |
7529 | the symbol. */ | |
7530 | m = gfc_match_formal_arglist (entry, 0, 1); | |
7531 | } | |
7532 | else | |
7533 | m = gfc_match_formal_arglist (entry, 0, 0); | |
7534 | ||
6de9cd9a DN |
7535 | if (m != MATCH_YES) |
7536 | return MATCH_ERROR; | |
7537 | ||
6de9cd9a DN |
7538 | result = NULL; |
7539 | ||
7540 | if (gfc_match_eos () == MATCH_YES) | |
7541 | { | |
524af0d6 JB |
7542 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
7543 | || !gfc_add_function (&entry->attr, entry->name, NULL)) | |
6de9cd9a DN |
7544 | return MATCH_ERROR; |
7545 | ||
d198b59a | 7546 | entry->result = entry; |
6de9cd9a DN |
7547 | } |
7548 | else | |
7549 | { | |
bc3e7a8c | 7550 | m = gfc_match_suffix (entry, &result); |
6de9cd9a DN |
7551 | if (m == MATCH_NO) |
7552 | gfc_syntax_error (ST_ENTRY); | |
7553 | if (m != MATCH_YES) | |
7554 | return MATCH_ERROR; | |
7555 | ||
bc3e7a8c TB |
7556 | if (result) |
7557 | { | |
524af0d6 JB |
7558 | if (!gfc_add_result (&result->attr, result->name, NULL) |
7559 | || !gfc_add_entry (&entry->attr, result->name, NULL) | |
7560 | || !gfc_add_function (&entry->attr, result->name, NULL)) | |
bc3e7a8c TB |
7561 | return MATCH_ERROR; |
7562 | entry->result = result; | |
7563 | } | |
7564 | else | |
7565 | { | |
524af0d6 JB |
7566 | if (!gfc_add_entry (&entry->attr, entry->name, NULL) |
7567 | || !gfc_add_function (&entry->attr, entry->name, NULL)) | |
bc3e7a8c TB |
7568 | return MATCH_ERROR; |
7569 | entry->result = entry; | |
7570 | } | |
6de9cd9a | 7571 | } |
f11de7c5 TB |
7572 | |
7573 | if (!gfc_current_ns->parent | |
3a43b5b3 TB |
7574 | && !add_global_entry (name, entry->binding_label, false, |
7575 | &old_loc)) | |
f11de7c5 | 7576 | return MATCH_ERROR; |
6de9cd9a DN |
7577 | } |
7578 | ||
7579 | if (gfc_match_eos () != MATCH_YES) | |
7580 | { | |
7581 | gfc_syntax_error (ST_ENTRY); | |
7582 | return MATCH_ERROR; | |
7583 | } | |
7584 | ||
89508a3f SK |
7585 | /* F2018:C1546 An elemental procedure shall not have the BIND attribute. */ |
7586 | if (proc->attr.elemental && entry->attr.is_bind_c) | |
7587 | { | |
7588 | gfc_error ("ENTRY statement at %L with BIND(C) prohibited in an " | |
7589 | "elemental procedure", &entry->declared_at); | |
7590 | return MATCH_ERROR; | |
7591 | } | |
7592 | ||
3d79abbd PB |
7593 | entry->attr.recursive = proc->attr.recursive; |
7594 | entry->attr.elemental = proc->attr.elemental; | |
7595 | entry->attr.pure = proc->attr.pure; | |
6de9cd9a | 7596 | |
3d79abbd PB |
7597 | el = gfc_get_entry_list (); |
7598 | el->sym = entry; | |
7599 | el->next = gfc_current_ns->entries; | |
7600 | gfc_current_ns->entries = el; | |
7601 | if (el->next) | |
7602 | el->id = el->next->id + 1; | |
7603 | else | |
7604 | el->id = 1; | |
6de9cd9a | 7605 | |
3d79abbd PB |
7606 | new_st.op = EXEC_ENTRY; |
7607 | new_st.ext.entry = el; | |
7608 | ||
7609 | return MATCH_YES; | |
6de9cd9a DN |
7610 | } |
7611 | ||
7612 | ||
7613 | /* Match a subroutine statement, including optional prefixes. */ | |
7614 | ||
7615 | match | |
7616 | gfc_match_subroutine (void) | |
7617 | { | |
7618 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7619 | gfc_symbol *sym; | |
7620 | match m; | |
a8b3b0b6 CR |
7621 | match is_bind_c; |
7622 | char peek_char; | |
1eabf70a | 7623 | bool allow_binding_name; |
f28c46cd | 7624 | locus loc; |
6de9cd9a DN |
7625 | |
7626 | if (gfc_current_state () != COMP_NONE | |
7627 | && gfc_current_state () != COMP_INTERFACE | |
7628 | && gfc_current_state () != COMP_CONTAINS) | |
7629 | return MATCH_NO; | |
7630 | ||
1c8bcdf7 | 7631 | m = gfc_match_prefix (NULL); |
6de9cd9a DN |
7632 | if (m != MATCH_YES) |
7633 | return m; | |
7634 | ||
7635 | m = gfc_match ("subroutine% %n", name); | |
7636 | if (m != MATCH_YES) | |
7637 | return m; | |
7638 | ||
1a492601 | 7639 | if (get_proc_name (name, &sym, false)) |
6de9cd9a | 7640 | return MATCH_ERROR; |
3070bab4 | 7641 | |
7fcd5ad5 | 7642 | /* Set declared_at as it might point to, e.g., a PUBLIC statement, if |
1cc0e193 | 7643 | the symbol existed before. */ |
7fcd5ad5 TB |
7644 | sym->declared_at = gfc_current_locus; |
7645 | ||
4668d6f9 PT |
7646 | if (current_attr.module_procedure) |
7647 | sym->attr.module_procedure = 1; | |
7648 | ||
524af0d6 | 7649 | if (add_hidden_procptr_result (sym)) |
3070bab4 JW |
7650 | sym = sym->result; |
7651 | ||
6de9cd9a DN |
7652 | gfc_new_block = sym; |
7653 | ||
a8b3b0b6 | 7654 | /* Check what next non-whitespace character is so we can tell if there |
bc3e7a8c | 7655 | is the required parens if we have a BIND(C). */ |
a8b3b0b6 | 7656 | gfc_gobble_whitespace (); |
8fc541d3 | 7657 | peek_char = gfc_peek_ascii_char (); |
f5acf0f2 | 7658 | |
524af0d6 | 7659 | if (!gfc_add_subroutine (&sym->attr, sym->name, NULL)) |
6de9cd9a DN |
7660 | return MATCH_ERROR; |
7661 | ||
7662 | if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES) | |
7663 | return MATCH_ERROR; | |
7664 | ||
a8b3b0b6 CR |
7665 | /* Make sure that it isn't already declared as BIND(C). If it is, it |
7666 | must have been marked BIND(C) with a BIND(C) attribute and that is | |
7667 | not allowed for procedures. */ | |
7668 | if (sym->attr.is_bind_c == 1) | |
7669 | { | |
7670 | sym->attr.is_bind_c = 0; | |
7671 | if (sym->old_symbol != NULL) | |
7672 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7673 | "variables or common blocks", | |
7674 | &(sym->old_symbol->declared_at)); | |
7675 | else | |
7676 | gfc_error_now ("BIND(C) attribute at %L can only be used for " | |
7677 | "variables or common blocks", &gfc_current_locus); | |
7678 | } | |
1eabf70a TB |
7679 | |
7680 | /* C binding names are not allowed for internal procedures. */ | |
7681 | if (gfc_current_state () == COMP_CONTAINS | |
7682 | && sym->ns->proc_name->attr.flavor != FL_MODULE) | |
7683 | allow_binding_name = false; | |
7684 | else | |
7685 | allow_binding_name = true; | |
7686 | ||
a8b3b0b6 CR |
7687 | /* Here, we are just checking if it has the bind(c) attribute, and if |
7688 | so, then we need to make sure it's all correct. If it doesn't, | |
7689 | we still need to continue matching the rest of the subroutine line. */ | |
f28c46cd SK |
7690 | gfc_gobble_whitespace (); |
7691 | loc = gfc_current_locus; | |
1eabf70a | 7692 | is_bind_c = gfc_match_bind_c (sym, allow_binding_name); |
a8b3b0b6 CR |
7693 | if (is_bind_c == MATCH_ERROR) |
7694 | { | |
7695 | /* There was an attempt at the bind(c), but it was wrong. An | |
7696 | error message should have been printed w/in the gfc_match_bind_c | |
7697 | so here we'll just return the MATCH_ERROR. */ | |
7698 | return MATCH_ERROR; | |
7699 | } | |
7700 | ||
7701 | if (is_bind_c == MATCH_YES) | |
7702 | { | |
f28c46cd SK |
7703 | gfc_formal_arglist *arg; |
7704 | ||
1eabf70a | 7705 | /* The following is allowed in the Fortran 2008 draft. */ |
01f4fff1 | 7706 | if (gfc_current_state () == COMP_CONTAINS |
1eabf70a | 7707 | && sym->ns->proc_name->attr.flavor != FL_MODULE |
524af0d6 JB |
7708 | && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute " |
7709 | "at %L may not be specified for an internal " | |
7710 | "procedure", &gfc_current_locus)) | |
1eabf70a TB |
7711 | return MATCH_ERROR; |
7712 | ||
a8b3b0b6 CR |
7713 | if (peek_char != '(') |
7714 | { | |
7715 | gfc_error ("Missing required parentheses before BIND(C) at %C"); | |
7716 | return MATCH_ERROR; | |
7717 | } | |
f28c46cd SK |
7718 | |
7719 | /* Scan the dummy arguments for an alternate return. */ | |
7720 | for (arg = sym->formal; arg; arg = arg->next) | |
7721 | if (!arg->sym) | |
7722 | { | |
7723 | gfc_error ("Alternate return dummy argument cannot appear in a " | |
7724 | "SUBROUTINE with the BIND(C) attribute at %L", &loc); | |
7725 | return MATCH_ERROR; | |
7726 | } | |
7727 | ||
7728 | if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)) | |
a8b3b0b6 CR |
7729 | return MATCH_ERROR; |
7730 | } | |
f5acf0f2 | 7731 | |
6de9cd9a DN |
7732 | if (gfc_match_eos () != MATCH_YES) |
7733 | { | |
7734 | gfc_syntax_error (ST_SUBROUTINE); | |
7735 | return MATCH_ERROR; | |
7736 | } | |
7737 | ||
524af0d6 | 7738 | if (!copy_prefix (&sym->attr, &sym->declared_at)) |
70112e2a PT |
7739 | { |
7740 | if(!sym->attr.module_procedure) | |
7741 | return MATCH_ERROR; | |
7742 | else | |
7743 | gfc_error_check (); | |
7744 | } | |
6de9cd9a | 7745 | |
c3005b0f | 7746 | /* Warn if it has the same name as an intrinsic. */ |
73e42eef | 7747 | do_warn_intrinsic_shadow (sym, false); |
c3005b0f | 7748 | |
6de9cd9a DN |
7749 | return MATCH_YES; |
7750 | } | |
7751 | ||
7752 | ||
3b37ccd4 FXC |
7753 | /* Check that the NAME identifier in a BIND attribute or statement |
7754 | is conform to C identifier rules. */ | |
7755 | ||
7756 | match | |
7757 | check_bind_name_identifier (char **name) | |
7758 | { | |
7759 | char *n = *name, *p; | |
7760 | ||
7761 | /* Remove leading spaces. */ | |
7762 | while (*n == ' ') | |
7763 | n++; | |
7764 | ||
7765 | /* On an empty string, free memory and set name to NULL. */ | |
7766 | if (*n == '\0') | |
7767 | { | |
7768 | free (*name); | |
7769 | *name = NULL; | |
7770 | return MATCH_YES; | |
7771 | } | |
7772 | ||
7773 | /* Remove trailing spaces. */ | |
7774 | p = n + strlen(n) - 1; | |
7775 | while (*p == ' ') | |
7776 | *(p--) = '\0'; | |
7777 | ||
7778 | /* Insert the identifier into the symbol table. */ | |
7779 | p = xstrdup (n); | |
7780 | free (*name); | |
7781 | *name = p; | |
7782 | ||
7783 | /* Now check that identifier is valid under C rules. */ | |
7784 | if (ISDIGIT (*p)) | |
7785 | { | |
7786 | gfc_error ("Invalid C identifier in NAME= specifier at %C"); | |
7787 | return MATCH_ERROR; | |
7788 | } | |
7789 | ||
7790 | for (; *p; p++) | |
7791 | if (!(ISALNUM (*p) || *p == '_' || *p == '$')) | |
7792 | { | |
7793 | gfc_error ("Invalid C identifier in NAME= specifier at %C"); | |
7794 | return MATCH_ERROR; | |
7795 | } | |
7796 | ||
7797 | return MATCH_YES; | |
7798 | } | |
7799 | ||
7800 | ||
a8b3b0b6 CR |
7801 | /* Match a BIND(C) specifier, with the optional 'name=' specifier if |
7802 | given, and set the binding label in either the given symbol (if not | |
86bf520d | 7803 | NULL), or in the current_ts. The symbol may be NULL because we may |
a8b3b0b6 CR |
7804 | encounter the BIND(C) before the declaration itself. Return |
7805 | MATCH_NO if what we're looking at isn't a BIND(C) specifier, | |
7806 | MATCH_ERROR if it is a BIND(C) clause but an error was encountered, | |
7807 | or MATCH_YES if the specifier was correct and the binding label and | |
7808 | bind(c) fields were set correctly for the given symbol or the | |
1eabf70a TB |
7809 | current_ts. If allow_binding_name is false, no binding name may be |
7810 | given. */ | |
a8b3b0b6 CR |
7811 | |
7812 | match | |
1eabf70a | 7813 | gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name) |
a8b3b0b6 | 7814 | { |
3b37ccd4 FXC |
7815 | char *binding_label = NULL; |
7816 | gfc_expr *e = NULL; | |
a8b3b0b6 | 7817 | |
f5acf0f2 | 7818 | /* Initialize the flag that specifies whether we encountered a NAME= |
a8b3b0b6 CR |
7819 | specifier or not. */ |
7820 | has_name_equals = 0; | |
7821 | ||
a8b3b0b6 CR |
7822 | /* This much we have to be able to match, in this order, if |
7823 | there is a bind(c) label. */ | |
7824 | if (gfc_match (" bind ( c ") != MATCH_YES) | |
7825 | return MATCH_NO; | |
7826 | ||
7827 | /* Now see if there is a binding label, or if we've reached the | |
7828 | end of the bind(c) attribute without one. */ | |
7829 | if (gfc_match_char (',') == MATCH_YES) | |
7830 | { | |
7831 | if (gfc_match (" name = ") != MATCH_YES) | |
7832 | { | |
7833 | gfc_error ("Syntax error in NAME= specifier for binding label " | |
7834 | "at %C"); | |
7835 | /* should give an error message here */ | |
7836 | return MATCH_ERROR; | |
7837 | } | |
7838 | ||
7839 | has_name_equals = 1; | |
7840 | ||
3b37ccd4 FXC |
7841 | if (gfc_match_init_expr (&e) != MATCH_YES) |
7842 | { | |
7843 | gfc_free_expr (e); | |
7844 | return MATCH_ERROR; | |
7845 | } | |
f5acf0f2 | 7846 | |
3b37ccd4 | 7847 | if (!gfc_simplify_expr(e, 0)) |
a8b3b0b6 | 7848 | { |
3b37ccd4 FXC |
7849 | gfc_error ("NAME= specifier at %C should be a constant expression"); |
7850 | gfc_free_expr (e); | |
7851 | return MATCH_ERROR; | |
a8b3b0b6 | 7852 | } |
3b37ccd4 FXC |
7853 | |
7854 | if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER | |
7855 | || e->ts.kind != gfc_default_character_kind || e->rank != 0) | |
a8b3b0b6 | 7856 | { |
3b37ccd4 FXC |
7857 | gfc_error ("NAME= specifier at %C should be a scalar of " |
7858 | "default character kind"); | |
7859 | gfc_free_expr(e); | |
7860 | return MATCH_ERROR; | |
a8b3b0b6 | 7861 | } |
3b37ccd4 FXC |
7862 | |
7863 | // Get a C string from the Fortran string constant | |
7864 | binding_label = gfc_widechar_to_char (e->value.character.string, | |
7865 | e->value.character.length); | |
7866 | gfc_free_expr(e); | |
7867 | ||
7868 | // Check that it is valid (old gfc_match_name_C) | |
7869 | if (check_bind_name_identifier (&binding_label) != MATCH_YES) | |
7870 | return MATCH_ERROR; | |
7871 | } | |
a8b3b0b6 CR |
7872 | |
7873 | /* Get the required right paren. */ | |
7874 | if (gfc_match_char (')') != MATCH_YES) | |
7875 | { | |
7876 | gfc_error ("Missing closing paren for binding label at %C"); | |
7877 | return MATCH_ERROR; | |
7878 | } | |
7879 | ||
1eabf70a TB |
7880 | if (has_name_equals && !allow_binding_name) |
7881 | { | |
7882 | gfc_error ("No binding name is allowed in BIND(C) at %C"); | |
7883 | return MATCH_ERROR; | |
7884 | } | |
7885 | ||
7886 | if (has_name_equals && sym != NULL && sym->attr.dummy) | |
7887 | { | |
7888 | gfc_error ("For dummy procedure %s, no binding name is " | |
7889 | "allowed in BIND(C) at %C", sym->name); | |
7890 | return MATCH_ERROR; | |
7891 | } | |
7892 | ||
7893 | ||
a8b3b0b6 CR |
7894 | /* Save the binding label to the symbol. If sym is null, we're |
7895 | probably matching the typespec attributes of a declaration and | |
7896 | haven't gotten the name yet, and therefore, no symbol yet. */ | |
62603fae | 7897 | if (binding_label) |
a8b3b0b6 CR |
7898 | { |
7899 | if (sym != NULL) | |
62603fae | 7900 | sym->binding_label = binding_label; |
a8b3b0b6 | 7901 | else |
62603fae | 7902 | curr_binding_label = binding_label; |
a8b3b0b6 | 7903 | } |
1eabf70a | 7904 | else if (allow_binding_name) |
a8b3b0b6 CR |
7905 | { |
7906 | /* No binding label, but if symbol isn't null, we | |
1eabf70a TB |
7907 | can set the label for it here. |
7908 | If name="" or allow_binding_name is false, no C binding name is | |
1cc0e193 | 7909 | created. */ |
a8b3b0b6 | 7910 | if (sym != NULL && sym->name != NULL && has_name_equals == 0) |
62603fae | 7911 | sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name)); |
a8b3b0b6 | 7912 | } |
9e1d712c | 7913 | |
129d15a3 JW |
7914 | if (has_name_equals && gfc_current_state () == COMP_INTERFACE |
7915 | && current_interface.type == INTERFACE_ABSTRACT) | |
9e1d712c TB |
7916 | { |
7917 | gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C"); | |
7918 | return MATCH_ERROR; | |
7919 | } | |
7920 | ||
a8b3b0b6 CR |
7921 | return MATCH_YES; |
7922 | } | |
7923 | ||
7924 | ||
1f2959f0 | 7925 | /* Return nonzero if we're currently compiling a contained procedure. */ |
ddc9ce91 TS |
7926 | |
7927 | static int | |
7928 | contained_procedure (void) | |
7929 | { | |
083de129 | 7930 | gfc_state_data *s = gfc_state_stack; |
ddc9ce91 | 7931 | |
083de129 TB |
7932 | if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION) |
7933 | && s->previous != NULL && s->previous->state == COMP_CONTAINS) | |
7934 | return 1; | |
ddc9ce91 TS |
7935 | |
7936 | return 0; | |
7937 | } | |
7938 | ||
d51347f9 | 7939 | /* Set the kind of each enumerator. The kind is selected such that it is |
25d8f0a2 TS |
7940 | interoperable with the corresponding C enumeration type, making |
7941 | sure that -fshort-enums is honored. */ | |
7942 | ||
7943 | static void | |
7944 | set_enum_kind(void) | |
7945 | { | |
7946 | enumerator_history *current_history = NULL; | |
7947 | int kind; | |
7948 | int i; | |
7949 | ||
7950 | if (max_enum == NULL || enum_history == NULL) | |
7951 | return; | |
7952 | ||
cab129d1 | 7953 | if (!flag_short_enums) |
d51347f9 TB |
7954 | return; |
7955 | ||
25d8f0a2 TS |
7956 | i = 0; |
7957 | do | |
7958 | { | |
7959 | kind = gfc_integer_kinds[i++].kind; | |
7960 | } | |
d51347f9 | 7961 | while (kind < gfc_c_int_kind |
25d8f0a2 TS |
7962 | && gfc_check_integer_range (max_enum->initializer->value.integer, |
7963 | kind) != ARITH_OK); | |
7964 | ||
7965 | current_history = enum_history; | |
7966 | while (current_history != NULL) | |
7967 | { | |
7968 | current_history->sym->ts.kind = kind; | |
7969 | current_history = current_history->next; | |
7970 | } | |
7971 | } | |
7972 | ||
636dff67 | 7973 | |
6de9cd9a | 7974 | /* Match any of the various end-block statements. Returns the type of |
9abe5e56 DK |
7975 | END to the caller. The END INTERFACE, END IF, END DO, END SELECT |
7976 | and END BLOCK statements cannot be replaced by a single END statement. */ | |
6de9cd9a DN |
7977 | |
7978 | match | |
636dff67 | 7979 | gfc_match_end (gfc_statement *st) |
6de9cd9a DN |
7980 | { |
7981 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7982 | gfc_compile_state state; | |
7983 | locus old_loc; | |
7984 | const char *block_name; | |
7985 | const char *target; | |
ddc9ce91 | 7986 | int eos_ok; |
6de9cd9a | 7987 | match m; |
0cab6b73 TK |
7988 | gfc_namespace *parent_ns, *ns, *prev_ns; |
7989 | gfc_namespace **nsp; | |
63af1586 | 7990 | bool abreviated_modproc_decl = false; |
874108a9 | 7991 | bool got_matching_end = false; |
6de9cd9a | 7992 | |
63645982 | 7993 | old_loc = gfc_current_locus; |
6de9cd9a DN |
7994 | if (gfc_match ("end") != MATCH_YES) |
7995 | return MATCH_NO; | |
7996 | ||
7997 | state = gfc_current_state (); | |
636dff67 SK |
7998 | block_name = gfc_current_block () == NULL |
7999 | ? NULL : gfc_current_block ()->name; | |
6de9cd9a | 8000 | |
03af1e4c | 8001 | switch (state) |
6de9cd9a | 8002 | { |
03af1e4c DK |
8003 | case COMP_ASSOCIATE: |
8004 | case COMP_BLOCK: | |
2eb3745a | 8005 | if (gfc_str_startswith (block_name, "block@")) |
03af1e4c DK |
8006 | block_name = NULL; |
8007 | break; | |
8008 | ||
8009 | case COMP_CONTAINS: | |
8010 | case COMP_DERIVED_CONTAINS: | |
6de9cd9a | 8011 | state = gfc_state_stack->previous->state; |
636dff67 SK |
8012 | block_name = gfc_state_stack->previous->sym == NULL |
8013 | ? NULL : gfc_state_stack->previous->sym->name; | |
63af1586 PT |
8014 | abreviated_modproc_decl = gfc_state_stack->previous->sym |
8015 | && gfc_state_stack->previous->sym->abr_modproc_decl; | |
03af1e4c DK |
8016 | break; |
8017 | ||
8018 | default: | |
8019 | break; | |
6de9cd9a DN |
8020 | } |
8021 | ||
63af1586 PT |
8022 | if (!abreviated_modproc_decl) |
8023 | abreviated_modproc_decl = gfc_current_block () | |
8024 | && gfc_current_block ()->abr_modproc_decl; | |
4668d6f9 | 8025 | |
6de9cd9a DN |
8026 | switch (state) |
8027 | { | |
8028 | case COMP_NONE: | |
8029 | case COMP_PROGRAM: | |
8030 | *st = ST_END_PROGRAM; | |
8031 | target = " program"; | |
ddc9ce91 | 8032 | eos_ok = 1; |
6de9cd9a DN |
8033 | break; |
8034 | ||
8035 | case COMP_SUBROUTINE: | |
8036 | *st = ST_END_SUBROUTINE; | |
4668d6f9 | 8037 | if (!abreviated_modproc_decl) |
6de9cd9a | 8038 | target = " subroutine"; |
4668d6f9 PT |
8039 | else |
8040 | target = " procedure"; | |
ddc9ce91 | 8041 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
8042 | break; |
8043 | ||
8044 | case COMP_FUNCTION: | |
8045 | *st = ST_END_FUNCTION; | |
4668d6f9 | 8046 | if (!abreviated_modproc_decl) |
6de9cd9a | 8047 | target = " function"; |
4668d6f9 PT |
8048 | else |
8049 | target = " procedure"; | |
ddc9ce91 | 8050 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
8051 | break; |
8052 | ||
8053 | case COMP_BLOCK_DATA: | |
8054 | *st = ST_END_BLOCK_DATA; | |
8055 | target = " block data"; | |
ddc9ce91 | 8056 | eos_ok = 1; |
6de9cd9a DN |
8057 | break; |
8058 | ||
8059 | case COMP_MODULE: | |
8060 | *st = ST_END_MODULE; | |
8061 | target = " module"; | |
ddc9ce91 | 8062 | eos_ok = 1; |
6de9cd9a DN |
8063 | break; |
8064 | ||
4668d6f9 PT |
8065 | case COMP_SUBMODULE: |
8066 | *st = ST_END_SUBMODULE; | |
8067 | target = " submodule"; | |
8068 | eos_ok = 1; | |
8069 | break; | |
8070 | ||
6de9cd9a DN |
8071 | case COMP_INTERFACE: |
8072 | *st = ST_END_INTERFACE; | |
8073 | target = " interface"; | |
ddc9ce91 | 8074 | eos_ok = 0; |
6de9cd9a DN |
8075 | break; |
8076 | ||
f6288c24 FR |
8077 | case COMP_MAP: |
8078 | *st = ST_END_MAP; | |
8079 | target = " map"; | |
8080 | eos_ok = 0; | |
8081 | break; | |
8082 | ||
8083 | case COMP_UNION: | |
8084 | *st = ST_END_UNION; | |
8085 | target = " union"; | |
8086 | eos_ok = 0; | |
8087 | break; | |
8088 | ||
8089 | case COMP_STRUCTURE: | |
8090 | *st = ST_END_STRUCTURE; | |
8091 | target = " structure"; | |
8092 | eos_ok = 0; | |
8093 | break; | |
8094 | ||
6de9cd9a | 8095 | case COMP_DERIVED: |
30b608eb | 8096 | case COMP_DERIVED_CONTAINS: |
6de9cd9a DN |
8097 | *st = ST_END_TYPE; |
8098 | target = " type"; | |
ddc9ce91 | 8099 | eos_ok = 0; |
6de9cd9a DN |
8100 | break; |
8101 | ||
03af1e4c DK |
8102 | case COMP_ASSOCIATE: |
8103 | *st = ST_END_ASSOCIATE; | |
8104 | target = " associate"; | |
8105 | eos_ok = 0; | |
8106 | break; | |
8107 | ||
9abe5e56 DK |
8108 | case COMP_BLOCK: |
8109 | *st = ST_END_BLOCK; | |
8110 | target = " block"; | |
8111 | eos_ok = 0; | |
8112 | break; | |
8113 | ||
6de9cd9a DN |
8114 | case COMP_IF: |
8115 | *st = ST_ENDIF; | |
8116 | target = " if"; | |
ddc9ce91 | 8117 | eos_ok = 0; |
6de9cd9a DN |
8118 | break; |
8119 | ||
8120 | case COMP_DO: | |
8c6a85e3 | 8121 | case COMP_DO_CONCURRENT: |
6de9cd9a DN |
8122 | *st = ST_ENDDO; |
8123 | target = " do"; | |
ddc9ce91 | 8124 | eos_ok = 0; |
6de9cd9a DN |
8125 | break; |
8126 | ||
d0a4a61c TB |
8127 | case COMP_CRITICAL: |
8128 | *st = ST_END_CRITICAL; | |
8129 | target = " critical"; | |
8130 | eos_ok = 0; | |
8131 | break; | |
8132 | ||
6de9cd9a | 8133 | case COMP_SELECT: |
cf2b3c22 | 8134 | case COMP_SELECT_TYPE: |
6de9cd9a DN |
8135 | *st = ST_END_SELECT; |
8136 | target = " select"; | |
ddc9ce91 | 8137 | eos_ok = 0; |
6de9cd9a DN |
8138 | break; |
8139 | ||
8140 | case COMP_FORALL: | |
8141 | *st = ST_END_FORALL; | |
8142 | target = " forall"; | |
ddc9ce91 | 8143 | eos_ok = 0; |
6de9cd9a DN |
8144 | break; |
8145 | ||
8146 | case COMP_WHERE: | |
8147 | *st = ST_END_WHERE; | |
8148 | target = " where"; | |
ddc9ce91 | 8149 | eos_ok = 0; |
6de9cd9a DN |
8150 | break; |
8151 | ||
25d8f0a2 TS |
8152 | case COMP_ENUM: |
8153 | *st = ST_END_ENUM; | |
8154 | target = " enum"; | |
8155 | eos_ok = 0; | |
8156 | last_initializer = NULL; | |
8157 | set_enum_kind (); | |
8158 | gfc_free_enum_history (); | |
8159 | break; | |
8160 | ||
6de9cd9a DN |
8161 | default: |
8162 | gfc_error ("Unexpected END statement at %C"); | |
8163 | goto cleanup; | |
8164 | } | |
8165 | ||
3a43b5b3 | 8166 | old_loc = gfc_current_locus; |
6de9cd9a DN |
8167 | if (gfc_match_eos () == MATCH_YES) |
8168 | { | |
272001a2 TB |
8169 | if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION)) |
8170 | { | |
524af0d6 | 8171 | if (!gfc_notify_std (GFC_STD_F2008, "END statement " |
70112e2a | 8172 | "instead of %s statement at %L", |
4668d6f9 PT |
8173 | abreviated_modproc_decl ? "END PROCEDURE" |
8174 | : gfc_ascii_statement(*st), &old_loc)) | |
272001a2 TB |
8175 | goto cleanup; |
8176 | } | |
8177 | else if (!eos_ok) | |
6de9cd9a | 8178 | { |
66e4ab31 | 8179 | /* We would have required END [something]. */ |
59ce85b5 TS |
8180 | gfc_error ("%s statement expected at %L", |
8181 | gfc_ascii_statement (*st), &old_loc); | |
6de9cd9a DN |
8182 | goto cleanup; |
8183 | } | |
8184 | ||
8185 | return MATCH_YES; | |
8186 | } | |
8187 | ||
8188 | /* Verify that we've got the sort of end-block that we're expecting. */ | |
8189 | if (gfc_match (target) != MATCH_YES) | |
8190 | { | |
4668d6f9 PT |
8191 | gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl |
8192 | ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc); | |
6de9cd9a DN |
8193 | goto cleanup; |
8194 | } | |
874108a9 AV |
8195 | else |
8196 | got_matching_end = true; | |
6de9cd9a | 8197 | |
3a43b5b3 | 8198 | old_loc = gfc_current_locus; |
6de9cd9a DN |
8199 | /* If we're at the end, make sure a block name wasn't required. */ |
8200 | if (gfc_match_eos () == MATCH_YES) | |
8201 | { | |
8202 | ||
690af379 | 8203 | if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT |
d0a4a61c | 8204 | && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK |
03af1e4c | 8205 | && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL) |
6de9cd9a DN |
8206 | return MATCH_YES; |
8207 | ||
9abe5e56 | 8208 | if (!block_name) |
6de9cd9a DN |
8209 | return MATCH_YES; |
8210 | ||
c4100eae | 8211 | gfc_error ("Expected block name of %qs in %s statement at %L", |
3a43b5b3 | 8212 | block_name, gfc_ascii_statement (*st), &old_loc); |
6de9cd9a DN |
8213 | |
8214 | return MATCH_ERROR; | |
8215 | } | |
8216 | ||
8217 | /* END INTERFACE has a special handler for its several possible endings. */ | |
8218 | if (*st == ST_END_INTERFACE) | |
8219 | return gfc_match_end_interface (); | |
8220 | ||
66e4ab31 SK |
8221 | /* We haven't hit the end of statement, so what is left must be an |
8222 | end-name. */ | |
6de9cd9a DN |
8223 | m = gfc_match_space (); |
8224 | if (m == MATCH_YES) | |
8225 | m = gfc_match_name (name); | |
8226 | ||
8227 | if (m == MATCH_NO) | |
8228 | gfc_error ("Expected terminating name at %C"); | |
8229 | if (m != MATCH_YES) | |
8230 | goto cleanup; | |
8231 | ||
8232 | if (block_name == NULL) | |
8233 | goto syntax; | |
8234 | ||
3d5dc929 PT |
8235 | /* We have to pick out the declared submodule name from the composite |
8236 | required by F2008:11.2.3 para 2, which ends in the declared name. */ | |
8237 | if (state == COMP_SUBMODULE) | |
8238 | block_name = strchr (block_name, '.') + 1; | |
8239 | ||
3070bab4 | 8240 | if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0) |
6de9cd9a | 8241 | { |
c4100eae | 8242 | gfc_error ("Expected label %qs for %s statement at %C", block_name, |
6de9cd9a DN |
8243 | gfc_ascii_statement (*st)); |
8244 | goto cleanup; | |
8245 | } | |
3070bab4 JW |
8246 | /* Procedure pointer as function result. */ |
8247 | else if (strcmp (block_name, "ppr@") == 0 | |
8248 | && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0) | |
8249 | { | |
c4100eae | 8250 | gfc_error ("Expected label %qs for %s statement at %C", |
3070bab4 JW |
8251 | gfc_current_block ()->ns->proc_name->name, |
8252 | gfc_ascii_statement (*st)); | |
8253 | goto cleanup; | |
8254 | } | |
6de9cd9a DN |
8255 | |
8256 | if (gfc_match_eos () == MATCH_YES) | |
8257 | return MATCH_YES; | |
8258 | ||
8259 | syntax: | |
8260 | gfc_syntax_error (*st); | |
8261 | ||
8262 | cleanup: | |
63645982 | 8263 | gfc_current_locus = old_loc; |
0cab6b73 TK |
8264 | |
8265 | /* If we are missing an END BLOCK, we created a half-ready namespace. | |
8266 | Remove it from the parent namespace's sibling list. */ | |
8267 | ||
874108a9 | 8268 | while (state == COMP_BLOCK && !got_matching_end) |
0cab6b73 TK |
8269 | { |
8270 | parent_ns = gfc_current_ns->parent; | |
8271 | ||
8272 | nsp = &(gfc_state_stack->previous->tail->ext.block.ns); | |
8273 | ||
8274 | prev_ns = NULL; | |
8275 | ns = *nsp; | |
8276 | while (ns) | |
8277 | { | |
8278 | if (ns == gfc_current_ns) | |
8279 | { | |
8280 | if (prev_ns == NULL) | |
8281 | *nsp = NULL; | |
8282 | else | |
8283 | prev_ns->sibling = ns->sibling; | |
8284 | } | |
8285 | prev_ns = ns; | |
8286 | ns = ns->sibling; | |
8287 | } | |
874108a9 | 8288 | |
0cab6b73 TK |
8289 | gfc_free_namespace (gfc_current_ns); |
8290 | gfc_current_ns = parent_ns; | |
9f7ba208 LK |
8291 | gfc_state_stack = gfc_state_stack->previous; |
8292 | state = gfc_current_state (); | |
0cab6b73 TK |
8293 | } |
8294 | ||
6de9cd9a DN |
8295 | return MATCH_ERROR; |
8296 | } | |
8297 | ||
8298 | ||
8299 | ||
8300 | /***************** Attribute declaration statements ****************/ | |
8301 | ||
8302 | /* Set the attribute of a single variable. */ | |
8303 | ||
8304 | static match | |
8305 | attr_decl1 (void) | |
8306 | { | |
8307 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
8308 | gfc_array_spec *as; | |
97440db5 ML |
8309 | |
8310 | /* Workaround -Wmaybe-uninitialized false positive during | |
8311 | profiledbootstrap by initializing them. */ | |
8312 | gfc_symbol *sym = NULL; | |
6de9cd9a DN |
8313 | locus var_locus; |
8314 | match m; | |
8315 | ||
8316 | as = NULL; | |
8317 | ||
8318 | m = gfc_match_name (name); | |
8319 | if (m != MATCH_YES) | |
8320 | goto cleanup; | |
8321 | ||
08a6b8e0 | 8322 | if (find_special (name, &sym, false)) |
6de9cd9a DN |
8323 | return MATCH_ERROR; |
8324 | ||
524af0d6 | 8325 | if (!check_function_name (name)) |
bb9de0c4 JW |
8326 | { |
8327 | m = MATCH_ERROR; | |
8328 | goto cleanup; | |
8329 | } | |
f5acf0f2 | 8330 | |
63645982 | 8331 | var_locus = gfc_current_locus; |
6de9cd9a DN |
8332 | |
8333 | /* Deal with possible array specification for certain attributes. */ | |
8334 | if (current_attr.dimension | |
be59db2d | 8335 | || current_attr.codimension |
6de9cd9a DN |
8336 | || current_attr.allocatable |
8337 | || current_attr.pointer | |
8338 | || current_attr.target) | |
8339 | { | |
be59db2d TB |
8340 | m = gfc_match_array_spec (&as, !current_attr.codimension, |
8341 | !current_attr.dimension | |
8342 | && !current_attr.pointer | |
8343 | && !current_attr.target); | |
6de9cd9a DN |
8344 | if (m == MATCH_ERROR) |
8345 | goto cleanup; | |
8346 | ||
8347 | if (current_attr.dimension && m == MATCH_NO) | |
8348 | { | |
636dff67 SK |
8349 | gfc_error ("Missing array specification at %L in DIMENSION " |
8350 | "statement", &var_locus); | |
6de9cd9a DN |
8351 | m = MATCH_ERROR; |
8352 | goto cleanup; | |
8353 | } | |
8354 | ||
1283ab12 TB |
8355 | if (current_attr.dimension && sym->value) |
8356 | { | |
8357 | gfc_error ("Dimensions specified for %s at %L after its " | |
bd2c6270 | 8358 | "initialization", sym->name, &var_locus); |
1283ab12 TB |
8359 | m = MATCH_ERROR; |
8360 | goto cleanup; | |
8361 | } | |
8362 | ||
be59db2d TB |
8363 | if (current_attr.codimension && m == MATCH_NO) |
8364 | { | |
8365 | gfc_error ("Missing array specification at %L in CODIMENSION " | |
8366 | "statement", &var_locus); | |
8367 | m = MATCH_ERROR; | |
8368 | goto cleanup; | |
8369 | } | |
8370 | ||
6de9cd9a DN |
8371 | if ((current_attr.allocatable || current_attr.pointer) |
8372 | && (m == MATCH_YES) && (as->type != AS_DEFERRED)) | |
8373 | { | |
636dff67 | 8374 | gfc_error ("Array specification must be deferred at %L", &var_locus); |
6de9cd9a DN |
8375 | m = MATCH_ERROR; |
8376 | goto cleanup; | |
8377 | } | |
8378 | } | |
8379 | ||
2e23972e JW |
8380 | /* Update symbol table. DIMENSION attribute is set in |
8381 | gfc_set_array_spec(). For CLASS variables, this must be applied | |
b04533af | 8382 | to the first component, or '_data' field. */ |
d40477b4 | 8383 | if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class) |
6de9cd9a | 8384 | { |
c9935123 SK |
8385 | /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr. Check |
8386 | for duplicate attribute here. */ | |
8387 | if (CLASS_DATA(sym)->attr.dimension == 1 && as) | |
8388 | { | |
8389 | gfc_error ("Duplicate DIMENSION attribute at %C"); | |
8390 | m = MATCH_ERROR; | |
8391 | goto cleanup; | |
8392 | } | |
8393 | ||
524af0d6 | 8394 | if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, ¤t_attr, &var_locus)) |
2e23972e JW |
8395 | { |
8396 | m = MATCH_ERROR; | |
8397 | goto cleanup; | |
8398 | } | |
2e23972e JW |
8399 | } |
8400 | else | |
8401 | { | |
be59db2d | 8402 | if (current_attr.dimension == 0 && current_attr.codimension == 0 |
524af0d6 | 8403 | && !gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus)) |
2e23972e JW |
8404 | { |
8405 | m = MATCH_ERROR; | |
8406 | goto cleanup; | |
8407 | } | |
6de9cd9a | 8408 | } |
f5acf0f2 | 8409 | |
528622fd | 8410 | if (sym->ts.type == BT_CLASS |
9b6da3c7 | 8411 | && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as)) |
96d9b22c JW |
8412 | { |
8413 | m = MATCH_ERROR; | |
8414 | goto cleanup; | |
8415 | } | |
6de9cd9a | 8416 | |
524af0d6 | 8417 | if (!gfc_set_array_spec (sym, as, &var_locus)) |
6de9cd9a DN |
8418 | { |
8419 | m = MATCH_ERROR; | |
8420 | goto cleanup; | |
8421 | } | |
d51347f9 | 8422 | |
83d890b9 AL |
8423 | if (sym->attr.cray_pointee && sym->as != NULL) |
8424 | { | |
8425 | /* Fix the array spec. */ | |
f5acf0f2 | 8426 | m = gfc_mod_pointee_as (sym->as); |
83d890b9 AL |
8427 | if (m == MATCH_ERROR) |
8428 | goto cleanup; | |
8429 | } | |
6de9cd9a | 8430 | |
524af0d6 | 8431 | if (!gfc_add_attribute (&sym->attr, &var_locus)) |
1902704e PT |
8432 | { |
8433 | m = MATCH_ERROR; | |
8434 | goto cleanup; | |
8435 | } | |
8436 | ||
6de9cd9a DN |
8437 | if ((current_attr.external || current_attr.intrinsic) |
8438 | && sym->attr.flavor != FL_PROCEDURE | |
524af0d6 | 8439 | && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL)) |
6de9cd9a DN |
8440 | { |
8441 | m = MATCH_ERROR; | |
8442 | goto cleanup; | |
8443 | } | |
8444 | ||
3070bab4 JW |
8445 | add_hidden_procptr_result (sym); |
8446 | ||
6de9cd9a DN |
8447 | return MATCH_YES; |
8448 | ||
8449 | cleanup: | |
8450 | gfc_free_array_spec (as); | |
8451 | return m; | |
8452 | } | |
8453 | ||
8454 | ||
8455 | /* Generic attribute declaration subroutine. Used for attributes that | |
8456 | just have a list of names. */ | |
8457 | ||
8458 | static match | |
8459 | attr_decl (void) | |
8460 | { | |
8461 | match m; | |
8462 | ||
8463 | /* Gobble the optional double colon, by simply ignoring the result | |
8464 | of gfc_match(). */ | |
8465 | gfc_match (" ::"); | |
8466 | ||
8467 | for (;;) | |
8468 | { | |
8469 | m = attr_decl1 (); | |
8470 | if (m != MATCH_YES) | |
8471 | break; | |
8472 | ||
8473 | if (gfc_match_eos () == MATCH_YES) | |
8474 | { | |
8475 | m = MATCH_YES; | |
8476 | break; | |
8477 | } | |
8478 | ||
8479 | if (gfc_match_char (',') != MATCH_YES) | |
8480 | { | |
8481 | gfc_error ("Unexpected character in variable list at %C"); | |
8482 | m = MATCH_ERROR; | |
8483 | break; | |
8484 | } | |
8485 | } | |
8486 | ||
8487 | return m; | |
8488 | } | |
8489 | ||
8490 | ||
83d890b9 AL |
8491 | /* This routine matches Cray Pointer declarations of the form: |
8492 | pointer ( <pointer>, <pointee> ) | |
8493 | or | |
d51347f9 TB |
8494 | pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ... |
8495 | The pointer, if already declared, should be an integer. Otherwise, we | |
83d890b9 AL |
8496 | set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may |
8497 | be either a scalar, or an array declaration. No space is allocated for | |
d51347f9 | 8498 | the pointee. For the statement |
83d890b9 AL |
8499 | pointer (ipt, ar(10)) |
8500 | any subsequent uses of ar will be translated (in C-notation) as | |
d51347f9 | 8501 | ar(i) => ((<type> *) ipt)(i) |
b122dc6a | 8502 | After gimplification, pointee variable will disappear in the code. */ |
83d890b9 AL |
8503 | |
8504 | static match | |
8505 | cray_pointer_decl (void) | |
8506 | { | |
8507 | match m; | |
be59db2d | 8508 | gfc_array_spec *as = NULL; |
83d890b9 AL |
8509 | gfc_symbol *cptr; /* Pointer symbol. */ |
8510 | gfc_symbol *cpte; /* Pointee symbol. */ | |
8511 | locus var_locus; | |
8512 | bool done = false; | |
8513 | ||
8514 | while (!done) | |
8515 | { | |
8516 | if (gfc_match_char ('(') != MATCH_YES) | |
8517 | { | |
a4d9b221 | 8518 | gfc_error ("Expected %<(%> at %C"); |
d51347f9 | 8519 | return MATCH_ERROR; |
83d890b9 | 8520 | } |
d51347f9 | 8521 | |
83d890b9 AL |
8522 | /* Match pointer. */ |
8523 | var_locus = gfc_current_locus; | |
8524 | gfc_clear_attr (¤t_attr); | |
8525 | gfc_add_cray_pointer (¤t_attr, &var_locus); | |
8526 | current_ts.type = BT_INTEGER; | |
8527 | current_ts.kind = gfc_index_integer_kind; | |
8528 | ||
d51347f9 | 8529 | m = gfc_match_symbol (&cptr, 0); |
83d890b9 AL |
8530 | if (m != MATCH_YES) |
8531 | { | |
8532 | gfc_error ("Expected variable name at %C"); | |
8533 | return m; | |
8534 | } | |
d51347f9 | 8535 | |
524af0d6 | 8536 | if (!gfc_add_cray_pointer (&cptr->attr, &var_locus)) |
83d890b9 AL |
8537 | return MATCH_ERROR; |
8538 | ||
d51347f9 | 8539 | gfc_set_sym_referenced (cptr); |
83d890b9 AL |
8540 | |
8541 | if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */ | |
8542 | { | |
8543 | cptr->ts.type = BT_INTEGER; | |
d51347f9 | 8544 | cptr->ts.kind = gfc_index_integer_kind; |
83d890b9 AL |
8545 | } |
8546 | else if (cptr->ts.type != BT_INTEGER) | |
8547 | { | |
e25a0da3 | 8548 | gfc_error ("Cray pointer at %C must be an integer"); |
83d890b9 AL |
8549 | return MATCH_ERROR; |
8550 | } | |
8551 | else if (cptr->ts.kind < gfc_index_integer_kind) | |
db30e21c | 8552 | gfc_warning (0, "Cray pointer at %C has %d bytes of precision;" |
e25a0da3 | 8553 | " memory addresses require %d bytes", |
636dff67 | 8554 | cptr->ts.kind, gfc_index_integer_kind); |
83d890b9 AL |
8555 | |
8556 | if (gfc_match_char (',') != MATCH_YES) | |
8557 | { | |
8558 | gfc_error ("Expected \",\" at %C"); | |
d51347f9 | 8559 | return MATCH_ERROR; |
83d890b9 AL |
8560 | } |
8561 | ||
d51347f9 | 8562 | /* Match Pointee. */ |
83d890b9 AL |
8563 | var_locus = gfc_current_locus; |
8564 | gfc_clear_attr (¤t_attr); | |
8565 | gfc_add_cray_pointee (¤t_attr, &var_locus); | |
8566 | current_ts.type = BT_UNKNOWN; | |
8567 | current_ts.kind = 0; | |
8568 | ||
8569 | m = gfc_match_symbol (&cpte, 0); | |
8570 | if (m != MATCH_YES) | |
8571 | { | |
8572 | gfc_error ("Expected variable name at %C"); | |
8573 | return m; | |
8574 | } | |
d51347f9 | 8575 | |
83d890b9 | 8576 | /* Check for an optional array spec. */ |
be59db2d | 8577 | m = gfc_match_array_spec (&as, true, false); |
83d890b9 AL |
8578 | if (m == MATCH_ERROR) |
8579 | { | |
8580 | gfc_free_array_spec (as); | |
8581 | return m; | |
8582 | } | |
8583 | else if (m == MATCH_NO) | |
8584 | { | |
8585 | gfc_free_array_spec (as); | |
8586 | as = NULL; | |
f5acf0f2 | 8587 | } |
83d890b9 | 8588 | |
524af0d6 | 8589 | if (!gfc_add_cray_pointee (&cpte->attr, &var_locus)) |
83d890b9 AL |
8590 | return MATCH_ERROR; |
8591 | ||
8592 | gfc_set_sym_referenced (cpte); | |
8593 | ||
8594 | if (cpte->as == NULL) | |
8595 | { | |
524af0d6 | 8596 | if (!gfc_set_array_spec (cpte, as, &var_locus)) |
1fe61adf | 8597 | gfc_internal_error ("Cannot set Cray pointee array spec."); |
83d890b9 AL |
8598 | } |
8599 | else if (as != NULL) | |
8600 | { | |
e25a0da3 | 8601 | gfc_error ("Duplicate array spec for Cray pointee at %C"); |
83d890b9 AL |
8602 | gfc_free_array_spec (as); |
8603 | return MATCH_ERROR; | |
8604 | } | |
f5acf0f2 | 8605 | |
83d890b9 | 8606 | as = NULL; |
f5acf0f2 | 8607 | |
83d890b9 AL |
8608 | if (cpte->as != NULL) |
8609 | { | |
8610 | /* Fix array spec. */ | |
8611 | m = gfc_mod_pointee_as (cpte->as); | |
8612 | if (m == MATCH_ERROR) | |
8613 | return m; | |
f5acf0f2 PT |
8614 | } |
8615 | ||
83d890b9 | 8616 | /* Point the Pointee at the Pointer. */ |
b122dc6a | 8617 | cpte->cp_pointer = cptr; |
83d890b9 AL |
8618 | |
8619 | if (gfc_match_char (')') != MATCH_YES) | |
8620 | { | |
8621 | gfc_error ("Expected \")\" at %C"); | |
f5acf0f2 | 8622 | return MATCH_ERROR; |
83d890b9 AL |
8623 | } |
8624 | m = gfc_match_char (','); | |
8625 | if (m != MATCH_YES) | |
8626 | done = true; /* Stop searching for more declarations. */ | |
8627 | ||
8628 | } | |
f5acf0f2 | 8629 | |
83d890b9 AL |
8630 | if (m == MATCH_ERROR /* Failed when trying to find ',' above. */ |
8631 | || gfc_match_eos () != MATCH_YES) | |
8632 | { | |
a4d9b221 | 8633 | gfc_error ("Expected %<,%> or end of statement at %C"); |
83d890b9 AL |
8634 | return MATCH_ERROR; |
8635 | } | |
8636 | return MATCH_YES; | |
8637 | } | |
8638 | ||
8639 | ||
6de9cd9a DN |
8640 | match |
8641 | gfc_match_external (void) | |
8642 | { | |
8643 | ||
8644 | gfc_clear_attr (¤t_attr); | |
1902704e | 8645 | current_attr.external = 1; |
6de9cd9a DN |
8646 | |
8647 | return attr_decl (); | |
8648 | } | |
8649 | ||
8650 | ||
6de9cd9a DN |
8651 | match |
8652 | gfc_match_intent (void) | |
8653 | { | |
8654 | sym_intent intent; | |
8655 | ||
9abe5e56 DK |
8656 | /* This is not allowed within a BLOCK construct! */ |
8657 | if (gfc_current_state () == COMP_BLOCK) | |
8658 | { | |
8659 | gfc_error ("INTENT is not allowed inside of BLOCK at %C"); | |
8660 | return MATCH_ERROR; | |
8661 | } | |
8662 | ||
6de9cd9a DN |
8663 | intent = match_intent_spec (); |
8664 | if (intent == INTENT_UNKNOWN) | |
8665 | return MATCH_ERROR; | |
8666 | ||
8667 | gfc_clear_attr (¤t_attr); | |
1902704e | 8668 | current_attr.intent = intent; |
6de9cd9a DN |
8669 | |
8670 | return attr_decl (); | |
8671 | } | |
8672 | ||
8673 | ||
8674 | match | |
8675 | gfc_match_intrinsic (void) | |
8676 | { | |
8677 | ||
8678 | gfc_clear_attr (¤t_attr); | |
1902704e | 8679 | current_attr.intrinsic = 1; |
6de9cd9a DN |
8680 | |
8681 | return attr_decl (); | |
8682 | } | |
8683 | ||
8684 | ||
8685 | match | |
8686 | gfc_match_optional (void) | |
8687 | { | |
9abe5e56 DK |
8688 | /* This is not allowed within a BLOCK construct! */ |
8689 | if (gfc_current_state () == COMP_BLOCK) | |
8690 | { | |
8691 | gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C"); | |
8692 | return MATCH_ERROR; | |
8693 | } | |
6de9cd9a DN |
8694 | |
8695 | gfc_clear_attr (¤t_attr); | |
1902704e | 8696 | current_attr.optional = 1; |
6de9cd9a DN |
8697 | |
8698 | return attr_decl (); | |
8699 | } | |
8700 | ||
8701 | ||
8702 | match | |
8703 | gfc_match_pointer (void) | |
8704 | { | |
83d890b9 | 8705 | gfc_gobble_whitespace (); |
8fc541d3 | 8706 | if (gfc_peek_ascii_char () == '(') |
83d890b9 | 8707 | { |
c61819ff | 8708 | if (!flag_cray_pointer) |
83d890b9 | 8709 | { |
a3f9f006 ML |
8710 | gfc_error ("Cray pointer declaration at %C requires " |
8711 | "%<-fcray-pointer%> flag"); | |
83d890b9 AL |
8712 | return MATCH_ERROR; |
8713 | } | |
8714 | return cray_pointer_decl (); | |
8715 | } | |
8716 | else | |
8717 | { | |
8718 | gfc_clear_attr (¤t_attr); | |
1902704e | 8719 | current_attr.pointer = 1; |
f5acf0f2 | 8720 | |
83d890b9 AL |
8721 | return attr_decl (); |
8722 | } | |
6de9cd9a DN |
8723 | } |
8724 | ||
8725 | ||
8726 | match | |
8727 | gfc_match_allocatable (void) | |
8728 | { | |
6de9cd9a | 8729 | gfc_clear_attr (¤t_attr); |
1902704e | 8730 | current_attr.allocatable = 1; |
6de9cd9a DN |
8731 | |
8732 | return attr_decl (); | |
8733 | } | |
8734 | ||
8735 | ||
be59db2d TB |
8736 | match |
8737 | gfc_match_codimension (void) | |
8738 | { | |
8739 | gfc_clear_attr (¤t_attr); | |
8740 | current_attr.codimension = 1; | |
8741 | ||
8742 | return attr_decl (); | |
8743 | } | |
8744 | ||
8745 | ||
fe4e525c TB |
8746 | match |
8747 | gfc_match_contiguous (void) | |
8748 | { | |
524af0d6 | 8749 | if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C")) |
fe4e525c TB |
8750 | return MATCH_ERROR; |
8751 | ||
8752 | gfc_clear_attr (¤t_attr); | |
8753 | current_attr.contiguous = 1; | |
8754 | ||
8755 | return attr_decl (); | |
8756 | } | |
8757 | ||
8758 | ||
6de9cd9a DN |
8759 | match |
8760 | gfc_match_dimension (void) | |
8761 | { | |
6de9cd9a | 8762 | gfc_clear_attr (¤t_attr); |
1902704e | 8763 | current_attr.dimension = 1; |
6de9cd9a DN |
8764 | |
8765 | return attr_decl (); | |
8766 | } | |
8767 | ||
8768 | ||
8769 | match | |
8770 | gfc_match_target (void) | |
8771 | { | |
6de9cd9a | 8772 | gfc_clear_attr (¤t_attr); |
1902704e | 8773 | current_attr.target = 1; |
6de9cd9a DN |
8774 | |
8775 | return attr_decl (); | |
8776 | } | |
8777 | ||
8778 | ||
8779 | /* Match the list of entities being specified in a PUBLIC or PRIVATE | |
8780 | statement. */ | |
8781 | ||
8782 | static match | |
8783 | access_attr_decl (gfc_statement st) | |
8784 | { | |
8785 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
8786 | interface_type type; | |
8787 | gfc_user_op *uop; | |
c3f34952 | 8788 | gfc_symbol *sym, *dt_sym; |
a1ee985f | 8789 | gfc_intrinsic_op op; |
6de9cd9a DN |
8790 | match m; |
8791 | ||
8792 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
8793 | goto done; | |
8794 | ||
8795 | for (;;) | |
8796 | { | |
a1ee985f | 8797 | m = gfc_match_generic_spec (&type, name, &op); |
6de9cd9a DN |
8798 | if (m == MATCH_NO) |
8799 | goto syntax; | |
8800 | if (m == MATCH_ERROR) | |
8801 | return MATCH_ERROR; | |
8802 | ||
8803 | switch (type) | |
8804 | { | |
8805 | case INTERFACE_NAMELESS: | |
9e1d712c | 8806 | case INTERFACE_ABSTRACT: |
6de9cd9a DN |
8807 | goto syntax; |
8808 | ||
8809 | case INTERFACE_GENERIC: | |
e73d3ca6 | 8810 | case INTERFACE_DTIO: |
dc42a736 | 8811 | |
6de9cd9a DN |
8812 | if (gfc_get_symbol (name, NULL, &sym)) |
8813 | goto done; | |
8814 | ||
41036686 PT |
8815 | if (type == INTERFACE_DTIO |
8816 | && gfc_current_ns->proc_name | |
8817 | && gfc_current_ns->proc_name->attr.flavor == FL_MODULE | |
8818 | && sym->attr.flavor == FL_UNKNOWN) | |
8819 | sym->attr.flavor = FL_PROCEDURE; | |
8820 | ||
70112e2a PT |
8821 | if (!gfc_add_access (&sym->attr, |
8822 | (st == ST_PUBLIC) | |
8823 | ? ACCESS_PUBLIC : ACCESS_PRIVATE, | |
524af0d6 | 8824 | sym->name, NULL)) |
6de9cd9a DN |
8825 | return MATCH_ERROR; |
8826 | ||
c3f34952 | 8827 | if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym)) |
70112e2a PT |
8828 | && !gfc_add_access (&dt_sym->attr, |
8829 | (st == ST_PUBLIC) | |
8830 | ? ACCESS_PUBLIC : ACCESS_PRIVATE, | |
524af0d6 | 8831 | sym->name, NULL)) |
c3f34952 TB |
8832 | return MATCH_ERROR; |
8833 | ||
6de9cd9a DN |
8834 | break; |
8835 | ||
8836 | case INTERFACE_INTRINSIC_OP: | |
a1ee985f | 8837 | if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN) |
6de9cd9a | 8838 | { |
fb03a37e TK |
8839 | gfc_intrinsic_op other_op; |
8840 | ||
a1ee985f | 8841 | gfc_current_ns->operator_access[op] = |
6de9cd9a | 8842 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; |
fb03a37e TK |
8843 | |
8844 | /* Handle the case if there is another op with the same | |
8845 | function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */ | |
8846 | other_op = gfc_equivalent_op (op); | |
8847 | ||
8848 | if (other_op != INTRINSIC_NONE) | |
8849 | gfc_current_ns->operator_access[other_op] = | |
8850 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
8851 | ||
6de9cd9a DN |
8852 | } |
8853 | else | |
8854 | { | |
8855 | gfc_error ("Access specification of the %s operator at %C has " | |
a1ee985f | 8856 | "already been specified", gfc_op2string (op)); |
6de9cd9a DN |
8857 | goto done; |
8858 | } | |
8859 | ||
8860 | break; | |
8861 | ||
8862 | case INTERFACE_USER_OP: | |
8863 | uop = gfc_get_uop (name); | |
8864 | ||
8865 | if (uop->access == ACCESS_UNKNOWN) | |
8866 | { | |
636dff67 SK |
8867 | uop->access = (st == ST_PUBLIC) |
8868 | ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
6de9cd9a DN |
8869 | } |
8870 | else | |
8871 | { | |
636dff67 SK |
8872 | gfc_error ("Access specification of the .%s. operator at %C " |
8873 | "has already been specified", sym->name); | |
6de9cd9a DN |
8874 | goto done; |
8875 | } | |
8876 | ||
8877 | break; | |
8878 | } | |
8879 | ||
8880 | if (gfc_match_char (',') == MATCH_NO) | |
8881 | break; | |
8882 | } | |
8883 | ||
8884 | if (gfc_match_eos () != MATCH_YES) | |
8885 | goto syntax; | |
8886 | return MATCH_YES; | |
8887 | ||
8888 | syntax: | |
8889 | gfc_syntax_error (st); | |
8890 | ||
8891 | done: | |
8892 | return MATCH_ERROR; | |
8893 | } | |
8894 | ||
8895 | ||
ee7e677f TB |
8896 | match |
8897 | gfc_match_protected (void) | |
8898 | { | |
8899 | gfc_symbol *sym; | |
8900 | match m; | |
8901 | ||
73641c88 SK |
8902 | if (!gfc_current_ns->proc_name |
8903 | || gfc_current_ns->proc_name->attr.flavor != FL_MODULE) | |
ee7e677f TB |
8904 | { |
8905 | gfc_error ("PROTECTED at %C only allowed in specification " | |
8906 | "part of a module"); | |
8907 | return MATCH_ERROR; | |
8908 | ||
8909 | } | |
8910 | ||
524af0d6 | 8911 | if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C")) |
ee7e677f TB |
8912 | return MATCH_ERROR; |
8913 | ||
8914 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
8915 | { | |
8916 | return MATCH_ERROR; | |
8917 | } | |
8918 | ||
8919 | if (gfc_match_eos () == MATCH_YES) | |
8920 | goto syntax; | |
8921 | ||
8922 | for(;;) | |
8923 | { | |
8924 | m = gfc_match_symbol (&sym, 0); | |
8925 | switch (m) | |
8926 | { | |
8927 | case MATCH_YES: | |
524af0d6 | 8928 | if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)) |
ee7e677f TB |
8929 | return MATCH_ERROR; |
8930 | goto next_item; | |
8931 | ||
8932 | case MATCH_NO: | |
8933 | break; | |
8934 | ||
8935 | case MATCH_ERROR: | |
8936 | return MATCH_ERROR; | |
8937 | } | |
8938 | ||
8939 | next_item: | |
8940 | if (gfc_match_eos () == MATCH_YES) | |
8941 | break; | |
8942 | if (gfc_match_char (',') != MATCH_YES) | |
8943 | goto syntax; | |
8944 | } | |
8945 | ||
8946 | return MATCH_YES; | |
8947 | ||
8948 | syntax: | |
8949 | gfc_error ("Syntax error in PROTECTED statement at %C"); | |
8950 | return MATCH_ERROR; | |
8951 | } | |
8952 | ||
8953 | ||
86bf520d | 8954 | /* The PRIVATE statement is a bit weird in that it can be an attribute |
df2fba9e | 8955 | declaration, but also works as a standalone statement inside of a |
6de9cd9a DN |
8956 | type declaration or a module. */ |
8957 | ||
8958 | match | |
636dff67 | 8959 | gfc_match_private (gfc_statement *st) |
6de9cd9a DN |
8960 | { |
8961 | ||
8962 | if (gfc_match ("private") != MATCH_YES) | |
8963 | return MATCH_NO; | |
8964 | ||
d51347f9 | 8965 | if (gfc_current_state () != COMP_MODULE |
30b608eb DK |
8966 | && !(gfc_current_state () == COMP_DERIVED |
8967 | && gfc_state_stack->previous | |
8968 | && gfc_state_stack->previous->state == COMP_MODULE) | |
8969 | && !(gfc_current_state () == COMP_DERIVED_CONTAINS | |
8970 | && gfc_state_stack->previous && gfc_state_stack->previous->previous | |
8971 | && gfc_state_stack->previous->previous->state == COMP_MODULE)) | |
d51347f9 TB |
8972 | { |
8973 | gfc_error ("PRIVATE statement at %C is only allowed in the " | |
8974 | "specification part of a module"); | |
8975 | return MATCH_ERROR; | |
8976 | } | |
8977 | ||
6de9cd9a DN |
8978 | if (gfc_current_state () == COMP_DERIVED) |
8979 | { | |
8980 | if (gfc_match_eos () == MATCH_YES) | |
8981 | { | |
8982 | *st = ST_PRIVATE; | |
8983 | return MATCH_YES; | |
8984 | } | |
8985 | ||
8986 | gfc_syntax_error (ST_PRIVATE); | |
8987 | return MATCH_ERROR; | |
8988 | } | |
8989 | ||
8990 | if (gfc_match_eos () == MATCH_YES) | |
8991 | { | |
8992 | *st = ST_PRIVATE; | |
8993 | return MATCH_YES; | |
8994 | } | |
8995 | ||
8996 | *st = ST_ATTR_DECL; | |
8997 | return access_attr_decl (ST_PRIVATE); | |
8998 | } | |
8999 | ||
9000 | ||
9001 | match | |
636dff67 | 9002 | gfc_match_public (gfc_statement *st) |
6de9cd9a DN |
9003 | { |
9004 | ||
9005 | if (gfc_match ("public") != MATCH_YES) | |
9006 | return MATCH_NO; | |
9007 | ||
d51347f9 TB |
9008 | if (gfc_current_state () != COMP_MODULE) |
9009 | { | |
9010 | gfc_error ("PUBLIC statement at %C is only allowed in the " | |
9011 | "specification part of a module"); | |
9012 | return MATCH_ERROR; | |
9013 | } | |
9014 | ||
6de9cd9a DN |
9015 | if (gfc_match_eos () == MATCH_YES) |
9016 | { | |
9017 | *st = ST_PUBLIC; | |
9018 | return MATCH_YES; | |
9019 | } | |
9020 | ||
9021 | *st = ST_ATTR_DECL; | |
9022 | return access_attr_decl (ST_PUBLIC); | |
9023 | } | |
9024 | ||
9025 | ||
9026 | /* Workhorse for gfc_match_parameter. */ | |
9027 | ||
9028 | static match | |
9029 | do_parm (void) | |
9030 | { | |
9031 | gfc_symbol *sym; | |
9032 | gfc_expr *init; | |
9033 | match m; | |
524af0d6 | 9034 | bool t; |
6de9cd9a DN |
9035 | |
9036 | m = gfc_match_symbol (&sym, 0); | |
9037 | if (m == MATCH_NO) | |
9038 | gfc_error ("Expected variable name at %C in PARAMETER statement"); | |
9039 | ||
9040 | if (m != MATCH_YES) | |
9041 | return m; | |
9042 | ||
9043 | if (gfc_match_char ('=') == MATCH_NO) | |
9044 | { | |
9045 | gfc_error ("Expected = sign in PARAMETER statement at %C"); | |
9046 | return MATCH_ERROR; | |
9047 | } | |
9048 | ||
9049 | m = gfc_match_init_expr (&init); | |
9050 | if (m == MATCH_NO) | |
9051 | gfc_error ("Expected expression at %C in PARAMETER statement"); | |
9052 | if (m != MATCH_YES) | |
9053 | return m; | |
9054 | ||
9055 | if (sym->ts.type == BT_UNKNOWN | |
524af0d6 | 9056 | && !gfc_set_default_type (sym, 1, NULL)) |
6de9cd9a DN |
9057 | { |
9058 | m = MATCH_ERROR; | |
9059 | goto cleanup; | |
9060 | } | |
9061 | ||
524af0d6 JB |
9062 | if (!gfc_check_assign_symbol (sym, NULL, init) |
9063 | || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL)) | |
6de9cd9a DN |
9064 | { |
9065 | m = MATCH_ERROR; | |
9066 | goto cleanup; | |
9067 | } | |
9068 | ||
1283ab12 TB |
9069 | if (sym->value) |
9070 | { | |
9071 | gfc_error ("Initializing already initialized variable at %C"); | |
9072 | m = MATCH_ERROR; | |
9073 | goto cleanup; | |
9074 | } | |
9075 | ||
7919373d | 9076 | t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus); |
524af0d6 | 9077 | return (t) ? MATCH_YES : MATCH_ERROR; |
6de9cd9a DN |
9078 | |
9079 | cleanup: | |
9080 | gfc_free_expr (init); | |
9081 | return m; | |
9082 | } | |
9083 | ||
9084 | ||
9085 | /* Match a parameter statement, with the weird syntax that these have. */ | |
9086 | ||
9087 | match | |
9088 | gfc_match_parameter (void) | |
9089 | { | |
35ea947f | 9090 | const char *term = " )%t"; |
6de9cd9a DN |
9091 | match m; |
9092 | ||
9093 | if (gfc_match_char ('(') == MATCH_NO) | |
35ea947f FR |
9094 | { |
9095 | /* With legacy PARAMETER statements, don't expect a terminating ')'. */ | |
9096 | if (!gfc_notify_std (GFC_STD_LEGACY, "PARAMETER without '()' at %C")) | |
9097 | return MATCH_NO; | |
9098 | term = " %t"; | |
9099 | } | |
6de9cd9a DN |
9100 | |
9101 | for (;;) | |
9102 | { | |
9103 | m = do_parm (); | |
9104 | if (m != MATCH_YES) | |
9105 | break; | |
9106 | ||
35ea947f | 9107 | if (gfc_match (term) == MATCH_YES) |
6de9cd9a DN |
9108 | break; |
9109 | ||
9110 | if (gfc_match_char (',') != MATCH_YES) | |
9111 | { | |
9112 | gfc_error ("Unexpected characters in PARAMETER statement at %C"); | |
9113 | m = MATCH_ERROR; | |
9114 | break; | |
9115 | } | |
9116 | } | |
9117 | ||
9118 | return m; | |
9119 | } | |
9120 | ||
9121 | ||
34d567d1 FR |
9122 | match |
9123 | gfc_match_automatic (void) | |
9124 | { | |
9125 | gfc_symbol *sym; | |
9126 | match m; | |
9127 | bool seen_symbol = false; | |
9128 | ||
9129 | if (!flag_dec_static) | |
9130 | { | |
cf004230 FR |
9131 | gfc_error ("%s at %C is a DEC extension, enable with " |
9132 | "%<-fdec-static%>", | |
9133 | "AUTOMATIC" | |
9134 | ); | |
34d567d1 FR |
9135 | return MATCH_ERROR; |
9136 | } | |
9137 | ||
9138 | gfc_match (" ::"); | |
9139 | ||
9140 | for (;;) | |
9141 | { | |
9142 | m = gfc_match_symbol (&sym, 0); | |
9143 | switch (m) | |
9144 | { | |
9145 | case MATCH_NO: | |
9146 | break; | |
9147 | ||
9148 | case MATCH_ERROR: | |
9149 | return MATCH_ERROR; | |
9150 | ||
9151 | case MATCH_YES: | |
9152 | if (!gfc_add_automatic (&sym->attr, sym->name, &gfc_current_locus)) | |
9153 | return MATCH_ERROR; | |
9154 | seen_symbol = true; | |
9155 | break; | |
9156 | } | |
9157 | ||
9158 | if (gfc_match_eos () == MATCH_YES) | |
9159 | break; | |
9160 | if (gfc_match_char (',') != MATCH_YES) | |
9161 | goto syntax; | |
9162 | } | |
9163 | ||
9164 | if (!seen_symbol) | |
9165 | { | |
9166 | gfc_error ("Expected entity-list in AUTOMATIC statement at %C"); | |
9167 | return MATCH_ERROR; | |
9168 | } | |
9169 | ||
9170 | return MATCH_YES; | |
9171 | ||
9172 | syntax: | |
9173 | gfc_error ("Syntax error in AUTOMATIC statement at %C"); | |
9174 | return MATCH_ERROR; | |
9175 | } | |
9176 | ||
9177 | ||
9178 | match | |
9179 | gfc_match_static (void) | |
9180 | { | |
9181 | gfc_symbol *sym; | |
9182 | match m; | |
9183 | bool seen_symbol = false; | |
9184 | ||
9185 | if (!flag_dec_static) | |
9186 | { | |
cf004230 FR |
9187 | gfc_error ("%s at %C is a DEC extension, enable with " |
9188 | "%<-fdec-static%>", | |
9189 | "STATIC"); | |
34d567d1 FR |
9190 | return MATCH_ERROR; |
9191 | } | |
9192 | ||
9193 | gfc_match (" ::"); | |
9194 | ||
9195 | for (;;) | |
9196 | { | |
9197 | m = gfc_match_symbol (&sym, 0); | |
9198 | switch (m) | |
9199 | { | |
9200 | case MATCH_NO: | |
9201 | break; | |
9202 | ||
9203 | case MATCH_ERROR: | |
9204 | return MATCH_ERROR; | |
9205 | ||
9206 | case MATCH_YES: | |
9207 | if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, | |
9208 | &gfc_current_locus)) | |
9209 | return MATCH_ERROR; | |
9210 | seen_symbol = true; | |
9211 | break; | |
9212 | } | |
9213 | ||
9214 | if (gfc_match_eos () == MATCH_YES) | |
9215 | break; | |
9216 | if (gfc_match_char (',') != MATCH_YES) | |
9217 | goto syntax; | |
9218 | } | |
9219 | ||
9220 | if (!seen_symbol) | |
9221 | { | |
9222 | gfc_error ("Expected entity-list in STATIC statement at %C"); | |
9223 | return MATCH_ERROR; | |
9224 | } | |
9225 | ||
9226 | return MATCH_YES; | |
9227 | ||
9228 | syntax: | |
9229 | gfc_error ("Syntax error in STATIC statement at %C"); | |
9230 | return MATCH_ERROR; | |
9231 | } | |
9232 | ||
9233 | ||
6de9cd9a DN |
9234 | /* Save statements have a special syntax. */ |
9235 | ||
9236 | match | |
9237 | gfc_match_save (void) | |
9238 | { | |
9056bd70 TS |
9239 | char n[GFC_MAX_SYMBOL_LEN+1]; |
9240 | gfc_common_head *c; | |
6de9cd9a DN |
9241 | gfc_symbol *sym; |
9242 | match m; | |
9243 | ||
9244 | if (gfc_match_eos () == MATCH_YES) | |
9245 | { | |
9246 | if (gfc_current_ns->seen_save) | |
9247 | { | |
524af0d6 JB |
9248 | if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C " |
9249 | "follows previous SAVE statement")) | |
09e87839 | 9250 | return MATCH_ERROR; |
6de9cd9a DN |
9251 | } |
9252 | ||
9253 | gfc_current_ns->save_all = gfc_current_ns->seen_save = 1; | |
9254 | return MATCH_YES; | |
9255 | } | |
9256 | ||
9257 | if (gfc_current_ns->save_all) | |
9258 | { | |
524af0d6 JB |
9259 | if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows " |
9260 | "blanket SAVE statement")) | |
09e87839 | 9261 | return MATCH_ERROR; |
6de9cd9a DN |
9262 | } |
9263 | ||
9264 | gfc_match (" ::"); | |
9265 | ||
9266 | for (;;) | |
9267 | { | |
9268 | m = gfc_match_symbol (&sym, 0); | |
9269 | switch (m) | |
9270 | { | |
9271 | case MATCH_YES: | |
70112e2a | 9272 | if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, |
524af0d6 | 9273 | &gfc_current_locus)) |
6de9cd9a DN |
9274 | return MATCH_ERROR; |
9275 | goto next_item; | |
9276 | ||
9277 | case MATCH_NO: | |
9278 | break; | |
9279 | ||
9280 | case MATCH_ERROR: | |
9281 | return MATCH_ERROR; | |
9282 | } | |
9283 | ||
9056bd70 | 9284 | m = gfc_match (" / %n /", &n); |
6de9cd9a DN |
9285 | if (m == MATCH_ERROR) |
9286 | return MATCH_ERROR; | |
9287 | if (m == MATCH_NO) | |
9288 | goto syntax; | |
9289 | ||
53814b8f | 9290 | c = gfc_get_common (n, 0); |
9056bd70 TS |
9291 | c->saved = 1; |
9292 | ||
6de9cd9a DN |
9293 | gfc_current_ns->seen_save = 1; |
9294 | ||
9295 | next_item: | |
9296 | if (gfc_match_eos () == MATCH_YES) | |
9297 | break; | |
9298 | if (gfc_match_char (',') != MATCH_YES) | |
9299 | goto syntax; | |
9300 | } | |
9301 | ||
9302 | return MATCH_YES; | |
9303 | ||
9304 | syntax: | |
9305 | gfc_error ("Syntax error in SAVE statement at %C"); | |
9306 | return MATCH_ERROR; | |
9307 | } | |
9308 | ||
9309 | ||
06469efd PT |
9310 | match |
9311 | gfc_match_value (void) | |
9312 | { | |
9313 | gfc_symbol *sym; | |
9314 | match m; | |
9315 | ||
9abe5e56 DK |
9316 | /* This is not allowed within a BLOCK construct! */ |
9317 | if (gfc_current_state () == COMP_BLOCK) | |
9318 | { | |
9319 | gfc_error ("VALUE is not allowed inside of BLOCK at %C"); | |
9320 | return MATCH_ERROR; | |
9321 | } | |
9322 | ||
524af0d6 | 9323 | if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C")) |
06469efd PT |
9324 | return MATCH_ERROR; |
9325 | ||
9326 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
9327 | { | |
9328 | return MATCH_ERROR; | |
9329 | } | |
9330 | ||
9331 | if (gfc_match_eos () == MATCH_YES) | |
9332 | goto syntax; | |
9333 | ||
9334 | for(;;) | |
9335 | { | |
9336 | m = gfc_match_symbol (&sym, 0); | |
9337 | switch (m) | |
9338 | { | |
9339 | case MATCH_YES: | |
524af0d6 | 9340 | if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)) |
06469efd PT |
9341 | return MATCH_ERROR; |
9342 | goto next_item; | |
9343 | ||
9344 | case MATCH_NO: | |
9345 | break; | |
9346 | ||
9347 | case MATCH_ERROR: | |
9348 | return MATCH_ERROR; | |
9349 | } | |
9350 | ||
9351 | next_item: | |
9352 | if (gfc_match_eos () == MATCH_YES) | |
9353 | break; | |
9354 | if (gfc_match_char (',') != MATCH_YES) | |
9355 | goto syntax; | |
9356 | } | |
9357 | ||
9358 | return MATCH_YES; | |
9359 | ||
9360 | syntax: | |
9361 | gfc_error ("Syntax error in VALUE statement at %C"); | |
9362 | return MATCH_ERROR; | |
9363 | } | |
9364 | ||
66e4ab31 | 9365 | |
775e6c3a TB |
9366 | match |
9367 | gfc_match_volatile (void) | |
9368 | { | |
9369 | gfc_symbol *sym; | |
ba77f7ba | 9370 | char *name; |
775e6c3a TB |
9371 | match m; |
9372 | ||
524af0d6 | 9373 | if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C")) |
775e6c3a TB |
9374 | return MATCH_ERROR; |
9375 | ||
9376 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
9377 | { | |
9378 | return MATCH_ERROR; | |
9379 | } | |
9380 | ||
9381 | if (gfc_match_eos () == MATCH_YES) | |
9382 | goto syntax; | |
9383 | ||
9384 | for(;;) | |
9385 | { | |
f5acf0f2 | 9386 | /* VOLATILE is special because it can be added to host-associated |
1cc0e193 | 9387 | symbols locally. Except for coarrays. */ |
9bce3c1c | 9388 | m = gfc_match_symbol (&sym, 1); |
775e6c3a TB |
9389 | switch (m) |
9390 | { | |
9391 | case MATCH_YES: | |
ba77f7ba SK |
9392 | name = XCNEWVAR (char, strlen (sym->name) + 1); |
9393 | strcpy (name, sym->name); | |
9394 | if (!check_function_name (name)) | |
9395 | return MATCH_ERROR; | |
be59db2d TB |
9396 | /* F2008, C560+C561. VOLATILE for host-/use-associated variable or |
9397 | for variable in a BLOCK which is defined outside of the BLOCK. */ | |
9398 | if (sym->ns != gfc_current_ns && sym->attr.codimension) | |
9399 | { | |
c4100eae | 9400 | gfc_error ("Specifying VOLATILE for coarray variable %qs at " |
be59db2d TB |
9401 | "%C, which is use-/host-associated", sym->name); |
9402 | return MATCH_ERROR; | |
9403 | } | |
524af0d6 | 9404 | if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)) |
775e6c3a TB |
9405 | return MATCH_ERROR; |
9406 | goto next_item; | |
9407 | ||
9408 | case MATCH_NO: | |
9409 | break; | |
9410 | ||
9411 | case MATCH_ERROR: | |
9412 | return MATCH_ERROR; | |
9413 | } | |
9414 | ||
9415 | next_item: | |
9416 | if (gfc_match_eos () == MATCH_YES) | |
9417 | break; | |
9418 | if (gfc_match_char (',') != MATCH_YES) | |
9419 | goto syntax; | |
9420 | } | |
9421 | ||
9422 | return MATCH_YES; | |
9423 | ||
9424 | syntax: | |
9425 | gfc_error ("Syntax error in VOLATILE statement at %C"); | |
9426 | return MATCH_ERROR; | |
9427 | } | |
9428 | ||
9429 | ||
1eee5628 TB |
9430 | match |
9431 | gfc_match_asynchronous (void) | |
9432 | { | |
9433 | gfc_symbol *sym; | |
ba77f7ba | 9434 | char *name; |
1eee5628 TB |
9435 | match m; |
9436 | ||
524af0d6 | 9437 | if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C")) |
1eee5628 TB |
9438 | return MATCH_ERROR; |
9439 | ||
9440 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
9441 | { | |
9442 | return MATCH_ERROR; | |
9443 | } | |
9444 | ||
9445 | if (gfc_match_eos () == MATCH_YES) | |
9446 | goto syntax; | |
9447 | ||
9448 | for(;;) | |
9449 | { | |
f5acf0f2 | 9450 | /* ASYNCHRONOUS is special because it can be added to host-associated |
1eee5628 TB |
9451 | symbols locally. */ |
9452 | m = gfc_match_symbol (&sym, 1); | |
9453 | switch (m) | |
9454 | { | |
9455 | case MATCH_YES: | |
ba77f7ba SK |
9456 | name = XCNEWVAR (char, strlen (sym->name) + 1); |
9457 | strcpy (name, sym->name); | |
9458 | if (!check_function_name (name)) | |
9459 | return MATCH_ERROR; | |
524af0d6 | 9460 | if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)) |
1eee5628 TB |
9461 | return MATCH_ERROR; |
9462 | goto next_item; | |
9463 | ||
9464 | case MATCH_NO: | |
9465 | break; | |
9466 | ||
9467 | case MATCH_ERROR: | |
9468 | return MATCH_ERROR; | |
9469 | } | |
9470 | ||
9471 | next_item: | |
9472 | if (gfc_match_eos () == MATCH_YES) | |
9473 | break; | |
9474 | if (gfc_match_char (',') != MATCH_YES) | |
9475 | goto syntax; | |
9476 | } | |
9477 | ||
9478 | return MATCH_YES; | |
9479 | ||
9480 | syntax: | |
9481 | gfc_error ("Syntax error in ASYNCHRONOUS statement at %C"); | |
9482 | return MATCH_ERROR; | |
9483 | } | |
9484 | ||
9485 | ||
4668d6f9 PT |
9486 | /* Match a module procedure statement in a submodule. */ |
9487 | ||
9488 | match | |
9489 | gfc_match_submod_proc (void) | |
9490 | { | |
9491 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
9492 | gfc_symbol *sym, *fsym; | |
9493 | match m; | |
9494 | gfc_formal_arglist *formal, *head, *tail; | |
9495 | ||
9496 | if (gfc_current_state () != COMP_CONTAINS | |
9497 | || !(gfc_state_stack->previous | |
70112e2a PT |
9498 | && (gfc_state_stack->previous->state == COMP_SUBMODULE |
9499 | || gfc_state_stack->previous->state == COMP_MODULE))) | |
4668d6f9 PT |
9500 | return MATCH_NO; |
9501 | ||
9502 | m = gfc_match (" module% procedure% %n", name); | |
9503 | if (m != MATCH_YES) | |
9504 | return m; | |
9505 | ||
9506 | if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration " | |
9507 | "at %C")) | |
9508 | return MATCH_ERROR; | |
9509 | ||
9510 | if (get_proc_name (name, &sym, false)) | |
9511 | return MATCH_ERROR; | |
9512 | ||
9513 | /* Make sure that the result field is appropriately filled, even though | |
9514 | the result symbol will be replaced later on. */ | |
c064374d | 9515 | if (sym->tlink && sym->tlink->attr.function) |
4668d6f9 | 9516 | { |
c064374d PT |
9517 | if (sym->tlink->result |
9518 | && sym->tlink->result != sym->tlink) | |
9519 | sym->result= sym->tlink->result; | |
4668d6f9 PT |
9520 | else |
9521 | sym->result = sym; | |
9522 | } | |
9523 | ||
9524 | /* Set declared_at as it might point to, e.g., a PUBLIC statement, if | |
9525 | the symbol existed before. */ | |
9526 | sym->declared_at = gfc_current_locus; | |
9527 | ||
9528 | if (!sym->attr.module_procedure) | |
9529 | return MATCH_ERROR; | |
9530 | ||
9531 | /* Signal match_end to expect "end procedure". */ | |
9532 | sym->abr_modproc_decl = 1; | |
9533 | ||
9534 | /* Change from IFSRC_IFBODY coming from the interface declaration. */ | |
9535 | sym->attr.if_source = IFSRC_DECL; | |
9536 | ||
9537 | gfc_new_block = sym; | |
9538 | ||
9539 | /* Make a new formal arglist with the symbols in the procedure | |
9540 | namespace. */ | |
9541 | head = tail = NULL; | |
9542 | for (formal = sym->formal; formal && formal->sym; formal = formal->next) | |
9543 | { | |
9544 | if (formal == sym->formal) | |
9545 | head = tail = gfc_get_formal_arglist (); | |
9546 | else | |
9547 | { | |
9548 | tail->next = gfc_get_formal_arglist (); | |
9549 | tail = tail->next; | |
9550 | } | |
9551 | ||
9552 | if (gfc_copy_dummy_sym (&fsym, formal->sym, 0)) | |
9553 | goto cleanup; | |
9554 | ||
9555 | tail->sym = fsym; | |
9556 | gfc_set_sym_referenced (fsym); | |
9557 | } | |
9558 | ||
9559 | /* The dummy symbols get cleaned up, when the formal_namespace of the | |
9560 | interface declaration is cleared. This allows us to add the | |
9561 | explicit interface as is done for other type of procedure. */ | |
9562 | if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head, | |
9563 | &gfc_current_locus)) | |
9564 | return MATCH_ERROR; | |
9565 | ||
9566 | if (gfc_match_eos () != MATCH_YES) | |
9567 | { | |
9568 | gfc_syntax_error (ST_MODULE_PROC); | |
9569 | return MATCH_ERROR; | |
9570 | } | |
9571 | ||
9572 | return MATCH_YES; | |
9573 | ||
9574 | cleanup: | |
9575 | gfc_free_formal_arglist (head); | |
9576 | return MATCH_ERROR; | |
9577 | } | |
9578 | ||
9579 | ||
6de9cd9a DN |
9580 | /* Match a module procedure statement. Note that we have to modify |
9581 | symbols in the parent's namespace because the current one was there | |
49de9e73 | 9582 | to receive symbols that are in an interface's formal argument list. */ |
6de9cd9a DN |
9583 | |
9584 | match | |
9585 | gfc_match_modproc (void) | |
9586 | { | |
9587 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
9588 | gfc_symbol *sym; | |
9589 | match m; | |
162b5a21 | 9590 | locus old_locus; |
060fca4a | 9591 | gfc_namespace *module_ns; |
2b77e908 | 9592 | gfc_interface *old_interface_head, *interface; |
6de9cd9a DN |
9593 | |
9594 | if (gfc_state_stack->state != COMP_INTERFACE | |
9595 | || gfc_state_stack->previous == NULL | |
129d15a3 JW |
9596 | || current_interface.type == INTERFACE_NAMELESS |
9597 | || current_interface.type == INTERFACE_ABSTRACT) | |
6de9cd9a | 9598 | { |
636dff67 SK |
9599 | gfc_error ("MODULE PROCEDURE at %C must be in a generic module " |
9600 | "interface"); | |
6de9cd9a DN |
9601 | return MATCH_ERROR; |
9602 | } | |
9603 | ||
060fca4a PT |
9604 | module_ns = gfc_current_ns->parent; |
9605 | for (; module_ns; module_ns = module_ns->parent) | |
43dfd40c SK |
9606 | if (module_ns->proc_name->attr.flavor == FL_MODULE |
9607 | || module_ns->proc_name->attr.flavor == FL_PROGRAM | |
9608 | || (module_ns->proc_name->attr.flavor == FL_PROCEDURE | |
9609 | && !module_ns->proc_name->attr.contained)) | |
060fca4a PT |
9610 | break; |
9611 | ||
9612 | if (module_ns == NULL) | |
9613 | return MATCH_ERROR; | |
9614 | ||
2b77e908 FXC |
9615 | /* Store the current state of the interface. We will need it if we |
9616 | end up with a syntax error and need to recover. */ | |
9617 | old_interface_head = gfc_current_interface_head (); | |
9618 | ||
162b5a21 SK |
9619 | /* Check if the F2008 optional double colon appears. */ |
9620 | gfc_gobble_whitespace (); | |
9621 | old_locus = gfc_current_locus; | |
9622 | if (gfc_match ("::") == MATCH_YES) | |
9623 | { | |
524af0d6 JB |
9624 | if (!gfc_notify_std (GFC_STD_F2008, "double colon in " |
9625 | "MODULE PROCEDURE statement at %L", &old_locus)) | |
162b5a21 SK |
9626 | return MATCH_ERROR; |
9627 | } | |
9628 | else | |
9629 | gfc_current_locus = old_locus; | |
f5acf0f2 | 9630 | |
6de9cd9a DN |
9631 | for (;;) |
9632 | { | |
2b77e908 | 9633 | bool last = false; |
162b5a21 | 9634 | old_locus = gfc_current_locus; |
2b77e908 | 9635 | |
6de9cd9a DN |
9636 | m = gfc_match_name (name); |
9637 | if (m == MATCH_NO) | |
9638 | goto syntax; | |
9639 | if (m != MATCH_YES) | |
9640 | return MATCH_ERROR; | |
9641 | ||
2b77e908 FXC |
9642 | /* Check for syntax error before starting to add symbols to the |
9643 | current namespace. */ | |
9644 | if (gfc_match_eos () == MATCH_YES) | |
9645 | last = true; | |
162b5a21 | 9646 | |
2b77e908 FXC |
9647 | if (!last && gfc_match_char (',') != MATCH_YES) |
9648 | goto syntax; | |
9649 | ||
9650 | /* Now we're sure the syntax is valid, we process this item | |
9651 | further. */ | |
060fca4a | 9652 | if (gfc_get_symbol (name, module_ns, &sym)) |
6de9cd9a DN |
9653 | return MATCH_ERROR; |
9654 | ||
43dfd40c SK |
9655 | if (sym->attr.intrinsic) |
9656 | { | |
9657 | gfc_error ("Intrinsic procedure at %L cannot be a MODULE " | |
9658 | "PROCEDURE", &old_locus); | |
9659 | return MATCH_ERROR; | |
9660 | } | |
9661 | ||
6de9cd9a | 9662 | if (sym->attr.proc != PROC_MODULE |
524af0d6 | 9663 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) |
6de9cd9a DN |
9664 | return MATCH_ERROR; |
9665 | ||
524af0d6 | 9666 | if (!gfc_add_interface (sym)) |
6de9cd9a DN |
9667 | return MATCH_ERROR; |
9668 | ||
71f77fd7 | 9669 | sym->attr.mod_proc = 1; |
43dfd40c | 9670 | sym->declared_at = old_locus; |
71f77fd7 | 9671 | |
2b77e908 | 9672 | if (last) |
6de9cd9a | 9673 | break; |
6de9cd9a DN |
9674 | } |
9675 | ||
9676 | return MATCH_YES; | |
9677 | ||
9678 | syntax: | |
2b77e908 FXC |
9679 | /* Restore the previous state of the interface. */ |
9680 | interface = gfc_current_interface_head (); | |
9681 | gfc_set_current_interface_head (old_interface_head); | |
9682 | ||
9683 | /* Free the new interfaces. */ | |
9684 | while (interface != old_interface_head) | |
9685 | { | |
9686 | gfc_interface *i = interface->next; | |
cede9502 | 9687 | free (interface); |
2b77e908 FXC |
9688 | interface = i; |
9689 | } | |
9690 | ||
9691 | /* And issue a syntax error. */ | |
6de9cd9a DN |
9692 | gfc_syntax_error (ST_MODULE_PROC); |
9693 | return MATCH_ERROR; | |
9694 | } | |
9695 | ||
9696 | ||
7d1f1e61 | 9697 | /* Check a derived type that is being extended. */ |
42e3d759 | 9698 | |
7d1f1e61 PT |
9699 | static gfc_symbol* |
9700 | check_extended_derived_type (char *name) | |
9701 | { | |
9702 | gfc_symbol *extended; | |
9703 | ||
9704 | if (gfc_find_symbol (name, gfc_current_ns, 1, &extended)) | |
9705 | { | |
9706 | gfc_error ("Ambiguous symbol in TYPE definition at %C"); | |
9707 | return NULL; | |
9708 | } | |
9709 | ||
42e3d759 JW |
9710 | extended = gfc_find_dt_in_generic (extended); |
9711 | ||
9712 | /* F08:C428. */ | |
7d1f1e61 PT |
9713 | if (!extended) |
9714 | { | |
c4100eae | 9715 | gfc_error ("Symbol %qs at %C has not been previously defined", name); |
7d1f1e61 PT |
9716 | return NULL; |
9717 | } | |
9718 | ||
9719 | if (extended->attr.flavor != FL_DERIVED) | |
9720 | { | |
c4100eae | 9721 | gfc_error ("%qs in EXTENDS expression at %C is not a " |
7d1f1e61 PT |
9722 | "derived type", name); |
9723 | return NULL; | |
9724 | } | |
9725 | ||
9726 | if (extended->attr.is_bind_c) | |
9727 | { | |
c4100eae | 9728 | gfc_error ("%qs cannot be extended at %C because it " |
7d1f1e61 PT |
9729 | "is BIND(C)", extended->name); |
9730 | return NULL; | |
9731 | } | |
9732 | ||
9733 | if (extended->attr.sequence) | |
9734 | { | |
c4100eae | 9735 | gfc_error ("%qs cannot be extended at %C because it " |
7d1f1e61 PT |
9736 | "is a SEQUENCE type", extended->name); |
9737 | return NULL; | |
9738 | } | |
9739 | ||
9740 | return extended; | |
9741 | } | |
9742 | ||
9743 | ||
a8b3b0b6 CR |
9744 | /* Match the optional attribute specifiers for a type declaration. |
9745 | Return MATCH_ERROR if an error is encountered in one of the handled | |
9746 | attributes (public, private, bind(c)), MATCH_NO if what's found is | |
9747 | not a handled attribute, and MATCH_YES otherwise. TODO: More error | |
9748 | checking on attribute conflicts needs to be done. */ | |
6de9cd9a DN |
9749 | |
9750 | match | |
7d1f1e61 | 9751 | gfc_get_type_attr_spec (symbol_attribute *attr, char *name) |
6de9cd9a | 9752 | { |
a8b3b0b6 | 9753 | /* See if the derived type is marked as private. */ |
6de9cd9a DN |
9754 | if (gfc_match (" , private") == MATCH_YES) |
9755 | { | |
d51347f9 | 9756 | if (gfc_current_state () != COMP_MODULE) |
6de9cd9a | 9757 | { |
d51347f9 TB |
9758 | gfc_error ("Derived type at %C can only be PRIVATE in the " |
9759 | "specification part of a module"); | |
6de9cd9a DN |
9760 | return MATCH_ERROR; |
9761 | } | |
9762 | ||
524af0d6 | 9763 | if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL)) |
6de9cd9a | 9764 | return MATCH_ERROR; |
6de9cd9a | 9765 | } |
a8b3b0b6 | 9766 | else if (gfc_match (" , public") == MATCH_YES) |
6de9cd9a | 9767 | { |
d51347f9 | 9768 | if (gfc_current_state () != COMP_MODULE) |
6de9cd9a | 9769 | { |
d51347f9 TB |
9770 | gfc_error ("Derived type at %C can only be PUBLIC in the " |
9771 | "specification part of a module"); | |
6de9cd9a DN |
9772 | return MATCH_ERROR; |
9773 | } | |
9774 | ||
524af0d6 | 9775 | if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL)) |
6de9cd9a | 9776 | return MATCH_ERROR; |
6de9cd9a | 9777 | } |
52f49934 | 9778 | else if (gfc_match (" , bind ( c )") == MATCH_YES) |
a8b3b0b6 CR |
9779 | { |
9780 | /* If the type is defined to be bind(c) it then needs to make | |
9781 | sure that all fields are interoperable. This will | |
9782 | need to be a semantic check on the finished derived type. | |
9783 | See 15.2.3 (lines 9-12) of F2003 draft. */ | |
524af0d6 | 9784 | if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0)) |
a8b3b0b6 CR |
9785 | return MATCH_ERROR; |
9786 | ||
9787 | /* TODO: attr conflicts need to be checked, probably in symbol.c. */ | |
9788 | } | |
52f49934 DK |
9789 | else if (gfc_match (" , abstract") == MATCH_YES) |
9790 | { | |
524af0d6 | 9791 | if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C")) |
52f49934 DK |
9792 | return MATCH_ERROR; |
9793 | ||
524af0d6 | 9794 | if (!gfc_add_abstract (attr, &gfc_current_locus)) |
52f49934 DK |
9795 | return MATCH_ERROR; |
9796 | } | |
524af0d6 | 9797 | else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES) |
7d1f1e61 | 9798 | { |
524af0d6 | 9799 | if (!gfc_add_extension (attr, &gfc_current_locus)) |
7d1f1e61 PT |
9800 | return MATCH_ERROR; |
9801 | } | |
a8b3b0b6 CR |
9802 | else |
9803 | return MATCH_NO; | |
9804 | ||
9805 | /* If we get here, something matched. */ | |
9806 | return MATCH_YES; | |
9807 | } | |
9808 | ||
9809 | ||
f6288c24 FR |
9810 | /* Common function for type declaration blocks similar to derived types, such |
9811 | as STRUCTURES and MAPs. Unlike derived types, a structure type | |
9812 | does NOT have a generic symbol matching the name given by the user. | |
9813 | STRUCTUREs can share names with variables and PARAMETERs so we must allow | |
9814 | for the creation of an independent symbol. | |
6442a6f4 | 9815 | Other parameters are a message to prefix errors with, the name of the new |
f6288c24 FR |
9816 | type to be created, and the flavor to add to the resulting symbol. */ |
9817 | ||
9818 | static bool | |
9819 | get_struct_decl (const char *name, sym_flavor fl, locus *decl, | |
9820 | gfc_symbol **result) | |
9821 | { | |
9822 | gfc_symbol *sym; | |
9823 | locus where; | |
9824 | ||
9825 | gcc_assert (name[0] == (char) TOUPPER (name[0])); | |
9826 | ||
9827 | if (decl) | |
9828 | where = *decl; | |
9829 | else | |
9830 | where = gfc_current_locus; | |
9831 | ||
9832 | if (gfc_get_symbol (name, NULL, &sym)) | |
9833 | return false; | |
9834 | ||
9835 | if (!sym) | |
9836 | { | |
9837 | gfc_internal_error ("Failed to create structure type '%s' at %C", name); | |
9838 | return false; | |
9839 | } | |
9840 | ||
9841 | if (sym->components != NULL || sym->attr.zero_comp) | |
9842 | { | |
2f029c08 | 9843 | gfc_error ("Type definition of %qs at %C was already defined at %L", |
f6288c24 FR |
9844 | sym->name, &sym->declared_at); |
9845 | return false; | |
9846 | } | |
9847 | ||
9848 | sym->declared_at = where; | |
9849 | ||
9850 | if (sym->attr.flavor != fl | |
9851 | && !gfc_add_flavor (&sym->attr, fl, sym->name, NULL)) | |
9852 | return false; | |
9853 | ||
9854 | if (!sym->hash_value) | |
9855 | /* Set the hash for the compound name for this type. */ | |
9856 | sym->hash_value = gfc_hash_value (sym); | |
9857 | ||
9858 | /* Normally the type is expected to have been completely parsed by the time | |
9859 | a field declaration with this type is seen. For unions, maps, and nested | |
9860 | structure declarations, we need to indicate that it is okay that we | |
9861 | haven't seen any components yet. This will be updated after the structure | |
9862 | is fully parsed. */ | |
9863 | sym->attr.zero_comp = 0; | |
9864 | ||
9865 | /* Structures always act like derived-types with the SEQUENCE attribute */ | |
9866 | gfc_add_sequence (&sym->attr, sym->name, NULL); | |
9867 | ||
9868 | if (result) *result = sym; | |
9869 | ||
9870 | return true; | |
9871 | } | |
9872 | ||
9873 | ||
9874 | /* Match the opening of a MAP block. Like a struct within a union in C; | |
9875 | behaves identical to STRUCTURE blocks. */ | |
9876 | ||
9877 | match | |
9878 | gfc_match_map (void) | |
9879 | { | |
05b8fcb4 FR |
9880 | /* Counter used to give unique internal names to map structures. */ |
9881 | static unsigned int gfc_map_id = 0; | |
9882 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
9883 | gfc_symbol *sym; | |
9884 | locus old_loc; | |
f6288c24 | 9885 | |
05b8fcb4 | 9886 | old_loc = gfc_current_locus; |
f6288c24 | 9887 | |
05b8fcb4 FR |
9888 | if (gfc_match_eos () != MATCH_YES) |
9889 | { | |
9890 | gfc_error ("Junk after MAP statement at %C"); | |
9891 | gfc_current_locus = old_loc; | |
9892 | return MATCH_ERROR; | |
9893 | } | |
f6288c24 | 9894 | |
05b8fcb4 FR |
9895 | /* Map blocks are anonymous so we make up unique names for the symbol table |
9896 | which are invalid Fortran identifiers. */ | |
9897 | snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "MM$%u", gfc_map_id++); | |
f6288c24 | 9898 | |
05b8fcb4 FR |
9899 | if (!get_struct_decl (name, FL_STRUCT, &old_loc, &sym)) |
9900 | return MATCH_ERROR; | |
f6288c24 | 9901 | |
05b8fcb4 | 9902 | gfc_new_block = sym; |
f6288c24 | 9903 | |
05b8fcb4 | 9904 | return MATCH_YES; |
f6288c24 FR |
9905 | } |
9906 | ||
9907 | ||
9908 | /* Match the opening of a UNION block. */ | |
9909 | ||
9910 | match | |
9911 | gfc_match_union (void) | |
9912 | { | |
05b8fcb4 FR |
9913 | /* Counter used to give unique internal names to union types. */ |
9914 | static unsigned int gfc_union_id = 0; | |
9915 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
9916 | gfc_symbol *sym; | |
9917 | locus old_loc; | |
f6288c24 | 9918 | |
05b8fcb4 | 9919 | old_loc = gfc_current_locus; |
f6288c24 | 9920 | |
05b8fcb4 FR |
9921 | if (gfc_match_eos () != MATCH_YES) |
9922 | { | |
9923 | gfc_error ("Junk after UNION statement at %C"); | |
9924 | gfc_current_locus = old_loc; | |
9925 | return MATCH_ERROR; | |
9926 | } | |
f6288c24 | 9927 | |
05b8fcb4 FR |
9928 | /* Unions are anonymous so we make up unique names for the symbol table |
9929 | which are invalid Fortran identifiers. */ | |
9930 | snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "UU$%u", gfc_union_id++); | |
f6288c24 | 9931 | |
05b8fcb4 FR |
9932 | if (!get_struct_decl (name, FL_UNION, &old_loc, &sym)) |
9933 | return MATCH_ERROR; | |
f6288c24 | 9934 | |
05b8fcb4 | 9935 | gfc_new_block = sym; |
f6288c24 | 9936 | |
05b8fcb4 | 9937 | return MATCH_YES; |
f6288c24 FR |
9938 | } |
9939 | ||
9940 | ||
9941 | /* Match the beginning of a STRUCTURE declaration. This is similar to | |
9942 | matching the beginning of a derived type declaration with a few | |
9943 | twists. The resulting type symbol has no access control or other | |
9944 | interesting attributes. */ | |
9945 | ||
9946 | match | |
9947 | gfc_match_structure_decl (void) | |
9948 | { | |
05b8fcb4 FR |
9949 | /* Counter used to give unique internal names to anonymous structures. */ |
9950 | static unsigned int gfc_structure_id = 0; | |
9951 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
9952 | gfc_symbol *sym; | |
9953 | match m; | |
9954 | locus where; | |
f6288c24 | 9955 | |
05b8fcb4 FR |
9956 | if (!flag_dec_structure) |
9957 | { | |
cf004230 FR |
9958 | gfc_error ("%s at %C is a DEC extension, enable with " |
9959 | "%<-fdec-structure%>", | |
9960 | "STRUCTURE"); | |
05b8fcb4 FR |
9961 | return MATCH_ERROR; |
9962 | } | |
f6288c24 | 9963 | |
05b8fcb4 | 9964 | name[0] = '\0'; |
f6288c24 | 9965 | |
05b8fcb4 FR |
9966 | m = gfc_match (" /%n/", name); |
9967 | if (m != MATCH_YES) | |
9968 | { | |
9969 | /* Non-nested structure declarations require a structure name. */ | |
9970 | if (!gfc_comp_struct (gfc_current_state ())) | |
9971 | { | |
9972 | gfc_error ("Structure name expected in non-nested structure " | |
9973 | "declaration at %C"); | |
9974 | return MATCH_ERROR; | |
9975 | } | |
9976 | /* This is an anonymous structure; make up a unique name for it | |
9977 | (upper-case letters never make it to symbol names from the source). | |
9978 | The important thing is initializing the type variable | |
9979 | and setting gfc_new_symbol, which is immediately used by | |
9980 | parse_structure () and variable_decl () to add components of | |
9981 | this type. */ | |
9982 | snprintf (name, GFC_MAX_SYMBOL_LEN + 1, "SS$%u", gfc_structure_id++); | |
9983 | } | |
f6288c24 | 9984 | |
05b8fcb4 FR |
9985 | where = gfc_current_locus; |
9986 | /* No field list allowed after non-nested structure declaration. */ | |
9987 | if (!gfc_comp_struct (gfc_current_state ()) | |
9988 | && gfc_match_eos () != MATCH_YES) | |
9989 | { | |
9990 | gfc_error ("Junk after non-nested STRUCTURE statement at %C"); | |
9991 | return MATCH_ERROR; | |
9992 | } | |
f6288c24 | 9993 | |
05b8fcb4 FR |
9994 | /* Make sure the name is not the name of an intrinsic type. */ |
9995 | if (gfc_is_intrinsic_typename (name)) | |
9996 | { | |
2f029c08 | 9997 | gfc_error ("Structure name %qs at %C cannot be the same as an" |
05b8fcb4 FR |
9998 | " intrinsic type", name); |
9999 | return MATCH_ERROR; | |
10000 | } | |
f6288c24 | 10001 | |
05b8fcb4 FR |
10002 | /* Store the actual type symbol for the structure with an upper-case first |
10003 | letter (an invalid Fortran identifier). */ | |
f6288c24 | 10004 | |
51f03c6b | 10005 | if (!get_struct_decl (gfc_dt_upper_string (name), FL_STRUCT, &where, &sym)) |
05b8fcb4 | 10006 | return MATCH_ERROR; |
f6288c24 | 10007 | |
05b8fcb4 FR |
10008 | gfc_new_block = sym; |
10009 | return MATCH_YES; | |
f6288c24 FR |
10010 | } |
10011 | ||
90051c26 FR |
10012 | |
10013 | /* This function does some work to determine which matcher should be used to | |
58b9de9e | 10014 | * match a statement beginning with "TYPE". This is used to disambiguate TYPE |
90051c26 | 10015 | * as an alias for PRINT from derived type declarations, TYPE IS statements, |
58b9de9e | 10016 | * and [parameterized] derived type declarations. */ |
90051c26 FR |
10017 | |
10018 | match | |
10019 | gfc_match_type (gfc_statement *st) | |
10020 | { | |
10021 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
10022 | match m; | |
10023 | locus old_loc; | |
10024 | ||
10025 | /* Requires -fdec. */ | |
10026 | if (!flag_dec) | |
10027 | return MATCH_NO; | |
10028 | ||
10029 | m = gfc_match ("type"); | |
10030 | if (m != MATCH_YES) | |
10031 | return m; | |
10032 | /* If we already have an error in the buffer, it is probably from failing to | |
10033 | * match a derived type data declaration. Let it happen. */ | |
10034 | else if (gfc_error_flag_test ()) | |
10035 | return MATCH_NO; | |
10036 | ||
10037 | old_loc = gfc_current_locus; | |
10038 | *st = ST_NONE; | |
10039 | ||
10040 | /* If we see an attribute list before anything else it's definitely a derived | |
10041 | * type declaration. */ | |
10042 | if (gfc_match (" ,") == MATCH_YES || gfc_match (" ::") == MATCH_YES) | |
58b9de9e | 10043 | goto derived; |
90051c26 FR |
10044 | |
10045 | /* By now "TYPE" has already been matched. If we do not see a name, this may | |
10046 | * be something like "TYPE *" or "TYPE <fmt>". */ | |
10047 | m = gfc_match_name (name); | |
10048 | if (m != MATCH_YES) | |
10049 | { | |
10050 | /* Let print match if it can, otherwise throw an error from | |
10051 | * gfc_match_derived_decl. */ | |
10052 | gfc_current_locus = old_loc; | |
10053 | if (gfc_match_print () == MATCH_YES) | |
10054 | { | |
10055 | *st = ST_WRITE; | |
10056 | return MATCH_YES; | |
10057 | } | |
58b9de9e | 10058 | goto derived; |
90051c26 FR |
10059 | } |
10060 | ||
58b9de9e FR |
10061 | /* Check for EOS. */ |
10062 | if (gfc_match_eos () == MATCH_YES) | |
90051c26 FR |
10063 | { |
10064 | /* By now we have "TYPE <name> <EOS>". Check first if the name is an | |
10065 | * intrinsic typename - if so let gfc_match_derived_decl dump an error. | |
10066 | * Otherwise if gfc_match_derived_decl fails it's probably an existing | |
10067 | * symbol which can be printed. */ | |
10068 | gfc_current_locus = old_loc; | |
10069 | m = gfc_match_derived_decl (); | |
10070 | if (gfc_is_intrinsic_typename (name) || m == MATCH_YES) | |
10071 | { | |
10072 | *st = ST_DERIVED_DECL; | |
10073 | return m; | |
10074 | } | |
90051c26 | 10075 | } |
58b9de9e FR |
10076 | else |
10077 | { | |
10078 | /* Here we have "TYPE <name>". Check for <TYPE IS (> or a PDT declaration | |
10079 | like <type name(parameter)>. */ | |
10080 | gfc_gobble_whitespace (); | |
10081 | bool paren = gfc_peek_ascii_char () == '('; | |
10082 | if (paren) | |
10083 | { | |
10084 | if (strcmp ("is", name) == 0) | |
10085 | goto typeis; | |
10086 | else | |
10087 | goto derived; | |
10088 | } | |
10089 | } | |
10090 | ||
10091 | /* Treat TYPE... like PRINT... */ | |
10092 | gfc_current_locus = old_loc; | |
10093 | *st = ST_WRITE; | |
10094 | return gfc_match_print (); | |
90051c26 | 10095 | |
58b9de9e FR |
10096 | derived: |
10097 | gfc_current_locus = old_loc; | |
10098 | *st = ST_DERIVED_DECL; | |
10099 | return gfc_match_derived_decl (); | |
10100 | ||
10101 | typeis: | |
10102 | gfc_current_locus = old_loc; | |
10103 | *st = ST_TYPE_IS; | |
10104 | return gfc_match_type_is (); | |
90051c26 FR |
10105 | } |
10106 | ||
10107 | ||
a8b3b0b6 CR |
10108 | /* Match the beginning of a derived type declaration. If a type name |
10109 | was the result of a function, then it is possible to have a symbol | |
10110 | already to be known as a derived type yet have no components. */ | |
10111 | ||
10112 | match | |
10113 | gfc_match_derived_decl (void) | |
10114 | { | |
10115 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
7d1f1e61 | 10116 | char parent[GFC_MAX_SYMBOL_LEN + 1]; |
a8b3b0b6 | 10117 | symbol_attribute attr; |
c3f34952 | 10118 | gfc_symbol *sym, *gensym; |
7d1f1e61 | 10119 | gfc_symbol *extended; |
a8b3b0b6 CR |
10120 | match m; |
10121 | match is_type_attr_spec = MATCH_NO; | |
e7303e85 | 10122 | bool seen_attr = false; |
c3f34952 | 10123 | gfc_interface *intr = NULL, *head; |
5bab4c96 PT |
10124 | bool parameterized_type = false; |
10125 | bool seen_colons = false; | |
a8b3b0b6 | 10126 | |
f6288c24 | 10127 | if (gfc_comp_struct (gfc_current_state ())) |
a8b3b0b6 CR |
10128 | return MATCH_NO; |
10129 | ||
7d1f1e61 PT |
10130 | name[0] = '\0'; |
10131 | parent[0] = '\0'; | |
a8b3b0b6 | 10132 | gfc_clear_attr (&attr); |
7d1f1e61 | 10133 | extended = NULL; |
a8b3b0b6 CR |
10134 | |
10135 | do | |
10136 | { | |
7d1f1e61 | 10137 | is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent); |
a8b3b0b6 CR |
10138 | if (is_type_attr_spec == MATCH_ERROR) |
10139 | return MATCH_ERROR; | |
e7303e85 FXC |
10140 | if (is_type_attr_spec == MATCH_YES) |
10141 | seen_attr = true; | |
a8b3b0b6 | 10142 | } while (is_type_attr_spec == MATCH_YES); |
6de9cd9a | 10143 | |
63a3341a PT |
10144 | /* Deal with derived type extensions. The extension attribute has |
10145 | been added to 'attr' but now the parent type must be found and | |
10146 | checked. */ | |
7d1f1e61 PT |
10147 | if (parent[0]) |
10148 | extended = check_extended_derived_type (parent); | |
10149 | ||
10150 | if (parent[0] && !extended) | |
10151 | return MATCH_ERROR; | |
10152 | ||
5bab4c96 PT |
10153 | m = gfc_match (" ::"); |
10154 | if (m == MATCH_YES) | |
10155 | { | |
10156 | seen_colons = true; | |
10157 | } | |
10158 | else if (seen_attr) | |
6de9cd9a DN |
10159 | { |
10160 | gfc_error ("Expected :: in TYPE definition at %C"); | |
10161 | return MATCH_ERROR; | |
10162 | } | |
10163 | ||
5bab4c96 | 10164 | m = gfc_match (" %n ", name); |
6de9cd9a DN |
10165 | if (m != MATCH_YES) |
10166 | return m; | |
10167 | ||
5bab4c96 PT |
10168 | /* Make sure that we don't identify TYPE IS (...) as a parameterized |
10169 | derived type named 'is'. | |
10170 | TODO Expand the check, when 'name' = "is" by matching " (tname) " | |
10171 | and checking if this is a(n intrinsic) typename. his picks up | |
10172 | misplaced TYPE IS statements such as in select_type_1.f03. */ | |
10173 | if (gfc_peek_ascii_char () == '(') | |
10174 | { | |
10175 | if (gfc_current_state () == COMP_SELECT_TYPE | |
10176 | || (!seen_colons && !strcmp (name, "is"))) | |
10177 | return MATCH_NO; | |
10178 | parameterized_type = true; | |
10179 | } | |
10180 | ||
10181 | m = gfc_match_eos (); | |
10182 | if (m != MATCH_YES && !parameterized_type) | |
10183 | return m; | |
10184 | ||
e9c06563 TB |
10185 | /* Make sure the name is not the name of an intrinsic type. */ |
10186 | if (gfc_is_intrinsic_typename (name)) | |
6de9cd9a | 10187 | { |
c4100eae | 10188 | gfc_error ("Type name %qs at %C cannot be the same as an intrinsic " |
636dff67 | 10189 | "type", name); |
6de9cd9a DN |
10190 | return MATCH_ERROR; |
10191 | } | |
10192 | ||
c3f34952 | 10193 | if (gfc_get_symbol (name, NULL, &gensym)) |
6de9cd9a DN |
10194 | return MATCH_ERROR; |
10195 | ||
c3f34952 | 10196 | if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN) |
6de9cd9a | 10197 | { |
1072bff8 SK |
10198 | if (gensym->ts.u.derived) |
10199 | gfc_error ("Derived type name %qs at %C already has a basic type " | |
10200 | "of %s", gensym->name, gfc_typename (&gensym->ts)); | |
10201 | else | |
10202 | gfc_error ("Derived type name %qs at %C already has a basic type", | |
10203 | gensym->name); | |
c3f34952 TB |
10204 | return MATCH_ERROR; |
10205 | } | |
10206 | ||
10207 | if (!gensym->attr.generic | |
524af0d6 | 10208 | && !gfc_add_generic (&gensym->attr, gensym->name, NULL)) |
c3f34952 TB |
10209 | return MATCH_ERROR; |
10210 | ||
10211 | if (!gensym->attr.function | |
524af0d6 | 10212 | && !gfc_add_function (&gensym->attr, gensym->name, NULL)) |
c3f34952 TB |
10213 | return MATCH_ERROR; |
10214 | ||
e00e1954 SK |
10215 | if (gensym->attr.dummy) |
10216 | { | |
10217 | gfc_error ("Dummy argument %qs at %L cannot be a derived type at %C", | |
10218 | name, &gensym->declared_at); | |
10219 | return MATCH_ERROR; | |
10220 | } | |
10221 | ||
c3f34952 TB |
10222 | sym = gfc_find_dt_in_generic (gensym); |
10223 | ||
10224 | if (sym && (sym->components != NULL || sym->attr.zero_comp)) | |
10225 | { | |
c4100eae | 10226 | gfc_error ("Derived type definition of %qs at %C has already been " |
c3f34952 | 10227 | "defined", sym->name); |
6de9cd9a DN |
10228 | return MATCH_ERROR; |
10229 | } | |
10230 | ||
c3f34952 TB |
10231 | if (!sym) |
10232 | { | |
10233 | /* Use upper case to save the actual derived-type symbol. */ | |
f6288c24 | 10234 | gfc_get_symbol (gfc_dt_upper_string (gensym->name), NULL, &sym); |
51f03c6b | 10235 | sym->name = gfc_get_string ("%s", gensym->name); |
c3f34952 TB |
10236 | head = gensym->generic; |
10237 | intr = gfc_get_interface (); | |
10238 | intr->sym = sym; | |
10239 | intr->where = gfc_current_locus; | |
10240 | intr->sym->declared_at = gfc_current_locus; | |
10241 | intr->next = head; | |
10242 | gensym->generic = intr; | |
10243 | gensym->attr.if_source = IFSRC_DECL; | |
10244 | } | |
10245 | ||
6de9cd9a DN |
10246 | /* The symbol may already have the derived attribute without the |
10247 | components. The ways this can happen is via a function | |
10248 | definition, an INTRINSIC statement or a subtype in another | |
10249 | derived type that is a pointer. The first part of the AND clause | |
df2fba9e | 10250 | is true if the symbol is not the return value of a function. */ |
6de9cd9a | 10251 | if (sym->attr.flavor != FL_DERIVED |
524af0d6 | 10252 | && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL)) |
6de9cd9a DN |
10253 | return MATCH_ERROR; |
10254 | ||
6de9cd9a | 10255 | if (attr.access != ACCESS_UNKNOWN |
524af0d6 | 10256 | && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL)) |
6de9cd9a | 10257 | return MATCH_ERROR; |
c3f34952 TB |
10258 | else if (sym->attr.access == ACCESS_UNKNOWN |
10259 | && gensym->attr.access != ACCESS_UNKNOWN | |
70112e2a | 10260 | && !gfc_add_access (&sym->attr, gensym->attr.access, |
524af0d6 | 10261 | sym->name, NULL)) |
c3f34952 TB |
10262 | return MATCH_ERROR; |
10263 | ||
10264 | if (sym->attr.access != ACCESS_UNKNOWN | |
10265 | && gensym->attr.access == ACCESS_UNKNOWN) | |
10266 | gensym->attr.access = sym->attr.access; | |
6de9cd9a | 10267 | |
a8b3b0b6 CR |
10268 | /* See if the derived type was labeled as bind(c). */ |
10269 | if (attr.is_bind_c != 0) | |
10270 | sym->attr.is_bind_c = attr.is_bind_c; | |
10271 | ||
34523524 DK |
10272 | /* Construct the f2k_derived namespace if it is not yet there. */ |
10273 | if (!sym->f2k_derived) | |
10274 | sym->f2k_derived = gfc_get_namespace (NULL, 0); | |
f5acf0f2 | 10275 | |
5bab4c96 PT |
10276 | if (parameterized_type) |
10277 | { | |
276515e6 PT |
10278 | /* Ignore error or mismatches by going to the end of the statement |
10279 | in order to avoid the component declarations causing problems. */ | |
10280 | m = gfc_match_formal_arglist (sym, 0, 0, true); | |
10281 | if (m != MATCH_YES) | |
10282 | gfc_error_recovery (); | |
84083a71 JW |
10283 | else |
10284 | sym->attr.pdt_template = 1; | |
5bab4c96 PT |
10285 | m = gfc_match_eos (); |
10286 | if (m != MATCH_YES) | |
f59986b2 PT |
10287 | { |
10288 | gfc_error_recovery (); | |
10289 | gfc_error_now ("Garbage after PARAMETERIZED TYPE declaration at %C"); | |
10290 | } | |
5bab4c96 PT |
10291 | } |
10292 | ||
7d1f1e61 PT |
10293 | if (extended && !sym->components) |
10294 | { | |
10295 | gfc_component *p; | |
5bab4c96 | 10296 | gfc_formal_arglist *f, *g, *h; |
7d1f1e61 PT |
10297 | |
10298 | /* Add the extended derived type as the first component. */ | |
10299 | gfc_add_component (sym, parent, &p); | |
7d1f1e61 PT |
10300 | extended->refs++; |
10301 | gfc_set_sym_referenced (extended); | |
10302 | ||
10303 | p->ts.type = BT_DERIVED; | |
bc21d315 | 10304 | p->ts.u.derived = extended; |
7d1f1e61 | 10305 | p->initializer = gfc_default_initializer (&p->ts); |
f5acf0f2 | 10306 | |
7c1dab0d JW |
10307 | /* Set extension level. */ |
10308 | if (extended->attr.extension == 255) | |
10309 | { | |
10310 | /* Since the extension field is 8 bit wide, we can only have | |
10311 | up to 255 extension levels. */ | |
c4100eae | 10312 | gfc_error ("Maximum extension level reached with type %qs at %L", |
7c1dab0d JW |
10313 | extended->name, &extended->declared_at); |
10314 | return MATCH_ERROR; | |
10315 | } | |
10316 | sym->attr.extension = extended->attr.extension + 1; | |
7d1f1e61 PT |
10317 | |
10318 | /* Provide the links between the extended type and its extension. */ | |
10319 | if (!extended->f2k_derived) | |
10320 | extended->f2k_derived = gfc_get_namespace (NULL, 0); | |
5bab4c96 PT |
10321 | |
10322 | /* Copy the extended type-param-name-list from the extended type, | |
10323 | append those of the extension and add the whole lot to the | |
10324 | extension. */ | |
10325 | if (extended->attr.pdt_template) | |
10326 | { | |
10327 | g = h = NULL; | |
10328 | sym->attr.pdt_template = 1; | |
10329 | for (f = extended->formal; f; f = f->next) | |
10330 | { | |
10331 | if (f == extended->formal) | |
10332 | { | |
10333 | g = gfc_get_formal_arglist (); | |
10334 | h = g; | |
10335 | } | |
10336 | else | |
10337 | { | |
10338 | g->next = gfc_get_formal_arglist (); | |
10339 | g = g->next; | |
10340 | } | |
10341 | g->sym = f->sym; | |
10342 | } | |
10343 | g->next = sym->formal; | |
10344 | sym->formal = h; | |
10345 | } | |
7d1f1e61 PT |
10346 | } |
10347 | ||
7c1dab0d JW |
10348 | if (!sym->hash_value) |
10349 | /* Set the hash for the compound name for this type. */ | |
4fa02692 | 10350 | sym->hash_value = gfc_hash_value (sym); |
cf2b3c22 | 10351 | |
52f49934 DK |
10352 | /* Take over the ABSTRACT attribute. */ |
10353 | sym->attr.abstract = attr.abstract; | |
10354 | ||
6de9cd9a DN |
10355 | gfc_new_block = sym; |
10356 | ||
10357 | return MATCH_YES; | |
10358 | } | |
83d890b9 AL |
10359 | |
10360 | ||
f5acf0f2 | 10361 | /* Cray Pointees can be declared as: |
b3aefde2 | 10362 | pointer (ipt, a (n,m,...,*)) */ |
83d890b9 | 10363 | |
32e8bb8e | 10364 | match |
83d890b9 AL |
10365 | gfc_mod_pointee_as (gfc_array_spec *as) |
10366 | { | |
10367 | as->cray_pointee = true; /* This will be useful to know later. */ | |
10368 | if (as->type == AS_ASSUMED_SIZE) | |
b3aefde2 | 10369 | as->cp_was_assumed = true; |
83d890b9 AL |
10370 | else if (as->type == AS_ASSUMED_SHAPE) |
10371 | { | |
10372 | gfc_error ("Cray Pointee at %C cannot be assumed shape array"); | |
10373 | return MATCH_ERROR; | |
10374 | } | |
10375 | return MATCH_YES; | |
10376 | } | |
25d8f0a2 TS |
10377 | |
10378 | ||
f5acf0f2 PT |
10379 | /* Match the enum definition statement, here we are trying to match |
10380 | the first line of enum definition statement. | |
25d8f0a2 TS |
10381 | Returns MATCH_YES if match is found. */ |
10382 | ||
10383 | match | |
10384 | gfc_match_enum (void) | |
10385 | { | |
10386 | match m; | |
f5acf0f2 | 10387 | |
25d8f0a2 TS |
10388 | m = gfc_match_eos (); |
10389 | if (m != MATCH_YES) | |
10390 | return m; | |
10391 | ||
524af0d6 | 10392 | if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C")) |
25d8f0a2 TS |
10393 | return MATCH_ERROR; |
10394 | ||
10395 | return MATCH_YES; | |
10396 | } | |
10397 | ||
10398 | ||
31224396 SK |
10399 | /* Returns an initializer whose value is one higher than the value of the |
10400 | LAST_INITIALIZER argument. If the argument is NULL, the | |
10401 | initializers value will be set to zero. The initializer's kind | |
10402 | will be set to gfc_c_int_kind. | |
10403 | ||
10404 | If -fshort-enums is given, the appropriate kind will be selected | |
10405 | later after all enumerators have been parsed. A warning is issued | |
10406 | here if an initializer exceeds gfc_c_int_kind. */ | |
10407 | ||
10408 | static gfc_expr * | |
10409 | enum_initializer (gfc_expr *last_initializer, locus where) | |
10410 | { | |
10411 | gfc_expr *result; | |
b7e75771 | 10412 | result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where); |
31224396 SK |
10413 | |
10414 | mpz_init (result->value.integer); | |
10415 | ||
10416 | if (last_initializer != NULL) | |
10417 | { | |
10418 | mpz_add_ui (result->value.integer, last_initializer->value.integer, 1); | |
10419 | result->where = last_initializer->where; | |
10420 | ||
10421 | if (gfc_check_integer_range (result->value.integer, | |
10422 | gfc_c_int_kind) != ARITH_OK) | |
10423 | { | |
10424 | gfc_error ("Enumerator exceeds the C integer type at %C"); | |
10425 | return NULL; | |
10426 | } | |
10427 | } | |
10428 | else | |
10429 | { | |
10430 | /* Control comes here, if it's the very first enumerator and no | |
10431 | initializer has been given. It will be initialized to zero. */ | |
10432 | mpz_set_si (result->value.integer, 0); | |
10433 | } | |
10434 | ||
10435 | return result; | |
10436 | } | |
10437 | ||
10438 | ||
6133c68a TS |
10439 | /* Match a variable name with an optional initializer. When this |
10440 | subroutine is called, a variable is expected to be parsed next. | |
10441 | Depending on what is happening at the moment, updates either the | |
10442 | symbol table or the current interface. */ | |
10443 | ||
10444 | static match | |
10445 | enumerator_decl (void) | |
10446 | { | |
10447 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
10448 | gfc_expr *initializer; | |
10449 | gfc_array_spec *as = NULL; | |
10450 | gfc_symbol *sym; | |
10451 | locus var_locus; | |
10452 | match m; | |
524af0d6 | 10453 | bool t; |
6133c68a TS |
10454 | locus old_locus; |
10455 | ||
10456 | initializer = NULL; | |
10457 | old_locus = gfc_current_locus; | |
10458 | ||
10459 | /* When we get here, we've just matched a list of attributes and | |
10460 | maybe a type and a double colon. The next thing we expect to see | |
10461 | is the name of the symbol. */ | |
10462 | m = gfc_match_name (name); | |
10463 | if (m != MATCH_YES) | |
10464 | goto cleanup; | |
10465 | ||
10466 | var_locus = gfc_current_locus; | |
10467 | ||
10468 | /* OK, we've successfully matched the declaration. Now put the | |
10469 | symbol in the current namespace. If we fail to create the symbol, | |
10470 | bail out. */ | |
524af0d6 | 10471 | if (!build_sym (name, NULL, false, &as, &var_locus)) |
6133c68a TS |
10472 | { |
10473 | m = MATCH_ERROR; | |
10474 | goto cleanup; | |
10475 | } | |
10476 | ||
10477 | /* The double colon must be present in order to have initializers. | |
10478 | Otherwise the statement is ambiguous with an assignment statement. */ | |
10479 | if (colon_seen) | |
10480 | { | |
10481 | if (gfc_match_char ('=') == MATCH_YES) | |
10482 | { | |
10483 | m = gfc_match_init_expr (&initializer); | |
10484 | if (m == MATCH_NO) | |
10485 | { | |
10486 | gfc_error ("Expected an initialization expression at %C"); | |
10487 | m = MATCH_ERROR; | |
10488 | } | |
10489 | ||
10490 | if (m != MATCH_YES) | |
10491 | goto cleanup; | |
10492 | } | |
10493 | } | |
10494 | ||
10495 | /* If we do not have an initializer, the initialization value of the | |
10496 | previous enumerator (stored in last_initializer) is incremented | |
10497 | by 1 and is used to initialize the current enumerator. */ | |
10498 | if (initializer == NULL) | |
31224396 | 10499 | initializer = enum_initializer (last_initializer, old_locus); |
d51347f9 | 10500 | |
6133c68a TS |
10501 | if (initializer == NULL || initializer->ts.type != BT_INTEGER) |
10502 | { | |
01e64c3d JJ |
10503 | gfc_error ("ENUMERATOR %L not initialized with integer expression", |
10504 | &var_locus); | |
d51347f9 | 10505 | m = MATCH_ERROR; |
6133c68a TS |
10506 | goto cleanup; |
10507 | } | |
10508 | ||
10509 | /* Store this current initializer, for the next enumerator variable | |
10510 | to be parsed. add_init_expr_to_sym() zeros initializer, so we | |
10511 | use last_initializer below. */ | |
10512 | last_initializer = initializer; | |
10513 | t = add_init_expr_to_sym (name, &initializer, &var_locus); | |
10514 | ||
10515 | /* Maintain enumerator history. */ | |
10516 | gfc_find_symbol (name, NULL, 0, &sym); | |
10517 | create_enum_history (sym, last_initializer); | |
10518 | ||
524af0d6 | 10519 | return (t) ? MATCH_YES : MATCH_ERROR; |
6133c68a TS |
10520 | |
10521 | cleanup: | |
10522 | /* Free stuff up and return. */ | |
10523 | gfc_free_expr (initializer); | |
10524 | ||
10525 | return m; | |
10526 | } | |
10527 | ||
10528 | ||
66e4ab31 | 10529 | /* Match the enumerator definition statement. */ |
25d8f0a2 TS |
10530 | |
10531 | match | |
10532 | gfc_match_enumerator_def (void) | |
10533 | { | |
10534 | match m; | |
524af0d6 | 10535 | bool t; |
d51347f9 | 10536 | |
25d8f0a2 | 10537 | gfc_clear_ts (¤t_ts); |
d51347f9 | 10538 | |
25d8f0a2 TS |
10539 | m = gfc_match (" enumerator"); |
10540 | if (m != MATCH_YES) | |
10541 | return m; | |
6133c68a TS |
10542 | |
10543 | m = gfc_match (" :: "); | |
10544 | if (m == MATCH_ERROR) | |
10545 | return m; | |
10546 | ||
10547 | colon_seen = (m == MATCH_YES); | |
d51347f9 | 10548 | |
25d8f0a2 TS |
10549 | if (gfc_current_state () != COMP_ENUM) |
10550 | { | |
10551 | gfc_error ("ENUM definition statement expected before %C"); | |
10552 | gfc_free_enum_history (); | |
10553 | return MATCH_ERROR; | |
10554 | } | |
10555 | ||
10556 | (¤t_ts)->type = BT_INTEGER; | |
10557 | (¤t_ts)->kind = gfc_c_int_kind; | |
d51347f9 | 10558 | |
6133c68a TS |
10559 | gfc_clear_attr (¤t_attr); |
10560 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL); | |
524af0d6 | 10561 | if (!t) |
25d8f0a2 | 10562 | { |
6133c68a | 10563 | m = MATCH_ERROR; |
25d8f0a2 TS |
10564 | goto cleanup; |
10565 | } | |
10566 | ||
25d8f0a2 TS |
10567 | for (;;) |
10568 | { | |
6133c68a | 10569 | m = enumerator_decl (); |
25d8f0a2 | 10570 | if (m == MATCH_ERROR) |
01e64c3d JJ |
10571 | { |
10572 | gfc_free_enum_history (); | |
10573 | goto cleanup; | |
10574 | } | |
25d8f0a2 TS |
10575 | if (m == MATCH_NO) |
10576 | break; | |
10577 | ||
10578 | if (gfc_match_eos () == MATCH_YES) | |
10579 | goto cleanup; | |
10580 | if (gfc_match_char (',') != MATCH_YES) | |
10581 | break; | |
10582 | } | |
10583 | ||
10584 | if (gfc_current_state () == COMP_ENUM) | |
10585 | { | |
10586 | gfc_free_enum_history (); | |
10587 | gfc_error ("Syntax error in ENUMERATOR definition at %C"); | |
10588 | m = MATCH_ERROR; | |
10589 | } | |
10590 | ||
10591 | cleanup: | |
10592 | gfc_free_array_spec (current_as); | |
10593 | current_as = NULL; | |
10594 | return m; | |
10595 | ||
10596 | } | |
10597 | ||
f6fad28e | 10598 | |
30b608eb DK |
10599 | /* Match binding attributes. */ |
10600 | ||
10601 | static match | |
713485cc | 10602 | match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc) |
30b608eb DK |
10603 | { |
10604 | bool found_passing = false; | |
713485cc | 10605 | bool seen_ptr = false; |
90661f26 | 10606 | match m = MATCH_YES; |
30b608eb | 10607 | |
eea58adb | 10608 | /* Initialize to defaults. Do so even before the MATCH_NO check so that in |
30b608eb DK |
10609 | this case the defaults are in there. */ |
10610 | ba->access = ACCESS_UNKNOWN; | |
10611 | ba->pass_arg = NULL; | |
10612 | ba->pass_arg_num = 0; | |
10613 | ba->nopass = 0; | |
10614 | ba->non_overridable = 0; | |
b0e5fa94 | 10615 | ba->deferred = 0; |
90661f26 | 10616 | ba->ppc = ppc; |
30b608eb DK |
10617 | |
10618 | /* If we find a comma, we believe there are binding attributes. */ | |
90661f26 JW |
10619 | m = gfc_match_char (','); |
10620 | if (m == MATCH_NO) | |
10621 | goto done; | |
30b608eb DK |
10622 | |
10623 | do | |
10624 | { | |
e157f736 DK |
10625 | /* Access specifier. */ |
10626 | ||
10627 | m = gfc_match (" public"); | |
30b608eb DK |
10628 | if (m == MATCH_ERROR) |
10629 | goto error; | |
10630 | if (m == MATCH_YES) | |
10631 | { | |
e157f736 | 10632 | if (ba->access != ACCESS_UNKNOWN) |
30b608eb | 10633 | { |
e157f736 | 10634 | gfc_error ("Duplicate access-specifier at %C"); |
30b608eb DK |
10635 | goto error; |
10636 | } | |
10637 | ||
e157f736 | 10638 | ba->access = ACCESS_PUBLIC; |
30b608eb DK |
10639 | continue; |
10640 | } | |
10641 | ||
e157f736 | 10642 | m = gfc_match (" private"); |
30b608eb DK |
10643 | if (m == MATCH_ERROR) |
10644 | goto error; | |
10645 | if (m == MATCH_YES) | |
10646 | { | |
e157f736 | 10647 | if (ba->access != ACCESS_UNKNOWN) |
30b608eb | 10648 | { |
e157f736 | 10649 | gfc_error ("Duplicate access-specifier at %C"); |
30b608eb DK |
10650 | goto error; |
10651 | } | |
10652 | ||
e157f736 | 10653 | ba->access = ACCESS_PRIVATE; |
30b608eb DK |
10654 | continue; |
10655 | } | |
10656 | ||
e157f736 DK |
10657 | /* If inside GENERIC, the following is not allowed. */ |
10658 | if (!generic) | |
30b608eb | 10659 | { |
30b608eb | 10660 | |
e157f736 DK |
10661 | /* NOPASS flag. */ |
10662 | m = gfc_match (" nopass"); | |
10663 | if (m == MATCH_ERROR) | |
10664 | goto error; | |
10665 | if (m == MATCH_YES) | |
30b608eb | 10666 | { |
e157f736 DK |
10667 | if (found_passing) |
10668 | { | |
10669 | gfc_error ("Binding attributes already specify passing," | |
10670 | " illegal NOPASS at %C"); | |
10671 | goto error; | |
10672 | } | |
10673 | ||
10674 | found_passing = true; | |
10675 | ba->nopass = 1; | |
10676 | continue; | |
30b608eb DK |
10677 | } |
10678 | ||
e157f736 DK |
10679 | /* PASS possibly including argument. */ |
10680 | m = gfc_match (" pass"); | |
10681 | if (m == MATCH_ERROR) | |
10682 | goto error; | |
10683 | if (m == MATCH_YES) | |
30b608eb | 10684 | { |
e157f736 DK |
10685 | char arg[GFC_MAX_SYMBOL_LEN + 1]; |
10686 | ||
10687 | if (found_passing) | |
10688 | { | |
10689 | gfc_error ("Binding attributes already specify passing," | |
10690 | " illegal PASS at %C"); | |
10691 | goto error; | |
10692 | } | |
10693 | ||
10694 | m = gfc_match (" ( %n )", arg); | |
10695 | if (m == MATCH_ERROR) | |
10696 | goto error; | |
10697 | if (m == MATCH_YES) | |
51f03c6b | 10698 | ba->pass_arg = gfc_get_string ("%s", arg); |
e157f736 DK |
10699 | gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL)); |
10700 | ||
10701 | found_passing = true; | |
10702 | ba->nopass = 0; | |
10703 | continue; | |
30b608eb DK |
10704 | } |
10705 | ||
713485cc JW |
10706 | if (ppc) |
10707 | { | |
10708 | /* POINTER flag. */ | |
10709 | m = gfc_match (" pointer"); | |
10710 | if (m == MATCH_ERROR) | |
10711 | goto error; | |
10712 | if (m == MATCH_YES) | |
10713 | { | |
10714 | if (seen_ptr) | |
10715 | { | |
10716 | gfc_error ("Duplicate POINTER attribute at %C"); | |
10717 | goto error; | |
10718 | } | |
10719 | ||
10720 | seen_ptr = true; | |
713485cc JW |
10721 | continue; |
10722 | } | |
10723 | } | |
10724 | else | |
10725 | { | |
10726 | /* NON_OVERRIDABLE flag. */ | |
10727 | m = gfc_match (" non_overridable"); | |
10728 | if (m == MATCH_ERROR) | |
10729 | goto error; | |
10730 | if (m == MATCH_YES) | |
10731 | { | |
10732 | if (ba->non_overridable) | |
10733 | { | |
10734 | gfc_error ("Duplicate NON_OVERRIDABLE at %C"); | |
10735 | goto error; | |
10736 | } | |
10737 | ||
10738 | ba->non_overridable = 1; | |
10739 | continue; | |
10740 | } | |
10741 | ||
10742 | /* DEFERRED flag. */ | |
10743 | m = gfc_match (" deferred"); | |
10744 | if (m == MATCH_ERROR) | |
10745 | goto error; | |
10746 | if (m == MATCH_YES) | |
10747 | { | |
10748 | if (ba->deferred) | |
10749 | { | |
10750 | gfc_error ("Duplicate DEFERRED at %C"); | |
10751 | goto error; | |
10752 | } | |
10753 | ||
10754 | ba->deferred = 1; | |
10755 | continue; | |
10756 | } | |
10757 | } | |
10758 | ||
30b608eb DK |
10759 | } |
10760 | ||
10761 | /* Nothing matching found. */ | |
e157f736 DK |
10762 | if (generic) |
10763 | gfc_error ("Expected access-specifier at %C"); | |
10764 | else | |
10765 | gfc_error ("Expected binding attribute at %C"); | |
30b608eb DK |
10766 | goto error; |
10767 | } | |
10768 | while (gfc_match_char (',') == MATCH_YES); | |
10769 | ||
b0e5fa94 DK |
10770 | /* NON_OVERRIDABLE and DEFERRED exclude themselves. */ |
10771 | if (ba->non_overridable && ba->deferred) | |
10772 | { | |
1fe61adf | 10773 | gfc_error ("NON_OVERRIDABLE and DEFERRED cannot both appear at %C"); |
b0e5fa94 DK |
10774 | goto error; |
10775 | } | |
10776 | ||
90661f26 JW |
10777 | m = MATCH_YES; |
10778 | ||
10779 | done: | |
e157f736 | 10780 | if (ba->access == ACCESS_UNKNOWN) |
d4beaf2a JW |
10781 | ba->access = ppc ? gfc_current_block()->component_access |
10782 | : gfc_typebound_default_access; | |
e157f736 | 10783 | |
713485cc JW |
10784 | if (ppc && !seen_ptr) |
10785 | { | |
10786 | gfc_error ("POINTER attribute is required for procedure pointer component" | |
10787 | " at %C"); | |
10788 | goto error; | |
10789 | } | |
10790 | ||
90661f26 | 10791 | return m; |
30b608eb DK |
10792 | |
10793 | error: | |
30b608eb DK |
10794 | return MATCH_ERROR; |
10795 | } | |
10796 | ||
10797 | ||
10798 | /* Match a PROCEDURE specific binding inside a derived type. */ | |
10799 | ||
10800 | static match | |
10801 | match_procedure_in_type (void) | |
10802 | { | |
10803 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
10804 | char target_buf[GFC_MAX_SYMBOL_LEN + 1]; | |
1be17993 | 10805 | char* target = NULL, *ifc = NULL; |
3e15518b | 10806 | gfc_typebound_proc tb; |
30b608eb DK |
10807 | bool seen_colons; |
10808 | bool seen_attrs; | |
10809 | match m; | |
10810 | gfc_symtree* stree; | |
10811 | gfc_namespace* ns; | |
10812 | gfc_symbol* block; | |
1be17993 | 10813 | int num; |
30b608eb DK |
10814 | |
10815 | /* Check current state. */ | |
10816 | gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS); | |
10817 | block = gfc_state_stack->previous->sym; | |
10818 | gcc_assert (block); | |
10819 | ||
b0e5fa94 | 10820 | /* Try to match PROCEDURE(interface). */ |
30b608eb DK |
10821 | if (gfc_match (" (") == MATCH_YES) |
10822 | { | |
b0e5fa94 DK |
10823 | m = gfc_match_name (target_buf); |
10824 | if (m == MATCH_ERROR) | |
10825 | return m; | |
10826 | if (m != MATCH_YES) | |
10827 | { | |
a4d9b221 | 10828 | gfc_error ("Interface-name expected after %<(%> at %C"); |
b0e5fa94 DK |
10829 | return MATCH_ERROR; |
10830 | } | |
10831 | ||
10832 | if (gfc_match (" )") != MATCH_YES) | |
10833 | { | |
a4d9b221 | 10834 | gfc_error ("%<)%> expected at %C"); |
b0e5fa94 DK |
10835 | return MATCH_ERROR; |
10836 | } | |
10837 | ||
1be17993 | 10838 | ifc = target_buf; |
30b608eb DK |
10839 | } |
10840 | ||
10841 | /* Construct the data structure. */ | |
ff5b6492 | 10842 | memset (&tb, 0, sizeof (tb)); |
3e15518b | 10843 | tb.where = gfc_current_locus; |
30b608eb DK |
10844 | |
10845 | /* Match binding attributes. */ | |
3e15518b | 10846 | m = match_binding_attributes (&tb, false, false); |
30b608eb DK |
10847 | if (m == MATCH_ERROR) |
10848 | return m; | |
10849 | seen_attrs = (m == MATCH_YES); | |
10850 | ||
1be17993 | 10851 | /* Check that attribute DEFERRED is given if an interface is specified. */ |
3e15518b | 10852 | if (tb.deferred && !ifc) |
b0e5fa94 DK |
10853 | { |
10854 | gfc_error ("Interface must be specified for DEFERRED binding at %C"); | |
10855 | return MATCH_ERROR; | |
10856 | } | |
3e15518b | 10857 | if (ifc && !tb.deferred) |
b0e5fa94 DK |
10858 | { |
10859 | gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED"); | |
10860 | return MATCH_ERROR; | |
10861 | } | |
10862 | ||
30b608eb DK |
10863 | /* Match the colons. */ |
10864 | m = gfc_match (" ::"); | |
10865 | if (m == MATCH_ERROR) | |
10866 | return m; | |
10867 | seen_colons = (m == MATCH_YES); | |
10868 | if (seen_attrs && !seen_colons) | |
10869 | { | |
a4d9b221 | 10870 | gfc_error ("Expected %<::%> after binding-attributes at %C"); |
30b608eb DK |
10871 | return MATCH_ERROR; |
10872 | } | |
10873 | ||
f5acf0f2 | 10874 | /* Match the binding names. */ |
1be17993 | 10875 | for(num=1;;num++) |
30b608eb | 10876 | { |
1be17993 JW |
10877 | m = gfc_match_name (name); |
10878 | if (m == MATCH_ERROR) | |
10879 | return m; | |
10880 | if (m == MATCH_NO) | |
b0e5fa94 | 10881 | { |
1be17993 | 10882 | gfc_error ("Expected binding name at %C"); |
b0e5fa94 DK |
10883 | return MATCH_ERROR; |
10884 | } | |
10885 | ||
524af0d6 | 10886 | if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C")) |
1be17993 | 10887 | return MATCH_ERROR; |
30b608eb | 10888 | |
1be17993 JW |
10889 | /* Try to match the '=> target', if it's there. */ |
10890 | target = ifc; | |
10891 | m = gfc_match (" =>"); | |
30b608eb DK |
10892 | if (m == MATCH_ERROR) |
10893 | return m; | |
1be17993 | 10894 | if (m == MATCH_YES) |
30b608eb | 10895 | { |
3e15518b | 10896 | if (tb.deferred) |
1be17993 | 10897 | { |
a4d9b221 | 10898 | gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C"); |
1be17993 JW |
10899 | return MATCH_ERROR; |
10900 | } | |
10901 | ||
10902 | if (!seen_colons) | |
10903 | { | |
a4d9b221 | 10904 | gfc_error ("%<::%> needed in PROCEDURE binding with explicit target" |
1be17993 JW |
10905 | " at %C"); |
10906 | return MATCH_ERROR; | |
10907 | } | |
10908 | ||
10909 | m = gfc_match_name (target_buf); | |
10910 | if (m == MATCH_ERROR) | |
10911 | return m; | |
10912 | if (m == MATCH_NO) | |
10913 | { | |
a4d9b221 | 10914 | gfc_error ("Expected binding target after %<=>%> at %C"); |
1be17993 JW |
10915 | return MATCH_ERROR; |
10916 | } | |
10917 | target = target_buf; | |
30b608eb | 10918 | } |
30b608eb | 10919 | |
1be17993 JW |
10920 | /* If no target was found, it has the same name as the binding. */ |
10921 | if (!target) | |
10922 | target = name; | |
30b608eb | 10923 | |
1be17993 JW |
10924 | /* Get the namespace to insert the symbols into. */ |
10925 | ns = block->f2k_derived; | |
10926 | gcc_assert (ns); | |
30b608eb | 10927 | |
1be17993 | 10928 | /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */ |
3e15518b | 10929 | if (tb.deferred && !block->attr.abstract) |
1be17993 | 10930 | { |
c4100eae | 10931 | gfc_error ("Type %qs containing DEFERRED binding at %C " |
1be17993 JW |
10932 | "is not ABSTRACT", block->name); |
10933 | return MATCH_ERROR; | |
10934 | } | |
30b608eb | 10935 | |
1be17993 | 10936 | /* See if we already have a binding with this name in the symtree which |
6bd2c800 | 10937 | would be an error. If a GENERIC already targeted this binding, it may |
1be17993 JW |
10938 | be already there but then typebound is still NULL. */ |
10939 | stree = gfc_find_symtree (ns->tb_sym_root, name); | |
9f23af48 | 10940 | if (stree && stree->n.tb) |
1be17993 | 10941 | { |
c4100eae MLI |
10942 | gfc_error ("There is already a procedure with binding name %qs for " |
10943 | "the derived type %qs at %C", name, block->name); | |
1be17993 JW |
10944 | return MATCH_ERROR; |
10945 | } | |
b0e5fa94 | 10946 | |
1be17993 | 10947 | /* Insert it and set attributes. */ |
30b608eb | 10948 | |
9f23af48 MM |
10949 | if (!stree) |
10950 | { | |
10951 | stree = gfc_new_symtree (&ns->tb_sym_root, name); | |
10952 | gcc_assert (stree); | |
10953 | } | |
3e15518b | 10954 | stree->n.tb = gfc_get_typebound_proc (&tb); |
e34ccb4c | 10955 | |
3e15518b JW |
10956 | if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific, |
10957 | false)) | |
1be17993 | 10958 | return MATCH_ERROR; |
3e15518b | 10959 | gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym); |
f9d49cd1 JW |
10960 | gfc_add_flavor(&stree->n.tb->u.specific->n.sym->attr, FL_PROCEDURE, |
10961 | target, &stree->n.tb->u.specific->n.sym->declared_at); | |
f5acf0f2 | 10962 | |
1be17993 JW |
10963 | if (gfc_match_eos () == MATCH_YES) |
10964 | return MATCH_YES; | |
10965 | if (gfc_match_char (',') != MATCH_YES) | |
10966 | goto syntax; | |
e34ccb4c | 10967 | } |
30b608eb | 10968 | |
1be17993 JW |
10969 | syntax: |
10970 | gfc_error ("Syntax error in PROCEDURE statement at %C"); | |
10971 | return MATCH_ERROR; | |
30b608eb DK |
10972 | } |
10973 | ||
10974 | ||
e157f736 DK |
10975 | /* Match a GENERIC procedure binding inside a derived type. */ |
10976 | ||
10977 | match | |
10978 | gfc_match_generic (void) | |
10979 | { | |
10980 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
94747289 | 10981 | char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */ |
e157f736 DK |
10982 | gfc_symbol* block; |
10983 | gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */ | |
10984 | gfc_typebound_proc* tb; | |
e157f736 | 10985 | gfc_namespace* ns; |
94747289 DK |
10986 | interface_type op_type; |
10987 | gfc_intrinsic_op op; | |
e157f736 DK |
10988 | match m; |
10989 | ||
10990 | /* Check current state. */ | |
10991 | if (gfc_current_state () == COMP_DERIVED) | |
10992 | { | |
10993 | gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS"); | |
10994 | return MATCH_ERROR; | |
10995 | } | |
10996 | if (gfc_current_state () != COMP_DERIVED_CONTAINS) | |
10997 | return MATCH_NO; | |
10998 | block = gfc_state_stack->previous->sym; | |
10999 | ns = block->f2k_derived; | |
11000 | gcc_assert (block && ns); | |
11001 | ||
ff5b6492 MM |
11002 | memset (&tbattr, 0, sizeof (tbattr)); |
11003 | tbattr.where = gfc_current_locus; | |
11004 | ||
e157f736 | 11005 | /* See if we get an access-specifier. */ |
713485cc | 11006 | m = match_binding_attributes (&tbattr, true, false); |
e157f736 DK |
11007 | if (m == MATCH_ERROR) |
11008 | goto error; | |
11009 | ||
11010 | /* Now the colons, those are required. */ | |
11011 | if (gfc_match (" ::") != MATCH_YES) | |
11012 | { | |
a4d9b221 | 11013 | gfc_error ("Expected %<::%> at %C"); |
e157f736 DK |
11014 | goto error; |
11015 | } | |
11016 | ||
94747289 DK |
11017 | /* Match the binding name; depending on type (operator / generic) format |
11018 | it for future error messages into bind_name. */ | |
f5acf0f2 | 11019 | |
94747289 | 11020 | m = gfc_match_generic_spec (&op_type, name, &op); |
e157f736 DK |
11021 | if (m == MATCH_ERROR) |
11022 | return MATCH_ERROR; | |
11023 | if (m == MATCH_NO) | |
11024 | { | |
94747289 | 11025 | gfc_error ("Expected generic name or operator descriptor at %C"); |
e157f736 DK |
11026 | goto error; |
11027 | } | |
11028 | ||
94747289 | 11029 | switch (op_type) |
e157f736 | 11030 | { |
94747289 | 11031 | case INTERFACE_GENERIC: |
e73d3ca6 | 11032 | case INTERFACE_DTIO: |
94747289 DK |
11033 | snprintf (bind_name, sizeof (bind_name), "%s", name); |
11034 | break; | |
f5acf0f2 | 11035 | |
94747289 DK |
11036 | case INTERFACE_USER_OP: |
11037 | snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name); | |
11038 | break; | |
f5acf0f2 | 11039 | |
94747289 DK |
11040 | case INTERFACE_INTRINSIC_OP: |
11041 | snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)", | |
11042 | gfc_op2string (op)); | |
11043 | break; | |
11044 | ||
377e37c1 SK |
11045 | case INTERFACE_NAMELESS: |
11046 | gfc_error ("Malformed GENERIC statement at %C"); | |
11047 | goto error; | |
11048 | break; | |
11049 | ||
94747289 DK |
11050 | default: |
11051 | gcc_unreachable (); | |
11052 | } | |
e34ccb4c | 11053 | |
94747289 DK |
11054 | /* Match the required =>. */ |
11055 | if (gfc_match (" =>") != MATCH_YES) | |
11056 | { | |
a4d9b221 | 11057 | gfc_error ("Expected %<=>%> at %C"); |
94747289 DK |
11058 | goto error; |
11059 | } | |
f5acf0f2 | 11060 | |
94747289 DK |
11061 | /* Try to find existing GENERIC binding with this name / for this operator; |
11062 | if there is something, check that it is another GENERIC and then extend | |
11063 | it rather than building a new node. Otherwise, create it and put it | |
11064 | at the right position. */ | |
11065 | ||
11066 | switch (op_type) | |
11067 | { | |
e73d3ca6 | 11068 | case INTERFACE_DTIO: |
94747289 DK |
11069 | case INTERFACE_USER_OP: |
11070 | case INTERFACE_GENERIC: | |
11071 | { | |
11072 | const bool is_op = (op_type == INTERFACE_USER_OP); | |
11073 | gfc_symtree* st; | |
11074 | ||
11075 | st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name); | |
b93d8a3f | 11076 | tb = st ? st->n.tb : NULL; |
94747289 DK |
11077 | break; |
11078 | } | |
11079 | ||
11080 | case INTERFACE_INTRINSIC_OP: | |
11081 | tb = ns->tb_op[op]; | |
11082 | break; | |
11083 | ||
11084 | default: | |
11085 | gcc_unreachable (); | |
11086 | } | |
11087 | ||
11088 | if (tb) | |
11089 | { | |
e34ccb4c | 11090 | if (!tb->is_generic) |
e157f736 | 11091 | { |
94747289 | 11092 | gcc_assert (op_type == INTERFACE_GENERIC); |
e157f736 | 11093 | gfc_error ("There's already a non-generic procedure with binding name" |
c4100eae | 11094 | " %qs for the derived type %qs at %C", |
94747289 | 11095 | bind_name, block->name); |
e157f736 DK |
11096 | goto error; |
11097 | } | |
11098 | ||
e157f736 DK |
11099 | if (tb->access != tbattr.access) |
11100 | { | |
11101 | gfc_error ("Binding at %C must have the same access as already" | |
c4100eae | 11102 | " defined binding %qs", bind_name); |
e157f736 DK |
11103 | goto error; |
11104 | } | |
11105 | } | |
11106 | else | |
11107 | { | |
3e15518b | 11108 | tb = gfc_get_typebound_proc (NULL); |
e157f736 DK |
11109 | tb->where = gfc_current_locus; |
11110 | tb->access = tbattr.access; | |
11111 | tb->is_generic = 1; | |
11112 | tb->u.generic = NULL; | |
94747289 DK |
11113 | |
11114 | switch (op_type) | |
11115 | { | |
e73d3ca6 | 11116 | case INTERFACE_DTIO: |
94747289 DK |
11117 | case INTERFACE_GENERIC: |
11118 | case INTERFACE_USER_OP: | |
11119 | { | |
11120 | const bool is_op = (op_type == INTERFACE_USER_OP); | |
b93d8a3f JW |
11121 | gfc_symtree* st = gfc_get_tbp_symtree (is_op ? &ns->tb_uop_root : |
11122 | &ns->tb_sym_root, name); | |
94747289 DK |
11123 | gcc_assert (st); |
11124 | st->n.tb = tb; | |
11125 | ||
11126 | break; | |
11127 | } | |
f5acf0f2 | 11128 | |
94747289 DK |
11129 | case INTERFACE_INTRINSIC_OP: |
11130 | ns->tb_op[op] = tb; | |
11131 | break; | |
11132 | ||
11133 | default: | |
11134 | gcc_unreachable (); | |
11135 | } | |
e157f736 DK |
11136 | } |
11137 | ||
11138 | /* Now, match all following names as specific targets. */ | |
11139 | do | |
11140 | { | |
11141 | gfc_symtree* target_st; | |
11142 | gfc_tbp_generic* target; | |
11143 | ||
11144 | m = gfc_match_name (name); | |
11145 | if (m == MATCH_ERROR) | |
11146 | goto error; | |
11147 | if (m == MATCH_NO) | |
11148 | { | |
11149 | gfc_error ("Expected specific binding name at %C"); | |
11150 | goto error; | |
11151 | } | |
11152 | ||
e34ccb4c | 11153 | target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name); |
e157f736 DK |
11154 | |
11155 | /* See if this is a duplicate specification. */ | |
11156 | for (target = tb->u.generic; target; target = target->next) | |
11157 | if (target_st == target->specific_st) | |
11158 | { | |
c4100eae MLI |
11159 | gfc_error ("%qs already defined as specific binding for the" |
11160 | " generic %qs at %C", name, bind_name); | |
e157f736 DK |
11161 | goto error; |
11162 | } | |
11163 | ||
e157f736 DK |
11164 | target = gfc_get_tbp_generic (); |
11165 | target->specific_st = target_st; | |
11166 | target->specific = NULL; | |
11167 | target->next = tb->u.generic; | |
218e1228 TB |
11168 | target->is_operator = ((op_type == INTERFACE_USER_OP) |
11169 | || (op_type == INTERFACE_INTRINSIC_OP)); | |
e157f736 DK |
11170 | tb->u.generic = target; |
11171 | } | |
11172 | while (gfc_match (" ,") == MATCH_YES); | |
11173 | ||
11174 | /* Here should be the end. */ | |
11175 | if (gfc_match_eos () != MATCH_YES) | |
11176 | { | |
11177 | gfc_error ("Junk after GENERIC binding at %C"); | |
11178 | goto error; | |
11179 | } | |
11180 | ||
11181 | return MATCH_YES; | |
11182 | ||
11183 | error: | |
11184 | return MATCH_ERROR; | |
11185 | } | |
11186 | ||
11187 | ||
34523524 DK |
11188 | /* Match a FINAL declaration inside a derived type. */ |
11189 | ||
11190 | match | |
11191 | gfc_match_final_decl (void) | |
11192 | { | |
11193 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
11194 | gfc_symbol* sym; | |
11195 | match m; | |
11196 | gfc_namespace* module_ns; | |
11197 | bool first, last; | |
30b608eb | 11198 | gfc_symbol* block; |
34523524 | 11199 | |
33344e0f JW |
11200 | if (gfc_current_form == FORM_FREE) |
11201 | { | |
11202 | char c = gfc_peek_ascii_char (); | |
11203 | if (!gfc_is_whitespace (c) && c != ':') | |
11204 | return MATCH_NO; | |
11205 | } | |
f5acf0f2 | 11206 | |
30b608eb | 11207 | if (gfc_state_stack->state != COMP_DERIVED_CONTAINS) |
34523524 | 11208 | { |
33344e0f JW |
11209 | if (gfc_current_form == FORM_FIXED) |
11210 | return MATCH_NO; | |
11211 | ||
34523524 | 11212 | gfc_error ("FINAL declaration at %C must be inside a derived type " |
30b608eb | 11213 | "CONTAINS section"); |
34523524 DK |
11214 | return MATCH_ERROR; |
11215 | } | |
11216 | ||
30b608eb DK |
11217 | block = gfc_state_stack->previous->sym; |
11218 | gcc_assert (block); | |
34523524 | 11219 | |
30b608eb DK |
11220 | if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous |
11221 | || gfc_state_stack->previous->previous->state != COMP_MODULE) | |
34523524 DK |
11222 | { |
11223 | gfc_error ("Derived type declaration with FINAL at %C must be in the" | |
11224 | " specification part of a MODULE"); | |
11225 | return MATCH_ERROR; | |
11226 | } | |
11227 | ||
11228 | module_ns = gfc_current_ns; | |
11229 | gcc_assert (module_ns); | |
11230 | gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE); | |
11231 | ||
11232 | /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */ | |
11233 | if (gfc_match (" ::") == MATCH_ERROR) | |
11234 | return MATCH_ERROR; | |
11235 | ||
11236 | /* Match the sequence of procedure names. */ | |
11237 | first = true; | |
11238 | last = false; | |
11239 | do | |
11240 | { | |
11241 | gfc_finalizer* f; | |
11242 | ||
11243 | if (first && gfc_match_eos () == MATCH_YES) | |
11244 | { | |
11245 | gfc_error ("Empty FINAL at %C"); | |
11246 | return MATCH_ERROR; | |
11247 | } | |
11248 | ||
11249 | m = gfc_match_name (name); | |
11250 | if (m == MATCH_NO) | |
11251 | { | |
11252 | gfc_error ("Expected module procedure name at %C"); | |
11253 | return MATCH_ERROR; | |
11254 | } | |
11255 | else if (m != MATCH_YES) | |
11256 | return MATCH_ERROR; | |
11257 | ||
11258 | if (gfc_match_eos () == MATCH_YES) | |
11259 | last = true; | |
11260 | if (!last && gfc_match_char (',') != MATCH_YES) | |
11261 | { | |
a4d9b221 | 11262 | gfc_error ("Expected %<,%> at %C"); |
34523524 DK |
11263 | return MATCH_ERROR; |
11264 | } | |
11265 | ||
11266 | if (gfc_get_symbol (name, module_ns, &sym)) | |
11267 | { | |
c4100eae | 11268 | gfc_error ("Unknown procedure name %qs at %C", name); |
34523524 DK |
11269 | return MATCH_ERROR; |
11270 | } | |
11271 | ||
11272 | /* Mark the symbol as module procedure. */ | |
11273 | if (sym->attr.proc != PROC_MODULE | |
524af0d6 | 11274 | && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL)) |
34523524 DK |
11275 | return MATCH_ERROR; |
11276 | ||
11277 | /* Check if we already have this symbol in the list, this is an error. */ | |
30b608eb | 11278 | for (f = block->f2k_derived->finalizers; f; f = f->next) |
f6fad28e | 11279 | if (f->proc_sym == sym) |
34523524 | 11280 | { |
546c8974 | 11281 | gfc_error ("%qs at %C is already defined as FINAL procedure", |
34523524 DK |
11282 | name); |
11283 | return MATCH_ERROR; | |
11284 | } | |
11285 | ||
11286 | /* Add this symbol to the list of finalizers. */ | |
30b608eb | 11287 | gcc_assert (block->f2k_derived); |
2050626a | 11288 | sym->refs++; |
ece3f663 | 11289 | f = XCNEW (gfc_finalizer); |
f6fad28e DK |
11290 | f->proc_sym = sym; |
11291 | f->proc_tree = NULL; | |
34523524 | 11292 | f->where = gfc_current_locus; |
30b608eb DK |
11293 | f->next = block->f2k_derived->finalizers; |
11294 | block->f2k_derived->finalizers = f; | |
34523524 DK |
11295 | |
11296 | first = false; | |
11297 | } | |
11298 | while (!last); | |
11299 | ||
11300 | return MATCH_YES; | |
11301 | } | |
08a6b8e0 TB |
11302 | |
11303 | ||
11304 | const ext_attr_t ext_attr_list[] = { | |
e7ac6a7c TB |
11305 | { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" }, |
11306 | { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" }, | |
11307 | { "cdecl", EXT_ATTR_CDECL, "cdecl" }, | |
11308 | { "stdcall", EXT_ATTR_STDCALL, "stdcall" }, | |
11309 | { "fastcall", EXT_ATTR_FASTCALL, "fastcall" }, | |
11310 | { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL }, | |
11311 | { NULL, EXT_ATTR_LAST, NULL } | |
08a6b8e0 TB |
11312 | }; |
11313 | ||
11314 | /* Match a !GCC$ ATTRIBUTES statement of the form: | |
11315 | !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ... | |
11316 | When we come here, we have already matched the !GCC$ ATTRIBUTES string. | |
11317 | ||
11318 | TODO: We should support all GCC attributes using the same syntax for | |
11319 | the attribute list, i.e. the list in C | |
11320 | __attributes(( attribute-list )) | |
11321 | matches then | |
11322 | !GCC$ ATTRIBUTES attribute-list :: | |
11323 | Cf. c-parser.c's c_parser_attributes; the data can then directly be | |
11324 | saved into a TREE. | |
11325 | ||
11326 | As there is absolutely no risk of confusion, we should never return | |
11327 | MATCH_NO. */ | |
11328 | match | |
11329 | gfc_match_gcc_attributes (void) | |
f5acf0f2 | 11330 | { |
08a6b8e0 TB |
11331 | symbol_attribute attr; |
11332 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
11333 | unsigned id; | |
11334 | gfc_symbol *sym; | |
11335 | match m; | |
11336 | ||
11337 | gfc_clear_attr (&attr); | |
11338 | for(;;) | |
11339 | { | |
11340 | char ch; | |
11341 | ||
11342 | if (gfc_match_name (name) != MATCH_YES) | |
11343 | return MATCH_ERROR; | |
11344 | ||
11345 | for (id = 0; id < EXT_ATTR_LAST; id++) | |
11346 | if (strcmp (name, ext_attr_list[id].name) == 0) | |
11347 | break; | |
11348 | ||
11349 | if (id == EXT_ATTR_LAST) | |
11350 | { | |
11351 | gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C"); | |
11352 | return MATCH_ERROR; | |
11353 | } | |
11354 | ||
524af0d6 | 11355 | if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus)) |
08a6b8e0 TB |
11356 | return MATCH_ERROR; |
11357 | ||
11358 | gfc_gobble_whitespace (); | |
11359 | ch = gfc_next_ascii_char (); | |
11360 | if (ch == ':') | |
11361 | { | |
11362 | /* This is the successful exit condition for the loop. */ | |
11363 | if (gfc_next_ascii_char () == ':') | |
11364 | break; | |
11365 | } | |
11366 | ||
11367 | if (ch == ',') | |
11368 | continue; | |
11369 | ||
11370 | goto syntax; | |
11371 | } | |
11372 | ||
11373 | if (gfc_match_eos () == MATCH_YES) | |
11374 | goto syntax; | |
11375 | ||
11376 | for(;;) | |
11377 | { | |
11378 | m = gfc_match_name (name); | |
11379 | if (m != MATCH_YES) | |
11380 | return m; | |
11381 | ||
11382 | if (find_special (name, &sym, true)) | |
11383 | return MATCH_ERROR; | |
f5acf0f2 | 11384 | |
08a6b8e0 TB |
11385 | sym->attr.ext_attr |= attr.ext_attr; |
11386 | ||
11387 | if (gfc_match_eos () == MATCH_YES) | |
11388 | break; | |
11389 | ||
11390 | if (gfc_match_char (',') != MATCH_YES) | |
11391 | goto syntax; | |
11392 | } | |
11393 | ||
11394 | return MATCH_YES; | |
11395 | ||
11396 | syntax: | |
11397 | gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C"); | |
11398 | return MATCH_ERROR; | |
11399 | } | |
170a8bd6 EB |
11400 | |
11401 | ||
11402 | /* Match a !GCC$ UNROLL statement of the form: | |
11403 | !GCC$ UNROLL n | |
11404 | ||
11405 | The parameter n is the number of times we are supposed to unroll. | |
11406 | ||
11407 | When we come here, we have already matched the !GCC$ UNROLL string. */ | |
11408 | match | |
11409 | gfc_match_gcc_unroll (void) | |
11410 | { | |
11411 | int value; | |
11412 | ||
11413 | if (gfc_match_small_int (&value) == MATCH_YES) | |
11414 | { | |
11415 | if (value < 0 || value > USHRT_MAX) | |
11416 | { | |
11417 | gfc_error ("%<GCC unroll%> directive requires a" | |
11418 | " non-negative integral constant" | |
11419 | " less than or equal to %u at %C", | |
11420 | USHRT_MAX | |
11421 | ); | |
11422 | return MATCH_ERROR; | |
11423 | } | |
11424 | if (gfc_match_eos () == MATCH_YES) | |
11425 | { | |
11426 | directive_unroll = value == 0 ? 1 : value; | |
11427 | return MATCH_YES; | |
11428 | } | |
11429 | } | |
11430 | ||
11431 | gfc_error ("Syntax error in !GCC$ UNROLL directive at %C"); | |
11432 | return MATCH_ERROR; | |
11433 | } | |
facf0354 | 11434 | |
e8cecccc | 11435 | /* Match a !GCC$ builtin (b) attributes simd flags if('target') form: |
facf0354 ML |
11436 | |
11437 | The parameter b is name of a middle-end built-in. | |
e8cecccc ML |
11438 | FLAGS is optional and must be one of: |
11439 | - (inbranch) | |
11440 | - (notinbranch) | |
11441 | ||
11442 | IF('target') is optional and TARGET is a name of a multilib ABI. | |
facf0354 ML |
11443 | |
11444 | When we come here, we have already matched the !GCC$ builtin string. */ | |
e8cecccc | 11445 | |
facf0354 ML |
11446 | match |
11447 | gfc_match_gcc_builtin (void) | |
11448 | { | |
11449 | char builtin[GFC_MAX_SYMBOL_LEN + 1]; | |
e8cecccc | 11450 | char target[GFC_MAX_SYMBOL_LEN + 1]; |
facf0354 ML |
11451 | |
11452 | if (gfc_match (" ( %n ) attributes simd", builtin) != MATCH_YES) | |
11453 | return MATCH_ERROR; | |
11454 | ||
11455 | gfc_simd_clause clause = SIMD_NONE; | |
11456 | if (gfc_match (" ( notinbranch ) ") == MATCH_YES) | |
11457 | clause = SIMD_NOTINBRANCH; | |
11458 | else if (gfc_match (" ( inbranch ) ") == MATCH_YES) | |
11459 | clause = SIMD_INBRANCH; | |
11460 | ||
e8cecccc ML |
11461 | if (gfc_match (" if ( '%n' ) ", target) == MATCH_YES) |
11462 | { | |
11463 | const char *abi = targetm.get_multilib_abi_name (); | |
11464 | if (abi == NULL || strcmp (abi, target) != 0) | |
11465 | return MATCH_YES; | |
11466 | } | |
11467 | ||
facf0354 ML |
11468 | if (gfc_vectorized_builtins == NULL) |
11469 | gfc_vectorized_builtins = new hash_map<nofree_string_hash, int> (); | |
11470 | ||
11471 | char *r = XNEWVEC (char, strlen (builtin) + 32); | |
11472 | sprintf (r, "__builtin_%s", builtin); | |
11473 | ||
11474 | bool existed; | |
11475 | int &value = gfc_vectorized_builtins->get_or_insert (r, &existed); | |
11476 | value |= clause; | |
11477 | if (existed) | |
11478 | free (r); | |
11479 | ||
11480 | return MATCH_YES; | |
11481 | } |