]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Declaration statement matcher |
ec378180 | 2 | Copyright (C) 2002, 2004, 2005 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 | |
9 | Software Foundation; either version 2, or (at your option) any later | |
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 | |
9fc4d79b | 18 | along with GCC; see the file COPYING. If not, write to the Free |
ab57747b KC |
19 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA |
20 | 02110-1301, USA. */ | |
6de9cd9a DN |
21 | |
22 | ||
23 | #include "config.h" | |
d22e4895 | 24 | #include "system.h" |
6de9cd9a DN |
25 | #include "gfortran.h" |
26 | #include "match.h" | |
27 | #include "parse.h" | |
6de9cd9a DN |
28 | |
29 | ||
2054fc29 | 30 | /* This flag is set if an old-style length selector is matched |
6de9cd9a DN |
31 | during a type-declaration statement. */ |
32 | ||
33 | static int old_char_selector; | |
34 | ||
46fa431d | 35 | /* When variables acquire types and attributes from a declaration |
6de9cd9a DN |
36 | statement, they get them from the following static variables. The |
37 | first part of a declaration sets these variables and the second | |
38 | part copies these into symbol structures. */ | |
39 | ||
40 | static gfc_typespec current_ts; | |
41 | ||
42 | static symbol_attribute current_attr; | |
43 | static gfc_array_spec *current_as; | |
44 | static int colon_seen; | |
45 | ||
25d8f0a2 TS |
46 | /* Initializer of the previous enumerator. */ |
47 | ||
48 | static gfc_expr *last_initializer; | |
49 | ||
50 | /* History of all the enumerators is maintained, so that | |
51 | kind values of all the enumerators could be updated depending | |
52 | upon the maximum initialized value. */ | |
53 | ||
54 | typedef struct enumerator_history | |
55 | { | |
56 | gfc_symbol *sym; | |
57 | gfc_expr *initializer; | |
58 | struct enumerator_history *next; | |
59 | } | |
60 | enumerator_history; | |
61 | ||
62 | /* Header of enum history chain. */ | |
63 | ||
64 | static enumerator_history *enum_history = NULL; | |
65 | ||
66 | /* Pointer of enum history node containing largest initializer. */ | |
67 | ||
68 | static enumerator_history *max_enum = NULL; | |
69 | ||
6de9cd9a DN |
70 | /* gfc_new_block points to the symbol of a newly matched block. */ |
71 | ||
72 | gfc_symbol *gfc_new_block; | |
73 | ||
74 | ||
294fbfc8 TS |
75 | /********************* DATA statement subroutines *********************/ |
76 | ||
77 | /* Free a gfc_data_variable structure and everything beneath it. */ | |
78 | ||
79 | static void | |
80 | free_variable (gfc_data_variable * p) | |
81 | { | |
82 | gfc_data_variable *q; | |
83 | ||
84 | for (; p; p = q) | |
85 | { | |
86 | q = p->next; | |
87 | gfc_free_expr (p->expr); | |
88 | gfc_free_iterator (&p->iter, 0); | |
89 | free_variable (p->list); | |
90 | ||
91 | gfc_free (p); | |
92 | } | |
93 | } | |
94 | ||
95 | ||
96 | /* Free a gfc_data_value structure and everything beneath it. */ | |
97 | ||
98 | static void | |
99 | free_value (gfc_data_value * p) | |
100 | { | |
101 | gfc_data_value *q; | |
102 | ||
103 | for (; p; p = q) | |
104 | { | |
105 | q = p->next; | |
106 | gfc_free_expr (p->expr); | |
107 | gfc_free (p); | |
108 | } | |
109 | } | |
110 | ||
111 | ||
112 | /* Free a list of gfc_data structures. */ | |
113 | ||
114 | void | |
115 | gfc_free_data (gfc_data * p) | |
116 | { | |
117 | gfc_data *q; | |
118 | ||
119 | for (; p; p = q) | |
120 | { | |
121 | q = p->next; | |
122 | ||
123 | free_variable (p->var); | |
124 | free_value (p->value); | |
125 | ||
126 | gfc_free (p); | |
127 | } | |
128 | } | |
129 | ||
130 | ||
131 | static match var_element (gfc_data_variable *); | |
132 | ||
133 | /* Match a list of variables terminated by an iterator and a right | |
134 | parenthesis. */ | |
135 | ||
136 | static match | |
137 | var_list (gfc_data_variable * parent) | |
138 | { | |
139 | gfc_data_variable *tail, var; | |
140 | match m; | |
141 | ||
142 | m = var_element (&var); | |
143 | if (m == MATCH_ERROR) | |
144 | return MATCH_ERROR; | |
145 | if (m == MATCH_NO) | |
146 | goto syntax; | |
147 | ||
148 | tail = gfc_get_data_variable (); | |
149 | *tail = var; | |
150 | ||
151 | parent->list = tail; | |
152 | ||
153 | for (;;) | |
154 | { | |
155 | if (gfc_match_char (',') != MATCH_YES) | |
156 | goto syntax; | |
157 | ||
158 | m = gfc_match_iterator (&parent->iter, 1); | |
159 | if (m == MATCH_YES) | |
160 | break; | |
161 | if (m == MATCH_ERROR) | |
162 | return MATCH_ERROR; | |
163 | ||
164 | m = var_element (&var); | |
165 | if (m == MATCH_ERROR) | |
166 | return MATCH_ERROR; | |
167 | if (m == MATCH_NO) | |
168 | goto syntax; | |
169 | ||
170 | tail->next = gfc_get_data_variable (); | |
171 | tail = tail->next; | |
172 | ||
173 | *tail = var; | |
174 | } | |
175 | ||
176 | if (gfc_match_char (')') != MATCH_YES) | |
177 | goto syntax; | |
178 | return MATCH_YES; | |
179 | ||
180 | syntax: | |
181 | gfc_syntax_error (ST_DATA); | |
182 | return MATCH_ERROR; | |
183 | } | |
184 | ||
185 | ||
186 | /* Match a single element in a data variable list, which can be a | |
187 | variable-iterator list. */ | |
188 | ||
189 | static match | |
190 | var_element (gfc_data_variable * new) | |
191 | { | |
192 | match m; | |
193 | gfc_symbol *sym; | |
194 | ||
195 | memset (new, 0, sizeof (gfc_data_variable)); | |
196 | ||
197 | if (gfc_match_char ('(') == MATCH_YES) | |
198 | return var_list (new); | |
199 | ||
200 | m = gfc_match_variable (&new->expr, 0); | |
201 | if (m != MATCH_YES) | |
202 | return m; | |
203 | ||
204 | sym = new->expr->symtree->n.sym; | |
205 | ||
4075a94e | 206 | if (!sym->attr.function && gfc_current_ns->parent && gfc_current_ns->parent == sym->ns) |
294fbfc8 | 207 | { |
4075a94e PT |
208 | gfc_error ("Host associated variable '%s' may not be in the DATA " |
209 | "statement at %C.", sym->name); | |
294fbfc8 TS |
210 | return MATCH_ERROR; |
211 | } | |
212 | ||
4075a94e PT |
213 | if (gfc_current_state () != COMP_BLOCK_DATA |
214 | && sym->attr.in_common | |
215 | && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of " | |
216 | "common block variable '%s' in DATA statement at %C", | |
217 | sym->name) == FAILURE) | |
218 | return MATCH_ERROR; | |
294fbfc8 | 219 | |
231b2fcc | 220 | if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE) |
294fbfc8 TS |
221 | return MATCH_ERROR; |
222 | ||
223 | return MATCH_YES; | |
224 | } | |
225 | ||
226 | ||
227 | /* Match the top-level list of data variables. */ | |
228 | ||
229 | static match | |
230 | top_var_list (gfc_data * d) | |
231 | { | |
232 | gfc_data_variable var, *tail, *new; | |
233 | match m; | |
234 | ||
235 | tail = NULL; | |
236 | ||
237 | for (;;) | |
238 | { | |
239 | m = var_element (&var); | |
240 | if (m == MATCH_NO) | |
241 | goto syntax; | |
242 | if (m == MATCH_ERROR) | |
243 | return MATCH_ERROR; | |
244 | ||
245 | new = gfc_get_data_variable (); | |
246 | *new = var; | |
247 | ||
248 | if (tail == NULL) | |
249 | d->var = new; | |
250 | else | |
251 | tail->next = new; | |
252 | ||
253 | tail = new; | |
254 | ||
255 | if (gfc_match_char ('/') == MATCH_YES) | |
256 | break; | |
257 | if (gfc_match_char (',') != MATCH_YES) | |
258 | goto syntax; | |
259 | } | |
260 | ||
261 | return MATCH_YES; | |
262 | ||
263 | syntax: | |
264 | gfc_syntax_error (ST_DATA); | |
265 | return MATCH_ERROR; | |
266 | } | |
267 | ||
268 | ||
269 | static match | |
270 | match_data_constant (gfc_expr ** result) | |
271 | { | |
272 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
273 | gfc_symbol *sym; | |
274 | gfc_expr *expr; | |
275 | match m; | |
276 | ||
277 | m = gfc_match_literal_constant (&expr, 1); | |
278 | if (m == MATCH_YES) | |
279 | { | |
280 | *result = expr; | |
281 | return MATCH_YES; | |
282 | } | |
283 | ||
284 | if (m == MATCH_ERROR) | |
285 | return MATCH_ERROR; | |
286 | ||
287 | m = gfc_match_null (result); | |
288 | if (m != MATCH_NO) | |
289 | return m; | |
290 | ||
291 | m = gfc_match_name (name); | |
292 | if (m != MATCH_YES) | |
293 | return m; | |
294 | ||
295 | if (gfc_find_symbol (name, NULL, 1, &sym)) | |
296 | return MATCH_ERROR; | |
297 | ||
298 | if (sym == NULL | |
299 | || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED)) | |
300 | { | |
301 | gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C", | |
302 | name); | |
303 | return MATCH_ERROR; | |
304 | } | |
305 | else if (sym->attr.flavor == FL_DERIVED) | |
306 | return gfc_match_structure_constructor (sym, result); | |
307 | ||
308 | *result = gfc_copy_expr (sym->value); | |
309 | return MATCH_YES; | |
310 | } | |
311 | ||
312 | ||
313 | /* Match a list of values in a DATA statement. The leading '/' has | |
314 | already been seen at this point. */ | |
315 | ||
316 | static match | |
317 | top_val_list (gfc_data * data) | |
318 | { | |
319 | gfc_data_value *new, *tail; | |
320 | gfc_expr *expr; | |
321 | const char *msg; | |
322 | match m; | |
323 | ||
324 | tail = NULL; | |
325 | ||
326 | for (;;) | |
327 | { | |
328 | m = match_data_constant (&expr); | |
329 | if (m == MATCH_NO) | |
330 | goto syntax; | |
331 | if (m == MATCH_ERROR) | |
332 | return MATCH_ERROR; | |
333 | ||
334 | new = gfc_get_data_value (); | |
335 | ||
336 | if (tail == NULL) | |
337 | data->value = new; | |
338 | else | |
339 | tail->next = new; | |
340 | ||
341 | tail = new; | |
342 | ||
343 | if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES) | |
344 | { | |
345 | tail->expr = expr; | |
346 | tail->repeat = 1; | |
347 | } | |
348 | else | |
349 | { | |
350 | signed int tmp; | |
351 | msg = gfc_extract_int (expr, &tmp); | |
352 | gfc_free_expr (expr); | |
353 | if (msg != NULL) | |
354 | { | |
355 | gfc_error (msg); | |
356 | return MATCH_ERROR; | |
357 | } | |
358 | tail->repeat = tmp; | |
359 | ||
360 | m = match_data_constant (&tail->expr); | |
361 | if (m == MATCH_NO) | |
362 | goto syntax; | |
363 | if (m == MATCH_ERROR) | |
364 | return MATCH_ERROR; | |
365 | } | |
366 | ||
367 | if (gfc_match_char ('/') == MATCH_YES) | |
368 | break; | |
369 | if (gfc_match_char (',') == MATCH_NO) | |
370 | goto syntax; | |
371 | } | |
372 | ||
373 | return MATCH_YES; | |
374 | ||
375 | syntax: | |
376 | gfc_syntax_error (ST_DATA); | |
377 | return MATCH_ERROR; | |
378 | } | |
379 | ||
380 | ||
381 | /* Matches an old style initialization. */ | |
382 | ||
383 | static match | |
384 | match_old_style_init (const char *name) | |
385 | { | |
386 | match m; | |
387 | gfc_symtree *st; | |
388 | gfc_data *newdata; | |
389 | ||
390 | /* Set up data structure to hold initializers. */ | |
391 | gfc_find_sym_tree (name, NULL, 0, &st); | |
392 | ||
393 | newdata = gfc_get_data (); | |
394 | newdata->var = gfc_get_data_variable (); | |
395 | newdata->var->expr = gfc_get_variable_expr (st); | |
396 | ||
397 | /* Match initial value list. This also eats the terminal | |
398 | '/'. */ | |
399 | m = top_val_list (newdata); | |
400 | if (m != MATCH_YES) | |
401 | { | |
402 | gfc_free (newdata); | |
403 | return m; | |
404 | } | |
405 | ||
406 | if (gfc_pure (NULL)) | |
407 | { | |
408 | gfc_error ("Initialization at %C is not allowed in a PURE procedure"); | |
409 | gfc_free (newdata); | |
410 | return MATCH_ERROR; | |
411 | } | |
412 | ||
413 | /* Chain in namespace list of DATA initializers. */ | |
414 | newdata->next = gfc_current_ns->data; | |
415 | gfc_current_ns->data = newdata; | |
416 | ||
417 | return m; | |
418 | } | |
419 | ||
420 | /* Match the stuff following a DATA statement. If ERROR_FLAG is set, | |
13795658 | 421 | we are matching a DATA statement and are therefore issuing an error |
294fbfc8 | 422 | if we encounter something unexpected, if not, we're trying to match |
69de3b83 | 423 | an old-style initialization expression of the form INTEGER I /2/. */ |
294fbfc8 TS |
424 | |
425 | match | |
426 | gfc_match_data (void) | |
427 | { | |
428 | gfc_data *new; | |
429 | match m; | |
430 | ||
431 | for (;;) | |
432 | { | |
433 | new = gfc_get_data (); | |
434 | new->where = gfc_current_locus; | |
435 | ||
436 | m = top_var_list (new); | |
437 | if (m != MATCH_YES) | |
438 | goto cleanup; | |
439 | ||
440 | m = top_val_list (new); | |
441 | if (m != MATCH_YES) | |
442 | goto cleanup; | |
443 | ||
444 | new->next = gfc_current_ns->data; | |
445 | gfc_current_ns->data = new; | |
446 | ||
447 | if (gfc_match_eos () == MATCH_YES) | |
448 | break; | |
449 | ||
450 | gfc_match_char (','); /* Optional comma */ | |
451 | } | |
452 | ||
453 | if (gfc_pure (NULL)) | |
454 | { | |
455 | gfc_error ("DATA statement at %C is not allowed in a PURE procedure"); | |
456 | return MATCH_ERROR; | |
457 | } | |
458 | ||
459 | return MATCH_YES; | |
460 | ||
461 | cleanup: | |
462 | gfc_free_data (new); | |
463 | return MATCH_ERROR; | |
464 | } | |
465 | ||
466 | ||
467 | /************************ Declaration statements *********************/ | |
468 | ||
6de9cd9a DN |
469 | /* Match an intent specification. Since this can only happen after an |
470 | INTENT word, a legal intent-spec must follow. */ | |
471 | ||
472 | static sym_intent | |
473 | match_intent_spec (void) | |
474 | { | |
475 | ||
476 | if (gfc_match (" ( in out )") == MATCH_YES) | |
477 | return INTENT_INOUT; | |
478 | if (gfc_match (" ( in )") == MATCH_YES) | |
479 | return INTENT_IN; | |
480 | if (gfc_match (" ( out )") == MATCH_YES) | |
481 | return INTENT_OUT; | |
482 | ||
483 | gfc_error ("Bad INTENT specification at %C"); | |
484 | return INTENT_UNKNOWN; | |
485 | } | |
486 | ||
487 | ||
488 | /* Matches a character length specification, which is either a | |
489 | specification expression or a '*'. */ | |
490 | ||
491 | static match | |
492 | char_len_param_value (gfc_expr ** expr) | |
493 | { | |
494 | ||
495 | if (gfc_match_char ('*') == MATCH_YES) | |
496 | { | |
497 | *expr = NULL; | |
498 | return MATCH_YES; | |
499 | } | |
500 | ||
501 | return gfc_match_expr (expr); | |
502 | } | |
503 | ||
504 | ||
505 | /* A character length is a '*' followed by a literal integer or a | |
506 | char_len_param_value in parenthesis. */ | |
507 | ||
508 | static match | |
509 | match_char_length (gfc_expr ** expr) | |
510 | { | |
8a8f7eca | 511 | int length, cnt; |
6de9cd9a DN |
512 | match m; |
513 | ||
514 | m = gfc_match_char ('*'); | |
515 | if (m != MATCH_YES) | |
516 | return m; | |
517 | ||
8a8f7eca SK |
518 | /* cnt is unused, here. */ |
519 | m = gfc_match_small_literal_int (&length, &cnt); | |
6de9cd9a DN |
520 | if (m == MATCH_ERROR) |
521 | return m; | |
522 | ||
523 | if (m == MATCH_YES) | |
524 | { | |
525 | *expr = gfc_int_expr (length); | |
526 | return m; | |
527 | } | |
528 | ||
529 | if (gfc_match_char ('(') == MATCH_NO) | |
530 | goto syntax; | |
531 | ||
532 | m = char_len_param_value (expr); | |
533 | if (m == MATCH_ERROR) | |
534 | return m; | |
535 | if (m == MATCH_NO) | |
536 | goto syntax; | |
537 | ||
538 | if (gfc_match_char (')') == MATCH_NO) | |
539 | { | |
540 | gfc_free_expr (*expr); | |
541 | *expr = NULL; | |
542 | goto syntax; | |
543 | } | |
544 | ||
545 | return MATCH_YES; | |
546 | ||
547 | syntax: | |
548 | gfc_error ("Syntax error in character length specification at %C"); | |
549 | return MATCH_ERROR; | |
550 | } | |
551 | ||
552 | ||
9e35b386 EE |
553 | /* Special subroutine for finding a symbol. Check if the name is found |
554 | in the current name space. If not, and we're compiling a function or | |
555 | subroutine and the parent compilation unit is an interface, then check | |
556 | to see if the name we've been given is the name of the interface | |
557 | (located in another namespace). */ | |
6de9cd9a DN |
558 | |
559 | static int | |
560 | find_special (const char *name, gfc_symbol ** result) | |
561 | { | |
562 | gfc_state_data *s; | |
9e35b386 | 563 | int i; |
6de9cd9a | 564 | |
9e35b386 EE |
565 | i = gfc_get_symbol (name, NULL, result); |
566 | if (i==0) | |
567 | goto end; | |
568 | ||
6de9cd9a DN |
569 | if (gfc_current_state () != COMP_SUBROUTINE |
570 | && gfc_current_state () != COMP_FUNCTION) | |
9e35b386 | 571 | goto end; |
6de9cd9a DN |
572 | |
573 | s = gfc_state_stack->previous; | |
574 | if (s == NULL) | |
9e35b386 | 575 | goto end; |
6de9cd9a DN |
576 | |
577 | if (s->state != COMP_INTERFACE) | |
9e35b386 | 578 | goto end; |
6de9cd9a | 579 | if (s->sym == NULL) |
9e35b386 | 580 | goto end; /* Nameless interface */ |
6de9cd9a DN |
581 | |
582 | if (strcmp (name, s->sym->name) == 0) | |
583 | { | |
584 | *result = s->sym; | |
585 | return 0; | |
586 | } | |
587 | ||
9e35b386 EE |
588 | end: |
589 | return i; | |
6de9cd9a DN |
590 | } |
591 | ||
592 | ||
593 | /* Special subroutine for getting a symbol node associated with a | |
594 | procedure name, used in SUBROUTINE and FUNCTION statements. The | |
595 | symbol is created in the parent using with symtree node in the | |
596 | child unit pointing to the symbol. If the current namespace has no | |
597 | parent, then the symbol is just created in the current unit. */ | |
598 | ||
599 | static int | |
600 | get_proc_name (const char *name, gfc_symbol ** result) | |
601 | { | |
602 | gfc_symtree *st; | |
603 | gfc_symbol *sym; | |
604 | int rc; | |
605 | ||
606 | if (gfc_current_ns->parent == NULL) | |
607 | return gfc_get_symbol (name, NULL, result); | |
608 | ||
609 | rc = gfc_get_symbol (name, gfc_current_ns->parent, result); | |
610 | if (*result == NULL) | |
611 | return rc; | |
612 | ||
3d79abbd | 613 | /* ??? Deal with ENTRY problem */ |
6de9cd9a DN |
614 | |
615 | st = gfc_new_symtree (&gfc_current_ns->sym_root, name); | |
616 | ||
617 | sym = *result; | |
618 | st->n.sym = sym; | |
619 | sym->refs++; | |
620 | ||
621 | /* See if the procedure should be a module procedure */ | |
622 | ||
623 | if (sym->ns->proc_name != NULL | |
624 | && sym->ns->proc_name->attr.flavor == FL_MODULE | |
625 | && sym->attr.proc != PROC_MODULE | |
231b2fcc TS |
626 | && gfc_add_procedure (&sym->attr, PROC_MODULE, |
627 | sym->name, NULL) == FAILURE) | |
6de9cd9a DN |
628 | rc = 2; |
629 | ||
630 | return rc; | |
631 | } | |
632 | ||
633 | ||
634 | /* Function called by variable_decl() that adds a name to the symbol | |
635 | table. */ | |
636 | ||
637 | static try | |
638 | build_sym (const char *name, gfc_charlen * cl, | |
639 | gfc_array_spec ** as, locus * var_locus) | |
640 | { | |
641 | symbol_attribute attr; | |
642 | gfc_symbol *sym; | |
643 | ||
9e35b386 EE |
644 | /* if (find_special (name, &sym)) */ |
645 | if (gfc_get_symbol (name, NULL, &sym)) | |
6de9cd9a DN |
646 | return FAILURE; |
647 | ||
648 | /* Start updating the symbol table. Add basic type attribute | |
649 | if present. */ | |
650 | if (current_ts.type != BT_UNKNOWN | |
651 | &&(sym->attr.implicit_type == 0 | |
652 | || !gfc_compare_types (&sym->ts, ¤t_ts)) | |
653 | && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE) | |
654 | return FAILURE; | |
655 | ||
656 | if (sym->ts.type == BT_CHARACTER) | |
657 | sym->ts.cl = cl; | |
658 | ||
659 | /* Add dimension attribute if present. */ | |
660 | if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE) | |
661 | return FAILURE; | |
662 | *as = NULL; | |
663 | ||
664 | /* Add attribute to symbol. The copy is so that we can reset the | |
665 | dimension attribute. */ | |
666 | attr = current_attr; | |
667 | attr.dimension = 0; | |
668 | ||
669 | if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE) | |
670 | return FAILURE; | |
671 | ||
672 | return SUCCESS; | |
673 | } | |
674 | ||
df7cc9b5 FW |
675 | /* Set character constant to the given length. The constant will be padded or |
676 | truncated. */ | |
677 | ||
678 | void | |
679 | gfc_set_constant_character_len (int len, gfc_expr * expr) | |
680 | { | |
681 | char * s; | |
682 | int slen; | |
683 | ||
684 | gcc_assert (expr->expr_type == EXPR_CONSTANT); | |
685 | gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1); | |
686 | ||
687 | slen = expr->value.character.length; | |
688 | if (len != slen) | |
689 | { | |
690 | s = gfc_getmem (len); | |
691 | memcpy (s, expr->value.character.string, MIN (len, slen)); | |
692 | if (len > slen) | |
693 | memset (&s[slen], ' ', len - slen); | |
694 | gfc_free (expr->value.character.string); | |
695 | expr->value.character.string = s; | |
696 | expr->value.character.length = len; | |
697 | } | |
698 | } | |
6de9cd9a | 699 | |
25d8f0a2 | 700 | |
831d7813 | 701 | /* Function to create and update the enumerator history |
25d8f0a2 TS |
702 | using the information passed as arguments. |
703 | Pointer "max_enum" is also updated, to point to | |
704 | enum history node containing largest initializer. | |
705 | ||
706 | SYM points to the symbol node of enumerator. | |
707 | INIT points to its enumerator value. */ | |
708 | ||
709 | static void | |
710 | create_enum_history(gfc_symbol *sym, gfc_expr *init) | |
711 | { | |
712 | enumerator_history *new_enum_history; | |
713 | gcc_assert (sym != NULL && init != NULL); | |
714 | ||
715 | new_enum_history = gfc_getmem (sizeof (enumerator_history)); | |
716 | ||
717 | new_enum_history->sym = sym; | |
718 | new_enum_history->initializer = init; | |
719 | new_enum_history->next = NULL; | |
720 | ||
721 | if (enum_history == NULL) | |
722 | { | |
723 | enum_history = new_enum_history; | |
724 | max_enum = enum_history; | |
725 | } | |
726 | else | |
727 | { | |
728 | new_enum_history->next = enum_history; | |
729 | enum_history = new_enum_history; | |
730 | ||
731 | if (mpz_cmp (max_enum->initializer->value.integer, | |
732 | new_enum_history->initializer->value.integer) < 0) | |
733 | max_enum = new_enum_history; | |
734 | } | |
735 | } | |
736 | ||
737 | ||
738 | /* Function to free enum kind history. */ | |
739 | ||
740 | void | |
741 | gfc_free_enum_history(void) | |
742 | { | |
743 | enumerator_history *current = enum_history; | |
744 | enumerator_history *next; | |
745 | ||
746 | while (current != NULL) | |
747 | { | |
748 | next = current->next; | |
749 | gfc_free (current); | |
750 | current = next; | |
751 | } | |
752 | max_enum = NULL; | |
753 | enum_history = NULL; | |
754 | } | |
755 | ||
756 | ||
6de9cd9a DN |
757 | /* Function called by variable_decl() that adds an initialization |
758 | expression to a symbol. */ | |
759 | ||
760 | static try | |
761 | add_init_expr_to_sym (const char *name, gfc_expr ** initp, | |
762 | locus * var_locus) | |
763 | { | |
764 | symbol_attribute attr; | |
765 | gfc_symbol *sym; | |
766 | gfc_expr *init; | |
767 | ||
768 | init = *initp; | |
769 | if (find_special (name, &sym)) | |
770 | return FAILURE; | |
771 | ||
772 | attr = sym->attr; | |
773 | ||
774 | /* If this symbol is confirming an implicit parameter type, | |
775 | then an initialization expression is not allowed. */ | |
776 | if (attr.flavor == FL_PARAMETER | |
777 | && sym->value != NULL | |
778 | && *initp != NULL) | |
779 | { | |
780 | gfc_error ("Initializer not allowed for PARAMETER '%s' at %C", | |
781 | sym->name); | |
782 | return FAILURE; | |
783 | } | |
784 | ||
c8e20bd0 TS |
785 | if (attr.in_common |
786 | && !attr.data | |
787 | && *initp != NULL) | |
788 | { | |
789 | gfc_error ("Initializer not allowed for COMMON variable '%s' at %C", | |
790 | sym->name); | |
791 | return FAILURE; | |
792 | } | |
793 | ||
6de9cd9a DN |
794 | if (init == NULL) |
795 | { | |
796 | /* An initializer is required for PARAMETER declarations. */ | |
797 | if (attr.flavor == FL_PARAMETER) | |
798 | { | |
799 | gfc_error ("PARAMETER at %L is missing an initializer", var_locus); | |
800 | return FAILURE; | |
801 | } | |
802 | } | |
803 | else | |
804 | { | |
805 | /* If a variable appears in a DATA block, it cannot have an | |
1de8a836 | 806 | initializer. */ |
6de9cd9a DN |
807 | if (sym->attr.data) |
808 | { | |
809 | gfc_error | |
810 | ("Variable '%s' at %C with an initializer already appears " | |
811 | "in a DATA statement", sym->name); | |
812 | return FAILURE; | |
813 | } | |
814 | ||
75d17889 TS |
815 | /* Check if the assignment can happen. This has to be put off |
816 | until later for a derived type variable. */ | |
6de9cd9a DN |
817 | if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED |
818 | && gfc_check_assign_symbol (sym, init) == FAILURE) | |
819 | return FAILURE; | |
820 | ||
df7cc9b5 FW |
821 | if (sym->ts.type == BT_CHARACTER && sym->ts.cl) |
822 | { | |
823 | /* Update symbol character length according initializer. */ | |
824 | if (sym->ts.cl->length == NULL) | |
825 | { | |
4213f93b PT |
826 | /* If there are multiple CHARACTER variables declared on |
827 | the same line, we don't want them to share the same | |
828 | length. */ | |
829 | sym->ts.cl = gfc_get_charlen (); | |
830 | sym->ts.cl->next = gfc_current_ns->cl_list; | |
831 | gfc_current_ns->cl_list = sym->ts.cl; | |
832 | ||
df7cc9b5 FW |
833 | if (init->expr_type == EXPR_CONSTANT) |
834 | sym->ts.cl->length = | |
835 | gfc_int_expr (init->value.character.length); | |
836 | else if (init->expr_type == EXPR_ARRAY) | |
837 | sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length); | |
838 | } | |
839 | /* Update initializer character length according symbol. */ | |
840 | else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT) | |
841 | { | |
842 | int len = mpz_get_si (sym->ts.cl->length->value.integer); | |
843 | gfc_constructor * p; | |
844 | ||
845 | if (init->expr_type == EXPR_CONSTANT) | |
846 | gfc_set_constant_character_len (len, init); | |
847 | else if (init->expr_type == EXPR_ARRAY) | |
848 | { | |
849 | gfc_free_expr (init->ts.cl->length); | |
850 | init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length); | |
851 | for (p = init->value.constructor; p; p = p->next) | |
852 | gfc_set_constant_character_len (len, p->expr); | |
853 | } | |
854 | } | |
855 | } | |
856 | ||
6de9cd9a DN |
857 | /* Add initializer. Make sure we keep the ranks sane. */ |
858 | if (sym->attr.dimension && init->rank == 0) | |
859 | init->rank = sym->as->rank; | |
860 | ||
861 | sym->value = init; | |
862 | *initp = NULL; | |
863 | } | |
864 | ||
25d8f0a2 TS |
865 | /* Maintain enumerator history. */ |
866 | if (gfc_current_state () == COMP_ENUM) | |
867 | create_enum_history (sym, init); | |
868 | ||
6de9cd9a DN |
869 | return SUCCESS; |
870 | } | |
871 | ||
872 | ||
873 | /* Function called by variable_decl() that adds a name to a structure | |
874 | being built. */ | |
875 | ||
876 | static try | |
877 | build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init, | |
878 | gfc_array_spec ** as) | |
879 | { | |
880 | gfc_component *c; | |
881 | ||
882 | /* If the current symbol is of the same derived type that we're | |
883 | constructing, it must have the pointer attribute. */ | |
884 | if (current_ts.type == BT_DERIVED | |
885 | && current_ts.derived == gfc_current_block () | |
886 | && current_attr.pointer == 0) | |
887 | { | |
888 | gfc_error ("Component at %C must have the POINTER attribute"); | |
889 | return FAILURE; | |
890 | } | |
891 | ||
892 | if (gfc_current_block ()->attr.pointer | |
893 | && (*as)->rank != 0) | |
894 | { | |
895 | if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT) | |
896 | { | |
897 | gfc_error ("Array component of structure at %C must have explicit " | |
898 | "or deferred shape"); | |
899 | return FAILURE; | |
900 | } | |
901 | } | |
902 | ||
903 | if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE) | |
904 | return FAILURE; | |
905 | ||
906 | c->ts = current_ts; | |
907 | c->ts.cl = cl; | |
908 | gfc_set_component_attr (c, ¤t_attr); | |
909 | ||
910 | c->initializer = *init; | |
911 | *init = NULL; | |
912 | ||
913 | c->as = *as; | |
914 | if (c->as != NULL) | |
915 | c->dimension = 1; | |
916 | *as = NULL; | |
917 | ||
918 | /* Check array components. */ | |
919 | if (!c->dimension) | |
920 | return SUCCESS; | |
921 | ||
922 | if (c->pointer) | |
923 | { | |
924 | if (c->as->type != AS_DEFERRED) | |
925 | { | |
926 | gfc_error ("Pointer array component of structure at %C " | |
927 | "must have a deferred shape"); | |
928 | return FAILURE; | |
929 | } | |
930 | } | |
931 | else | |
932 | { | |
933 | if (c->as->type != AS_EXPLICIT) | |
934 | { | |
935 | gfc_error | |
936 | ("Array component of structure at %C must have an explicit " | |
937 | "shape"); | |
938 | return FAILURE; | |
939 | } | |
940 | } | |
941 | ||
942 | return SUCCESS; | |
943 | } | |
944 | ||
945 | ||
946 | /* Match a 'NULL()', and possibly take care of some side effects. */ | |
947 | ||
948 | match | |
949 | gfc_match_null (gfc_expr ** result) | |
950 | { | |
951 | gfc_symbol *sym; | |
952 | gfc_expr *e; | |
953 | match m; | |
954 | ||
955 | m = gfc_match (" null ( )"); | |
956 | if (m != MATCH_YES) | |
957 | return m; | |
958 | ||
959 | /* The NULL symbol now has to be/become an intrinsic function. */ | |
960 | if (gfc_get_symbol ("null", NULL, &sym)) | |
961 | { | |
962 | gfc_error ("NULL() initialization at %C is ambiguous"); | |
963 | return MATCH_ERROR; | |
964 | } | |
965 | ||
966 | gfc_intrinsic_symbol (sym); | |
967 | ||
968 | if (sym->attr.proc != PROC_INTRINSIC | |
231b2fcc TS |
969 | && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC, |
970 | sym->name, NULL) == FAILURE | |
971 | || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)) | |
6de9cd9a DN |
972 | return MATCH_ERROR; |
973 | ||
974 | e = gfc_get_expr (); | |
63645982 | 975 | e->where = gfc_current_locus; |
6de9cd9a DN |
976 | e->expr_type = EXPR_NULL; |
977 | e->ts.type = BT_UNKNOWN; | |
978 | ||
979 | *result = e; | |
980 | ||
981 | return MATCH_YES; | |
982 | } | |
983 | ||
984 | ||
6de9cd9a DN |
985 | /* Match a variable name with an optional initializer. When this |
986 | subroutine is called, a variable is expected to be parsed next. | |
987 | Depending on what is happening at the moment, updates either the | |
988 | symbol table or the current interface. */ | |
989 | ||
990 | static match | |
949d5b72 | 991 | variable_decl (int elem) |
6de9cd9a DN |
992 | { |
993 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
994 | gfc_expr *initializer, *char_len; | |
995 | gfc_array_spec *as; | |
83d890b9 | 996 | gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */ |
6de9cd9a DN |
997 | gfc_charlen *cl; |
998 | locus var_locus; | |
999 | match m; | |
1000 | try t; | |
83d890b9 | 1001 | gfc_symbol *sym; |
25d8f0a2 | 1002 | locus old_locus; |
6de9cd9a DN |
1003 | |
1004 | initializer = NULL; | |
1005 | as = NULL; | |
83d890b9 | 1006 | cp_as = NULL; |
25d8f0a2 | 1007 | old_locus = gfc_current_locus; |
6de9cd9a DN |
1008 | |
1009 | /* When we get here, we've just matched a list of attributes and | |
1010 | maybe a type and a double colon. The next thing we expect to see | |
1011 | is the name of the symbol. */ | |
1012 | m = gfc_match_name (name); | |
1013 | if (m != MATCH_YES) | |
1014 | goto cleanup; | |
1015 | ||
63645982 | 1016 | var_locus = gfc_current_locus; |
6de9cd9a DN |
1017 | |
1018 | /* Now we could see the optional array spec. or character length. */ | |
1019 | m = gfc_match_array_spec (&as); | |
83d890b9 AL |
1020 | if (gfc_option.flag_cray_pointer && m == MATCH_YES) |
1021 | cp_as = gfc_copy_array_spec (as); | |
1022 | else if (m == MATCH_ERROR) | |
6de9cd9a | 1023 | goto cleanup; |
25d8f0a2 | 1024 | |
6de9cd9a DN |
1025 | if (m == MATCH_NO) |
1026 | as = gfc_copy_array_spec (current_as); | |
25d8f0a2 TS |
1027 | else if (gfc_current_state () == COMP_ENUM) |
1028 | { | |
1029 | gfc_error ("Enumerator cannot be array at %C"); | |
1030 | gfc_free_enum_history (); | |
1031 | m = MATCH_ERROR; | |
1032 | goto cleanup; | |
1033 | } | |
1034 | ||
6de9cd9a DN |
1035 | |
1036 | char_len = NULL; | |
1037 | cl = NULL; | |
1038 | ||
1039 | if (current_ts.type == BT_CHARACTER) | |
1040 | { | |
1041 | switch (match_char_length (&char_len)) | |
1042 | { | |
1043 | case MATCH_YES: | |
1044 | cl = gfc_get_charlen (); | |
1045 | cl->next = gfc_current_ns->cl_list; | |
1046 | gfc_current_ns->cl_list = cl; | |
1047 | ||
1048 | cl->length = char_len; | |
1049 | break; | |
1050 | ||
949d5b72 PT |
1051 | /* Non-constant lengths need to be copied after the first |
1052 | element. */ | |
6de9cd9a | 1053 | case MATCH_NO: |
949d5b72 PT |
1054 | if (elem > 1 && current_ts.cl->length |
1055 | && current_ts.cl->length->expr_type != EXPR_CONSTANT) | |
1056 | { | |
1057 | cl = gfc_get_charlen (); | |
1058 | cl->next = gfc_current_ns->cl_list; | |
1059 | gfc_current_ns->cl_list = cl; | |
1060 | cl->length = gfc_copy_expr (current_ts.cl->length); | |
1061 | } | |
1062 | else | |
1063 | cl = current_ts.cl; | |
1064 | ||
6de9cd9a DN |
1065 | break; |
1066 | ||
1067 | case MATCH_ERROR: | |
1068 | goto cleanup; | |
1069 | } | |
1070 | } | |
1071 | ||
83d890b9 AL |
1072 | /* If this symbol has already shown up in a Cray Pointer declaration, |
1073 | then we want to set the type & bail out. */ | |
1074 | if (gfc_option.flag_cray_pointer) | |
1075 | { | |
1076 | gfc_find_symbol (name, gfc_current_ns, 1, &sym); | |
1077 | if (sym != NULL && sym->attr.cray_pointee) | |
1078 | { | |
1079 | sym->ts.type = current_ts.type; | |
1080 | sym->ts.kind = current_ts.kind; | |
1081 | sym->ts.cl = cl; | |
1082 | sym->ts.derived = current_ts.derived; | |
1083 | m = MATCH_YES; | |
1084 | ||
1085 | /* Check to see if we have an array specification. */ | |
1086 | if (cp_as != NULL) | |
1087 | { | |
1088 | if (sym->as != NULL) | |
1089 | { | |
1090 | gfc_error ("Duplicate array spec for Cray pointee at %C."); | |
1091 | gfc_free_array_spec (cp_as); | |
1092 | m = MATCH_ERROR; | |
1093 | goto cleanup; | |
1094 | } | |
1095 | else | |
1096 | { | |
1097 | if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE) | |
1098 | gfc_internal_error ("Couldn't set pointee array spec."); | |
1099 | ||
1100 | /* Fix the array spec. */ | |
1101 | m = gfc_mod_pointee_as (sym->as); | |
1102 | if (m == MATCH_ERROR) | |
1103 | goto cleanup; | |
1104 | } | |
1105 | } | |
1106 | goto cleanup; | |
1107 | } | |
1108 | else | |
1109 | { | |
1110 | gfc_free_array_spec (cp_as); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | ||
6de9cd9a DN |
1115 | /* OK, we've successfully matched the declaration. Now put the |
1116 | symbol in the current namespace, because it might be used in the | |
69de3b83 | 1117 | optional initialization expression for this symbol, e.g. this is |
6de9cd9a DN |
1118 | perfectly legal: |
1119 | ||
1120 | integer, parameter :: i = huge(i) | |
1121 | ||
1122 | This is only true for parameters or variables of a basic type. | |
1123 | For components of derived types, it is not true, so we don't | |
1124 | create a symbol for those yet. If we fail to create the symbol, | |
1125 | bail out. */ | |
1126 | if (gfc_current_state () != COMP_DERIVED | |
1127 | && build_sym (name, cl, &as, &var_locus) == FAILURE) | |
1128 | { | |
1129 | m = MATCH_ERROR; | |
1130 | goto cleanup; | |
1131 | } | |
1132 | ||
1133 | /* In functions that have a RESULT variable defined, the function | |
1134 | name always refers to function calls. Therefore, the name is | |
1135 | not allowed to appear in specification statements. */ | |
1136 | if (gfc_current_state () == COMP_FUNCTION | |
1137 | && gfc_current_block () != NULL | |
1138 | && gfc_current_block ()->result != NULL | |
1139 | && gfc_current_block ()->result != gfc_current_block () | |
1140 | && strcmp (gfc_current_block ()->name, name) == 0) | |
1141 | { | |
1142 | gfc_error ("Function name '%s' not allowed at %C", name); | |
1143 | m = MATCH_ERROR; | |
1144 | goto cleanup; | |
1145 | } | |
1146 | ||
294fbfc8 TS |
1147 | /* We allow old-style initializations of the form |
1148 | integer i /2/, j(4) /3*3, 1/ | |
1149 | (if no colon has been seen). These are different from data | |
1150 | statements in that initializers are only allowed to apply to the | |
1151 | variable immediately preceding, i.e. | |
1152 | integer i, j /1, 2/ | |
1153 | is not allowed. Therefore we have to do some work manually, that | |
75d17889 | 1154 | could otherwise be left to the matchers for DATA statements. */ |
294fbfc8 TS |
1155 | |
1156 | if (!colon_seen && gfc_match (" /") == MATCH_YES) | |
1157 | { | |
1158 | if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style " | |
1159 | "initialization at %C") == FAILURE) | |
1160 | return MATCH_ERROR; | |
1161 | ||
1162 | return match_old_style_init (name); | |
1163 | } | |
1164 | ||
6de9cd9a DN |
1165 | /* The double colon must be present in order to have initializers. |
1166 | Otherwise the statement is ambiguous with an assignment statement. */ | |
1167 | if (colon_seen) | |
1168 | { | |
1169 | if (gfc_match (" =>") == MATCH_YES) | |
1170 | { | |
1171 | ||
1172 | if (!current_attr.pointer) | |
1173 | { | |
1174 | gfc_error ("Initialization at %C isn't for a pointer variable"); | |
1175 | m = MATCH_ERROR; | |
1176 | goto cleanup; | |
1177 | } | |
1178 | ||
1179 | m = gfc_match_null (&initializer); | |
1180 | if (m == MATCH_NO) | |
1181 | { | |
1182 | gfc_error ("Pointer initialization requires a NULL at %C"); | |
1183 | m = MATCH_ERROR; | |
1184 | } | |
1185 | ||
1186 | if (gfc_pure (NULL)) | |
1187 | { | |
1188 | gfc_error | |
1189 | ("Initialization of pointer at %C is not allowed in a " | |
1190 | "PURE procedure"); | |
1191 | m = MATCH_ERROR; | |
1192 | } | |
1193 | ||
1194 | if (m != MATCH_YES) | |
1195 | goto cleanup; | |
1196 | ||
1197 | initializer->ts = current_ts; | |
1198 | ||
1199 | } | |
1200 | else if (gfc_match_char ('=') == MATCH_YES) | |
1201 | { | |
1202 | if (current_attr.pointer) | |
1203 | { | |
1204 | gfc_error | |
1205 | ("Pointer initialization at %C requires '=>', not '='"); | |
1206 | m = MATCH_ERROR; | |
1207 | goto cleanup; | |
1208 | } | |
1209 | ||
1210 | m = gfc_match_init_expr (&initializer); | |
1211 | if (m == MATCH_NO) | |
1212 | { | |
1213 | gfc_error ("Expected an initialization expression at %C"); | |
1214 | m = MATCH_ERROR; | |
1215 | } | |
1216 | ||
1217 | if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)) | |
1218 | { | |
1219 | gfc_error | |
1220 | ("Initialization of variable at %C is not allowed in a " | |
1221 | "PURE procedure"); | |
1222 | m = MATCH_ERROR; | |
1223 | } | |
1224 | ||
1225 | if (m != MATCH_YES) | |
1226 | goto cleanup; | |
1227 | } | |
cb44ab82 VL |
1228 | } |
1229 | ||
25d8f0a2 TS |
1230 | /* Check if we are parsing an enumeration and if the current enumerator |
1231 | variable has an initializer or not. If it does not have an | |
1232 | initializer, the initialization value of the previous enumerator | |
1233 | (stored in last_initializer) is incremented by 1 and is used to | |
1234 | initialize the current enumerator. */ | |
1235 | if (gfc_current_state () == COMP_ENUM) | |
1236 | { | |
1237 | if (initializer == NULL) | |
1238 | initializer = gfc_enum_initializer (last_initializer, old_locus); | |
1239 | ||
1240 | if (initializer == NULL || initializer->ts.type != BT_INTEGER) | |
1241 | { | |
1242 | gfc_error("ENUMERATOR %L not initialized with integer expression", | |
1243 | &var_locus); | |
1244 | m = MATCH_ERROR; | |
1245 | gfc_free_enum_history (); | |
1246 | goto cleanup; | |
1247 | } | |
1248 | ||
1249 | /* Store this current initializer, for the next enumerator | |
1250 | variable to be parsed. */ | |
1251 | last_initializer = initializer; | |
1252 | } | |
1253 | ||
54b4ba60 | 1254 | /* Add the initializer. Note that it is fine if initializer is |
6de9cd9a DN |
1255 | NULL here, because we sometimes also need to check if a |
1256 | declaration *must* have an initialization expression. */ | |
1257 | if (gfc_current_state () != COMP_DERIVED) | |
1258 | t = add_init_expr_to_sym (name, &initializer, &var_locus); | |
1259 | else | |
54b4ba60 | 1260 | { |
6019a1a4 | 1261 | if (current_ts.type == BT_DERIVED && !current_attr.pointer && !initializer) |
54b4ba60 PB |
1262 | initializer = gfc_default_initializer (¤t_ts); |
1263 | t = build_struct (name, cl, &initializer, &as); | |
1264 | } | |
6de9cd9a DN |
1265 | |
1266 | m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR; | |
1267 | ||
1268 | cleanup: | |
1269 | /* Free stuff up and return. */ | |
1270 | gfc_free_expr (initializer); | |
1271 | gfc_free_array_spec (as); | |
1272 | ||
1273 | return m; | |
1274 | } | |
1275 | ||
1276 | ||
1277 | /* Match an extended-f77 kind specification. */ | |
1278 | ||
1279 | match | |
1280 | gfc_match_old_kind_spec (gfc_typespec * ts) | |
1281 | { | |
1282 | match m; | |
8a8f7eca | 1283 | int original_kind, cnt; |
6de9cd9a DN |
1284 | |
1285 | if (gfc_match_char ('*') != MATCH_YES) | |
1286 | return MATCH_NO; | |
1287 | ||
8a8f7eca SK |
1288 | /* cnt is unsed, here. */ |
1289 | m = gfc_match_small_literal_int (&ts->kind, &cnt); | |
6de9cd9a DN |
1290 | if (m != MATCH_YES) |
1291 | return MATCH_ERROR; | |
1292 | ||
e45b3c75 ES |
1293 | original_kind = ts->kind; |
1294 | ||
6de9cd9a | 1295 | /* Massage the kind numbers for complex types. */ |
e45b3c75 ES |
1296 | if (ts->type == BT_COMPLEX) |
1297 | { | |
1298 | if (ts->kind % 2) | |
1299 | { | |
1300 | gfc_error ("Old-style type declaration %s*%d not supported at %C", | |
1301 | gfc_basic_typename (ts->type), original_kind); | |
1302 | return MATCH_ERROR; | |
1303 | } | |
1304 | ts->kind /= 2; | |
1305 | } | |
6de9cd9a | 1306 | |
e7a2d5fb | 1307 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a | 1308 | { |
e45b3c75 ES |
1309 | gfc_error ("Old-style type declaration %s*%d not supported at %C", |
1310 | gfc_basic_typename (ts->type), original_kind); | |
6de9cd9a DN |
1311 | return MATCH_ERROR; |
1312 | } | |
1313 | ||
1314 | return MATCH_YES; | |
1315 | } | |
1316 | ||
1317 | ||
1318 | /* Match a kind specification. Since kinds are generally optional, we | |
1319 | usually return MATCH_NO if something goes wrong. If a "kind=" | |
1320 | string is found, then we know we have an error. */ | |
1321 | ||
1322 | match | |
1323 | gfc_match_kind_spec (gfc_typespec * ts) | |
1324 | { | |
1325 | locus where; | |
1326 | gfc_expr *e; | |
1327 | match m, n; | |
1328 | const char *msg; | |
1329 | ||
1330 | m = MATCH_NO; | |
1331 | e = NULL; | |
1332 | ||
63645982 | 1333 | where = gfc_current_locus; |
6de9cd9a DN |
1334 | |
1335 | if (gfc_match_char ('(') == MATCH_NO) | |
1336 | return MATCH_NO; | |
1337 | ||
1338 | /* Also gobbles optional text. */ | |
1339 | if (gfc_match (" kind = ") == MATCH_YES) | |
1340 | m = MATCH_ERROR; | |
1341 | ||
1342 | n = gfc_match_init_expr (&e); | |
1343 | if (n == MATCH_NO) | |
1344 | gfc_error ("Expected initialization expression at %C"); | |
1345 | if (n != MATCH_YES) | |
1346 | return MATCH_ERROR; | |
1347 | ||
1348 | if (e->rank != 0) | |
1349 | { | |
1350 | gfc_error ("Expected scalar initialization expression at %C"); | |
1351 | m = MATCH_ERROR; | |
1352 | goto no_match; | |
1353 | } | |
1354 | ||
1355 | msg = gfc_extract_int (e, &ts->kind); | |
1356 | if (msg != NULL) | |
1357 | { | |
1358 | gfc_error (msg); | |
1359 | m = MATCH_ERROR; | |
1360 | goto no_match; | |
1361 | } | |
1362 | ||
1363 | gfc_free_expr (e); | |
1364 | e = NULL; | |
1365 | ||
e7a2d5fb | 1366 | if (gfc_validate_kind (ts->type, ts->kind, true) < 0) |
6de9cd9a DN |
1367 | { |
1368 | gfc_error ("Kind %d not supported for type %s at %C", ts->kind, | |
1369 | gfc_basic_typename (ts->type)); | |
1370 | ||
1371 | m = MATCH_ERROR; | |
1372 | goto no_match; | |
1373 | } | |
1374 | ||
1375 | if (gfc_match_char (')') != MATCH_YES) | |
1376 | { | |
1377 | gfc_error ("Missing right paren at %C"); | |
1378 | goto no_match; | |
1379 | } | |
1380 | ||
1381 | return MATCH_YES; | |
1382 | ||
1383 | no_match: | |
1384 | gfc_free_expr (e); | |
63645982 | 1385 | gfc_current_locus = where; |
6de9cd9a DN |
1386 | return m; |
1387 | } | |
1388 | ||
1389 | ||
1390 | /* Match the various kind/length specifications in a CHARACTER | |
1391 | declaration. We don't return MATCH_NO. */ | |
1392 | ||
1393 | static match | |
1394 | match_char_spec (gfc_typespec * ts) | |
1395 | { | |
1396 | int i, kind, seen_length; | |
1397 | gfc_charlen *cl; | |
1398 | gfc_expr *len; | |
1399 | match m; | |
1400 | ||
9d64df18 | 1401 | kind = gfc_default_character_kind; |
6de9cd9a DN |
1402 | len = NULL; |
1403 | seen_length = 0; | |
1404 | ||
1405 | /* Try the old-style specification first. */ | |
1406 | old_char_selector = 0; | |
1407 | ||
1408 | m = match_char_length (&len); | |
1409 | if (m != MATCH_NO) | |
1410 | { | |
1411 | if (m == MATCH_YES) | |
1412 | old_char_selector = 1; | |
1413 | seen_length = 1; | |
1414 | goto done; | |
1415 | } | |
1416 | ||
1417 | m = gfc_match_char ('('); | |
1418 | if (m != MATCH_YES) | |
1419 | { | |
1420 | m = MATCH_YES; /* character without length is a single char */ | |
1421 | goto done; | |
1422 | } | |
1423 | ||
1424 | /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */ | |
1425 | if (gfc_match (" kind =") == MATCH_YES) | |
1426 | { | |
1427 | m = gfc_match_small_int (&kind); | |
1428 | if (m == MATCH_ERROR) | |
1429 | goto done; | |
1430 | if (m == MATCH_NO) | |
1431 | goto syntax; | |
1432 | ||
1433 | if (gfc_match (" , len =") == MATCH_NO) | |
1434 | goto rparen; | |
1435 | ||
1436 | m = char_len_param_value (&len); | |
1437 | if (m == MATCH_NO) | |
1438 | goto syntax; | |
1439 | if (m == MATCH_ERROR) | |
1440 | goto done; | |
1441 | seen_length = 1; | |
1442 | ||
1443 | goto rparen; | |
1444 | } | |
1445 | ||
1446 | /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */ | |
1447 | if (gfc_match (" len =") == MATCH_YES) | |
1448 | { | |
1449 | m = char_len_param_value (&len); | |
1450 | if (m == MATCH_NO) | |
1451 | goto syntax; | |
1452 | if (m == MATCH_ERROR) | |
1453 | goto done; | |
1454 | seen_length = 1; | |
1455 | ||
1456 | if (gfc_match_char (')') == MATCH_YES) | |
1457 | goto done; | |
1458 | ||
1459 | if (gfc_match (" , kind =") != MATCH_YES) | |
1460 | goto syntax; | |
1461 | ||
1462 | gfc_match_small_int (&kind); | |
1463 | ||
e7a2d5fb | 1464 | if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0) |
6de9cd9a DN |
1465 | { |
1466 | gfc_error ("Kind %d is not a CHARACTER kind at %C", kind); | |
1467 | return MATCH_YES; | |
1468 | } | |
1469 | ||
1470 | goto rparen; | |
1471 | } | |
1472 | ||
1473 | /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */ | |
1474 | m = char_len_param_value (&len); | |
1475 | if (m == MATCH_NO) | |
1476 | goto syntax; | |
1477 | if (m == MATCH_ERROR) | |
1478 | goto done; | |
1479 | seen_length = 1; | |
1480 | ||
1481 | m = gfc_match_char (')'); | |
1482 | if (m == MATCH_YES) | |
1483 | goto done; | |
1484 | ||
1485 | if (gfc_match_char (',') != MATCH_YES) | |
1486 | goto syntax; | |
1487 | ||
1488 | gfc_match (" kind ="); /* Gobble optional text */ | |
1489 | ||
1490 | m = gfc_match_small_int (&kind); | |
1491 | if (m == MATCH_ERROR) | |
1492 | goto done; | |
1493 | if (m == MATCH_NO) | |
1494 | goto syntax; | |
1495 | ||
1496 | rparen: | |
1497 | /* Require a right-paren at this point. */ | |
1498 | m = gfc_match_char (')'); | |
1499 | if (m == MATCH_YES) | |
1500 | goto done; | |
1501 | ||
1502 | syntax: | |
1503 | gfc_error ("Syntax error in CHARACTER declaration at %C"); | |
1504 | m = MATCH_ERROR; | |
1505 | ||
1506 | done: | |
e7a2d5fb | 1507 | if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0) |
6de9cd9a DN |
1508 | { |
1509 | gfc_error ("Kind %d is not a CHARACTER kind at %C", kind); | |
1510 | m = MATCH_ERROR; | |
1511 | } | |
1512 | ||
1513 | if (m != MATCH_YES) | |
1514 | { | |
1515 | gfc_free_expr (len); | |
1516 | return m; | |
1517 | } | |
1518 | ||
1519 | /* Do some final massaging of the length values. */ | |
1520 | cl = gfc_get_charlen (); | |
1521 | cl->next = gfc_current_ns->cl_list; | |
1522 | gfc_current_ns->cl_list = cl; | |
1523 | ||
1524 | if (seen_length == 0) | |
1525 | cl->length = gfc_int_expr (1); | |
1526 | else | |
1527 | { | |
1528 | if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0) | |
1529 | cl->length = len; | |
1530 | else | |
1531 | { | |
1532 | gfc_free_expr (len); | |
1533 | cl->length = gfc_int_expr (0); | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | ts->cl = cl; | |
1538 | ts->kind = kind; | |
1539 | ||
1540 | return MATCH_YES; | |
1541 | } | |
1542 | ||
1543 | ||
1544 | /* Matches a type specification. If successful, sets the ts structure | |
1545 | to the matched specification. This is necessary for FUNCTION and | |
1546 | IMPLICIT statements. | |
1547 | ||
e5ddaa24 TS |
1548 | If implicit_flag is nonzero, then we don't check for the optional |
1549 | kind specification. Not doing so is needed for matching an IMPLICIT | |
6de9cd9a DN |
1550 | statement correctly. */ |
1551 | ||
e5ddaa24 TS |
1552 | static match |
1553 | match_type_spec (gfc_typespec * ts, int implicit_flag) | |
6de9cd9a DN |
1554 | { |
1555 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
1556 | gfc_symbol *sym; | |
1557 | match m; | |
0ff0dfbf | 1558 | int c; |
6de9cd9a DN |
1559 | |
1560 | gfc_clear_ts (ts); | |
1561 | ||
5f700e6d AL |
1562 | if (gfc_match (" byte") == MATCH_YES) |
1563 | { | |
1564 | if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C") | |
1565 | == FAILURE) | |
1566 | return MATCH_ERROR; | |
1567 | ||
1568 | if (gfc_validate_kind (BT_INTEGER, 1, true) < 0) | |
1569 | { | |
1570 | gfc_error ("BYTE type used at %C " | |
1571 | "is not available on the target machine"); | |
1572 | return MATCH_ERROR; | |
1573 | } | |
1574 | ||
1575 | ts->type = BT_INTEGER; | |
1576 | ts->kind = 1; | |
1577 | return MATCH_YES; | |
1578 | } | |
1579 | ||
6de9cd9a DN |
1580 | if (gfc_match (" integer") == MATCH_YES) |
1581 | { | |
1582 | ts->type = BT_INTEGER; | |
9d64df18 | 1583 | ts->kind = gfc_default_integer_kind; |
6de9cd9a DN |
1584 | goto get_kind; |
1585 | } | |
1586 | ||
1587 | if (gfc_match (" character") == MATCH_YES) | |
1588 | { | |
1589 | ts->type = BT_CHARACTER; | |
e5ddaa24 TS |
1590 | if (implicit_flag == 0) |
1591 | return match_char_spec (ts); | |
1592 | else | |
1593 | return MATCH_YES; | |
6de9cd9a DN |
1594 | } |
1595 | ||
1596 | if (gfc_match (" real") == MATCH_YES) | |
1597 | { | |
1598 | ts->type = BT_REAL; | |
9d64df18 | 1599 | ts->kind = gfc_default_real_kind; |
6de9cd9a DN |
1600 | goto get_kind; |
1601 | } | |
1602 | ||
1603 | if (gfc_match (" double precision") == MATCH_YES) | |
1604 | { | |
1605 | ts->type = BT_REAL; | |
9d64df18 | 1606 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
1607 | return MATCH_YES; |
1608 | } | |
1609 | ||
1610 | if (gfc_match (" complex") == MATCH_YES) | |
1611 | { | |
1612 | ts->type = BT_COMPLEX; | |
9d64df18 | 1613 | ts->kind = gfc_default_complex_kind; |
6de9cd9a DN |
1614 | goto get_kind; |
1615 | } | |
1616 | ||
1617 | if (gfc_match (" double complex") == MATCH_YES) | |
1618 | { | |
1619 | ts->type = BT_COMPLEX; | |
9d64df18 | 1620 | ts->kind = gfc_default_double_kind; |
6de9cd9a DN |
1621 | return MATCH_YES; |
1622 | } | |
1623 | ||
1624 | if (gfc_match (" logical") == MATCH_YES) | |
1625 | { | |
1626 | ts->type = BT_LOGICAL; | |
9d64df18 | 1627 | ts->kind = gfc_default_logical_kind; |
6de9cd9a DN |
1628 | goto get_kind; |
1629 | } | |
1630 | ||
1631 | m = gfc_match (" type ( %n )", name); | |
1632 | if (m != MATCH_YES) | |
1633 | return m; | |
1634 | ||
1635 | /* Search for the name but allow the components to be defined later. */ | |
1636 | if (gfc_get_ha_symbol (name, &sym)) | |
1637 | { | |
1638 | gfc_error ("Type name '%s' at %C is ambiguous", name); | |
1639 | return MATCH_ERROR; | |
1640 | } | |
1641 | ||
1642 | if (sym->attr.flavor != FL_DERIVED | |
231b2fcc | 1643 | && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
1644 | return MATCH_ERROR; |
1645 | ||
1646 | ts->type = BT_DERIVED; | |
1647 | ts->kind = 0; | |
1648 | ts->derived = sym; | |
1649 | ||
1650 | return MATCH_YES; | |
1651 | ||
1652 | get_kind: | |
1653 | /* For all types except double, derived and character, look for an | |
1654 | optional kind specifier. MATCH_NO is actually OK at this point. */ | |
e5ddaa24 | 1655 | if (implicit_flag == 1) |
6de9cd9a DN |
1656 | return MATCH_YES; |
1657 | ||
0ff0dfbf TS |
1658 | if (gfc_current_form == FORM_FREE) |
1659 | { | |
1660 | c = gfc_peek_char(); | |
1661 | if (!gfc_is_whitespace(c) && c != '*' && c != '(' | |
1662 | && c != ':' && c != ',') | |
1663 | return MATCH_NO; | |
1664 | } | |
1665 | ||
6de9cd9a DN |
1666 | m = gfc_match_kind_spec (ts); |
1667 | if (m == MATCH_NO && ts->type != BT_CHARACTER) | |
1668 | m = gfc_match_old_kind_spec (ts); | |
1669 | ||
1670 | if (m == MATCH_NO) | |
1671 | m = MATCH_YES; /* No kind specifier found. */ | |
1672 | ||
1673 | return m; | |
1674 | } | |
1675 | ||
1676 | ||
e5ddaa24 TS |
1677 | /* Match an IMPLICIT NONE statement. Actually, this statement is |
1678 | already matched in parse.c, or we would not end up here in the | |
1679 | first place. So the only thing we need to check, is if there is | |
1680 | trailing garbage. If not, the match is successful. */ | |
1681 | ||
1682 | match | |
1683 | gfc_match_implicit_none (void) | |
1684 | { | |
1685 | ||
1686 | return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO; | |
1687 | } | |
1688 | ||
1689 | ||
1690 | /* Match the letter range(s) of an IMPLICIT statement. */ | |
1691 | ||
1692 | static match | |
1107b970 | 1693 | match_implicit_range (void) |
e5ddaa24 TS |
1694 | { |
1695 | int c, c1, c2, inner; | |
1696 | locus cur_loc; | |
1697 | ||
1698 | cur_loc = gfc_current_locus; | |
1699 | ||
1700 | gfc_gobble_whitespace (); | |
1701 | c = gfc_next_char (); | |
1702 | if (c != '(') | |
1703 | { | |
1704 | gfc_error ("Missing character range in IMPLICIT at %C"); | |
1705 | goto bad; | |
1706 | } | |
1707 | ||
1708 | inner = 1; | |
1709 | while (inner) | |
1710 | { | |
1711 | gfc_gobble_whitespace (); | |
1712 | c1 = gfc_next_char (); | |
1713 | if (!ISALPHA (c1)) | |
1714 | goto bad; | |
1715 | ||
1716 | gfc_gobble_whitespace (); | |
1717 | c = gfc_next_char (); | |
1718 | ||
1719 | switch (c) | |
1720 | { | |
1721 | case ')': | |
1722 | inner = 0; /* Fall through */ | |
1723 | ||
1724 | case ',': | |
1725 | c2 = c1; | |
1726 | break; | |
1727 | ||
1728 | case '-': | |
1729 | gfc_gobble_whitespace (); | |
1730 | c2 = gfc_next_char (); | |
1731 | if (!ISALPHA (c2)) | |
1732 | goto bad; | |
1733 | ||
1734 | gfc_gobble_whitespace (); | |
1735 | c = gfc_next_char (); | |
1736 | ||
1737 | if ((c != ',') && (c != ')')) | |
1738 | goto bad; | |
1739 | if (c == ')') | |
1740 | inner = 0; | |
1741 | ||
1742 | break; | |
1743 | ||
1744 | default: | |
1745 | goto bad; | |
1746 | } | |
1747 | ||
1748 | if (c1 > c2) | |
1749 | { | |
1750 | gfc_error ("Letters must be in alphabetic order in " | |
1751 | "IMPLICIT statement at %C"); | |
1752 | goto bad; | |
1753 | } | |
1754 | ||
1755 | /* See if we can add the newly matched range to the pending | |
1756 | implicits from this IMPLICIT statement. We do not check for | |
1757 | conflicts with whatever earlier IMPLICIT statements may have | |
1758 | set. This is done when we've successfully finished matching | |
1759 | the current one. */ | |
1107b970 | 1760 | if (gfc_add_new_implicit_range (c1, c2) != SUCCESS) |
e5ddaa24 TS |
1761 | goto bad; |
1762 | } | |
1763 | ||
1764 | return MATCH_YES; | |
1765 | ||
1766 | bad: | |
1767 | gfc_syntax_error (ST_IMPLICIT); | |
1768 | ||
1769 | gfc_current_locus = cur_loc; | |
1770 | return MATCH_ERROR; | |
1771 | } | |
1772 | ||
1773 | ||
1774 | /* Match an IMPLICIT statement, storing the types for | |
1775 | gfc_set_implicit() if the statement is accepted by the parser. | |
1776 | There is a strange looking, but legal syntactic construction | |
1777 | possible. It looks like: | |
1778 | ||
1779 | IMPLICIT INTEGER (a-b) (c-d) | |
1780 | ||
1781 | This is legal if "a-b" is a constant expression that happens to | |
1782 | equal one of the legal kinds for integers. The real problem | |
1783 | happens with an implicit specification that looks like: | |
1784 | ||
1785 | IMPLICIT INTEGER (a-b) | |
1786 | ||
1787 | In this case, a typespec matcher that is "greedy" (as most of the | |
1788 | matchers are) gobbles the character range as a kindspec, leaving | |
1789 | nothing left. We therefore have to go a bit more slowly in the | |
1790 | matching process by inhibiting the kindspec checking during | |
1791 | typespec matching and checking for a kind later. */ | |
1792 | ||
1793 | match | |
1794 | gfc_match_implicit (void) | |
1795 | { | |
1796 | gfc_typespec ts; | |
1797 | locus cur_loc; | |
1798 | int c; | |
1799 | match m; | |
1800 | ||
1801 | /* We don't allow empty implicit statements. */ | |
1802 | if (gfc_match_eos () == MATCH_YES) | |
1803 | { | |
1804 | gfc_error ("Empty IMPLICIT statement at %C"); | |
1805 | return MATCH_ERROR; | |
1806 | } | |
1807 | ||
e5ddaa24 TS |
1808 | do |
1809 | { | |
1107b970 PB |
1810 | /* First cleanup. */ |
1811 | gfc_clear_new_implicit (); | |
1812 | ||
e5ddaa24 TS |
1813 | /* A basic type is mandatory here. */ |
1814 | m = match_type_spec (&ts, 1); | |
1815 | if (m == MATCH_ERROR) | |
1816 | goto error; | |
1817 | if (m == MATCH_NO) | |
1818 | goto syntax; | |
1819 | ||
1820 | cur_loc = gfc_current_locus; | |
1107b970 | 1821 | m = match_implicit_range (); |
e5ddaa24 TS |
1822 | |
1823 | if (m == MATCH_YES) | |
1824 | { | |
1107b970 | 1825 | /* We may have <TYPE> (<RANGE>). */ |
e5ddaa24 TS |
1826 | gfc_gobble_whitespace (); |
1827 | c = gfc_next_char (); | |
1828 | if ((c == '\n') || (c == ',')) | |
1107b970 PB |
1829 | { |
1830 | /* Check for CHARACTER with no length parameter. */ | |
1831 | if (ts.type == BT_CHARACTER && !ts.cl) | |
1832 | { | |
9d64df18 | 1833 | ts.kind = gfc_default_character_kind; |
1107b970 PB |
1834 | ts.cl = gfc_get_charlen (); |
1835 | ts.cl->next = gfc_current_ns->cl_list; | |
1836 | gfc_current_ns->cl_list = ts.cl; | |
1837 | ts.cl->length = gfc_int_expr (1); | |
1838 | } | |
1839 | ||
1840 | /* Record the Successful match. */ | |
1841 | if (gfc_merge_new_implicit (&ts) != SUCCESS) | |
1842 | return MATCH_ERROR; | |
1843 | continue; | |
1844 | } | |
e5ddaa24 TS |
1845 | |
1846 | gfc_current_locus = cur_loc; | |
1847 | } | |
1848 | ||
1107b970 PB |
1849 | /* Discard the (incorrectly) matched range. */ |
1850 | gfc_clear_new_implicit (); | |
1851 | ||
1852 | /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */ | |
1853 | if (ts.type == BT_CHARACTER) | |
1854 | m = match_char_spec (&ts); | |
1855 | else | |
e5ddaa24 | 1856 | { |
1107b970 | 1857 | m = gfc_match_kind_spec (&ts); |
e5ddaa24 | 1858 | if (m == MATCH_NO) |
1107b970 PB |
1859 | { |
1860 | m = gfc_match_old_kind_spec (&ts); | |
1861 | if (m == MATCH_ERROR) | |
1862 | goto error; | |
1863 | if (m == MATCH_NO) | |
1864 | goto syntax; | |
1865 | } | |
e5ddaa24 | 1866 | } |
1107b970 PB |
1867 | if (m == MATCH_ERROR) |
1868 | goto error; | |
e5ddaa24 | 1869 | |
1107b970 | 1870 | m = match_implicit_range (); |
e5ddaa24 TS |
1871 | if (m == MATCH_ERROR) |
1872 | goto error; | |
1873 | if (m == MATCH_NO) | |
1874 | goto syntax; | |
1875 | ||
1876 | gfc_gobble_whitespace (); | |
1877 | c = gfc_next_char (); | |
1878 | if ((c != '\n') && (c != ',')) | |
1879 | goto syntax; | |
1880 | ||
1107b970 PB |
1881 | if (gfc_merge_new_implicit (&ts) != SUCCESS) |
1882 | return MATCH_ERROR; | |
e5ddaa24 TS |
1883 | } |
1884 | while (c == ','); | |
1885 | ||
1107b970 | 1886 | return MATCH_YES; |
e5ddaa24 TS |
1887 | |
1888 | syntax: | |
1889 | gfc_syntax_error (ST_IMPLICIT); | |
1890 | ||
1891 | error: | |
1892 | return MATCH_ERROR; | |
1893 | } | |
1894 | ||
1895 | ||
6de9cd9a DN |
1896 | /* Matches an attribute specification including array specs. If |
1897 | successful, leaves the variables current_attr and current_as | |
1898 | holding the specification. Also sets the colon_seen variable for | |
1899 | later use by matchers associated with initializations. | |
1900 | ||
1901 | This subroutine is a little tricky in the sense that we don't know | |
1902 | if we really have an attr-spec until we hit the double colon. | |
1903 | Until that time, we can only return MATCH_NO. This forces us to | |
1904 | check for duplicate specification at this level. */ | |
1905 | ||
1906 | static match | |
1907 | match_attr_spec (void) | |
1908 | { | |
1909 | ||
1910 | /* Modifiers that can exist in a type statement. */ | |
1911 | typedef enum | |
1912 | { GFC_DECL_BEGIN = 0, | |
1913 | DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL, | |
1914 | DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL, | |
1915 | DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE, | |
1916 | DECL_TARGET, DECL_COLON, DECL_NONE, | |
1917 | GFC_DECL_END /* Sentinel */ | |
1918 | } | |
1919 | decl_types; | |
1920 | ||
1921 | /* GFC_DECL_END is the sentinel, index starts at 0. */ | |
1922 | #define NUM_DECL GFC_DECL_END | |
1923 | ||
1924 | static mstring decls[] = { | |
1925 | minit (", allocatable", DECL_ALLOCATABLE), | |
1926 | minit (", dimension", DECL_DIMENSION), | |
1927 | minit (", external", DECL_EXTERNAL), | |
1928 | minit (", intent ( in )", DECL_IN), | |
1929 | minit (", intent ( out )", DECL_OUT), | |
1930 | minit (", intent ( in out )", DECL_INOUT), | |
1931 | minit (", intrinsic", DECL_INTRINSIC), | |
1932 | minit (", optional", DECL_OPTIONAL), | |
1933 | minit (", parameter", DECL_PARAMETER), | |
1934 | minit (", pointer", DECL_POINTER), | |
1935 | minit (", private", DECL_PRIVATE), | |
1936 | minit (", public", DECL_PUBLIC), | |
1937 | minit (", save", DECL_SAVE), | |
1938 | minit (", target", DECL_TARGET), | |
1939 | minit ("::", DECL_COLON), | |
1940 | minit (NULL, DECL_NONE) | |
1941 | }; | |
1942 | ||
1943 | locus start, seen_at[NUM_DECL]; | |
1944 | int seen[NUM_DECL]; | |
1945 | decl_types d; | |
1946 | const char *attr; | |
1947 | match m; | |
1948 | try t; | |
1949 | ||
1950 | gfc_clear_attr (¤t_attr); | |
63645982 | 1951 | start = gfc_current_locus; |
6de9cd9a DN |
1952 | |
1953 | current_as = NULL; | |
1954 | colon_seen = 0; | |
1955 | ||
1956 | /* See if we get all of the keywords up to the final double colon. */ | |
1957 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
1958 | seen[d] = 0; | |
1959 | ||
1960 | for (;;) | |
1961 | { | |
1962 | d = (decl_types) gfc_match_strings (decls); | |
1963 | if (d == DECL_NONE || d == DECL_COLON) | |
1964 | break; | |
25d8f0a2 TS |
1965 | |
1966 | if (gfc_current_state () == COMP_ENUM) | |
1967 | { | |
1968 | gfc_error ("Enumerator cannot have attributes %C"); | |
1969 | return MATCH_ERROR; | |
1970 | } | |
6de9cd9a DN |
1971 | |
1972 | seen[d]++; | |
63645982 | 1973 | seen_at[d] = gfc_current_locus; |
6de9cd9a DN |
1974 | |
1975 | if (d == DECL_DIMENSION) | |
1976 | { | |
1977 | m = gfc_match_array_spec (¤t_as); | |
1978 | ||
1979 | if (m == MATCH_NO) | |
1980 | { | |
1981 | gfc_error ("Missing dimension specification at %C"); | |
1982 | m = MATCH_ERROR; | |
1983 | } | |
1984 | ||
1985 | if (m == MATCH_ERROR) | |
1986 | goto cleanup; | |
1987 | } | |
1988 | } | |
1989 | ||
831d7813 | 1990 | /* If we are parsing an enumeration and have ensured that no other |
25d8f0a2 TS |
1991 | attributes are present we can now set the parameter attribute. */ |
1992 | if (gfc_current_state () == COMP_ENUM) | |
1993 | { | |
1994 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL); | |
1995 | if (t == FAILURE) | |
1996 | { | |
1997 | m = MATCH_ERROR; | |
1998 | goto cleanup; | |
1999 | } | |
2000 | } | |
2001 | ||
6de9cd9a DN |
2002 | /* No double colon, so assume that we've been looking at something |
2003 | else the whole time. */ | |
2004 | if (d == DECL_NONE) | |
2005 | { | |
2006 | m = MATCH_NO; | |
2007 | goto cleanup; | |
2008 | } | |
2009 | ||
2010 | /* Since we've seen a double colon, we have to be looking at an | |
2011 | attr-spec. This means that we can now issue errors. */ | |
2012 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
2013 | if (seen[d] > 1) | |
2014 | { | |
2015 | switch (d) | |
2016 | { | |
2017 | case DECL_ALLOCATABLE: | |
2018 | attr = "ALLOCATABLE"; | |
2019 | break; | |
2020 | case DECL_DIMENSION: | |
2021 | attr = "DIMENSION"; | |
2022 | break; | |
2023 | case DECL_EXTERNAL: | |
2024 | attr = "EXTERNAL"; | |
2025 | break; | |
2026 | case DECL_IN: | |
2027 | attr = "INTENT (IN)"; | |
2028 | break; | |
2029 | case DECL_OUT: | |
2030 | attr = "INTENT (OUT)"; | |
2031 | break; | |
2032 | case DECL_INOUT: | |
2033 | attr = "INTENT (IN OUT)"; | |
2034 | break; | |
2035 | case DECL_INTRINSIC: | |
2036 | attr = "INTRINSIC"; | |
2037 | break; | |
2038 | case DECL_OPTIONAL: | |
2039 | attr = "OPTIONAL"; | |
2040 | break; | |
2041 | case DECL_PARAMETER: | |
2042 | attr = "PARAMETER"; | |
2043 | break; | |
2044 | case DECL_POINTER: | |
2045 | attr = "POINTER"; | |
2046 | break; | |
2047 | case DECL_PRIVATE: | |
2048 | attr = "PRIVATE"; | |
2049 | break; | |
2050 | case DECL_PUBLIC: | |
2051 | attr = "PUBLIC"; | |
2052 | break; | |
2053 | case DECL_SAVE: | |
2054 | attr = "SAVE"; | |
2055 | break; | |
2056 | case DECL_TARGET: | |
2057 | attr = "TARGET"; | |
2058 | break; | |
2059 | default: | |
2060 | attr = NULL; /* This shouldn't happen */ | |
2061 | } | |
2062 | ||
2063 | gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]); | |
2064 | m = MATCH_ERROR; | |
2065 | goto cleanup; | |
2066 | } | |
2067 | ||
2068 | /* Now that we've dealt with duplicate attributes, add the attributes | |
2069 | to the current attribute. */ | |
2070 | for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++) | |
2071 | { | |
2072 | if (seen[d] == 0) | |
2073 | continue; | |
2074 | ||
2075 | if (gfc_current_state () == COMP_DERIVED | |
2076 | && d != DECL_DIMENSION && d != DECL_POINTER | |
2077 | && d != DECL_COLON && d != DECL_NONE) | |
2078 | { | |
2079 | ||
2080 | gfc_error ("Attribute at %L is not allowed in a TYPE definition", | |
2081 | &seen_at[d]); | |
2082 | m = MATCH_ERROR; | |
2083 | goto cleanup; | |
2084 | } | |
2085 | ||
4213f93b PT |
2086 | if ((d == DECL_PRIVATE || d == DECL_PUBLIC) |
2087 | && gfc_current_state () != COMP_MODULE) | |
2088 | { | |
2089 | if (d == DECL_PRIVATE) | |
2090 | attr = "PRIVATE"; | |
2091 | else | |
2092 | attr = "PUBLIC"; | |
2093 | ||
2094 | gfc_error ("%s attribute at %L is not allowed outside of a MODULE", | |
2095 | attr, &seen_at[d]); | |
2096 | m = MATCH_ERROR; | |
2097 | goto cleanup; | |
2098 | } | |
2099 | ||
6de9cd9a DN |
2100 | switch (d) |
2101 | { | |
2102 | case DECL_ALLOCATABLE: | |
2103 | t = gfc_add_allocatable (¤t_attr, &seen_at[d]); | |
2104 | break; | |
2105 | ||
2106 | case DECL_DIMENSION: | |
231b2fcc | 2107 | t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]); |
6de9cd9a DN |
2108 | break; |
2109 | ||
2110 | case DECL_EXTERNAL: | |
2111 | t = gfc_add_external (¤t_attr, &seen_at[d]); | |
2112 | break; | |
2113 | ||
2114 | case DECL_IN: | |
2115 | t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]); | |
2116 | break; | |
2117 | ||
2118 | case DECL_OUT: | |
2119 | t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]); | |
2120 | break; | |
2121 | ||
2122 | case DECL_INOUT: | |
2123 | t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]); | |
2124 | break; | |
2125 | ||
2126 | case DECL_INTRINSIC: | |
2127 | t = gfc_add_intrinsic (¤t_attr, &seen_at[d]); | |
2128 | break; | |
2129 | ||
2130 | case DECL_OPTIONAL: | |
2131 | t = gfc_add_optional (¤t_attr, &seen_at[d]); | |
2132 | break; | |
2133 | ||
2134 | case DECL_PARAMETER: | |
231b2fcc | 2135 | t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]); |
6de9cd9a DN |
2136 | break; |
2137 | ||
2138 | case DECL_POINTER: | |
2139 | t = gfc_add_pointer (¤t_attr, &seen_at[d]); | |
2140 | break; | |
2141 | ||
2142 | case DECL_PRIVATE: | |
231b2fcc TS |
2143 | t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL, |
2144 | &seen_at[d]); | |
6de9cd9a DN |
2145 | break; |
2146 | ||
2147 | case DECL_PUBLIC: | |
231b2fcc TS |
2148 | t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL, |
2149 | &seen_at[d]); | |
6de9cd9a DN |
2150 | break; |
2151 | ||
2152 | case DECL_SAVE: | |
231b2fcc | 2153 | t = gfc_add_save (¤t_attr, NULL, &seen_at[d]); |
6de9cd9a DN |
2154 | break; |
2155 | ||
2156 | case DECL_TARGET: | |
2157 | t = gfc_add_target (¤t_attr, &seen_at[d]); | |
2158 | break; | |
2159 | ||
2160 | default: | |
2161 | gfc_internal_error ("match_attr_spec(): Bad attribute"); | |
2162 | } | |
2163 | ||
2164 | if (t == FAILURE) | |
2165 | { | |
2166 | m = MATCH_ERROR; | |
2167 | goto cleanup; | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | colon_seen = 1; | |
2172 | return MATCH_YES; | |
2173 | ||
2174 | cleanup: | |
63645982 | 2175 | gfc_current_locus = start; |
6de9cd9a DN |
2176 | gfc_free_array_spec (current_as); |
2177 | current_as = NULL; | |
2178 | return m; | |
2179 | } | |
2180 | ||
2181 | ||
2182 | /* Match a data declaration statement. */ | |
2183 | ||
2184 | match | |
2185 | gfc_match_data_decl (void) | |
2186 | { | |
2187 | gfc_symbol *sym; | |
2188 | match m; | |
949d5b72 | 2189 | int elem; |
6de9cd9a | 2190 | |
e5ddaa24 | 2191 | m = match_type_spec (¤t_ts, 0); |
6de9cd9a DN |
2192 | if (m != MATCH_YES) |
2193 | return m; | |
2194 | ||
2195 | if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED) | |
2196 | { | |
2197 | sym = gfc_use_derived (current_ts.derived); | |
2198 | ||
2199 | if (sym == NULL) | |
2200 | { | |
2201 | m = MATCH_ERROR; | |
2202 | goto cleanup; | |
2203 | } | |
2204 | ||
2205 | current_ts.derived = sym; | |
2206 | } | |
2207 | ||
2208 | m = match_attr_spec (); | |
2209 | if (m == MATCH_ERROR) | |
2210 | { | |
2211 | m = MATCH_NO; | |
2212 | goto cleanup; | |
2213 | } | |
2214 | ||
2215 | if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL) | |
2216 | { | |
2217 | ||
2218 | if (current_attr.pointer && gfc_current_state () == COMP_DERIVED) | |
2219 | goto ok; | |
2220 | ||
976e21f6 PT |
2221 | gfc_find_symbol (current_ts.derived->name, |
2222 | current_ts.derived->ns->parent, 1, &sym); | |
6de9cd9a | 2223 | |
976e21f6 PT |
2224 | /* Any symbol that we find had better be a type definition |
2225 | which has its components defined. */ | |
2226 | if (sym != NULL && sym->attr.flavor == FL_DERIVED | |
2227 | && current_ts.derived->components != NULL) | |
6de9cd9a DN |
2228 | goto ok; |
2229 | ||
976e21f6 PT |
2230 | /* Now we have an error, which we signal, and then fix up |
2231 | because the knock-on is plain and simple confusing. */ | |
2232 | gfc_error_now ("Derived type at %C has not been previously defined " | |
2233 | "and so cannot appear in a derived type definition."); | |
2234 | current_attr.pointer = 1; | |
2235 | goto ok; | |
6de9cd9a DN |
2236 | } |
2237 | ||
2238 | ok: | |
2239 | /* If we have an old-style character declaration, and no new-style | |
2240 | attribute specifications, then there a comma is optional between | |
2241 | the type specification and the variable list. */ | |
2242 | if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector) | |
2243 | gfc_match_char (','); | |
2244 | ||
949d5b72 PT |
2245 | /* Give the types/attributes to symbols that follow. Give the element |
2246 | a number so that repeat character length expressions can be copied. */ | |
2247 | elem = 1; | |
6de9cd9a DN |
2248 | for (;;) |
2249 | { | |
949d5b72 | 2250 | m = variable_decl (elem++); |
6de9cd9a DN |
2251 | if (m == MATCH_ERROR) |
2252 | goto cleanup; | |
2253 | if (m == MATCH_NO) | |
2254 | break; | |
2255 | ||
2256 | if (gfc_match_eos () == MATCH_YES) | |
2257 | goto cleanup; | |
2258 | if (gfc_match_char (',') != MATCH_YES) | |
2259 | break; | |
2260 | } | |
2261 | ||
2262 | gfc_error ("Syntax error in data declaration at %C"); | |
2263 | m = MATCH_ERROR; | |
2264 | ||
2265 | cleanup: | |
2266 | gfc_free_array_spec (current_as); | |
2267 | current_as = NULL; | |
2268 | return m; | |
2269 | } | |
2270 | ||
2271 | ||
2272 | /* Match a prefix associated with a function or subroutine | |
2273 | declaration. If the typespec pointer is nonnull, then a typespec | |
2274 | can be matched. Note that if nothing matches, MATCH_YES is | |
2275 | returned (the null string was matched). */ | |
2276 | ||
2277 | static match | |
2278 | match_prefix (gfc_typespec * ts) | |
2279 | { | |
2280 | int seen_type; | |
2281 | ||
2282 | gfc_clear_attr (¤t_attr); | |
2283 | seen_type = 0; | |
2284 | ||
2285 | loop: | |
2286 | if (!seen_type && ts != NULL | |
e5ddaa24 | 2287 | && match_type_spec (ts, 0) == MATCH_YES |
6de9cd9a DN |
2288 | && gfc_match_space () == MATCH_YES) |
2289 | { | |
2290 | ||
2291 | seen_type = 1; | |
2292 | goto loop; | |
2293 | } | |
2294 | ||
2295 | if (gfc_match ("elemental% ") == MATCH_YES) | |
2296 | { | |
2297 | if (gfc_add_elemental (¤t_attr, NULL) == FAILURE) | |
2298 | return MATCH_ERROR; | |
2299 | ||
2300 | goto loop; | |
2301 | } | |
2302 | ||
2303 | if (gfc_match ("pure% ") == MATCH_YES) | |
2304 | { | |
2305 | if (gfc_add_pure (¤t_attr, NULL) == FAILURE) | |
2306 | return MATCH_ERROR; | |
2307 | ||
2308 | goto loop; | |
2309 | } | |
2310 | ||
2311 | if (gfc_match ("recursive% ") == MATCH_YES) | |
2312 | { | |
2313 | if (gfc_add_recursive (¤t_attr, NULL) == FAILURE) | |
2314 | return MATCH_ERROR; | |
2315 | ||
2316 | goto loop; | |
2317 | } | |
2318 | ||
2319 | /* At this point, the next item is not a prefix. */ | |
2320 | return MATCH_YES; | |
2321 | } | |
2322 | ||
2323 | ||
2324 | /* Copy attributes matched by match_prefix() to attributes on a symbol. */ | |
2325 | ||
2326 | static try | |
2327 | copy_prefix (symbol_attribute * dest, locus * where) | |
2328 | { | |
2329 | ||
2330 | if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE) | |
2331 | return FAILURE; | |
2332 | ||
2333 | if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE) | |
2334 | return FAILURE; | |
2335 | ||
2336 | if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE) | |
2337 | return FAILURE; | |
2338 | ||
2339 | return SUCCESS; | |
2340 | } | |
2341 | ||
2342 | ||
2343 | /* Match a formal argument list. */ | |
2344 | ||
2345 | match | |
2346 | gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag) | |
2347 | { | |
2348 | gfc_formal_arglist *head, *tail, *p, *q; | |
2349 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2350 | gfc_symbol *sym; | |
2351 | match m; | |
2352 | ||
2353 | head = tail = NULL; | |
2354 | ||
2355 | if (gfc_match_char ('(') != MATCH_YES) | |
2356 | { | |
2357 | if (null_flag) | |
2358 | goto ok; | |
2359 | return MATCH_NO; | |
2360 | } | |
2361 | ||
2362 | if (gfc_match_char (')') == MATCH_YES) | |
2363 | goto ok; | |
2364 | ||
2365 | for (;;) | |
2366 | { | |
2367 | if (gfc_match_char ('*') == MATCH_YES) | |
2368 | sym = NULL; | |
2369 | else | |
2370 | { | |
2371 | m = gfc_match_name (name); | |
2372 | if (m != MATCH_YES) | |
2373 | goto cleanup; | |
2374 | ||
2375 | if (gfc_get_symbol (name, NULL, &sym)) | |
2376 | goto cleanup; | |
2377 | } | |
2378 | ||
2379 | p = gfc_get_formal_arglist (); | |
2380 | ||
2381 | if (head == NULL) | |
2382 | head = tail = p; | |
2383 | else | |
2384 | { | |
2385 | tail->next = p; | |
2386 | tail = p; | |
2387 | } | |
2388 | ||
2389 | tail->sym = sym; | |
2390 | ||
2391 | /* We don't add the VARIABLE flavor because the name could be a | |
2392 | dummy procedure. We don't apply these attributes to formal | |
2393 | arguments of statement functions. */ | |
2394 | if (sym != NULL && !st_flag | |
231b2fcc | 2395 | && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE |
6de9cd9a DN |
2396 | || gfc_missing_attr (&sym->attr, NULL) == FAILURE)) |
2397 | { | |
2398 | m = MATCH_ERROR; | |
2399 | goto cleanup; | |
2400 | } | |
2401 | ||
2402 | /* The name of a program unit can be in a different namespace, | |
2403 | so check for it explicitly. After the statement is accepted, | |
2404 | the name is checked for especially in gfc_get_symbol(). */ | |
2405 | if (gfc_new_block != NULL && sym != NULL | |
2406 | && strcmp (sym->name, gfc_new_block->name) == 0) | |
2407 | { | |
2408 | gfc_error ("Name '%s' at %C is the name of the procedure", | |
2409 | sym->name); | |
2410 | m = MATCH_ERROR; | |
2411 | goto cleanup; | |
2412 | } | |
2413 | ||
2414 | if (gfc_match_char (')') == MATCH_YES) | |
2415 | goto ok; | |
2416 | ||
2417 | m = gfc_match_char (','); | |
2418 | if (m != MATCH_YES) | |
2419 | { | |
2420 | gfc_error ("Unexpected junk in formal argument list at %C"); | |
2421 | goto cleanup; | |
2422 | } | |
2423 | } | |
2424 | ||
2425 | ok: | |
2426 | /* Check for duplicate symbols in the formal argument list. */ | |
2427 | if (head != NULL) | |
2428 | { | |
2429 | for (p = head; p->next; p = p->next) | |
2430 | { | |
2431 | if (p->sym == NULL) | |
2432 | continue; | |
2433 | ||
2434 | for (q = p->next; q; q = q->next) | |
2435 | if (p->sym == q->sym) | |
2436 | { | |
2437 | gfc_error | |
2438 | ("Duplicate symbol '%s' in formal argument list at %C", | |
2439 | p->sym->name); | |
2440 | ||
2441 | m = MATCH_ERROR; | |
2442 | goto cleanup; | |
2443 | } | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) == | |
2448 | FAILURE) | |
2449 | { | |
2450 | m = MATCH_ERROR; | |
2451 | goto cleanup; | |
2452 | } | |
2453 | ||
2454 | return MATCH_YES; | |
2455 | ||
2456 | cleanup: | |
2457 | gfc_free_formal_arglist (head); | |
2458 | return m; | |
2459 | } | |
2460 | ||
2461 | ||
2462 | /* Match a RESULT specification following a function declaration or | |
2463 | ENTRY statement. Also matches the end-of-statement. */ | |
2464 | ||
2465 | static match | |
2466 | match_result (gfc_symbol * function, gfc_symbol ** result) | |
2467 | { | |
2468 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2469 | gfc_symbol *r; | |
2470 | match m; | |
2471 | ||
2472 | if (gfc_match (" result (") != MATCH_YES) | |
2473 | return MATCH_NO; | |
2474 | ||
2475 | m = gfc_match_name (name); | |
2476 | if (m != MATCH_YES) | |
2477 | return m; | |
2478 | ||
2479 | if (gfc_match (" )%t") != MATCH_YES) | |
2480 | { | |
2481 | gfc_error ("Unexpected junk following RESULT variable at %C"); | |
2482 | return MATCH_ERROR; | |
2483 | } | |
2484 | ||
2485 | if (strcmp (function->name, name) == 0) | |
2486 | { | |
2487 | gfc_error | |
2488 | ("RESULT variable at %C must be different than function name"); | |
2489 | return MATCH_ERROR; | |
2490 | } | |
2491 | ||
2492 | if (gfc_get_symbol (name, NULL, &r)) | |
2493 | return MATCH_ERROR; | |
2494 | ||
231b2fcc TS |
2495 | if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE |
2496 | || gfc_add_result (&r->attr, r->name, NULL) == FAILURE) | |
6de9cd9a DN |
2497 | return MATCH_ERROR; |
2498 | ||
2499 | *result = r; | |
2500 | ||
2501 | return MATCH_YES; | |
2502 | } | |
2503 | ||
2504 | ||
2505 | /* Match a function declaration. */ | |
2506 | ||
2507 | match | |
2508 | gfc_match_function_decl (void) | |
2509 | { | |
2510 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2511 | gfc_symbol *sym, *result; | |
2512 | locus old_loc; | |
2513 | match m; | |
2514 | ||
2515 | if (gfc_current_state () != COMP_NONE | |
2516 | && gfc_current_state () != COMP_INTERFACE | |
2517 | && gfc_current_state () != COMP_CONTAINS) | |
2518 | return MATCH_NO; | |
2519 | ||
2520 | gfc_clear_ts (¤t_ts); | |
2521 | ||
63645982 | 2522 | old_loc = gfc_current_locus; |
6de9cd9a DN |
2523 | |
2524 | m = match_prefix (¤t_ts); | |
2525 | if (m != MATCH_YES) | |
2526 | { | |
63645982 | 2527 | gfc_current_locus = old_loc; |
6de9cd9a DN |
2528 | return m; |
2529 | } | |
2530 | ||
2531 | if (gfc_match ("function% %n", name) != MATCH_YES) | |
2532 | { | |
63645982 | 2533 | gfc_current_locus = old_loc; |
6de9cd9a DN |
2534 | return MATCH_NO; |
2535 | } | |
2536 | ||
2537 | if (get_proc_name (name, &sym)) | |
2538 | return MATCH_ERROR; | |
2539 | gfc_new_block = sym; | |
2540 | ||
2541 | m = gfc_match_formal_arglist (sym, 0, 0); | |
2542 | if (m == MATCH_NO) | |
2543 | gfc_error ("Expected formal argument list in function definition at %C"); | |
2544 | else if (m == MATCH_ERROR) | |
2545 | goto cleanup; | |
2546 | ||
2547 | result = NULL; | |
2548 | ||
2549 | if (gfc_match_eos () != MATCH_YES) | |
2550 | { | |
2551 | /* See if a result variable is present. */ | |
2552 | m = match_result (sym, &result); | |
2553 | if (m == MATCH_NO) | |
2554 | gfc_error ("Unexpected junk after function declaration at %C"); | |
2555 | ||
2556 | if (m != MATCH_YES) | |
2557 | { | |
2558 | m = MATCH_ERROR; | |
2559 | goto cleanup; | |
2560 | } | |
2561 | } | |
2562 | ||
2563 | /* Make changes to the symbol. */ | |
2564 | m = MATCH_ERROR; | |
2565 | ||
231b2fcc | 2566 | if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
2567 | goto cleanup; |
2568 | ||
2569 | if (gfc_missing_attr (&sym->attr, NULL) == FAILURE | |
2570 | || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE) | |
2571 | goto cleanup; | |
2572 | ||
2573 | if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN) | |
2574 | { | |
2575 | gfc_error ("Function '%s' at %C already has a type of %s", name, | |
2576 | gfc_basic_typename (sym->ts.type)); | |
2577 | goto cleanup; | |
2578 | } | |
2579 | ||
2580 | if (result == NULL) | |
2581 | { | |
2582 | sym->ts = current_ts; | |
2583 | sym->result = sym; | |
2584 | } | |
2585 | else | |
2586 | { | |
2587 | result->ts = current_ts; | |
2588 | sym->result = result; | |
2589 | } | |
2590 | ||
2591 | return MATCH_YES; | |
2592 | ||
2593 | cleanup: | |
63645982 | 2594 | gfc_current_locus = old_loc; |
6de9cd9a DN |
2595 | return m; |
2596 | } | |
2597 | ||
2598 | ||
2599 | /* Match an ENTRY statement. */ | |
2600 | ||
2601 | match | |
2602 | gfc_match_entry (void) | |
2603 | { | |
3d79abbd PB |
2604 | gfc_symbol *proc; |
2605 | gfc_symbol *result; | |
2606 | gfc_symbol *entry; | |
6de9cd9a DN |
2607 | char name[GFC_MAX_SYMBOL_LEN + 1]; |
2608 | gfc_compile_state state; | |
2609 | match m; | |
3d79abbd | 2610 | gfc_entry_list *el; |
c96cfa49 | 2611 | locus old_loc; |
6de9cd9a DN |
2612 | |
2613 | m = gfc_match_name (name); | |
2614 | if (m != MATCH_YES) | |
2615 | return m; | |
2616 | ||
3d79abbd | 2617 | state = gfc_current_state (); |
4c93c95a | 2618 | if (state != COMP_SUBROUTINE && state != COMP_FUNCTION) |
3d79abbd | 2619 | { |
4c93c95a FXC |
2620 | switch (state) |
2621 | { | |
2622 | case COMP_PROGRAM: | |
2623 | gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM"); | |
2624 | break; | |
2625 | case COMP_MODULE: | |
2626 | gfc_error ("ENTRY statement at %C cannot appear within a MODULE"); | |
2627 | break; | |
2628 | case COMP_BLOCK_DATA: | |
2629 | gfc_error | |
2630 | ("ENTRY statement at %C cannot appear within a BLOCK DATA"); | |
2631 | break; | |
2632 | case COMP_INTERFACE: | |
2633 | gfc_error | |
2634 | ("ENTRY statement at %C cannot appear within an INTERFACE"); | |
2635 | break; | |
2636 | case COMP_DERIVED: | |
2637 | gfc_error | |
2638 | ("ENTRY statement at %C cannot appear " | |
2639 | "within a DERIVED TYPE block"); | |
2640 | break; | |
2641 | case COMP_IF: | |
2642 | gfc_error | |
2643 | ("ENTRY statement at %C cannot appear within an IF-THEN block"); | |
2644 | break; | |
2645 | case COMP_DO: | |
2646 | gfc_error | |
2647 | ("ENTRY statement at %C cannot appear within a DO block"); | |
2648 | break; | |
2649 | case COMP_SELECT: | |
2650 | gfc_error | |
2651 | ("ENTRY statement at %C cannot appear within a SELECT block"); | |
2652 | break; | |
2653 | case COMP_FORALL: | |
2654 | gfc_error | |
2655 | ("ENTRY statement at %C cannot appear within a FORALL block"); | |
2656 | break; | |
2657 | case COMP_WHERE: | |
2658 | gfc_error | |
2659 | ("ENTRY statement at %C cannot appear within a WHERE block"); | |
2660 | break; | |
2661 | case COMP_CONTAINS: | |
2662 | gfc_error | |
2663 | ("ENTRY statement at %C cannot appear " | |
2664 | "within a contained subprogram"); | |
2665 | break; | |
2666 | default: | |
2667 | gfc_internal_error ("gfc_match_entry(): Bad state"); | |
2668 | } | |
3d79abbd PB |
2669 | return MATCH_ERROR; |
2670 | } | |
2671 | ||
2672 | if (gfc_current_ns->parent != NULL | |
2673 | && gfc_current_ns->parent->proc_name | |
2674 | && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE) | |
2675 | { | |
2676 | gfc_error("ENTRY statement at %C cannot appear in a " | |
2677 | "contained procedure"); | |
2678 | return MATCH_ERROR; | |
2679 | } | |
2680 | ||
6de9cd9a DN |
2681 | if (get_proc_name (name, &entry)) |
2682 | return MATCH_ERROR; | |
2683 | ||
3d79abbd PB |
2684 | proc = gfc_current_block (); |
2685 | ||
2686 | if (state == COMP_SUBROUTINE) | |
6de9cd9a | 2687 | { |
231b2fcc | 2688 | /* An entry in a subroutine. */ |
6de9cd9a DN |
2689 | m = gfc_match_formal_arglist (entry, 0, 1); |
2690 | if (m != MATCH_YES) | |
2691 | return MATCH_ERROR; | |
2692 | ||
231b2fcc TS |
2693 | if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE |
2694 | || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE) | |
6de9cd9a | 2695 | return MATCH_ERROR; |
3d79abbd PB |
2696 | } |
2697 | else | |
2698 | { | |
c96cfa49 TS |
2699 | /* An entry in a function. |
2700 | We need to take special care because writing | |
2701 | ENTRY f() | |
2702 | as | |
2703 | ENTRY f | |
2704 | is allowed, whereas | |
2705 | ENTRY f() RESULT (r) | |
2706 | can't be written as | |
2707 | ENTRY f RESULT (r). */ | |
2708 | old_loc = gfc_current_locus; | |
2709 | if (gfc_match_eos () == MATCH_YES) | |
2710 | { | |
2711 | gfc_current_locus = old_loc; | |
2712 | /* Match the empty argument list, and add the interface to | |
2713 | the symbol. */ | |
2714 | m = gfc_match_formal_arglist (entry, 0, 1); | |
2715 | } | |
2716 | else | |
2717 | m = gfc_match_formal_arglist (entry, 0, 0); | |
2718 | ||
6de9cd9a DN |
2719 | if (m != MATCH_YES) |
2720 | return MATCH_ERROR; | |
2721 | ||
6de9cd9a DN |
2722 | result = NULL; |
2723 | ||
2724 | if (gfc_match_eos () == MATCH_YES) | |
2725 | { | |
231b2fcc TS |
2726 | if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE |
2727 | || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE) | |
6de9cd9a DN |
2728 | return MATCH_ERROR; |
2729 | ||
d198b59a | 2730 | entry->result = entry; |
6de9cd9a DN |
2731 | } |
2732 | else | |
2733 | { | |
3d79abbd | 2734 | m = match_result (proc, &result); |
6de9cd9a DN |
2735 | if (m == MATCH_NO) |
2736 | gfc_syntax_error (ST_ENTRY); | |
2737 | if (m != MATCH_YES) | |
2738 | return MATCH_ERROR; | |
2739 | ||
231b2fcc TS |
2740 | if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE |
2741 | || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE | |
2742 | || gfc_add_function (&entry->attr, result->name, | |
2743 | NULL) == FAILURE) | |
6de9cd9a | 2744 | return MATCH_ERROR; |
d198b59a JJ |
2745 | |
2746 | entry->result = result; | |
6de9cd9a DN |
2747 | } |
2748 | ||
3d79abbd | 2749 | if (proc->attr.recursive && result == NULL) |
6de9cd9a DN |
2750 | { |
2751 | gfc_error ("RESULT attribute required in ENTRY statement at %C"); | |
2752 | return MATCH_ERROR; | |
2753 | } | |
6de9cd9a DN |
2754 | } |
2755 | ||
2756 | if (gfc_match_eos () != MATCH_YES) | |
2757 | { | |
2758 | gfc_syntax_error (ST_ENTRY); | |
2759 | return MATCH_ERROR; | |
2760 | } | |
2761 | ||
3d79abbd PB |
2762 | entry->attr.recursive = proc->attr.recursive; |
2763 | entry->attr.elemental = proc->attr.elemental; | |
2764 | entry->attr.pure = proc->attr.pure; | |
6de9cd9a | 2765 | |
3d79abbd PB |
2766 | el = gfc_get_entry_list (); |
2767 | el->sym = entry; | |
2768 | el->next = gfc_current_ns->entries; | |
2769 | gfc_current_ns->entries = el; | |
2770 | if (el->next) | |
2771 | el->id = el->next->id + 1; | |
2772 | else | |
2773 | el->id = 1; | |
6de9cd9a | 2774 | |
3d79abbd PB |
2775 | new_st.op = EXEC_ENTRY; |
2776 | new_st.ext.entry = el; | |
2777 | ||
2778 | return MATCH_YES; | |
6de9cd9a DN |
2779 | } |
2780 | ||
2781 | ||
2782 | /* Match a subroutine statement, including optional prefixes. */ | |
2783 | ||
2784 | match | |
2785 | gfc_match_subroutine (void) | |
2786 | { | |
2787 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2788 | gfc_symbol *sym; | |
2789 | match m; | |
2790 | ||
2791 | if (gfc_current_state () != COMP_NONE | |
2792 | && gfc_current_state () != COMP_INTERFACE | |
2793 | && gfc_current_state () != COMP_CONTAINS) | |
2794 | return MATCH_NO; | |
2795 | ||
2796 | m = match_prefix (NULL); | |
2797 | if (m != MATCH_YES) | |
2798 | return m; | |
2799 | ||
2800 | m = gfc_match ("subroutine% %n", name); | |
2801 | if (m != MATCH_YES) | |
2802 | return m; | |
2803 | ||
2804 | if (get_proc_name (name, &sym)) | |
2805 | return MATCH_ERROR; | |
2806 | gfc_new_block = sym; | |
2807 | ||
231b2fcc | 2808 | if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
2809 | return MATCH_ERROR; |
2810 | ||
2811 | if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES) | |
2812 | return MATCH_ERROR; | |
2813 | ||
2814 | if (gfc_match_eos () != MATCH_YES) | |
2815 | { | |
2816 | gfc_syntax_error (ST_SUBROUTINE); | |
2817 | return MATCH_ERROR; | |
2818 | } | |
2819 | ||
2820 | if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE) | |
2821 | return MATCH_ERROR; | |
2822 | ||
2823 | return MATCH_YES; | |
2824 | } | |
2825 | ||
2826 | ||
1f2959f0 | 2827 | /* Return nonzero if we're currently compiling a contained procedure. */ |
ddc9ce91 TS |
2828 | |
2829 | static int | |
2830 | contained_procedure (void) | |
2831 | { | |
2832 | gfc_state_data *s; | |
2833 | ||
2834 | for (s=gfc_state_stack; s; s=s->previous) | |
2835 | if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION) | |
2836 | && s->previous != NULL | |
2837 | && s->previous->state == COMP_CONTAINS) | |
2838 | return 1; | |
2839 | ||
2840 | return 0; | |
2841 | } | |
2842 | ||
25d8f0a2 TS |
2843 | /* Set the kind of each enumerator. The kind is selected such that it is |
2844 | interoperable with the corresponding C enumeration type, making | |
2845 | sure that -fshort-enums is honored. */ | |
2846 | ||
2847 | static void | |
2848 | set_enum_kind(void) | |
2849 | { | |
2850 | enumerator_history *current_history = NULL; | |
2851 | int kind; | |
2852 | int i; | |
2853 | ||
2854 | if (max_enum == NULL || enum_history == NULL) | |
2855 | return; | |
2856 | ||
2857 | if (!gfc_option.fshort_enums) | |
2858 | return; | |
2859 | ||
2860 | i = 0; | |
2861 | do | |
2862 | { | |
2863 | kind = gfc_integer_kinds[i++].kind; | |
2864 | } | |
2865 | while (kind < gfc_c_int_kind | |
2866 | && gfc_check_integer_range (max_enum->initializer->value.integer, | |
2867 | kind) != ARITH_OK); | |
2868 | ||
2869 | current_history = enum_history; | |
2870 | while (current_history != NULL) | |
2871 | { | |
2872 | current_history->sym->ts.kind = kind; | |
2873 | current_history = current_history->next; | |
2874 | } | |
2875 | } | |
2876 | ||
6de9cd9a DN |
2877 | /* Match any of the various end-block statements. Returns the type of |
2878 | END to the caller. The END INTERFACE, END IF, END DO and END | |
2879 | SELECT statements cannot be replaced by a single END statement. */ | |
2880 | ||
2881 | match | |
2882 | gfc_match_end (gfc_statement * st) | |
2883 | { | |
2884 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
2885 | gfc_compile_state state; | |
2886 | locus old_loc; | |
2887 | const char *block_name; | |
2888 | const char *target; | |
ddc9ce91 | 2889 | int eos_ok; |
6de9cd9a DN |
2890 | match m; |
2891 | ||
63645982 | 2892 | old_loc = gfc_current_locus; |
6de9cd9a DN |
2893 | if (gfc_match ("end") != MATCH_YES) |
2894 | return MATCH_NO; | |
2895 | ||
2896 | state = gfc_current_state (); | |
2897 | block_name = | |
2898 | gfc_current_block () == NULL ? NULL : gfc_current_block ()->name; | |
2899 | ||
2900 | if (state == COMP_CONTAINS) | |
2901 | { | |
2902 | state = gfc_state_stack->previous->state; | |
2903 | block_name = gfc_state_stack->previous->sym == NULL ? NULL | |
2904 | : gfc_state_stack->previous->sym->name; | |
2905 | } | |
2906 | ||
2907 | switch (state) | |
2908 | { | |
2909 | case COMP_NONE: | |
2910 | case COMP_PROGRAM: | |
2911 | *st = ST_END_PROGRAM; | |
2912 | target = " program"; | |
ddc9ce91 | 2913 | eos_ok = 1; |
6de9cd9a DN |
2914 | break; |
2915 | ||
2916 | case COMP_SUBROUTINE: | |
2917 | *st = ST_END_SUBROUTINE; | |
2918 | target = " subroutine"; | |
ddc9ce91 | 2919 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
2920 | break; |
2921 | ||
2922 | case COMP_FUNCTION: | |
2923 | *st = ST_END_FUNCTION; | |
2924 | target = " function"; | |
ddc9ce91 | 2925 | eos_ok = !contained_procedure (); |
6de9cd9a DN |
2926 | break; |
2927 | ||
2928 | case COMP_BLOCK_DATA: | |
2929 | *st = ST_END_BLOCK_DATA; | |
2930 | target = " block data"; | |
ddc9ce91 | 2931 | eos_ok = 1; |
6de9cd9a DN |
2932 | break; |
2933 | ||
2934 | case COMP_MODULE: | |
2935 | *st = ST_END_MODULE; | |
2936 | target = " module"; | |
ddc9ce91 | 2937 | eos_ok = 1; |
6de9cd9a DN |
2938 | break; |
2939 | ||
2940 | case COMP_INTERFACE: | |
2941 | *st = ST_END_INTERFACE; | |
2942 | target = " interface"; | |
ddc9ce91 | 2943 | eos_ok = 0; |
6de9cd9a DN |
2944 | break; |
2945 | ||
2946 | case COMP_DERIVED: | |
2947 | *st = ST_END_TYPE; | |
2948 | target = " type"; | |
ddc9ce91 | 2949 | eos_ok = 0; |
6de9cd9a DN |
2950 | break; |
2951 | ||
2952 | case COMP_IF: | |
2953 | *st = ST_ENDIF; | |
2954 | target = " if"; | |
ddc9ce91 | 2955 | eos_ok = 0; |
6de9cd9a DN |
2956 | break; |
2957 | ||
2958 | case COMP_DO: | |
2959 | *st = ST_ENDDO; | |
2960 | target = " do"; | |
ddc9ce91 | 2961 | eos_ok = 0; |
6de9cd9a DN |
2962 | break; |
2963 | ||
2964 | case COMP_SELECT: | |
2965 | *st = ST_END_SELECT; | |
2966 | target = " select"; | |
ddc9ce91 | 2967 | eos_ok = 0; |
6de9cd9a DN |
2968 | break; |
2969 | ||
2970 | case COMP_FORALL: | |
2971 | *st = ST_END_FORALL; | |
2972 | target = " forall"; | |
ddc9ce91 | 2973 | eos_ok = 0; |
6de9cd9a DN |
2974 | break; |
2975 | ||
2976 | case COMP_WHERE: | |
2977 | *st = ST_END_WHERE; | |
2978 | target = " where"; | |
ddc9ce91 | 2979 | eos_ok = 0; |
6de9cd9a DN |
2980 | break; |
2981 | ||
25d8f0a2 TS |
2982 | case COMP_ENUM: |
2983 | *st = ST_END_ENUM; | |
2984 | target = " enum"; | |
2985 | eos_ok = 0; | |
2986 | last_initializer = NULL; | |
2987 | set_enum_kind (); | |
2988 | gfc_free_enum_history (); | |
2989 | break; | |
2990 | ||
6de9cd9a DN |
2991 | default: |
2992 | gfc_error ("Unexpected END statement at %C"); | |
2993 | goto cleanup; | |
2994 | } | |
2995 | ||
2996 | if (gfc_match_eos () == MATCH_YES) | |
2997 | { | |
ddc9ce91 | 2998 | if (!eos_ok) |
6de9cd9a | 2999 | { |
ddc9ce91 | 3000 | /* We would have required END [something] */ |
59ce85b5 TS |
3001 | gfc_error ("%s statement expected at %L", |
3002 | gfc_ascii_statement (*st), &old_loc); | |
6de9cd9a DN |
3003 | goto cleanup; |
3004 | } | |
3005 | ||
3006 | return MATCH_YES; | |
3007 | } | |
3008 | ||
3009 | /* Verify that we've got the sort of end-block that we're expecting. */ | |
3010 | if (gfc_match (target) != MATCH_YES) | |
3011 | { | |
3012 | gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st)); | |
3013 | goto cleanup; | |
3014 | } | |
3015 | ||
3016 | /* If we're at the end, make sure a block name wasn't required. */ | |
3017 | if (gfc_match_eos () == MATCH_YES) | |
3018 | { | |
3019 | ||
3020 | if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT) | |
3021 | return MATCH_YES; | |
3022 | ||
3023 | if (gfc_current_block () == NULL) | |
3024 | return MATCH_YES; | |
3025 | ||
3026 | gfc_error ("Expected block name of '%s' in %s statement at %C", | |
3027 | block_name, gfc_ascii_statement (*st)); | |
3028 | ||
3029 | return MATCH_ERROR; | |
3030 | } | |
3031 | ||
3032 | /* END INTERFACE has a special handler for its several possible endings. */ | |
3033 | if (*st == ST_END_INTERFACE) | |
3034 | return gfc_match_end_interface (); | |
3035 | ||
3036 | /* We haven't hit the end of statement, so what is left must be an end-name. */ | |
3037 | m = gfc_match_space (); | |
3038 | if (m == MATCH_YES) | |
3039 | m = gfc_match_name (name); | |
3040 | ||
3041 | if (m == MATCH_NO) | |
3042 | gfc_error ("Expected terminating name at %C"); | |
3043 | if (m != MATCH_YES) | |
3044 | goto cleanup; | |
3045 | ||
3046 | if (block_name == NULL) | |
3047 | goto syntax; | |
3048 | ||
3049 | if (strcmp (name, block_name) != 0) | |
3050 | { | |
3051 | gfc_error ("Expected label '%s' for %s statement at %C", block_name, | |
3052 | gfc_ascii_statement (*st)); | |
3053 | goto cleanup; | |
3054 | } | |
3055 | ||
3056 | if (gfc_match_eos () == MATCH_YES) | |
3057 | return MATCH_YES; | |
3058 | ||
3059 | syntax: | |
3060 | gfc_syntax_error (*st); | |
3061 | ||
3062 | cleanup: | |
63645982 | 3063 | gfc_current_locus = old_loc; |
6de9cd9a DN |
3064 | return MATCH_ERROR; |
3065 | } | |
3066 | ||
3067 | ||
3068 | ||
3069 | /***************** Attribute declaration statements ****************/ | |
3070 | ||
3071 | /* Set the attribute of a single variable. */ | |
3072 | ||
3073 | static match | |
3074 | attr_decl1 (void) | |
3075 | { | |
3076 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
3077 | gfc_array_spec *as; | |
3078 | gfc_symbol *sym; | |
3079 | locus var_locus; | |
3080 | match m; | |
3081 | ||
3082 | as = NULL; | |
3083 | ||
3084 | m = gfc_match_name (name); | |
3085 | if (m != MATCH_YES) | |
3086 | goto cleanup; | |
3087 | ||
3088 | if (find_special (name, &sym)) | |
3089 | return MATCH_ERROR; | |
3090 | ||
63645982 | 3091 | var_locus = gfc_current_locus; |
6de9cd9a DN |
3092 | |
3093 | /* Deal with possible array specification for certain attributes. */ | |
3094 | if (current_attr.dimension | |
3095 | || current_attr.allocatable | |
3096 | || current_attr.pointer | |
3097 | || current_attr.target) | |
3098 | { | |
3099 | m = gfc_match_array_spec (&as); | |
3100 | if (m == MATCH_ERROR) | |
3101 | goto cleanup; | |
3102 | ||
3103 | if (current_attr.dimension && m == MATCH_NO) | |
3104 | { | |
3105 | gfc_error | |
3106 | ("Missing array specification at %L in DIMENSION statement", | |
3107 | &var_locus); | |
3108 | m = MATCH_ERROR; | |
3109 | goto cleanup; | |
3110 | } | |
3111 | ||
3112 | if ((current_attr.allocatable || current_attr.pointer) | |
3113 | && (m == MATCH_YES) && (as->type != AS_DEFERRED)) | |
3114 | { | |
3115 | gfc_error ("Array specification must be deferred at %L", | |
3116 | &var_locus); | |
3117 | m = MATCH_ERROR; | |
3118 | goto cleanup; | |
3119 | } | |
3120 | } | |
3121 | ||
3122 | /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */ | |
3123 | if (current_attr.dimension == 0 | |
3124 | && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE) | |
3125 | { | |
3126 | m = MATCH_ERROR; | |
3127 | goto cleanup; | |
3128 | } | |
3129 | ||
3130 | if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE) | |
3131 | { | |
3132 | m = MATCH_ERROR; | |
3133 | goto cleanup; | |
3134 | } | |
83d890b9 AL |
3135 | |
3136 | if (sym->attr.cray_pointee && sym->as != NULL) | |
3137 | { | |
3138 | /* Fix the array spec. */ | |
3139 | m = gfc_mod_pointee_as (sym->as); | |
3140 | if (m == MATCH_ERROR) | |
3141 | goto cleanup; | |
3142 | } | |
6de9cd9a DN |
3143 | |
3144 | if ((current_attr.external || current_attr.intrinsic) | |
3145 | && sym->attr.flavor != FL_PROCEDURE | |
231b2fcc | 3146 | && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
3147 | { |
3148 | m = MATCH_ERROR; | |
3149 | goto cleanup; | |
3150 | } | |
3151 | ||
3152 | return MATCH_YES; | |
3153 | ||
3154 | cleanup: | |
3155 | gfc_free_array_spec (as); | |
3156 | return m; | |
3157 | } | |
3158 | ||
3159 | ||
3160 | /* Generic attribute declaration subroutine. Used for attributes that | |
3161 | just have a list of names. */ | |
3162 | ||
3163 | static match | |
3164 | attr_decl (void) | |
3165 | { | |
3166 | match m; | |
3167 | ||
3168 | /* Gobble the optional double colon, by simply ignoring the result | |
3169 | of gfc_match(). */ | |
3170 | gfc_match (" ::"); | |
3171 | ||
3172 | for (;;) | |
3173 | { | |
3174 | m = attr_decl1 (); | |
3175 | if (m != MATCH_YES) | |
3176 | break; | |
3177 | ||
3178 | if (gfc_match_eos () == MATCH_YES) | |
3179 | { | |
3180 | m = MATCH_YES; | |
3181 | break; | |
3182 | } | |
3183 | ||
3184 | if (gfc_match_char (',') != MATCH_YES) | |
3185 | { | |
3186 | gfc_error ("Unexpected character in variable list at %C"); | |
3187 | m = MATCH_ERROR; | |
3188 | break; | |
3189 | } | |
3190 | } | |
3191 | ||
3192 | return m; | |
3193 | } | |
3194 | ||
3195 | ||
83d890b9 AL |
3196 | /* This routine matches Cray Pointer declarations of the form: |
3197 | pointer ( <pointer>, <pointee> ) | |
3198 | or | |
3199 | pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ... | |
3200 | The pointer, if already declared, should be an integer. Otherwise, we | |
3201 | set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may | |
3202 | be either a scalar, or an array declaration. No space is allocated for | |
3203 | the pointee. For the statement | |
3204 | pointer (ipt, ar(10)) | |
3205 | any subsequent uses of ar will be translated (in C-notation) as | |
3206 | ar(i) => ((<type> *) ipt)(i) | |
b122dc6a | 3207 | After gimplification, pointee variable will disappear in the code. */ |
83d890b9 AL |
3208 | |
3209 | static match | |
3210 | cray_pointer_decl (void) | |
3211 | { | |
3212 | match m; | |
3213 | gfc_array_spec *as; | |
3214 | gfc_symbol *cptr; /* Pointer symbol. */ | |
3215 | gfc_symbol *cpte; /* Pointee symbol. */ | |
3216 | locus var_locus; | |
3217 | bool done = false; | |
3218 | ||
3219 | while (!done) | |
3220 | { | |
3221 | if (gfc_match_char ('(') != MATCH_YES) | |
3222 | { | |
3223 | gfc_error ("Expected '(' at %C"); | |
3224 | return MATCH_ERROR; | |
3225 | } | |
3226 | ||
3227 | /* Match pointer. */ | |
3228 | var_locus = gfc_current_locus; | |
3229 | gfc_clear_attr (¤t_attr); | |
3230 | gfc_add_cray_pointer (¤t_attr, &var_locus); | |
3231 | current_ts.type = BT_INTEGER; | |
3232 | current_ts.kind = gfc_index_integer_kind; | |
3233 | ||
3234 | m = gfc_match_symbol (&cptr, 0); | |
3235 | if (m != MATCH_YES) | |
3236 | { | |
3237 | gfc_error ("Expected variable name at %C"); | |
3238 | return m; | |
3239 | } | |
3240 | ||
3241 | if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE) | |
3242 | return MATCH_ERROR; | |
3243 | ||
3244 | gfc_set_sym_referenced (cptr); | |
3245 | ||
3246 | if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */ | |
3247 | { | |
3248 | cptr->ts.type = BT_INTEGER; | |
3249 | cptr->ts.kind = gfc_index_integer_kind; | |
3250 | } | |
3251 | else if (cptr->ts.type != BT_INTEGER) | |
3252 | { | |
3253 | gfc_error ("Cray pointer at %C must be an integer."); | |
3254 | return MATCH_ERROR; | |
3255 | } | |
3256 | else if (cptr->ts.kind < gfc_index_integer_kind) | |
3257 | gfc_warning ("Cray pointer at %C has %d bytes of precision;" | |
3258 | " memory addresses require %d bytes.", | |
3259 | cptr->ts.kind, | |
3260 | gfc_index_integer_kind); | |
3261 | ||
3262 | if (gfc_match_char (',') != MATCH_YES) | |
3263 | { | |
3264 | gfc_error ("Expected \",\" at %C"); | |
3265 | return MATCH_ERROR; | |
3266 | } | |
3267 | ||
3268 | /* Match Pointee. */ | |
3269 | var_locus = gfc_current_locus; | |
3270 | gfc_clear_attr (¤t_attr); | |
3271 | gfc_add_cray_pointee (¤t_attr, &var_locus); | |
3272 | current_ts.type = BT_UNKNOWN; | |
3273 | current_ts.kind = 0; | |
3274 | ||
3275 | m = gfc_match_symbol (&cpte, 0); | |
3276 | if (m != MATCH_YES) | |
3277 | { | |
3278 | gfc_error ("Expected variable name at %C"); | |
3279 | return m; | |
3280 | } | |
3281 | ||
3282 | /* Check for an optional array spec. */ | |
3283 | m = gfc_match_array_spec (&as); | |
3284 | if (m == MATCH_ERROR) | |
3285 | { | |
3286 | gfc_free_array_spec (as); | |
3287 | return m; | |
3288 | } | |
3289 | else if (m == MATCH_NO) | |
3290 | { | |
3291 | gfc_free_array_spec (as); | |
3292 | as = NULL; | |
3293 | } | |
3294 | ||
3295 | if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE) | |
3296 | return MATCH_ERROR; | |
3297 | ||
3298 | gfc_set_sym_referenced (cpte); | |
3299 | ||
3300 | if (cpte->as == NULL) | |
3301 | { | |
3302 | if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE) | |
3303 | gfc_internal_error ("Couldn't set Cray pointee array spec."); | |
3304 | } | |
3305 | else if (as != NULL) | |
3306 | { | |
3307 | gfc_error ("Duplicate array spec for Cray pointee at %C."); | |
3308 | gfc_free_array_spec (as); | |
3309 | return MATCH_ERROR; | |
3310 | } | |
3311 | ||
3312 | as = NULL; | |
3313 | ||
3314 | if (cpte->as != NULL) | |
3315 | { | |
3316 | /* Fix array spec. */ | |
3317 | m = gfc_mod_pointee_as (cpte->as); | |
3318 | if (m == MATCH_ERROR) | |
3319 | return m; | |
3320 | } | |
3321 | ||
3322 | /* Point the Pointee at the Pointer. */ | |
b122dc6a | 3323 | cpte->cp_pointer = cptr; |
83d890b9 AL |
3324 | |
3325 | if (gfc_match_char (')') != MATCH_YES) | |
3326 | { | |
3327 | gfc_error ("Expected \")\" at %C"); | |
3328 | return MATCH_ERROR; | |
3329 | } | |
3330 | m = gfc_match_char (','); | |
3331 | if (m != MATCH_YES) | |
3332 | done = true; /* Stop searching for more declarations. */ | |
3333 | ||
3334 | } | |
3335 | ||
3336 | if (m == MATCH_ERROR /* Failed when trying to find ',' above. */ | |
3337 | || gfc_match_eos () != MATCH_YES) | |
3338 | { | |
3339 | gfc_error ("Expected \",\" or end of statement at %C"); | |
3340 | return MATCH_ERROR; | |
3341 | } | |
3342 | return MATCH_YES; | |
3343 | } | |
3344 | ||
3345 | ||
6de9cd9a DN |
3346 | match |
3347 | gfc_match_external (void) | |
3348 | { | |
3349 | ||
3350 | gfc_clear_attr (¤t_attr); | |
3351 | gfc_add_external (¤t_attr, NULL); | |
3352 | ||
3353 | return attr_decl (); | |
3354 | } | |
3355 | ||
3356 | ||
3357 | ||
3358 | match | |
3359 | gfc_match_intent (void) | |
3360 | { | |
3361 | sym_intent intent; | |
3362 | ||
3363 | intent = match_intent_spec (); | |
3364 | if (intent == INTENT_UNKNOWN) | |
3365 | return MATCH_ERROR; | |
3366 | ||
3367 | gfc_clear_attr (¤t_attr); | |
3368 | gfc_add_intent (¤t_attr, intent, NULL); /* Can't fail */ | |
3369 | ||
3370 | return attr_decl (); | |
3371 | } | |
3372 | ||
3373 | ||
3374 | match | |
3375 | gfc_match_intrinsic (void) | |
3376 | { | |
3377 | ||
3378 | gfc_clear_attr (¤t_attr); | |
3379 | gfc_add_intrinsic (¤t_attr, NULL); | |
3380 | ||
3381 | return attr_decl (); | |
3382 | } | |
3383 | ||
3384 | ||
3385 | match | |
3386 | gfc_match_optional (void) | |
3387 | { | |
3388 | ||
3389 | gfc_clear_attr (¤t_attr); | |
3390 | gfc_add_optional (¤t_attr, NULL); | |
3391 | ||
3392 | return attr_decl (); | |
3393 | } | |
3394 | ||
3395 | ||
3396 | match | |
3397 | gfc_match_pointer (void) | |
3398 | { | |
83d890b9 AL |
3399 | gfc_gobble_whitespace (); |
3400 | if (gfc_peek_char () == '(') | |
3401 | { | |
3402 | if (!gfc_option.flag_cray_pointer) | |
3403 | { | |
3404 | gfc_error ("Cray pointer declaration at %C requires -fcray-pointer" | |
3405 | " flag."); | |
3406 | return MATCH_ERROR; | |
3407 | } | |
3408 | return cray_pointer_decl (); | |
3409 | } | |
3410 | else | |
3411 | { | |
3412 | gfc_clear_attr (¤t_attr); | |
3413 | gfc_add_pointer (¤t_attr, NULL); | |
3414 | ||
3415 | return attr_decl (); | |
3416 | } | |
6de9cd9a DN |
3417 | } |
3418 | ||
3419 | ||
3420 | match | |
3421 | gfc_match_allocatable (void) | |
3422 | { | |
3423 | ||
3424 | gfc_clear_attr (¤t_attr); | |
3425 | gfc_add_allocatable (¤t_attr, NULL); | |
3426 | ||
3427 | return attr_decl (); | |
3428 | } | |
3429 | ||
3430 | ||
3431 | match | |
3432 | gfc_match_dimension (void) | |
3433 | { | |
3434 | ||
3435 | gfc_clear_attr (¤t_attr); | |
231b2fcc | 3436 | gfc_add_dimension (¤t_attr, NULL, NULL); |
6de9cd9a DN |
3437 | |
3438 | return attr_decl (); | |
3439 | } | |
3440 | ||
3441 | ||
3442 | match | |
3443 | gfc_match_target (void) | |
3444 | { | |
3445 | ||
3446 | gfc_clear_attr (¤t_attr); | |
3447 | gfc_add_target (¤t_attr, NULL); | |
3448 | ||
3449 | return attr_decl (); | |
3450 | } | |
3451 | ||
3452 | ||
3453 | /* Match the list of entities being specified in a PUBLIC or PRIVATE | |
3454 | statement. */ | |
3455 | ||
3456 | static match | |
3457 | access_attr_decl (gfc_statement st) | |
3458 | { | |
3459 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
3460 | interface_type type; | |
3461 | gfc_user_op *uop; | |
3462 | gfc_symbol *sym; | |
3463 | gfc_intrinsic_op operator; | |
3464 | match m; | |
3465 | ||
3466 | if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO) | |
3467 | goto done; | |
3468 | ||
3469 | for (;;) | |
3470 | { | |
3471 | m = gfc_match_generic_spec (&type, name, &operator); | |
3472 | if (m == MATCH_NO) | |
3473 | goto syntax; | |
3474 | if (m == MATCH_ERROR) | |
3475 | return MATCH_ERROR; | |
3476 | ||
3477 | switch (type) | |
3478 | { | |
3479 | case INTERFACE_NAMELESS: | |
3480 | goto syntax; | |
3481 | ||
3482 | case INTERFACE_GENERIC: | |
3483 | if (gfc_get_symbol (name, NULL, &sym)) | |
3484 | goto done; | |
3485 | ||
3486 | if (gfc_add_access (&sym->attr, | |
3487 | (st == | |
3488 | ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE, | |
231b2fcc | 3489 | sym->name, NULL) == FAILURE) |
6de9cd9a DN |
3490 | return MATCH_ERROR; |
3491 | ||
3492 | break; | |
3493 | ||
3494 | case INTERFACE_INTRINSIC_OP: | |
3495 | if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN) | |
3496 | { | |
3497 | gfc_current_ns->operator_access[operator] = | |
3498 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
3499 | } | |
3500 | else | |
3501 | { | |
3502 | gfc_error ("Access specification of the %s operator at %C has " | |
3503 | "already been specified", gfc_op2string (operator)); | |
3504 | goto done; | |
3505 | } | |
3506 | ||
3507 | break; | |
3508 | ||
3509 | case INTERFACE_USER_OP: | |
3510 | uop = gfc_get_uop (name); | |
3511 | ||
3512 | if (uop->access == ACCESS_UNKNOWN) | |
3513 | { | |
3514 | uop->access = | |
3515 | (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE; | |
3516 | } | |
3517 | else | |
3518 | { | |
3519 | gfc_error | |
3520 | ("Access specification of the .%s. operator at %C has " | |
3521 | "already been specified", sym->name); | |
3522 | goto done; | |
3523 | } | |
3524 | ||
3525 | break; | |
3526 | } | |
3527 | ||
3528 | if (gfc_match_char (',') == MATCH_NO) | |
3529 | break; | |
3530 | } | |
3531 | ||
3532 | if (gfc_match_eos () != MATCH_YES) | |
3533 | goto syntax; | |
3534 | return MATCH_YES; | |
3535 | ||
3536 | syntax: | |
3537 | gfc_syntax_error (st); | |
3538 | ||
3539 | done: | |
3540 | return MATCH_ERROR; | |
3541 | } | |
3542 | ||
3543 | ||
3544 | /* The PRIVATE statement is a bit weird in that it can be a attribute | |
3545 | declaration, but also works as a standlone statement inside of a | |
3546 | type declaration or a module. */ | |
3547 | ||
3548 | match | |
3549 | gfc_match_private (gfc_statement * st) | |
3550 | { | |
3551 | ||
3552 | if (gfc_match ("private") != MATCH_YES) | |
3553 | return MATCH_NO; | |
3554 | ||
3555 | if (gfc_current_state () == COMP_DERIVED) | |
3556 | { | |
3557 | if (gfc_match_eos () == MATCH_YES) | |
3558 | { | |
3559 | *st = ST_PRIVATE; | |
3560 | return MATCH_YES; | |
3561 | } | |
3562 | ||
3563 | gfc_syntax_error (ST_PRIVATE); | |
3564 | return MATCH_ERROR; | |
3565 | } | |
3566 | ||
3567 | if (gfc_match_eos () == MATCH_YES) | |
3568 | { | |
3569 | *st = ST_PRIVATE; | |
3570 | return MATCH_YES; | |
3571 | } | |
3572 | ||
3573 | *st = ST_ATTR_DECL; | |
3574 | return access_attr_decl (ST_PRIVATE); | |
3575 | } | |
3576 | ||
3577 | ||
3578 | match | |
3579 | gfc_match_public (gfc_statement * st) | |
3580 | { | |
3581 | ||
3582 | if (gfc_match ("public") != MATCH_YES) | |
3583 | return MATCH_NO; | |
3584 | ||
3585 | if (gfc_match_eos () == MATCH_YES) | |
3586 | { | |
3587 | *st = ST_PUBLIC; | |
3588 | return MATCH_YES; | |
3589 | } | |
3590 | ||
3591 | *st = ST_ATTR_DECL; | |
3592 | return access_attr_decl (ST_PUBLIC); | |
3593 | } | |
3594 | ||
3595 | ||
3596 | /* Workhorse for gfc_match_parameter. */ | |
3597 | ||
3598 | static match | |
3599 | do_parm (void) | |
3600 | { | |
3601 | gfc_symbol *sym; | |
3602 | gfc_expr *init; | |
3603 | match m; | |
3604 | ||
3605 | m = gfc_match_symbol (&sym, 0); | |
3606 | if (m == MATCH_NO) | |
3607 | gfc_error ("Expected variable name at %C in PARAMETER statement"); | |
3608 | ||
3609 | if (m != MATCH_YES) | |
3610 | return m; | |
3611 | ||
3612 | if (gfc_match_char ('=') == MATCH_NO) | |
3613 | { | |
3614 | gfc_error ("Expected = sign in PARAMETER statement at %C"); | |
3615 | return MATCH_ERROR; | |
3616 | } | |
3617 | ||
3618 | m = gfc_match_init_expr (&init); | |
3619 | if (m == MATCH_NO) | |
3620 | gfc_error ("Expected expression at %C in PARAMETER statement"); | |
3621 | if (m != MATCH_YES) | |
3622 | return m; | |
3623 | ||
3624 | if (sym->ts.type == BT_UNKNOWN | |
3625 | && gfc_set_default_type (sym, 1, NULL) == FAILURE) | |
3626 | { | |
3627 | m = MATCH_ERROR; | |
3628 | goto cleanup; | |
3629 | } | |
3630 | ||
3631 | if (gfc_check_assign_symbol (sym, init) == FAILURE | |
231b2fcc | 3632 | || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
3633 | { |
3634 | m = MATCH_ERROR; | |
3635 | goto cleanup; | |
3636 | } | |
3637 | ||
7e2eba4b DE |
3638 | if (sym->ts.type == BT_CHARACTER |
3639 | && sym->ts.cl != NULL | |
3640 | && sym->ts.cl->length != NULL | |
3641 | && sym->ts.cl->length->expr_type == EXPR_CONSTANT | |
3642 | && init->expr_type == EXPR_CONSTANT | |
3643 | && init->ts.type == BT_CHARACTER | |
3644 | && init->ts.kind == 1) | |
3645 | gfc_set_constant_character_len ( | |
3646 | mpz_get_si (sym->ts.cl->length->value.integer), init); | |
3647 | ||
6de9cd9a DN |
3648 | sym->value = init; |
3649 | return MATCH_YES; | |
3650 | ||
3651 | cleanup: | |
3652 | gfc_free_expr (init); | |
3653 | return m; | |
3654 | } | |
3655 | ||
3656 | ||
3657 | /* Match a parameter statement, with the weird syntax that these have. */ | |
3658 | ||
3659 | match | |
3660 | gfc_match_parameter (void) | |
3661 | { | |
3662 | match m; | |
3663 | ||
3664 | if (gfc_match_char ('(') == MATCH_NO) | |
3665 | return MATCH_NO; | |
3666 | ||
3667 | for (;;) | |
3668 | { | |
3669 | m = do_parm (); | |
3670 | if (m != MATCH_YES) | |
3671 | break; | |
3672 | ||
3673 | if (gfc_match (" )%t") == MATCH_YES) | |
3674 | break; | |
3675 | ||
3676 | if (gfc_match_char (',') != MATCH_YES) | |
3677 | { | |
3678 | gfc_error ("Unexpected characters in PARAMETER statement at %C"); | |
3679 | m = MATCH_ERROR; | |
3680 | break; | |
3681 | } | |
3682 | } | |
3683 | ||
3684 | return m; | |
3685 | } | |
3686 | ||
3687 | ||
3688 | /* Save statements have a special syntax. */ | |
3689 | ||
3690 | match | |
3691 | gfc_match_save (void) | |
3692 | { | |
9056bd70 TS |
3693 | char n[GFC_MAX_SYMBOL_LEN+1]; |
3694 | gfc_common_head *c; | |
6de9cd9a DN |
3695 | gfc_symbol *sym; |
3696 | match m; | |
3697 | ||
3698 | if (gfc_match_eos () == MATCH_YES) | |
3699 | { | |
3700 | if (gfc_current_ns->seen_save) | |
3701 | { | |
09e87839 AL |
3702 | if (gfc_notify_std (GFC_STD_LEGACY, |
3703 | "Blanket SAVE statement at %C follows previous " | |
3704 | "SAVE statement") | |
3705 | == FAILURE) | |
3706 | return MATCH_ERROR; | |
6de9cd9a DN |
3707 | } |
3708 | ||
3709 | gfc_current_ns->save_all = gfc_current_ns->seen_save = 1; | |
3710 | return MATCH_YES; | |
3711 | } | |
3712 | ||
3713 | if (gfc_current_ns->save_all) | |
3714 | { | |
09e87839 AL |
3715 | if (gfc_notify_std (GFC_STD_LEGACY, |
3716 | "SAVE statement at %C follows blanket SAVE statement") | |
3717 | == FAILURE) | |
3718 | return MATCH_ERROR; | |
6de9cd9a DN |
3719 | } |
3720 | ||
3721 | gfc_match (" ::"); | |
3722 | ||
3723 | for (;;) | |
3724 | { | |
3725 | m = gfc_match_symbol (&sym, 0); | |
3726 | switch (m) | |
3727 | { | |
3728 | case MATCH_YES: | |
231b2fcc TS |
3729 | if (gfc_add_save (&sym->attr, sym->name, |
3730 | &gfc_current_locus) == FAILURE) | |
6de9cd9a DN |
3731 | return MATCH_ERROR; |
3732 | goto next_item; | |
3733 | ||
3734 | case MATCH_NO: | |
3735 | break; | |
3736 | ||
3737 | case MATCH_ERROR: | |
3738 | return MATCH_ERROR; | |
3739 | } | |
3740 | ||
9056bd70 | 3741 | m = gfc_match (" / %n /", &n); |
6de9cd9a DN |
3742 | if (m == MATCH_ERROR) |
3743 | return MATCH_ERROR; | |
3744 | if (m == MATCH_NO) | |
3745 | goto syntax; | |
3746 | ||
53814b8f | 3747 | c = gfc_get_common (n, 0); |
9056bd70 TS |
3748 | c->saved = 1; |
3749 | ||
6de9cd9a DN |
3750 | gfc_current_ns->seen_save = 1; |
3751 | ||
3752 | next_item: | |
3753 | if (gfc_match_eos () == MATCH_YES) | |
3754 | break; | |
3755 | if (gfc_match_char (',') != MATCH_YES) | |
3756 | goto syntax; | |
3757 | } | |
3758 | ||
3759 | return MATCH_YES; | |
3760 | ||
3761 | syntax: | |
3762 | gfc_error ("Syntax error in SAVE statement at %C"); | |
3763 | return MATCH_ERROR; | |
3764 | } | |
3765 | ||
3766 | ||
3767 | /* Match a module procedure statement. Note that we have to modify | |
3768 | symbols in the parent's namespace because the current one was there | |
49de9e73 | 3769 | to receive symbols that are in an interface's formal argument list. */ |
6de9cd9a DN |
3770 | |
3771 | match | |
3772 | gfc_match_modproc (void) | |
3773 | { | |
3774 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
3775 | gfc_symbol *sym; | |
3776 | match m; | |
3777 | ||
3778 | if (gfc_state_stack->state != COMP_INTERFACE | |
3779 | || gfc_state_stack->previous == NULL | |
3780 | || current_interface.type == INTERFACE_NAMELESS) | |
3781 | { | |
3782 | gfc_error | |
3783 | ("MODULE PROCEDURE at %C must be in a generic module interface"); | |
3784 | return MATCH_ERROR; | |
3785 | } | |
3786 | ||
3787 | for (;;) | |
3788 | { | |
3789 | m = gfc_match_name (name); | |
3790 | if (m == MATCH_NO) | |
3791 | goto syntax; | |
3792 | if (m != MATCH_YES) | |
3793 | return MATCH_ERROR; | |
3794 | ||
3795 | if (gfc_get_symbol (name, gfc_current_ns->parent, &sym)) | |
3796 | return MATCH_ERROR; | |
3797 | ||
3798 | if (sym->attr.proc != PROC_MODULE | |
231b2fcc TS |
3799 | && gfc_add_procedure (&sym->attr, PROC_MODULE, |
3800 | sym->name, NULL) == FAILURE) | |
6de9cd9a DN |
3801 | return MATCH_ERROR; |
3802 | ||
3803 | if (gfc_add_interface (sym) == FAILURE) | |
3804 | return MATCH_ERROR; | |
3805 | ||
3806 | if (gfc_match_eos () == MATCH_YES) | |
3807 | break; | |
3808 | if (gfc_match_char (',') != MATCH_YES) | |
3809 | goto syntax; | |
3810 | } | |
3811 | ||
3812 | return MATCH_YES; | |
3813 | ||
3814 | syntax: | |
3815 | gfc_syntax_error (ST_MODULE_PROC); | |
3816 | return MATCH_ERROR; | |
3817 | } | |
3818 | ||
3819 | ||
3820 | /* Match the beginning of a derived type declaration. If a type name | |
3821 | was the result of a function, then it is possible to have a symbol | |
3822 | already to be known as a derived type yet have no components. */ | |
3823 | ||
3824 | match | |
3825 | gfc_match_derived_decl (void) | |
3826 | { | |
3827 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
3828 | symbol_attribute attr; | |
3829 | gfc_symbol *sym; | |
3830 | match m; | |
3831 | ||
3832 | if (gfc_current_state () == COMP_DERIVED) | |
3833 | return MATCH_NO; | |
3834 | ||
3835 | gfc_clear_attr (&attr); | |
3836 | ||
3837 | loop: | |
3838 | if (gfc_match (" , private") == MATCH_YES) | |
3839 | { | |
3840 | if (gfc_find_state (COMP_MODULE) == FAILURE) | |
3841 | { | |
3842 | gfc_error | |
3843 | ("Derived type at %C can only be PRIVATE within a MODULE"); | |
3844 | return MATCH_ERROR; | |
3845 | } | |
3846 | ||
231b2fcc | 3847 | if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE) |
6de9cd9a DN |
3848 | return MATCH_ERROR; |
3849 | goto loop; | |
3850 | } | |
3851 | ||
3852 | if (gfc_match (" , public") == MATCH_YES) | |
3853 | { | |
3854 | if (gfc_find_state (COMP_MODULE) == FAILURE) | |
3855 | { | |
3856 | gfc_error ("Derived type at %C can only be PUBLIC within a MODULE"); | |
3857 | return MATCH_ERROR; | |
3858 | } | |
3859 | ||
231b2fcc | 3860 | if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE) |
6de9cd9a DN |
3861 | return MATCH_ERROR; |
3862 | goto loop; | |
3863 | } | |
3864 | ||
3865 | if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN) | |
3866 | { | |
3867 | gfc_error ("Expected :: in TYPE definition at %C"); | |
3868 | return MATCH_ERROR; | |
3869 | } | |
3870 | ||
3871 | m = gfc_match (" %n%t", name); | |
3872 | if (m != MATCH_YES) | |
3873 | return m; | |
3874 | ||
3875 | /* Make sure the name isn't the name of an intrinsic type. The | |
3876 | 'double precision' type doesn't get past the name matcher. */ | |
3877 | if (strcmp (name, "integer") == 0 | |
3878 | || strcmp (name, "real") == 0 | |
3879 | || strcmp (name, "character") == 0 | |
3880 | || strcmp (name, "logical") == 0 | |
3881 | || strcmp (name, "complex") == 0) | |
3882 | { | |
3883 | gfc_error | |
3884 | ("Type name '%s' at %C cannot be the same as an intrinsic type", | |
3885 | name); | |
3886 | return MATCH_ERROR; | |
3887 | } | |
3888 | ||
3889 | if (gfc_get_symbol (name, NULL, &sym)) | |
3890 | return MATCH_ERROR; | |
3891 | ||
3892 | if (sym->ts.type != BT_UNKNOWN) | |
3893 | { | |
3894 | gfc_error ("Derived type name '%s' at %C already has a basic type " | |
3895 | "of %s", sym->name, gfc_typename (&sym->ts)); | |
3896 | return MATCH_ERROR; | |
3897 | } | |
3898 | ||
3899 | /* The symbol may already have the derived attribute without the | |
3900 | components. The ways this can happen is via a function | |
3901 | definition, an INTRINSIC statement or a subtype in another | |
3902 | derived type that is a pointer. The first part of the AND clause | |
f7b529fa | 3903 | is true if a the symbol is not the return value of a function. */ |
6de9cd9a | 3904 | if (sym->attr.flavor != FL_DERIVED |
231b2fcc | 3905 | && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
3906 | return MATCH_ERROR; |
3907 | ||
3908 | if (sym->components != NULL) | |
3909 | { | |
3910 | gfc_error | |
3911 | ("Derived type definition of '%s' at %C has already been defined", | |
3912 | sym->name); | |
3913 | return MATCH_ERROR; | |
3914 | } | |
3915 | ||
3916 | if (attr.access != ACCESS_UNKNOWN | |
231b2fcc | 3917 | && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE) |
6de9cd9a DN |
3918 | return MATCH_ERROR; |
3919 | ||
3920 | gfc_new_block = sym; | |
3921 | ||
3922 | return MATCH_YES; | |
3923 | } | |
83d890b9 AL |
3924 | |
3925 | ||
3926 | /* Cray Pointees can be declared as: | |
3927 | pointer (ipt, a (n,m,...,*)) | |
3928 | By default, this is treated as an AS_ASSUMED_SIZE array. We'll | |
3929 | cheat and set a constant bound of 1 for the last dimension, if this | |
3930 | is the case. Since there is no bounds-checking for Cray Pointees, | |
3931 | this will be okay. */ | |
3932 | ||
3933 | try | |
3934 | gfc_mod_pointee_as (gfc_array_spec *as) | |
3935 | { | |
3936 | as->cray_pointee = true; /* This will be useful to know later. */ | |
3937 | if (as->type == AS_ASSUMED_SIZE) | |
3938 | { | |
3939 | as->type = AS_EXPLICIT; | |
3940 | as->upper[as->rank - 1] = gfc_int_expr (1); | |
3941 | as->cp_was_assumed = true; | |
3942 | } | |
3943 | else if (as->type == AS_ASSUMED_SHAPE) | |
3944 | { | |
3945 | gfc_error ("Cray Pointee at %C cannot be assumed shape array"); | |
3946 | return MATCH_ERROR; | |
3947 | } | |
3948 | return MATCH_YES; | |
3949 | } | |
25d8f0a2 TS |
3950 | |
3951 | ||
3952 | /* Match the enum definition statement, here we are trying to match | |
3953 | the first line of enum definition statement. | |
3954 | Returns MATCH_YES if match is found. */ | |
3955 | ||
3956 | match | |
3957 | gfc_match_enum (void) | |
3958 | { | |
3959 | match m; | |
3960 | ||
3961 | m = gfc_match_eos (); | |
3962 | if (m != MATCH_YES) | |
3963 | return m; | |
3964 | ||
3965 | if (gfc_notify_std (GFC_STD_F2003, | |
3966 | "New in Fortran 2003: ENUM AND ENUMERATOR at %C") | |
3967 | == FAILURE) | |
3968 | return MATCH_ERROR; | |
3969 | ||
3970 | return MATCH_YES; | |
3971 | } | |
3972 | ||
3973 | ||
3974 | /* Match the enumerator definition statement. */ | |
3975 | ||
3976 | match | |
3977 | gfc_match_enumerator_def (void) | |
3978 | { | |
3979 | match m; | |
3980 | int elem; | |
3981 | ||
3982 | gfc_clear_ts (¤t_ts); | |
3983 | ||
3984 | m = gfc_match (" enumerator"); | |
3985 | if (m != MATCH_YES) | |
3986 | return m; | |
3987 | ||
3988 | if (gfc_current_state () != COMP_ENUM) | |
3989 | { | |
3990 | gfc_error ("ENUM definition statement expected before %C"); | |
3991 | gfc_free_enum_history (); | |
3992 | return MATCH_ERROR; | |
3993 | } | |
3994 | ||
3995 | (¤t_ts)->type = BT_INTEGER; | |
3996 | (¤t_ts)->kind = gfc_c_int_kind; | |
3997 | ||
3998 | m = match_attr_spec (); | |
3999 | if (m == MATCH_ERROR) | |
4000 | { | |
4001 | m = MATCH_NO; | |
4002 | goto cleanup; | |
4003 | } | |
4004 | ||
4005 | elem = 1; | |
4006 | for (;;) | |
4007 | { | |
4008 | m = variable_decl (elem++); | |
4009 | if (m == MATCH_ERROR) | |
4010 | goto cleanup; | |
4011 | if (m == MATCH_NO) | |
4012 | break; | |
4013 | ||
4014 | if (gfc_match_eos () == MATCH_YES) | |
4015 | goto cleanup; | |
4016 | if (gfc_match_char (',') != MATCH_YES) | |
4017 | break; | |
4018 | } | |
4019 | ||
4020 | if (gfc_current_state () == COMP_ENUM) | |
4021 | { | |
4022 | gfc_free_enum_history (); | |
4023 | gfc_error ("Syntax error in ENUMERATOR definition at %C"); | |
4024 | m = MATCH_ERROR; | |
4025 | } | |
4026 | ||
4027 | cleanup: | |
4028 | gfc_free_array_spec (current_as); | |
4029 | current_as = NULL; | |
4030 | return m; | |
4031 | ||
4032 | } | |
4033 |