]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
Make OSF/1 getopt.h fix work on AIX also
[gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba
MS
1/* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* Known bugs or deficiencies include:
22 * templates for class static data don't work (methods only)
23 * duplicated method templates can crash the compiler
24 * interface/impl data is taken from file defining the template
25 * all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win"
27 * method templates must be seen before the expansion of the
28 class template is done
29 */
30
31#include "config.h"
32#include <stdio.h>
33#include "obstack.h"
34
35#include "tree.h"
36#include "flags.h"
37#include "cp-tree.h"
38#include "decl.h"
39#include "parse.h"
40
41extern struct obstack permanent_obstack;
42extern tree grokdeclarator ();
43
44extern int lineno;
45extern char *input_filename;
46struct pending_inline *pending_template_expansions;
47
48int processing_template_decl;
49int processing_template_defn;
50
51#define obstack_chunk_alloc xmalloc
52#define obstack_chunk_free free
53
54static int unify ();
55static void add_pending_template ();
56
57void overload_template_name (), pop_template_decls ();
58
59/* We've got a template header coming up; set obstacks up to save the
60 nodes created permanently. (There might be cases with nested templates
61 where we don't have to do this, but they aren't implemented, and it
62 probably wouldn't be worth the effort.) */
63void
64begin_template_parm_list ()
65{
66 pushlevel (0);
67 push_obstacks (&permanent_obstack, &permanent_obstack);
68 pushlevel (0);
69}
70
71/* Process information from new template parameter NEXT and append it to the
72 LIST being built. The rules for use of a template parameter type name
73 by later parameters are not well-defined for us just yet. However, the
74 only way to avoid having to parse expressions of unknown complexity (and
75 with tokens of unknown types) is to disallow it completely. So for now,
76 that is what is assumed. */
77tree
78process_template_parm (list, next)
79 tree list, next;
80{
81 tree parm;
82 tree decl = 0;
83 int is_type;
84 parm = next;
85 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
86 is_type = TREE_CODE (TREE_PURPOSE (parm)) == IDENTIFIER_NODE;
87 if (!is_type)
88 {
89 tree tinfo = 0;
8d08fdba
MS
90 parm = TREE_PURPOSE (parm);
91 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 260);
92 parm = TREE_VALUE (parm);
93 /* is a const-param */
94 parm = grokdeclarator (TREE_VALUE (next), TREE_PURPOSE (next),
95 PARM, 0, NULL_TREE);
96 /* A template parameter is not modifiable. */
97 TREE_READONLY (parm) = 1;
98 if (TREE_CODE (TREE_TYPE (parm)) == RECORD_TYPE
99 || TREE_CODE (TREE_TYPE (parm)) == UNION_TYPE)
100 {
101 sorry ("aggregate template parameter types");
102 TREE_TYPE (parm) = void_type_node;
103 }
104 tinfo = make_node (TEMPLATE_CONST_PARM);
105 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
106 if (TREE_PERMANENT (parm) == 0)
107 {
108 parm = copy_node (parm);
109 TREE_PERMANENT (parm) = 1;
110 }
111 TREE_TYPE (tinfo) = TREE_TYPE (parm);
112 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
113 DECL_INITIAL (decl) = tinfo;
114 DECL_INITIAL (parm) = tinfo;
115 }
116 else
117 {
118 tree t = make_node (TEMPLATE_TYPE_PARM);
119 decl = build_lang_decl (TYPE_DECL, TREE_PURPOSE (parm), t);
120 TYPE_NAME (t) = decl;
121 TREE_VALUE (parm) = t;
122 }
123 pushdecl (decl);
124 return chainon (list, parm);
125}
126
127/* The end of a template parameter list has been reached. Process the
128 tree list into a parameter vector, converting each parameter into a more
129 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
130 as PARM_DECLs. */
131
132tree
133end_template_parm_list (parms)
134 tree parms;
135{
136 int nparms = 0;
137 tree saved_parmlist;
138 tree parm;
139 for (parm = parms; parm; parm = TREE_CHAIN (parm))
140 nparms++;
141 saved_parmlist = make_tree_vec (nparms);
142
143 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
144 {
145 tree p = parm;
146 if (TREE_CODE (p) == TREE_LIST)
147 {
148 tree t = TREE_VALUE (p);
149 TREE_VALUE (p) = NULL_TREE;
150 p = TREE_PURPOSE (p);
151 my_friendly_assert (TREE_CODE (p) == IDENTIFIER_NODE, 261);
152 TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
153 }
154 else
155 {
156 tree tinfo = DECL_INITIAL (p);
157 DECL_INITIAL (p) = NULL_TREE;
158 TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
159 }
160 TREE_VEC_ELT (saved_parmlist, nparms) = p;
161 }
162 set_current_level_tags_transparency (1);
163 processing_template_decl++;
164 return saved_parmlist;
165}
166
167/* end_template_decl is called after a template declaration is seen.
168 D1 is template header; D2 is class_head_sans_basetype or a
169 TEMPLATE_DECL with its DECL_RESULT field set. */
170void
51c184be 171end_template_decl (d1, d2, is_class, defn)
8d08fdba 172 tree d1, d2, is_class;
51c184be 173 int defn;
8d08fdba
MS
174{
175 tree decl;
176 struct template_info *tmpl;
177
178 tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
179 sizeof (struct template_info));
180 tmpl->text = 0;
181 tmpl->length = 0;
182 tmpl->aggr = is_class;
183
184 /* cloned from reinit_parse_for_template */
185 tmpl->filename = input_filename;
186 tmpl->lineno = lineno;
187 tmpl->parm_vec = d1; /* [eichin:19911015.2306EST] */
188
189 if (d2 == NULL_TREE || d2 == error_mark_node)
190 {
191 decl = 0;
192 goto lose;
193 }
194
195 if (is_class)
196 {
197 decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
198 }
199 else
200 {
201 if (TREE_CODE (d2) == TEMPLATE_DECL)
202 decl = d2;
203 else
204 {
205 /* Class destructor templates and operator templates are
206 slipping past as non-template nodes. Process them here, since
207 I haven't figured out where to catch them earlier. I could
208 go do that, but it's a choice between getting that done and
209 staying only N months behind schedule. Sorry.... */
210 enum tree_code code;
211 my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
212 code = TREE_CODE (TREE_OPERAND (d2, 0));
213 my_friendly_assert (code == BIT_NOT_EXPR
214 || code == OP_IDENTIFIER
215 || code == SCOPE_REF, 264);
216 d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE);
217 decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
218 TREE_TYPE (d2));
219 DECL_TEMPLATE_RESULT (decl) = d2;
220 DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
221 DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
222 DECL_NAME (decl) = DECL_NAME (d2);
223 TREE_TYPE (decl) = TREE_TYPE (d2);
224 if (interface_unknown && flag_external_templates && ! DECL_IN_SYSTEM_HEADER (decl))
225 warn_if_unknown_interface ();
226 TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = flag_external_templates && !interface_unknown;
227 DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
228 && !(DECL_CLASS_CONTEXT (d2)
229 && !DECL_THIS_EXTERN (d2)));
230 }
231
232 /* All routines creating TEMPLATE_DECL nodes should now be using
233 build_lang_decl, which will have set this up already. */
234 my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);
235
236 /* @@ Somewhere, permanent allocation isn't being used. */
237 if (! DECL_TEMPLATE_IS_CLASS (decl)
238 && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
239 {
240 tree result = DECL_TEMPLATE_RESULT (decl);
241 /* Will do nothing if allocation was already permanent. */
242 DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
243 }
244
245 /* If this is for a method, there's an extra binding level here. */
246 if (! DECL_TEMPLATE_IS_CLASS (decl)
247 && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
248 {
249 /* @@ Find out where this should be getting set! */
250 tree r = DECL_TEMPLATE_RESULT (decl);
251 if (DECL_CLASS_CONTEXT (r) == NULL_TREE)
252 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
253 }
254 }
255 DECL_TEMPLATE_INFO (decl) = tmpl;
256 DECL_TEMPLATE_PARMS (decl) = d1;
51c184be
MS
257
258 /* So that duplicate_decls can do the right thing. */
259 if (defn)
260 DECL_INITIAL (decl) = error_mark_node;
261
262 /* If context of decl is non-null (i.e., method template), add it
263 to the appropriate class template, and pop the binding levels. */
264 if (! DECL_TEMPLATE_IS_CLASS (decl)
265 && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
8d08fdba 266 {
51c184be 267 tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
a4443a08 268 tree tmpl, t;
51c184be
MS
269 my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
270 tmpl = UPT_TEMPLATE (ctx);
a4443a08
MS
271 for (t = DECL_TEMPLATE_MEMBERS (tmpl); t; t = TREE_CHAIN (t))
272 if (TREE_PURPOSE (t) == DECL_NAME (decl)
273 && duplicate_decls (decl, TREE_VALUE (t)))
274 goto already_there;
51c184be 275 DECL_TEMPLATE_MEMBERS (tmpl) =
a4443a08
MS
276 perm_tree_cons (DECL_NAME (decl), decl, DECL_TEMPLATE_MEMBERS (tmpl));
277 already_there:
51c184be
MS
278 poplevel (0, 0, 0);
279 poplevel (0, 0, 0);
280 }
281 /* Otherwise, go back to top level first, and push the template decl
282 again there. */
283 else
284 {
285 poplevel (0, 0, 0);
286 poplevel (0, 0, 0);
8926095f 287 pushdecl (decl);
8d08fdba 288 }
51c184be 289 lose:
8d08fdba
MS
290#if 0 /* It happens sometimes, with syntactic or semantic errors.
291
292 One specific case:
293 template <class A, int X, int Y> class Foo { ... };
294 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
295 Note the missing "A" in the class containing "method". */
296 my_friendly_assert (global_bindings_p (), 267);
297#else
298 while (! global_bindings_p ())
299 poplevel (0, 0, 0);
300#endif
301 pop_obstacks ();
302 processing_template_decl--;
303 (void) get_pending_sizes ();
304}
305
306/* If TYPE contains a template parm type, then substitute that type
307 with its actual type that is found in TVEC. */
308static void
309grok_template_type (tvec, type)
310 tree tvec;
311 tree* type;
312{
313 switch (TREE_CODE (*type))
314 {
315 case TEMPLATE_TYPE_PARM:
316 if (*type != TYPE_MAIN_VARIANT (*type))
317 {
318 /* we are here for cases like const T* etc. */
319 grok_template_type (tvec, &TYPE_MAIN_VARIANT (*type));
320 *type = build_type_variant (TYPE_MAIN_VARIANT (*type),
321 TYPE_READONLY (*type),
322 TYPE_VOLATILE (*type));
323 }
324 else
325 *type = TREE_VEC_ELT (tvec, TEMPLATE_TYPE_IDX (*type));
326 return;
327 case POINTER_TYPE:
328 case REFERENCE_TYPE:
329 grok_template_type (tvec, &TREE_TYPE (*type));
330 return;
331 case FUNCTION_TYPE:
332 {
333 tree p;
334
335 /* take care of function's return type first */
336 grok_template_type (tvec, &TREE_TYPE (*type));
337
338 /* take care of function's arguments */
339 for (p = TYPE_ARG_TYPES (*type); p; p = TREE_CHAIN (p))
340 grok_template_type (tvec, &TREE_VALUE (p));
341 return;
342 }
343 default:
344 break;
345 }
346 return;
347}
348
349/* Convert all template arguments to their appropriate types, and return
350 a vector containing the resulting values. If any error occurs, return
351 error_mark_node. */
352static tree
353coerce_template_parms (parms, arglist, in_decl)
354 tree parms, arglist;
355 tree in_decl;
356{
357 int nparms, i, lost = 0;
358 tree vec;
359
360 if (TREE_CODE (arglist) == TREE_VEC)
361 nparms = TREE_VEC_LENGTH (arglist);
362 else
363 nparms = list_length (arglist);
364 if (nparms != TREE_VEC_LENGTH (parms))
365 {
366 error ("incorrect number of parameters (%d, should be %d)",
367 nparms, TREE_VEC_LENGTH (parms));
368 if (in_decl)
369 cp_error_at ("in template expansion for decl `%D'", in_decl);
370 return error_mark_node;
371 }
372
373 if (TREE_CODE (arglist) == TREE_VEC)
374 vec = copy_node (arglist);
375 else
376 {
377 vec = make_tree_vec (nparms);
378 for (i = 0; i < nparms; i++)
379 {
380 tree arg = arglist;
381 arglist = TREE_CHAIN (arglist);
382 if (arg == error_mark_node)
383 lost++;
384 else
385 arg = TREE_VALUE (arg);
386 TREE_VEC_ELT (vec, i) = arg;
387 }
388 }
389 for (i = 0; i < nparms; i++)
390 {
391 tree arg = TREE_VEC_ELT (vec, i);
392 tree parm = TREE_VEC_ELT (parms, i);
393 tree val = 0;
394 int is_type, requires_type;
395
396 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
397 requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
398 if (is_type != requires_type)
399 {
400 if (in_decl)
401 cp_error_at ("type/value mismatch in template parameter list for `%D'", in_decl);
402 lost++;
403 TREE_VEC_ELT (vec, i) = error_mark_node;
404 continue;
405 }
406 if (is_type)
407 val = groktypename (arg);
408 else if (TREE_CODE (arg) == STRING_CST)
409 {
410 cp_error ("string literal %E is not a valid template argument", arg);
411 error ("because it is the address of an object with static linkage");
412 val = error_mark_node;
413 }
414 else
415 {
416 grok_template_type (vec, &TREE_TYPE (parm));
417 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
418
419 if (val == error_mark_node)
420 ;
421
422 /* 14.2: Other template-arguments must be constant-expressions,
423 addresses of objects or functions with external linkage, or of
424 static class members. */
425 else if (!TREE_CONSTANT (val))
426 {
427 cp_error ("non-const `%E' cannot be used as template argument",
428 arg);
429 val = error_mark_node;
430 }
431 else if (TREE_CODE (val) == ADDR_EXPR)
432 {
433 tree a = TREE_OPERAND (val, 0);
434 if ((TREE_CODE (a) == VAR_DECL
435 || TREE_CODE (a) == FUNCTION_DECL)
436 && !TREE_PUBLIC (a))
437 {
438 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
439 val = error_mark_node;
440 }
441 }
442 }
443
444 if (val == error_mark_node)
445 lost++;
446
447 TREE_VEC_ELT (vec, i) = val;
448 }
449 if (lost)
450 return error_mark_node;
451 return vec;
452}
453
454/* Given class template name and parameter list, produce a user-friendly name
455 for the instantiation. */
456static char *
457mangle_class_name_for_template (name, parms, arglist)
458 char *name;
459 tree parms, arglist;
460{
461 static struct obstack scratch_obstack;
462 static char *scratch_firstobj;
463 int i, nparms;
8d08fdba
MS
464
465 if (!scratch_firstobj)
466 {
467 gcc_obstack_init (&scratch_obstack);
468 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
469 }
470 else
471 obstack_free (&scratch_obstack, scratch_firstobj);
472
473#if 0
474#define buflen sizeof(buf)
475#define check if (bufp >= buf+buflen-1) goto too_long
476#define ccat(c) *bufp++=(c); check
477#define advance bufp+=strlen(bufp); check
478#define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
479#else
480#define check
481#define ccat(c) obstack_1grow (&scratch_obstack, (c));
482#define advance
483#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
484#endif
8d08fdba
MS
485
486 cat (name);
487 ccat ('<');
488 nparms = TREE_VEC_LENGTH (parms);
489 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
490 for (i = 0; i < nparms; i++)
491 {
492 tree parm = TREE_VEC_ELT (parms, i), arg = TREE_VEC_ELT (arglist, i);
493
494 if (i)
495 ccat (',');
496
497 if (TREE_CODE (parm) == IDENTIFIER_NODE)
498 {
499 cat (type_as_string (arg, 0));
500 continue;
501 }
502 else
503 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
504
505 if (TREE_CODE (arg) == TREE_LIST)
506 {
507 /* New list cell was built because old chain link was in
508 use. */
509 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
510 arg = TREE_VALUE (arg);
511 }
512 /* No need to check arglist against parmlist here; we did that
513 in coerce_template_parms, called from lookup_template_class. */
514 cat (expr_as_string (arg, 0));
515 }
516 {
517 char *bufp = obstack_next_free (&scratch_obstack);
518 int offset = 0;
519 while (bufp[offset - 1] == ' ')
520 offset--;
521 obstack_blank_fast (&scratch_obstack, offset);
522
523 /* B<C<char> >, not B<C<char>> */
524 if (bufp[offset - 1] == '>')
525 ccat (' ');
526 }
527 ccat ('>');
528 ccat ('\0');
529 return (char *) obstack_base (&scratch_obstack);
530
8926095f 531#if 0
8d08fdba 532 too_long:
8926095f 533#endif
8d08fdba
MS
534 fatal ("out of (preallocated) string space creating template instantiation name");
535 /* NOTREACHED */
536 return NULL;
537}
538
539/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
540 parameters, find the desired type.
541
542 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
543 Since ARGLIST is build on the decl_obstack, we must copy it here
544 to keep it from being reclaimed when the decl storage is reclaimed.
545
546 IN_DECL, if non-NULL, is the template declaration we are trying to
547 instantiate. */
548tree
549lookup_template_class (d1, arglist, in_decl)
550 tree d1, arglist;
551 tree in_decl;
552{
553 tree template, parmlist;
554 char *mangled_name;
555 tree id;
556
557 my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
558 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
559 if (! template)
560 template = IDENTIFIER_CLASS_VALUE (d1);
561 /* With something like `template <class T> class X class X { ... };'
562 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
563 We don't want to do that, but we have to deal with the situation, so
564 let's give them some syntax errors to chew on instead of a crash. */
565 if (! template)
566 return error_mark_node;
567 if (TREE_CODE (template) != TEMPLATE_DECL)
568 {
569 cp_error ("non-template type `%T' used as a template", d1);
570 if (in_decl)
571 cp_error_at ("for template declaration `%D'", in_decl);
572 return error_mark_node;
573 }
574 parmlist = DECL_TEMPLATE_PARMS (template);
575
576 arglist = coerce_template_parms (parmlist, arglist, in_decl);
577 if (arglist == error_mark_node)
578 return error_mark_node;
579 if (uses_template_parms (arglist))
580 {
581 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
582 tree d;
583 id = make_anon_name ();
584 d = build_lang_decl (TYPE_DECL, id, t);
585 TYPE_NAME (t) = d;
586 TYPE_VALUES (t) = build_tree_list (template, arglist);
587 pushdecl_top_level (d);
588 }
589 else
590 {
591 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
592 parmlist, arglist);
593 id = get_identifier (mangled_name);
594 }
595 if (!IDENTIFIER_TEMPLATE (id))
596 {
597 arglist = copy_to_permanent (arglist);
598 IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
599 }
600 return id;
601}
602\f
603void
604push_template_decls (parmlist, arglist, class_level)
605 tree parmlist, arglist;
606 int class_level;
607{
608 int i, nparms;
609
610 /* Don't want to push values into global context. */
611 if (!class_level)
8926095f
MS
612 {
613 pushlevel (1);
614 declare_pseudo_global_level ();
615 }
616
8d08fdba
MS
617 nparms = TREE_VEC_LENGTH (parmlist);
618
619 for (i = 0; i < nparms; i++)
620 {
621 int requires_type, is_type;
622 tree parm = TREE_VEC_ELT (parmlist, i);
623 tree arg = TREE_VEC_ELT (arglist, i);
624 tree decl = 0;
625
626 requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
627 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
628 if (is_type)
629 {
630 /* add typename to namespace */
631 if (!requires_type)
632 {
633 error ("template use error: type provided where value needed");
634 continue;
635 }
636 decl = arg;
637 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
638 decl = build_lang_decl (TYPE_DECL, parm, decl);
639 }
640 else
641 {
642 /* add const decl to namespace */
643 tree val;
644 if (requires_type)
645 {
646 error ("template use error: value provided where type needed");
647 continue;
648 }
649 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
650 if (val != error_mark_node)
651 {
652 decl = build_decl (VAR_DECL, DECL_NAME (parm), TREE_TYPE (parm));
653 DECL_INITIAL (decl) = val;
654 TREE_READONLY (decl) = 1;
655 }
656 }
657 if (decl != 0)
658 {
659 layout_decl (decl, 0);
660 if (class_level)
661 pushdecl_class_level (decl);
662 else
663 pushdecl (decl);
664 }
665 }
8d08fdba
MS
666}
667
668void
669pop_template_decls (parmlist, arglist, class_level)
670 tree parmlist, arglist;
671 int class_level;
672{
673 if (!class_level)
674 poplevel (0, 0, 0);
675}
676\f
51c184be 677/* Should be defined in parse.h. */
8d08fdba
MS
678extern int yychar;
679
680int
681uses_template_parms (t)
682 tree t;
683{
684 if (!t)
685 return 0;
686 switch (TREE_CODE (t))
687 {
688 case INDIRECT_REF:
689 case COMPONENT_REF:
690 /* We assume that the object must be instantiated in order to build
691 the COMPONENT_REF, so we test only whether the type of the
692 COMPONENT_REF uses template parms. */
693 return uses_template_parms (TREE_TYPE (t));
694
695 case IDENTIFIER_NODE:
696 if (!IDENTIFIER_TEMPLATE (t))
697 return 0;
698 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
699
700 /* aggregates of tree nodes */
701 case TREE_VEC:
702 {
703 int i = TREE_VEC_LENGTH (t);
704 while (i--)
705 if (uses_template_parms (TREE_VEC_ELT (t, i)))
706 return 1;
707 return 0;
708 }
709 case TREE_LIST:
710 if (uses_template_parms (TREE_PURPOSE (t))
711 || uses_template_parms (TREE_VALUE (t)))
712 return 1;
713 return uses_template_parms (TREE_CHAIN (t));
714
715 /* constructed type nodes */
716 case POINTER_TYPE:
717 case REFERENCE_TYPE:
718 return uses_template_parms (TREE_TYPE (t));
719 case RECORD_TYPE:
720 case UNION_TYPE:
721 if (!TYPE_NAME (t))
722 return 0;
723 if (!TYPE_IDENTIFIER (t))
724 return 0;
725 return uses_template_parms (TYPE_IDENTIFIER (t));
726 case FUNCTION_TYPE:
727 if (uses_template_parms (TYPE_ARG_TYPES (t)))
728 return 1;
729 return uses_template_parms (TREE_TYPE (t));
730 case ARRAY_TYPE:
731 if (uses_template_parms (TYPE_DOMAIN (t)))
732 return 1;
733 return uses_template_parms (TREE_TYPE (t));
734 case OFFSET_TYPE:
735 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
736 return 1;
737 return uses_template_parms (TREE_TYPE (t));
738 case METHOD_TYPE:
739 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
740 return 1;
741 if (uses_template_parms (TYPE_ARG_TYPES (t)))
742 return 1;
743 return uses_template_parms (TREE_TYPE (t));
744
745 /* decl nodes */
746 case TYPE_DECL:
747 return uses_template_parms (DECL_NAME (t));
748 case FUNCTION_DECL:
749 if (uses_template_parms (TREE_TYPE (t)))
750 return 1;
751 /* fall through */
752 case VAR_DECL:
753 case PARM_DECL:
754 /* ??? What about FIELD_DECLs? */
755 /* The type of a decl can't use template parms if the name of the
756 variable doesn't, because it's impossible to resolve them. So
757 ignore the type field for now. */
758 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
759 return 1;
760 if (uses_template_parms (TREE_TYPE (t)))
761 {
762 error ("template parms used where they can't be resolved");
763 }
764 return 0;
765
766 case CALL_EXPR:
767 return uses_template_parms (TREE_TYPE (t));
768 case ADDR_EXPR:
769 return uses_template_parms (TREE_OPERAND (t, 0));
770
771 /* template parm nodes */
772 case TEMPLATE_TYPE_PARM:
773 case TEMPLATE_CONST_PARM:
774 return 1;
775
776 /* simple type nodes */
777 case INTEGER_TYPE:
778 if (uses_template_parms (TYPE_MIN_VALUE (t)))
779 return 1;
780 return uses_template_parms (TYPE_MAX_VALUE (t));
781
782 case REAL_TYPE:
783 case VOID_TYPE:
784 case ENUMERAL_TYPE:
785 return 0;
786
787 /* constants */
788 case INTEGER_CST:
789 case REAL_CST:
790 case STRING_CST:
791 return 0;
792
793 case ERROR_MARK:
794 /* Non-error_mark_node ERROR_MARKs are bad things. */
795 my_friendly_assert (t == error_mark_node, 274);
796 /* NOTREACHED */
797 return 0;
798
799 case UNINSTANTIATED_P_TYPE:
800 return 1;
801
802 default:
803 switch (TREE_CODE_CLASS (TREE_CODE (t)))
804 {
805 case '1':
806 case '2':
807 case '3':
808 case '<':
809 {
810 int i;
811 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
812 if (uses_template_parms (TREE_OPERAND (t, i)))
813 return 1;
814 return 0;
815 }
816 default:
817 break;
818 }
819 sorry ("testing %s for template parms",
820 tree_code_name [(int) TREE_CODE (t)]);
821 my_friendly_abort (82);
822 /* NOTREACHED */
823 return 0;
824 }
825}
826
827void
828instantiate_member_templates (classname)
829 tree classname;
830{
831 tree t;
832 tree id = classname;
833 tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
834
835 for (t = members; t; t = TREE_CHAIN (t))
836 {
837 tree parmvec, type, classparms, tdecl, t2;
838 int nparms, xxx = 0, i;
839
840 my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
841 my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
842 /* @@ Should verify that class parm list is a list of
843 distinct template parameters, and covers all the template
844 parameters. */
845 tdecl = TREE_VALUE (t);
846 type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
847 classparms = UPT_PARMS (type);
848 nparms = TREE_VEC_LENGTH (classparms);
849 parmvec = make_tree_vec (nparms);
850 for (i = 0; i < nparms; i++)
851 TREE_VEC_ELT (parmvec, i) = NULL_TREE;
852 switch (unify (DECL_TEMPLATE_PARMS (tdecl),
853 &TREE_VEC_ELT (parmvec, 0), nparms,
854 type, IDENTIFIER_TYPE_VALUE (classname),
855 &xxx))
856 {
857 case 0:
858 /* Success -- well, no inconsistency, at least. */
859 for (i = 0; i < nparms; i++)
860 if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
861 goto failure;
862 t2 = instantiate_template (tdecl,
863 &TREE_VEC_ELT (parmvec, 0));
864 type = IDENTIFIER_TYPE_VALUE (id);
865 my_friendly_assert (type != 0, 277);
866 if (CLASSTYPE_INTERFACE_UNKNOWN (type))
867 {
868 DECL_EXTERNAL (t2) = 0;
869 TREE_PUBLIC (t2) = 0;
870 }
871 else
872 {
873 DECL_EXTERNAL (t2) = CLASSTYPE_INTERFACE_ONLY (type);
874 TREE_PUBLIC (t2) = 1;
875 }
876 break;
877 case 1:
878 /* Failure. */
879 failure:
880 cp_error ("type unification error instantiating %T::%D",
881 classname, tdecl);
882 cp_error_at ("for template declaration `%D'", tdecl);
883
884 continue /* loop of members */;
885 default:
886 /* Eek, a bug. */
887 my_friendly_abort (83);
888 }
889 }
890}
891
892struct tinst_level *current_tinst_level = 0;
893struct tinst_level *free_tinst_level = 0;
894
895void
896push_tinst_level (name)
897 tree name;
898{
899 struct tinst_level *new;
900 tree global = IDENTIFIER_GLOBAL_VALUE (name);
901
902 if (free_tinst_level)
903 {
904 new = free_tinst_level;
905 free_tinst_level = new->next;
906 }
907 else
908 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
909
910 new->classname = name;
911 if (global)
912 {
913 new->line = DECL_SOURCE_LINE (global);
914 new->file = DECL_SOURCE_FILE (global);
915 }
916 else
917 {
918 new->line = lineno;
919 new->file = input_filename;
920 }
921 new->next = current_tinst_level;
922 current_tinst_level = new;
923}
924
925void
926pop_tinst_level ()
927{
928 struct tinst_level *old = current_tinst_level;
929
930 current_tinst_level = old->next;
931 old->next = free_tinst_level;
932 free_tinst_level = old;
933}
934
935struct tinst_level *
936tinst_for_decl ()
937{
938 struct tinst_level *p = current_tinst_level;
939
940 if (p)
941 for (; p->next ; p = p->next )
942 ;
943 return p;
944}
945
946tree
947instantiate_class_template (classname, setup_parse)
948 tree classname;
949 int setup_parse;
950{
951 struct template_info *template_info;
952 tree template, t1;
953
954 if (classname == error_mark_node)
955 return error_mark_node;
956
957 my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
958 template = IDENTIFIER_TEMPLATE (classname);
959
960 if (IDENTIFIER_HAS_TYPE_VALUE (classname))
961 {
962 tree type = IDENTIFIER_TYPE_VALUE (classname);
963 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
964 return type;
965 if (TYPE_BEING_DEFINED (type)
966 || TYPE_SIZE (type)
967 || CLASSTYPE_USE_TEMPLATE (type) != 0)
968 return type;
969 }
970
971 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
972 (it's something like `foo<int>'), that means we're already working on
973 the instantiation for it. Normally, a classname comes in with nothing
974 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
975 this again, we'd get a redeclaration error. Since we're already working
976 on it, we'll pass back this classname's TYPE_DECL (it's the value of
977 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
978 things up for the parser, though---if we're just trying to instantiate
979 it (e.g., via tsubst) we can trip up cuz it may not have an
980 IDENTIFIER_TYPE_VALUE when it will need one. */
981 if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
982 return IDENTIFIER_LOCAL_VALUE (classname);
983
984 if (uses_template_parms (classname))
985 {
986 if (!TREE_TYPE (classname))
987 {
988 tree t = make_lang_type (RECORD_TYPE);
989 tree d = build_lang_decl (TYPE_DECL, classname, t);
990 DECL_NAME (d) = classname;
991 TYPE_NAME (t) = d;
992 pushdecl (d);
993 }
994 return NULL_TREE;
995 }
996
997 t1 = TREE_PURPOSE (template);
998 my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
999
1000 /* If a template is declared but not defined, accept it; don't crash.
1001 Later uses requiring the definition will be flagged as errors by
1002 other code. Thanks to niklas@appli.se for this bug fix. */
1003 if (DECL_TEMPLATE_INFO (t1)->text == 0)
1004 setup_parse = 0;
1005
1006 push_to_top_level ();
1007 template_info = DECL_TEMPLATE_INFO (t1);
1008 if (setup_parse)
1009 {
1010 push_tinst_level (classname);
1011 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1012 TREE_VALUE (template), 0);
1013 set_current_level_tags_transparency (1);
1014 feed_input (template_info->text, template_info->length, (struct obstack *)0);
1015 lineno = template_info->lineno;
1016 input_filename = template_info->filename;
1017 /* Get interface/implementation back in sync. */
1018 extract_interface_info ();
1019 overload_template_name (classname, 0);
7177d104
MS
1020 /* Kludge so that we don't get screwed by our own base classes. */
1021 TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
8d08fdba
MS
1022 yychar = PRE_PARSED_CLASS_DECL;
1023 yylval.ttype = classname;
1024 processing_template_defn++;
1025 if (!flag_external_templates)
1026 interface_unknown++;
1027 }
1028 else
1029 {
1030 tree t, decl, id, tmpl;
1031
1032 id = classname;
1033 tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
1034 t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
1035 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1036 || TREE_CODE (t) == UNION_TYPE, 280);
1037
1038 /* Now, put a copy of the decl in global scope, to avoid
1039 * recursive expansion. */
1040 decl = IDENTIFIER_LOCAL_VALUE (id);
1041 if (!decl)
1042 decl = IDENTIFIER_CLASS_VALUE (id);
1043 if (decl)
1044 {
1045 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
1046 /* We'd better make sure we're on the permanent obstack or else
1047 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1048 * copy_to_permanent would be sufficient here, but then a
1049 * sharing problem might occur. I don't know -- niklas@appli.se */
1050 push_obstacks (&permanent_obstack, &permanent_obstack);
1051 pushdecl_top_level (copy_node (decl));
1052 pop_obstacks ();
1053 }
1054 pop_from_top_level ();
1055 }
1056
1057 return NULL_TREE;
1058}
1059
1060static int
1061list_eq (t1, t2)
1062 tree t1, t2;
1063{
1064 if (t1 == NULL_TREE)
1065 return t2 == NULL_TREE;
1066 if (t2 == NULL_TREE)
1067 return 0;
1068 /* Don't care if one declares its arg const and the other doesn't -- the
1069 main variant of the arg type is all that matters. */
1070 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1071 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1072 return 0;
1073 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1074}
1075
1076static tree
1077lookup_nested_type_by_name (ctype, name)
1078 tree ctype, name;
1079{
1080 tree t;
1081
1082 t = TREE_VALUE(CLASSTYPE_TAGS(ctype));
1083 while (t)
1084 {
1085 if (strcmp(IDENTIFIER_POINTER(name), IDENTIFIER_POINTER(TYPE_IDENTIFIER(t)))
1086 == 0)
1087 return t;
1088 else
1089 t = TREE_CHAIN(t);
1090 }
1091 return NULL_TREE;
1092}
1093
1094static tree
1095search_nested_type_in_tmpl (tmpl, type)
1096 tree tmpl, type;
1097{
1098 tree t;
1099
1100 if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
1101 return tmpl;
1102 t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
1103 if (t == NULL) return t;
1104 t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
1105 return t;
1106}
1107
1108static tree
1109tsubst (t, args, nargs, in_decl)
1110 tree t, *args;
1111 int nargs;
1112 tree in_decl;
1113{
1114 tree type;
1115
1116 if (t == NULL_TREE || t == error_mark_node)
1117 return t;
1118
1119 type = TREE_TYPE (t);
1120 if (type
1121 /* Minor optimization.
1122 ?? Are these really the most frequent cases? Is the savings
1123 significant? */
1124 && type != integer_type_node
1125 && type != void_type_node
1126 && type != char_type_node)
1127 type = build_type_variant (tsubst (type, args, nargs, in_decl),
1128 TYPE_READONLY (type),
1129 TYPE_VOLATILE (type));
1130 switch (TREE_CODE (t))
1131 {
1132 case RECORD_TYPE:
1133 if (TYPE_PTRMEMFUNC_P (t))
1134 return build_ptrmemfunc_type
1135 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1136
1137 /* else fall through */
1138
1139 case ERROR_MARK:
1140 case IDENTIFIER_NODE:
1141 case OP_IDENTIFIER:
1142 case VOID_TYPE:
1143 case REAL_TYPE:
1144 case ENUMERAL_TYPE:
1145 case INTEGER_CST:
1146 case REAL_CST:
1147 case STRING_CST:
1148 case UNION_TYPE:
1149 return t;
1150
1151 case INTEGER_TYPE:
1152 if (t == integer_type_node)
1153 return t;
1154
1155 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1156 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1157 return t;
1158 return build_index_2_type
1159 (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
1160 tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
1161
1162 case TEMPLATE_TYPE_PARM:
1163 return build_type_variant (args[TEMPLATE_TYPE_IDX (t)],
1164 TYPE_READONLY (t),
1165 TYPE_VOLATILE (t));
1166
1167 case TEMPLATE_CONST_PARM:
1168 return args[TEMPLATE_CONST_IDX (t)];
1169
1170 case FUNCTION_DECL:
1171 {
1172 tree r;
1173 tree fnargs, result;
1174
1175 if (type == TREE_TYPE (t)
1176 && (DECL_CONTEXT (t) == NULL_TREE
1177 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
1178 return t;
1179 fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1180 result = tsubst (DECL_RESULT (t), args, nargs, t);
1181 if (DECL_CONTEXT (t) != NULL_TREE
1182 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1183 {
1184 /* Look it up in that class, and return the decl node there,
1185 instead of creating a new one. */
1186 tree ctx, methods, name, method;
1187 int n_methods;
1188 int i, found = 0;
1189
1190 name = DECL_NAME (t);
1191 ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
1192 methods = CLASSTYPE_METHOD_VEC (ctx);
1193 if (methods == NULL_TREE)
1194 /* No methods at all -- no way this one can match. */
1195 goto no_match;
1196 n_methods = TREE_VEC_LENGTH (methods);
1197
1198 r = NULL_TREE;
1199
1200 if (!strncmp (OPERATOR_TYPENAME_FORMAT,
1201 IDENTIFIER_POINTER (name),
1202 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1203 {
1204 /* Type-conversion operator. Reconstruct the name, in
1205 case it's the name of one of the template's parameters. */
1206 name = build_typename_overload (TREE_TYPE (type));
1207 }
1208
1209 if (DECL_CONTEXT (t) != NULL_TREE
1210 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
1211 && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
1212 name = constructor_name (ctx);
1213#if 0
1214 fprintf (stderr, "\nfor function %s in class %s:\n",
1215 IDENTIFIER_POINTER (name),
1216 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1217#endif
1218 for (i = 0; i < n_methods; i++)
1219 {
1220 int pass;
1221
1222 method = TREE_VEC_ELT (methods, i);
1223 if (method == NULL_TREE || DECL_NAME (method) != name)
1224 continue;
1225
1226 pass = 0;
1227 maybe_error:
1228 for (; method; method = DECL_CHAIN (method))
1229 {
1230 my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
1231 282);
8926095f 1232 if (! comptypes (type, TREE_TYPE (method), 1))
8d08fdba
MS
1233 {
1234 tree mtype = TREE_TYPE (method);
1235 tree t1, t2;
1236
1237 /* Keep looking for a method that matches
1238 perfectly. This takes care of the problem
1239 where destructors (which have implicit int args)
1240 look like constructors which have an int arg. */
1241 if (pass == 0)
1242 continue;
1243
1244 t1 = TYPE_ARG_TYPES (mtype);
1245 t2 = TYPE_ARG_TYPES (type);
1246 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1247 t2 = TREE_CHAIN (t2);
1248
1249 if (list_eq (t1, t2))
1250 {
1251 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1252 {
1253 tree newtype;
1254 newtype = build_function_type (TREE_TYPE (type),
1255 TYPE_ARG_TYPES (type));
1256 newtype = build_type_variant (newtype,
1257 TYPE_READONLY (type),
1258 TYPE_VOLATILE (type));
1259 type = newtype;
1260 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1261 goto maybe_bad_return_type;
1262 }
1263 else if (TYPE_METHOD_BASETYPE (mtype)
1264 == TYPE_METHOD_BASETYPE (type))
1265 {
1266 /* Types didn't match, but arg types and
1267 `this' do match, so the return type is
1268 all that should be messing it up. */
1269 maybe_bad_return_type:
1270 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1271 error ("inconsistent return types for method `%s' in class `%s'",
1272 IDENTIFIER_POINTER (name),
1273 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1274 }
1275 r = method;
1276 break;
1277 }
1278 found = 1;
1279 continue;
1280 }
1281#if 0
1282 fprintf (stderr, "\tfound %s\n\n",
1283 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
1284#endif
1285
1286 if (DECL_ARGUMENTS (method)
1287 && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
1288 /* @@ Is this early enough? Might we want to do
1289 this instead while processing the expansion? */
1290 DECL_ARGUMENTS (method)
1291 = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1292 r = method;
1293 break;
1294 }
1295 if (r == NULL_TREE && pass == 0)
1296 {
1297 pass = 1;
1298 method = TREE_VEC_ELT (methods, i);
1299 goto maybe_error;
1300 }
1301 }
1302 if (r == NULL_TREE)
1303 {
1304 no_match:
1305 cp_error
1306 (found
1307 ? "template for method `%D' doesn't match any in class `%T'"
1308 : "method `%D' not found in class `%T'", name, ctx);
1309 if (in_decl)
1310 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1311 return error_mark_node;
1312 }
1313 }
1314 else
1315 {
1316 r = DECL_NAME (t);
1317 {
1318 tree decls;
1319 int got_it = 0;
1320
700f8a87 1321 decls = lookup_name (r, 0);
8d08fdba
MS
1322 if (decls == NULL_TREE)
1323 /* no match */;
1324 else if (TREE_CODE (decls) == TREE_LIST)
1325 for (decls = TREE_VALUE (decls); decls ;
1326 decls = DECL_CHAIN (decls))
1327 {
1328 if (TREE_CODE (decls) == FUNCTION_DECL
1329 && TREE_TYPE (decls) == type)
1330 {
1331 got_it = 1;
1332 r = decls;
1333 break;
1334 }
1335 }
1336 else
1337 {
1338 tree val = decls;
1339 decls = NULL_TREE;
1340 if (TREE_CODE (val) == FUNCTION_DECL
1341 && TREE_TYPE (val) == type)
1342 {
1343 got_it = 1;
1344 r = val;
8d08fdba
MS
1345 }
1346 }
1347
1348 if (!got_it)
1349 {
1350 r = build_decl_overload (r, TYPE_VALUES (type),
1351 DECL_CONTEXT (t) != NULL_TREE);
1352 r = build_lang_decl (FUNCTION_DECL, r, type);
1353 }
1354 }
1355 }
1356 TREE_PUBLIC (r) = TREE_PUBLIC (t);
1357 DECL_EXTERNAL (r) = DECL_EXTERNAL (t);
1358 TREE_STATIC (r) = TREE_STATIC (t);
1359 DECL_INLINE (r) = DECL_INLINE (t);
1360 {
1361#if 0 /* Maybe later. -jason */
1362 struct tinst_level *til = tinst_for_decl();
1363
1364 /* should always be true under new approach */
1365 if (til)
1366 {
1367 DECL_SOURCE_FILE (r) = til->file;
1368 DECL_SOURCE_LINE (r) = til->line;
1369 }
1370 else
1371#endif
1372 {
1373 DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
1374 DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
1375 }
1376 }
1377 DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1378 make_decl_rtl (r, NULL_PTR, 1);
1379 DECL_ARGUMENTS (r) = fnargs;
1380 DECL_RESULT (r) = result;
1381 if (DECL_CONTEXT (t) == NULL_TREE
1382 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
1383 push_overloaded_decl_top_level (r, 0);
1384 return r;
1385 }
1386
1387 case PARM_DECL:
1388 {
1389 tree r;
1390 r = build_decl (PARM_DECL, DECL_NAME (t), type);
1391 DECL_INITIAL (r) = TREE_TYPE (r);
1392 if (TREE_CHAIN (t))
1393 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1394 return r;
1395 }
1396
1397 case TREE_LIST:
1398 {
1399 tree purpose, value, chain, result;
1400 int via_public, via_virtual, via_protected;
1401
1402 if (t == void_list_node)
1403 return t;
1404
1405 via_public = TREE_VIA_PUBLIC (t);
1406 via_protected = TREE_VIA_PROTECTED (t);
1407 via_virtual = TREE_VIA_VIRTUAL (t);
1408
1409 purpose = TREE_PURPOSE (t);
1410 if (purpose)
1411 purpose = tsubst (purpose, args, nargs, in_decl);
1412 value = TREE_VALUE (t);
1413 if (value)
1414 value = tsubst (value, args, nargs, in_decl);
1415 chain = TREE_CHAIN (t);
1416 if (chain && chain != void_type_node)
1417 chain = tsubst (chain, args, nargs, in_decl);
1418 if (purpose == TREE_PURPOSE (t)
1419 && value == TREE_VALUE (t)
1420 && chain == TREE_CHAIN (t))
1421 return t;
1422 result = hash_tree_cons (via_public, via_virtual, via_protected,
1423 purpose, value, chain);
1424 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1425 return result;
1426 }
1427 case TREE_VEC:
1428 {
1429 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1430 tree *elts = (tree *) alloca (len * sizeof (tree));
1431 bzero (elts, len * sizeof (tree));
1432
1433 for (i = 0; i < len; i++)
1434 {
1435 elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1436 if (elts[i] != TREE_VEC_ELT (t, i))
1437 need_new = 1;
1438 }
1439
1440 if (!need_new)
1441 return t;
1442
1443 t = make_tree_vec (len);
1444 for (i = 0; i < len; i++)
1445 TREE_VEC_ELT (t, i) = elts[i];
1446 return t;
1447 }
1448 case POINTER_TYPE:
1449 case REFERENCE_TYPE:
1450 {
1451 tree r;
1452 enum tree_code code;
1453 if (type == TREE_TYPE (t))
1454 return t;
1455
1456 code = TREE_CODE (t);
1457 if (code == POINTER_TYPE)
1458 r = build_pointer_type (type);
1459 else
1460 r = build_reference_type (type);
1461 r = build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1462 /* Will this ever be needed for TYPE_..._TO values? */
1463 layout_type (r);
1464 return r;
1465 }
a4443a08
MS
1466 case OFFSET_TYPE:
1467 return build_offset_type
1468 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
8d08fdba
MS
1469 case FUNCTION_TYPE:
1470 case METHOD_TYPE:
1471 {
1472 tree values = TYPE_VALUES (t); /* same as TYPE_ARG_TYPES */
1473 tree context = TYPE_CONTEXT (t);
1474 tree new_value;
1475
1476 /* Don't bother recursing if we know it won't change anything. */
1477 if (values != void_list_node)
1478 values = tsubst (values, args, nargs, in_decl);
1479 if (context)
1480 context = tsubst (context, args, nargs, in_decl);
1481 /* Could also optimize cases where return value and
1482 values have common elements (e.g., T min(const &T, const T&). */
1483
1484 /* If the above parameters haven't changed, just return the type. */
1485 if (type == TREE_TYPE (t)
1486 && values == TYPE_VALUES (t)
1487 && context == TYPE_CONTEXT (t))
1488 return t;
1489
1490 /* Construct a new type node and return it. */
1491 if (TREE_CODE (t) == FUNCTION_TYPE
1492 && context == NULL_TREE)
1493 {
1494 new_value = build_function_type (type, values);
1495 }
1496 else if (context == NULL_TREE)
1497 {
1498 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1499 args, nargs, in_decl);
1500 new_value = build_cplus_method_type (base, type,
1501 TREE_CHAIN (values));
1502 }
1503 else
1504 {
1505 new_value = make_node (TREE_CODE (t));
1506 TREE_TYPE (new_value) = type;
1507 TYPE_CONTEXT (new_value) = context;
1508 TYPE_VALUES (new_value) = values;
1509 TYPE_SIZE (new_value) = TYPE_SIZE (t);
1510 TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
1511 TYPE_MODE (new_value) = TYPE_MODE (t);
1512 if (TYPE_METHOD_BASETYPE (t))
1513 TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
1514 args, nargs, in_decl);
1515 /* Need to generate hash value. */
1516 my_friendly_abort (84);
1517 }
1518 new_value = build_type_variant (new_value,
1519 TYPE_READONLY (t),
1520 TYPE_VOLATILE (t));
1521 return new_value;
1522 }
1523 case ARRAY_TYPE:
1524 {
1525 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1526 tree r;
1527 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1528 return t;
1529 r = build_cplus_array_type (type, domain);
1530 return r;
1531 }
1532
1533 case UNINSTANTIATED_P_TYPE:
1534 {
1535 int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
1536 tree argvec = make_tree_vec (nparms);
1537 tree parmvec = UPT_PARMS (t);
1538 int i;
1539 tree id, rt;
1540 for (i = 0; i < nparms; i++)
1541 TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
1542 args, nargs, in_decl);
1543 id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
1544 if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
1545 instantiate_class_template(id, 0);
1546 /* set up pending_classes */
1547 add_pending_template (id);
1548
1549 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
1550 IDENTIFIER_TYPE_VALUE (id);
1551 }
1552 rt = IDENTIFIER_TYPE_VALUE (id);
1553
1554 /* kung: this part handles nested type in template definition */
1555
1556 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t))))
1557 {
1558 rt = search_nested_type_in_tmpl (rt, t);
1559 }
1560
1561 return build_type_variant (rt, TYPE_READONLY (t), TYPE_VOLATILE (t));
1562 }
1563
1564 case MINUS_EXPR:
1565 case PLUS_EXPR:
1566 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1567 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1568 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1569
1570 case NEGATE_EXPR:
1571 case NOP_EXPR:
1572 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1573 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1574
1575 default:
1576 sorry ("use of `%s' in function template",
1577 tree_code_name [(int) TREE_CODE (t)]);
1578 return error_mark_node;
1579 }
1580}
1581
1582tree
1583instantiate_template (tmpl, targ_ptr)
1584 tree tmpl, *targ_ptr;
1585{
1586 tree targs, fndecl;
1587 int i, len;
1588 struct pending_inline *p;
1589 struct template_info *t;
1590 struct obstack *old_fmp_obstack;
1591 extern struct obstack *function_maybepermanent_obstack;
1592
1593 push_obstacks (&permanent_obstack, &permanent_obstack);
1594 old_fmp_obstack = function_maybepermanent_obstack;
1595 function_maybepermanent_obstack = &permanent_obstack;
1596
1597 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
1598 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
1599
1600 for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1601 fndecl; fndecl = TREE_CHAIN (fndecl))
1602 {
1603 tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
1604 for (i = len - 1; i >= 0; i--)
1605 if (t1[i] != targ_ptr[i])
1606 goto no_match;
1607
1608 /* Here, we have a match. */
1609 fndecl = TREE_VALUE (fndecl);
1610 function_maybepermanent_obstack = old_fmp_obstack;
1611 pop_obstacks ();
1612 return fndecl;
1613
1614 no_match:
1615 ;
1616 }
1617
1618 targs = make_tree_vec (len);
1619 i = len;
1620 while (i--)
1621 TREE_VEC_ELT (targs, i) = targ_ptr[i];
1622
1623 /* substitute template parameters */
1624 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
1625 TREE_VEC_LENGTH (targs), tmpl);
1626
1627 /* If it's a static member fn in the template, we need to change it
1628 into a FUNCTION_TYPE and chop off its this pointer. */
1629 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
1630 && fndecl != error_mark_node
1631 && DECL_STATIC_FUNCTION_P (fndecl))
1632 {
1633 tree olddecl = DECL_RESULT (tmpl);
700f8a87 1634 revert_static_member_fn (&DECL_RESULT (tmpl), NULL, NULL);
8d08fdba
MS
1635 /* Chop off the this pointer that grokclassfn so kindly added
1636 for us (it didn't know yet if the fn was static or not). */
1637 DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
1638 DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1639 }
1640
1641 t = DECL_TEMPLATE_INFO (tmpl);
1642 if (t->text)
1643 {
1644 p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
1645 p->parm_vec = t->parm_vec;
1646 p->bindings = targs;
1647 p->can_free = 0;
1648 p->deja_vu = 0;
1649 p->buf = t->text;
1650 p->len = t->length;
1651 p->fndecl = fndecl;
1652 {
1653 int l = lineno;
1654 char * f = input_filename;
1655
1656 lineno = p->lineno = t->lineno;
1657 input_filename = p->filename = t->filename;
1658
1659 extract_interface_info ();
1660
1661 if (interface_unknown && flag_external_templates && ! DECL_IN_SYSTEM_HEADER (tmpl))
1662 warn_if_unknown_interface ();
1663 if (interface_unknown || !flag_external_templates)
1664 p->interface = 1; /* unknown */
1665 else
1666 p->interface = interface_only ? 0 : 2;
1667
1668 lineno = l;
1669 input_filename = f;
1670
1671 extract_interface_info ();
1672 }
1673 }
1674 else
1675 p = (struct pending_inline *)0;
1676
1677 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1678 tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1679
1680 function_maybepermanent_obstack = old_fmp_obstack;
1681 pop_obstacks ();
1682
1683 if (fndecl == error_mark_node || p == (struct pending_inline *)0)
1684 {
1685 /* do nothing */
1686 }
1687 else if (DECL_INLINE (fndecl))
1688 {
1689 DECL_PENDING_INLINE_INFO (fndecl) = p;
1690 p->next = pending_inlines;
1691 pending_inlines = p;
1692 }
1693 else
1694 {
1695 p->next = pending_template_expansions;
1696 pending_template_expansions = p;
1697 }
1698 return fndecl;
1699}
1700
a28e3c7f 1701/* classlevel should now never be true. jason 4/12/94 */
8d08fdba
MS
1702void
1703undo_template_name_overload (id, classlevel)
1704 tree id;
1705 int classlevel;
1706{
1707 tree template;
1708
1709 template = IDENTIFIER_TEMPLATE (id);
1710 if (!template)
1711 return;
1712
1713#if 0 /* not yet, should get fixed properly later */
1714 poplevel (0, 0, 0);
1715#endif
1716#if 1 /* XXX */
1717 /* This was a botch... See `overload_template_name' just below. */
1718 if (!classlevel)
1719 poplevel (0, 0, 0);
1720#endif
1721}
1722
a28e3c7f 1723/* classlevel should now never be true. jason 4/12/94 */
8d08fdba
MS
1724void
1725overload_template_name (id, classlevel)
1726 tree id;
1727 int classlevel;
1728{
1729 tree template, t, decl;
1730 struct template_info *tinfo;
1731
1732 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
1733 template = IDENTIFIER_TEMPLATE (id);
1734 if (!template)
1735 return;
1736
1737 template = TREE_PURPOSE (template);
1738 tinfo = DECL_TEMPLATE_INFO (template);
1739 template = DECL_NAME (template);
1740 my_friendly_assert (template != NULL_TREE, 285);
1741
1742#if 1 /* XXX */
1743 /* This was a botch... names of templates do not get their own private
a28e3c7f
MS
1744 scopes. Rather, they should go into the binding level already created
1745 by push_template_decls. Except that there isn't one of those for
1746 specializations. */
8d08fdba
MS
1747 if (!classlevel)
1748 {
1749 pushlevel (1);
1750 declare_pseudo_global_level ();
1751 }
1752#endif
1753
1754 t = xref_tag (tinfo->aggr, id, NULL_TREE, 0);
1755 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1756 || TREE_CODE (t) == UNION_TYPE
1757 || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
1758
1759 decl = build_decl (TYPE_DECL, template, t);
1760
1761#if 0 /* fix this later */
1762 /* We don't want to call here if the work has already been done. */
1763 t = (classlevel
1764 ? IDENTIFIER_CLASS_VALUE (template)
1765 : IDENTIFIER_LOCAL_VALUE (template));
1766 if (t
1767 && TREE_CODE (t) == TYPE_DECL
1768 && TREE_TYPE (t) == t)
1769 my_friendly_abort (85);
1770#endif
1771
1772 if (classlevel)
1773 pushdecl_class_level (decl);
1774 else
8d08fdba 1775 pushdecl (decl);
8d08fdba 1776
a28e3c7f 1777#if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
8d08fdba
MS
1778 /* Fake this for now, just to make dwarfout.c happy. It will have to
1779 be done in a proper way later on. */
1780 DECL_CONTEXT (decl) = t;
a28e3c7f 1781#endif
8d08fdba
MS
1782}
1783
1784/* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1785void
1786end_template_instantiation (name)
1787 tree name;
1788{
1789 extern struct pending_input *to_be_restored;
1790 tree t, decl;
1791
1792 processing_template_defn--;
1793 if (!flag_external_templates)
1794 interface_unknown--;
1795
1796 /* Restore the old parser input state. */
1797 if (yychar == YYEMPTY)
1798 yychar = yylex ();
1799 if (yychar != END_OF_SAVED_INPUT)
1800 error ("parse error at end of class template");
1801 else
1802 {
1803 restore_pending_input (to_be_restored);
1804 to_be_restored = 0;
1805 }
1806
1807 /* Our declarations didn't get stored in the global slot, since
1808 there was a (supposedly tags-transparent) scope in between. */
1809 t = IDENTIFIER_TYPE_VALUE (name);
1810 my_friendly_assert (t != NULL_TREE
1811 && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
1812 287);
1813 CLASSTYPE_USE_TEMPLATE (t) = 2;
1814 /* Make methods of template classes static, unless
1815 -fexternal-templates is given. */
1816 if (!flag_external_templates)
1817 SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
1818 decl = IDENTIFIER_GLOBAL_VALUE (name);
1819 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
1820
1821 undo_template_name_overload (name, 0);
1822 t = IDENTIFIER_TEMPLATE (name);
1823 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
1824 0);
1825 /* This will fix up the type-value field. */
1826 pushdecl (decl);
1827 pop_from_top_level ();
1828
1829#ifdef DWARF_DEBUGGING_INFO
1830 if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
1831 {
1832 /* We just completed the definition of a new file-scope type,
1833 so we can go ahead and output debug-info for it now. */
1834 TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
1835 rest_of_type_compilation (TREE_TYPE (decl), 1);
1836 }
1837#endif /* DWARF_DEBUGGING_INFO */
1838
1839 /* Restore interface/implementation settings. */
1840 extract_interface_info ();
1841}
1842\f
51c184be 1843/* Store away the text of an template. */
8d08fdba
MS
1844
1845void
1846reinit_parse_for_template (yychar, d1, d2)
1847 int yychar;
1848 tree d1, d2;
1849{
1850 struct template_info *template_info;
51c184be 1851 extern struct obstack inline_text_obstack; /* see comment in lex.c */
8d08fdba
MS
1852
1853 if (d2 == NULL_TREE || d2 == error_mark_node)
1854 {
1855 lose:
1856 /* @@ Should use temp obstack, and discard results. */
1857 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1858 return;
1859 }
1860
1861 if (TREE_CODE (d2) == IDENTIFIER_NODE)
1862 d2 = IDENTIFIER_GLOBAL_VALUE (d2);
1863 if (!d2)
1864 goto lose;
1865 template_info = DECL_TEMPLATE_INFO (d2);
1866 if (!template_info)
1867 {
1868 template_info = (struct template_info *) permalloc (sizeof (struct template_info));
1869 bzero (template_info, sizeof (struct template_info));
1870 DECL_TEMPLATE_INFO (d2) = template_info;
1871 }
1872 template_info->filename = input_filename;
1873 template_info->lineno = lineno;
1874 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1875 template_info->text = obstack_base (&inline_text_obstack);
1876 template_info->length = obstack_object_size (&inline_text_obstack);
1877 obstack_finish (&inline_text_obstack);
1878 template_info->parm_vec = d1;
1879}
1880
1881/* Type unification.
1882
1883 We have a function template signature with one or more references to
1884 template parameters, and a parameter list we wish to fit to this
1885 template. If possible, produce a list of parameters for the template
1886 which will cause it to fit the supplied parameter list.
1887
1888 Return zero for success, 2 for an incomplete match that doesn't resolve
1889 all the types, and 1 for complete failure. An error message will be
1890 printed only for an incomplete match.
1891
1892 TPARMS[NTPARMS] is an array of template parameter types;
1893 TARGS[NTPARMS] is the array of template parameter values. PARMS is
1894 the function template's signature (using TEMPLATE_PARM_IDX nodes),
1895 and ARGS is the argument list we're trying to match against it.
1896
1897 If SUBR is 1, we're being called recursively (to unify the arguments of
1898 a function or method parameter of a function template), so don't zero
1899 out targs and don't fail on an incomplete match. */
1900
1901int
1902type_unification (tparms, targs, parms, args, nsubsts, subr)
1903 tree tparms, *targs, parms, args;
1904 int *nsubsts, subr;
1905{
1906 tree parm, arg;
1907 int i;
1908 int ntparms = TREE_VEC_LENGTH (tparms);
1909
1910 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
1911 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
51c184be 1912 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
1913 build_x_function_call). */
1914 if (args)
1915 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
1916 my_friendly_assert (ntparms > 0, 292);
1917
1918 if (!subr)
1919 bzero (targs, sizeof (tree) * ntparms);
1920
1921 while (parms
1922 && parms != void_list_node
1923 && args
1924 && args != void_list_node)
1925 {
1926 parm = TREE_VALUE (parms);
1927 parms = TREE_CHAIN (parms);
1928 arg = TREE_VALUE (args);
1929 args = TREE_CHAIN (args);
1930
1931 if (arg == error_mark_node)
1932 return 1;
1933 if (arg == unknown_type_node)
1934 return 1;
1935#if 0
1936 if (TREE_CODE (arg) == VAR_DECL)
1937 arg = TREE_TYPE (arg);
1938 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
1939 arg = TREE_TYPE (arg);
1940#else
1941 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
1942 {
1943 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
1944 arg = TREE_TYPE (arg);
1945 }
1946#endif
1947 if (TREE_CODE (arg) == FUNCTION_TYPE
1948 || TREE_CODE (arg) == METHOD_TYPE)
1949 arg = build_pointer_type (arg);
1950
1951 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
1952 {
1953 case 0:
1954 break;
1955 case 1:
1956 return 1;
1957 }
1958 }
1959 /* Fail if we've reached the end of the parm list, and more args
1960 are present, and the parm list isn't variadic. */
1961 if (args && args != void_list_node && parms == void_list_node)
1962 return 1;
1963 /* Fail if parms are left and they don't have default values. */
1964 if (parms
1965 && parms != void_list_node
1966 && TREE_PURPOSE (parms) == NULL_TREE)
1967 return 1;
1968 if (!subr)
1969 for (i = 0; i < ntparms; i++)
1970 if (!targs[i])
1971 {
1972 error ("incomplete type unification");
1973 return 2;
1974 }
1975 return 0;
1976}
1977
1978/* Tail recursion is your friend. */
1979static int
1980unify (tparms, targs, ntparms, parm, arg, nsubsts)
1981 tree tparms, *targs, parm, arg;
1982 int *nsubsts, ntparms;
1983{
1984 int idx;
1985
1986 /* I don't think this will do the right thing with respect to types.
1987 But the only case I've seen it in so far has been array bounds, where
1988 signedness is the only information lost, and I think that will be
1989 okay. */
1990 while (TREE_CODE (parm) == NOP_EXPR)
1991 parm = TREE_OPERAND (parm, 0);
1992
1993 if (arg == error_mark_node)
1994 return 1;
1995 if (arg == unknown_type_node)
1996 return 1;
1997 if (arg == parm)
1998 return 0;
1999
2000 if (TREE_CODE (arg) == REFERENCE_TYPE)
2001 arg = TREE_TYPE (arg);
2002
2003 switch (TREE_CODE (parm))
2004 {
2005 case TEMPLATE_TYPE_PARM:
2006 (*nsubsts)++;
2007 if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
2008 {
2009 error ("mixed template headers?!");
2010 my_friendly_abort (86);
2011 return 1;
2012 }
2013 idx = TEMPLATE_TYPE_IDX (parm);
2014 /* Simple cases: Value already set, does match or doesn't. */
2015 if (targs[idx] == arg)
2016 return 0;
2017 else if (targs[idx])
a4443a08
MS
2018 {
2019 if (TYPE_MAIN_VARIANT (targs[idx]) == TYPE_MAIN_VARIANT (arg))
2020 /* allow different parms to have different cv-qualifiers */;
2021 else
2022 return 1;
2023 }
8d08fdba
MS
2024 /* Check for mixed types and values. */
2025 if (TREE_CODE (TREE_VEC_ELT (tparms, idx)) != IDENTIFIER_NODE)
2026 return 1;
a4443a08
MS
2027 /* Allow trivial conversions. */
2028 if (TYPE_READONLY (parm) < TYPE_READONLY (arg)
2029 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2030 return 1;
8d08fdba
MS
2031 targs[idx] = arg;
2032 return 0;
2033 case TEMPLATE_CONST_PARM:
2034 (*nsubsts)++;
2035 idx = TEMPLATE_CONST_IDX (parm);
2036 if (targs[idx] == arg)
2037 return 0;
2038 else if (targs[idx])
2039 {
a28e3c7f
MS
2040 tree t = targs[idx];
2041 if (TREE_CODE (t) == TREE_CODE (arg))
2042 switch (TREE_CODE (arg))
2043 {
2044 case INTEGER_CST:
2045 if (tree_int_cst_equal (t, arg))
2046 return 0;
2047 break;
2048 case REAL_CST:
2049 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2050 return 0;
2051 break;
2052 /* STRING_CST values are not valid template const parms. */
2053 default:
2054 ;
2055 }
8d08fdba
MS
2056 my_friendly_abort (87);
2057 return 1;
2058 }
2059/* else if (typeof arg != tparms[idx])
2060 return 1;*/
2061
2062 targs[idx] = copy_to_permanent (arg);
2063 return 0;
2064
2065 case POINTER_TYPE:
2066 if (TREE_CODE (arg) != POINTER_TYPE)
2067 return 1;
2068 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2069 nsubsts);
2070
2071 case REFERENCE_TYPE:
2072 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
2073
2074 case ARRAY_TYPE:
2075 if (TREE_CODE (arg) != ARRAY_TYPE)
2076 return 1;
2077 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2078 nsubsts) != 0)
2079 return 1;
2080 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2081 nsubsts);
2082
2083 case REAL_TYPE:
2084 case INTEGER_TYPE:
2085 if (TREE_CODE (parm) == INTEGER_TYPE && TREE_CODE (arg) == INTEGER_TYPE)
2086 {
2087 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2088 && unify (tparms, targs, ntparms,
2089 TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
2090 return 1;
2091 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2092 && unify (tparms, targs, ntparms,
2093 TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
2094 return 1;
2095 }
2096 /* As far as unification is concerned, this wins. Later checks
2097 will invalidate it if necessary. */
2098 return 0;
2099
2100 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2101 case INTEGER_CST:
2102 if (TREE_CODE (arg) != INTEGER_CST)
2103 return 1;
2104 return !tree_int_cst_equal (parm, arg);
2105
2106 case MINUS_EXPR:
2107 {
2108 tree t1, t2;
2109 t1 = TREE_OPERAND (parm, 0);
2110 t2 = TREE_OPERAND (parm, 1);
2111 if (TREE_CODE (t1) != TEMPLATE_CONST_PARM)
2112 return 1;
2113 return unify (tparms, targs, ntparms, t1,
2114 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2115 nsubsts);
2116 }
2117
2118 case TREE_VEC:
2119 {
2120 int i;
2121 if (TREE_CODE (arg) != TREE_VEC)
2122 return 1;
2123 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2124 return 1;
2125 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2126 if (unify (tparms, targs, ntparms,
2127 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2128 nsubsts))
2129 return 1;
2130 return 0;
2131 }
2132
2133 case UNINSTANTIATED_P_TYPE:
2134 {
2135 tree a;
2136 /* Unification of something that is not a template fails. (mrs) */
2137 if (TYPE_NAME (arg) == 0)
2138 return 1;
2139 a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
2140 /* Unification of something that is not a template fails. (mrs) */
2141 if (a == 0)
2142 return 1;
2143 if (UPT_TEMPLATE (parm) != TREE_PURPOSE (a))
2144 /* different templates */
2145 return 1;
2146 return unify (tparms, targs, ntparms, UPT_PARMS (parm), TREE_VALUE (a),
2147 nsubsts);
2148 }
2149
2150 case RECORD_TYPE:
2151 if (TYPE_PTRMEMFUNC_P (parm))
2152 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2153 arg, nsubsts);
2154
a4443a08
MS
2155 /* Allow trivial conversions. */
2156 if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)
2157 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2158 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2159 return 1;
2160 return 0;
8d08fdba
MS
2161
2162 case METHOD_TYPE:
2163 if (TREE_CODE (arg) != METHOD_TYPE)
2164 return 1;
2165 goto check_args;
2166
2167 case FUNCTION_TYPE:
2168 if (TREE_CODE (arg) != FUNCTION_TYPE)
2169 return 1;
2170 check_args:
2171 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2172 TYPE_ARG_TYPES (arg), nsubsts, 1);
a4443a08
MS
2173
2174 case OFFSET_TYPE:
2175 if (TREE_CODE (arg) != OFFSET_TYPE)
2176 return 1;
2177 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2178 TYPE_OFFSET_BASETYPE (arg), nsubsts))
2179 return 1;
2180 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2181 TREE_TYPE (arg), nsubsts);
2182
8d08fdba
MS
2183 default:
2184 sorry ("use of `%s' in template type unification",
2185 tree_code_name [(int) TREE_CODE (parm)]);
2186 return 1;
2187 }
2188}
2189
2190\f
2191#undef DEBUG
2192
2193int
2194do_pending_expansions ()
2195{
2196 struct pending_inline *i, *new_list = 0;
2197
2198 if (!pending_template_expansions)
2199 return 0;
2200
2201#ifdef DEBUG
2202 fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2203#endif
2204
2205 i = pending_template_expansions;
2206 while (i)
2207 {
2208 tree context;
2209
2210 struct pending_inline *next = i->next;
2211 tree t = i->fndecl;
2212
2213 int decision = 0;
8926095f 2214#define DECIDE(N) do {decision=(N); goto decided;} while(0)
8d08fdba
MS
2215
2216 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
2217 || TREE_CODE (t) == VAR_DECL, 294);
2218 if (TREE_ASM_WRITTEN (t))
2219 DECIDE (0);
7177d104
MS
2220
2221 if (DECL_EXPLICITLY_INSTANTIATED (t))
2222 DECIDE (1);
2223
8d08fdba
MS
2224 /* If it's a method, let the class type decide it.
2225 @@ What if the method template is in a separate file?
2226 Maybe both file contexts should be taken into account?
2227 Maybe only do this if i->interface == 1 (unknown)? */
2228 context = DECL_CONTEXT (t);
2229 if (context != NULL_TREE
2230 && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
2231 {
2232 /* I'm interested in the context of this version of the function,
2233 not the original virtual declaration. */
2234 context = DECL_CLASS_CONTEXT (t);
2235
2236 /* If `unknown', we might want a static copy.
2237 If `implementation', we want a global one.
2238 If `interface', ext ref. */
2239 if (CLASSTYPE_INTERFACE_KNOWN (context))
2240 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
2241#if 0 /* This doesn't get us stuff needed only by the file initializer. */
2242 DECIDE (TREE_USED (t));
2243#else /* This compiles too much stuff, but that's probably better in
2244 most cases than never compiling the stuff we need. */
2245 DECIDE (1);
2246#endif
2247 }
2248
2249 if (i->interface == 1)
2250 DECIDE (TREE_USED (t));
2251 else
2252 DECIDE (i->interface);
2253
2254 decided:
2255#ifdef DEBUG
2256 print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
2257 fprintf (stderr, "\t%s\n",
2258 (DECL_ASSEMBLER_NAME (t)
2259 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
2260 : ""));
2261#endif
2262 if (decision)
2263 {
2264 i->next = pending_inlines;
2265 pending_inlines = i;
2266 }
2267 else
2268 {
2269 i->next = new_list;
2270 new_list = i;
2271 }
2272 i = next;
2273 }
2274 pending_template_expansions = new_list;
2275 if (!pending_inlines)
2276 return 0;
2277 do_pending_inlines ();
2278 return 1;
2279}
2280
2281\f
2282struct pending_template {
2283 struct pending_template *next;
2284 tree id;
2285};
2286
2287static struct pending_template* pending_templates;
2288
2289void
2290do_pending_templates ()
2291{
2292 struct pending_template* t;
2293
2294 for ( t = pending_templates; t; t = t->next)
2295 {
2296 instantiate_class_template (t->id, 1);
2297 }
2298
2299 for ( t = pending_templates; t; t = pending_templates)
2300 {
2301 pending_templates = t->next;
2302 free(t);
2303 }
2304}
2305
2306static void
2307add_pending_template (pt)
2308 tree pt;
2309{
2310 struct pending_template *p;
2311
2312 p = (struct pending_template *) malloc (sizeof (struct pending_template));
2313 p->next = pending_templates;
2314 pending_templates = p;
2315 p->id = pt;
2316}
2317
2318/* called from the parser. */
2319void
2320do_function_instantiation (declspecs, declarator)
2321 tree declspecs, declarator;
2322{
2323 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, 0);
2324 tree name = DECL_NAME (decl);
2325 tree fn = IDENTIFIER_GLOBAL_VALUE (name);
2326 tree result = NULL_TREE;
2327 if (fn)
2328 {
2329 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
2330 if (TREE_CODE (fn) == TEMPLATE_DECL)
2331 {
2332 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2333 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2334 int i, dummy;
2335 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2336 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2337 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2338 &dummy, 0);
2339 if (i == 0)
2340 {
2341 if (result)
2342 cp_error ("ambiguous template instantiation for `%D' requested", decl);
2343 else
2344 result = instantiate_template (fn, targs);
2345 }
2346 }
2347 }
7177d104 2348 if (! result)
8d08fdba 2349 cp_error ("no matching template for `%D' found", decl);
7177d104
MS
2350
2351 DECL_EXPLICITLY_INSTANTIATED (result) = 1;
2352}
2353
2354void
2355do_type_instantiation (name)
2356 tree name;
2357{
2358 tree t = TREE_TYPE (name);
2359
2360 CLASSTYPE_EXPLICITLY_INSTANTIATED (t) = 1;
2361 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
2362
2363 /* this should really be done by instantiate_member_templates */
2364 {
2365 tree method = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2366 for (; method; method = TREE_CHAIN (method))
2367 DECL_EXPLICITLY_INSTANTIATED (method) = 1;
2368 }
2369
2370 /* and data member templates, too */
8d08fdba 2371}
a28e3c7f
MS
2372
2373tree
2374create_nested_upt (scope, name)
2375 tree scope, name;
2376{
2377 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
2378 tree d = build_lang_decl (TYPE_DECL, name, t);
2379
2380 TYPE_NAME (t) = d;
2381 TYPE_VALUES (t) = TYPE_VALUES (scope);
2382 TYPE_CONTEXT (t) = scope;
2383
2384 pushdecl (d);
2385 return d;
2386}
This page took 0.272584 seconds and 5 git commands to generate.