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