OSDN Git Service

2011-02-14 Janus Weil <janus@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / class.c
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>
6
7 This file is part of GCC.
8
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
12 version.
13
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
17 for more details.
18
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/>.  */
22
23
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.  */
27
28
29 /* Outline of the internal representation:
30
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.
37     
38    For each derived type we set up a "vtable" entry, i.e. a structure with the
39    following fields:
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.  */
47
48
49 #include "config.h"
50 #include "system.h"
51 #include "gfortran.h"
52 #include "constructor.h"
53
54
55 /* Insert a reference to the component of the given name.
56    Only to be used with CLASS containers and vtables.  */
57
58 void
59 gfc_add_component_ref (gfc_expr *e, const char *name)
60 {
61   gfc_ref **tail = &(e->ref);
62   gfc_ref *next = NULL;
63   gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
64   while (*tail != NULL)
65     {
66       if ((*tail)->type == REF_COMPONENT)
67         derived = (*tail)->u.c.component->ts.u.derived;
68       if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
69         break;
70       tail = &((*tail)->next);
71     }
72   if (*tail != NULL && strcmp (name, "_data") == 0)
73     next = *tail;
74   (*tail) = gfc_get_ref();
75   (*tail)->next = next;
76   (*tail)->type = REF_COMPONENT;
77   (*tail)->u.c.sym = derived;
78   (*tail)->u.c.component = gfc_find_component (derived, name, true, true);
79   gcc_assert((*tail)->u.c.component);
80   if (!next)
81     e->ts = (*tail)->u.c.component->ts;
82 }
83
84
85 /* Build a NULL initializer for CLASS pointers,
86    initializing the _data component to NULL and
87    the _vptr component to the declared type.  */
88
89 gfc_expr *
90 gfc_class_null_initializer (gfc_typespec *ts)
91 {
92   gfc_expr *init;
93   gfc_component *comp;
94   
95   init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
96                                              &ts->u.derived->declared_at);
97   init->ts = *ts;
98   
99   for (comp = ts->u.derived->components; comp; comp = comp->next)
100     {
101       gfc_constructor *ctor = gfc_constructor_get();
102       if (strcmp (comp->name, "_vptr") == 0)
103         ctor->expr = gfc_lval_expr_from_sym (gfc_find_derived_vtab (ts->u.derived));
104       else
105         ctor->expr = gfc_get_null_expr (NULL);
106       gfc_constructor_append (&init->value.constructor, ctor);
107     }
108
109   return init;
110 }
111
112
113 /* Create a unique string identifier for a derived type, composed of its name
114    and module name. This is used to construct unique names for the class
115    containers and vtab symbols.  */
116
117 static void
118 get_unique_type_string (char *string, gfc_symbol *derived)
119 {
120   char dt_name[GFC_MAX_SYMBOL_LEN+1];
121   sprintf (dt_name, "%s", derived->name);
122   dt_name[0] = TOUPPER (dt_name[0]);
123   if (derived->module)
124     sprintf (string, "%s_%s", derived->module, dt_name);
125   else if (derived->ns->proc_name)
126     sprintf (string, "%s_%s", derived->ns->proc_name->name, dt_name);
127   else
128     sprintf (string, "_%s", dt_name);
129 }
130
131
132 /* A relative of 'get_unique_type_string' which makes sure the generated
133    string will not be too long (replacing it by a hash string if needed).  */
134
135 static void
136 get_unique_hashed_string (char *string, gfc_symbol *derived)
137 {
138   char tmp[2*GFC_MAX_SYMBOL_LEN+2];
139   get_unique_type_string (&tmp[0], derived);
140   /* If string is too long, use hash value in hex representation
141      (allow for extra decoration, cf. gfc_build_class_symbol)*/
142   if (strlen (tmp) > GFC_MAX_SYMBOL_LEN - 10)
143     {
144       int h = gfc_hash_value (derived);
145       sprintf (string, "%X", h);
146     }
147   else
148     strcpy (string, tmp);
149 }
150
151
152 /* Assign a hash value for a derived type. The algorithm is that of SDBM.  */
153
154 unsigned int
155 gfc_hash_value (gfc_symbol *sym)
156 {
157   unsigned int hash = 0;
158   char c[2*(GFC_MAX_SYMBOL_LEN+1)];
159   int i, len;
160   
161   get_unique_type_string (&c[0], sym);
162   len = strlen (c);
163   
164   for (i = 0; i < len; i++)
165     hash = (hash << 6) + (hash << 16) - hash + c[i];
166
167   /* Return the hash but take the modulus for the sake of module read,
168      even though this slightly increases the chance of collision.  */
169   return (hash % 100000000);
170 }
171
172
173 /* Build a polymorphic CLASS entity, using the symbol that comes from
174    build_sym. A CLASS entity is represented by an encapsulating type,
175    which contains the declared type as '_data' component, plus a pointer
176    component '_vptr' which determines the dynamic type.  */
177
178 gfc_try
179 gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
180                         gfc_array_spec **as, bool delayed_vtab)
181 {
182   char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
183   gfc_symbol *fclass;
184   gfc_symbol *vtab;
185   gfc_component *c;
186
187   if (*as)
188     {
189       gfc_fatal_error ("Polymorphic array at %C not yet supported");
190       return FAILURE;
191     }
192
193   /* Determine the name of the encapsulating type.  */
194   get_unique_hashed_string (tname, ts->u.derived);
195   if ((*as) && (*as)->rank && attr->allocatable)
196     sprintf (name, "__class_%s_%d_a", tname, (*as)->rank);
197   else if ((*as) && (*as)->rank)
198     sprintf (name, "__class_%s_%d", tname, (*as)->rank);
199   else if (attr->pointer)
200     sprintf (name, "__class_%s_p", tname);
201   else if (attr->allocatable)
202     sprintf (name, "__class_%s_a", tname);
203   else
204     sprintf (name, "__class_%s", tname);
205
206   gfc_find_symbol (name, ts->u.derived->ns, 0, &fclass);
207   if (fclass == NULL)
208     {
209       gfc_symtree *st;
210       /* If not there, create a new symbol.  */
211       fclass = gfc_new_symbol (name, ts->u.derived->ns);
212       st = gfc_new_symtree (&ts->u.derived->ns->sym_root, name);
213       st->n.sym = fclass;
214       gfc_set_sym_referenced (fclass);
215       fclass->refs++;
216       fclass->ts.type = BT_UNKNOWN;
217       fclass->attr.abstract = ts->u.derived->attr.abstract;
218       if (ts->u.derived->f2k_derived)
219         fclass->f2k_derived = gfc_get_namespace (NULL, 0);
220       if (gfc_add_flavor (&fclass->attr, FL_DERIVED,
221           NULL, &gfc_current_locus) == FAILURE)
222         return FAILURE;
223
224       /* Add component '_data'.  */
225       if (gfc_add_component (fclass, "_data", &c) == FAILURE)
226         return FAILURE;
227       c->ts = *ts;
228       c->ts.type = BT_DERIVED;
229       c->attr.access = ACCESS_PRIVATE;
230       c->ts.u.derived = ts->u.derived;
231       c->attr.class_pointer = attr->pointer;
232       c->attr.pointer = attr->pointer || attr->dummy;
233       c->attr.allocatable = attr->allocatable;
234       c->attr.dimension = attr->dimension;
235       c->attr.codimension = attr->codimension;
236       c->attr.abstract = ts->u.derived->attr.abstract;
237       c->as = (*as);
238       c->initializer = NULL;
239
240       /* Add component '_vptr'.  */
241       if (gfc_add_component (fclass, "_vptr", &c) == FAILURE)
242         return FAILURE;
243       c->ts.type = BT_DERIVED;
244       if (delayed_vtab)
245         c->ts.u.derived = NULL;
246       else
247         {
248           vtab = gfc_find_derived_vtab (ts->u.derived);
249           gcc_assert (vtab);
250           c->ts.u.derived = vtab->ts.u.derived;
251         }
252       c->attr.access = ACCESS_PRIVATE;
253       c->attr.pointer = 1;
254     }
255
256   /* Since the extension field is 8 bit wide, we can only have
257      up to 255 extension levels.  */
258   if (ts->u.derived->attr.extension == 255)
259     {
260       gfc_error ("Maximum extension level reached with type '%s' at %L",
261                  ts->u.derived->name, &ts->u.derived->declared_at);
262       return FAILURE;
263     }
264     
265   fclass->attr.extension = ts->u.derived->attr.extension + 1;
266   fclass->attr.is_class = 1;
267   ts->u.derived = fclass;
268   attr->allocatable = attr->pointer = attr->dimension = 0;
269   (*as) = NULL;  /* XXX */
270   return SUCCESS;
271 }
272
273
274 /* Add a procedure pointer component to the vtype
275    to represent a specific type-bound procedure.  */
276
277 static void
278 add_proc_comp (gfc_symbol *vtype, const char *name, gfc_typebound_proc *tb)
279 {
280   gfc_component *c;
281   c = gfc_find_component (vtype, name, true, true);
282
283   if (c == NULL)
284     {
285       /* Add procedure component.  */
286       if (gfc_add_component (vtype, name, &c) == FAILURE)
287         return;
288
289       if (!c->tb)
290         c->tb = XCNEW (gfc_typebound_proc);
291       *c->tb = *tb;
292       c->tb->ppc = 1;
293       c->attr.procedure = 1;
294       c->attr.proc_pointer = 1;
295       c->attr.flavor = FL_PROCEDURE;
296       c->attr.access = ACCESS_PRIVATE;
297       c->attr.external = 1;
298       c->attr.untyped = 1;
299       c->attr.if_source = IFSRC_IFBODY;
300     }
301   else if (c->attr.proc_pointer && c->tb)
302     {
303       *c->tb = *tb;
304       c->tb->ppc = 1;
305     }
306
307   if (tb->u.specific)
308     {
309       c->ts.interface = tb->u.specific->n.sym;
310       if (!tb->deferred)
311         c->initializer = gfc_get_variable_expr (tb->u.specific);
312     }
313 }
314
315
316 /* Add all specific type-bound procedures in the symtree 'st' to a vtype.  */
317
318 static void
319 add_procs_to_declared_vtab1 (gfc_symtree *st, gfc_symbol *vtype)
320 {
321   if (!st)
322     return;
323
324   if (st->left)
325     add_procs_to_declared_vtab1 (st->left, vtype);
326
327   if (st->right)
328     add_procs_to_declared_vtab1 (st->right, vtype);
329
330   if (st->n.tb && !st->n.tb->error 
331       && !st->n.tb->is_generic && st->n.tb->u.specific)
332     add_proc_comp (vtype, st->name, st->n.tb);
333 }
334
335
336 /* Copy procedure pointers components from the parent type.  */
337
338 static void
339 copy_vtab_proc_comps (gfc_symbol *declared, gfc_symbol *vtype)
340 {
341   gfc_component *cmp;
342   gfc_symbol *vtab;
343
344   vtab = gfc_find_derived_vtab (declared);
345
346   for (cmp = vtab->ts.u.derived->components; cmp; cmp = cmp->next)
347     {
348       if (gfc_find_component (vtype, cmp->name, true, true))
349         continue;
350
351       add_proc_comp (vtype, cmp->name, cmp->tb);
352     }
353 }
354
355
356 /* Add procedure pointers for all type-bound procedures to a vtab.  */
357
358 static void
359 add_procs_to_declared_vtab (gfc_symbol *derived, gfc_symbol *vtype)
360 {
361   gfc_symbol* super_type;
362
363   super_type = gfc_get_derived_super_type (derived);
364
365   if (super_type && (super_type != derived))
366     {
367       /* Make sure that the PPCs appear in the same order as in the parent.  */
368       copy_vtab_proc_comps (super_type, vtype);
369       /* Only needed to get the PPC initializers right.  */
370       add_procs_to_declared_vtab (super_type, vtype);
371     }
372
373   if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
374     add_procs_to_declared_vtab1 (derived->f2k_derived->tb_sym_root, vtype);
375
376   if (derived->f2k_derived && derived->f2k_derived->tb_uop_root)
377     add_procs_to_declared_vtab1 (derived->f2k_derived->tb_uop_root, vtype);
378 }
379
380
381 /* Find (or generate) the symbol for a derived type's vtab.  */
382
383 gfc_symbol *
384 gfc_find_derived_vtab (gfc_symbol *derived)
385 {
386   gfc_namespace *ns;
387   gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
388   gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
389   
390   /* Find the top-level namespace (MODULE or PROGRAM).  */
391   for (ns = gfc_current_ns; ns; ns = ns->parent)
392     if (!ns->parent)
393       break;
394
395   /* If the type is a class container, use the underlying derived type.  */
396   if (derived->attr.is_class)
397     derived = gfc_get_derived_super_type (derived);
398     
399   if (ns)
400     {
401       char name[GFC_MAX_SYMBOL_LEN+1], tname[GFC_MAX_SYMBOL_LEN+1];
402       
403       get_unique_hashed_string (tname, derived);
404       sprintf (name, "__vtab_%s", tname);
405
406       /* Look for the vtab symbol in various namespaces.  */
407       gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
408       if (vtab == NULL)
409         gfc_find_symbol (name, ns, 0, &vtab);
410       if (vtab == NULL)
411         gfc_find_symbol (name, derived->ns, 0, &vtab);
412
413       if (vtab == NULL)
414         {
415           gfc_get_symbol (name, ns, &vtab);
416           vtab->ts.type = BT_DERIVED;
417           if (gfc_add_flavor (&vtab->attr, FL_VARIABLE, NULL,
418                               &gfc_current_locus) == FAILURE)
419             goto cleanup;
420           vtab->attr.target = 1;
421           vtab->attr.save = SAVE_EXPLICIT;
422           vtab->attr.vtab = 1;
423           vtab->attr.access = ACCESS_PUBLIC;
424           gfc_set_sym_referenced (vtab);
425           sprintf (name, "__vtype_%s", tname);
426           
427           gfc_find_symbol (name, ns, 0, &vtype);
428           if (vtype == NULL)
429             {
430               gfc_component *c;
431               gfc_symbol *parent = NULL, *parent_vtab = NULL;
432
433               gfc_get_symbol (name, ns, &vtype);
434               if (gfc_add_flavor (&vtype->attr, FL_DERIVED,
435                                   NULL, &gfc_current_locus) == FAILURE)
436                 goto cleanup;
437               vtype->attr.access = ACCESS_PUBLIC;
438               vtype->attr.vtype = 1;
439               gfc_set_sym_referenced (vtype);
440
441               /* Add component '_hash'.  */
442               if (gfc_add_component (vtype, "_hash", &c) == FAILURE)
443                 goto cleanup;
444               c->ts.type = BT_INTEGER;
445               c->ts.kind = 4;
446               c->attr.access = ACCESS_PRIVATE;
447               c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
448                                                  NULL, derived->hash_value);
449
450               /* Add component '_size'.  */
451               if (gfc_add_component (vtype, "_size", &c) == FAILURE)
452                 goto cleanup;
453               c->ts.type = BT_INTEGER;
454               c->ts.kind = 4;
455               c->attr.access = ACCESS_PRIVATE;
456               /* Remember the derived type in ts.u.derived,
457                  so that the correct initializer can be set later on
458                  (in gfc_conv_structure).  */
459               c->ts.u.derived = derived;
460               c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
461                                                  NULL, 0);
462
463               /* Add component _extends.  */
464               if (gfc_add_component (vtype, "_extends", &c) == FAILURE)
465                 goto cleanup;
466               c->attr.pointer = 1;
467               c->attr.access = ACCESS_PRIVATE;
468               parent = gfc_get_derived_super_type (derived);
469               if (parent)
470                 {
471                   parent_vtab = gfc_find_derived_vtab (parent);
472                   c->ts.type = BT_DERIVED;
473                   c->ts.u.derived = parent_vtab->ts.u.derived;
474                   c->initializer = gfc_get_expr ();
475                   c->initializer->expr_type = EXPR_VARIABLE;
476                   gfc_find_sym_tree (parent_vtab->name, parent_vtab->ns,
477                                      0, &c->initializer->symtree);
478                 }
479               else
480                 {
481                   c->ts.type = BT_DERIVED;
482                   c->ts.u.derived = vtype;
483                   c->initializer = gfc_get_null_expr (NULL);
484                 }
485
486               if (derived->components == NULL && !derived->attr.zero_comp)
487                 {
488                   /* At this point an error must have occurred.
489                      Prevent further errors on the vtype components.  */
490                   found_sym = vtab;
491                   goto have_vtype;
492                 }
493
494               /* Add component _def_init.  */
495               if (gfc_add_component (vtype, "_def_init", &c) == FAILURE)
496                 goto cleanup;
497               c->attr.pointer = 1;
498               c->attr.access = ACCESS_PRIVATE;
499               c->ts.type = BT_DERIVED;
500               c->ts.u.derived = derived;
501               if (derived->attr.abstract)
502                 c->initializer = gfc_get_null_expr (NULL);
503               else
504                 {
505                   /* Construct default initialization variable.  */
506                   sprintf (name, "__def_init_%s", tname);
507                   gfc_get_symbol (name, ns, &def_init);
508                   def_init->attr.target = 1;
509                   def_init->attr.save = SAVE_EXPLICIT;
510                   def_init->attr.access = ACCESS_PUBLIC;
511                   def_init->attr.flavor = FL_VARIABLE;
512                   gfc_set_sym_referenced (def_init);
513                   def_init->ts.type = BT_DERIVED;
514                   def_init->ts.u.derived = derived;
515                   def_init->value = gfc_default_initializer (&def_init->ts);
516
517                   c->initializer = gfc_lval_expr_from_sym (def_init);
518                 }
519
520               /* Add component _copy.  */
521               if (gfc_add_component (vtype, "_copy", &c) == FAILURE)
522                 goto cleanup;
523               c->attr.proc_pointer = 1;
524               c->attr.access = ACCESS_PRIVATE;
525               c->tb = XCNEW (gfc_typebound_proc);
526               c->tb->ppc = 1;
527               if (derived->attr.abstract)
528                 c->initializer = gfc_get_null_expr (NULL);
529               else
530                 {
531                   /* Set up namespace.  */
532                   gfc_namespace *sub_ns = gfc_get_namespace (ns, 0);
533                   sub_ns->sibling = ns->contained;
534                   ns->contained = sub_ns;
535                   sub_ns->resolved = 1;
536                   /* Set up procedure symbol.  */
537                   sprintf (name, "__copy_%s", tname);
538                   gfc_get_symbol (name, sub_ns, &copy);
539                   sub_ns->proc_name = copy;
540                   copy->attr.flavor = FL_PROCEDURE;
541                   copy->attr.if_source = IFSRC_DECL;
542                   if (ns->proc_name->attr.flavor == FL_MODULE)
543                     copy->module = ns->proc_name->name;
544                   gfc_set_sym_referenced (copy);
545                   /* Set up formal arguments.  */
546                   gfc_get_symbol ("src", sub_ns, &src);
547                   src->ts.type = BT_DERIVED;
548                   src->ts.u.derived = derived;
549                   src->attr.flavor = FL_VARIABLE;
550                   src->attr.dummy = 1;
551                   gfc_set_sym_referenced (src);
552                   copy->formal = gfc_get_formal_arglist ();
553                   copy->formal->sym = src;
554                   gfc_get_symbol ("dst", sub_ns, &dst);
555                   dst->ts.type = BT_DERIVED;
556                   dst->ts.u.derived = derived;
557                   dst->attr.flavor = FL_VARIABLE;
558                   dst->attr.dummy = 1;
559                   gfc_set_sym_referenced (dst);
560                   copy->formal->next = gfc_get_formal_arglist ();
561                   copy->formal->next->sym = dst;
562                   /* Set up code.  */
563                   sub_ns->code = gfc_get_code ();
564                   sub_ns->code->op = EXEC_INIT_ASSIGN;
565                   sub_ns->code->expr1 = gfc_lval_expr_from_sym (dst);
566                   sub_ns->code->expr2 = gfc_lval_expr_from_sym (src);
567                   /* Set initializer.  */
568                   c->initializer = gfc_lval_expr_from_sym (copy);
569                   c->ts.interface = copy;
570                 }
571
572               /* Add procedure pointers for type-bound procedures.  */
573               add_procs_to_declared_vtab (derived, vtype);
574             }
575
576 have_vtype:
577           vtab->ts.u.derived = vtype;
578           vtab->value = gfc_default_initializer (&vtab->ts);
579         }
580     }
581
582   found_sym = vtab;
583
584 cleanup:
585   /* It is unexpected to have some symbols added at resolution or code
586      generation time. We commit the changes in order to keep a clean state.  */
587   if (found_sym)
588     {
589       gfc_commit_symbol (vtab);
590       if (vtype)
591         gfc_commit_symbol (vtype);
592       if (def_init)
593         gfc_commit_symbol (def_init);
594       if (copy)
595         gfc_commit_symbol (copy);
596       if (src)
597         gfc_commit_symbol (src);
598       if (dst)
599         gfc_commit_symbol (dst);
600     }
601   else
602     gfc_undo_symbols ();
603
604   return found_sym;
605 }
606
607
608 /* General worker function to find either a type-bound procedure or a
609    type-bound user operator.  */
610
611 static gfc_symtree*
612 find_typebound_proc_uop (gfc_symbol* derived, gfc_try* t,
613                          const char* name, bool noaccess, bool uop,
614                          locus* where)
615 {
616   gfc_symtree* res;
617   gfc_symtree* root;
618
619   /* Set correct symbol-root.  */
620   gcc_assert (derived->f2k_derived);
621   root = (uop ? derived->f2k_derived->tb_uop_root
622               : derived->f2k_derived->tb_sym_root);
623
624   /* Set default to failure.  */
625   if (t)
626     *t = FAILURE;
627
628   /* Try to find it in the current type's namespace.  */
629   res = gfc_find_symtree (root, name);
630   if (res && res->n.tb && !res->n.tb->error)
631     {
632       /* We found one.  */
633       if (t)
634         *t = SUCCESS;
635
636       if (!noaccess && derived->attr.use_assoc
637           && res->n.tb->access == ACCESS_PRIVATE)
638         {
639           if (where)
640             gfc_error ("'%s' of '%s' is PRIVATE at %L",
641                        name, derived->name, where);
642           if (t)
643             *t = FAILURE;
644         }
645
646       return res;
647     }
648
649   /* Otherwise, recurse on parent type if derived is an extension.  */
650   if (derived->attr.extension)
651     {
652       gfc_symbol* super_type;
653       super_type = gfc_get_derived_super_type (derived);
654       gcc_assert (super_type);
655
656       return find_typebound_proc_uop (super_type, t, name,
657                                       noaccess, uop, where);
658     }
659
660   /* Nothing found.  */
661   return NULL;
662 }
663
664
665 /* Find a type-bound procedure or user operator by name for a derived-type
666    (looking recursively through the super-types).  */
667
668 gfc_symtree*
669 gfc_find_typebound_proc (gfc_symbol* derived, gfc_try* t,
670                          const char* name, bool noaccess, locus* where)
671 {
672   return find_typebound_proc_uop (derived, t, name, noaccess, false, where);
673 }
674
675 gfc_symtree*
676 gfc_find_typebound_user_op (gfc_symbol* derived, gfc_try* t,
677                             const char* name, bool noaccess, locus* where)
678 {
679   return find_typebound_proc_uop (derived, t, name, noaccess, true, where);
680 }
681
682
683 /* Find a type-bound intrinsic operator looking recursively through the
684    super-type hierarchy.  */
685
686 gfc_typebound_proc*
687 gfc_find_typebound_intrinsic_op (gfc_symbol* derived, gfc_try* t,
688                                  gfc_intrinsic_op op, bool noaccess,
689                                  locus* where)
690 {
691   gfc_typebound_proc* res;
692
693   /* Set default to failure.  */
694   if (t)
695     *t = FAILURE;
696
697   /* Try to find it in the current type's namespace.  */
698   if (derived->f2k_derived)
699     res = derived->f2k_derived->tb_op[op];
700   else  
701     res = NULL;
702
703   /* Check access.  */
704   if (res && !res->error)
705     {
706       /* We found one.  */
707       if (t)
708         *t = SUCCESS;
709
710       if (!noaccess && derived->attr.use_assoc
711           && res->access == ACCESS_PRIVATE)
712         {
713           if (where)
714             gfc_error ("'%s' of '%s' is PRIVATE at %L",
715                        gfc_op2string (op), derived->name, where);
716           if (t)
717             *t = FAILURE;
718         }
719
720       return res;
721     }
722
723   /* Otherwise, recurse on parent type if derived is an extension.  */
724   if (derived->attr.extension)
725     {
726       gfc_symbol* super_type;
727       super_type = gfc_get_derived_super_type (derived);
728       gcc_assert (super_type);
729
730       return gfc_find_typebound_intrinsic_op (super_type, t, op,
731                                               noaccess, where);
732     }
733
734   /* Nothing found.  */
735   return NULL;
736 }
737
738
739 /* Get a typebound-procedure symtree or create and insert it if not yet
740    present.  This is like a very simplified version of gfc_get_sym_tree for
741    tbp-symtrees rather than regular ones.  */
742
743 gfc_symtree*
744 gfc_get_tbp_symtree (gfc_symtree **root, const char *name)
745 {
746   gfc_symtree *result;
747
748   result = gfc_find_symtree (*root, name);
749   if (!result)
750     {
751       result = gfc_new_symtree (root, name);
752       gcc_assert (result);
753       result->n.tb = NULL;
754     }
755
756   return result;
757 }