1 /* Implementation of Fortran 2003 Polymorphism.
2 Copyright (C) 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Paul Richard Thomas <pault@gcc.gnu.org>
5 and Janus Weil <janus@gcc.gnu.org>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* class.c -- This file contains the front end functions needed to service
25 the implementation of Fortran 2003 polymorphism and other
26 object-oriented features. */
29 /* Outline of the internal representation:
31 Each CLASS variable is encapsulated by a class container, which is a
32 structure with two fields:
33 * _data: A pointer to the actual data of the variable. This field has the
34 declared type of the class variable and its attributes
35 (pointer/allocatable/dimension/...).
36 * _vptr: A pointer to the vtable entry (see below) of the dynamic type.
38 For each derived type we set up a "vtable" entry, i.e. a structure with the
40 * _hash: A hash value serving as a unique identifier for this type.
41 * _size: The size in bytes of the derived type.
42 * _extends: A pointer to the vtable entry of the parent derived type.
43 * _def_init: A pointer to a default initialized variable of this type.
44 * _copy: A procedure pointer to a copying procedure.
45 After these follow procedure pointer components for the specific
46 type-bound procedures. */
52 #include "constructor.h"
55 /* Inserts a derived type component reference in a data reference chain.
56 TS: base type of the ref chain so far, in which we will pick the component
57 REF: the address of the GFC_REF pointer to update
58 NAME: name of the component to insert
59 Note that component insertion makes sense only if we are at the end of
60 the chain (*REF == NULL) or if we are adding a missing "_data" component
61 to access the actual contents of a class object. */
64 insert_component_ref (gfc_typespec *ts, gfc_ref **ref, const char * const name)
69 gcc_assert (ts->type == BT_DERIVED || ts->type == BT_CLASS);
70 type_sym = ts->u.derived;
72 new_ref = gfc_get_ref ();
73 new_ref->type = REF_COMPONENT;
75 new_ref->u.c.sym = type_sym;
76 new_ref->u.c.component = gfc_find_component (type_sym, name, true, true);
77 gcc_assert (new_ref->u.c.component);
83 /* We need to update the base type in the trailing reference chain to
84 that of the new component. */
86 gcc_assert (strcmp (name, "_data") == 0);
88 if (new_ref->next->type == REF_COMPONENT)
90 else if (new_ref->next->type == REF_ARRAY
91 && new_ref->next->next
92 && new_ref->next->next->type == REF_COMPONENT)
93 next = new_ref->next->next;
97 gcc_assert (new_ref->u.c.component->ts.type == BT_CLASS
98 || new_ref->u.c.component->ts.type == BT_DERIVED);
99 next->u.c.sym = new_ref->u.c.component->ts.u.derived;
107 /* Tells whether we need to add a "_data" reference to access REF subobject
108 from an object of type TS. If FIRST_REF_IN_CHAIN is set, then the base
109 object accessed by REF is a variable; in other words it is a full object,
113 class_data_ref_missing (gfc_typespec *ts, gfc_ref *ref, bool first_ref_in_chain)
115 /* Only class containers may need the "_data" reference. */
116 if (ts->type != BT_CLASS)
119 /* Accessing a class container with an array reference is certainly wrong. */
120 if (ref->type != REF_COMPONENT)
123 /* Accessing the class container's fields is fine. */
124 if (ref->u.c.component->name[0] == '_')
127 /* At this point we have a class container with a non class container's field
128 component reference. We don't want to add the "_data" component if we are
129 at the first reference and the symbol's type is an extended derived type.
130 In that case, conv_parent_component_references will do the right thing so
131 it is not absolutely necessary. Omitting it prevents a regression (see
132 class_41.f03) in the interface mapping mechanism. When evaluating string
133 lengths depending on dummy arguments, we create a fake symbol with a type
134 equal to that of the dummy type. However, because of type extension,
135 the backend type (corresponding to the actual argument) can have a
136 different (extended) type. Adding the "_data" component explicitly, using
137 the base type, confuses the gfc_conv_component_ref code which deals with
138 the extended type. */
139 if (first_ref_in_chain && ts->u.derived->attr.extension)
142 /* We have a class container with a non class container's field component
143 reference that doesn't fall into the above. */
148 /* Browse through a data reference chain and add the missing "_data" references
149 when a subobject of a class object is accessed without it.
150 Note that it doesn't add the "_data" reference when the class container
151 is the last element in the reference chain. */
154 gfc_fix_class_refs (gfc_expr *e)
159 if ((e->expr_type != EXPR_VARIABLE
160 && e->expr_type != EXPR_FUNCTION)
161 || (e->expr_type == EXPR_FUNCTION
162 && e->value.function.isym != NULL))
165 if (e->expr_type == EXPR_VARIABLE)
166 ts = &e->symtree->n.sym->ts;
171 gcc_assert (e->expr_type == EXPR_FUNCTION);
172 if (e->value.function.esym != NULL)
173 func = e->value.function.esym;
175 func = e->symtree->n.sym;
177 if (func->result != NULL)
178 ts = &func->result->ts;
183 for (ref = &e->ref; *ref != NULL; ref = &(*ref)->next)
185 if (class_data_ref_missing (ts, *ref, ref == &e->ref))
186 insert_component_ref (ts, ref, "_data");
188 if ((*ref)->type == REF_COMPONENT)
189 ts = &(*ref)->u.c.component->ts;
194 /* Insert a reference to the component of the given name.
195 Only to be used with CLASS containers and vtables. */
198 gfc_add_component_ref (gfc_expr *e, const char *name)
200 gfc_ref **tail = &(e->ref);
201 gfc_ref *next = NULL;
202 gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
203 while (*tail != NULL)
205 if ((*tail)->type == REF_COMPONENT)
207 if (strcmp ((*tail)->u.c.component->name, "_data") == 0
209 && (*tail)->next->type == REF_ARRAY
210 && (*tail)->next->next == NULL)
212 derived = (*tail)->u.c.component->ts.u.derived;
214 if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
216 tail = &((*tail)->next);
218 if (*tail != NULL && strcmp (name, "_data") == 0)
220 (*tail) = gfc_get_ref();
221 (*tail)->next = next;
222 (*tail)->type = REF_COMPONENT;
223 (*tail)->u.c.sym = derived;
224 (*tail)->u.c.component = gfc_find_component (derived, name, true, true);
225 gcc_assert((*tail)->u.c.component);
227 e->ts = (*tail)->u.c.component->ts;
231 /* This is used to add both the _data component reference and an array
232 reference to class expressions. Used in translation of intrinsic
233 array inquiry functions. */
236 gfc_add_class_array_ref (gfc_expr *e)
238 int rank = CLASS_DATA (e)->as->rank;
239 gfc_array_spec *as = CLASS_DATA (e)->as;
241 gfc_add_component_ref (e, "_data");
243 for (ref = e->ref; ref; ref = ref->next)
246 if (ref->type != REF_ARRAY)
248 ref->next = gfc_get_ref ();
250 ref->type = REF_ARRAY;
251 ref->u.ar.type = AR_FULL;
257 /* Unfortunately, class array expressions can appear in various conditions;
258 with and without both _data component and an arrayspec. This function
259 deals with that variability. The previous reference to 'ref' is to a
263 class_array_ref_detected (gfc_ref *ref, bool *full_array)
265 bool no_data = false;
266 bool with_data = false;
268 /* An array reference with no _data component. */
269 if (ref && ref->type == REF_ARRAY
271 && ref->u.ar.type != AR_ELEMENT)
274 *full_array = ref->u.ar.type == AR_FULL;
278 /* Cover cases where _data appears, with or without an array ref. */
279 if (ref && ref->type == REF_COMPONENT
280 && strcmp (ref->u.c.component->name, "_data") == 0)
288 else if (ref->next && ref->next->type == REF_ARRAY
290 && ref->type == REF_COMPONENT
291 && ref->next->type == REF_ARRAY
292 && ref->next->u.ar.type != AR_ELEMENT)
296 *full_array = ref->next->u.ar.type == AR_FULL;
300 return no_data || with_data;
304 /* Returns true if the expression contains a reference to a class
305 array. Notice that class array elements return false. */
308 gfc_is_class_array_ref (gfc_expr *e, bool *full_array)
318 /* Is this a class array object? ie. Is the symbol of type class? */
320 && e->symtree->n.sym->ts.type == BT_CLASS
321 && CLASS_DATA (e->symtree->n.sym)
322 && CLASS_DATA (e->symtree->n.sym)->attr.dimension
323 && class_array_ref_detected (e->ref, full_array))
326 /* Or is this a class array component reference? */
327 for (ref = e->ref; ref; ref = ref->next)
329 if (ref->type == REF_COMPONENT
330 && ref->u.c.component->ts.type == BT_CLASS
331 && CLASS_DATA (ref->u.c.component)->attr.dimension
332 && class_array_ref_detected (ref->next, full_array))
340 /* Returns true if the expression is a reference to a class
341 scalar. This function is necessary because such expressions
342 can be dressed with a reference to the _data component and so
343 have a type other than BT_CLASS. */
346 gfc_is_class_scalar_expr (gfc_expr *e)
353 /* Is this a class object? */
355 && e->symtree->n.sym->ts.type == BT_CLASS
356 && CLASS_DATA (e->symtree->n.sym)
357 && !CLASS_DATA (e->symtree->n.sym)->attr.dimension
359 || (strcmp (e->ref->u.c.component->name, "_data") == 0
360 && e->ref->next == NULL)))
363 /* Or is the final reference BT_CLASS or _data? */
364 for (ref = e->ref; ref; ref = ref->next)
366 if (ref->type == REF_COMPONENT
367 && ref->u.c.component->ts.type == BT_CLASS
368 && CLASS_DATA (ref->u.c.component)
369 && !CLASS_DATA (ref->u.c.component)->attr.dimension
370 && (ref->next == NULL
371 || (strcmp (ref->next->u.c.component->name, "_data") == 0
372 && ref->next->next == NULL)))
380 /* Build a NULL initializer for CLASS pointers,
381 initializing the _data component to NULL and
382 the _vptr component to the declared type. */
385 gfc_class_null_initializer (gfc_typespec *ts)
390 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
391 &ts->u.derived->declared_at);
394 for (comp = ts->u.derived->components; comp; comp = comp->next)
396 gfc_constructor *ctor = gfc_constructor_get();
397 if (strcmp (comp->name, "_vptr") == 0)
398 ctor->expr = gfc_lval_expr_from_sym (gfc_find_derived_vtab (ts->u.derived));
400 ctor->expr = gfc_get_null_expr (NULL);
401 gfc_constructor_append (&init->value.constructor, ctor);
408 /* Create a unique string identifier for a derived type, composed of its name
409 and module name. This is used to construct unique names for the class
410 containers and vtab symbols. */
413 get_unique_type_string (char *string, gfc_symbol *derived)
415 char dt_name[GFC_MAX_SYMBOL_LEN+1];
416 sprintf (dt_name, "%s", derived->name);
417 dt_name[0] = TOUPPER (dt_name[0]);
419 sprintf (string, "%s_%s", derived->module, dt_name);
420 else if (derived->ns->proc_name)
421 sprintf (string, "%s_%s", derived->ns->proc_name->name, dt_name);
423 sprintf (string, "_%s", dt_name);
427 /* A relative of 'get_unique_type_string' which makes sure the generated
428 string will not be too long (replacing it by a hash string if needed). */
431 get_unique_hashed_string (char *string, gfc_symbol *derived)
433 char tmp[2*GFC_MAX_SYMBOL_LEN+2];
434 get_unique_type_string (&tmp[0], derived);
435 /* If string is too long, use hash value in hex representation (allow for
436 extra decoration, cf. gfc_build_class_symbol & gfc_find_derived_vtab).
437 We need space to for 15 characters "__class_" + symbol name + "_%d_%da",
438 where %d is the (co)rank which can be up to n = 15. */
439 if (strlen (tmp) > GFC_MAX_SYMBOL_LEN - 15)
441 int h = gfc_hash_value (derived);
442 sprintf (string, "%X", h);
445 strcpy (string, tmp);
449 /* Assign a hash value for a derived type. The algorithm is that of SDBM. */
452 gfc_hash_value (gfc_symbol *sym)
454 unsigned int hash = 0;
455 char c[2*(GFC_MAX_SYMBOL_LEN+1)];
458 get_unique_type_string (&c[0], sym);
461 for (i = 0; i < len; i++)
462 hash = (hash << 6) + (hash << 16) - hash + c[i];
464 /* Return the hash but take the modulus for the sake of module read,
465 even though this slightly increases the chance of collision. */
466 return (hash % 100000000);
470 /* Build a polymorphic CLASS entity, using the symbol that comes from
471 build_sym. A CLASS entity is represented by an encapsulating type,
472 which contains the declared type as '_data' component, plus a pointer
473 component '_vptr' which determines the dynamic type. */
476 gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
477 gfc_array_spec **as, bool delayed_vtab)
479 char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
484 if (as && *as && (*as)->type == AS_ASSUMED_SIZE)
486 gfc_error ("Assumed size polymorphic objects or components, such "
487 "as that at %C, have not yet been implemented");
492 /* Class container has already been built. */
495 attr->class_ok = attr->dummy || attr->pointer || attr->allocatable
496 || attr->select_type_temporary;
499 /* We can not build the class container yet. */
502 /* Determine the name of the encapsulating type. */
503 get_unique_hashed_string (tname, ts->u.derived);
504 if ((*as) && attr->allocatable)
505 sprintf (name, "__class_%s_%d_%da", tname, (*as)->rank, (*as)->corank);
507 sprintf (name, "__class_%s_%d_%d", tname, (*as)->rank, (*as)->corank);
508 else if (attr->pointer)
509 sprintf (name, "__class_%s_p", tname);
510 else if (attr->allocatable)
511 sprintf (name, "__class_%s_a", tname);
513 sprintf (name, "__class_%s", tname);
515 gfc_find_symbol (name, ts->u.derived->ns, 0, &fclass);
519 /* If not there, create a new symbol. */
520 fclass = gfc_new_symbol (name, ts->u.derived->ns);
521 st = gfc_new_symtree (&ts->u.derived->ns->sym_root, name);
523 gfc_set_sym_referenced (fclass);
525 fclass->ts.type = BT_UNKNOWN;
526 fclass->attr.abstract = ts->u.derived->attr.abstract;
527 if (ts->u.derived->f2k_derived)
528 fclass->f2k_derived = gfc_get_namespace (NULL, 0);
529 if (gfc_add_flavor (&fclass->attr, FL_DERIVED,
530 NULL, &gfc_current_locus) == FAILURE)
533 /* Add component '_data'. */
534 if (gfc_add_component (fclass, "_data", &c) == FAILURE)
537 c->ts.type = BT_DERIVED;
538 c->attr.access = ACCESS_PRIVATE;
539 c->ts.u.derived = ts->u.derived;
540 c->attr.class_pointer = attr->pointer;
541 c->attr.pointer = attr->pointer || (attr->dummy && !attr->allocatable)
542 || attr->select_type_temporary;
543 c->attr.allocatable = attr->allocatable;
544 c->attr.dimension = attr->dimension;
545 c->attr.codimension = attr->codimension;
546 c->attr.abstract = ts->u.derived->attr.abstract;
548 c->initializer = NULL;
550 /* Add component '_vptr'. */
551 if (gfc_add_component (fclass, "_vptr", &c) == FAILURE)
553 c->ts.type = BT_DERIVED;
555 c->ts.u.derived = NULL;
558 vtab = gfc_find_derived_vtab (ts->u.derived);
560 c->ts.u.derived = vtab->ts.u.derived;
562 c->attr.access = ACCESS_PRIVATE;
565 else if (!fclass->f2k_derived)
566 fclass->f2k_derived = gfc_get_namespace (NULL, 0);
568 /* Since the extension field is 8 bit wide, we can only have
569 up to 255 extension levels. */
570 if (ts->u.derived->attr.extension == 255)
572 gfc_error ("Maximum extension level reached with type '%s' at %L",
573 ts->u.derived->name, &ts->u.derived->declared_at);
577 fclass->attr.extension = ts->u.derived->attr.extension + 1;
578 fclass->attr.alloc_comp = ts->u.derived->attr.alloc_comp;
579 fclass->attr.is_class = 1;
580 ts->u.derived = fclass;
581 attr->allocatable = attr->pointer = attr->dimension = attr->codimension = 0;
587 /* Add a procedure pointer component to the vtype
588 to represent a specific type-bound procedure. */
591 add_proc_comp (gfc_symbol *vtype, const char *name, gfc_typebound_proc *tb)
595 if (tb->non_overridable)
598 c = gfc_find_component (vtype, name, true, true);
602 /* Add procedure component. */
603 if (gfc_add_component (vtype, name, &c) == FAILURE)
607 c->tb = XCNEW (gfc_typebound_proc);
610 c->attr.procedure = 1;
611 c->attr.proc_pointer = 1;
612 c->attr.flavor = FL_PROCEDURE;
613 c->attr.access = ACCESS_PRIVATE;
614 c->attr.external = 1;
616 c->attr.if_source = IFSRC_IFBODY;
618 else if (c->attr.proc_pointer && c->tb)
626 c->ts.interface = tb->u.specific->n.sym;
628 c->initializer = gfc_get_variable_expr (tb->u.specific);
633 /* Add all specific type-bound procedures in the symtree 'st' to a vtype. */
636 add_procs_to_declared_vtab1 (gfc_symtree *st, gfc_symbol *vtype)
642 add_procs_to_declared_vtab1 (st->left, vtype);
645 add_procs_to_declared_vtab1 (st->right, vtype);
647 if (st->n.tb && !st->n.tb->error
648 && !st->n.tb->is_generic && st->n.tb->u.specific)
649 add_proc_comp (vtype, st->name, st->n.tb);
653 /* Copy procedure pointers components from the parent type. */
656 copy_vtab_proc_comps (gfc_symbol *declared, gfc_symbol *vtype)
661 vtab = gfc_find_derived_vtab (declared);
663 for (cmp = vtab->ts.u.derived->components; cmp; cmp = cmp->next)
665 if (gfc_find_component (vtype, cmp->name, true, true))
668 add_proc_comp (vtype, cmp->name, cmp->tb);
673 /* Add procedure pointers for all type-bound procedures to a vtab. */
676 add_procs_to_declared_vtab (gfc_symbol *derived, gfc_symbol *vtype)
678 gfc_symbol* super_type;
680 super_type = gfc_get_derived_super_type (derived);
682 if (super_type && (super_type != derived))
684 /* Make sure that the PPCs appear in the same order as in the parent. */
685 copy_vtab_proc_comps (super_type, vtype);
686 /* Only needed to get the PPC initializers right. */
687 add_procs_to_declared_vtab (super_type, vtype);
690 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
691 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_sym_root, vtype);
693 if (derived->f2k_derived && derived->f2k_derived->tb_uop_root)
694 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_uop_root, vtype);
698 /* Find (or generate) the symbol for a derived type's vtab. */
701 gfc_find_derived_vtab (gfc_symbol *derived)
704 gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
705 gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
707 /* Find the top-level namespace (MODULE or PROGRAM). */
708 for (ns = gfc_current_ns; ns; ns = ns->parent)
712 /* If the type is a class container, use the underlying derived type. */
713 if (derived->attr.is_class)
714 derived = gfc_get_derived_super_type (derived);
718 char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
720 get_unique_hashed_string (tname, derived);
721 sprintf (name, "__vtab_%s", tname);
723 /* Look for the vtab symbol in various namespaces. */
724 gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
726 gfc_find_symbol (name, ns, 0, &vtab);
728 gfc_find_symbol (name, derived->ns, 0, &vtab);
732 gfc_get_symbol (name, ns, &vtab);
733 vtab->ts.type = BT_DERIVED;
734 if (gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
735 &gfc_current_locus) == FAILURE)
737 vtab->attr.target = 1;
738 vtab->attr.save = SAVE_IMPLICIT;
740 vtab->attr.access = ACCESS_PUBLIC;
741 gfc_set_sym_referenced (vtab);
742 sprintf (name, "__vtype_%s", tname);
744 gfc_find_symbol (name, ns, 0, &vtype);
748 gfc_symbol *parent = NULL, *parent_vtab = NULL;
750 gfc_get_symbol (name, ns, &vtype);
751 if (gfc_add_flavor (&vtype->attr, FL_DERIVED,
752 NULL, &gfc_current_locus) == FAILURE)
754 vtype->attr.access = ACCESS_PUBLIC;
755 vtype->attr.vtype = 1;
756 gfc_set_sym_referenced (vtype);
758 /* Add component '_hash'. */
759 if (gfc_add_component (vtype, "_hash", &c) == FAILURE)
761 c->ts.type = BT_INTEGER;
763 c->attr.access = ACCESS_PRIVATE;
764 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
765 NULL, derived->hash_value);
767 /* Add component '_size'. */
768 if (gfc_add_component (vtype, "_size", &c) == FAILURE)
770 c->ts.type = BT_INTEGER;
772 c->attr.access = ACCESS_PRIVATE;
773 /* Remember the derived type in ts.u.derived,
774 so that the correct initializer can be set later on
775 (in gfc_conv_structure). */
776 c->ts.u.derived = derived;
777 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
780 /* Add component _extends. */
781 if (gfc_add_component (vtype, "_extends", &c) == FAILURE)
784 c->attr.access = ACCESS_PRIVATE;
785 parent = gfc_get_derived_super_type (derived);
788 parent_vtab = gfc_find_derived_vtab (parent);
789 c->ts.type = BT_DERIVED;
790 c->ts.u.derived = parent_vtab->ts.u.derived;
791 c->initializer = gfc_get_expr ();
792 c->initializer->expr_type = EXPR_VARIABLE;
793 gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
794 0, &c->initializer->symtree);
798 c->ts.type = BT_DERIVED;
799 c->ts.u.derived = vtype;
800 c->initializer = gfc_get_null_expr (NULL);
803 if (derived->components == NULL && !derived->attr.zero_comp)
805 /* At this point an error must have occurred.
806 Prevent further errors on the vtype components. */
811 /* Add component _def_init. */
812 if (gfc_add_component (vtype, "_def_init", &c) == FAILURE)
815 c->attr.access = ACCESS_PRIVATE;
816 c->ts.type = BT_DERIVED;
817 c->ts.u.derived = derived;
818 if (derived->attr.abstract)
819 c->initializer = gfc_get_null_expr (NULL);
822 /* Construct default initialization variable. */
823 sprintf (name, "__def_init_%s", tname);
824 gfc_get_symbol (name, ns, &def_init);
825 def_init->attr.target = 1;
826 def_init->attr.save = SAVE_IMPLICIT;
827 def_init->attr.access = ACCESS_PUBLIC;
828 def_init->attr.flavor = FL_VARIABLE;
829 gfc_set_sym_referenced (def_init);
830 def_init->ts.type = BT_DERIVED;
831 def_init->ts.u.derived = derived;
832 def_init->value = gfc_default_initializer (&def_init->ts);
834 c->initializer = gfc_lval_expr_from_sym (def_init);
837 /* Add component _copy. */
838 if (gfc_add_component (vtype, "_copy", &c) == FAILURE)
840 c->attr.proc_pointer = 1;
841 c->attr.access = ACCESS_PRIVATE;
842 c->tb = XCNEW (gfc_typebound_proc);
844 if (derived->attr.abstract)
845 c->initializer = gfc_get_null_expr (NULL);
848 /* Set up namespace. */
849 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
850 sub_ns->sibling = ns->contained;
851 ns->contained = sub_ns;
852 sub_ns->resolved = 1;
853 /* Set up procedure symbol. */
854 sprintf (name, "__copy_%s", tname);
855 gfc_get_symbol (name, sub_ns, ©);
856 sub_ns->proc_name = copy;
857 copy->attr.flavor = FL_PROCEDURE;
858 copy->attr.subroutine = 1;
860 copy->attr.if_source = IFSRC_DECL;
861 /* This is elemental so that arrays are automatically
862 treated correctly by the scalarizer. */
863 copy->attr.elemental = 1;
864 if (ns->proc_name->attr.flavor == FL_MODULE)
865 copy->module = ns->proc_name->name;
866 gfc_set_sym_referenced (copy);
867 /* Set up formal arguments. */
868 gfc_get_symbol ("src", sub_ns, &src);
869 src->ts.type = BT_DERIVED;
870 src->ts.u.derived = derived;
871 src->attr.flavor = FL_VARIABLE;
873 src->attr.intent = INTENT_IN;
874 gfc_set_sym_referenced (src);
875 copy->formal = gfc_get_formal_arglist ();
876 copy->formal->sym = src;
877 gfc_get_symbol ("dst", sub_ns, &dst);
878 dst->ts.type = BT_DERIVED;
879 dst->ts.u.derived = derived;
880 dst->attr.flavor = FL_VARIABLE;
882 dst->attr.intent = INTENT_OUT;
883 gfc_set_sym_referenced (dst);
884 copy->formal->next = gfc_get_formal_arglist ();
885 copy->formal->next->sym = dst;
887 sub_ns->code = gfc_get_code ();
888 sub_ns->code->op = EXEC_INIT_ASSIGN;
889 sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
890 sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
891 /* Set initializer. */
892 c->initializer = gfc_lval_expr_from_sym (copy);
893 c->ts.interface = copy;
896 /* Add procedure pointers for type-bound procedures. */
897 add_procs_to_declared_vtab (derived, vtype);
901 vtab->ts.u.derived = vtype;
902 vtab->value = gfc_default_initializer (&vtab->ts);
909 /* It is unexpected to have some symbols added at resolution or code
910 generation time. We commit the changes in order to keep a clean state. */
913 gfc_commit_symbol (vtab);
915 gfc_commit_symbol (vtype);
917 gfc_commit_symbol (def_init);
919 gfc_commit_symbol (copy);
921 gfc_commit_symbol (src);
923 gfc_commit_symbol (dst);
932 /* General worker function to find either a type-bound procedure or a
933 type-bound user operator. */
936 find_typebound_proc_uop (gfc_symbol* derived, gfc_try* t,
937 const char* name, bool noaccess, bool uop,
943 /* Set correct symbol-root. */
944 gcc_assert (derived->f2k_derived);
945 root = (uop ? derived->f2k_derived->tb_uop_root
946 : derived->f2k_derived->tb_sym_root);
948 /* Set default to failure. */
952 /* Try to find it in the current type's namespace. */
953 res = gfc_find_symtree (root, name);
954 if (res && res->n.tb && !res->n.tb->error)
960 if (!noaccess && derived->attr.use_assoc
961 && res->n.tb->access == ACCESS_PRIVATE)
964 gfc_error ("'%s' of '%s' is PRIVATE at %L",
965 name, derived->name, where);
973 /* Otherwise, recurse on parent type if derived is an extension. */
974 if (derived->attr.extension)
976 gfc_symbol* super_type;
977 super_type = gfc_get_derived_super_type (derived);
978 gcc_assert (super_type);
980 return find_typebound_proc_uop (super_type, t, name,
981 noaccess, uop, where);
989 /* Find a type-bound procedure or user operator by name for a derived-type
990 (looking recursively through the super-types). */
993 gfc_find_typebound_proc (gfc_symbol* derived, gfc_try* t,
994 const char* name, bool noaccess, locus* where)
996 return find_typebound_proc_uop (derived, t, name, noaccess, false, where);
1000 gfc_find_typebound_user_op (gfc_symbol* derived, gfc_try* t,
1001 const char* name, bool noaccess, locus* where)
1003 return find_typebound_proc_uop (derived, t, name, noaccess, true, where);
1007 /* Find a type-bound intrinsic operator looking recursively through the
1008 super-type hierarchy. */
1011 gfc_find_typebound_intrinsic_op (gfc_symbol* derived, gfc_try* t,
1012 gfc_intrinsic_op op, bool noaccess,
1015 gfc_typebound_proc* res;
1017 /* Set default to failure. */
1021 /* Try to find it in the current type's namespace. */
1022 if (derived->f2k_derived)
1023 res = derived->f2k_derived->tb_op[op];
1028 if (res && !res->error)
1034 if (!noaccess && derived->attr.use_assoc
1035 && res->access == ACCESS_PRIVATE)
1038 gfc_error ("'%s' of '%s' is PRIVATE at %L",
1039 gfc_op2string (op), derived->name, where);
1047 /* Otherwise, recurse on parent type if derived is an extension. */
1048 if (derived->attr.extension)
1050 gfc_symbol* super_type;
1051 super_type = gfc_get_derived_super_type (derived);
1052 gcc_assert (super_type);
1054 return gfc_find_typebound_intrinsic_op (super_type, t, op,
1058 /* Nothing found. */
1063 /* Get a typebound-procedure symtree or create and insert it if not yet
1064 present. This is like a very simplified version of gfc_get_sym_tree for
1065 tbp-symtrees rather than regular ones. */
1068 gfc_get_tbp_symtree (gfc_symtree **root, const char *name)
1070 gfc_symtree *result;
1072 result = gfc_find_symtree (*root, name);
1075 result = gfc_new_symtree (root, name);
1076 gcc_assert (result);
1077 result->n.tb = NULL;