Debian Codegraph @

gcc/fortran/class.cc

raw
1 /* Implementation of Fortran 2003 Polymorphism.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
3 Contributed by Paul Richard Thomas <[email protected]>
4 and Janus Weil <[email protected]>
5
6 This file is part of GCC.
7
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 3, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 /* class.cc -- This file contains the front end functions needed to service
24 the implementation of Fortran 2003 polymorphism and other
25 object-oriented features. */
26
27
28 /* Outline of the internal representation:
29
30 Each CLASS variable is encapsulated by a class container, which is a
31 structure with two fields:
32 * _data: A pointer to the actual data of the variable. This field has the
33 declared type of the class variable and its attributes
34 (pointer/allocatable/dimension/...).
35 * _vptr: A pointer to the vtable entry (see below) of the dynamic type.
36
37 Only for unlimited polymorphic classes:
38 * _len: An integer(C_SIZE_T) to store the string length when the unlimited
39 polymorphic pointer is used to point to a char array. The '_len'
40 component will be zero when no character array is stored in
41 '_data'.
42
43 For each derived type we set up a "vtable" entry, i.e. a structure with the
44 following fields:
45 * _hash: A hash value serving as a unique identifier for this type.
46 * _size: The size in bytes of the derived type.
47 * _extends: A pointer to the vtable entry of the parent derived type.
48 * _def_init: A pointer to a default initialized variable of this type.
49 * _copy: A procedure pointer to a copying procedure.
50 * _final: A procedure pointer to a wrapper function, which frees
51 allocatable components and calls FINAL subroutines.
52 * _deallocate: A procedure pointer to a deallocation procedure; nonnull
53 only for a recursive derived type.
54
55 After these follow procedure pointer components for the specific
56 type-bound procedures. */
57
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "gfortran.h"
63 #include "constructor.h"
64 #include "target-memory.h"
65
66 /* Inserts a derived type component reference in a data reference chain.
67 TS: base type of the ref chain so far, in which we will pick the component
68 REF: the address of the GFC_REF pointer to update
69 NAME: name of the component to insert
70 Note that component insertion makes sense only if we are at the end of
71 the chain (*REF == NULL) or if we are adding a missing "_data" component
72 to access the actual contents of a class object. */
73
74 static void
75 insert_component_ref (gfc_typespec *ts, gfc_ref **ref, const char * const name)
76 {
77 gfc_ref *new_ref;
78 int wcnt, ecnt;
79
80 gcc_assert (ts->type == BT_DERIVED || ts->type == BT_CLASS);
81
82 gfc_find_component (ts->u.derived, name, true, true, &new_ref);
83
84 gfc_get_errors (&wcnt, &ecnt);
85 if (ecnt > 0 && !new_ref)
86 return;
87 gcc_assert (new_ref->u.c.component);
88
89 while (new_ref->next)
90 new_ref = new_ref->next;
91 new_ref->next = *ref;
92
93 if (new_ref->next)
94 {
95 gfc_ref *next = NULL;
96
97 /* We need to update the base type in the trailing reference chain to
98 that of the new component. */
99
100 gcc_assert (strcmp (name, "_data") == 0);
101
102 if (new_ref->next->type == REF_COMPONENT)
103 next = new_ref->next;
104 else if (new_ref->next->type == REF_ARRAY
105 && new_ref->next->next
106 && new_ref->next->next->type == REF_COMPONENT)
107 next = new_ref->next->next;
108
109 if (next != NULL)
110 {
111 gcc_assert (new_ref->u.c.component->ts.type == BT_CLASS
112 || new_ref->u.c.component->ts.type == BT_DERIVED);
113 next->u.c.sym = new_ref->u.c.component->ts.u.derived;
114 }
115 }
116
117 *ref = new_ref;
118 }
119
120
121 /* Tells whether we need to add a "_data" reference to access REF subobject
122 from an object of type TS. If FIRST_REF_IN_CHAIN is set, then the base
123 object accessed by REF is a variable; in other words it is a full object,
124 not a subobject. */
125
126 static bool
127 class_data_ref_missing (gfc_typespec *ts, gfc_ref *ref, bool first_ref_in_chain)
128 {
129 /* Only class containers may need the "_data" reference. */
130 if (ts->type != BT_CLASS)
131 return false;
132
133 /* Accessing a class container with an array reference is certainly wrong. */
134 if (ref->type != REF_COMPONENT)
135 return true;
136
137 /* Accessing the class container's fields is fine. */
138 if (ref->u.c.component->name[0] == '_')
139 return false;
140
141 /* At this point we have a class container with a non class container's field
142 component reference. We don't want to add the "_data" component if we are
143 at the first reference and the symbol's type is an extended derived type.
144 In that case, conv_parent_component_references will do the right thing so
145 it is not absolutely necessary. Omitting it prevents a regression (see
146 class_41.f03) in the interface mapping mechanism. When evaluating string
147 lengths depending on dummy arguments, we create a fake symbol with a type
148 equal to that of the dummy type. However, because of type extension,
149 the backend type (corresponding to the actual argument) can have a
150 different (extended) type. Adding the "_data" component explicitly, using
151 the base type, confuses the gfc_conv_component_ref code which deals with
152 the extended type. */
153 if (first_ref_in_chain && ts->u.derived->attr.extension)
154 return false;
155
156 /* We have a class container with a non class container's field component
157 reference that doesn't fall into the above. */
158 return true;
159 }
160
161
162 /* Browse through a data reference chain and add the missing "_data" references
163 when a subobject of a class object is accessed without it.
164 Note that it doesn't add the "_data" reference when the class container
165 is the last element in the reference chain. */
166
167 void
168 gfc_fix_class_refs (gfc_expr *e)
169 {
170 gfc_typespec *ts;
171 gfc_ref **ref;
172
173 if ((e->expr_type != EXPR_VARIABLE
174 && e->expr_type != EXPR_FUNCTION)
175 || (e->expr_type == EXPR_FUNCTION
176 && e->value.function.isym != NULL))
177 return;
178
179 if (e->expr_type == EXPR_VARIABLE)
180 ts = &e->symtree->n.sym->ts;
181 else
182 {
183 gfc_symbol *func;
184
185 gcc_assert (e->expr_type == EXPR_FUNCTION);
186 if (e->value.function.esym != NULL)
187 func = e->value.function.esym;
188 else
189 func = e->symtree->n.sym;
190
191 if (func->result != NULL)
192 ts = &func->result->ts;
193 else
194 ts = &func->ts;
195 }
196
197 for (ref = &e->ref; *ref != NULL; ref = &(*ref)->next)
198 {
199 if (class_data_ref_missing (ts, *ref, ref == &e->ref))
200 insert_component_ref (ts, ref, "_data");
201
202 if ((*ref)->type == REF_COMPONENT)
203 ts = &(*ref)->u.c.component->ts;
204 }
205 }
206
207
208 /* Insert a reference to the component of the given name.
209 Only to be used with CLASS containers and vtables. */
210
211 void
212 gfc_add_component_ref (gfc_expr *e, const char *name)
213 {
214 gfc_component *c;
215 gfc_ref **tail = &(e->ref);
216 gfc_ref *ref, *next = NULL;
217 gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
218 while (*tail != NULL)
219 {
220 if ((*tail)->type == REF_COMPONENT)
221 {
222 if (strcmp ((*tail)->u.c.component->name, "_data") == 0
223 && (*tail)->next
224 && (*tail)->next->type == REF_ARRAY
225 && (*tail)->next->next == NULL)
226 return;
227 derived = (*tail)->u.c.component->ts.u.derived;
228 }
229 if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
230 break;
231 tail = &((*tail)->next);
232 }
233 if (derived && derived->components && derived->components->next &&
234 derived->components->next->ts.type == BT_DERIVED &&
235 derived->components->next->ts.u.derived == NULL)
236 {
237 /* Fix up missing vtype. */
238 gfc_symbol *vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
239 gcc_assert (vtab);
240 derived->components->next->ts.u.derived = vtab->ts.u.derived;
241 }
242 if (*tail != NULL && strcmp (name, "_data") == 0)
243 next = *tail;
244 else
245 /* Avoid losing memory. */
246 gfc_free_ref_list (*tail);
247 c = gfc_find_component (derived, name, true, true, tail);
248
249 if (c) {
250 for (ref = *tail; ref->next; ref = ref->next)
251 ;
252 ref->next = next;
253 if (!next)
254 e->ts = c->ts;
255 }
256 }
257
258
259 /* This is used to add both the _data component reference and an array
260 reference to class expressions. Used in translation of intrinsic
261 array inquiry functions. */
262
263 void
264 gfc_add_class_array_ref (gfc_expr *e)
265 {
266 int rank = CLASS_DATA (e)->as->rank;
267 int corank = CLASS_DATA (e)->as->corank;
268 gfc_array_spec *as = CLASS_DATA (e)->as;
269 gfc_ref *ref = NULL;
270 gfc_add_data_component (e);
271 e->rank = rank;
272 e->corank = corank;
273 for (ref = e->ref; ref; ref = ref->next)
274 if (!ref->next)
275 break;
276 if (ref && ref->type != REF_ARRAY)
277 {
278 ref->next = gfc_get_ref ();
279 ref = ref->next;
280 ref->type = REF_ARRAY;
281 ref->u.ar.type = AR_FULL;
282 ref->u.ar.as = as;
283 }
284 }
285
286
287 /* Unfortunately, class array expressions can appear in various conditions;
288 with and without both _data component and an arrayspec. This function
289 deals with that variability. The previous reference to 'ref' is to a
290 class array. */
291
292 static bool
293 class_array_ref_detected (gfc_ref *ref, bool *full_array)
294 {
295 bool no_data = false;
296 bool with_data = false;
297
298 /* An array reference with no _data component. */
299 if (ref && ref->type == REF_ARRAY
300 && !ref->next
301 && ref->u.ar.type != AR_ELEMENT)
302 {
303 if (full_array)
304 *full_array = ref->u.ar.type == AR_FULL;
305 no_data = true;
306 }
307
308 /* Cover cases where _data appears, with or without an array ref. */
309 if (ref && ref->type == REF_COMPONENT
310 && strcmp (ref->u.c.component->name, "_data") == 0)
311 {
312 if (!ref->next)
313 {
314 with_data = true;
315 if (full_array)
316 *full_array = true;
317 }
318 else if (ref->next && ref->next->type == REF_ARRAY
319 && ref->type == REF_COMPONENT
320 && ref->next->u.ar.type != AR_ELEMENT)
321 {
322 with_data = true;
323 if (full_array)
324 *full_array = ref->next->u.ar.type == AR_FULL;
325 }
326 }
327
328 return no_data || with_data;
329 }
330
331
332 /* Returns true if the expression contains a reference to a class
333 array. Notice that class array elements return false. */
334
335 bool
336 gfc_is_class_array_ref (gfc_expr *e, bool *full_array)
337 {
338 gfc_ref *ref;
339
340 if (!e->rank)
341 return false;
342
343 if (full_array)
344 *full_array= false;
345
346 /* Is this a class array object? ie. Is the symbol of type class? */
347 if (e->symtree
348 && e->symtree->n.sym->ts.type == BT_CLASS
349 && CLASS_DATA (e->symtree->n.sym)
350 && CLASS_DATA (e->symtree->n.sym)->attr.dimension
351 && class_array_ref_detected (e->ref, full_array))
352 return true;
353
354 /* Or is this a class array component reference? */
355 for (ref = e->ref; ref; ref = ref->next)
356 {
357 if (ref->type == REF_COMPONENT
358 && ref->u.c.component->ts.type == BT_CLASS
359 && CLASS_DATA (ref->u.c.component)->attr.dimension
360 && class_array_ref_detected (ref->next, full_array))
361 return true;
362 }
363
364 return false;
365 }
366
367
368 /* Returns true if the expression is a reference to a class
369 scalar. This function is necessary because such expressions
370 can be dressed with a reference to the _data component and so
371 have a type other than BT_CLASS. */
372
373 bool
374 gfc_is_class_scalar_expr (gfc_expr *e)
375 {
376 gfc_ref *ref;
377
378 if (e->rank)
379 return false;
380
381 /* Is this a class object? */
382 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS
383 && CLASS_DATA (e->symtree->n.sym)
384 && !CLASS_DATA (e->symtree->n.sym)->attr.dimension
385 && (e->ref == NULL
386 || (e->ref->type == REF_COMPONENT
387 && strcmp (e->ref->u.c.component->name, "_data") == 0
388 && (e->ref->next == NULL
389 || (e->ref->next->type == REF_ARRAY
390 && e->ref->next->u.ar.codimen > 0
391 && e->ref->next->u.ar.dimen == 0
392 && e->ref->next->next == NULL)))))
393 return true;
394
395 /* Or is the final reference BT_CLASS or _data? */
396 for (ref = e->ref; ref; ref = ref->next)
397 {
398 if (ref->type == REF_COMPONENT && ref->u.c.component->ts.type == BT_CLASS
399 && CLASS_DATA (ref->u.c.component)
400 && !CLASS_DATA (ref->u.c.component)->attr.dimension
401 && (ref->next == NULL
402 || (ref->next->type == REF_COMPONENT
403 && strcmp (ref->next->u.c.component->name, "_data") == 0
404 && (ref->next->next == NULL
405 || (ref->next->next->type == REF_ARRAY
406 && ref->next->next->u.ar.codimen > 0
407 && ref->next->next->u.ar.dimen == 0
408 && ref->next->next->next == NULL)))))
409 return true;
410 }
411
412 return false;
413 }
414
415
416 /* Tells whether the expression E is a reference to a (scalar) class container.
417 Scalar because array class containers usually have an array reference after
418 them, and gfc_fix_class_refs will add the missing "_data" component reference
419 in that case. */
420
421 bool
422 gfc_is_class_container_ref (gfc_expr *e)
423 {
424 gfc_ref *ref;
425 bool result;
426
427 if (e->expr_type != EXPR_VARIABLE)
428 return e->ts.type == BT_CLASS;
429
430 if (e->symtree->n.sym->ts.type == BT_CLASS)
431 result = true;
432 else
433 result = false;
434
435 for (ref = e->ref; ref; ref = ref->next)
436 {
437 if (ref->type != REF_COMPONENT)
438 result = false;
439 else if (ref->u.c.component->ts.type == BT_CLASS)
440 result = true;
441 else
442 result = false;
443 }
444
445 return result;
446 }
447
448
449 /* Build an initializer for CLASS pointers,
450 initializing the _data component to the init_expr (or NULL) and the _vptr
451 component to the corresponding type (or the declared type, given by ts). */
452
453 gfc_expr *
454 gfc_class_initializer (gfc_typespec *ts, gfc_expr *init_expr)
455 {
456 gfc_expr *init;
457 gfc_component *comp;
458 gfc_symbol *vtab = NULL;
459
460 if (init_expr && init_expr->expr_type != EXPR_NULL)
461 vtab = gfc_find_vtab (&init_expr->ts);
462 else
463 vtab = gfc_find_vtab (ts);
464
465 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
466 &ts->u.derived->declared_at);
467 init->ts = *ts;
468
469 for (comp = ts->u.derived->components; comp; comp = comp->next)
470 {
471 gfc_constructor *ctor = gfc_constructor_get();
472 if (strcmp (comp->name, "_vptr") == 0 && vtab)
473 ctor->expr = gfc_lval_expr_from_sym (vtab);
474 else if (init_expr && init_expr->expr_type != EXPR_NULL)
475 ctor->expr = gfc_copy_expr (init_expr);
476 else
477 ctor->expr = gfc_get_null_expr (NULL);
478 gfc_constructor_append (&init->value.constructor, ctor);
479 }
480
481 return init;
482 }
483
484
485 /* Create a unique string identifier for a derived type, composed of its name
486 and module name. This is used to construct unique names for the class
487 containers and vtab symbols. */
488
489 static char *
490 get_unique_type_string (gfc_symbol *derived)
491 {
492 const char *dt_name;
493 char *string;
494 size_t len;
495 if (derived->attr.unlimited_polymorphic)
496 dt_name = "STAR";
497 else
498 dt_name = gfc_dt_upper_string (derived->name);
499 len = strlen (dt_name) + 2;
500 if (derived->attr.unlimited_polymorphic)
501 {
502 string = XNEWVEC (char, len);
503 sprintf (string, "_%s", dt_name);
504 }
505 else if (derived->module)
506 {
507 string = XNEWVEC (char, strlen (derived->module) + len);
508 sprintf (string, "%s_%s", derived->module, dt_name);
509 }
510 else if (derived->ns->proc_name)
511 {
512 string = XNEWVEC (char, strlen (derived->ns->proc_name->name) + len);
513 sprintf (string, "%s_%s", derived->ns->proc_name->name, dt_name);
514 }
515 else
516 {
517 string = XNEWVEC (char, len);
518 sprintf (string, "_%s", dt_name);
519 }
520 return string;
521 }
522
523
524 /* A relative of 'get_unique_type_string' which makes sure the generated
525 string will not be too long (replacing it by a hash string if needed). */
526
527 static void
528 get_unique_hashed_string (char *string, gfc_symbol *derived)
529 {
530 /* Provide sufficient space to hold "symbol.symbol_symbol". */
531 char *tmp;
532 tmp = get_unique_type_string (derived);
533 /* If string is too long, use hash value in hex representation (allow for
534 extra decoration, cf. gfc_build_class_symbol & gfc_find_derived_vtab).
535 We need space to for 15 characters "__class_" + symbol name + "_%d_%da",
536 where %d is the (co)rank which can be up to n = 15. */
537 if (strlen (tmp) > GFC_MAX_SYMBOL_LEN - 15)
538 {
539 int h = gfc_hash_value (derived);
540 sprintf (string, "%X", h);
541 }
542 else
543 strcpy (string, tmp);
544 free (tmp);
545 }
546
547
548 /* Assign a hash value for a derived type. The algorithm is that of SDBM. */
549
550 unsigned int
551 gfc_hash_value (gfc_symbol *sym)
552 {
553 unsigned int hash = 0;
554 /* Provide sufficient space to hold "symbol.symbol_symbol". */
555 char *c;
556 int i, len;
557
558 c = get_unique_type_string (sym);
559 len = strlen (c);
560
561 for (i = 0; i < len; i++)
562 hash = (hash << 6) + (hash << 16) - hash + c[i];
563
564 free (c);
565 /* Return the hash but take the modulus for the sake of module read,
566 even though this slightly increases the chance of collision. */
567 return (hash % 100000000);
568 }
569
570
571 /* Assign a hash value for an intrinsic type. The algorithm is that of SDBM. */
572
573 unsigned int
574 gfc_intrinsic_hash_value (gfc_typespec *ts)
575 {
576 unsigned int hash = 0;
577 const char *c = gfc_typename (ts, true);
578 int i, len;
579
580 len = strlen (c);
581
582 for (i = 0; i < len; i++)
583 hash = (hash << 6) + (hash << 16) - hash + c[i];
584
585 /* Return the hash but take the modulus for the sake of module read,
586 even though this slightly increases the chance of collision. */
587 return (hash % 100000000);
588 }
589
590
591 /* Get the _len component from a class/derived object storing a string.
592 For unlimited polymorphic entities a ref to the _data component is available
593 while a ref to the _len component is needed. This routine traverese the
594 ref-chain and strips the last ref to a _data from it replacing it with a
595 ref to the _len component. */
596
597 gfc_expr *
598 gfc_get_len_component (gfc_expr *e, int k)
599 {
600 gfc_expr *ptr;
601 gfc_ref *ref, **last;
602
603 ptr = gfc_copy_expr (e);
604
605 /* We need to remove the last _data component ref from ptr. */
606 last = &(ptr->ref);
607 ref = ptr->ref;
608 while (ref)
609 {
610 if (!ref->next
611 && ref->type == REF_COMPONENT
612 && strcmp ("_data", ref->u.c.component->name)== 0)
613 {
614 gfc_free_ref_list (ref);
615 *last = NULL;
616 break;
617 }
618 last = &(ref->next);
619 ref = ref->next;
620 }
621 /* And replace if with a ref to the _len component. */
622 gfc_add_len_component (ptr);
623 if (k != ptr->ts.kind)
624 {
625 gfc_typespec ts;
626 gfc_clear_ts (&ts);
627 ts.type = BT_INTEGER;
628 ts.kind = k;
629 gfc_convert_type_warn (ptr, &ts, 2, 0);
630 }
631 return ptr;
632 }
633
634
635 /* Build a polymorphic CLASS entity, using the symbol that comes from
636 build_sym. A CLASS entity is represented by an encapsulating type,
637 which contains the declared type as '_data' component, plus a pointer
638 component '_vptr' which determines the dynamic type. When this CLASS
639 entity is unlimited polymorphic, then also add a component '_len' to
640 store the length of string when that is stored in it. */
641 static int ctr = 0;
642
643 bool
644 gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
645 gfc_array_spec **as)
646 {
647 char tname[GFC_MAX_SYMBOL_LEN+1];
648 char *name;
649 gfc_typespec *orig_ts = ts;
650 gfc_symbol *fclass;
651 gfc_symbol *vtab;
652 gfc_component *c;
653 gfc_namespace *ns;
654 int rank;
655
656 gcc_assert (as);
657
658 /* We cannot build the class container now. */
659 if (attr->class_ok && (!ts->u.derived || !ts->u.derived->components))
660 return false;
661
662 /* Class container has already been built with same name. */
663 if (attr->class_ok
664 && ts->u.derived->components->attr.dimension >= attr->dimension
665 && ts->u.derived->components->attr.codimension >= attr->codimension
666 && ts->u.derived->components->attr.class_pointer >= attr->pointer
667 && ts->u.derived->components->attr.allocatable >= attr->allocatable)
668 return true;
669 if (attr->class_ok)
670 {
671 attr->dimension |= ts->u.derived->components->attr.dimension;
672 attr->codimension |= ts->u.derived->components->attr.codimension;
673 attr->pointer |= ts->u.derived->components->attr.class_pointer;
674 attr->allocatable |= ts->u.derived->components->attr.allocatable;
675 ts = &ts->u.derived->components->ts;
676 }
677
678 attr->class_ok = attr->dummy || attr->pointer || attr->allocatable
679 || attr->select_type_temporary || attr->associate_var;
680
681 if (!attr->class_ok)
682 /* We cannot build the class container yet. */
683 return true;
684
685 /* Determine the name of the encapsulating type. */
686 rank = !(*as) || (*as)->rank == -1 ? GFC_MAX_DIMENSIONS : (*as)->rank;
687
688 if (!ts->u.derived)
689 return false;
690
691 get_unique_hashed_string (tname, ts->u.derived);
692 if ((*as) && attr->allocatable)
693 name = xasprintf ("__class_%s_%d_%da", tname, rank, (*as)->corank);
694 else if ((*as) && attr->pointer)
695 name = xasprintf ("__class_%s_%d_%dp", tname, rank, (*as)->corank);
696 else if ((*as))
697 name = xasprintf ("__class_%s_%d_%dt", tname, rank, (*as)->corank);
698 else if (attr->pointer)
699 name = xasprintf ("__class_%s_p", tname);
700 else if (attr->allocatable)
701 name = xasprintf ("__class_%s_a", tname);
702 else
703 name = xasprintf ("__class_%s_t", tname);
704
705 if (ts->u.derived->attr.unlimited_polymorphic)
706 {
707 /* Find the top-level namespace. */
708 for (ns = gfc_current_ns; ns; ns = ns->parent)
709 if (!ns->parent)
710 break;
711 }
712 else
713 ns = ts->u.derived->ns;
714
715 /* Although this might seem to be counterintuitive, we can build separate
716 class types with different array specs because the TKR interface checks
717 work on the declared type. All array type other than deferred shape or
718 assumed rank are added to the function namespace to ensure that they
719 are properly distinguished. */
720 if (attr->dummy && (*as)
721 && ((!attr->codimension
722 && !((*as)->type == AS_DEFERRED || (*as)->type == AS_ASSUMED_RANK))
723 || (attr->codimension
724 && !((*as)->cotype == AS_DEFERRED
725 || (*as)->cotype == AS_ASSUMED_RANK))))
726 {
727 char *sname;
728 ns = gfc_current_ns;
729 gfc_find_symbol (name, ns, 0, &fclass);
730 /* If a local class type with this name already exists, update the
731 name with an index. */
732 if (fclass)
733 {
734 fclass = NULL;
735 sname = xasprintf ("%s_%d", name, ++ctr);
736 free (name);
737 name = sname;
738 }
739 }
740 else
741 gfc_find_symbol (name, ns, 0, &fclass);
742
743 if (fclass == NULL)
744 {
745 gfc_symtree *st;
746 /* If not there, create a new symbol. */
747 fclass = gfc_new_symbol (name, ns);
748 st = gfc_new_symtree (&ns->sym_root, name);
749 st->n.sym = fclass;
750 gfc_set_sym_referenced (fclass);
751 fclass->refs++;
752 fclass->ts.type = BT_UNKNOWN;
753 if (!ts->u.derived->attr.unlimited_polymorphic)
754 fclass->attr.abstract = ts->u.derived->attr.abstract;
755 fclass->f2k_derived = gfc_get_namespace (NULL, 0);
756 if (!gfc_add_flavor (&fclass->attr, FL_DERIVED, NULL,
757 &gfc_current_locus))
758 return false;
759
760 /* Add component '_data'. */
761 if (!gfc_add_component (fclass, "_data", &c))
762 return false;
763 c->ts = *ts;
764 c->ts.type = BT_DERIVED;
765 c->attr.access = ACCESS_PRIVATE;
766 c->ts.u.derived = ts->u.derived;
767 c->attr.class_pointer = attr->pointer;
768 c->attr.pointer = attr->pointer || (attr->dummy && !attr->allocatable)
769 || attr->select_type_temporary;
770 c->attr.allocatable = attr->allocatable;
771 c->attr.dimension = attr->dimension;
772 c->attr.codimension = attr->codimension;
773 c->attr.abstract = fclass->attr.abstract;
774 c->as = (*as);
775 c->initializer = NULL;
776
777 /* Add component '_vptr'. */
778 if (!gfc_add_component (fclass, "_vptr", &c))
779 return false;
780 c->ts.type = BT_DERIVED;
781 c->attr.access = ACCESS_PRIVATE;
782 c->attr.pointer = 1;
783
784 if (ts->u.derived->attr.unlimited_polymorphic)
785 {
786 vtab = gfc_find_derived_vtab (ts->u.derived);
787 gcc_assert (vtab);
788 c->ts.u.derived = vtab->ts.u.derived;
789
790 /* Add component '_len'. Only unlimited polymorphic pointers may
791 have a string assigned to them, i.e., only those need the _len
792 component. */
793 if (!gfc_add_component (fclass, "_len", &c))
794 return false;
795 c->ts.type = BT_INTEGER;
796 c->ts.kind = gfc_charlen_int_kind;
797 c->attr.access = ACCESS_PRIVATE;
798 c->attr.artificial = 1;
799 }
800 else
801 /* Build vtab later. */
802 c->ts.u.derived = NULL;
803 }
804
805 if (!ts->u.derived->attr.unlimited_polymorphic)
806 {
807 /* Since the extension field is 8 bit wide, we can only have
808 up to 255 extension levels. */
809 if (ts->u.derived->attr.extension == 255)
810 {
811 gfc_error ("Maximum extension level reached with type %qs at %L",
812 ts->u.derived->name, &ts->u.derived->declared_at);
813 return false;
814 }
815
816 fclass->attr.extension = ts->u.derived->attr.extension + 1;
817 fclass->attr.alloc_comp = ts->u.derived->attr.alloc_comp;
818 fclass->attr.coarray_comp = ts->u.derived->attr.coarray_comp;
819 }
820
821 fclass->attr.is_class = 1;
822 orig_ts->u.derived = fclass;
823 attr->allocatable = attr->pointer = attr->dimension = attr->codimension = 0;
824 (*as) = NULL;
825 free (name);
826 return true;
827 }
828
829
830 /* Change class, using gfc_build_class_symbol. This is needed for associate
831 names, when rank changes or a derived type is produced by resolution. */
832
833 void
834 gfc_change_class (gfc_typespec *ts, symbol_attribute *sym_attr,
835 gfc_array_spec *sym_as, int rank, int corank)
836 {
837 symbol_attribute attr;
838 gfc_component *c;
839 gfc_array_spec *as = NULL;
840 gfc_symbol *der = ts->u.derived;
841
842 ts->type = BT_CLASS;
843 attr = *sym_attr;
844 attr.class_ok = 0;
845 attr.associate_var = 1;
846 attr.class_pointer = 1;
847 attr.allocatable = 0;
848 attr.pointer = 1;
849 attr.dimension = rank ? 1 : 0;
850 if (rank)
851 {
852 if (sym_as)
853 as = gfc_copy_array_spec (sym_as);
854 else
855 {
856 as = gfc_get_array_spec ();
857 as->rank = rank;
858 as->type = AS_DEFERRED;
859 as->corank = corank;
860 }
861 }
862 if (as && as->corank != 0)
863 attr.codimension = 1;
864
865 if (!gfc_build_class_symbol (ts, &attr, &as))
866 gcc_unreachable ();
867
868 gfc_set_sym_referenced (ts->u.derived);
869
870 /* Make sure the _vptr is set. */
871 c = gfc_find_component (ts->u.derived, "_vptr", true, true, NULL);
872 if (c->ts.u.derived == NULL)
873 c->ts.u.derived = gfc_find_derived_vtab (der);
874 /* _vptr now has the _vtab in it, change it to the _vtype. */
875 if (c->ts.u.derived->attr.vtab)
876 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
877 }
878
879
880 /* Add a procedure pointer component to the vtype
881 to represent a specific type-bound procedure. */
882
883 static void
884 add_proc_comp (gfc_symbol *vtype, const char *name, gfc_typebound_proc *tb)
885 {
886 gfc_component *c;
887 bool is_abstract = false;
888
889 c = gfc_find_component (vtype, name, true, true, NULL);
890
891 /* If the present component typebound proc is abstract, the new version
892 should unconditionally be tested if it is a suitable replacement. */
893 if (c && c->tb && c->tb->u.specific
894 && c->tb->u.specific->n.sym->attr.abstract)
895 is_abstract = true;
896
897 /* Pass on the new tb being not overridable if a component is found and
898 either there is not an overridden specific or the present component
899 tb is abstract. This ensures that possible, viable replacements are
900 loaded. */
901 if (tb->non_overridable && !tb->overridden && !is_abstract && c)
902 return;
903
904 if (c == NULL)
905 {
906 /* Add procedure component. */
907 if (!gfc_add_component (vtype, name, &c))
908 return;
909
910 if (!c->tb)
911 c->tb = XCNEW (gfc_typebound_proc);
912 *c->tb = *tb;
913 c->tb->ppc = 1;
914 c->attr.procedure = 1;
915 c->attr.proc_pointer = 1;
916 c->attr.flavor = FL_PROCEDURE;
917 c->attr.access = ACCESS_PRIVATE;
918 c->attr.external = 1;
919 c->attr.untyped = 1;
920 c->attr.if_source = IFSRC_IFBODY;
921 }
922 else if (c->attr.proc_pointer && c->tb)
923 {
924 *c->tb = *tb;
925 c->tb->ppc = 1;
926 }
927
928 if (tb->u.specific)
929 {
930 gfc_symbol *ifc = tb->u.specific->n.sym;
931 c->ts.interface = ifc;
932 if (!tb->deferred)
933 c->initializer = gfc_get_variable_expr (tb->u.specific);
934 c->attr.pure = ifc->attr.pure;
935 }
936 }
937
938
939 /* Add all specific type-bound procedures in the symtree 'st' to a vtype. */
940
941 static void
942 add_procs_to_declared_vtab1 (gfc_symtree *st, gfc_symbol *vtype)
943 {
944 if (!st)
945 return;
946
947 if (st->left)
948 add_procs_to_declared_vtab1 (st->left, vtype);
949
950 if (st->right)
951 add_procs_to_declared_vtab1 (st->right, vtype);
952
953 if (st->n.tb && !st->n.tb->error
954 && !st->n.tb->is_generic && st->n.tb->u.specific)
955 add_proc_comp (vtype, st->name, st->n.tb);
956 }
957
958
959 /* Copy procedure pointers components from the parent type. */
960
961 static void
962 copy_vtab_proc_comps (gfc_symbol *declared, gfc_symbol *vtype)
963 {
964 gfc_component *cmp;
965 gfc_symbol *vtab;
966
967 vtab = gfc_find_derived_vtab (declared);
968
969 for (cmp = vtab->ts.u.derived->components; cmp; cmp = cmp->next)
970 {
971 if (gfc_find_component (vtype, cmp->name, true, true, NULL))
972 continue;
973
974 add_proc_comp (vtype, cmp->name, cmp->tb);
975 }
976 }
977
978
979 /* Returns true if any of its nonpointer nonallocatable components or
980 their nonpointer nonallocatable subcomponents has a finalization
981 subroutine. */
982
983 static bool
984 has_finalizer_component (gfc_symbol *derived)
985 {
986 gfc_component *c;
987
988 for (c = derived->components; c; c = c->next)
989 if (c->ts.type == BT_DERIVED && !c->attr.pointer && !c->attr.allocatable
990 && c->attr.flavor != FL_PROCEDURE)
991 {
992 if (c->ts.u.derived->f2k_derived
993 && c->ts.u.derived->f2k_derived->finalizers)
994 return true;
995
996 /* Stop infinite recursion through this function by inhibiting
997 calls when the derived type and that of the component are
998 the same. */
999 if (!gfc_compare_derived_types (derived, c->ts.u.derived)
1000 && has_finalizer_component (c->ts.u.derived))
1001 return true;
1002 }
1003 return false;
1004 }
1005
1006
1007 static bool
1008 comp_is_finalizable (gfc_component *comp)
1009 {
1010 if (comp->attr.proc_pointer)
1011 return false;
1012 else if (comp->attr.allocatable && comp->ts.type != BT_CLASS)
1013 return true;
1014 else if (comp->ts.type == BT_DERIVED && !comp->attr.pointer
1015 && (comp->ts.u.derived->attr.alloc_comp
1016 || has_finalizer_component (comp->ts.u.derived)
1017 || (comp->ts.u.derived->f2k_derived
1018 && comp->ts.u.derived->f2k_derived->finalizers)))
1019 return true;
1020 else if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
1021 && CLASS_DATA (comp)->attr.allocatable)
1022 return true;
1023 else
1024 return false;
1025 }
1026
1027
1028 /* Call DEALLOCATE for the passed component if it is allocatable, if it is
1029 neither allocatable nor a pointer but has a finalizer, call it. If it
1030 is a nonpointer component with allocatable components or has finalizers, walk
1031 them. Either of them is required; other nonallocatables and pointers aren't
1032 handled gracefully.
1033 Note: If the component is allocatable, the DEALLOCATE handling takes care
1034 of calling the appropriate finalizers, coarray deregistering, and
1035 deallocation of allocatable subcomponents. */
1036
1037 static bool
1038 finalize_component (gfc_expr *expr, gfc_symbol *derived, gfc_component *comp,
1039 gfc_symbol *stat, gfc_symbol *fini_coarray, gfc_code **code,
1040 gfc_namespace *sub_ns)
1041 {
1042 gfc_expr *e;
1043 gfc_ref *ref;
1044 gfc_was_finalized *f;
1045
1046 if (!comp_is_finalizable (comp))
1047 return false;
1048
1049 /* If this expression with this component has been finalized
1050 already in this namespace, there is nothing to do. */
1051 for (f = sub_ns->was_finalized; f; f = f->next)
1052 {
1053 if (f->e == expr && f->c == comp)
1054 return false;
1055 }
1056
1057 e = gfc_copy_expr (expr);
1058 if (!e->ref)
1059 e->ref = ref = gfc_get_ref ();
1060 else
1061 {
1062 for (ref = e->ref; ref->next; ref = ref->next)
1063 ;
1064 ref->next = gfc_get_ref ();
1065 ref = ref->next;
1066 }
1067 ref->type = REF_COMPONENT;
1068 ref->u.c.sym = derived;
1069 ref->u.c.component = comp;
1070 e->ts = comp->ts;
1071
1072 if (comp->attr.dimension || comp->attr.codimension
1073 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
1074 && (CLASS_DATA (comp)->attr.dimension
1075 || CLASS_DATA (comp)->attr.codimension)))
1076 {
1077 ref->next = gfc_get_ref ();
1078 ref->next->type = REF_ARRAY;
1079 ref->next->u.ar.dimen = 0;
1080 ref->next->u.ar.as = comp->ts.type == BT_CLASS ? CLASS_DATA (comp)->as
1081 : comp->as;
1082 e->rank = ref->next->u.ar.as->rank;
1083 e->corank = ref->next->u.ar.as->corank;
1084 ref->next->u.ar.type = e->rank ? AR_FULL : AR_ELEMENT;
1085 }
1086
1087 /* Call DEALLOCATE (comp, stat=ignore). */
1088 if (comp->attr.allocatable
1089 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
1090 && CLASS_DATA (comp)->attr.allocatable))
1091 {
1092 gfc_code *dealloc, *block = NULL;
1093
1094 /* Add IF (fini_coarray). */
1095 if (comp->attr.codimension
1096 || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)
1097 && CLASS_DATA (comp)->attr.codimension))
1098 {
1099 block = gfc_get_code (EXEC_IF);
1100 if (*code)
1101 {
1102 (*code)->next = block;
1103 (*code) = (*code)->next;
1104 }
1105 else
1106 (*code) = block;
1107
1108 block->block = gfc_get_code (EXEC_IF);
1109 block = block->block;
1110 block->expr1 = gfc_lval_expr_from_sym (fini_coarray);
1111 }
1112
1113 dealloc = gfc_get_code (EXEC_DEALLOCATE);
1114
1115 dealloc->ext.alloc.list = gfc_get_alloc ();
1116 dealloc->ext.alloc.list->expr = e;
1117 dealloc->expr1 = gfc_lval_expr_from_sym (stat);
1118
1119 gfc_code *cond = gfc_get_code (EXEC_IF);
1120 cond->block = gfc_get_code (EXEC_IF);
1121 cond->block->expr1 = gfc_get_expr ();
1122 cond->block->expr1->expr_type = EXPR_FUNCTION;
1123 cond->block->expr1->where = gfc_current_locus;
1124 gfc_get_sym_tree ("associated", sub_ns, &cond->block->expr1->symtree, false);
1125 cond->block->expr1->symtree->n.sym->attr.flavor = FL_PROCEDURE;
1126 cond->block->expr1->symtree->n.sym->attr.intrinsic = 1;
1127 cond->block->expr1->symtree->n.sym->result = cond->block->expr1->symtree->n.sym;
1128 gfc_commit_symbol (cond->block->expr1->symtree->n.sym);
1129 cond->block->expr1->ts.type = BT_LOGICAL;
1130 cond->block->expr1->ts.kind = gfc_default_logical_kind;
1131 cond->block->expr1->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_ASSOCIATED);
1132 cond->block->expr1->value.function.actual = gfc_get_actual_arglist ();
1133 cond->block->expr1->value.function.actual->expr = gfc_copy_expr (expr);
1134 cond->block->expr1->value.function.actual->next = gfc_get_actual_arglist ();
1135 cond->block->next = dealloc;
1136
1137 if (block)
1138 block->next = cond;
1139 else if (*code)
1140 {
1141 (*code)->next = cond;
1142 (*code) = (*code)->next;
1143 }
1144 else
1145 (*code) = cond;
1146
1147 }
1148 else if (comp->ts.type == BT_DERIVED
1149 && comp->ts.u.derived->f2k_derived
1150 && comp->ts.u.derived->f2k_derived->finalizers)
1151 {
1152 /* Call FINAL_WRAPPER (comp); */
1153 gfc_code *final_wrap;
1154 gfc_symbol *vtab, *byte_stride;
1155 gfc_expr *scalar, *size_expr, *fini_coarray_expr;
1156 gfc_component *c;
1157
1158 vtab = gfc_find_derived_vtab (comp->ts.u.derived);
1159 for (c = vtab->ts.u.derived->components; c; c = c->next)
1160 if (strcmp (c->name, "_final") == 0)
1161 break;
1162
1163 gcc_assert (c);
1164
1165 /* Set scalar argument for storage_size. A leading underscore in
1166 the name prevents an unwanted finalization. */
1167 gfc_get_symbol ("_comp_byte_stride", sub_ns, &byte_stride);
1168 byte_stride->ts = e->ts;
1169 byte_stride->attr.flavor = FL_VARIABLE;
1170 byte_stride->attr.value = 1;
1171 byte_stride->attr.artificial = 1;
1172 gfc_set_sym_referenced (byte_stride);
1173 gfc_commit_symbol (byte_stride);
1174 scalar = gfc_lval_expr_from_sym (byte_stride);
1175
1176 final_wrap = gfc_get_code (EXEC_CALL);
1177 final_wrap->symtree = c->initializer->symtree;
1178 final_wrap->resolved_sym = c->initializer->symtree->n.sym;
1179 final_wrap->ext.actual = gfc_get_actual_arglist ();
1180 final_wrap->ext.actual->expr = e;
1181
1182 /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
1183 size_expr = gfc_get_expr ();
1184 size_expr->where = gfc_current_locus;
1185 size_expr->expr_type = EXPR_OP;
1186 size_expr->value.op.op = INTRINSIC_DIVIDE;
1187
1188 /* STORAGE_SIZE (array,kind=c_intptr_t). */
1189 size_expr->value.op.op1
1190 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
1191 "storage_size", gfc_current_locus, 2,
1192 scalar,
1193 gfc_get_int_expr (gfc_index_integer_kind,
1194 NULL, 0));
1195
1196 /* NUMERIC_STORAGE_SIZE. */
1197 size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
1198 gfc_character_storage_size);
1199 size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
1200 size_expr->ts = size_expr->value.op.op1->ts;
1201
1202 /* Which provides the argument 'byte_stride'..... */
1203 final_wrap->ext.actual->next = gfc_get_actual_arglist ();
1204 final_wrap->ext.actual->next->expr = size_expr;
1205
1206 /* ...and last of all the 'fini_coarray' argument. */
1207 fini_coarray_expr = gfc_lval_expr_from_sym (fini_coarray);
1208 final_wrap->ext.actual->next->next = gfc_get_actual_arglist ();
1209 final_wrap->ext.actual->next->next->expr = fini_coarray_expr;
1210
1211 if (*code)
1212 {
1213 (*code)->next = final_wrap;
1214 (*code) = (*code)->next;
1215 }
1216 else
1217 (*code) = final_wrap;
1218 }
1219 else
1220 {
1221 gfc_component *c;
1222 bool ret = false;
1223
1224 for (c = comp->ts.u.derived->components; c; c = c->next)
1225 ret |= finalize_component (e, comp->ts.u.derived, c, stat, fini_coarray,
1226 code, sub_ns);
1227 /* Only free the expression, if it has never been used. */
1228 if (!ret)
1229 gfc_free_expr (e);
1230 }
1231
1232 /* Record that this was finalized already in this namespace. */
1233 f = sub_ns->was_finalized;
1234 sub_ns->was_finalized = XCNEW (gfc_was_finalized);
1235 sub_ns->was_finalized->e = expr;
1236 sub_ns->was_finalized->c = comp;
1237 sub_ns->was_finalized->next = f;
1238 return true;
1239 }
1240
1241
1242 /* Generate code equivalent to
1243 CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
1244 + offset, c_ptr), ptr). */
1245
1246 static gfc_code *
1247 finalization_scalarizer (gfc_symbol *array, gfc_symbol *ptr,
1248 gfc_expr *offset, gfc_namespace *sub_ns)
1249 {
1250 gfc_code *block;
1251 gfc_expr *expr, *expr2;
1252
1253 /* C_F_POINTER(). */
1254 block = gfc_get_code (EXEC_CALL);
1255 gfc_get_sym_tree ("c_f_pointer", sub_ns, &block->symtree, true);
1256 block->resolved_sym = block->symtree->n.sym;
1257 block->resolved_sym->attr.flavor = FL_PROCEDURE;
1258 block->resolved_sym->attr.intrinsic = 1;
1259 block->resolved_sym->attr.subroutine = 1;
1260 block->resolved_sym->from_intmod = INTMOD_ISO_C_BINDING;
1261 block->resolved_sym->intmod_sym_id = ISOCBINDING_F_POINTER;
1262 block->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_C_F_POINTER);
1263 gfc_commit_symbol (block->resolved_sym);
1264
1265 /* C_F_POINTER's first argument: TRANSFER ( <addr>, c_intptr_t). */
1266 block->ext.actual = gfc_get_actual_arglist ();
1267 block->ext.actual->next = gfc_get_actual_arglist ();
1268 block->ext.actual->next->expr = gfc_get_int_expr (gfc_index_integer_kind,
1269 NULL, 0);
1270 block->ext.actual->next->next = gfc_get_actual_arglist (); /* SIZE. */
1271
1272 /* The <addr> part: TRANSFER (C_LOC (array), c_intptr_t). */
1273
1274 /* TRANSFER's first argument: C_LOC (array). */
1275 expr = gfc_get_expr ();
1276 expr->expr_type = EXPR_FUNCTION;
1277 gfc_get_sym_tree ("c_loc", sub_ns, &expr->symtree, false);
1278 expr->symtree->n.sym->attr.flavor = FL_PROCEDURE;
1279 expr->symtree->n.sym->intmod_sym_id = ISOCBINDING_LOC;
1280 expr->symtree->n.sym->attr.intrinsic = 1;
1281 expr->symtree->n.sym->from_intmod = INTMOD_ISO_C_BINDING;
1282 expr->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_C_LOC);
1283 expr->value.function.actual = gfc_get_actual_arglist ();
1284 expr->value.function.actual->expr
1285 = gfc_lval_expr_from_sym (array);
1286 expr->symtree->n.sym->result = expr->symtree->n.sym;
1287 gfc_commit_symbol (expr->symtree->n.sym);
1288 expr->ts.type = BT_INTEGER;
1289 expr->ts.kind = gfc_index_integer_kind;
1290 expr->where = gfc_current_locus;
1291
1292 /* TRANSFER. */
1293 expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_TRANSFER, "transfer",
1294 gfc_current_locus, 3, expr,
1295 gfc_get_int_expr (gfc_index_integer_kind,
1296 NULL, 0), NULL);
1297 expr2->ts.type = BT_INTEGER;
1298 expr2->ts.kind = gfc_index_integer_kind;
1299
1300 /* <array addr> + <offset>. */
1301 block->ext.actual->expr = gfc_get_expr ();
1302 block->ext.actual->expr->expr_type = EXPR_OP;
1303 block->ext.actual->expr->value.op.op = INTRINSIC_PLUS;
1304 block->ext.actual->expr->value.op.op1 = expr2;
1305 block->ext.actual->expr->value.op.op2 = offset;
1306 block->ext.actual->expr->ts = expr->ts;
1307 block->ext.actual->expr->where = gfc_current_locus;
1308
1309 /* C_F_POINTER's 2nd arg: ptr -- and its absent shape=. */
1310 block->ext.actual->next = gfc_get_actual_arglist ();
1311 block->ext.actual->next->expr = gfc_lval_expr_from_sym (ptr);
1312 block->ext.actual->next->next = gfc_get_actual_arglist ();
1313
1314 return block;
1315 }
1316
1317
1318 /* Calculates the offset to the (idx+1)th element of an array, taking the
1319 stride into account. It generates the code:
1320 offset = 0
1321 do idx2 = 1, rank
1322 offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1) * strides(idx2)
1323 end do
1324 offset = offset * byte_stride. */
1325
1326 static gfc_code*
1327 finalization_get_offset (gfc_symbol *idx, gfc_symbol *idx2, gfc_symbol *offset,
1328 gfc_symbol *strides, gfc_symbol *sizes,
1329 gfc_symbol *byte_stride, gfc_expr *rank,
1330 gfc_code *block, gfc_namespace *sub_ns)
1331 {
1332 gfc_iterator *iter;
1333 gfc_expr *expr, *expr2;
1334
1335 /* offset = 0. */
1336 block->next = gfc_get_code (EXEC_ASSIGN);
1337 block = block->next;
1338 block->expr1 = gfc_lval_expr_from_sym (offset);
1339 block->expr2 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1340
1341 /* Create loop. */
1342 iter = gfc_get_iterator ();
1343 iter->var = gfc_lval_expr_from_sym (idx2);
1344 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1345 iter->end = gfc_copy_expr (rank);
1346 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1347 block->next = gfc_get_code (EXEC_DO);
1348 block = block->next;
1349 block->ext.iterator = iter;
1350 block->block = gfc_get_code (EXEC_DO);
1351
1352 /* Loop body: offset = offset + mod (idx, sizes(idx2)) / sizes(idx2-1)
1353 * strides(idx2). */
1354
1355 /* mod (idx, sizes(idx2)). */
1356 expr = gfc_lval_expr_from_sym (sizes);
1357 expr->ref = gfc_get_ref ();
1358 expr->ref->type = REF_ARRAY;
1359 expr->ref->u.ar.as = sizes->as;
1360 expr->ref->u.ar.type = AR_ELEMENT;
1361 expr->ref->u.ar.dimen = 1;
1362 expr->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
1363 expr->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
1364 expr->where = sizes->declared_at;
1365
1366 expr = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_MOD, "mod",
1367 gfc_current_locus, 2,
1368 gfc_lval_expr_from_sym (idx), expr);
1369 expr->ts = idx->ts;
1370
1371 /* (...) / sizes(idx2-1). */
1372 expr2 = gfc_get_expr ();
1373 expr2->expr_type = EXPR_OP;
1374 expr2->value.op.op = INTRINSIC_DIVIDE;
1375 expr2->value.op.op1 = expr;
1376 expr2->value.op.op2 = gfc_lval_expr_from_sym (sizes);
1377 expr2->value.op.op2->ref = gfc_get_ref ();
1378 expr2->value.op.op2->ref->type = REF_ARRAY;
1379 expr2->value.op.op2->ref->u.ar.as = sizes->as;
1380 expr2->value.op.op2->ref->u.ar.type = AR_ELEMENT;
1381 expr2->value.op.op2->ref->u.ar.dimen = 1;
1382 expr2->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
1383 expr2->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
1384 expr2->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
1385 expr2->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
1386 expr2->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
1387 expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1
1388 = gfc_lval_expr_from_sym (idx2);
1389 expr2->value.op.op2->ref->u.ar.start[0]->value.op.op2
1390 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1391 expr2->value.op.op2->ref->u.ar.start[0]->ts
1392 = expr2->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
1393 expr2->ts = idx->ts;
1394 expr2->where = gfc_current_locus;
1395
1396 /* ... * strides(idx2). */
1397 expr = gfc_get_expr ();
1398 expr->expr_type = EXPR_OP;
1399 expr->value.op.op = INTRINSIC_TIMES;
1400 expr->value.op.op1 = expr2;
1401 expr->value.op.op2 = gfc_lval_expr_from_sym (strides);
1402 expr->value.op.op2->ref = gfc_get_ref ();
1403 expr->value.op.op2->ref->type = REF_ARRAY;
1404 expr->value.op.op2->ref->u.ar.type = AR_ELEMENT;
1405 expr->value.op.op2->ref->u.ar.dimen = 1;
1406 expr->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
1407 expr->value.op.op2->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx2);
1408 expr->value.op.op2->ref->u.ar.as = strides->as;
1409 expr->ts = idx->ts;
1410 expr->where = gfc_current_locus;
1411
1412 /* offset = offset + ... */
1413 block->block->next = gfc_get_code (EXEC_ASSIGN);
1414 block->block->next->expr1 = gfc_lval_expr_from_sym (offset);
1415 block->block->next->expr2 = gfc_get_expr ();
1416 block->block->next->expr2->expr_type = EXPR_OP;
1417 block->block->next->expr2->value.op.op = INTRINSIC_PLUS;
1418 block->block->next->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
1419 block->block->next->expr2->value.op.op2 = expr;
1420 block->block->next->expr2->ts = idx->ts;
1421 block->block->next->expr2->where = gfc_current_locus;
1422
1423 /* After the loop: offset = offset * byte_stride. */
1424 block->next = gfc_get_code (EXEC_ASSIGN);
1425 block = block->next;
1426 block->expr1 = gfc_lval_expr_from_sym (offset);
1427 block->expr2 = gfc_get_expr ();
1428 block->expr2->expr_type = EXPR_OP;
1429 block->expr2->value.op.op = INTRINSIC_TIMES;
1430 block->expr2->value.op.op1 = gfc_lval_expr_from_sym (offset);
1431 block->expr2->value.op.op2 = gfc_lval_expr_from_sym (byte_stride);
1432 block->expr2->ts = block->expr2->value.op.op1->ts;
1433 block->expr2->where = gfc_current_locus;
1434 return block;
1435 }
1436
1437
1438 /* Insert code of the following form:
1439
1440 block
1441 integer(c_intptr_t) :: i
1442
1443 if ((byte_stride == STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
1444 && (is_contiguous || !final_rank3->attr.contiguous
1445 || final_rank3->as->type != AS_ASSUMED_SHAPE))
1446 || 0 == STORAGE_SIZE (array)) then
1447 call final_rank3 (array)
1448 else
1449 block
1450 integer(c_intptr_t) :: offset, j
1451 type(t) :: tmp(shape (array))
1452
1453 do i = 0, size (array)-1
1454 offset = obtain_offset(i, strides, sizes, byte_stride)
1455 addr = transfer (c_loc (array), addr) + offset
1456 call c_f_pointer (transfer (addr, cptr), ptr)
1457
1458 addr = transfer (c_loc (tmp), addr)
1459 + i * STORAGE_SIZE (array)/NUMERIC_STORAGE_SIZE
1460 call c_f_pointer (transfer (addr, cptr), ptr2)
1461 ptr2 = ptr
1462 end do
1463 call final_rank3 (tmp)
1464 end block
1465 end if
1466 block */
1467
1468 static void
1469 finalizer_insert_packed_call (gfc_code *block, gfc_finalizer *fini,
1470 gfc_symbol *array, gfc_symbol *byte_stride,
1471 gfc_symbol *idx, gfc_symbol *ptr,
1472 gfc_symbol *nelem,
1473 gfc_symbol *strides, gfc_symbol *sizes,
1474 gfc_symbol *idx2, gfc_symbol *offset,
1475 gfc_symbol *is_contiguous, gfc_expr *rank,
1476 gfc_namespace *sub_ns)
1477 {
1478 gfc_symbol *tmp_array, *ptr2;
1479 gfc_expr *size_expr, *offset2, *expr;
1480 gfc_namespace *ns;
1481 gfc_iterator *iter;
1482 gfc_code *block2;
1483 int i;
1484
1485 block->next = gfc_get_code (EXEC_IF);
1486 block = block->next;
1487
1488 block->block = gfc_get_code (EXEC_IF);
1489 block = block->block;
1490
1491 /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
1492 size_expr = gfc_get_expr ();
1493 size_expr->where = gfc_current_locus;
1494 size_expr->expr_type = EXPR_OP;
1495 size_expr->value.op.op = INTRINSIC_DIVIDE;
1496
1497 /* STORAGE_SIZE (array,kind=c_intptr_t). */
1498 size_expr->value.op.op1
1499 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STORAGE_SIZE,
1500 "storage_size", gfc_current_locus, 2,
1501 gfc_lval_expr_from_sym (array),
1502 gfc_get_int_expr (gfc_index_integer_kind,
1503 NULL, 0));
1504
1505 /* NUMERIC_STORAGE_SIZE. */
1506 size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
1507 gfc_character_storage_size);
1508 size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
1509 size_expr->ts = size_expr->value.op.op1->ts;
1510
1511 /* IF condition: (stride == size_expr
1512 && ((fini's as->ASSUMED_SIZE && !fini's attr.contiguous)
1513 || is_contiguous)
1514 || 0 == size_expr. */
1515 block->expr1 = gfc_get_expr ();
1516 block->expr1->ts.type = BT_LOGICAL;
1517 block->expr1->ts.kind = gfc_default_logical_kind;
1518 block->expr1->expr_type = EXPR_OP;
1519 block->expr1->where = gfc_current_locus;
1520
1521 block->expr1->value.op.op = INTRINSIC_OR;
1522
1523 /* byte_stride == size_expr */
1524 expr = gfc_get_expr ();
1525 expr->ts.type = BT_LOGICAL;
1526 expr->ts.kind = gfc_default_logical_kind;
1527 expr->expr_type = EXPR_OP;
1528 expr->where = gfc_current_locus;
1529 expr->value.op.op = INTRINSIC_EQ;
1530 expr->value.op.op1
1531 = gfc_lval_expr_from_sym (byte_stride);
1532 expr->value.op.op2 = size_expr;
1533
1534 /* If strides aren't allowed (not assumed shape or CONTIGUOUS),
1535 add is_contiguous check. */
1536
1537 if (fini->proc_tree->n.sym->formal->sym->as->type != AS_ASSUMED_SHAPE
1538 || fini->proc_tree->n.sym->formal->sym->attr.contiguous)
1539 {
1540 gfc_expr *expr2;
1541 expr2 = gfc_get_expr ();
1542 expr2->ts.type = BT_LOGICAL;
1543 expr2->ts.kind = gfc_default_logical_kind;
1544 expr2->expr_type = EXPR_OP;
1545 expr2->where = gfc_current_locus;
1546 expr2->value.op.op = INTRINSIC_AND;
1547 expr2->value.op.op1 = expr;
1548 expr2->value.op.op2 = gfc_lval_expr_from_sym (is_contiguous);
1549 expr = expr2;
1550 }
1551
1552 block->expr1->value.op.op1 = expr;
1553
1554 /* 0 == size_expr */
1555 block->expr1->value.op.op2 = gfc_get_expr ();
1556 block->expr1->value.op.op2->ts.type = BT_LOGICAL;
1557 block->expr1->value.op.op2->ts.kind = gfc_default_logical_kind;
1558 block->expr1->value.op.op2->expr_type = EXPR_OP;
1559 block->expr1->value.op.op2->where = gfc_current_locus;
1560 block->expr1->value.op.op2->value.op.op = INTRINSIC_EQ;
1561 block->expr1->value.op.op2->value.op.op1 =
1562 gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1563 block->expr1->value.op.op2->value.op.op2 = gfc_copy_expr (size_expr);
1564
1565 /* IF body: call final subroutine. */
1566 block->next = gfc_get_code (EXEC_CALL);
1567 block->next->symtree = fini->proc_tree;
1568 block->next->resolved_sym = fini->proc_tree->n.sym;
1569 block->next->ext.actual = gfc_get_actual_arglist ();
1570 block->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
1571
1572 /* ELSE. */
1573
1574 block->block = gfc_get_code (EXEC_IF);
1575 block = block->block;
1576
1577 /* BLOCK ... END BLOCK. */
1578 block->next = gfc_get_code (EXEC_BLOCK);
1579 block = block->next;
1580
1581 ns = gfc_build_block_ns (sub_ns);
1582 block->ext.block.ns = ns;
1583 block->ext.block.assoc = NULL;
1584
1585 gfc_get_symbol ("ptr2", ns, &ptr2);
1586 ptr2->ts.type = BT_DERIVED;
1587 ptr2->ts.u.derived = array->ts.u.derived;
1588 ptr2->attr.flavor = FL_VARIABLE;
1589 ptr2->attr.pointer = 1;
1590 ptr2->attr.artificial = 1;
1591 gfc_set_sym_referenced (ptr2);
1592 gfc_commit_symbol (ptr2);
1593
1594 gfc_get_symbol ("tmp_array", ns, &tmp_array);
1595 tmp_array->ts.type = BT_DERIVED;
1596 tmp_array->ts.u.derived = array->ts.u.derived;
1597 tmp_array->attr.flavor = FL_VARIABLE;
1598 tmp_array->attr.dimension = 1;
1599 tmp_array->attr.artificial = 1;
1600 tmp_array->as = gfc_get_array_spec();
1601 tmp_array->attr.intent = INTENT_INOUT;
1602 tmp_array->as->type = AS_EXPLICIT;
1603 tmp_array->as->rank = fini->proc_tree->n.sym->formal->sym->as->rank;
1604
1605 for (i = 0; i < tmp_array->as->rank; i++)
1606 {
1607 gfc_expr *shape_expr;
1608 tmp_array->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
1609 NULL, 1);
1610 /* SIZE (array, dim=i+1, kind=gfc_index_integer_kind). */
1611 shape_expr
1612 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
1613 gfc_current_locus, 3,
1614 gfc_lval_expr_from_sym (array),
1615 gfc_get_int_expr (gfc_default_integer_kind,
1616 NULL, i+1),
1617 gfc_get_int_expr (gfc_default_integer_kind,
1618 NULL,
1619 gfc_index_integer_kind));
1620 shape_expr->ts.kind = gfc_index_integer_kind;
1621 tmp_array->as->upper[i] = shape_expr;
1622 }
1623 gfc_set_sym_referenced (tmp_array);
1624 gfc_commit_symbol (tmp_array);
1625
1626 /* Create loop. */
1627 iter = gfc_get_iterator ();
1628 iter->var = gfc_lval_expr_from_sym (idx);
1629 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1630 iter->end = gfc_lval_expr_from_sym (nelem);
1631 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1632
1633 block = gfc_get_code (EXEC_DO);
1634 ns->code = block;
1635 block->ext.iterator = iter;
1636 block->block = gfc_get_code (EXEC_DO);
1637
1638 /* Offset calculation for the new array: idx * size of type (in bytes). */
1639 offset2 = gfc_get_expr ();
1640 offset2->expr_type = EXPR_OP;
1641 offset2->where = gfc_current_locus;
1642 offset2->value.op.op = INTRINSIC_TIMES;
1643 offset2->value.op.op1 = gfc_lval_expr_from_sym (idx);
1644 offset2->value.op.op2 = gfc_copy_expr (size_expr);
1645 offset2->ts = byte_stride->ts;
1646
1647 /* Offset calculation of "array". */
1648 block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
1649 byte_stride, rank, block->block, sub_ns);
1650
1651 /* Create code for
1652 CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
1653 + idx * stride, c_ptr), ptr). */
1654 block2->next = finalization_scalarizer (array, ptr,
1655 gfc_lval_expr_from_sym (offset),
1656 sub_ns);
1657 block2 = block2->next;
1658 block2->next = finalization_scalarizer (tmp_array, ptr2, offset2, sub_ns);
1659 block2 = block2->next;
1660
1661 /* ptr2 = ptr. */
1662 block2->next = gfc_get_code (EXEC_ASSIGN);
1663 block2 = block2->next;
1664 block2->expr1 = gfc_lval_expr_from_sym (ptr2);
1665 block2->expr2 = gfc_lval_expr_from_sym (ptr);
1666
1667 /* Call now the user's final subroutine. */
1668 block->next = gfc_get_code (EXEC_CALL);
1669 block = block->next;
1670 block->symtree = fini->proc_tree;
1671 block->resolved_sym = fini->proc_tree->n.sym;
1672 block->ext.actual = gfc_get_actual_arglist ();
1673 block->ext.actual->expr = gfc_lval_expr_from_sym (tmp_array);
1674
1675 if (fini->proc_tree->n.sym->formal->sym->attr.intent == INTENT_IN)
1676 return;
1677
1678 /* Copy back. */
1679
1680 /* Loop. */
1681 iter = gfc_get_iterator ();
1682 iter->var = gfc_lval_expr_from_sym (idx);
1683 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1684 iter->end = gfc_lval_expr_from_sym (nelem);
1685 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1686
1687 block->next = gfc_get_code (EXEC_DO);
1688 block = block->next;
1689 block->ext.iterator = iter;
1690 block->block = gfc_get_code (EXEC_DO);
1691
1692 /* Offset calculation of "array". */
1693 block2 = finalization_get_offset (idx, idx2, offset, strides, sizes,
1694 byte_stride, rank, block->block, sub_ns);
1695
1696 /* Create code for
1697 CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
1698 + offset, c_ptr), ptr). */
1699 block2->next = finalization_scalarizer (array, ptr,
1700 gfc_lval_expr_from_sym (offset),
1701 sub_ns);
1702 block2 = block2->next;
1703 block2->next = finalization_scalarizer (tmp_array, ptr2,
1704 gfc_copy_expr (offset2), sub_ns);
1705 block2 = block2->next;
1706
1707 /* ptr = ptr2. */
1708 block2->next = gfc_get_code (EXEC_ASSIGN);
1709 block2->next->expr1 = gfc_lval_expr_from_sym (ptr);
1710 block2->next->expr2 = gfc_lval_expr_from_sym (ptr2);
1711 }
1712
1713
1714 /* Generate the finalization/polymorphic freeing wrapper subroutine for the
1715 derived type "derived". The function first calls the appropriate FINAL
1716 subroutine, then it DEALLOCATEs (finalizes/frees) the allocatable
1717 components (but not the inherited ones). Last, it calls the wrapper
1718 subroutine of the parent. The generated wrapper procedure takes as argument
1719 an assumed-rank array.
1720 If neither allocatable components nor FINAL subroutines exists, the vtab
1721 will contain a NULL pointer.
1722 The generated function has the form
1723 _final(assumed-rank array, stride, skip_corarray)
1724 where the array has to be contiguous (except of the lowest dimension). The
1725 stride (in bytes) is used to allow different sizes for ancestor types by
1726 skipping over the additionally added components in the scalarizer. If
1727 "fini_coarray" is false, coarray components are not finalized to allow for
1728 the correct semantic with intrinsic assignment. */
1729
1730 static void
1731 generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
1732 const char *tname, gfc_component *vtab_final)
1733 {
1734 gfc_symbol *final, *array, *fini_coarray, *byte_stride, *sizes, *strides;
1735 gfc_symbol *ptr = NULL, *idx, *idx2, *is_contiguous, *offset, *nelem;
1736 gfc_component *comp;
1737 gfc_namespace *sub_ns;
1738 gfc_code *last_code, *block;
1739 char *name;
1740 bool finalizable_comp = false;
1741 gfc_expr *ancestor_wrapper = NULL, *rank;
1742 gfc_iterator *iter;
1743
1744 if (derived->attr.unlimited_polymorphic || derived->error)
1745 {
1746 vtab_final->initializer = gfc_get_null_expr (NULL);
1747 return;
1748 }
1749
1750 /* Search for the ancestor's finalizers. */
1751 if (derived->attr.extension && derived->components
1752 && (!derived->components->ts.u.derived->attr.abstract
1753 || has_finalizer_component (derived)))
1754 {
1755 gfc_symbol *vtab;
1756 gfc_component *comp;
1757
1758 vtab = gfc_find_derived_vtab (derived->components->ts.u.derived);
1759 for (comp = vtab->ts.u.derived->components; comp; comp = comp->next)
1760 if (comp->name[0] == '_' && comp->name[1] == 'f')
1761 {
1762 ancestor_wrapper = comp->initializer;
1763 break;
1764 }
1765 }
1766
1767 /* No wrapper of the ancestor and no own FINAL subroutines and allocatable
1768 components: Return a NULL() expression; we defer this a bit to have
1769 an interface declaration. */
1770 if ((!ancestor_wrapper || ancestor_wrapper->expr_type == EXPR_NULL)
1771 && !derived->attr.alloc_comp
1772 && (!derived->f2k_derived || !derived->f2k_derived->finalizers)
1773 && !has_finalizer_component (derived))
1774 {
1775 vtab_final->initializer = gfc_get_null_expr (NULL);
1776 gcc_assert (vtab_final->ts.interface == NULL);
1777 return;
1778 }
1779 else
1780 /* Check whether there are new allocatable components. */
1781 for (comp = derived->components; comp; comp = comp->next)
1782 {
1783 if (comp == derived->components && derived->attr.extension
1784 && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
1785 continue;
1786
1787 finalizable_comp |= comp_is_finalizable (comp);
1788 }
1789
1790 /* If there is no new finalizer and no new allocatable, return with
1791 an expr to the ancestor's one. */
1792 if (!finalizable_comp
1793 && (!derived->f2k_derived || !derived->f2k_derived->finalizers))
1794 {
1795 gcc_assert (ancestor_wrapper && ancestor_wrapper->ref == NULL
1796 && ancestor_wrapper->expr_type == EXPR_VARIABLE);
1797 vtab_final->initializer = gfc_copy_expr (ancestor_wrapper);
1798 vtab_final->ts.interface = vtab_final->initializer->symtree->n.sym;
1799 return;
1800 }
1801
1802 /* We now create a wrapper, which does the following:
1803 1. Call the suitable finalization subroutine for this type
1804 2. Loop over all noninherited allocatable components and noninherited
1805 components with allocatable components and DEALLOCATE those; this will
1806 take care of finalizers, coarray deregistering and allocatable
1807 nested components.
1808 3. Call the ancestor's finalizer. */
1809
1810 /* Declare the wrapper function; it takes an assumed-rank array
1811 and a VALUE logical as arguments. */
1812
1813 /* Set up the namespace. */
1814 sub_ns = gfc_get_namespace (ns, 0);
1815 sub_ns->sibling = ns->contained;
1816 ns->contained = sub_ns;
1817 sub_ns->resolved = 1;
1818
1819 /* Set up the procedure symbol. */
1820 name = xasprintf ("__final_%s", tname);
1821 gfc_get_symbol (name, sub_ns, &final);
1822 sub_ns->proc_name = final;
1823 final->attr.flavor = FL_PROCEDURE;
1824 final->attr.function = 1;
1825 final->attr.pure = 0;
1826 final->attr.recursive = 1;
1827 final->result = final;
1828 final->ts.type = BT_INTEGER;
1829 final->ts.kind = 4;
1830 final->attr.artificial = 1;
1831 final->attr.always_explicit = 1;
1832 final->attr.if_source = IFSRC_DECL;
1833 if (ns->proc_name->attr.flavor == FL_MODULE)
1834 final->module = ns->proc_name->name;
1835 gfc_set_sym_referenced (final);
1836 gfc_commit_symbol (final);
1837
1838 /* Set up formal argument. */
1839 gfc_get_symbol ("array", sub_ns, &array);
1840 array->ts.type = BT_DERIVED;
1841 array->ts.u.derived = derived;
1842 array->attr.flavor = FL_VARIABLE;
1843 array->attr.dummy = 1;
1844 array->attr.contiguous = 1;
1845 array->attr.dimension = 1;
1846 array->attr.artificial = 1;
1847 array->as = gfc_get_array_spec();
1848 array->as->type = AS_ASSUMED_RANK;
1849 array->as->rank = -1;
1850 array->attr.intent = INTENT_INOUT;
1851 gfc_set_sym_referenced (array);
1852 final->formal = gfc_get_formal_arglist ();
1853 final->formal->sym = array;
1854 gfc_commit_symbol (array);
1855
1856 /* Set up formal argument. */
1857 gfc_get_symbol ("byte_stride", sub_ns, &byte_stride);
1858 byte_stride->ts.type = BT_INTEGER;
1859 byte_stride->ts.kind = gfc_index_integer_kind;
1860 byte_stride->attr.flavor = FL_VARIABLE;
1861 byte_stride->attr.dummy = 1;
1862 byte_stride->attr.value = 1;
1863 byte_stride->attr.artificial = 1;
1864 gfc_set_sym_referenced (byte_stride);
1865 final->formal->next = gfc_get_formal_arglist ();
1866 final->formal->next->sym = byte_stride;
1867 gfc_commit_symbol (byte_stride);
1868
1869 /* Set up formal argument. */
1870 gfc_get_symbol ("fini_coarray", sub_ns, &fini_coarray);
1871 fini_coarray->ts.type = BT_LOGICAL;
1872 fini_coarray->ts.kind = 1;
1873 fini_coarray->attr.flavor = FL_VARIABLE;
1874 fini_coarray->attr.dummy = 1;
1875 fini_coarray->attr.value = 1;
1876 fini_coarray->attr.artificial = 1;
1877 gfc_set_sym_referenced (fini_coarray);
1878 final->formal->next->next = gfc_get_formal_arglist ();
1879 final->formal->next->next->sym = fini_coarray;
1880 gfc_commit_symbol (fini_coarray);
1881
1882 /* Local variables. */
1883
1884 gfc_get_symbol ("idx", sub_ns, &idx);
1885 idx->ts.type = BT_INTEGER;
1886 idx->ts.kind = gfc_index_integer_kind;
1887 idx->attr.flavor = FL_VARIABLE;
1888 idx->attr.artificial = 1;
1889 gfc_set_sym_referenced (idx);
1890 gfc_commit_symbol (idx);
1891
1892 gfc_get_symbol ("idx2", sub_ns, &idx2);
1893 idx2->ts.type = BT_INTEGER;
1894 idx2->ts.kind = gfc_index_integer_kind;
1895 idx2->attr.flavor = FL_VARIABLE;
1896 idx2->attr.artificial = 1;
1897 gfc_set_sym_referenced (idx2);
1898 gfc_commit_symbol (idx2);
1899
1900 gfc_get_symbol ("offset", sub_ns, &offset);
1901 offset->ts.type = BT_INTEGER;
1902 offset->ts.kind = gfc_index_integer_kind;
1903 offset->attr.flavor = FL_VARIABLE;
1904 offset->attr.artificial = 1;
1905 gfc_set_sym_referenced (offset);
1906 gfc_commit_symbol (offset);
1907
1908 /* Create RANK expression. */
1909 rank = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_RANK, "rank",
1910 gfc_current_locus, 1,
1911 gfc_lval_expr_from_sym (array));
1912 if (rank->ts.kind != idx->ts.kind)
1913 gfc_convert_type_warn (rank, &idx->ts, 2, 0);
1914
1915 /* Create is_contiguous variable. */
1916 gfc_get_symbol ("is_contiguous", sub_ns, &is_contiguous);
1917 is_contiguous->ts.type = BT_LOGICAL;
1918 is_contiguous->ts.kind = gfc_default_logical_kind;
1919 is_contiguous->attr.flavor = FL_VARIABLE;
1920 is_contiguous->attr.artificial = 1;
1921 gfc_set_sym_referenced (is_contiguous);
1922 gfc_commit_symbol (is_contiguous);
1923
1924 /* Create "sizes(0..rank)" variable, which contains the multiplied
1925 up extent of the dimensions, i.e. sizes(0) = 1, sizes(1) = extent(dim=1),
1926 sizes(2) = sizes(1) * extent(dim=2) etc. */
1927 gfc_get_symbol ("sizes", sub_ns, &sizes);
1928 sizes->ts.type = BT_INTEGER;
1929 sizes->ts.kind = gfc_index_integer_kind;
1930 sizes->attr.flavor = FL_VARIABLE;
1931 sizes->attr.dimension = 1;
1932 sizes->attr.artificial = 1;
1933 sizes->as = gfc_get_array_spec();
1934 sizes->attr.intent = INTENT_INOUT;
1935 sizes->as->type = AS_EXPLICIT;
1936 sizes->as->rank = 1;
1937 sizes->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1938 sizes->as->upper[0] = gfc_copy_expr (rank);
1939 gfc_set_sym_referenced (sizes);
1940 gfc_commit_symbol (sizes);
1941
1942 /* Create "strides(1..rank)" variable, which contains the strides per
1943 dimension. */
1944 gfc_get_symbol ("strides", sub_ns, &strides);
1945 strides->ts.type = BT_INTEGER;
1946 strides->ts.kind = gfc_index_integer_kind;
1947 strides->attr.flavor = FL_VARIABLE;
1948 strides->attr.dimension = 1;
1949 strides->attr.artificial = 1;
1950 strides->as = gfc_get_array_spec();
1951 strides->attr.intent = INTENT_INOUT;
1952 strides->as->type = AS_EXPLICIT;
1953 strides->as->rank = 1;
1954 strides->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1955 strides->as->upper[0] = gfc_copy_expr (rank);
1956 gfc_set_sym_referenced (strides);
1957 gfc_commit_symbol (strides);
1958
1959
1960 /* Set return value to 0. */
1961 last_code = gfc_get_code (EXEC_ASSIGN);
1962 last_code->expr1 = gfc_lval_expr_from_sym (final);
1963 last_code->expr2 = gfc_get_int_expr (4, NULL, 0);
1964 sub_ns->code = last_code;
1965
1966 /* Set: is_contiguous = .true. */
1967 last_code->next = gfc_get_code (EXEC_ASSIGN);
1968 last_code = last_code->next;
1969 last_code->expr1 = gfc_lval_expr_from_sym (is_contiguous);
1970 last_code->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
1971 &gfc_current_locus, true);
1972
1973 /* Set: sizes(0) = 1. */
1974 last_code->next = gfc_get_code (EXEC_ASSIGN);
1975 last_code = last_code->next;
1976 last_code->expr1 = gfc_lval_expr_from_sym (sizes);
1977 last_code->expr1->ref = gfc_get_ref ();
1978 last_code->expr1->ref->type = REF_ARRAY;
1979 last_code->expr1->ref->u.ar.type = AR_ELEMENT;
1980 last_code->expr1->ref->u.ar.dimen = 1;
1981 last_code->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
1982 last_code->expr1->ref->u.ar.start[0]
1983 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
1984 last_code->expr1->ref->u.ar.as = sizes->as;
1985 last_code->expr2 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1986
1987 /* Create:
1988 DO idx = 1, rank
1989 strides(idx) = _F._stride (array, dim=idx)
1990 sizes(idx) = sizes(i-1) * size(array, dim=idx, kind=index_kind)
1991 if (strides (idx) /= sizes(i-1)) is_contiguous = .false.
1992 END DO. */
1993
1994 /* Create loop. */
1995 iter = gfc_get_iterator ();
1996 iter->var = gfc_lval_expr_from_sym (idx);
1997 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
1998 iter->end = gfc_copy_expr (rank);
1999 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2000 last_code->next = gfc_get_code (EXEC_DO);
2001 last_code = last_code->next;
2002 last_code->ext.iterator = iter;
2003 last_code->block = gfc_get_code (EXEC_DO);
2004
2005 /* strides(idx) = _F._stride(array,dim=idx). */
2006 last_code->block->next = gfc_get_code (EXEC_ASSIGN);
2007 block = last_code->block->next;
2008
2009 block->expr1 = gfc_lval_expr_from_sym (strides);
2010 block->expr1->ref = gfc_get_ref ();
2011 block->expr1->ref->type = REF_ARRAY;
2012 block->expr1->ref->u.ar.type = AR_ELEMENT;
2013 block->expr1->ref->u.ar.dimen = 1;
2014 block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2015 block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
2016 block->expr1->ref->u.ar.as = strides->as;
2017
2018 block->expr2 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_STRIDE, "stride",
2019 gfc_current_locus, 2,
2020 gfc_lval_expr_from_sym (array),
2021 gfc_lval_expr_from_sym (idx));
2022
2023 /* sizes(idx) = sizes(idx-1) * size(array,dim=idx, kind=index_kind). */
2024 block->next = gfc_get_code (EXEC_ASSIGN);
2025 block = block->next;
2026
2027 /* sizes(idx) = ... */
2028 block->expr1 = gfc_lval_expr_from_sym (sizes);
2029 block->expr1->ref = gfc_get_ref ();
2030 block->expr1->ref->type = REF_ARRAY;
2031 block->expr1->ref->u.ar.type = AR_ELEMENT;
2032 block->expr1->ref->u.ar.dimen = 1;
2033 block->expr1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2034 block->expr1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
2035 block->expr1->ref->u.ar.as = sizes->as;
2036
2037 block->expr2 = gfc_get_expr ();
2038 block->expr2->expr_type = EXPR_OP;
2039 block->expr2->value.op.op = INTRINSIC_TIMES;
2040 block->expr2->where = gfc_current_locus;
2041
2042 /* sizes(idx-1). */
2043 block->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
2044 block->expr2->value.op.op1->ref = gfc_get_ref ();
2045 block->expr2->value.op.op1->ref->type = REF_ARRAY;
2046 block->expr2->value.op.op1->ref->u.ar.as = sizes->as;
2047 block->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
2048 block->expr2->value.op.op1->ref->u.ar.dimen = 1;
2049 block->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2050 block->expr2->value.op.op1->ref->u.ar.start[0] = gfc_get_expr ();
2051 block->expr2->value.op.op1->ref->u.ar.start[0]->expr_type = EXPR_OP;
2052 block->expr2->value.op.op1->ref->u.ar.start[0]->where = gfc_current_locus;
2053 block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
2054 block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1
2055 = gfc_lval_expr_from_sym (idx);
2056 block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op2
2057 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2058 block->expr2->value.op.op1->ref->u.ar.start[0]->ts
2059 = block->expr2->value.op.op1->ref->u.ar.start[0]->value.op.op1->ts;
2060
2061 /* size(array, dim=idx, kind=index_kind). */
2062 block->expr2->value.op.op2
2063 = gfc_build_intrinsic_call (sub_ns, GFC_ISYM_SIZE, "size",
2064 gfc_current_locus, 3,
2065 gfc_lval_expr_from_sym (array),
2066 gfc_lval_expr_from_sym (idx),
2067 gfc_get_int_expr (gfc_index_integer_kind,
2068 NULL,
2069 gfc_index_integer_kind));
2070 block->expr2->value.op.op2->ts.kind = gfc_index_integer_kind;
2071 block->expr2->ts = idx->ts;
2072
2073 /* if (strides (idx) /= sizes(idx-1)) is_contiguous = .false. */
2074 block->next = gfc_get_code (EXEC_IF);
2075 block = block->next;
2076
2077 block->block = gfc_get_code (EXEC_IF);
2078 block = block->block;
2079
2080 /* if condition: strides(idx) /= sizes(idx-1). */
2081 block->expr1 = gfc_get_expr ();
2082 block->expr1->ts.type = BT_LOGICAL;
2083 block->expr1->ts.kind = gfc_default_logical_kind;
2084 block->expr1->expr_type = EXPR_OP;
2085 block->expr1->where = gfc_current_locus;
2086 block->expr1->value.op.op = INTRINSIC_NE;
2087
2088 block->expr1->value.op.op1 = gfc_lval_expr_from_sym (strides);
2089 block->expr1->value.op.op1->ref = gfc_get_ref ();
2090 block->expr1->value.op.op1->ref->type = REF_ARRAY;
2091 block->expr1->value.op.op1->ref->u.ar.type = AR_ELEMENT;
2092 block->expr1->value.op.op1->ref->u.ar.dimen = 1;
2093 block->expr1->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2094 block->expr1->value.op.op1->ref->u.ar.start[0] = gfc_lval_expr_from_sym (idx);
2095 block->expr1->value.op.op1->ref->u.ar.as = strides->as;
2096
2097 block->expr1->value.op.op2 = gfc_lval_expr_from_sym (sizes);
2098 block->expr1->value.op.op2->ref = gfc_get_ref ();
2099 block->expr1->value.op.op2->ref->type = REF_ARRAY;
2100 block->expr1->value.op.op2->ref->u.ar.as = sizes->as;
2101 block->expr1->value.op.op2->ref->u.ar.type = AR_ELEMENT;
2102 block->expr1->value.op.op2->ref->u.ar.dimen = 1;
2103 block->expr1->value.op.op2->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2104 block->expr1->value.op.op2->ref->u.ar.start[0] = gfc_get_expr ();
2105 block->expr1->value.op.op2->ref->u.ar.start[0]->expr_type = EXPR_OP;
2106 block->expr1->value.op.op2->ref->u.ar.start[0]->where = gfc_current_locus;
2107 block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op = INTRINSIC_MINUS;
2108 block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1
2109 = gfc_lval_expr_from_sym (idx);
2110 block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op2
2111 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2112 block->expr1->value.op.op2->ref->u.ar.start[0]->ts
2113 = block->expr1->value.op.op2->ref->u.ar.start[0]->value.op.op1->ts;
2114
2115 /* if body: is_contiguous = .false. */
2116 block->next = gfc_get_code (EXEC_ASSIGN);
2117 block = block->next;
2118 block->expr1 = gfc_lval_expr_from_sym (is_contiguous);
2119 block->expr2 = gfc_get_logical_expr (gfc_default_logical_kind,
2120 &gfc_current_locus, false);
2121
2122 /* Obtain the size (number of elements) of "array" MINUS ONE,
2123 which is used in the scalarization. */
2124 gfc_get_symbol ("nelem", sub_ns, &nelem);
2125 nelem->ts.type = BT_INTEGER;
2126 nelem->ts.kind = gfc_index_integer_kind;
2127 nelem->attr.flavor = FL_VARIABLE;
2128 nelem->attr.artificial = 1;
2129 gfc_set_sym_referenced (nelem);
2130 gfc_commit_symbol (nelem);
2131
2132 /* nelem = sizes (rank) - 1. */
2133 last_code->next = gfc_get_code (EXEC_ASSIGN);
2134 last_code = last_code->next;
2135
2136 last_code->expr1 = gfc_lval_expr_from_sym (nelem);
2137
2138 last_code->expr2 = gfc_get_expr ();
2139 last_code->expr2->expr_type = EXPR_OP;
2140 last_code->expr2->value.op.op = INTRINSIC_MINUS;
2141 last_code->expr2->value.op.op2
2142 = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2143 last_code->expr2->ts = last_code->expr2->value.op.op2->ts;
2144 last_code->expr2->where = gfc_current_locus;
2145
2146 last_code->expr2->value.op.op1 = gfc_lval_expr_from_sym (sizes);
2147 last_code->expr2->value.op.op1->ref = gfc_get_ref ();
2148 last_code->expr2->value.op.op1->ref->type = REF_ARRAY;
2149 last_code->expr2->value.op.op1->ref->u.ar.type = AR_ELEMENT;
2150 last_code->expr2->value.op.op1->ref->u.ar.dimen = 1;
2151 last_code->expr2->value.op.op1->ref->u.ar.dimen_type[0] = DIMEN_ELEMENT;
2152 last_code->expr2->value.op.op1->ref->u.ar.start[0] = gfc_copy_expr (rank);
2153 last_code->expr2->value.op.op1->ref->u.ar.as = sizes->as;
2154
2155 /* Call final subroutines. We now generate code like:
2156 use iso_c_binding
2157 integer, pointer :: ptr
2158 type(c_ptr) :: cptr
2159 integer(c_intptr_t) :: i, addr
2160
2161 select case (rank (array))
2162 case (3)
2163 ! If needed, the array is packed
2164 call final_rank3 (array)
2165 case default:
2166 do i = 0, size (array)-1
2167 addr = transfer (c_loc (array), addr) + i * stride
2168 call c_f_pointer (transfer (addr, cptr), ptr)
2169 call elemental_final (ptr)
2170 end do
2171 end select */
2172
2173 if (derived->f2k_derived && derived->f2k_derived->finalizers)
2174 {
2175 gfc_finalizer *fini, *fini_elem = NULL;
2176
2177 gfc_get_symbol ("ptr1", sub_ns, &ptr);
2178 ptr->ts.type = BT_DERIVED;
2179 ptr->ts.u.derived = derived;
2180 ptr->attr.flavor = FL_VARIABLE;
2181 ptr->attr.pointer = 1;
2182 ptr->attr.artificial = 1;
2183 gfc_set_sym_referenced (ptr);
2184 gfc_commit_symbol (ptr);
2185
2186 fini = derived->f2k_derived->finalizers;
2187
2188 /* Assumed rank finalizers can be called directly. The call takes care
2189 of setting up the descriptor. resolve_finalizers has already checked
2190 that this is the only finalizer for this kind/type (F2018: C790). */
2191 if (fini->proc_tree && fini->proc_tree->n.sym->formal->sym->as
2192 && fini->proc_tree->n.sym->formal->sym->as->type == AS_ASSUMED_RANK)
2193 {
2194 last_code->next = gfc_get_code (EXEC_CALL);
2195 last_code->next->symtree = fini->proc_tree;
2196 last_code->next->resolved_sym = fini->proc_tree->n.sym;
2197 last_code->next->ext.actual = gfc_get_actual_arglist ();
2198 last_code->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
2199
2200 last_code = last_code->next;
2201 goto finish_assumed_rank;
2202 }
2203
2204 /* SELECT CASE (RANK (array)). */
2205 last_code->next = gfc_get_code (EXEC_SELECT);
2206 last_code = last_code->next;
2207 last_code->expr1 = gfc_copy_expr (rank);
2208 block = NULL;
2209
2210
2211 for (; fini; fini = fini->next)
2212 {
2213 gcc_assert (fini->proc_tree); /* Should have been set in gfc_resolve_finalizers. */
2214 if (fini->proc_tree->n.sym->attr.elemental)
2215 {
2216 fini_elem = fini;
2217 continue;
2218 }
2219
2220 /* CASE (fini_rank). */
2221 if (block)
2222 {
2223 block->block = gfc_get_code (EXEC_SELECT);
2224 block = block->block;
2225 }
2226 else
2227 {
2228 block = gfc_get_code (EXEC_SELECT);
2229 last_code->block = block;
2230 }
2231 block->ext.block.case_list = gfc_get_case ();
2232 block->ext.block.case_list->where = gfc_current_locus;
2233 if (fini->proc_tree->n.sym->formal->sym->attr.dimension)
2234 block->ext.block.case_list->low
2235 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
2236 fini->proc_tree->n.sym->formal->sym->as->rank);
2237 else
2238 block->ext.block.case_list->low
2239 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2240 block->ext.block.case_list->high
2241 = gfc_copy_expr (block->ext.block.case_list->low);
2242
2243 /* CALL fini_rank (array) - possibly with packing. */
2244 if (fini->proc_tree->n.sym->formal->sym->attr.dimension)
2245 finalizer_insert_packed_call (block, fini, array, byte_stride,
2246 idx, ptr, nelem, strides,
2247 sizes, idx2, offset, is_contiguous,
2248 rank, sub_ns);
2249 else
2250 {
2251 block->next = gfc_get_code (EXEC_CALL);
2252 block->next->symtree = fini->proc_tree;
2253 block->next->resolved_sym = fini->proc_tree->n.sym;
2254 block->next->ext.actual = gfc_get_actual_arglist ();
2255 block->next->ext.actual->expr = gfc_lval_expr_from_sym (array);
2256 }
2257 }
2258
2259 /* Elemental call - scalarized. */
2260 if (fini_elem)
2261 {
2262 /* CASE DEFAULT. */
2263 if (block)
2264 {
2265 block->block = gfc_get_code (EXEC_SELECT);
2266 block = block->block;
2267 }
2268 else
2269 {
2270 block = gfc_get_code (EXEC_SELECT);
2271 last_code->block = block;
2272 }
2273 block->ext.block.case_list = gfc_get_case ();
2274
2275 /* Create loop. */
2276 iter = gfc_get_iterator ();
2277 iter->var = gfc_lval_expr_from_sym (idx);
2278 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
2279 iter->end = gfc_lval_expr_from_sym (nelem);
2280 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2281 block->next = gfc_get_code (EXEC_DO);
2282 block = block->next;
2283 block->ext.iterator = iter;
2284 block->block = gfc_get_code (EXEC_DO);
2285
2286 /* Offset calculation. */
2287 block = finalization_get_offset (idx, idx2, offset, strides, sizes,
2288 byte_stride, rank, block->block,
2289 sub_ns);
2290
2291 /* Create code for
2292 CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
2293 + offset, c_ptr), ptr). */
2294 block->next
2295 = finalization_scalarizer (array, ptr,
2296 gfc_lval_expr_from_sym (offset),
2297 sub_ns);
2298 block = block->next;
2299
2300 /* CALL final_elemental (array). */
2301 block->next = gfc_get_code (EXEC_CALL);
2302 block = block->next;
2303 block->symtree = fini_elem->proc_tree;
2304 block->resolved_sym = fini_elem->proc_sym;
2305 block->ext.actual = gfc_get_actual_arglist ();
2306 block->ext.actual->expr = gfc_lval_expr_from_sym (ptr);
2307 }
2308 }
2309
2310 finish_assumed_rank:
2311
2312 /* Finalize and deallocate allocatable components. The same manual
2313 scalarization is used as above. */
2314
2315 if (finalizable_comp)
2316 {
2317 gfc_symbol *stat;
2318 gfc_code *block = NULL;
2319 gfc_expr *ptr_expr;
2320
2321 if (!ptr)
2322 {
2323 gfc_get_symbol ("ptr2", sub_ns, &ptr);
2324 ptr->ts.type = BT_DERIVED;
2325 ptr->ts.u.derived = derived;
2326 ptr->attr.flavor = FL_VARIABLE;
2327 ptr->attr.pointer = 1;
2328 ptr->attr.artificial = 1;
2329 gfc_set_sym_referenced (ptr);
2330 gfc_commit_symbol (ptr);
2331 }
2332
2333 gfc_get_symbol ("ignore", sub_ns, &stat);
2334 stat->attr.flavor = FL_VARIABLE;
2335 stat->attr.artificial = 1;
2336 stat->ts.type = BT_INTEGER;
2337 stat->ts.kind = gfc_default_integer_kind;
2338 gfc_set_sym_referenced (stat);
2339 gfc_commit_symbol (stat);
2340
2341 /* Create loop. */
2342 iter = gfc_get_iterator ();
2343 iter->var = gfc_lval_expr_from_sym (idx);
2344 iter->start = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
2345 iter->end = gfc_lval_expr_from_sym (nelem);
2346 iter->step = gfc_get_int_expr (gfc_index_integer_kind, NULL, 1);
2347 last_code->next = gfc_get_code (EXEC_DO);
2348 last_code = last_code->next;
2349 last_code->ext.iterator = iter;
2350 last_code->block = gfc_get_code (EXEC_DO);
2351
2352 /* Offset calculation. */
2353 block = finalization_get_offset (idx, idx2, offset, strides, sizes,
2354 byte_stride, rank, last_code->block,
2355 sub_ns);
2356
2357 /* Create code for
2358 CALL C_F_POINTER (TRANSFER (TRANSFER (C_LOC (array, cptr), c_intptr)
2359 + idx * stride, c_ptr), ptr). */
2360 block->next = finalization_scalarizer (array, ptr,
2361 gfc_lval_expr_from_sym(offset),
2362 sub_ns);
2363 block = block->next;
2364
2365 ptr_expr = gfc_lval_expr_from_sym (ptr);
2366 for (comp = derived->components; comp; comp = comp->next)
2367 {
2368 if (comp == derived->components && derived->attr.extension
2369 && ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
2370 continue;
2371
2372 finalize_component (ptr_expr, derived, comp, stat, fini_coarray,
2373 &block, sub_ns);
2374 if (!last_code->block->next)
2375 last_code->block->next = block;
2376 }
2377
2378 }
2379
2380 /* Call the finalizer of the ancestor. */
2381 if (ancestor_wrapper && ancestor_wrapper->expr_type != EXPR_NULL)
2382 {
2383 last_code->next = gfc_get_code (EXEC_CALL);
2384 last_code = last_code->next;
2385 last_code->symtree = ancestor_wrapper->symtree;
2386 last_code->resolved_sym = ancestor_wrapper->symtree->n.sym;
2387
2388 last_code->ext.actual = gfc_get_actual_arglist ();
2389 last_code->ext.actual->expr = gfc_lval_expr_from_sym (array);
2390 last_code->ext.actual->next = gfc_get_actual_arglist ();
2391 last_code->ext.actual->next->expr = gfc_lval_expr_from_sym (byte_stride);
2392 last_code->ext.actual->next->next = gfc_get_actual_arglist ();
2393 last_code->ext.actual->next->next->expr
2394 = gfc_lval_expr_from_sym (fini_coarray);
2395 }
2396
2397 gfc_free_expr (rank);
2398 vtab_final->initializer = gfc_lval_expr_from_sym (final);
2399 vtab_final->ts.interface = final;
2400 free (name);
2401 }
2402
2403
2404 /* Add procedure pointers for all type-bound procedures to a vtab. */
2405
2406 static void
2407 add_procs_to_declared_vtab (gfc_symbol *derived, gfc_symbol *vtype)
2408 {
2409 gfc_symbol* super_type;
2410
2411 super_type = gfc_get_derived_super_type (derived);
2412
2413 if (super_type && (super_type != derived))
2414 {
2415 /* Make sure that the PPCs appear in the same order as in the parent. */
2416 copy_vtab_proc_comps (super_type, vtype);
2417 /* Only needed to get the PPC initializers right. */
2418 add_procs_to_declared_vtab (super_type, vtype);
2419 }
2420
2421 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
2422 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_sym_root, vtype);
2423
2424 if (derived->f2k_derived && derived->f2k_derived->tb_uop_root)
2425 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_uop_root, vtype);
2426 }
2427
2428
2429 /* Find or generate the symbol for a derived type's vtab. */
2430
2431 gfc_symbol *
2432 gfc_find_derived_vtab (gfc_symbol *derived)
2433 {
2434 gfc_namespace *ns;
2435 gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
2436 gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
2437 gfc_gsymbol *gsym = NULL;
2438 gfc_symbol *dealloc = NULL, *arg = NULL;
2439
2440 if (derived->attr.pdt_template)
2441 return NULL;
2442
2443 /* Find the top-level namespace. */
2444 for (ns = gfc_current_ns; ns; ns = ns->parent)
2445 if (!ns->parent)
2446 break;
2447
2448 /* If the type is a class container, use the underlying derived type. */
2449 if (!derived->attr.unlimited_polymorphic && derived->attr.is_class)
2450 derived = gfc_get_derived_super_type (derived);
2451
2452 if (!derived)
2453 return NULL;
2454
2455 if (!derived->name)
2456 return NULL;
2457
2458 /* Find the gsymbol for the module of use associated derived types. */
2459 if ((derived->attr.use_assoc || derived->attr.used_in_submodule)
2460 && !derived->attr.vtype && !derived->attr.is_class)
2461 gsym = gfc_find_gsymbol (gfc_gsym_root, derived->module);
2462 else
2463 gsym = NULL;
2464
2465 /* Work in the gsymbol namespace if the top-level namespace is a module.
2466 This ensures that the vtable is unique, which is required since we use
2467 its address in SELECT TYPE. */
2468 if (gsym && gsym->ns && ns && ns->proc_name
2469 && ns->proc_name->attr.flavor == FL_MODULE)
2470 ns = gsym->ns;
2471
2472 if (ns)
2473 {
2474 char tname[GFC_MAX_SYMBOL_LEN+1];
2475 char *name;
2476
2477 get_unique_hashed_string (tname, derived);
2478 name = xasprintf ("__vtab_%s", tname);
2479
2480 /* Look for the vtab symbol in various namespaces. */
2481 if (gsym && gsym->ns)
2482 {
2483 gfc_find_symbol (name, gsym->ns, 0, &vtab);
2484 if (vtab)
2485 ns = gsym->ns;
2486 }
2487 if (vtab == NULL)
2488 gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
2489 if (vtab == NULL)
2490 gfc_find_symbol (name, ns, 0, &vtab);
2491 if (vtab == NULL)
2492 gfc_find_symbol (name, derived->ns, 0, &vtab);
2493
2494 if (vtab == NULL)
2495 {
2496 gfc_get_symbol (name, ns, &vtab);
2497 vtab->ts.type = BT_DERIVED;
2498 if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
2499 &gfc_current_locus))
2500 goto cleanup;
2501 vtab->attr.target = 1;
2502 vtab->attr.save = SAVE_IMPLICIT;
2503 vtab->attr.vtab = 1;
2504 vtab->attr.access = ACCESS_PUBLIC;
2505 vtab->attr.artificial = 1;
2506 gfc_set_sym_referenced (vtab);
2507 free (name);
2508 name = xasprintf ("__vtype_%s", tname);
2509
2510 gfc_find_symbol (name, ns, 0, &vtype);
2511 if (vtype == NULL)
2512 {
2513 gfc_component *c;
2514 gfc_symbol *parent = NULL, *parent_vtab = NULL;
2515
2516 gfc_get_symbol (name, ns, &vtype);
2517 if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
2518 &gfc_current_locus))
2519 goto cleanup;
2520 vtype->attr.access = ACCESS_PUBLIC;
2521 vtype->attr.vtype = 1;
2522 gfc_set_sym_referenced (vtype);
2523
2524 /* Add component '_hash'. */
2525 if (!gfc_add_component (vtype, "_hash", &c))
2526 goto cleanup;
2527 c->ts.type = BT_INTEGER;
2528 c->ts.kind = 4;
2529 c->attr.access = ACCESS_PRIVATE;
2530 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
2531 NULL, derived->hash_value);
2532
2533 /* Add component '_size'. */
2534 if (!gfc_add_component (vtype, "_size", &c))
2535 goto cleanup;
2536 c->ts.type = BT_INTEGER;
2537 c->ts.kind = gfc_size_kind;
2538 c->attr.access = ACCESS_PRIVATE;
2539 /* Remember the derived type in ts.u.derived,
2540 so that the correct initializer can be set later on
2541 (in gfc_conv_structure). */
2542 c->ts.u.derived = derived;
2543 c->initializer = gfc_get_int_expr (gfc_size_kind,
2544 NULL, 0);
2545
2546 /* Add component _extends. */
2547 if (!gfc_add_component (vtype, "_extends", &c))
2548 goto cleanup;
2549 c->attr.pointer = 1;
2550 c->attr.access = ACCESS_PRIVATE;
2551 if (!derived->attr.unlimited_polymorphic)
2552 parent = gfc_get_derived_super_type (derived);
2553 else
2554 parent = NULL;
2555
2556 if (parent)
2557 {
2558 parent_vtab = gfc_find_derived_vtab (parent);
2559 c->ts.type = BT_DERIVED;
2560 c->ts.u.derived = parent_vtab->ts.u.derived;
2561 c->initializer = gfc_get_expr ();
2562 c->initializer->expr_type = EXPR_VARIABLE;
2563 gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
2564 0, &c->initializer->symtree);
2565 }
2566 else
2567 {
2568 c->ts.type = BT_DERIVED;
2569 c->ts.u.derived = vtype;
2570 c->initializer = gfc_get_null_expr (NULL);
2571 }
2572
2573 if (!derived->attr.unlimited_polymorphic
2574 && derived->components == NULL
2575 && !derived->attr.zero_comp)
2576 {
2577 /* At this point an error must have occurred.
2578 Prevent further errors on the vtype components. */
2579 found_sym = vtab;
2580 goto have_vtype;
2581 }
2582
2583 /* Add component _def_init. */
2584 if (!gfc_add_component (vtype, "_def_init", &c))
2585 goto cleanup;
2586 c->attr.pointer = 1;
2587 c->attr.artificial = 1;
2588 c->attr.access = ACCESS_PRIVATE;
2589 c->ts.type = BT_DERIVED;
2590 c->ts.u.derived = derived;
2591 if (derived->attr.unlimited_polymorphic
2592 || derived->attr.abstract)
2593 c->initializer = gfc_get_null_expr (NULL);
2594 else
2595 {
2596 /* Construct default initialization variable. */
2597 free (name);
2598 name = xasprintf ("__def_init_%s", tname);
2599 gfc_get_symbol (name, ns, &def_init);
2600 def_init->attr.target = 1;
2601 def_init->attr.artificial = 1;
2602 def_init->attr.save = SAVE_IMPLICIT;
2603 def_init->attr.access = ACCESS_PUBLIC;
2604 def_init->attr.flavor = FL_VARIABLE;
2605 gfc_set_sym_referenced (def_init);
2606 def_init->ts.type = BT_DERIVED;
2607 def_init->ts.u.derived = derived;
2608 def_init->value = gfc_default_initializer (&def_init->ts);
2609
2610 c->initializer = gfc_lval_expr_from_sym (def_init);
2611 }
2612
2613 /* Add component _copy. */
2614 if (!gfc_add_component (vtype, "_copy", &c))
2615 goto cleanup;
2616 c->attr.proc_pointer = 1;
2617 c->attr.access = ACCESS_PRIVATE;
2618 c->attr.artificial = 1;
2619 c->tb = XCNEW (gfc_typebound_proc);
2620 c->tb->ppc = 1;
2621 if (derived->attr.unlimited_polymorphic
2622 || derived->attr.abstract)
2623 c->initializer = gfc_get_null_expr (NULL);
2624 else
2625 {
2626 /* Set up namespace. */
2627 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
2628 sub_ns->sibling = ns->contained;
2629 ns->contained = sub_ns;
2630 sub_ns->resolved = 1;
2631 /* Set up procedure symbol. */
2632 free (name);
2633 name = xasprintf ("__copy_%s", tname);
2634 gfc_get_symbol (name, sub_ns, &copy);
2635 sub_ns->proc_name = copy;
2636 copy->attr.flavor = FL_PROCEDURE;
2637 copy->attr.subroutine = 1;
2638 copy->attr.pure = 1;
2639 copy->attr.artificial = 1;
2640 copy->attr.if_source = IFSRC_DECL;
2641 /* This is elemental so that arrays are automatically
2642 treated correctly by the scalarizer. */
2643 copy->attr.elemental = 1;
2644 if (ns->proc_name->attr.flavor == FL_MODULE)
2645 copy->module = ns->proc_name->name;
2646 gfc_set_sym_referenced (copy);
2647 /* Set up formal arguments. */
2648 gfc_get_symbol ("src", sub_ns, &src);
2649 src->ts.type = BT_DERIVED;
2650 src->ts.u.derived = derived;
2651 src->attr.flavor = FL_VARIABLE;
2652 src->attr.dummy = 1;
2653 src->attr.artificial = 1;
2654 src->attr.intent = INTENT_IN;
2655 gfc_set_sym_referenced (src);
2656 copy->formal = gfc_get_formal_arglist ();
2657 copy->formal->sym = src;
2658 gfc_get_symbol ("dst", sub_ns, &dst);
2659 dst->ts.type = BT_DERIVED;
2660 dst->ts.u.derived = derived;
2661 dst->attr.flavor = FL_VARIABLE;
2662 dst->attr.dummy = 1;
2663 dst->attr.artificial = 1;
2664 dst->attr.intent = INTENT_INOUT;
2665 gfc_set_sym_referenced (dst);
2666 copy->formal->next = gfc_get_formal_arglist ();
2667 copy->formal->next->sym = dst;
2668 /* Set up code. */
2669 sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
2670 sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
2671 sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
2672 /* Set initializer. */
2673 c->initializer = gfc_lval_expr_from_sym (copy);
2674 c->ts.interface = copy;
2675 }
2676
2677 /* Add component _final, which contains a procedure pointer to
2678 a wrapper which handles both the freeing of allocatable
2679 components and the calls to finalization subroutines.
2680 Note: The actual wrapper function can only be generated
2681 at resolution time. */
2682 if (!gfc_add_component (vtype, "_final", &c))
2683 goto cleanup;
2684 c->attr.proc_pointer = 1;
2685 c->attr.access = ACCESS_PRIVATE;
2686 c->attr.artificial = 1;
2687 c->tb = XCNEW (gfc_typebound_proc);
2688 c->tb->ppc = 1;
2689 generate_finalization_wrapper (derived, ns, tname, c);
2690
2691 /* Add component _deallocate. */
2692 if (!gfc_add_component (vtype, "_deallocate", &c))
2693 goto cleanup;
2694 c->attr.proc_pointer = 1;
2695 c->attr.access = ACCESS_PRIVATE;
2696 c->attr.artificial = 1;
2697 c->tb = XCNEW (gfc_typebound_proc);
2698 c->tb->ppc = 1;
2699 if (derived->attr.unlimited_polymorphic || derived->attr.abstract
2700 || !derived->attr.recursive)
2701 c->initializer = gfc_get_null_expr (NULL);
2702 else
2703 {
2704 /* Set up namespace. */
2705 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
2706
2707 sub_ns->sibling = ns->contained;
2708 ns->contained = sub_ns;
2709 sub_ns->resolved = 1;
2710 /* Set up procedure symbol. */
2711 free (name);
2712 name = xasprintf ("__deallocate_%s", tname);
2713 gfc_get_symbol (name, sub_ns, &dealloc);
2714 sub_ns->proc_name = dealloc;
2715 dealloc->attr.flavor = FL_PROCEDURE;
2716 dealloc->attr.subroutine = 1;
2717 dealloc->attr.pure = 1;
2718 dealloc->attr.artificial = 1;
2719 dealloc->attr.if_source = IFSRC_DECL;
2720
2721 if (ns->proc_name->attr.flavor == FL_MODULE)
2722 dealloc->module = ns->proc_name->name;
2723 gfc_set_sym_referenced (dealloc);
2724 /* Set up formal argument. */
2725 gfc_get_symbol ("arg", sub_ns, &arg);
2726 arg->ts.type = BT_DERIVED;
2727 arg->ts.u.derived = derived;
2728 arg->attr.flavor = FL_VARIABLE;
2729 arg->attr.dummy = 1;
2730 arg->attr.artificial = 1;
2731 arg->attr.intent = INTENT_INOUT;
2732 arg->attr.dimension = 1;
2733 arg->attr.allocatable = 1;
2734 arg->as = gfc_get_array_spec();
2735 arg->as->type = AS_ASSUMED_SHAPE;
2736 arg->as->rank = 1;
2737 arg->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
2738 NULL, 1);
2739 gfc_set_sym_referenced (arg);
2740 dealloc->formal = gfc_get_formal_arglist ();
2741 dealloc->formal->sym = arg;
2742 /* Set up code. */
2743 sub_ns->code = gfc_get_code (EXEC_DEALLOCATE);
2744 sub_ns->code->ext.alloc.list = gfc_get_alloc ();
2745 sub_ns->code->ext.alloc.list->expr
2746 = gfc_lval_expr_from_sym (arg);
2747 /* Set initializer. */
2748 c->initializer = gfc_lval_expr_from_sym (dealloc);
2749 c->ts.interface = dealloc;
2750 }
2751
2752 /* Add procedure pointers for type-bound procedures. */
2753 if (!derived->attr.unlimited_polymorphic)
2754 add_procs_to_declared_vtab (derived, vtype);
2755 }
2756
2757 have_vtype:
2758 vtab->ts.u.derived = vtype;
2759 vtab->value = gfc_default_initializer (&vtab->ts);
2760 }
2761 free (name);
2762 }
2763
2764 found_sym = vtab;
2765
2766 cleanup:
2767 /* It is unexpected to have some symbols added at resolution or code
2768 generation time. We commit the changes in order to keep a clean state. */
2769 if (found_sym)
2770 {
2771 gfc_commit_symbol (vtab);
2772 if (vtype)
2773 gfc_commit_symbol (vtype);
2774 if (def_init)
2775 gfc_commit_symbol (def_init);
2776 if (copy)
2777 gfc_commit_symbol (copy);
2778 if (src)
2779 gfc_commit_symbol (src);
2780 if (dst)
2781 gfc_commit_symbol (dst);
2782 if (dealloc)
2783 gfc_commit_symbol (dealloc);
2784 if (arg)
2785 gfc_commit_symbol (arg);
2786 }
2787 else
2788 gfc_undo_symbols ();
2789
2790 return found_sym;
2791 }
2792
2793
2794 /* Check if a derived type is finalizable. That is the case if it
2795 (1) has a FINAL subroutine or
2796 (2) has a nonpointer nonallocatable component of finalizable type.
2797 If it is finalizable, return an expression containing the
2798 finalization wrapper. */
2799
2800 bool
2801 gfc_is_finalizable (gfc_symbol *derived, gfc_expr **final_expr)
2802 {
2803 gfc_symbol *vtab;
2804 gfc_component *c;
2805
2806 /* (1) Check for FINAL subroutines. */
2807 if (derived->f2k_derived && derived->f2k_derived->finalizers)
2808 goto yes;
2809
2810 /* (2) Check for components of finalizable type. */
2811 for (c = derived->components; c; c = c->next)
2812 if (c->ts.type == BT_DERIVED
2813 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
2814 && gfc_is_finalizable (c->ts.u.derived, NULL))
2815 goto yes;
2816
2817 return false;
2818
2819 yes:
2820 /* Make sure vtab is generated. */
2821 vtab = gfc_find_derived_vtab (derived);
2822 if (final_expr)
2823 {
2824 /* Return finalizer expression. */
2825 gfc_component *final;
2826 final = vtab->ts.u.derived->components->next->next->next->next->next;
2827 gcc_assert (strcmp (final->name, "_final") == 0);
2828 gcc_assert (final->initializer
2829 && final->initializer->expr_type != EXPR_NULL);
2830 *final_expr = final->initializer;
2831 }
2832 return true;
2833 }
2834
2835
2836 bool
2837 gfc_may_be_finalized (gfc_typespec ts)
2838 {
2839 return (ts.type == BT_CLASS || (ts.type == BT_DERIVED
2840 && ts.u.derived && gfc_is_finalizable (ts.u.derived, NULL)));
2841 }
2842
2843
2844 /* Find (or generate) the symbol for an intrinsic type's vtab. This is
2845 needed to support unlimited polymorphism. */
2846
2847 static gfc_symbol *
2848 find_intrinsic_vtab (gfc_typespec *ts)
2849 {
2850 gfc_namespace *ns;
2851 gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL;
2852 gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
2853
2854 /* Find the top-level namespace. */
2855 for (ns = gfc_current_ns; ns; ns = ns->parent)
2856 if (!ns->parent)
2857 break;
2858
2859 if (ns)
2860 {
2861 char tname[GFC_MAX_SYMBOL_LEN+1];
2862 char *name;
2863
2864 /* Encode all types as TYPENAME_KIND_ including especially character
2865 arrays, whose length is now consistently stored in the _len component
2866 of the class-variable. */
2867 sprintf (tname, "%s_%d_", gfc_basic_typename (ts->type), ts->kind);
2868 name = xasprintf ("__vtab_%s", tname);
2869
2870 /* Look for the vtab symbol in the top-level namespace only. */
2871 gfc_find_symbol (name, ns, 0, &vtab);
2872
2873 if (vtab == NULL)
2874 {
2875 gfc_get_symbol (name, ns, &vtab);
2876 vtab->ts.type = BT_DERIVED;
2877 if (!gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
2878 &gfc_current_locus))
2879 goto cleanup;
2880 vtab->attr.target = 1;
2881 vtab->attr.save = SAVE_IMPLICIT;
2882 vtab->attr.vtab = 1;
2883 vtab->attr.access = ACCESS_PUBLIC;
2884 gfc_set_sym_referenced (vtab);
2885 free (name);
2886 name = xasprintf ("__vtype_%s", tname);
2887
2888 gfc_find_symbol (name, ns, 0, &vtype);
2889 if (vtype == NULL)
2890 {
2891 gfc_component *c;
2892 int hash;
2893 gfc_namespace *sub_ns;
2894 gfc_namespace *contained;
2895 gfc_expr *e;
2896 size_t e_size;
2897
2898 gfc_get_symbol (name, ns, &vtype);
2899 if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
2900 &gfc_current_locus))
2901 goto cleanup;
2902 vtype->attr.access = ACCESS_PUBLIC;
2903 vtype->attr.vtype = 1;
2904 gfc_set_sym_referenced (vtype);
2905
2906 /* Add component '_hash'. */
2907 if (!gfc_add_component (vtype, "_hash", &c))
2908 goto cleanup;
2909 c->ts.type = BT_INTEGER;
2910 c->ts.kind = 4;
2911 c->attr.access = ACCESS_PRIVATE;
2912 hash = gfc_intrinsic_hash_value (ts);
2913 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
2914 NULL, hash);
2915
2916 /* Add component '_size'. */
2917 if (!gfc_add_component (vtype, "_size", &c))
2918 goto cleanup;
2919 c->ts.type = BT_INTEGER;
2920 c->ts.kind = gfc_size_kind;
2921 c->attr.access = ACCESS_PRIVATE;
2922
2923 /* Build a minimal expression to make use of
2924 target-memory.cc/gfc_element_size for 'size'. Special handling
2925 for character arrays, that are not constant sized: to support
2926 len (str) * kind, only the kind information is stored in the
2927 vtab. */
2928 e = gfc_get_expr ();
2929 e->ts = *ts;
2930 e->expr_type = EXPR_VARIABLE;
2931 if (ts->type == BT_CHARACTER)
2932 e_size = ts->kind;
2933 else
2934 gfc_element_size (e, &e_size);
2935 c->initializer = gfc_get_int_expr (gfc_size_kind,
2936 NULL,
2937 e_size);
2938 gfc_free_expr (e);
2939
2940 /* Add component _extends. */
2941 if (!gfc_add_component (vtype, "_extends", &c))
2942 goto cleanup;
2943 c->attr.pointer = 1;
2944 c->attr.access = ACCESS_PRIVATE;
2945 c->ts.type = BT_VOID;
2946 c->initializer = gfc_get_null_expr (NULL);
2947
2948 /* Add component _def_init. */
2949 if (!gfc_add_component (vtype, "_def_init", &c))
2950 goto cleanup;
2951 c->attr.pointer = 1;
2952 c->attr.access = ACCESS_PRIVATE;
2953 c->ts.type = BT_VOID;
2954 c->initializer = gfc_get_null_expr (NULL);
2955
2956 /* Add component _copy. */
2957 if (!gfc_add_component (vtype, "_copy", &c))
2958 goto cleanup;
2959 c->attr.proc_pointer = 1;
2960 c->attr.access = ACCESS_PRIVATE;
2961 c->attr.artificial = 1;
2962 c->tb = XCNEW (gfc_typebound_proc);
2963 c->tb->ppc = 1;
2964
2965 free (name);
2966 if (ts->type != BT_CHARACTER)
2967 name = xasprintf ("__copy_%s", tname);
2968 else
2969 {
2970 /* __copy is always the same for characters.
2971 Check to see if copy function already exists. */
2972 name = xasprintf ("__copy_character_%d", ts->kind);
2973 contained = ns->contained;
2974 for (; contained; contained = contained->sibling)
2975 if (contained->proc_name
2976 && strcmp (name, contained->proc_name->name) == 0)
2977 {
2978 copy = contained->proc_name;
2979 goto got_char_copy;
2980 }
2981 }
2982
2983 /* Set up namespace. */
2984 sub_ns = gfc_get_namespace (ns, 0);
2985 sub_ns->sibling = ns->contained;
2986 ns->contained = sub_ns;
2987 sub_ns->resolved = 1;
2988 /* Set up procedure symbol. */
2989 gfc_get_symbol (name, sub_ns, &copy);
2990 sub_ns->proc_name = copy;
2991 copy->attr.flavor = FL_PROCEDURE;
2992 copy->attr.subroutine = 1;
2993 copy->attr.pure = 1;
2994 copy->attr.if_source = IFSRC_DECL;
2995 /* This is elemental so that arrays are automatically
2996 treated correctly by the scalarizer. */
2997 copy->attr.elemental = 1;
2998 if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
2999 copy->module = ns->proc_name->name;
3000 gfc_set_sym_referenced (copy);
3001 /* Set up formal arguments. */
3002 gfc_get_symbol ("src", sub_ns, &src);
3003 src->ts.type = ts->type;
3004 src->ts.kind = ts->kind;
3005 src->attr.flavor = FL_VARIABLE;
3006 src->attr.dummy = 1;
3007 src->attr.intent = INTENT_IN;
3008 gfc_set_sym_referenced (src);
3009 copy->formal = gfc_get_formal_arglist ();
3010 copy->formal->sym = src;
3011 gfc_get_symbol ("dst", sub_ns, &dst);
3012 dst->ts.type = ts->type;
3013 dst->ts.kind = ts->kind;
3014 dst->attr.flavor = FL_VARIABLE;
3015 dst->attr.dummy = 1;
3016 dst->attr.intent = INTENT_INOUT;
3017 gfc_set_sym_referenced (dst);
3018 copy->formal->next = gfc_get_formal_arglist ();
3019 copy->formal->next->sym = dst;
3020 /* Set up code. */
3021 sub_ns->code = gfc_get_code (EXEC_INIT_ASSIGN);
3022 sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
3023 sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
3024 got_char_copy:
3025 /* Set initializer. */
3026 c->initializer = gfc_lval_expr_from_sym (copy);
3027 c->ts.interface = copy;
3028
3029 /* Add component _final. */
3030 if (!gfc_add_component (vtype, "_final", &c))
3031 goto cleanup;
3032 c->attr.proc_pointer = 1;
3033 c->attr.access = ACCESS_PRIVATE;
3034 c->attr.artificial = 1;
3035 c->tb = XCNEW (gfc_typebound_proc);
3036 c->tb->ppc = 1;
3037 c->initializer = gfc_get_null_expr (NULL);
3038 }
3039 vtab->ts.u.derived = vtype;
3040 vtab->value = gfc_default_initializer (&vtab->ts);
3041 }
3042 free (name);
3043 }
3044
3045 found_sym = vtab;
3046
3047 cleanup:
3048 /* It is unexpected to have some symbols added at resolution or code
3049 generation time. We commit the changes in order to keep a clean state. */
3050 if (found_sym)
3051 {
3052 gfc_commit_symbol (vtab);
3053 if (vtype)
3054 gfc_commit_symbol (vtype);
3055 if (copy)
3056 gfc_commit_symbol (copy);
3057 if (src)
3058 gfc_commit_symbol (src);
3059 if (dst)
3060 gfc_commit_symbol (dst);
3061 }
3062 else
3063 gfc_undo_symbols ();
3064
3065 return found_sym;
3066 }
3067
3068
3069 /* Find (or generate) a vtab for an arbitrary type (derived or intrinsic). */
3070
3071 gfc_symbol *
3072 gfc_find_vtab (gfc_typespec *ts)
3073 {
3074 switch (ts->type)
3075 {
3076 case BT_UNKNOWN:
3077 return NULL;
3078 case BT_DERIVED:
3079 return gfc_find_derived_vtab (ts->u.derived);
3080 case BT_CLASS:
3081 if (ts->u.derived->attr.is_class
3082 && ts->u.derived->components
3083 && ts->u.derived->components->ts.u.derived)
3084 return gfc_find_derived_vtab (ts->u.derived->components->ts.u.derived);
3085 else
3086 return NULL;
3087 default:
3088 return find_intrinsic_vtab (ts);
3089 }
3090 }
3091
3092
3093 /* General worker function to find either a type-bound procedure or a
3094 type-bound user operator. */
3095
3096 static gfc_symtree*
3097 find_typebound_proc_uop (gfc_symbol* derived, bool* t,
3098 const char* name, bool noaccess, bool uop,
3099 locus* where)
3100 {
3101 gfc_symtree* res;
3102 gfc_symtree* root;
3103
3104 /* Set default to failure. */
3105 if (t)
3106 *t = false;
3107
3108 if (derived->f2k_derived)
3109 /* Set correct symbol-root. */
3110 root = (uop ? derived->f2k_derived->tb_uop_root
3111 : derived->f2k_derived->tb_sym_root);
3112 else
3113 return NULL;
3114
3115 /* Try to find it in the current type's namespace. */
3116 res = gfc_find_symtree (root, name);
3117 if (res && res->n.tb && !res->n.tb->error)
3118 {
3119 /* We found one. */
3120 if (t)
3121 *t = true;
3122
3123 if (!noaccess && derived->attr.use_assoc
3124 && res->n.tb->access == ACCESS_PRIVATE)
3125 {
3126 if (where)
3127 gfc_error ("%qs of %qs is PRIVATE at %L",
3128 name, derived->name, where);
3129 if (t)
3130 *t = false;
3131 }
3132
3133 return res;
3134 }
3135
3136 /* Otherwise, recurse on parent type if derived is an extension. */
3137 if (derived->attr.extension)
3138 {
3139 gfc_symbol* super_type;
3140 super_type = gfc_get_derived_super_type (derived);
3141 gcc_assert (super_type);
3142
3143 return find_typebound_proc_uop (super_type, t, name,
3144 noaccess, uop, where);
3145 }
3146
3147 /* Nothing found. */
3148 return NULL;
3149 }
3150
3151
3152 /* Find a type-bound procedure or user operator by name for a derived-type
3153 (looking recursively through the super-types). */
3154
3155 gfc_symtree*
3156 gfc_find_typebound_proc (gfc_symbol* derived, bool* t,
3157 const char* name, bool noaccess, locus* where)
3158 {
3159 return find_typebound_proc_uop (derived, t, name, noaccess, false, where);
3160 }
3161
3162 gfc_symtree*
3163 gfc_find_typebound_user_op (gfc_symbol* derived, bool* t,
3164 const char* name, bool noaccess, locus* where)
3165 {
3166 return find_typebound_proc_uop (derived, t, name, noaccess, true, where);
3167 }
3168
3169
3170 /* Find a type-bound intrinsic operator looking recursively through the
3171 super-type hierarchy. */
3172
3173 gfc_typebound_proc*
3174 gfc_find_typebound_intrinsic_op (gfc_symbol* derived, bool* t,
3175 gfc_intrinsic_op op, bool noaccess,
3176 locus* where)
3177 {
3178 gfc_typebound_proc* res;
3179
3180 /* Set default to failure. */
3181 if (t)
3182 *t = false;
3183
3184 /* Try to find it in the current type's namespace. */
3185 if (derived->f2k_derived)
3186 res = derived->f2k_derived->tb_op[op];
3187 else
3188 res = NULL;
3189
3190 /* Check access. */
3191 if (res && !res->error)
3192 {
3193 /* We found one. */
3194 if (t)
3195 *t = true;
3196
3197 if (!noaccess && derived->attr.use_assoc
3198 && res->access == ACCESS_PRIVATE)
3199 {
3200 if (where)
3201 gfc_error ("%qs of %qs is PRIVATE at %L",
3202 gfc_op2string (op), derived->name, where);
3203 if (t)
3204 *t = false;
3205 }
3206
3207 return res;
3208 }
3209
3210 /* Otherwise, recurse on parent type if derived is an extension. */
3211 if (derived->attr.extension)
3212 {
3213 gfc_symbol* super_type;
3214 super_type = gfc_get_derived_super_type (derived);
3215 gcc_assert (super_type);
3216
3217 return gfc_find_typebound_intrinsic_op (super_type, t, op,
3218 noaccess, where);
3219 }
3220
3221 /* Nothing found. */
3222 return NULL;
3223 }
3224
3225
3226 /* Get a typebound-procedure symtree or create and insert it if not yet
3227 present. This is like a very simplified version of gfc_get_sym_tree for
3228 tbp-symtrees rather than regular ones. */
3229
3230 gfc_symtree*
3231 gfc_get_tbp_symtree (gfc_symtree **root, const char *name)
3232 {
3233 gfc_symtree *result = gfc_find_symtree (*root, name);
3234 return result ? result : gfc_new_symtree (root, name);
3235 }