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