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