]>
Commit | Line | Data |
---|---|---|
6de9cd9a DN |
1 | /* Deal with interfaces. |
2 | Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. | |
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 TS |
18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
6de9cd9a DN |
21 | |
22 | ||
23 | /* Deal with interfaces. An explicit interface is represented as a | |
24 | singly linked list of formal argument structures attached to the | |
25 | relevant symbols. For an implicit interface, the arguments don't | |
26 | point to symbols. Explicit interfaces point to namespaces that | |
27 | contain the symbols within that interface. | |
28 | ||
29 | Implicit interfaces are linked together in a singly linked list | |
30 | along the next_if member of symbol nodes. Since a particular | |
31 | symbol can only have a single explicit interface, the symbol cannot | |
32 | be part of multiple lists and a single next-member suffices. | |
33 | ||
34 | This is not the case for general classes, though. An operator | |
35 | definition is independent of just about all other uses and has it's | |
36 | own head pointer. | |
37 | ||
38 | Nameless interfaces: | |
39 | Nameless interfaces create symbols with explicit interfaces within | |
40 | the current namespace. They are otherwise unlinked. | |
41 | ||
42 | Generic interfaces: | |
43 | The generic name points to a linked list of symbols. Each symbol | |
44 | has an explicit interface. Each explicit interface has it's own | |
45 | namespace containing the arguments. Module procedures are symbols in | |
46 | which the interface is added later when the module procedure is parsed. | |
47 | ||
48 | User operators: | |
49 | User-defined operators are stored in a their own set of symtrees | |
50 | separate from regular symbols. The symtrees point to gfc_user_op | |
51 | structures which in turn head up a list of relevant interfaces. | |
52 | ||
53 | Extended intrinsics and assignment: | |
54 | The head of these interface lists are stored in the containing namespace. | |
55 | ||
56 | Implicit interfaces: | |
57 | An implicit interface is represented as a singly linked list of | |
58 | formal argument list structures that don't point to any symbol | |
59 | nodes -- they just contain types. | |
60 | ||
61 | ||
62 | When a subprogram is defined, the program unit's name points to an | |
63 | interface as usual, but the link to the namespace is NULL and the | |
64 | formal argument list points to symbols within the same namespace as | |
65 | the program unit name. */ | |
66 | ||
67 | #include "config.h" | |
68 | #include <string.h> | |
69 | #include <stdlib.h> | |
70 | ||
71 | #include "gfortran.h" | |
72 | #include "match.h" | |
73 | ||
74 | ||
75 | /* The current_interface structure holds information about the | |
76 | interface currently being parsed. This structure is saved and | |
77 | restored during recursive interfaces. */ | |
78 | ||
79 | gfc_interface_info current_interface; | |
80 | ||
81 | ||
82 | /* Free a singly linked list of gfc_interface structures. */ | |
83 | ||
84 | void | |
85 | gfc_free_interface (gfc_interface * intr) | |
86 | { | |
87 | gfc_interface *next; | |
88 | ||
89 | for (; intr; intr = next) | |
90 | { | |
91 | next = intr->next; | |
92 | gfc_free (intr); | |
93 | } | |
94 | } | |
95 | ||
96 | ||
97 | /* Change the operators unary plus and minus into binary plus and | |
98 | minus respectively, leaving the rest unchanged. */ | |
99 | ||
100 | static gfc_intrinsic_op | |
101 | fold_unary (gfc_intrinsic_op operator) | |
102 | { | |
103 | ||
104 | switch (operator) | |
105 | { | |
106 | case INTRINSIC_UPLUS: | |
107 | operator = INTRINSIC_PLUS; | |
108 | break; | |
109 | case INTRINSIC_UMINUS: | |
110 | operator = INTRINSIC_MINUS; | |
111 | break; | |
112 | default: | |
113 | break; | |
114 | } | |
115 | ||
116 | return operator; | |
117 | } | |
118 | ||
119 | ||
120 | /* Match a generic specification. Depending on which type of | |
121 | interface is found, the 'name' or 'operator' pointers may be set. | |
122 | This subroutine doesn't return MATCH_NO. */ | |
123 | ||
124 | match | |
125 | gfc_match_generic_spec (interface_type * type, | |
126 | char *name, | |
127 | gfc_intrinsic_op *operator) | |
128 | { | |
129 | char buffer[GFC_MAX_SYMBOL_LEN + 1]; | |
130 | match m; | |
131 | gfc_intrinsic_op i; | |
132 | ||
133 | if (gfc_match (" assignment ( = )") == MATCH_YES) | |
134 | { | |
135 | *type = INTERFACE_INTRINSIC_OP; | |
136 | *operator = INTRINSIC_ASSIGN; | |
137 | return MATCH_YES; | |
138 | } | |
139 | ||
140 | if (gfc_match (" operator ( %o )", &i) == MATCH_YES) | |
141 | { /* Operator i/f */ | |
142 | *type = INTERFACE_INTRINSIC_OP; | |
143 | *operator = fold_unary (i); | |
144 | return MATCH_YES; | |
145 | } | |
146 | ||
147 | if (gfc_match (" operator ( ") == MATCH_YES) | |
148 | { | |
149 | m = gfc_match_defined_op_name (buffer, 1); | |
150 | if (m == MATCH_NO) | |
151 | goto syntax; | |
152 | if (m != MATCH_YES) | |
153 | return MATCH_ERROR; | |
154 | ||
155 | m = gfc_match_char (')'); | |
156 | if (m == MATCH_NO) | |
157 | goto syntax; | |
158 | if (m != MATCH_YES) | |
159 | return MATCH_ERROR; | |
160 | ||
161 | strcpy (name, buffer); | |
162 | *type = INTERFACE_USER_OP; | |
163 | return MATCH_YES; | |
164 | } | |
165 | ||
166 | if (gfc_match_name (buffer) == MATCH_YES) | |
167 | { | |
168 | strcpy (name, buffer); | |
169 | *type = INTERFACE_GENERIC; | |
170 | return MATCH_YES; | |
171 | } | |
172 | ||
173 | *type = INTERFACE_NAMELESS; | |
174 | return MATCH_YES; | |
175 | ||
176 | syntax: | |
177 | gfc_error ("Syntax error in generic specification at %C"); | |
178 | return MATCH_ERROR; | |
179 | } | |
180 | ||
181 | ||
182 | /* Match one of the five forms of an interface statement. */ | |
183 | ||
184 | match | |
185 | gfc_match_interface (void) | |
186 | { | |
187 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
188 | interface_type type; | |
189 | gfc_symbol *sym; | |
190 | gfc_intrinsic_op operator; | |
191 | match m; | |
192 | ||
193 | m = gfc_match_space (); | |
194 | ||
195 | if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR) | |
196 | return MATCH_ERROR; | |
197 | ||
198 | ||
199 | /* If we're not looking at the end of the statement now, or if this | |
200 | is not a nameless interface but we did not see a space, punt. */ | |
201 | if (gfc_match_eos () != MATCH_YES | |
202 | || (type != INTERFACE_NAMELESS | |
203 | && m != MATCH_YES)) | |
204 | { | |
205 | gfc_error | |
206 | ("Syntax error: Trailing garbage in INTERFACE statement at %C"); | |
207 | return MATCH_ERROR; | |
208 | } | |
209 | ||
210 | current_interface.type = type; | |
211 | ||
212 | switch (type) | |
213 | { | |
214 | case INTERFACE_GENERIC: | |
215 | if (gfc_get_symbol (name, NULL, &sym)) | |
216 | return MATCH_ERROR; | |
217 | ||
218 | if (!sym->attr.generic && gfc_add_generic (&sym->attr, NULL) == FAILURE) | |
219 | return MATCH_ERROR; | |
220 | ||
221 | current_interface.sym = gfc_new_block = sym; | |
222 | break; | |
223 | ||
224 | case INTERFACE_USER_OP: | |
225 | current_interface.uop = gfc_get_uop (name); | |
226 | break; | |
227 | ||
228 | case INTERFACE_INTRINSIC_OP: | |
229 | current_interface.op = operator; | |
230 | break; | |
231 | ||
232 | case INTERFACE_NAMELESS: | |
233 | break; | |
234 | } | |
235 | ||
236 | return MATCH_YES; | |
237 | } | |
238 | ||
239 | ||
240 | /* Match the different sort of generic-specs that can be present after | |
241 | the END INTERFACE itself. */ | |
242 | ||
243 | match | |
244 | gfc_match_end_interface (void) | |
245 | { | |
246 | char name[GFC_MAX_SYMBOL_LEN + 1]; | |
247 | interface_type type; | |
248 | gfc_intrinsic_op operator; | |
249 | match m; | |
250 | ||
251 | m = gfc_match_space (); | |
252 | ||
253 | if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR) | |
254 | return MATCH_ERROR; | |
255 | ||
256 | /* If we're not looking at the end of the statement now, or if this | |
257 | is not a nameless interface but we did not see a space, punt. */ | |
258 | if (gfc_match_eos () != MATCH_YES | |
259 | || (type != INTERFACE_NAMELESS | |
260 | && m != MATCH_YES)) | |
261 | { | |
262 | gfc_error | |
263 | ("Syntax error: Trailing garbage in END INTERFACE statement at %C"); | |
264 | return MATCH_ERROR; | |
265 | } | |
266 | ||
267 | m = MATCH_YES; | |
268 | ||
269 | switch (current_interface.type) | |
270 | { | |
271 | case INTERFACE_NAMELESS: | |
272 | if (type != current_interface.type) | |
273 | { | |
274 | gfc_error ("Expected a nameless interface at %C"); | |
275 | m = MATCH_ERROR; | |
276 | } | |
277 | ||
278 | break; | |
279 | ||
280 | case INTERFACE_INTRINSIC_OP: | |
281 | if (type != current_interface.type || operator != current_interface.op) | |
282 | { | |
283 | ||
284 | if (current_interface.op == INTRINSIC_ASSIGN) | |
285 | gfc_error ("Expected 'END INTERFACE ASSIGNMENT (=)' at %C"); | |
286 | else | |
287 | gfc_error ("Expecting 'END INTERFACE OPERATOR (%s)' at %C", | |
288 | gfc_op2string (current_interface.op)); | |
289 | ||
290 | m = MATCH_ERROR; | |
291 | } | |
292 | ||
293 | break; | |
294 | ||
295 | case INTERFACE_USER_OP: | |
296 | /* Comparing the symbol node names is OK because only use-associated | |
297 | symbols can be renamed. */ | |
298 | if (type != current_interface.type | |
299 | || strcmp (current_interface.sym->name, name) != 0) | |
300 | { | |
301 | gfc_error ("Expecting 'END INTERFACE OPERATOR (.%s.)' at %C", | |
302 | current_interface.sym->name); | |
303 | m = MATCH_ERROR; | |
304 | } | |
305 | ||
306 | break; | |
307 | ||
308 | case INTERFACE_GENERIC: | |
309 | if (type != current_interface.type | |
310 | || strcmp (current_interface.sym->name, name) != 0) | |
311 | { | |
312 | gfc_error ("Expecting 'END INTERFACE %s' at %C", | |
313 | current_interface.sym->name); | |
314 | m = MATCH_ERROR; | |
315 | } | |
316 | ||
317 | break; | |
318 | } | |
319 | ||
320 | return m; | |
321 | } | |
322 | ||
323 | ||
324 | /* Compare two typespecs, recursively if necessary. */ | |
325 | ||
326 | int | |
327 | gfc_compare_types (gfc_typespec * ts1, gfc_typespec * ts2) | |
328 | { | |
329 | gfc_component *dt1, *dt2; | |
330 | ||
331 | if (ts1->type != ts2->type) | |
332 | return 0; | |
333 | if (ts1->type != BT_DERIVED) | |
334 | return (ts1->kind == ts2->kind); | |
335 | ||
336 | /* Compare derived types. */ | |
337 | if (ts1->derived == ts2->derived) | |
338 | return 1; | |
339 | ||
340 | /* Special case for comparing derived types across namespaces. If the | |
341 | true names and module names are the same and the module name is | |
342 | nonnull, then they are equal. */ | |
343 | if (strcmp (ts1->derived->name, ts2->derived->name) == 0 | |
344 | && ts1->derived->module[0] != '\0' | |
345 | && strcmp (ts1->derived->module, ts2->derived->module) == 0) | |
346 | return 1; | |
347 | ||
348 | /* Compare type via the rules of the standard. Both types must have | |
349 | the SEQUENCE attribute to be equal. */ | |
350 | ||
351 | if (strcmp (ts1->derived->name, ts2->derived->name)) | |
352 | return 0; | |
353 | ||
354 | dt1 = ts1->derived->components; | |
355 | dt2 = ts2->derived->components; | |
356 | ||
357 | if (ts1->derived->attr.sequence == 0 || ts2->derived->attr.sequence == 0) | |
358 | return 0; | |
359 | ||
360 | /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a | |
361 | simple test can speed things up. Otherwise, lots of things have to | |
362 | match. */ | |
363 | for (;;) | |
364 | { | |
365 | if (strcmp (dt1->name, dt2->name) != 0) | |
366 | return 0; | |
367 | ||
368 | if (dt1->pointer != dt2->pointer) | |
369 | return 0; | |
370 | ||
371 | if (dt1->dimension != dt2->dimension) | |
372 | return 0; | |
373 | ||
374 | if (dt1->dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0) | |
375 | return 0; | |
376 | ||
377 | if (gfc_compare_types (&dt1->ts, &dt2->ts) == 0) | |
378 | return 0; | |
379 | ||
380 | dt1 = dt1->next; | |
381 | dt2 = dt2->next; | |
382 | ||
383 | if (dt1 == NULL && dt2 == NULL) | |
384 | break; | |
385 | if (dt1 == NULL || dt2 == NULL) | |
386 | return 0; | |
387 | } | |
388 | ||
389 | return 1; | |
390 | } | |
391 | ||
392 | ||
393 | /* Given two symbols that are formal arguments, compare their ranks | |
394 | and types. Returns nonzero if they have the same rank and type, | |
395 | zero otherwise. */ | |
396 | ||
397 | static int | |
398 | compare_type_rank (gfc_symbol * s1, gfc_symbol * s2) | |
399 | { | |
400 | int r1, r2; | |
401 | ||
402 | r1 = (s1->as != NULL) ? s1->as->rank : 0; | |
403 | r2 = (s2->as != NULL) ? s2->as->rank : 0; | |
404 | ||
405 | if (r1 != r2) | |
406 | return 0; /* Ranks differ */ | |
407 | ||
408 | return gfc_compare_types (&s1->ts, &s2->ts); | |
409 | } | |
410 | ||
411 | ||
412 | static int compare_interfaces (gfc_symbol *, gfc_symbol *, int); | |
413 | ||
414 | /* Given two symbols that are formal arguments, compare their types | |
415 | and rank and their formal interfaces if they are both dummy | |
416 | procedures. Returns nonzero if the same, zero if different. */ | |
417 | ||
418 | static int | |
419 | compare_type_rank_if (gfc_symbol * s1, gfc_symbol * s2) | |
420 | { | |
421 | ||
422 | if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE) | |
423 | return compare_type_rank (s1, s2); | |
424 | ||
425 | if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE) | |
426 | return 0; | |
427 | ||
428 | /* At this point, both symbols are procedures. */ | |
429 | if ((s1->attr.function == 0 && s1->attr.subroutine == 0) | |
430 | || (s2->attr.function == 0 && s2->attr.subroutine == 0)) | |
431 | return 0; | |
432 | ||
433 | if (s1->attr.function != s2->attr.function | |
434 | || s1->attr.subroutine != s2->attr.subroutine) | |
435 | return 0; | |
436 | ||
437 | if (s1->attr.function && compare_type_rank (s1, s2) == 0) | |
438 | return 0; | |
439 | ||
440 | return compare_interfaces (s1, s2, 0); /* Recurse! */ | |
441 | } | |
442 | ||
443 | ||
444 | /* Given a formal argument list and a keyword name, search the list | |
445 | for that keyword. Returns the correct symbol node if found, NULL | |
446 | if not found. */ | |
447 | ||
448 | static gfc_symbol * | |
449 | find_keyword_arg (const char *name, gfc_formal_arglist * f) | |
450 | { | |
451 | ||
452 | for (; f; f = f->next) | |
453 | if (strcmp (f->sym->name, name) == 0) | |
454 | return f->sym; | |
455 | ||
456 | return NULL; | |
457 | } | |
458 | ||
459 | ||
460 | /******** Interface checking subroutines **********/ | |
461 | ||
462 | ||
463 | /* Given an operator interface and the operator, make sure that all | |
464 | interfaces for that operator are legal. */ | |
465 | ||
466 | static void | |
467 | check_operator_interface (gfc_interface * intr, gfc_intrinsic_op operator) | |
468 | { | |
469 | gfc_formal_arglist *formal; | |
470 | sym_intent i1, i2; | |
471 | gfc_symbol *sym; | |
472 | bt t1, t2; | |
473 | int args; | |
474 | ||
475 | if (intr == NULL) | |
476 | return; | |
477 | ||
478 | args = 0; | |
479 | t1 = t2 = BT_UNKNOWN; | |
480 | i1 = i2 = INTENT_UNKNOWN; | |
481 | ||
482 | for (formal = intr->sym->formal; formal; formal = formal->next) | |
483 | { | |
484 | sym = formal->sym; | |
485 | ||
486 | if (args == 0) | |
487 | { | |
488 | t1 = sym->ts.type; | |
489 | i1 = sym->attr.intent; | |
490 | } | |
491 | if (args == 1) | |
492 | { | |
493 | t2 = sym->ts.type; | |
494 | i2 = sym->attr.intent; | |
495 | } | |
496 | args++; | |
497 | } | |
498 | ||
499 | if (args == 0 || args > 2) | |
500 | goto num_args; | |
501 | ||
502 | sym = intr->sym; | |
503 | ||
504 | if (operator == INTRINSIC_ASSIGN) | |
505 | { | |
506 | if (!sym->attr.subroutine) | |
507 | { | |
508 | gfc_error | |
509 | ("Assignment operator interface at %L must be a SUBROUTINE", | |
510 | &intr->where); | |
511 | return; | |
512 | } | |
513 | } | |
514 | else | |
515 | { | |
516 | if (!sym->attr.function) | |
517 | { | |
518 | gfc_error ("Intrinsic operator interface at %L must be a FUNCTION", | |
519 | &intr->where); | |
520 | return; | |
521 | } | |
522 | } | |
523 | ||
524 | switch (operator) | |
525 | { | |
526 | case INTRINSIC_PLUS: /* Numeric unary or binary */ | |
527 | case INTRINSIC_MINUS: | |
528 | if ((args == 1) | |
529 | && (t1 == BT_INTEGER | |
530 | || t1 == BT_REAL | |
531 | || t1 == BT_COMPLEX)) | |
532 | goto bad_repl; | |
533 | ||
534 | if ((args == 2) | |
535 | && (t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX) | |
536 | && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX)) | |
537 | goto bad_repl; | |
538 | ||
539 | break; | |
540 | ||
541 | case INTRINSIC_POWER: /* Binary numeric */ | |
542 | case INTRINSIC_TIMES: | |
543 | case INTRINSIC_DIVIDE: | |
544 | ||
545 | case INTRINSIC_EQ: | |
546 | case INTRINSIC_NE: | |
547 | if (args == 1) | |
548 | goto num_args; | |
549 | ||
550 | if ((t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX) | |
551 | && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX)) | |
552 | goto bad_repl; | |
553 | ||
554 | break; | |
555 | ||
556 | case INTRINSIC_GE: /* Binary numeric operators that do not support */ | |
557 | case INTRINSIC_LE: /* complex numbers */ | |
558 | case INTRINSIC_LT: | |
559 | case INTRINSIC_GT: | |
560 | if (args == 1) | |
561 | goto num_args; | |
562 | ||
563 | if ((t1 == BT_INTEGER || t1 == BT_REAL) | |
564 | && (t2 == BT_INTEGER || t2 == BT_REAL)) | |
565 | goto bad_repl; | |
566 | ||
567 | break; | |
568 | ||
569 | case INTRINSIC_OR: /* Binary logical */ | |
570 | case INTRINSIC_AND: | |
571 | case INTRINSIC_EQV: | |
572 | case INTRINSIC_NEQV: | |
573 | if (args == 1) | |
574 | goto num_args; | |
575 | if (t1 == BT_LOGICAL && t2 == BT_LOGICAL) | |
576 | goto bad_repl; | |
577 | break; | |
578 | ||
579 | case INTRINSIC_NOT: /* Unary logical */ | |
580 | if (args != 1) | |
581 | goto num_args; | |
582 | if (t1 == BT_LOGICAL) | |
583 | goto bad_repl; | |
584 | break; | |
585 | ||
586 | case INTRINSIC_CONCAT: /* Binary string */ | |
587 | if (args != 2) | |
588 | goto num_args; | |
589 | if (t1 == BT_CHARACTER && t2 == BT_CHARACTER) | |
590 | goto bad_repl; | |
591 | break; | |
592 | ||
593 | case INTRINSIC_ASSIGN: /* Class by itself */ | |
594 | if (args != 2) | |
595 | goto num_args; | |
596 | break; | |
597 | default: | |
598 | gfc_internal_error ("check_operator_interface(): Bad operator"); | |
599 | } | |
600 | ||
601 | /* Check intents on operator interfaces. */ | |
602 | if (operator == INTRINSIC_ASSIGN) | |
603 | { | |
604 | if (i1 != INTENT_OUT && i1 != INTENT_INOUT) | |
605 | gfc_error ("First argument of defined assignment at %L must be " | |
606 | "INTENT(IN) or INTENT(INOUT)", &intr->where); | |
607 | ||
608 | if (i2 != INTENT_IN) | |
609 | gfc_error ("Second argument of defined assignment at %L must be " | |
610 | "INTENT(IN)", &intr->where); | |
611 | } | |
612 | else | |
613 | { | |
614 | if (i1 != INTENT_IN) | |
615 | gfc_error ("First argument of operator interface at %L must be " | |
616 | "INTENT(IN)", &intr->where); | |
617 | ||
618 | if (args == 2 && i2 != INTENT_IN) | |
619 | gfc_error ("Second argument of operator interface at %L must be " | |
620 | "INTENT(IN)", &intr->where); | |
621 | } | |
622 | ||
623 | return; | |
624 | ||
625 | bad_repl: | |
626 | gfc_error ("Operator interface at %L conflicts with intrinsic interface", | |
627 | &intr->where); | |
628 | return; | |
629 | ||
630 | num_args: | |
631 | gfc_error ("Operator interface at %L has the wrong number of arguments", | |
632 | &intr->where); | |
633 | return; | |
634 | } | |
635 | ||
636 | ||
637 | /* Given a pair of formal argument lists, we see if the two lists can | |
638 | be distinguished by counting the number of nonoptional arguments of | |
639 | a given type/rank in f1 and seeing if there are less then that | |
640 | number of those arguments in f2 (including optional arguments). | |
641 | Since this test is asymmetric, it has to be called twice to make it | |
642 | symmetric. Returns nonzero if the argument lists are incompatible | |
643 | by this test. This subroutine implements rule 1 of section | |
644 | 14.1.2.3. */ | |
645 | ||
646 | static int | |
647 | count_types_test (gfc_formal_arglist * f1, gfc_formal_arglist * f2) | |
648 | { | |
649 | int rc, ac1, ac2, i, j, k, n1; | |
650 | gfc_formal_arglist *f; | |
651 | ||
652 | typedef struct | |
653 | { | |
654 | int flag; | |
655 | gfc_symbol *sym; | |
656 | } | |
657 | arginfo; | |
658 | ||
659 | arginfo *arg; | |
660 | ||
661 | n1 = 0; | |
662 | ||
663 | for (f = f1; f; f = f->next) | |
664 | n1++; | |
665 | ||
666 | /* Build an array of integers that gives the same integer to | |
667 | arguments of the same type/rank. */ | |
668 | arg = gfc_getmem (n1 * sizeof (arginfo)); | |
669 | ||
670 | f = f1; | |
671 | for (i = 0; i < n1; i++, f = f->next) | |
672 | { | |
673 | arg[i].flag = -1; | |
674 | arg[i].sym = f->sym; | |
675 | } | |
676 | ||
677 | k = 0; | |
678 | ||
679 | for (i = 0; i < n1; i++) | |
680 | { | |
681 | if (arg[i].flag != -1) | |
682 | continue; | |
683 | ||
684 | if (arg[i].sym->attr.optional) | |
685 | continue; /* Skip optional arguments */ | |
686 | ||
687 | arg[i].flag = k; | |
688 | ||
689 | /* Find other nonoptional arguments of the same type/rank. */ | |
690 | for (j = i + 1; j < n1; j++) | |
691 | if (!arg[j].sym->attr.optional | |
692 | && compare_type_rank_if (arg[i].sym, arg[j].sym)) | |
693 | arg[j].flag = k; | |
694 | ||
695 | k++; | |
696 | } | |
697 | ||
698 | /* Now loop over each distinct type found in f1. */ | |
699 | k = 0; | |
700 | rc = 0; | |
701 | ||
702 | for (i = 0; i < n1; i++) | |
703 | { | |
704 | if (arg[i].flag != k) | |
705 | continue; | |
706 | ||
707 | ac1 = 1; | |
708 | for (j = i + 1; j < n1; j++) | |
709 | if (arg[j].flag == k) | |
710 | ac1++; | |
711 | ||
712 | /* Count the number of arguments in f2 with that type, including | |
713 | those that are optional. */ | |
714 | ac2 = 0; | |
715 | ||
716 | for (f = f2; f; f = f->next) | |
717 | if (compare_type_rank_if (arg[i].sym, f->sym)) | |
718 | ac2++; | |
719 | ||
720 | if (ac1 > ac2) | |
721 | { | |
722 | rc = 1; | |
723 | break; | |
724 | } | |
725 | ||
726 | k++; | |
727 | } | |
728 | ||
729 | gfc_free (arg); | |
730 | ||
731 | return rc; | |
732 | } | |
733 | ||
734 | ||
735 | /* Perform the abbreviated correspondence test for operators. The | |
736 | arguments cannot be optional and are always ordered correctly, | |
737 | which makes this test much easier than that for generic tests. | |
738 | ||
739 | This subroutine is also used when comparing a formal and actual | |
740 | argument list when an actual parameter is a dummy procedure. At | |
741 | that point, two formal interfaces must be compared for equality | |
742 | which is what happens here. */ | |
743 | ||
744 | static int | |
745 | operator_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2) | |
746 | { | |
747 | for (;;) | |
748 | { | |
749 | if (f1 == NULL && f2 == NULL) | |
750 | break; | |
751 | if (f1 == NULL || f2 == NULL) | |
752 | return 1; | |
753 | ||
754 | if (!compare_type_rank (f1->sym, f2->sym)) | |
755 | return 1; | |
756 | ||
757 | f1 = f1->next; | |
758 | f2 = f2->next; | |
759 | } | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
764 | ||
765 | /* Perform the correspondence test in rule 2 of section 14.1.2.3. | |
766 | Returns zero if no argument is found that satisifes rule 2, nonzero | |
767 | otherwise. | |
768 | ||
769 | This test is also not symmetric in f1 and f2 and must be called | |
770 | twice. This test finds problems caused by sorting the actual | |
771 | argument list with keywords. For example: | |
772 | ||
773 | INTERFACE FOO | |
774 | SUBROUTINE F1(A, B) | |
775 | INTEGER :: A ; REAL :: B | |
776 | END SUBROUTINE F1 | |
777 | ||
778 | SUBROUTINE F2(B, A) | |
779 | INTEGER :: A ; REAL :: B | |
780 | END SUBROUTINE F1 | |
781 | END INTERFACE FOO | |
782 | ||
783 | At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous. */ | |
784 | ||
785 | static int | |
786 | generic_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2) | |
787 | { | |
788 | ||
789 | gfc_formal_arglist *f2_save, *g; | |
790 | gfc_symbol *sym; | |
791 | ||
792 | f2_save = f2; | |
793 | ||
794 | while (f1) | |
795 | { | |
796 | if (f1->sym->attr.optional) | |
797 | goto next; | |
798 | ||
799 | if (f2 != NULL && compare_type_rank (f1->sym, f2->sym)) | |
800 | goto next; | |
801 | ||
802 | /* Now search for a disambiguating keyword argument starting at | |
803 | the current non-match. */ | |
804 | for (g = f1; g; g = g->next) | |
805 | { | |
806 | if (g->sym->attr.optional) | |
807 | continue; | |
808 | ||
809 | sym = find_keyword_arg (g->sym->name, f2_save); | |
810 | if (sym == NULL || !compare_type_rank (g->sym, sym)) | |
811 | return 1; | |
812 | } | |
813 | ||
814 | next: | |
815 | f1 = f1->next; | |
816 | if (f2 != NULL) | |
817 | f2 = f2->next; | |
818 | } | |
819 | ||
820 | return 0; | |
821 | } | |
822 | ||
823 | ||
824 | /* 'Compare' two formal interfaces associated with a pair of symbols. | |
825 | We return nonzero if there exists an actual argument list that | |
826 | would be ambiguous between the two interfaces, zero otherwise. */ | |
827 | ||
828 | static int | |
829 | compare_interfaces (gfc_symbol * s1, gfc_symbol * s2, int generic_flag) | |
830 | { | |
831 | gfc_formal_arglist *f1, *f2; | |
832 | ||
833 | if (s1->attr.function != s2->attr.function | |
834 | && s1->attr.subroutine != s2->attr.subroutine) | |
835 | return 0; /* disagreement between function/subroutine */ | |
836 | ||
837 | f1 = s1->formal; | |
838 | f2 = s2->formal; | |
839 | ||
840 | if (f1 == NULL && f2 == NULL) | |
841 | return 1; /* Special case */ | |
842 | ||
843 | if (count_types_test (f1, f2)) | |
844 | return 0; | |
845 | if (count_types_test (f2, f1)) | |
846 | return 0; | |
847 | ||
848 | if (generic_flag) | |
849 | { | |
850 | if (generic_correspondence (f1, f2)) | |
851 | return 0; | |
852 | if (generic_correspondence (f2, f1)) | |
853 | return 0; | |
854 | } | |
855 | else | |
856 | { | |
857 | if (operator_correspondence (f1, f2)) | |
858 | return 0; | |
859 | } | |
860 | ||
861 | return 1; | |
862 | } | |
863 | ||
864 | ||
865 | /* Given a pointer to an interface pointer, remove duplicate | |
866 | interfaces and make sure that all symbols are either functions or | |
867 | subroutines. Returns nonzero if something goes wrong. */ | |
868 | ||
869 | static int | |
870 | check_interface0 (gfc_interface * p, const char *interface_name) | |
871 | { | |
872 | gfc_interface *psave, *q, *qlast; | |
873 | ||
874 | psave = p; | |
875 | /* Make sure all symbols in the interface have been defined as | |
876 | functions or subroutines. */ | |
877 | for (; p; p = p->next) | |
878 | if (!p->sym->attr.function && !p->sym->attr.subroutine) | |
879 | { | |
880 | gfc_error ("Procedure '%s' in %s at %L is neither function nor " | |
881 | "subroutine", p->sym->name, interface_name, | |
882 | &p->sym->declared_at); | |
883 | return 1; | |
884 | } | |
885 | p = psave; | |
886 | ||
887 | /* Remove duplicate interfaces in this interface list. */ | |
888 | for (; p; p = p->next) | |
889 | { | |
890 | qlast = p; | |
891 | ||
892 | for (q = p->next; q;) | |
893 | { | |
894 | if (p->sym != q->sym) | |
895 | { | |
896 | qlast = q; | |
897 | q = q->next; | |
898 | ||
899 | } | |
900 | else | |
901 | { | |
902 | /* Duplicate interface */ | |
903 | qlast->next = q->next; | |
904 | gfc_free (q); | |
905 | q = qlast->next; | |
906 | } | |
907 | } | |
908 | } | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | ||
914 | /* Check lists of interfaces to make sure that no two interfaces are | |
915 | ambiguous. Duplicate interfaces (from the same symbol) are OK | |
916 | here. */ | |
917 | ||
918 | static int | |
919 | check_interface1 (gfc_interface * p, gfc_interface * q, | |
920 | int generic_flag, const char *interface_name) | |
921 | { | |
922 | ||
923 | for (; p; p = p->next) | |
924 | for (; q; q = q->next) | |
925 | { | |
926 | if (p->sym == q->sym) | |
927 | continue; /* Duplicates OK here */ | |
928 | ||
929 | if (strcmp (p->sym->name, q->sym->name) == 0 | |
930 | && strcmp (p->sym->module, q->sym->module) == 0) | |
931 | continue; | |
932 | ||
933 | if (compare_interfaces (p->sym, q->sym, generic_flag)) | |
934 | { | |
935 | gfc_error ("Ambiguous interfaces '%s' and '%s' in %s at %L", | |
936 | p->sym->name, q->sym->name, interface_name, &p->where); | |
937 | return 1; | |
938 | } | |
939 | } | |
940 | ||
941 | return 0; | |
942 | } | |
943 | ||
944 | ||
945 | /* Check the generic and operator interfaces of symbols to make sure | |
946 | that none of the interfaces conflict. The check has to be done | |
947 | after all of the symbols are actually loaded. */ | |
948 | ||
949 | static void | |
950 | check_sym_interfaces (gfc_symbol * sym) | |
951 | { | |
952 | char interface_name[100]; | |
953 | gfc_symbol *s2; | |
954 | ||
955 | if (sym->ns != gfc_current_ns) | |
956 | return; | |
957 | ||
958 | if (sym->generic != NULL) | |
959 | { | |
960 | sprintf (interface_name, "generic interface '%s'", sym->name); | |
961 | if (check_interface0 (sym->generic, interface_name)) | |
962 | return; | |
963 | ||
964 | s2 = sym; | |
965 | while (s2 != NULL) | |
966 | { | |
967 | if (check_interface1 (sym->generic, s2->generic, 1, interface_name)) | |
968 | return; | |
969 | ||
970 | if (s2->ns->parent == NULL) | |
971 | break; | |
972 | if (gfc_find_symbol (sym->name, s2->ns->parent, 1, &s2)) | |
973 | break; | |
974 | } | |
975 | } | |
976 | } | |
977 | ||
978 | ||
979 | static void | |
980 | check_uop_interfaces (gfc_user_op * uop) | |
981 | { | |
982 | char interface_name[100]; | |
983 | gfc_user_op *uop2; | |
984 | gfc_namespace *ns; | |
985 | ||
986 | sprintf (interface_name, "operator interface '%s'", uop->name); | |
987 | if (check_interface0 (uop->operator, interface_name)) | |
988 | return; | |
989 | ||
990 | for (ns = gfc_current_ns; ns; ns = ns->parent) | |
991 | { | |
992 | uop2 = gfc_find_uop (uop->name, ns); | |
993 | if (uop2 == NULL) | |
994 | continue; | |
995 | ||
996 | check_interface1 (uop->operator, uop2->operator, 0, interface_name); | |
997 | } | |
998 | } | |
999 | ||
1000 | ||
1001 | /* For the namespace, check generic, user operator and intrinsic | |
1002 | operator interfaces for consistency and to remove duplicate | |
1003 | interfaces. We traverse the whole namespace, counting on the fact | |
1004 | that most symbols will not have generic or operator interfaces. */ | |
1005 | ||
1006 | void | |
1007 | gfc_check_interfaces (gfc_namespace * ns) | |
1008 | { | |
1009 | gfc_namespace *old_ns, *ns2; | |
1010 | char interface_name[100]; | |
1011 | gfc_intrinsic_op i; | |
1012 | ||
1013 | old_ns = gfc_current_ns; | |
1014 | gfc_current_ns = ns; | |
1015 | ||
1016 | gfc_traverse_ns (ns, check_sym_interfaces); | |
1017 | ||
1018 | gfc_traverse_user_op (ns, check_uop_interfaces); | |
1019 | ||
1020 | for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++) | |
1021 | { | |
1022 | if (i == INTRINSIC_USER) | |
1023 | continue; | |
1024 | ||
1025 | if (i == INTRINSIC_ASSIGN) | |
1026 | strcpy (interface_name, "intrinsic assignment operator"); | |
1027 | else | |
1028 | sprintf (interface_name, "intrinsic '%s' operator", | |
1029 | gfc_op2string (i)); | |
1030 | ||
1031 | if (check_interface0 (ns->operator[i], interface_name)) | |
1032 | continue; | |
1033 | ||
1034 | check_operator_interface (ns->operator[i], i); | |
1035 | ||
1036 | for (ns2 = ns->parent; ns2; ns2 = ns2->parent) | |
1037 | if (check_interface1 (ns->operator[i], ns2->operator[i], 0, | |
1038 | interface_name)) | |
1039 | break; | |
1040 | } | |
1041 | ||
1042 | gfc_current_ns = old_ns; | |
1043 | } | |
1044 | ||
1045 | ||
1046 | static int | |
1047 | symbol_rank (gfc_symbol * sym) | |
1048 | { | |
1049 | ||
1050 | return (sym->as == NULL) ? 0 : sym->as->rank; | |
1051 | } | |
1052 | ||
1053 | ||
1054 | /* Given a symbol of a formal argument list and an expression, if the | |
1055 | formal argument is a pointer, see if the actual argument is a | |
1056 | pointer. Returns nonzero if compatible, zero if not compatible. */ | |
1057 | ||
1058 | static int | |
1059 | compare_pointer (gfc_symbol * formal, gfc_expr * actual) | |
1060 | { | |
1061 | symbol_attribute attr; | |
1062 | ||
1063 | if (formal->attr.pointer) | |
1064 | { | |
1065 | attr = gfc_expr_attr (actual); | |
1066 | if (!attr.pointer) | |
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | return 1; | |
1071 | } | |
1072 | ||
1073 | ||
1074 | /* Given a symbol of a formal argument list and an expression, see if | |
1075 | the two are compatible as arguments. Returns nonzero if | |
1076 | compatible, zero if not compatible. */ | |
1077 | ||
1078 | static int | |
1079 | compare_parameter (gfc_symbol * formal, gfc_expr * actual, | |
1080 | int ranks_must_agree, int is_elemental) | |
1081 | { | |
1082 | gfc_ref *ref; | |
1083 | ||
1084 | if (actual->ts.type == BT_PROCEDURE) | |
1085 | { | |
1086 | if (formal->attr.flavor != FL_PROCEDURE) | |
1087 | return 0; | |
1088 | ||
1089 | if (formal->attr.function | |
1090 | && !compare_type_rank (formal, actual->symtree->n.sym)) | |
1091 | return 0; | |
1092 | ||
1093 | if (formal->attr.if_source == IFSRC_UNKNOWN) | |
1094 | return 1; /* Assume match */ | |
1095 | ||
1096 | return compare_interfaces (formal, actual->symtree->n.sym, 0); | |
1097 | } | |
1098 | ||
1099 | if (!gfc_compare_types (&formal->ts, &actual->ts)) | |
1100 | return 0; | |
1101 | ||
1102 | if (symbol_rank (formal) == actual->rank) | |
1103 | return 1; | |
1104 | ||
1105 | /* At this point the ranks didn't agree. */ | |
1106 | if (ranks_must_agree || formal->attr.pointer) | |
1107 | return 0; | |
1108 | ||
1109 | if (actual->rank != 0) | |
1110 | return is_elemental || formal->attr.dimension; | |
1111 | ||
1112 | /* At this point, we are considering a scalar passed to an array. | |
1113 | This is legal if the scalar is an array element of the right sort. */ | |
1114 | if (formal->as->type == AS_ASSUMED_SHAPE) | |
1115 | return 0; | |
1116 | ||
1117 | for (ref = actual->ref; ref; ref = ref->next) | |
1118 | if (ref->type == REF_SUBSTRING) | |
1119 | return 0; | |
1120 | ||
1121 | for (ref = actual->ref; ref; ref = ref->next) | |
1122 | if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT) | |
1123 | break; | |
1124 | ||
1125 | if (ref == NULL) | |
1126 | return 0; /* Not an array element */ | |
1127 | ||
1128 | return 1; | |
1129 | } | |
1130 | ||
1131 | ||
1132 | /* Given formal and actual argument lists, see if they are compatible. | |
1133 | If they are compatible, the actual argument list is sorted to | |
1134 | correspond with the formal list, and elements for missing optional | |
1135 | arguments are inserted. If WHERE pointer is nonnull, then we issue | |
1136 | errors when things don't match instead of just returning the status | |
1137 | code. */ | |
1138 | ||
1139 | static int | |
1140 | compare_actual_formal (gfc_actual_arglist ** ap, | |
1141 | gfc_formal_arglist * formal, | |
1142 | int ranks_must_agree, int is_elemental, locus * where) | |
1143 | { | |
1144 | gfc_actual_arglist **new, *a, *actual, temp; | |
1145 | gfc_formal_arglist *f; | |
1146 | int i, n, na; | |
1147 | ||
1148 | actual = *ap; | |
1149 | ||
1150 | if (actual == NULL && formal == NULL) | |
1151 | return 1; | |
1152 | ||
1153 | n = 0; | |
1154 | for (f = formal; f; f = f->next) | |
1155 | n++; | |
1156 | ||
1157 | new = (gfc_actual_arglist **) alloca (n * sizeof (gfc_actual_arglist *)); | |
1158 | ||
1159 | for (i = 0; i < n; i++) | |
1160 | new[i] = NULL; | |
1161 | ||
1162 | na = 0; | |
1163 | f = formal; | |
1164 | i = 0; | |
1165 | ||
1166 | for (a = actual; a; a = a->next, f = f->next) | |
1167 | { | |
1168 | if (a->name[0] != '\0') | |
1169 | { | |
1170 | i = 0; | |
1171 | for (f = formal; f; f = f->next, i++) | |
1172 | { | |
1173 | if (f->sym == NULL) | |
1174 | continue; | |
1175 | if (strcmp (f->sym->name, a->name) == 0) | |
1176 | break; | |
1177 | } | |
1178 | ||
1179 | if (f == NULL) | |
1180 | { | |
1181 | if (where) | |
1182 | gfc_error | |
1183 | ("Keyword argument '%s' at %L is not in the procedure", | |
1184 | a->name, &a->expr->where); | |
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | if (new[i] != NULL) | |
1189 | { | |
1190 | if (where) | |
1191 | gfc_error | |
1192 | ("Keyword argument '%s' at %L is already associated " | |
1193 | "with another actual argument", a->name, &a->expr->where); | |
1194 | return 0; | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | if (f == NULL) | |
1199 | { | |
1200 | if (where) | |
1201 | gfc_error | |
1202 | ("More actual than formal arguments in procedure call at %L", | |
1203 | where); | |
1204 | ||
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | if (f->sym == NULL && a->expr == NULL) | |
1209 | goto match; | |
1210 | ||
1211 | if (f->sym == NULL) | |
1212 | { | |
1213 | if (where) | |
1214 | gfc_error | |
1215 | ("Missing alternate return spec in subroutine call at %L", | |
1216 | where); | |
1217 | return 0; | |
1218 | } | |
1219 | ||
1220 | if (a->expr == NULL) | |
1221 | { | |
1222 | if (where) | |
1223 | gfc_error | |
1224 | ("Unexpected alternate return spec in subroutine call at %L", | |
1225 | where); | |
1226 | return 0; | |
1227 | } | |
1228 | ||
1229 | if (!compare_parameter | |
1230 | (f->sym, a->expr, ranks_must_agree, is_elemental)) | |
1231 | { | |
1232 | if (where) | |
1233 | gfc_error ("Type/rank mismatch in argument '%s' at %L", | |
1234 | f->sym->name, &a->expr->where); | |
1235 | return 0; | |
1236 | } | |
1237 | ||
1238 | if (compare_pointer (f->sym, a->expr) == 0) | |
1239 | { | |
1240 | if (where) | |
1241 | gfc_error ("Actual argument for '%s' must be a pointer at %L", | |
1242 | f->sym->name, &a->expr->where); | |
1243 | return 0; | |
1244 | } | |
1245 | ||
1246 | match: | |
1247 | if (a == actual) | |
1248 | na = i; | |
1249 | ||
1250 | new[i++] = a; | |
1251 | } | |
1252 | ||
1253 | /* Make sure missing actual arguments are optional. */ | |
1254 | i = 0; | |
1255 | for (f = formal; f; f = f->next, i++) | |
1256 | { | |
1257 | if (new[i] != NULL) | |
1258 | continue; | |
1259 | if (!f->sym->attr.optional) | |
1260 | { | |
1261 | if (where) | |
1262 | gfc_error ("Missing actual argument for argument '%s' at %L", | |
1263 | f->sym->name, where); | |
1264 | return 0; | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | /* The argument lists are compatible. We now relink a new actual | |
1269 | argument list with null arguments in the right places. The head | |
1270 | of the list remains the head. */ | |
1271 | for (i = 0; i < n; i++) | |
1272 | if (new[i] == NULL) | |
1273 | new[i] = gfc_get_actual_arglist (); | |
1274 | ||
1275 | if (na != 0) | |
1276 | { | |
1277 | temp = *new[0]; | |
1278 | *new[0] = *actual; | |
1279 | *actual = temp; | |
1280 | ||
1281 | a = new[0]; | |
1282 | new[0] = new[na]; | |
1283 | new[na] = a; | |
1284 | } | |
1285 | ||
1286 | for (i = 0; i < n - 1; i++) | |
1287 | new[i]->next = new[i + 1]; | |
1288 | ||
1289 | new[i]->next = NULL; | |
1290 | ||
1291 | if (*ap == NULL && n > 0) | |
1292 | *ap = new[0]; | |
1293 | ||
1294 | return 1; | |
1295 | } | |
1296 | ||
1297 | ||
1298 | typedef struct | |
1299 | { | |
1300 | gfc_formal_arglist *f; | |
1301 | gfc_actual_arglist *a; | |
1302 | } | |
1303 | argpair; | |
1304 | ||
1305 | /* qsort comparison function for argument pairs, with the following | |
1306 | order: | |
1307 | - p->a->expr == NULL | |
1308 | - p->a->expr->expr_type != EXPR_VARIABLE | |
1309 | - growing p->a->expr->symbol. */ | |
1310 | ||
1311 | static int | |
1312 | pair_cmp (const void *p1, const void *p2) | |
1313 | { | |
1314 | const gfc_actual_arglist *a1, *a2; | |
1315 | ||
1316 | /* *p1 and *p2 are elements of the to-be-sorted array. */ | |
1317 | a1 = ((const argpair *) p1)->a; | |
1318 | a2 = ((const argpair *) p2)->a; | |
1319 | if (!a1->expr) | |
1320 | { | |
1321 | if (!a2->expr) | |
1322 | return 0; | |
1323 | return -1; | |
1324 | } | |
1325 | if (!a2->expr) | |
1326 | return 1; | |
1327 | if (a1->expr->expr_type != EXPR_VARIABLE) | |
1328 | { | |
1329 | if (a2->expr->expr_type != EXPR_VARIABLE) | |
1330 | return 0; | |
1331 | return -1; | |
1332 | } | |
1333 | if (a2->expr->expr_type != EXPR_VARIABLE) | |
1334 | return 1; | |
1335 | return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym; | |
1336 | } | |
1337 | ||
1338 | ||
1339 | /* Given two expressions from some actual arguments, test whether they | |
1340 | refer to the same expression. The analysis is conservative. | |
1341 | Returning FAILURE will produce no warning. */ | |
1342 | ||
1343 | static try | |
1344 | compare_actual_expr (gfc_expr * e1, gfc_expr * e2) | |
1345 | { | |
1346 | const gfc_ref *r1, *r2; | |
1347 | ||
1348 | if (!e1 || !e2 | |
1349 | || e1->expr_type != EXPR_VARIABLE | |
1350 | || e2->expr_type != EXPR_VARIABLE | |
1351 | || e1->symtree->n.sym != e2->symtree->n.sym) | |
1352 | return FAILURE; | |
1353 | ||
1354 | /* TODO: improve comparison, see expr.c:show_ref(). */ | |
1355 | for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next) | |
1356 | { | |
1357 | if (r1->type != r2->type) | |
1358 | return FAILURE; | |
1359 | switch (r1->type) | |
1360 | { | |
1361 | case REF_ARRAY: | |
1362 | if (r1->u.ar.type != r2->u.ar.type) | |
1363 | return FAILURE; | |
1364 | /* TODO: At the moment, consider only full arrays; | |
1365 | we could do better. */ | |
1366 | if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL) | |
1367 | return FAILURE; | |
1368 | break; | |
1369 | ||
1370 | case REF_COMPONENT: | |
1371 | if (r1->u.c.component != r2->u.c.component) | |
1372 | return FAILURE; | |
1373 | break; | |
1374 | ||
1375 | case REF_SUBSTRING: | |
1376 | return FAILURE; | |
1377 | ||
1378 | default: | |
1379 | gfc_internal_error ("compare_actual_expr(): Bad component code"); | |
1380 | } | |
1381 | } | |
1382 | if (!r1 && !r2) | |
1383 | return SUCCESS; | |
1384 | return FAILURE; | |
1385 | } | |
1386 | ||
1387 | /* Given formal and actual argument lists that correspond to one | |
1388 | another, check that identical actual arguments aren't not | |
1389 | associated with some incompatible INTENTs. */ | |
1390 | ||
1391 | static try | |
1392 | check_some_aliasing (gfc_formal_arglist * f, gfc_actual_arglist * a) | |
1393 | { | |
1394 | sym_intent f1_intent, f2_intent; | |
1395 | gfc_formal_arglist *f1; | |
1396 | gfc_actual_arglist *a1; | |
1397 | size_t n, i, j; | |
1398 | argpair *p; | |
1399 | try t = SUCCESS; | |
1400 | ||
1401 | n = 0; | |
1402 | for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next) | |
1403 | { | |
1404 | if (f1 == NULL && a1 == NULL) | |
1405 | break; | |
1406 | if (f1 == NULL || a1 == NULL) | |
1407 | gfc_internal_error ("check_some_aliasing(): List mismatch"); | |
1408 | n++; | |
1409 | } | |
1410 | if (n == 0) | |
1411 | return t; | |
1412 | p = (argpair *) alloca (n * sizeof (argpair)); | |
1413 | ||
1414 | for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next) | |
1415 | { | |
1416 | p[i].f = f1; | |
1417 | p[i].a = a1; | |
1418 | } | |
1419 | ||
1420 | qsort (p, n, sizeof (argpair), pair_cmp); | |
1421 | ||
1422 | for (i = 0; i < n; i++) | |
1423 | { | |
1424 | if (!p[i].a->expr | |
1425 | || p[i].a->expr->expr_type != EXPR_VARIABLE | |
1426 | || p[i].a->expr->ts.type == BT_PROCEDURE) | |
1427 | continue; | |
1428 | f1_intent = p[i].f->sym->attr.intent; | |
1429 | for (j = i + 1; j < n; j++) | |
1430 | { | |
1431 | /* Expected order after the sort. */ | |
1432 | if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE) | |
1433 | gfc_internal_error ("check_some_aliasing(): corrupted data"); | |
1434 | ||
1435 | /* Are the expression the same? */ | |
1436 | if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE) | |
1437 | break; | |
1438 | f2_intent = p[j].f->sym->attr.intent; | |
1439 | if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT) | |
1440 | || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)) | |
1441 | { | |
1442 | gfc_warning ("Same actual argument associated with INTENT(%s) " | |
1443 | "argument '%s' and INTENT(%s) argument '%s' at %L", | |
1444 | gfc_intent_string (f1_intent), p[i].f->sym->name, | |
1445 | gfc_intent_string (f2_intent), p[j].f->sym->name, | |
1446 | &p[i].a->expr->where); | |
1447 | t = FAILURE; | |
1448 | } | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | return t; | |
1453 | } | |
1454 | ||
1455 | ||
1456 | /* Given formal and actual argument lists that correspond to one | |
1457 | another, check that they are compatible in the sense that intents | |
1458 | are not mismatched. */ | |
1459 | ||
1460 | static try | |
1461 | check_intents (gfc_formal_arglist * f, gfc_actual_arglist * a) | |
1462 | { | |
1463 | sym_intent a_intent, f_intent; | |
1464 | ||
1465 | for (;; f = f->next, a = a->next) | |
1466 | { | |
1467 | if (f == NULL && a == NULL) | |
1468 | break; | |
1469 | if (f == NULL || a == NULL) | |
1470 | gfc_internal_error ("check_intents(): List mismatch"); | |
1471 | ||
1472 | if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE) | |
1473 | continue; | |
1474 | ||
1475 | a_intent = a->expr->symtree->n.sym->attr.intent; | |
1476 | f_intent = f->sym->attr.intent; | |
1477 | ||
1478 | if (a_intent == INTENT_IN | |
1479 | && (f_intent == INTENT_INOUT | |
1480 | || f_intent == INTENT_OUT)) | |
1481 | { | |
1482 | ||
1483 | gfc_error ("Procedure argument at %L is INTENT(IN) while interface " | |
1484 | "specifies INTENT(%s)", &a->expr->where, | |
1485 | gfc_intent_string (f_intent)); | |
1486 | return FAILURE; | |
1487 | } | |
1488 | ||
1489 | if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym)) | |
1490 | { | |
1491 | if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT) | |
1492 | { | |
1493 | gfc_error | |
1494 | ("Procedure argument at %L is local to a PURE procedure and " | |
1495 | "is passed to an INTENT(%s) argument", &a->expr->where, | |
1496 | gfc_intent_string (f_intent)); | |
1497 | return FAILURE; | |
1498 | } | |
1499 | ||
1500 | if (a->expr->symtree->n.sym->attr.pointer) | |
1501 | { | |
1502 | gfc_error | |
1503 | ("Procedure argument at %L is local to a PURE procedure and " | |
1504 | "has the POINTER attribute", &a->expr->where); | |
1505 | return FAILURE; | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | ||
1510 | return SUCCESS; | |
1511 | } | |
1512 | ||
1513 | ||
1514 | /* Check how a procedure is used against its interface. If all goes | |
1515 | well, the actual argument list will also end up being properly | |
1516 | sorted. */ | |
1517 | ||
1518 | void | |
1519 | gfc_procedure_use (gfc_symbol * sym, gfc_actual_arglist ** ap, locus * where) | |
1520 | { | |
1521 | /* Warn about calls with an implicit interface. */ | |
1522 | if (gfc_option.warn_implicit_interface | |
1523 | && sym->attr.if_source == IFSRC_UNKNOWN) | |
1524 | gfc_warning ("Procedure '%s' called with an implicit interface at %L", | |
1525 | sym->name, where); | |
1526 | ||
1527 | if (sym->attr.if_source == IFSRC_UNKNOWN | |
1528 | || !compare_actual_formal (ap, sym->formal, 0, | |
1529 | sym->attr.elemental, where)) | |
1530 | return; | |
1531 | ||
1532 | check_intents (sym->formal, *ap); | |
1533 | if (gfc_option.warn_aliasing) | |
1534 | check_some_aliasing (sym->formal, *ap); | |
1535 | } | |
1536 | ||
1537 | ||
1538 | /* Given an interface pointer and an actual argument list, search for | |
1539 | a formal argument list that matches the actual. If found, returns | |
1540 | a pointer to the symbol of the correct interface. Returns NULL if | |
1541 | not found. */ | |
1542 | ||
1543 | gfc_symbol * | |
1544 | gfc_search_interface (gfc_interface * intr, int sub_flag, | |
1545 | gfc_actual_arglist ** ap) | |
1546 | { | |
1547 | int r; | |
1548 | ||
1549 | for (; intr; intr = intr->next) | |
1550 | { | |
1551 | if (sub_flag && intr->sym->attr.function) | |
1552 | continue; | |
1553 | if (!sub_flag && intr->sym->attr.subroutine) | |
1554 | continue; | |
1555 | ||
1556 | r = !intr->sym->attr.elemental; | |
1557 | ||
1558 | if (compare_actual_formal (ap, intr->sym->formal, r, !r, NULL)) | |
1559 | { | |
1560 | check_intents (intr->sym->formal, *ap); | |
1561 | if (gfc_option.warn_aliasing) | |
1562 | check_some_aliasing (intr->sym->formal, *ap); | |
1563 | return intr->sym; | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | return NULL; | |
1568 | } | |
1569 | ||
1570 | ||
1571 | /* Do a brute force recursive search for a symbol. */ | |
1572 | ||
1573 | static gfc_symtree * | |
1574 | find_symtree0 (gfc_symtree * root, gfc_symbol * sym) | |
1575 | { | |
1576 | gfc_symtree * st; | |
1577 | ||
1578 | if (root->n.sym == sym) | |
1579 | return root; | |
1580 | ||
1581 | st = NULL; | |
1582 | if (root->left) | |
1583 | st = find_symtree0 (root->left, sym); | |
1584 | if (root->right && ! st) | |
1585 | st = find_symtree0 (root->right, sym); | |
1586 | return st; | |
1587 | } | |
1588 | ||
1589 | ||
1590 | /* Find a symtree for a symbol. */ | |
1591 | ||
1592 | static gfc_symtree * | |
1593 | find_sym_in_symtree (gfc_symbol * sym) | |
1594 | { | |
1595 | gfc_symtree *st; | |
1596 | gfc_namespace *ns; | |
1597 | ||
1598 | /* First try to find it by name. */ | |
1599 | gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st); | |
1600 | if (st && st->n.sym == sym) | |
1601 | return st; | |
1602 | ||
1603 | /* if it's been renamed, resort to a brute-force search. */ | |
1604 | /* TODO: avoid having to do this search. If the symbol doesn't exist | |
1605 | in the symtree for the current namespace, it should probably be added. */ | |
1606 | for (ns = gfc_current_ns; ns; ns = ns->parent) | |
1607 | { | |
1608 | st = find_symtree0 (ns->sym_root, sym); | |
1609 | if (st) | |
1610 | return st; | |
1611 | } | |
1612 | gfc_internal_error ("Unable to find symbol %s", sym->name); | |
1613 | /* Not reached */ | |
1614 | } | |
1615 | ||
1616 | ||
1617 | /* This subroutine is called when an expression is being resolved. | |
1618 | The expression node in question is either a user defined operator | |
1619 | or an instrinsic operator with arguments that aren't compatible | |
1620 | with the operator. This subroutine builds an actual argument list | |
1621 | corresponding to the operands, then searches for a compatible | |
1622 | interface. If one is found, the expression node is replaced with | |
1623 | the appropriate function call. */ | |
1624 | ||
1625 | try | |
1626 | gfc_extend_expr (gfc_expr * e) | |
1627 | { | |
1628 | gfc_actual_arglist *actual; | |
1629 | gfc_symbol *sym; | |
1630 | gfc_namespace *ns; | |
1631 | gfc_user_op *uop; | |
1632 | gfc_intrinsic_op i; | |
1633 | ||
1634 | sym = NULL; | |
1635 | ||
1636 | actual = gfc_get_actual_arglist (); | |
1637 | actual->expr = e->op1; | |
1638 | ||
1639 | if (e->op2 != NULL) | |
1640 | { | |
1641 | actual->next = gfc_get_actual_arglist (); | |
1642 | actual->next->expr = e->op2; | |
1643 | } | |
1644 | ||
1645 | i = fold_unary (e->operator); | |
1646 | ||
1647 | if (i == INTRINSIC_USER) | |
1648 | { | |
1649 | for (ns = gfc_current_ns; ns; ns = ns->parent) | |
1650 | { | |
1651 | uop = gfc_find_uop (e->uop->name, ns); | |
1652 | if (uop == NULL) | |
1653 | continue; | |
1654 | ||
1655 | sym = gfc_search_interface (uop->operator, 0, &actual); | |
1656 | if (sym != NULL) | |
1657 | break; | |
1658 | } | |
1659 | } | |
1660 | else | |
1661 | { | |
1662 | for (ns = gfc_current_ns; ns; ns = ns->parent) | |
1663 | { | |
1664 | sym = gfc_search_interface (ns->operator[i], 0, &actual); | |
1665 | if (sym != NULL) | |
1666 | break; | |
1667 | } | |
1668 | } | |
1669 | ||
1670 | if (sym == NULL) | |
1671 | { | |
1672 | /* Don't use gfc_free_actual_arglist() */ | |
1673 | if (actual->next != NULL) | |
1674 | gfc_free (actual->next); | |
1675 | gfc_free (actual); | |
1676 | ||
1677 | return FAILURE; | |
1678 | } | |
1679 | ||
1680 | /* Change the expression node to a function call. */ | |
1681 | e->expr_type = EXPR_FUNCTION; | |
1682 | e->symtree = find_sym_in_symtree (sym); | |
1683 | e->value.function.actual = actual; | |
1684 | ||
1685 | if (gfc_pure (NULL) && !gfc_pure (sym)) | |
1686 | { | |
1687 | gfc_error | |
1688 | ("Function '%s' called in lieu of an operator at %L must be PURE", | |
1689 | sym->name, &e->where); | |
1690 | return FAILURE; | |
1691 | } | |
1692 | ||
1693 | if (gfc_resolve_expr (e) == FAILURE) | |
1694 | return FAILURE; | |
1695 | ||
1696 | return SUCCESS; | |
1697 | } | |
1698 | ||
1699 | ||
1700 | /* Tries to replace an assignment code node with a subroutine call to | |
1701 | the subroutine associated with the assignment operator. Return | |
1702 | SUCCESS if the node was replaced. On FAILURE, no error is | |
1703 | generated. */ | |
1704 | ||
1705 | try | |
1706 | gfc_extend_assign (gfc_code * c, gfc_namespace * ns) | |
1707 | { | |
1708 | gfc_actual_arglist *actual; | |
1709 | gfc_expr *lhs, *rhs; | |
1710 | gfc_symbol *sym; | |
1711 | ||
1712 | lhs = c->expr; | |
1713 | rhs = c->expr2; | |
1714 | ||
1715 | /* Don't allow an intrinsic assignment to be replaced. */ | |
1716 | if (lhs->ts.type != BT_DERIVED && rhs->ts.type != BT_DERIVED | |
1717 | && (lhs->ts.type == rhs->ts.type | |
1718 | || (gfc_numeric_ts (&lhs->ts) | |
1719 | && gfc_numeric_ts (&rhs->ts)))) | |
1720 | return FAILURE; | |
1721 | ||
1722 | actual = gfc_get_actual_arglist (); | |
1723 | actual->expr = lhs; | |
1724 | ||
1725 | actual->next = gfc_get_actual_arglist (); | |
1726 | actual->next->expr = rhs; | |
1727 | ||
1728 | sym = NULL; | |
1729 | ||
1730 | for (; ns; ns = ns->parent) | |
1731 | { | |
1732 | sym = gfc_search_interface (ns->operator[INTRINSIC_ASSIGN], 1, &actual); | |
1733 | if (sym != NULL) | |
1734 | break; | |
1735 | } | |
1736 | ||
1737 | if (sym == NULL) | |
1738 | { | |
1739 | gfc_free (actual->next); | |
1740 | gfc_free (actual); | |
1741 | return FAILURE; | |
1742 | } | |
1743 | ||
1744 | /* Replace the assignment with the call. */ | |
1745 | c->op = EXEC_CALL; | |
1746 | c->symtree = find_sym_in_symtree (sym); | |
1747 | c->expr = NULL; | |
1748 | c->expr2 = NULL; | |
1749 | c->ext.actual = actual; | |
1750 | ||
1751 | if (gfc_pure (NULL) && !gfc_pure (sym)) | |
1752 | { | |
1753 | gfc_error ("Subroutine '%s' called in lieu of assignment at %L must be " | |
1754 | "PURE", sym->name, &c->loc); | |
1755 | return FAILURE; | |
1756 | } | |
1757 | ||
1758 | return SUCCESS; | |
1759 | } | |
1760 | ||
1761 | ||
1762 | /* Make sure that the interface just parsed is not already present in | |
1763 | the given interface list. Ambiguity isn't checked yet since module | |
1764 | procedures can be present without interfaces. */ | |
1765 | ||
1766 | static try | |
1767 | check_new_interface (gfc_interface * base, gfc_symbol * new) | |
1768 | { | |
1769 | gfc_interface *ip; | |
1770 | ||
1771 | for (ip = base; ip; ip = ip->next) | |
1772 | { | |
1773 | if (ip->sym == new) | |
1774 | { | |
1775 | gfc_error ("Entity '%s' at %C is already present in the interface", | |
1776 | new->name); | |
1777 | return FAILURE; | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | return SUCCESS; | |
1782 | } | |
1783 | ||
1784 | ||
1785 | /* Add a symbol to the current interface. */ | |
1786 | ||
1787 | try | |
1788 | gfc_add_interface (gfc_symbol * new) | |
1789 | { | |
1790 | gfc_interface **head, *intr; | |
1791 | gfc_namespace *ns; | |
1792 | gfc_symbol *sym; | |
1793 | ||
1794 | switch (current_interface.type) | |
1795 | { | |
1796 | case INTERFACE_NAMELESS: | |
1797 | return SUCCESS; | |
1798 | ||
1799 | case INTERFACE_INTRINSIC_OP: | |
1800 | for (ns = current_interface.ns; ns; ns = ns->parent) | |
1801 | if (check_new_interface (ns->operator[current_interface.op], new) | |
1802 | == FAILURE) | |
1803 | return FAILURE; | |
1804 | ||
1805 | head = ¤t_interface.ns->operator[current_interface.op]; | |
1806 | break; | |
1807 | ||
1808 | case INTERFACE_GENERIC: | |
1809 | for (ns = current_interface.ns; ns; ns = ns->parent) | |
1810 | { | |
1811 | gfc_find_symbol (current_interface.sym->name, ns, 0, &sym); | |
1812 | if (sym == NULL) | |
1813 | continue; | |
1814 | ||
1815 | if (check_new_interface (sym->generic, new) == FAILURE) | |
1816 | return FAILURE; | |
1817 | } | |
1818 | ||
1819 | head = ¤t_interface.sym->generic; | |
1820 | break; | |
1821 | ||
1822 | case INTERFACE_USER_OP: | |
1823 | if (check_new_interface (current_interface.uop->operator, new) == | |
1824 | FAILURE) | |
1825 | return FAILURE; | |
1826 | ||
1827 | head = ¤t_interface.uop->operator; | |
1828 | break; | |
1829 | ||
1830 | default: | |
1831 | gfc_internal_error ("gfc_add_interface(): Bad interface type"); | |
1832 | } | |
1833 | ||
1834 | intr = gfc_get_interface (); | |
1835 | intr->sym = new; | |
1836 | intr->where = *gfc_current_locus (); | |
1837 | ||
1838 | intr->next = *head; | |
1839 | *head = intr; | |
1840 | ||
1841 | return SUCCESS; | |
1842 | } | |
1843 | ||
1844 | ||
1845 | /* Gets rid of a formal argument list. We do not free symbols. | |
1846 | Symbols are freed when a namespace is freed. */ | |
1847 | ||
1848 | void | |
1849 | gfc_free_formal_arglist (gfc_formal_arglist * p) | |
1850 | { | |
1851 | gfc_formal_arglist *q; | |
1852 | ||
1853 | for (; p; p = q) | |
1854 | { | |
1855 | q = p->next; | |
1856 | gfc_free (p); | |
1857 | } | |
1858 | } |