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