1 /* Implementation of Fortran 2003 Polymorphism.
2 Copyright (C) 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Paul Richard Thomas & Janus Weil
6 This file is part of GCC.
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
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
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/>. */
23 /* class.c -- This file contains the front end functions needed to service
24 the implementation of Fortran 2003 polymorphism and other
25 object-oriented features. */
28 /* Outline of the internal representation:
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.
37 For each derived type we set up a "vtable" entry, i.e. a structure with the
39 * _hash: A hash value serving as a unique identifier for this type.
40 * _size: The size in bytes of the derived type.
41 * _extends: A pointer to the vtable entry of the parent derived type.
42 * _def_init: A pointer to a default initialized variable of this type.
43 * _copy: A procedure pointer to a copying procedure.
44 After these follow procedure pointer components for the specific
45 type-bound procedures. */
51 #include "constructor.h"
54 /* Insert a reference to the component of the given name.
55 Only to be used with CLASS containers and vtables. */
58 gfc_add_component_ref (gfc_expr *e, const char *name)
60 gfc_ref **tail = &(e->ref);
62 gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
65 if ((*tail)->type == REF_COMPONENT)
66 derived = (*tail)->u.c.component->ts.u.derived;
67 if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
69 tail = &((*tail)->next);
71 if (*tail != NULL && strcmp (name, "_data") == 0)
73 (*tail) = gfc_get_ref();
75 (*tail)->type = REF_COMPONENT;
76 (*tail)->u.c.sym = derived;
77 (*tail)->u.c.component = gfc_find_component (derived, name, true, true);
78 gcc_assert((*tail)->u.c.component);
80 e->ts = (*tail)->u.c.component->ts;
84 /* Build a NULL initializer for CLASS pointers,
85 initializing the _data and _vptr components to zero. */
88 gfc_class_null_initializer (gfc_typespec *ts)
93 init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
94 &ts->u.derived->declared_at);
97 for (comp = ts->u.derived->components; comp; comp = comp->next)
99 gfc_constructor *ctor = gfc_constructor_get();
100 ctor->expr = gfc_get_expr ();
101 ctor->expr->expr_type = EXPR_NULL;
102 ctor->expr->ts = comp->ts;
103 gfc_constructor_append (&init->value.constructor, ctor);
110 /* Create a unique string identifier for a derived type, composed of its name
111 and module name. This is used to construct unique names for the class
112 containers and vtab symbols. */
115 get_unique_type_string (char *string, gfc_symbol *derived)
118 sprintf (string, "%s_%s", derived->module, derived->name);
120 sprintf (string, "%s_%s", derived->ns->proc_name->name, derived->name);
124 /* Build a polymorphic CLASS entity, using the symbol that comes from
125 build_sym. A CLASS entity is represented by an encapsulating type,
126 which contains the declared type as '_data' component, plus a pointer
127 component '_vptr' which determines the dynamic type. */
130 gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
131 gfc_array_spec **as, bool delayed_vtab)
133 char name[GFC_MAX_SYMBOL_LEN], tname[GFC_MAX_SYMBOL_LEN];
138 /* Determine the name of the encapsulating type. */
139 get_unique_type_string (tname, ts->u.derived);
140 if ((*as) && (*as)->rank && attr->allocatable)
141 sprintf (name, "__class_%s_%d_a", tname, (*as)->rank);
142 else if ((*as) && (*as)->rank)
143 sprintf (name, "__class_%s_%d", tname, (*as)->rank);
144 else if (attr->pointer)
145 sprintf (name, "__class_%s_p", tname);
146 else if (attr->allocatable)
147 sprintf (name, "__class_%s_a", tname);
149 sprintf (name, "__class_%s", tname);
151 gfc_find_symbol (name, ts->u.derived->ns, 0, &fclass);
155 /* If not there, create a new symbol. */
156 fclass = gfc_new_symbol (name, ts->u.derived->ns);
157 st = gfc_new_symtree (&ts->u.derived->ns->sym_root, name);
159 gfc_set_sym_referenced (fclass);
161 fclass->ts.type = BT_UNKNOWN;
162 fclass->attr.abstract = ts->u.derived->attr.abstract;
163 if (ts->u.derived->f2k_derived)
164 fclass->f2k_derived = gfc_get_namespace (NULL, 0);
165 if (gfc_add_flavor (&fclass->attr, FL_DERIVED,
166 NULL, &gfc_current_locus) == FAILURE)
169 /* Add component '_data'. */
170 if (gfc_add_component (fclass, "_data", &c) == FAILURE)
173 c->ts.type = BT_DERIVED;
174 c->attr.access = ACCESS_PRIVATE;
175 c->ts.u.derived = ts->u.derived;
176 c->attr.class_pointer = attr->pointer;
177 c->attr.pointer = attr->pointer || attr->dummy;
178 c->attr.allocatable = attr->allocatable;
179 c->attr.dimension = attr->dimension;
180 c->attr.codimension = attr->codimension;
181 c->attr.abstract = ts->u.derived->attr.abstract;
183 c->initializer = NULL;
185 /* Add component '_vptr'. */
186 if (gfc_add_component (fclass, "_vptr", &c) == FAILURE)
188 c->ts.type = BT_DERIVED;
190 c->ts.u.derived = NULL;
193 vtab = gfc_find_derived_vtab (ts->u.derived);
195 c->ts.u.derived = vtab->ts.u.derived;
197 c->attr.access = ACCESS_PRIVATE;
201 /* Since the extension field is 8 bit wide, we can only have
202 up to 255 extension levels. */
203 if (ts->u.derived->attr.extension == 255)
205 gfc_error ("Maximum extension level reached with type '%s' at %L",
206 ts->u.derived->name, &ts->u.derived->declared_at);
210 fclass->attr.extension = ts->u.derived->attr.extension + 1;
211 fclass->attr.is_class = 1;
212 ts->u.derived = fclass;
213 attr->allocatable = attr->pointer = attr->dimension = 0;
214 (*as) = NULL; /* XXX */
219 /* Add a procedure pointer component to the vtype
220 to represent a specific type-bound procedure. */
223 add_proc_comp (gfc_symbol *vtype, const char *name, gfc_typebound_proc *tb)
226 c = gfc_find_component (vtype, name, true, true);
230 /* Add procedure component. */
231 if (gfc_add_component (vtype, name, &c) == FAILURE)
235 c->tb = XCNEW (gfc_typebound_proc);
238 c->attr.procedure = 1;
239 c->attr.proc_pointer = 1;
240 c->attr.flavor = FL_PROCEDURE;
241 c->attr.access = ACCESS_PRIVATE;
242 c->attr.external = 1;
244 c->attr.if_source = IFSRC_IFBODY;
246 else if (c->attr.proc_pointer && c->tb)
254 c->ts.interface = tb->u.specific->n.sym;
256 c->initializer = gfc_get_variable_expr (tb->u.specific);
261 /* Add all specific type-bound procedures in the symtree 'st' to a vtype. */
264 add_procs_to_declared_vtab1 (gfc_symtree *st, gfc_symbol *vtype)
270 add_procs_to_declared_vtab1 (st->left, vtype);
273 add_procs_to_declared_vtab1 (st->right, vtype);
275 if (st->n.tb && !st->n.tb->error
276 && !st->n.tb->is_generic && st->n.tb->u.specific)
277 add_proc_comp (vtype, st->name, st->n.tb);
281 /* Copy procedure pointers components from the parent type. */
284 copy_vtab_proc_comps (gfc_symbol *declared, gfc_symbol *vtype)
289 vtab = gfc_find_derived_vtab (declared);
291 for (cmp = vtab->ts.u.derived->components; cmp; cmp = cmp->next)
293 if (gfc_find_component (vtype, cmp->name, true, true))
296 add_proc_comp (vtype, cmp->name, cmp->tb);
301 /* Add procedure pointers for all type-bound procedures to a vtab. */
304 add_procs_to_declared_vtab (gfc_symbol *derived, gfc_symbol *vtype)
306 gfc_symbol* super_type;
308 super_type = gfc_get_derived_super_type (derived);
310 if (super_type && (super_type != derived))
312 /* Make sure that the PPCs appear in the same order as in the parent. */
313 copy_vtab_proc_comps (super_type, vtype);
314 /* Only needed to get the PPC initializers right. */
315 add_procs_to_declared_vtab (super_type, vtype);
318 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
319 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_sym_root, vtype);
321 if (derived->f2k_derived && derived->f2k_derived->tb_uop_root)
322 add_procs_to_declared_vtab1 (derived->f2k_derived->tb_uop_root, vtype);
326 /* Find (or generate) the symbol for a derived type's vtab. */
329 gfc_find_derived_vtab (gfc_symbol *derived)
332 gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
333 gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
335 /* Find the top-level namespace (MODULE or PROGRAM). */
336 for (ns = gfc_current_ns; ns; ns = ns->parent)
340 /* If the type is a class container, use the underlying derived type. */
341 if (derived->attr.is_class)
342 derived = gfc_get_derived_super_type (derived);
346 char name[GFC_MAX_SYMBOL_LEN], tname[GFC_MAX_SYMBOL_LEN];
348 get_unique_type_string (tname, derived);
349 sprintf (name, "__vtab_%s", tname);
351 /* Look for the vtab symbol in various namespaces. */
352 gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
354 gfc_find_symbol (name, ns, 0, &vtab);
356 gfc_find_symbol (name, derived->ns, 0, &vtab);
360 gfc_get_symbol (name, ns, &vtab);
361 vtab->ts.type = BT_DERIVED;
362 if (gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
363 &gfc_current_locus) == FAILURE)
365 vtab->attr.target = 1;
366 vtab->attr.save = SAVE_EXPLICIT;
368 vtab->attr.access = ACCESS_PUBLIC;
369 gfc_set_sym_referenced (vtab);
370 sprintf (name, "__vtype_%s", tname);
372 gfc_find_symbol (name, ns, 0, &vtype);
376 gfc_symbol *parent = NULL, *parent_vtab = NULL;
378 gfc_get_symbol (name, ns, &vtype);
379 if (gfc_add_flavor (&vtype->attr, FL_DERIVED,
380 NULL, &gfc_current_locus) == FAILURE)
382 vtype->attr.access = ACCESS_PUBLIC;
383 vtype->attr.vtype = 1;
384 gfc_set_sym_referenced (vtype);
386 /* Add component '_hash'. */
387 if (gfc_add_component (vtype, "_hash", &c) == FAILURE)
389 c->ts.type = BT_INTEGER;
391 c->attr.access = ACCESS_PRIVATE;
392 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
393 NULL, derived->hash_value);
395 /* Add component '_size'. */
396 if (gfc_add_component (vtype, "_size", &c) == FAILURE)
398 c->ts.type = BT_INTEGER;
400 c->attr.access = ACCESS_PRIVATE;
401 /* Remember the derived type in ts.u.derived,
402 so that the correct initializer can be set later on
403 (in gfc_conv_structure). */
404 c->ts.u.derived = derived;
405 c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
408 /* Add component _extends. */
409 if (gfc_add_component (vtype, "_extends", &c) == FAILURE)
412 c->attr.access = ACCESS_PRIVATE;
413 parent = gfc_get_derived_super_type (derived);
416 parent_vtab = gfc_find_derived_vtab (parent);
417 c->ts.type = BT_DERIVED;
418 c->ts.u.derived = parent_vtab->ts.u.derived;
419 c->initializer = gfc_get_expr ();
420 c->initializer->expr_type = EXPR_VARIABLE;
421 gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
422 0, &c->initializer->symtree);
426 c->ts.type = BT_DERIVED;
427 c->ts.u.derived = vtype;
428 c->initializer = gfc_get_null_expr (NULL);
431 if (derived->components == NULL && !derived->attr.zero_comp)
433 /* At this point an error must have occurred.
434 Prevent further errors on the vtype components. */
439 /* Add component _def_init. */
440 if (gfc_add_component (vtype, "_def_init", &c) == FAILURE)
443 c->attr.access = ACCESS_PRIVATE;
444 c->ts.type = BT_DERIVED;
445 c->ts.u.derived = derived;
446 if (derived->attr.abstract)
447 c->initializer = gfc_get_null_expr (NULL);
450 /* Construct default initialization variable. */
451 sprintf (name, "__def_init_%s", tname);
452 gfc_get_symbol (name, ns, &def_init);
453 def_init->attr.target = 1;
454 def_init->attr.save = SAVE_EXPLICIT;
455 def_init->attr.access = ACCESS_PUBLIC;
456 def_init->attr.flavor = FL_VARIABLE;
457 gfc_set_sym_referenced (def_init);
458 def_init->ts.type = BT_DERIVED;
459 def_init->ts.u.derived = derived;
460 def_init->value = gfc_default_initializer (&def_init->ts);
462 c->initializer = gfc_lval_expr_from_sym (def_init);
465 /* Add component _copy. */
466 if (gfc_add_component (vtype, "_copy", &c) == FAILURE)
468 c->attr.proc_pointer = 1;
469 c->attr.access = ACCESS_PRIVATE;
470 c->tb = XCNEW (gfc_typebound_proc);
472 if (derived->attr.abstract)
473 c->initializer = gfc_get_null_expr (NULL);
476 /* Set up namespace. */
477 gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
478 sub_ns->sibling = ns->contained;
479 ns->contained = sub_ns;
480 sub_ns->resolved = 1;
481 /* Set up procedure symbol. */
482 sprintf (name, "__copy_%s", tname);
483 gfc_get_symbol (name, sub_ns, ©);
484 sub_ns->proc_name = copy;
485 copy->attr.flavor = FL_PROCEDURE;
486 copy->attr.if_source = IFSRC_DECL;
487 gfc_set_sym_referenced (copy);
488 /* Set up formal arguments. */
489 gfc_get_symbol ("src", sub_ns, &src);
490 src->ts.type = BT_DERIVED;
491 src->ts.u.derived = derived;
492 src->attr.flavor = FL_VARIABLE;
494 gfc_set_sym_referenced (src);
495 copy->formal = gfc_get_formal_arglist ();
496 copy->formal->sym = src;
497 gfc_get_symbol ("dst", sub_ns, &dst);
498 dst->ts.type = BT_DERIVED;
499 dst->ts.u.derived = derived;
500 dst->attr.flavor = FL_VARIABLE;
502 gfc_set_sym_referenced (dst);
503 copy->formal->next = gfc_get_formal_arglist ();
504 copy->formal->next->sym = dst;
506 sub_ns->code = gfc_get_code ();
507 sub_ns->code->op = EXEC_ASSIGN;
508 sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
509 sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
510 /* Set initializer. */
511 c->initializer = gfc_lval_expr_from_sym (copy);
512 c->ts.interface = copy;
515 /* Add procedure pointers for type-bound procedures. */
516 add_procs_to_declared_vtab (derived, vtype);
520 vtab->ts.u.derived = vtype;
521 vtab->value = gfc_default_initializer (&vtab->ts);
528 /* It is unexpected to have some symbols added at resolution or code
529 generation time. We commit the changes in order to keep a clean state. */
532 gfc_commit_symbol (vtab);
534 gfc_commit_symbol (vtype);
536 gfc_commit_symbol (def_init);
538 gfc_commit_symbol (copy);
540 gfc_commit_symbol (src);
542 gfc_commit_symbol (dst);
551 /* General worker function to find either a type-bound procedure or a
552 type-bound user operator. */
555 find_typebound_proc_uop (gfc_symbol* derived, gfc_try* t,
556 const char* name, bool noaccess, bool uop,
562 /* Set correct symbol-root. */
563 gcc_assert (derived->f2k_derived);
564 root = (uop ? derived->f2k_derived->tb_uop_root
565 : derived->f2k_derived->tb_sym_root);
567 /* Set default to failure. */
571 /* Try to find it in the current type's namespace. */
572 res = gfc_find_symtree (root, name);
573 if (res && res->n.tb && !res->n.tb->error)
579 if (!noaccess && derived->attr.use_assoc
580 && res->n.tb->access == ACCESS_PRIVATE)
583 gfc_error ("'%s' of '%s' is PRIVATE at %L",
584 name, derived->name, where);
592 /* Otherwise, recurse on parent type if derived is an extension. */
593 if (derived->attr.extension)
595 gfc_symbol* super_type;
596 super_type = gfc_get_derived_super_type (derived);
597 gcc_assert (super_type);
599 return find_typebound_proc_uop (super_type, t, name,
600 noaccess, uop, where);
608 /* Find a type-bound procedure or user operator by name for a derived-type
609 (looking recursively through the super-types). */
612 gfc_find_typebound_proc (gfc_symbol* derived, gfc_try* t,
613 const char* name, bool noaccess, locus* where)
615 return find_typebound_proc_uop (derived, t, name, noaccess, false, where);
619 gfc_find_typebound_user_op (gfc_symbol* derived, gfc_try* t,
620 const char* name, bool noaccess, locus* where)
622 return find_typebound_proc_uop (derived, t, name, noaccess, true, where);
626 /* Find a type-bound intrinsic operator looking recursively through the
627 super-type hierarchy. */
630 gfc_find_typebound_intrinsic_op (gfc_symbol* derived, gfc_try* t,
631 gfc_intrinsic_op op, bool noaccess,
634 gfc_typebound_proc* res;
636 /* Set default to failure. */
640 /* Try to find it in the current type's namespace. */
641 if (derived->f2k_derived)
642 res = derived->f2k_derived->tb_op[op];
647 if (res && !res->error)
653 if (!noaccess && derived->attr.use_assoc
654 && res->access == ACCESS_PRIVATE)
657 gfc_error ("'%s' of '%s' is PRIVATE at %L",
658 gfc_op2string (op), derived->name, where);
666 /* Otherwise, recurse on parent type if derived is an extension. */
667 if (derived->attr.extension)
669 gfc_symbol* super_type;
670 super_type = gfc_get_derived_super_type (derived);
671 gcc_assert (super_type);
673 return gfc_find_typebound_intrinsic_op (super_type, t, op,
682 /* Get a typebound-procedure symtree or create and insert it if not yet
683 present. This is like a very simplified version of gfc_get_sym_tree for
684 tbp-symtrees rather than regular ones. */
687 gfc_get_tbp_symtree (gfc_symtree **root, const char *name)
691 result = gfc_find_symtree (*root, name);
694 result = gfc_new_symtree (root, name);