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