Debian Codegraph @

gcc/fortran/coarray.cc

raw
1 /* Rewrite the expression tree for coarrays.
2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
3 Contributed by Andre Vehreschild.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* Rewrite the expression for coarrays where needed:
22 - coarray indexing operations need the indexing expression put into a
23 routine callable on the remote image
24
25 This rewriter is meant to used for non-optimisational expression tree
26 rewrites. When implementing early optimisation it is recommended to
27 do this in frontend-passes.cc.
28 */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "options.h"
34 #include "bitmap.h"
35 #include "gfortran.h"
36
37 /* The code tree element that is currently processed. */
38 static gfc_code **current_code;
39
40 /* Code that is inserted into the current caf_accessor at the beginning. */
41 static gfc_code *caf_accessor_prepend = nullptr;
42
43 static bool caf_on_lhs = false;
44
45 static int caf_sym_cnt = 0;
46
47 static gfc_array_spec *
48 get_arrayspec_from_expr (gfc_expr *expr)
49 {
50 gfc_array_spec *src_as, *dst_as = NULL;
51 gfc_ref *ref;
52 gfc_array_ref mod_src_ar;
53 int dst_rank = 0;
54
55 if (expr->rank == 0)
56 return NULL;
57
58 if (expr->expr_type == EXPR_FUNCTION)
59 return gfc_copy_array_spec (expr->symtree->n.sym->as);
60
61 /* Follow any component references. */
62 if (expr->expr_type == EXPR_VARIABLE || expr->expr_type == EXPR_CONSTANT)
63 {
64 if (expr->symtree)
65 src_as = expr->symtree->n.sym->as;
66 else
67 src_as = NULL;
68
69 for (ref = expr->ref; ref; ref = ref->next)
70 {
71 switch (ref->type)
72 {
73 case REF_COMPONENT:
74 src_as = ref->u.c.component->as;
75 continue;
76
77 case REF_SUBSTRING:
78 case REF_INQUIRY:
79 continue;
80
81 case REF_ARRAY:
82 switch (ref->u.ar.type)
83 {
84 case AR_ELEMENT:
85 src_as = NULL;
86 break;
87 case AR_SECTION:
88 {
89 if (!dst_as)
90 dst_as = gfc_get_array_spec ();
91 memset (&mod_src_ar, 0, sizeof (gfc_array_ref));
92 mod_src_ar = ref->u.ar;
93 for (int dim = 0; dim < src_as->rank; ++dim)
94 {
95 switch (ref->u.ar.dimen_type[dim])
96 {
97 case DIMEN_ELEMENT:
98 gfc_free_expr (mod_src_ar.start[dim]);
99 mod_src_ar.start[dim] = NULL;
100 break;
101 case DIMEN_RANGE:
102 dst_as->lower[dst_rank]
103 = gfc_copy_expr (ref->u.ar.start[dim]);
104 mod_src_ar.start[dst_rank]
105 = gfc_copy_expr (ref->u.ar.start[dim]);
106 if (ref->u.ar.end[dim])
107 {
108 dst_as->upper[dst_rank]
109 = gfc_copy_expr (ref->u.ar.end[dim]);
110 mod_src_ar.end[dst_rank] = ref->u.ar.end[dim];
111 mod_src_ar.stride[dst_rank]
112 = ref->u.ar.stride[dim];
113 }
114 else
115 dst_as->upper[dst_rank]
116 = gfc_copy_expr (ref->u.ar.as->upper[dim]);
117 ++dst_rank;
118 break;
119 case DIMEN_STAR:
120 dst_as->lower[dst_rank]
121 = gfc_copy_expr (ref->u.ar.as->lower[dim]);
122 mod_src_ar.start[dst_rank]
123 = gfc_copy_expr (ref->u.ar.start[dim]);
124 if (ref->u.ar.as->upper[dim])
125 {
126 dst_as->upper[dst_rank]
127 = gfc_copy_expr (ref->u.ar.as->upper[dim]);
128 mod_src_ar.end[dst_rank] = ref->u.ar.end[dim];
129 mod_src_ar.stride[dst_rank]
130 = ref->u.ar.stride[dim];
131 }
132 ++dst_rank;
133 break;
134 case DIMEN_VECTOR:
135 dst_as->lower[dst_rank]
136 = gfc_get_constant_expr (BT_INTEGER,
137 gfc_index_integer_kind,
138 &expr->where);
139 mpz_set_ui (dst_as->lower[dst_rank]->value.integer,
140 1);
141 mod_src_ar.start[dst_rank]
142 = gfc_copy_expr (ref->u.ar.start[dim]);
143 dst_as->upper[dst_rank]
144 = gfc_get_constant_expr (BT_INTEGER,
145 gfc_index_integer_kind,
146 &expr->where);
147 mpz_set (dst_as->upper[dst_rank]->value.integer,
148 ref->u.ar.start[dim]->shape[0]);
149 ++dst_rank;
150 break;
151 case DIMEN_THIS_IMAGE:
152 case DIMEN_UNKNOWN:
153 gcc_unreachable ();
154 }
155 if (ref->u.ar.dimen_type[dim] != DIMEN_ELEMENT)
156 mod_src_ar.dimen_type[dst_rank]
157 = ref->u.ar.dimen_type[dim];
158 }
159 dst_as->rank = dst_rank;
160 dst_as->type = AS_EXPLICIT;
161 ref->u.ar = mod_src_ar;
162 ref->u.ar.dimen = dst_rank;
163 break;
164
165 case AR_UNKNOWN:
166 src_as = NULL;
167 break;
168
169 case AR_FULL:
170 if (dst_as)
171 /* Prevent memory loss. */
172 gfc_free_array_spec (dst_as);
173 dst_as = gfc_copy_array_spec (src_as);
174 break;
175 }
176 break;
177 }
178 }
179 }
180 }
181 else
182 src_as = NULL;
183
184 return dst_as;
185 }
186
187 static void
188 remove_coarray_from_derived_type (gfc_symbol *base, gfc_namespace *ns,
189 gfc_array_spec *src_as = NULL)
190 {
191 gfc_symbol *derived;
192 gfc_symbol *src_derived = base->ts.u.derived;
193
194 if (!src_as)
195 src_as = src_derived->as;
196 gfc_get_symbol (src_derived->name, ns, &derived);
197 derived->attr.flavor = FL_DERIVED;
198 derived->attr.alloc_comp = src_derived->attr.alloc_comp;
199 if (src_as && src_as->rank != 0)
200 {
201 base->attr.dimension = 1;
202 base->as = gfc_copy_array_spec (src_as);
203 base->as->corank = 0;
204 }
205 for (gfc_component *p = NULL, *c = src_derived->components; c; c = c->next)
206 {
207 gfc_component *n = gfc_get_component ();
208 *n = *c;
209 if (n->as)
210 n->as = gfc_copy_array_spec (c->as);
211 n->backend_decl = NULL;
212 n->initializer = NULL;
213 n->param_list = NULL;
214 if (p)
215 p->next = n;
216 else
217 derived->components = n;
218
219 p = n;
220 }
221 derived->declared_at = base->declared_at;
222 gfc_set_sym_referenced (derived);
223 gfc_commit_symbol (derived);
224 base->ts.u.derived = derived;
225 gfc_commit_symbol (base);
226 }
227
228 static void
229 convert_coarray_class_to_derived_type (gfc_symbol *base, gfc_namespace *ns)
230 {
231 gfc_symbol *src_derived = CLASS_DATA (base)->ts.u.derived;
232 gfc_array_spec *src_as = CLASS_DATA (base)->as;
233 const bool attr_allocatable
234 = src_as && src_as->rank && src_as->type == AS_DEFERRED;
235
236 base->ts.type = BT_DERIVED;
237 base->ts.u.derived = src_derived;
238
239 remove_coarray_from_derived_type (base, ns, src_as);
240
241 base->attr.allocatable = attr_allocatable;
242 base->attr.pointer = 0; // Ensure, that it is no pointer.
243 }
244
245 static void
246 move_coarray_ref (gfc_ref **from, gfc_expr *expr)
247 {
248 int i;
249 gfc_ref *to = expr->ref;
250 for (; to && to->next; to = to->next)
251 ;
252
253 if (!to)
254 {
255 expr->ref = gfc_get_ref ();
256 to = expr->ref;
257 to->type = REF_ARRAY;
258 }
259 gcc_assert (to->type == REF_ARRAY);
260 to->u.ar.as = gfc_copy_array_spec ((*from)->u.ar.as);
261 to->u.ar.codimen = (*from)->u.ar.codimen;
262 to->u.ar.dimen = (*from)->u.ar.dimen;
263 to->u.ar.type = AR_FULL;
264 to->u.ar.stat = (*from)->u.ar.stat;
265 (*from)->u.ar.stat = nullptr;
266 to->u.ar.team = (*from)->u.ar.team;
267 (*from)->u.ar.team = nullptr;
268 to->u.ar.team_type = (*from)->u.ar.team_type;
269 (*from)->u.ar.team_type = TEAM_UNSET;
270 for (i = 0; i < to->u.ar.dimen; ++i)
271 {
272 to->u.ar.start[i] = nullptr;
273 to->u.ar.end[i] = nullptr;
274 to->u.ar.stride[i] = nullptr;
275 }
276 for (i = (*from)->u.ar.dimen; i < (*from)->u.ar.dimen + (*from)->u.ar.codimen;
277 ++i)
278 {
279 to->u.ar.dimen_type[i] = (*from)->u.ar.dimen_type[i];
280 to->u.ar.start[i] = (*from)->u.ar.start[i];
281 (*from)->u.ar.start[i] = nullptr;
282 to->u.ar.end[i] = (*from)->u.ar.end[i];
283 (*from)->u.ar.end[i] = nullptr;
284 to->u.ar.stride[i] = (*from)->u.ar.stride[i];
285 (*from)->u.ar.stride[i] = nullptr;
286 }
287 (*from)->u.ar.codimen = 0;
288 if ((*from)->u.ar.dimen == 0)
289 {
290 gfc_ref *nref = (*from)->next;
291 (*from)->next = nullptr;
292 gfc_free_ref_list (*from);
293 *from = nref;
294 }
295 }
296
297 static void
298 fixup_comp_refs (gfc_expr *expr)
299 {
300 bool class_ref = expr->symtree->n.sym->ts.type == BT_CLASS;
301 gfc_symbol *type
302 = expr->symtree->n.sym->ts.type == BT_DERIVED
303 ? expr->symtree->n.sym->ts.u.derived
304 : (class_ref ? CLASS_DATA (expr->symtree->n.sym)->ts.u.derived
305 : nullptr);
306 if (!type)
307 return;
308 gfc_ref **pref = &(expr->ref);
309 for (gfc_ref *ref = expr->ref; ref && type;)
310 {
311 switch (ref->type)
312 {
313 case REF_COMPONENT:
314 gfc_find_component (type, ref->u.c.component->name, false, true,
315 pref);
316 if (!*pref)
317 {
318 /* This happens when there were errors previously. Just don't
319 crash. */
320 ref = nullptr;
321 break;
322 }
323 if (class_ref)
324 /* Link to the class type to allow for derived type resolution. */
325 (*pref)->u.c.sym = ref->u.c.sym;
326 (*pref)->next = ref->next;
327 ref->next = NULL;
328 gfc_free_ref_list (ref);
329 ref = (*pref)->next;
330 type = (*pref)->u.c.component->ts.type == BT_DERIVED
331 ? (*pref)->u.c.component->ts.u.derived
332 : ((*pref)->u.c.component->ts.type == BT_CLASS
333 ? CLASS_DATA ((*pref)->u.c.component)->ts.u.derived
334 : nullptr);
335 pref = &(*pref)->next;
336 break;
337 case REF_ARRAY:
338 pref = &ref->next;
339 ref = ref->next;
340 break;
341 default:
342 gcc_unreachable ();
343 break;
344 }
345 }
346 }
347
348 static void
349 split_expr_at_caf_ref (gfc_expr *expr, gfc_namespace *ns,
350 gfc_expr **post_caf_ref_expr, bool for_send)
351 {
352 gfc_ref *caf_ref = NULL;
353 gfc_symtree *st;
354 gfc_symbol *base;
355 gfc_typespec *caf_ts;
356 bool created;
357
358 gcc_assert (expr->expr_type == EXPR_VARIABLE);
359 caf_ts = &expr->symtree->n.sym->ts;
360 if (!expr->symtree->n.sym->attr.codimension)
361 {
362 /* The coarray is in some component. Find it. */
363 caf_ref = expr->ref;
364 while (caf_ref)
365 {
366 if (caf_ref->type == REF_ARRAY && caf_ref->u.ar.codimen != 0)
367 break;
368 if (caf_ref->type == REF_COMPONENT)
369 caf_ts = &caf_ref->u.c.component->ts;
370 caf_ref = caf_ref->next;
371 }
372 }
373
374 created = !gfc_get_sym_tree (!caf_ref ? expr->symtree->name : "base", ns, &st,
375 false);
376 gcc_assert (created);
377 st->n.sym->attr.flavor = FL_PARAMETER;
378 st->n.sym->attr.dummy = 1;
379 st->n.sym->attr.intent = INTENT_IN;
380 st->n.sym->ts = *caf_ts;
381 st->n.sym->declared_at = expr->where;
382
383 *post_caf_ref_expr = gfc_get_variable_expr (st);
384 (*post_caf_ref_expr)->where = expr->where;
385 base = (*post_caf_ref_expr)->symtree->n.sym;
386
387 if (!caf_ref)
388 {
389 (*post_caf_ref_expr)->ref = gfc_get_ref ();
390 *(*post_caf_ref_expr)->ref = *expr->ref;
391 expr->ref = nullptr;
392 move_coarray_ref (&(*post_caf_ref_expr)->ref, expr);
393 fixup_comp_refs (expr);
394
395 if (expr->symtree->n.sym->attr.dimension)
396 {
397 base->as = gfc_copy_array_spec (expr->symtree->n.sym->as);
398 base->as->corank = 0;
399 base->attr.dimension = 1;
400 base->attr.allocatable = expr->symtree->n.sym->attr.allocatable;
401 base->attr.pointer = expr->symtree->n.sym->attr.pointer
402 || expr->symtree->n.sym->attr.associate_var;
403 }
404 }
405 else
406 {
407 (*post_caf_ref_expr)->ref = gfc_get_ref ();
408 *(*post_caf_ref_expr)->ref = *caf_ref;
409 caf_ref->next = nullptr;
410 move_coarray_ref (&(*post_caf_ref_expr)->ref, expr);
411 fixup_comp_refs (expr);
412
413 if (caf_ref && caf_ref->u.ar.dimen)
414 {
415 base->as = gfc_copy_array_spec (caf_ref->u.ar.as);
416 base->as->corank = 0;
417 base->attr.dimension = 1;
418 base->attr.allocatable = caf_ref->u.ar.as->type != AS_EXPLICIT;
419 }
420 base->ts = *caf_ts;
421 }
422 (*post_caf_ref_expr)->ts = expr->ts;
423 if (base->ts.type == BT_CHARACTER)
424 {
425 base->ts.u.cl = gfc_get_charlen ();
426 *base->ts.u.cl = *(caf_ts->u.cl);
427 base->ts.deferred = 1;
428 base->ts.u.cl->length = nullptr;
429 }
430 else if (base->ts.type == BT_DERIVED)
431 remove_coarray_from_derived_type (base, ns);
432 else if (base->ts.type == BT_CLASS)
433 convert_coarray_class_to_derived_type (base, ns);
434
435 gfc_expression_rank (*post_caf_ref_expr);
436 if (for_send)
437 gfc_expression_rank (expr);
438 else
439 expr->rank = (*post_caf_ref_expr)->rank;
440 }
441
442 static void add_caf_get_from_remote (gfc_expr *e);
443
444 static gfc_component *
445 find_comp (gfc_symbol *type, gfc_expr *e, int *cnt, const bool is_var)
446 {
447 char *temp_name = nullptr;
448 gfc_component *comp = type->components;
449
450 /* For variables:
451 - look up same name or create new
452 all else:
453 - create unique new
454 */
455 if (is_var)
456 {
457 ++(*cnt);
458 free (temp_name);
459 temp_name = xasprintf ("caf_temp_%s_%d", e->symtree->name, *cnt);
460 while (comp && strcmp (comp->name, temp_name) != 0)
461 comp = comp->next;
462 if (!comp)
463 {
464 const bool added = gfc_add_component (type, temp_name, &comp);
465 gcc_assert (added);
466 }
467 }
468 else
469 {
470 int r = -1;
471 /* Components are always appended, i.e., when searching to add a unique
472 one just iterating forward is sufficient. */
473 do
474 {
475 ++(*cnt);
476 free (temp_name);
477 temp_name = xasprintf ("caf_temp_%s_%d", e->symtree->name, *cnt);
478
479 while (comp && (r = strcmp (comp->name, temp_name)) <= 0)
480 comp = comp->next;
481 }
482 while (comp && r <= 0);
483 {
484 const bool added = gfc_add_component (type, temp_name, &comp);
485 gcc_assert (added);
486 }
487 }
488
489 comp->loc = e->where;
490 comp->ts = e->ts;
491 free (temp_name);
492
493 return comp;
494 }
495
496 static void
497 check_add_new_comp_handle_array (gfc_expr *e, gfc_symbol *type,
498 gfc_symbol *add_data)
499 {
500 gfc_component *comp;
501 static int cnt = -1;
502 gfc_symtree *caller_image;
503 gfc_code *pre_code = caf_accessor_prepend;
504 bool static_array_or_scalar = true;
505 symbol_attribute e_attr = gfc_expr_attr (e);
506
507 gfc_free_shape (&e->shape, e->rank);
508
509 /* When already code to prepend into the accessor exists, go to
510 the end of the chain. */
511 for (; pre_code && pre_code->next; pre_code = pre_code->next)
512 ;
513
514 comp = find_comp (type, e, &cnt,
515 e->symtree->n.sym->attr.flavor == FL_VARIABLE
516 || e->symtree->n.sym->attr.flavor == FL_PARAMETER);
517
518 if (e->expr_type == EXPR_FUNCTION
519 || (e->expr_type == EXPR_VARIABLE && e_attr.dimension
520 && e_attr.allocatable))
521 {
522 gfc_code *code;
523 gfc_symtree *st;
524 const bool created
525 = !gfc_get_sym_tree (comp->name, gfc_current_ns, &st, false, &e->where);
526 gcc_assert (created);
527
528 st->n.sym->ts = e->ts;
529 gfc_set_sym_referenced (st->n.sym);
530 code = gfc_get_code (EXEC_ASSIGN);
531 code->loc = e->where;
532 code->expr1 = gfc_get_variable_expr (st);
533 code->expr2 = XCNEW (gfc_expr);
534 *(code->expr2) = *e;
535 code->next = *current_code;
536 *current_code = code;
537
538 if (e_attr.dimension)
539 {
540 gfc_array_spec *as = get_arrayspec_from_expr (e);
541 static_array_or_scalar = gfc_is_compile_time_shape (as);
542
543 comp->attr.dimension = 1;
544 st->n.sym->attr.dimension = 1;
545 st->n.sym->as = as;
546
547 if (!static_array_or_scalar)
548 {
549 comp->attr.allocatable = 1;
550 st->n.sym->attr.allocatable = 1;
551 }
552 code->expr1->rank = as->rank;
553 gfc_add_full_array_ref (code->expr1, gfc_copy_array_spec (as));
554 comp->as = gfc_copy_array_spec (as);
555 }
556
557 gfc_expression_rank (code->expr1);
558 comp->initializer = gfc_get_variable_expr (st);
559 gfc_commit_symbol (st->n.sym);
560 }
561 else
562 {
563 comp->initializer = gfc_copy_expr (e);
564 if (e_attr.dimension && e->rank)
565 {
566 comp->attr.dimension = 1;
567 comp->as = get_arrayspec_from_expr (e);
568 }
569 }
570 comp->initializer->where = e->where;
571 comp->attr.access = ACCESS_PRIVATE;
572 memset (e, 0, sizeof (gfc_expr));
573 e->ts = comp->initializer->ts;
574 e->expr_type = EXPR_VARIABLE;
575 e->where = comp->initializer->where;
576
577 if (comp->as && comp->as->rank)
578 {
579 if (static_array_or_scalar)
580 {
581 e->ref = gfc_get_ref ();
582 e->ref->type = REF_ARRAY;
583 e->ref->u.ar.as = gfc_copy_array_spec (add_data->as);
584 e->ref->u.ar.codimen = 1;
585 e->ref->u.ar.dimen_type[0] = DIMEN_THIS_IMAGE;
586 }
587 else
588 {
589 gfc_code *c;
590 gfc_symtree *lv, *ad;
591 bool created = !gfc_get_sym_tree (comp->name, add_data->ns, &lv,
592 false, &e->where);
593 gcc_assert (created);
594
595 lv->n.sym->ts = e->ts;
596 lv->n.sym->attr.dimension = 1;
597 lv->n.sym->attr.allocatable = 1;
598 lv->n.sym->attr.flavor = FL_VARIABLE;
599 lv->n.sym->as = gfc_copy_array_spec (comp->as);
600 gfc_set_sym_referenced (lv->n.sym);
601 gfc_commit_symbol (lv->n.sym);
602 c = gfc_get_code (EXEC_ASSIGN);
603 c->loc = e->where;
604 c->expr1 = gfc_get_variable_expr (lv);
605 c->expr1->where = e->where;
606
607 created = !gfc_find_sym_tree (add_data->name, add_data->ns, 0, &ad);
608 gcc_assert (created);
609 c->expr2 = gfc_get_variable_expr (ad);
610 c->expr2->where = e->where;
611 c->expr2->ts = comp->initializer->ts;
612 c->expr2->ref = gfc_get_ref ();
613 c->expr2->ref->type = REF_ARRAY;
614 c->expr2->ref->u.ar.as = gfc_copy_array_spec (add_data->as);
615 c->expr2->ref->u.ar.codimen = 1;
616 c->expr2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
617 caller_image
618 = gfc_find_symtree_in_proc ("__caller_image", add_data->ns);
619 gcc_assert (caller_image);
620 c->expr2->ref->u.ar.start[0] = gfc_get_variable_expr (caller_image);
621 c->expr2->ref->u.ar.start[0]->where = e->where;
622 created = gfc_find_component (ad->n.sym->ts.u.derived, comp->name,
623 false, true, &c->expr2->ref->next)
624 != nullptr;
625 gcc_assert (created);
626 c->expr2->rank = comp->as->rank;
627 gfc_add_full_array_ref (c->expr2, gfc_copy_array_spec (comp->as));
628 gfc_set_sym_referenced (ad->n.sym);
629 gfc_commit_symbol (ad->n.sym);
630 if (pre_code)
631 pre_code->next = c;
632 else
633 caf_accessor_prepend = c;
634 add_caf_get_from_remote (c->expr2);
635
636 e->symtree = lv;
637 gfc_expression_rank (e);
638 gfc_add_full_array_ref (e, gfc_copy_array_spec (comp->as));
639 }
640 }
641 else
642 {
643 e->ref = gfc_get_ref ();
644 e->ref->type = REF_ARRAY;
645 e->ref->u.ar.as = gfc_copy_array_spec (add_data->as);
646 e->ref->u.ar.codimen = 1;
647 e->ref->u.ar.dimen_type[0] = DIMEN_THIS_IMAGE;
648 }
649
650 if (static_array_or_scalar)
651 {
652 const bool created
653 = gfc_find_component (add_data->ts.u.derived, comp->name, false, true,
654 &e->ref);
655 gcc_assert (created);
656 e->symtree = gfc_find_symtree (add_data->ns->sym_root, add_data->name);
657 gcc_assert (e->symtree);
658 if (IS_CLASS_ARRAY (e->ref->u.c.component)
659 || e->ref->u.c.component->attr.dimension)
660 {
661 gfc_add_full_array_ref (e, e->ref->u.c.component->ts.type == BT_CLASS
662 ? CLASS_DATA (e->ref->u.c.component)->as
663 : e->ref->u.c.component->as);
664 e->ref->next->u.ar.dimen
665 = e->ref->u.c.component->ts.type == BT_CLASS
666 ? CLASS_DATA (e->ref->u.c.component)->as->rank
667 : e->ref->u.c.component->as->rank;
668 }
669 gfc_expression_rank (e);
670 }
671 }
672
673 static void
674 check_add_new_component (gfc_symbol *type, gfc_expr *e, gfc_symbol *add_data)
675 {
676 if (e)
677 {
678 switch (e->expr_type)
679 {
680 case EXPR_CONSTANT:
681 case EXPR_NULL:
682 break;
683 case EXPR_OP:
684 check_add_new_component (type, e->value.op.op1, add_data);
685 if (e->value.op.op2)
686 check_add_new_component (type, e->value.op.op2, add_data);
687 break;
688 case EXPR_COMPCALL:
689 for (gfc_actual_arglist *actual = e->value.compcall.actual; actual;
690 actual = actual->next)
691 check_add_new_component (type, actual->expr, add_data);
692 break;
693 case EXPR_FUNCTION:
694 if (!e->symtree->n.sym->attr.pure
695 && !e->symtree->n.sym->attr.elemental)
696 /* Treat non-pure/non-elemental functions. */
697 check_add_new_comp_handle_array (e, type, add_data);
698 else
699 for (gfc_actual_arglist *actual = e->value.function.actual; actual;
700 actual = actual->next)
701 check_add_new_component (type, actual->expr, add_data);
702 break;
703 case EXPR_VARIABLE:
704 check_add_new_comp_handle_array (e, type, add_data);
705 break;
706 case EXPR_ARRAY:
707 case EXPR_PPC:
708 case EXPR_STRUCTURE:
709 case EXPR_SUBSTRING:
710 gcc_unreachable ();
711 default:;
712 }
713 }
714 }
715
716 static gfc_symbol *
717 create_caf_add_data_parameter_type (gfc_expr *expr, gfc_namespace *ns,
718 gfc_symbol *add_data)
719 {
720 static int type_cnt = 0;
721 char tname[GFC_MAX_SYMBOL_LEN + 1];
722 char *name;
723 gfc_symbol *type;
724
725 gcc_assert (expr->expr_type == EXPR_VARIABLE);
726
727 strcpy (tname, expr->symtree->name);
728 name = xasprintf ("@_caf_add_data_t_%s_%d", tname, ++type_cnt);
729 gfc_get_symbol (name, ns, &type);
730
731 type->attr.flavor = FL_DERIVED;
732 add_data->ts.u.derived = type;
733 add_data->attr.codimension = 1;
734 add_data->as = gfc_get_array_spec ();
735 add_data->as->corank = 1;
736 add_data->as->type = AS_EXPLICIT;
737 add_data->as->cotype = AS_DEFERRED;
738 add_data->as->lower[0]
739 = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
740 &expr->where);
741 mpz_init (add_data->as->lower[0]->value.integer);
742 mpz_set_si (add_data->as->lower[0]->value.integer, 1);
743
744 for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
745 {
746 if (ref->type == REF_ARRAY)
747 {
748 gfc_array_ref *ar = &ref->u.ar;
749 for (int i = 0; i < ar->dimen; ++i)
750 {
751 check_add_new_component (type, ar->start[i], add_data);
752 check_add_new_component (type, ar->end[i], add_data);
753 check_add_new_component (type, ar->stride[i], add_data);
754 }
755 }
756 }
757
758 type->declared_at = expr->where;
759 gfc_set_sym_referenced (type);
760 gfc_commit_symbol (type);
761 return type;
762 }
763
764 static void
765 remove_caf_ref (gfc_expr *expr, const bool conv_to_this_image_cafref = false)
766 {
767 gfc_ref *ref = expr->ref;
768 while (ref && (ref->type != REF_ARRAY || ref->u.ar.codimen == 0))
769 {
770 ref = ref->next;
771 }
772 if (ref && ref->type == REF_ARRAY && ref->u.ar.codimen != 0)
773 {
774 if (ref->u.ar.dimen != 0)
775 {
776 ref->u.ar.codimen = 0;
777 ref = ref->next;
778 }
779 else
780 {
781 if (conv_to_this_image_cafref)
782 {
783 for (int i = ref->u.ar.dimen;
784 i < ref->u.ar.dimen + ref->u.ar.codimen; ++i)
785 ref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
786 }
787 else
788 {
789 expr->ref = ref->next;
790 ref->next = NULL;
791 gfc_free_ref_list (ref);
792 ref = expr->ref;
793 }
794 }
795 }
796 fixup_comp_refs (expr);
797 }
798
799 static gfc_expr *
800 create_get_callback (gfc_expr *expr)
801 {
802 gfc_namespace *ns;
803 gfc_symbol *extproc, *proc, *buffer, *free_buffer, *base, *get_data,
804 *old_buffer_data, *caller_image;
805 char tname[GFC_MAX_SYMBOL_LEN + 1];
806 char *name;
807 const char *mname;
808 gfc_expr *cb, *post_caf_ref_expr;
809 gfc_code *code;
810 int expr_rank = expr->rank;
811 gfc_code *backup_caf_accessor_prepend = caf_accessor_prepend;
812 caf_accessor_prepend = nullptr;
813
814 /* Find the top-level namespace. */
815 for (ns = gfc_current_ns; ns->parent; ns = ns->parent)
816 ;
817
818 if (expr->expr_type == EXPR_VARIABLE)
819 strcpy (tname, expr->symtree->name);
820 else
821 strcpy (tname, "dummy");
822 if (expr->symtree->n.sym->module)
823 mname = expr->symtree->n.sym->module;
824 else
825 mname = "main";
826 name = xasprintf ("_caf_accessor_%s_%s_%d", mname, tname, ++caf_sym_cnt);
827 gfc_get_symbol (name, ns, &extproc);
828 extproc->declared_at = expr->where;
829 gfc_set_sym_referenced (extproc);
830 ++extproc->refs;
831 gfc_commit_symbol (extproc);
832
833 /* Set up namespace. */
834 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
835 sub_ns->sibling = ns->contained;
836 ns->contained = sub_ns;
837 sub_ns->resolved = 1;
838 /* Set up procedure symbol. */
839 gfc_find_symbol (name, sub_ns, 1, &proc);
840 sub_ns->proc_name = proc;
841 proc->attr.if_source = IFSRC_DECL;
842 proc->attr.access = ACCESS_PUBLIC;
843 gfc_add_subroutine (&proc->attr, name, NULL);
844 proc->attr.host_assoc = 1;
845 proc->attr.always_explicit = 1;
846 ++proc->refs;
847 proc->declared_at = expr->where;
848 gfc_commit_symbol (proc);
849 free (name);
850
851 split_expr_at_caf_ref (expr, sub_ns, &post_caf_ref_expr, false);
852
853 if (ns->proc_name->attr.flavor == FL_MODULE)
854 proc->module = ns->proc_name->name;
855 gfc_set_sym_referenced (proc);
856 /* Set up formal arguments. */
857 gfc_formal_arglist **argptr = &proc->formal;
858 #define ADD_ARG(name, nsym, stype, skind, sintent) \
859 gfc_get_symbol (name, sub_ns, &nsym); \
860 nsym->ts.type = stype; \
861 nsym->ts.kind = skind; \
862 nsym->attr.flavor = FL_PARAMETER; \
863 nsym->attr.dummy = 1; \
864 nsym->attr.intent = sintent; \
865 nsym->declared_at = expr->where; \
866 gfc_set_sym_referenced (nsym); \
867 *argptr = gfc_get_formal_arglist (); \
868 (*argptr)->sym = nsym; \
869 argptr = &(*argptr)->next
870
871 name = xasprintf ("__add_data_%s_%s_%d", mname, tname, caf_sym_cnt);
872 ADD_ARG (name, get_data, BT_DERIVED, 0, INTENT_IN);
873 gfc_commit_symbol (get_data);
874 free (name);
875
876 ADD_ARG ("__caller_image", caller_image, BT_INTEGER, gfc_default_integer_kind,
877 INTENT_IN);
878 gfc_commit_symbol (caller_image);
879
880 ADD_ARG ("__buffer", buffer, expr->ts.type, expr->ts.kind, INTENT_INOUT);
881 buffer->ts = expr->ts;
882 if (expr_rank)
883 {
884 buffer->as = gfc_get_array_spec ();
885 buffer->as->rank = expr_rank;
886 if (expr->shape)
887 {
888 buffer->as->type = AS_EXPLICIT;
889 for (int d = 0; d < expr_rank; ++d)
890 {
891 buffer->as->lower[d]
892 = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
893 &gfc_current_locus);
894 gfc_mpz_set_hwi (buffer->as->lower[d]->value.integer, 1);
895 buffer->as->upper[d]
896 = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
897 &gfc_current_locus);
898 gfc_mpz_set_hwi (buffer->as->upper[d]->value.integer,
899 gfc_mpz_get_hwi (expr->shape[d]));
900 }
901 buffer->attr.allocatable = 1;
902 }
903 else
904 {
905 buffer->as->type = AS_DEFERRED;
906 buffer->attr.allocatable = 1;
907 }
908 buffer->attr.dimension = 1;
909 }
910 else
911 buffer->attr.pointer = 1;
912 if (buffer->ts.type == BT_CHARACTER)
913 {
914 buffer->ts.u.cl = gfc_get_charlen ();
915 *buffer->ts.u.cl = *expr->ts.u.cl;
916 buffer->ts.u.cl->length = gfc_copy_expr (expr->ts.u.cl->length);
917 }
918 gfc_commit_symbol (buffer);
919
920 ADD_ARG ("__free_buffer", free_buffer, BT_LOGICAL, gfc_default_logical_kind,
921 INTENT_OUT);
922 gfc_commit_symbol (free_buffer);
923
924 // ADD_ARG (expr->symtree->name, base, BT_VOID, INTENT_IN);
925 base = post_caf_ref_expr->symtree->n.sym;
926 gfc_set_sym_referenced (base);
927 gfc_commit_symbol (base);
928 *argptr = gfc_get_formal_arglist ();
929 (*argptr)->sym = base;
930 argptr = &(*argptr)->next;
931 gfc_commit_symbol (base);
932 #undef ADD_ARG
933
934 /* Set up code. */
935 if (expr->rank != 0)
936 {
937 /* Code: old_buffer_ptr = C_LOC (buffer); */
938 code = sub_ns->code = gfc_get_code (EXEC_ASSIGN);
939 gfc_get_symbol ("old_buffer_data", sub_ns, &old_buffer_data);
940 old_buffer_data->ts.type = BT_VOID;
941 old_buffer_data->attr.flavor = FL_VARIABLE;
942 old_buffer_data->declared_at = expr->where;
943 gfc_set_sym_referenced (old_buffer_data);
944 gfc_commit_symbol (old_buffer_data);
945 code->loc = expr->where;
946 code->expr1 = gfc_lval_expr_from_sym (old_buffer_data);
947 code->expr2 = gfc_build_intrinsic_call (ns, GFC_ISYM_C_LOC, "C_LOC",
948 gfc_current_locus, 1,
949 gfc_lval_expr_from_sym (buffer));
950 code->next = gfc_get_code (EXEC_ASSIGN);
951 code = code->next;
952 }
953 else
954 code = sub_ns->code = gfc_get_code (EXEC_POINTER_ASSIGN);
955
956 /* Code: buffer = expr; */
957 code->loc = expr->where;
958 code->expr1 = gfc_lval_expr_from_sym (buffer);
959 code->expr2 = post_caf_ref_expr;
960 remove_caf_ref (post_caf_ref_expr);
961 get_data->ts.u.derived
962 = create_caf_add_data_parameter_type (code->expr2, ns, get_data);
963 if (code->expr2->rank == 0 && code->expr2->ts.type != BT_CHARACTER)
964 code->expr2 = gfc_build_intrinsic_call (ns, GFC_ISYM_C_LOC, "C_LOC",
965 gfc_current_locus, 1, code->expr2);
966
967 /* Code: *free_buffer = old_buffer_ptr /= C_LOC (buffer); for rank != 0 or
968 * *free_buffer = 0; for rank == 0. */
969 code->next = gfc_get_code (EXEC_ASSIGN);
970 code = code->next;
971 code->loc = expr->where;
972 code->expr1 = gfc_lval_expr_from_sym (free_buffer);
973 if (expr->rank != 0)
974 {
975 code->expr2 = gfc_get_operator_expr (
976 &gfc_current_locus, INTRINSIC_NE_OS,
977 gfc_lval_expr_from_sym (old_buffer_data),
978 gfc_build_intrinsic_call (ns, GFC_ISYM_C_LOC, "C_LOC",
979 gfc_current_locus, 1,
980 gfc_lval_expr_from_sym (buffer)));
981 code->expr2->ts.type = BT_LOGICAL;
982 code->expr2->ts.kind = gfc_default_logical_kind;
983 }
984 else
985 {
986 code->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
987 &gfc_current_locus, false);
988 }
989
990 cb = gfc_lval_expr_from_sym (extproc);
991 cb->ts.interface = extproc;
992
993 if (caf_accessor_prepend)
994 {
995 gfc_code *c = caf_accessor_prepend;
996 /* Find last in chain. */
997 for (; c->next; c = c->next)
998 ;
999 c->next = sub_ns->code;
1000 sub_ns->code = caf_accessor_prepend;
1001 }
1002 caf_accessor_prepend = backup_caf_accessor_prepend;
1003 return cb;
1004 }
1005
1006 void
1007 add_caf_get_from_remote (gfc_expr *e)
1008 {
1009 gfc_expr *wrapper, *tmp_expr, *get_from_remote_expr,
1010 *get_from_remote_hash_expr;
1011 gfc_ref *ref;
1012 int n;
1013
1014 for (ref = e->ref; ref; ref = ref->next)
1015 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
1016 break;
1017 if (ref == NULL)
1018 return;
1019
1020 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
1021 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
1022 return;
1023
1024 tmp_expr = XCNEW (gfc_expr);
1025 *tmp_expr = *e;
1026 get_from_remote_expr = create_get_callback (tmp_expr);
1027 get_from_remote_hash_expr = gfc_get_expr ();
1028 get_from_remote_hash_expr->expr_type = EXPR_CONSTANT;
1029 get_from_remote_hash_expr->ts.type = BT_INTEGER;
1030 get_from_remote_hash_expr->ts.kind = gfc_default_integer_kind;
1031 get_from_remote_hash_expr->where = tmp_expr->where;
1032 mpz_init_set_ui (get_from_remote_hash_expr->value.integer,
1033 gfc_hash_value (get_from_remote_expr->symtree->n.sym));
1034 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
1035 "caf_get", tmp_expr->where, 3, tmp_expr,
1036 get_from_remote_hash_expr,
1037 get_from_remote_expr);
1038 gfc_add_caf_accessor (get_from_remote_hash_expr, get_from_remote_expr);
1039 wrapper->ts = e->ts;
1040 wrapper->rank = e->rank;
1041 wrapper->corank = e->corank;
1042 if (e->rank)
1043 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
1044 *e = *wrapper;
1045 free (wrapper);
1046 }
1047
1048 static gfc_expr *
1049 create_allocated_callback (gfc_expr *expr)
1050 {
1051 gfc_namespace *ns;
1052 gfc_symbol *extproc, *proc, *result, *base, *add_data, *caller_image;
1053 char tname[GFC_MAX_SYMBOL_LEN + 1];
1054 char *name;
1055 const char *mname;
1056 gfc_expr *cb, *post_caf_ref_expr;
1057 gfc_code *code;
1058 gfc_code *backup_caf_accessor_prepend = caf_accessor_prepend;
1059 caf_accessor_prepend = nullptr;
1060 gfc_expr swp;
1061
1062 /* Find the top-level namespace. */
1063 for (ns = gfc_current_ns; ns->parent; ns = ns->parent)
1064 ;
1065
1066 if (expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
1067 strcpy (tname, expr->value.function.actual->expr->symtree->name);
1068 else
1069 strcpy (tname, "dummy");
1070 if (expr->value.function.actual->expr->symtree->n.sym->module)
1071 mname = expr->value.function.actual->expr->symtree->n.sym->module;
1072 else
1073 mname = "main";
1074 name = xasprintf ("_caf_present_%s_%s_%d", mname, tname, ++caf_sym_cnt);
1075 gfc_get_symbol (name, ns, &extproc);
1076 extproc->declared_at = expr->where;
1077 gfc_set_sym_referenced (extproc);
1078 ++extproc->refs;
1079 gfc_commit_symbol (extproc);
1080
1081 /* Set up namespace. */
1082 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
1083 sub_ns->sibling = ns->contained;
1084 ns->contained = sub_ns;
1085 sub_ns->resolved = 1;
1086 /* Set up procedure symbol. */
1087 gfc_find_symbol (name, sub_ns, 1, &proc);
1088 sub_ns->proc_name = proc;
1089 proc->attr.if_source = IFSRC_DECL;
1090 proc->attr.access = ACCESS_PUBLIC;
1091 gfc_add_subroutine (&proc->attr, name, NULL);
1092 proc->attr.host_assoc = 1;
1093 proc->attr.always_explicit = 1;
1094 proc->declared_at = expr->where;
1095 ++proc->refs;
1096 gfc_commit_symbol (proc);
1097 free (name);
1098
1099 split_expr_at_caf_ref (expr->value.function.actual->expr, sub_ns,
1100 &post_caf_ref_expr, true);
1101
1102 if (ns->proc_name->attr.flavor == FL_MODULE)
1103 proc->module = ns->proc_name->name;
1104 gfc_set_sym_referenced (proc);
1105 /* Set up formal arguments. */
1106 gfc_formal_arglist **argptr = &proc->formal;
1107 #define ADD_ARG(name, nsym, stype, skind, sintent) \
1108 gfc_get_symbol (name, sub_ns, &nsym); \
1109 nsym->ts.type = stype; \
1110 nsym->ts.kind = skind; \
1111 nsym->attr.flavor = FL_PARAMETER; \
1112 nsym->attr.dummy = 1; \
1113 nsym->attr.intent = sintent; \
1114 nsym->declared_at = expr->where; \
1115 gfc_set_sym_referenced (nsym); \
1116 *argptr = gfc_get_formal_arglist (); \
1117 (*argptr)->sym = nsym; \
1118 argptr = &(*argptr)->next
1119
1120 name = xasprintf ("__add_data_%s_%s_%d", mname, tname, ++caf_sym_cnt);
1121 ADD_ARG (name, add_data, BT_DERIVED, 0, INTENT_IN);
1122 gfc_commit_symbol (add_data);
1123 free (name);
1124 ADD_ARG ("__caller_image", caller_image, BT_INTEGER, gfc_default_integer_kind,
1125 INTENT_IN);
1126 gfc_commit_symbol (caller_image);
1127
1128 ADD_ARG ("__result", result, BT_LOGICAL, gfc_default_logical_kind,
1129 INTENT_OUT);
1130 gfc_commit_symbol (result);
1131
1132 // ADD_ARG (expr->symtree->name, base, BT_VOID, INTENT_IN);
1133 base = post_caf_ref_expr->symtree->n.sym;
1134 gfc_set_sym_referenced (base);
1135 gfc_commit_symbol (base);
1136 *argptr = gfc_get_formal_arglist ();
1137 (*argptr)->sym = base;
1138 argptr = &(*argptr)->next;
1139 gfc_commit_symbol (base);
1140 #undef ADD_ARG
1141
1142 /* Set up code. */
1143 /* Code: result = post_caf_ref_expr; */
1144 code = sub_ns->code = gfc_get_code (EXEC_ASSIGN);
1145 code->loc = expr->where;
1146 code->expr1 = gfc_lval_expr_from_sym (result);
1147 swp = *expr;
1148 *expr = *swp.value.function.actual->expr;
1149 swp.value.function.actual->expr = nullptr;
1150 code->expr2 = gfc_copy_expr (&swp);
1151 code->expr2->value.function.actual->expr = post_caf_ref_expr;
1152
1153 remove_caf_ref (code->expr2->value.function.actual->expr, true);
1154 add_data->ts.u.derived
1155 = create_caf_add_data_parameter_type (post_caf_ref_expr, ns, add_data);
1156
1157 cb = gfc_lval_expr_from_sym (extproc);
1158 cb->ts.interface = extproc;
1159
1160 if (caf_accessor_prepend)
1161 {
1162 gfc_code *c = caf_accessor_prepend;
1163 /* Find last in chain. */
1164 for (; c->next; c = c->next)
1165 ;
1166 c->next = sub_ns->code;
1167 sub_ns->code = caf_accessor_prepend;
1168 }
1169 caf_accessor_prepend = backup_caf_accessor_prepend;
1170 return cb;
1171 }
1172
1173 static void
1174 rewrite_caf_allocated (gfc_expr **e)
1175 {
1176 gfc_expr *present_fn_expr, *present_hash_expr, *wrapper;
1177
1178 present_fn_expr = create_allocated_callback (*e);
1179
1180 present_hash_expr = gfc_get_expr ();
1181 present_hash_expr->expr_type = EXPR_CONSTANT;
1182 present_hash_expr->ts.type = BT_INTEGER;
1183 present_hash_expr->ts.kind = gfc_default_integer_kind;
1184 present_hash_expr->where = (*e)->where;
1185 mpz_init_set_ui (present_hash_expr->value.integer,
1186 gfc_hash_value (present_fn_expr->symtree->n.sym));
1187 wrapper
1188 = gfc_build_intrinsic_call (gfc_current_ns,
1189 GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE,
1190 "caf_is_present_on_remote", (*e)->where, 3, *e,
1191 present_hash_expr, present_fn_expr);
1192 gfc_add_caf_accessor (present_hash_expr, present_fn_expr);
1193 *e = wrapper;
1194 }
1195
1196 static gfc_expr *
1197 create_send_callback (gfc_expr *expr, gfc_expr *rhs)
1198 {
1199 gfc_namespace *ns;
1200 gfc_symbol *extproc, *proc, *buffer, *base, *send_data, *caller_image;
1201 char tname[GFC_MAX_SYMBOL_LEN + 1];
1202 char *name;
1203 const char *mname;
1204 gfc_expr *cb, *post_caf_ref_expr;
1205 gfc_code *code;
1206 gfc_code *backup_caf_accessor_prepend = caf_accessor_prepend;
1207 caf_accessor_prepend = nullptr;
1208
1209 /* Find the top-level namespace. */
1210 for (ns = gfc_current_ns; ns->parent; ns = ns->parent)
1211 ;
1212
1213 if (expr->expr_type == EXPR_VARIABLE)
1214 strcpy (tname, expr->symtree->name);
1215 else
1216 strcpy (tname, "dummy");
1217 if (expr->symtree->n.sym->module)
1218 mname = expr->symtree->n.sym->module;
1219 else
1220 mname = "main";
1221 name = xasprintf ("_caf_accessor_%s_%s_%d", mname, tname, ++caf_sym_cnt);
1222 gfc_get_symbol (name, ns, &extproc);
1223 extproc->declared_at = expr->where;
1224 gfc_set_sym_referenced (extproc);
1225 ++extproc->refs;
1226 gfc_commit_symbol (extproc);
1227
1228 /* Set up namespace. */
1229 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
1230 sub_ns->sibling = ns->contained;
1231 ns->contained = sub_ns;
1232 sub_ns->resolved = 1;
1233 /* Set up procedure symbol. */
1234 gfc_find_symbol (name, sub_ns, 1, &proc);
1235 sub_ns->proc_name = proc;
1236 proc->attr.if_source = IFSRC_DECL;
1237 proc->attr.access = ACCESS_PUBLIC;
1238 gfc_add_subroutine (&proc->attr, name, NULL);
1239 proc->attr.host_assoc = 1;
1240 proc->attr.always_explicit = 1;
1241 ++proc->refs;
1242 proc->declared_at = expr->where;
1243 gfc_commit_symbol (proc);
1244 free (name);
1245
1246 split_expr_at_caf_ref (expr, sub_ns, &post_caf_ref_expr, true);
1247
1248 if (ns->proc_name->attr.flavor == FL_MODULE)
1249 proc->module = ns->proc_name->name;
1250 gfc_set_sym_referenced (proc);
1251 /* Set up formal arguments. */
1252 gfc_formal_arglist **argptr = &proc->formal;
1253 #define ADD_ARG(name, nsym, stype, skind, sintent) \
1254 gfc_get_symbol (name, sub_ns, &nsym); \
1255 nsym->ts.type = stype; \
1256 nsym->ts.kind = skind; \
1257 nsym->attr.flavor = FL_PARAMETER; \
1258 nsym->attr.dummy = 1; \
1259 nsym->attr.intent = sintent; \
1260 nsym->declared_at = expr->where; \
1261 gfc_set_sym_referenced (nsym); \
1262 *argptr = gfc_get_formal_arglist (); \
1263 (*argptr)->sym = nsym; \
1264 argptr = &(*argptr)->next
1265
1266 name = xasprintf ("__add_send_data_%s_%s_%d", mname, tname, caf_sym_cnt);
1267 ADD_ARG (name, send_data, BT_DERIVED, 0, INTENT_IN);
1268 gfc_commit_symbol (send_data);
1269 free (name);
1270
1271 ADD_ARG ("__caller_image", caller_image, BT_INTEGER, gfc_default_integer_kind,
1272 INTENT_IN);
1273 gfc_commit_symbol (caller_image);
1274
1275 // ADD_ARG (expr->symtree->name, base, BT_VOID, INTENT_IN);
1276 base = post_caf_ref_expr->symtree->n.sym;
1277 base->attr.intent = INTENT_INOUT;
1278 gfc_set_sym_referenced (base);
1279 gfc_commit_symbol (base);
1280 *argptr = gfc_get_formal_arglist ();
1281 (*argptr)->sym = base;
1282 argptr = &(*argptr)->next;
1283 gfc_commit_symbol (base);
1284
1285 ADD_ARG ("__buffer", buffer, rhs->ts.type, rhs->ts.kind, INTENT_IN);
1286 buffer->ts = rhs->ts;
1287 if (rhs->rank)
1288 {
1289 buffer->as = gfc_get_array_spec ();
1290 buffer->as->rank = rhs->rank;
1291 buffer->as->type = AS_DEFERRED;
1292 buffer->attr.allocatable = 1;
1293 buffer->attr.dimension = 1;
1294 }
1295 if (buffer->ts.type == BT_CHARACTER)
1296 {
1297 buffer->ts.u.cl = gfc_get_charlen ();
1298 *buffer->ts.u.cl = *rhs->ts.u.cl;
1299 buffer->ts.deferred = 1;
1300 buffer->ts.u.cl->length = gfc_copy_expr (rhs->ts.u.cl->length);
1301 }
1302 gfc_commit_symbol (buffer);
1303 #undef ADD_ARG
1304
1305 /* Set up code. */
1306 /* Code: base = buffer; */
1307 code = sub_ns->code = gfc_get_code (EXEC_ASSIGN);
1308 code->loc = expr->where;
1309 code->expr1 = post_caf_ref_expr;
1310 if (code->expr1->ts.type == BT_CHARACTER
1311 && code->expr1->ts.kind != buffer->ts.kind)
1312 {
1313 bool converted;
1314 code->expr2 = gfc_lval_expr_from_sym (buffer);
1315 converted = gfc_convert_chartype (code->expr2, &code->expr1->ts);
1316 gcc_assert (converted);
1317 }
1318 else if (code->expr1->ts.type != buffer->ts.type)
1319 {
1320 bool converted;
1321 code->expr2 = gfc_lval_expr_from_sym (buffer);
1322 converted = gfc_convert_type_warn (code->expr2, &code->expr1->ts, 0, 0,
1323 buffer->attr.dimension);
1324 gcc_assert (converted);
1325 }
1326 else
1327 code->expr2 = gfc_lval_expr_from_sym (buffer);
1328 remove_caf_ref (post_caf_ref_expr);
1329 send_data->ts.u.derived
1330 = create_caf_add_data_parameter_type (code->expr1, ns, send_data);
1331
1332 cb = gfc_lval_expr_from_sym (extproc);
1333 cb->ts.interface = extproc;
1334
1335 if (caf_accessor_prepend)
1336 {
1337 gfc_code *c = caf_accessor_prepend;
1338 /* Find last in chain. */
1339 for (; c->next; c = c->next)
1340 ;
1341 c->next = sub_ns->code;
1342 sub_ns->code = caf_accessor_prepend;
1343 }
1344 caf_accessor_prepend = backup_caf_accessor_prepend;
1345 return cb;
1346 }
1347
1348 static void
1349 rewrite_caf_send (gfc_code *c)
1350 {
1351 gfc_expr *send_to_remote_expr, *send_to_remote_hash_expr, *lhs, *rhs;
1352 gfc_actual_arglist *arg = c->ext.actual;
1353
1354 lhs = arg->expr;
1355 arg = arg->next;
1356 rhs = arg->expr;
1357 /* Detect an already rewritten caf_send. */
1358 if (arg->next && arg->next->expr->expr_type == EXPR_CONSTANT
1359 && arg->next->next && arg->next->next->expr->expr_type == EXPR_VARIABLE)
1360 return;
1361
1362 send_to_remote_expr = create_send_callback (lhs, rhs);
1363 send_to_remote_hash_expr = gfc_get_expr ();
1364 send_to_remote_hash_expr->expr_type = EXPR_CONSTANT;
1365 send_to_remote_hash_expr->ts.type = BT_INTEGER;
1366 send_to_remote_hash_expr->ts.kind = gfc_default_integer_kind;
1367 send_to_remote_hash_expr->where = lhs->where;
1368 mpz_init_set_ui (send_to_remote_hash_expr->value.integer,
1369 gfc_hash_value (send_to_remote_expr->symtree->n.sym));
1370 arg->next = gfc_get_actual_arglist ();
1371 arg = arg->next;
1372 arg->expr = send_to_remote_hash_expr;
1373 arg->next = gfc_get_actual_arglist ();
1374 arg = arg->next;
1375 arg->expr = send_to_remote_expr;
1376 gfc_add_caf_accessor (send_to_remote_hash_expr, send_to_remote_expr);
1377
1378 if (gfc_is_coindexed (rhs))
1379 {
1380 gfc_expr *get_from_remote_expr, *get_from_remote_hash_expr;
1381
1382 c->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SENDGET);
1383 get_from_remote_expr = create_get_callback (rhs);
1384 get_from_remote_hash_expr = gfc_get_expr ();
1385 get_from_remote_hash_expr->expr_type = EXPR_CONSTANT;
1386 get_from_remote_hash_expr->ts.type = BT_INTEGER;
1387 get_from_remote_hash_expr->ts.kind = gfc_default_integer_kind;
1388 get_from_remote_hash_expr->where = rhs->where;
1389 mpz_init_set_ui (get_from_remote_hash_expr->value.integer,
1390 gfc_hash_value (get_from_remote_expr->symtree->n.sym));
1391 arg->next = gfc_get_actual_arglist ();
1392 arg = arg->next;
1393 arg->expr = get_from_remote_hash_expr;
1394 arg->next = gfc_get_actual_arglist ();
1395 arg = arg->next;
1396 arg->expr = get_from_remote_expr;
1397 gfc_add_caf_accessor (get_from_remote_hash_expr, get_from_remote_expr);
1398 }
1399 }
1400
1401 static int
1402 coindexed_expr_callback (gfc_expr **e, int *walk_subtrees,
1403 void *data ATTRIBUTE_UNUSED)
1404 {
1405 *walk_subtrees = 1;
1406
1407 switch ((*e)->expr_type)
1408 {
1409 case EXPR_VARIABLE:
1410 if (!caf_on_lhs && gfc_is_coindexed (*e))
1411 {
1412 add_caf_get_from_remote (*e);
1413 *walk_subtrees = 0;
1414 }
1415 /* Clear the flag to rewrite caf_gets in sub expressions of the lhs. */
1416 caf_on_lhs = false;
1417 break;
1418 case EXPR_FUNCTION:
1419 if ((*e)->value.function.isym)
1420 switch ((*e)->value.function.isym->id)
1421 {
1422 case GFC_ISYM_ALLOCATED:
1423 if ((*e)->value.function.actual->expr
1424 && gfc_is_coindexed ((*e)->value.function.actual->expr))
1425 {
1426 rewrite_caf_allocated (e);
1427 *walk_subtrees = 0;
1428 }
1429 break;
1430 case GFC_ISYM_CAF_GET:
1431 case GFC_ISYM_CAF_IS_PRESENT_ON_REMOTE:
1432 *walk_subtrees = 0;
1433 break;
1434 default:
1435 break;
1436 }
1437 default:
1438 break;
1439 }
1440
1441 return 0;
1442 }
1443
1444 static int
1445 coindexed_code_callback (gfc_code **c, int *walk_subtrees,
1446 void *data ATTRIBUTE_UNUSED)
1447 {
1448 int ws = 1;
1449 current_code = c;
1450
1451 switch ((*c)->op)
1452 {
1453 case EXEC_ASSIGN:
1454 case EXEC_POINTER_ASSIGN:
1455 caf_on_lhs = true;
1456 coindexed_expr_callback (&((*c)->expr1), &ws, NULL);
1457 caf_on_lhs = false;
1458 ws = 1;
1459 coindexed_expr_callback (&((*c)->expr2), &ws, NULL);
1460 *walk_subtrees = ws;
1461 break;
1462 case EXEC_LOCK:
1463 case EXEC_UNLOCK:
1464 case EXEC_EVENT_POST:
1465 case EXEC_EVENT_WAIT:
1466 *walk_subtrees = 0;
1467 break;
1468 case EXEC_CALL:
1469 *walk_subtrees = 1;
1470 if ((*c)->resolved_isym)
1471 switch ((*c)->resolved_isym->id)
1472 {
1473 case GFC_ISYM_CAF_SEND:
1474 rewrite_caf_send (*c);
1475 *walk_subtrees = 0;
1476 break;
1477 case GFC_ISYM_CAF_SENDGET:
1478 /* Seldomly this routine is called again with the symbol already
1479 changed to CAF_SENDGET. Do not process the subtree again. The
1480 rewrite has already been done by rewrite_caf_send (). */
1481 *walk_subtrees = 0;
1482 break;
1483 case GFC_ISYM_ATOMIC_ADD:
1484 case GFC_ISYM_ATOMIC_AND:
1485 case GFC_ISYM_ATOMIC_CAS:
1486 case GFC_ISYM_ATOMIC_DEF:
1487 case GFC_ISYM_ATOMIC_FETCH_ADD:
1488 case GFC_ISYM_ATOMIC_FETCH_AND:
1489 case GFC_ISYM_ATOMIC_FETCH_OR:
1490 case GFC_ISYM_ATOMIC_FETCH_XOR:
1491 case GFC_ISYM_ATOMIC_OR:
1492 case GFC_ISYM_ATOMIC_REF:
1493 case GFC_ISYM_ATOMIC_XOR:
1494 *walk_subtrees = 0;
1495 break;
1496 default:
1497 break;
1498 }
1499 break;
1500 default:
1501 *walk_subtrees = 1;
1502 break;
1503 }
1504 return 0;
1505 }
1506
1507 void
1508 gfc_coarray_rewrite (gfc_namespace *ns)
1509 {
1510 gfc_namespace *saved_ns = gfc_current_ns;
1511 gfc_current_ns = ns;
1512
1513 if (flag_coarray == GFC_FCOARRAY_LIB)
1514 {
1515 gfc_code_walker (&ns->code, coindexed_code_callback,
1516 coindexed_expr_callback, NULL);
1517
1518 for (gfc_namespace *cns = ns->contained; cns; cns = cns->sibling)
1519 gfc_coarray_rewrite (cns);
1520 }
1521
1522 gfc_current_ns = saved_ns;
1523 }