00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "config.h"
00034 #include "system.h"
00035 #include "coretypes.h"
00036 #include "tm.h"
00037 #include "obstack.h"
00038 #include "tree.h"
00039 #include "pointer-set.h"
00040 #include "flags.h"
00041 #include "cp-tree.h"
00042 #include "tree-inline.h"
00043 #include "decl.h"
00044 #include "output.h"
00045 #include "except.h"
00046 #include "toplev.h"
00047 #include "rtl.h"
00048 #include "timevar.h"
00049 #include "tree-iterator.h"
00050
00051
00052
00053 typedef int (*tree_fn_t) (tree, void*);
00054
00055
00056
00057
00058
00059
00060
00061 static GTY(()) tree pending_templates;
00062 static GTY(()) tree last_pending_template;
00063
00064 int processing_template_parmlist;
00065 static int template_header_count;
00066
00067 static GTY(()) tree saved_trees;
00068 static GTY(()) varray_type inline_parm_levels;
00069 static size_t inline_parm_levels_used;
00070
00071 static GTY(()) tree current_tinst_level;
00072
00073 static GTY(()) tree saved_access_scope;
00074
00075
00076
00077
00078 static tree cur_stmt_expr;
00079
00080
00081
00082
00083 static htab_t local_specializations;
00084
00085 #define UNIFY_ALLOW_NONE 0
00086 #define UNIFY_ALLOW_MORE_CV_QUAL 1
00087 #define UNIFY_ALLOW_LESS_CV_QUAL 2
00088 #define UNIFY_ALLOW_DERIVED 4
00089 #define UNIFY_ALLOW_INTEGER 8
00090 #define UNIFY_ALLOW_OUTER_LEVEL 16
00091 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
00092 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
00093 #define UNIFY_ALLOW_MAX_CORRECTION 128
00094
00095 static void push_access_scope (tree);
00096 static void pop_access_scope (tree);
00097 static int resolve_overloaded_unification (tree, tree, tree, tree,
00098 unification_kind_t, int);
00099 static int try_one_overload (tree, tree, tree, tree, tree,
00100 unification_kind_t, int, bool);
00101 static int unify (tree, tree, tree, tree, int);
00102 static void add_pending_template (tree);
00103 static void reopen_tinst_level (tree);
00104 static tree classtype_mangled_name (tree);
00105 static char* mangle_class_name_for_template (const char *, tree, tree);
00106 static tree tsubst_initializer_list (tree, tree);
00107 static tree get_class_bindings (tree, tree, tree);
00108 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
00109 static void tsubst_enum (tree, tree, tree);
00110 static tree add_to_template_args (tree, tree);
00111 static tree add_outermost_template_args (tree, tree);
00112 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
00113 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
00114 static int type_unification_real (tree, tree, tree, tree,
00115 int, unification_kind_t, int, int);
00116 static void note_template_header (int);
00117 static tree convert_nontype_argument_function (tree, tree);
00118 static tree convert_nontype_argument (tree, tree);
00119 static tree convert_template_argument (tree, tree, tree,
00120 tsubst_flags_t, int, tree);
00121 static tree get_bindings_overload (tree, tree, tree);
00122 static int for_each_template_parm (tree, tree_fn_t, void*,
00123 struct pointer_set_t*);
00124 static tree build_template_parm_index (int, int, int, tree, tree);
00125 static int inline_needs_template_parms (tree);
00126 static void push_inline_template_parms_recursive (tree, int);
00127 static tree retrieve_local_specialization (tree);
00128 static void register_local_specialization (tree, tree);
00129 static tree reduce_template_parm_level (tree, tree, int);
00130 static int mark_template_parm (tree, void *);
00131 static int template_parm_this_level_p (tree, void *);
00132 static tree tsubst_friend_function (tree, tree);
00133 static tree tsubst_friend_class (tree, tree);
00134 static int can_complete_type_without_circularity (tree);
00135 static tree get_bindings (tree, tree, tree);
00136 static tree get_bindings_real (tree, tree, tree, int, int, int);
00137 static int template_decl_level (tree);
00138 static int check_cv_quals_for_unify (int, tree, tree);
00139 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
00140 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
00141 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
00142 static void regenerate_decl_from_template (tree, tree);
00143 static tree most_specialized (tree, tree, tree);
00144 static tree most_specialized_class (tree, tree);
00145 static int template_class_depth_real (tree, int);
00146 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
00147 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
00148 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
00149 static void check_specialization_scope (void);
00150 static tree process_partial_specialization (tree);
00151 static void set_current_access_from_decl (tree);
00152 static void check_default_tmpl_args (tree, tree, int, int);
00153 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
00154 static tree get_template_base (tree, tree, tree, tree);
00155 static int verify_class_unification (tree, tree, tree);
00156 static tree try_class_unification (tree, tree, tree, tree);
00157 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
00158 tree, tree);
00159 static tree determine_specialization (tree, tree, tree *, int, int);
00160 static int template_args_equal (tree, tree);
00161 static void tsubst_default_arguments (tree);
00162 static tree for_each_template_parm_r (tree *, int *, void *);
00163 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
00164 static void copy_default_args_to_explicit_spec (tree);
00165 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
00166 static int eq_local_specializations (const void *, const void *);
00167 static bool dependent_type_p_r (tree);
00168 static tree tsubst (tree, tree, tsubst_flags_t, tree);
00169 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
00170 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
00171
00172
00173
00174
00175
00176
00177 static void
00178 push_access_scope (tree t)
00179 {
00180 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
00181 || TREE_CODE (t) == VAR_DECL);
00182
00183 if (DECL_FRIEND_CONTEXT (t))
00184 push_nested_class (DECL_FRIEND_CONTEXT (t));
00185 else if (DECL_CLASS_SCOPE_P (t))
00186 push_nested_class (DECL_CONTEXT (t));
00187 else
00188 push_to_top_level ();
00189
00190 if (TREE_CODE (t) == FUNCTION_DECL)
00191 {
00192 saved_access_scope = tree_cons
00193 (NULL_TREE, current_function_decl, saved_access_scope);
00194 current_function_decl = t;
00195 }
00196 }
00197
00198
00199
00200
00201 static void
00202 pop_access_scope (tree t)
00203 {
00204 if (TREE_CODE (t) == FUNCTION_DECL)
00205 {
00206 current_function_decl = TREE_VALUE (saved_access_scope);
00207 saved_access_scope = TREE_CHAIN (saved_access_scope);
00208 }
00209
00210 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
00211 pop_nested_class ();
00212 else
00213 pop_from_top_level ();
00214 }
00215
00216
00217
00218
00219
00220
00221 tree
00222 finish_member_template_decl (tree decl)
00223 {
00224 if (decl == error_mark_node)
00225 return error_mark_node;
00226
00227 gcc_assert (DECL_P (decl));
00228
00229 if (TREE_CODE (decl) == TYPE_DECL)
00230 {
00231 tree type;
00232
00233 type = TREE_TYPE (decl);
00234 if (IS_AGGR_TYPE (type)
00235 && CLASSTYPE_TEMPLATE_INFO (type)
00236 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
00237 {
00238 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
00239 check_member_template (tmpl);
00240 return tmpl;
00241 }
00242 return NULL_TREE;
00243 }
00244 else if (TREE_CODE (decl) == FIELD_DECL)
00245 error ("data member %qD cannot be a member template", decl);
00246 else if (DECL_TEMPLATE_INFO (decl))
00247 {
00248 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
00249 {
00250 check_member_template (DECL_TI_TEMPLATE (decl));
00251 return DECL_TI_TEMPLATE (decl);
00252 }
00253 else
00254 return decl;
00255 }
00256 else
00257 error ("invalid member template declaration %qD", decl);
00258
00259 return error_mark_node;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 static int
00282 template_class_depth_real (tree type, int count_specializations)
00283 {
00284 int depth;
00285
00286 for (depth = 0;
00287 type && TREE_CODE (type) != NAMESPACE_DECL;
00288 type = (TREE_CODE (type) == FUNCTION_DECL)
00289 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
00290 {
00291 if (TREE_CODE (type) != FUNCTION_DECL)
00292 {
00293 if (CLASSTYPE_TEMPLATE_INFO (type)
00294 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
00295 && ((count_specializations
00296 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
00297 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
00298 ++depth;
00299 }
00300 else
00301 {
00302 if (DECL_TEMPLATE_INFO (type)
00303 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
00304 && ((count_specializations
00305 && DECL_TEMPLATE_SPECIALIZATION (type))
00306 || uses_template_parms (DECL_TI_ARGS (type))))
00307 ++depth;
00308 }
00309 }
00310
00311 return depth;
00312 }
00313
00314
00315
00316
00317
00318 int
00319 template_class_depth (tree type)
00320 {
00321 return template_class_depth_real (type, 0);
00322 }
00323
00324
00325
00326
00327 static int
00328 inline_needs_template_parms (tree decl)
00329 {
00330 if (! DECL_TEMPLATE_INFO (decl))
00331 return 0;
00332
00333 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
00334 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
00335 }
00336
00337
00338
00339
00340
00341
00342 static void
00343 push_inline_template_parms_recursive (tree parmlist, int levels)
00344 {
00345 tree parms = TREE_VALUE (parmlist);
00346 int i;
00347
00348 if (levels > 1)
00349 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
00350
00351 ++processing_template_decl;
00352 current_template_parms
00353 = tree_cons (size_int (processing_template_decl),
00354 parms, current_template_parms);
00355 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
00356
00357 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
00358 NULL);
00359 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
00360 {
00361 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
00362 gcc_assert (DECL_P (parm));
00363
00364 switch (TREE_CODE (parm))
00365 {
00366 case TYPE_DECL:
00367 case TEMPLATE_DECL:
00368 pushdecl (parm);
00369 break;
00370
00371 case PARM_DECL:
00372 {
00373
00374
00375
00376
00377 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
00378 TREE_TYPE (parm));
00379 DECL_ARTIFICIAL (decl) = 1;
00380 TREE_CONSTANT (decl) = 1;
00381 TREE_INVARIANT (decl) = 1;
00382 TREE_READONLY (decl) = 1;
00383 DECL_INITIAL (decl) = DECL_INITIAL (parm);
00384 SET_DECL_TEMPLATE_PARM_P (decl);
00385 pushdecl (decl);
00386 }
00387 break;
00388
00389 default:
00390 gcc_unreachable ();
00391 }
00392 }
00393 }
00394
00395
00396
00397
00398 void
00399 maybe_begin_member_template_processing (tree decl)
00400 {
00401 tree parms;
00402 int levels = 0;
00403
00404 if (inline_needs_template_parms (decl))
00405 {
00406 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
00407 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
00408
00409 if (DECL_TEMPLATE_SPECIALIZATION (decl))
00410 {
00411 --levels;
00412 parms = TREE_CHAIN (parms);
00413 }
00414
00415 push_inline_template_parms_recursive (parms, levels);
00416 }
00417
00418
00419
00420 if (!inline_parm_levels)
00421 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
00422 if (inline_parm_levels_used == inline_parm_levels->num_elements)
00423 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
00424 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
00425 ++inline_parm_levels_used;
00426 }
00427
00428
00429
00430 void
00431 maybe_end_member_template_processing (void)
00432 {
00433 int i;
00434
00435 if (!inline_parm_levels_used)
00436 return;
00437
00438 --inline_parm_levels_used;
00439 for (i = 0;
00440 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
00441 ++i)
00442 {
00443 --processing_template_decl;
00444 current_template_parms = TREE_CHAIN (current_template_parms);
00445 poplevel (0, 0, 0);
00446 }
00447 }
00448
00449
00450
00451
00452 static tree
00453 add_to_template_args (tree args, tree extra_args)
00454 {
00455 tree new_args;
00456 int extra_depth;
00457 int i;
00458 int j;
00459
00460 extra_depth = TMPL_ARGS_DEPTH (extra_args);
00461 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
00462
00463 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
00464 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
00465
00466 for (j = 1; j <= extra_depth; ++j, ++i)
00467 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
00468
00469 return new_args;
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479 static tree
00480 add_outermost_template_args (tree args, tree extra_args)
00481 {
00482 tree new_args;
00483
00484
00485
00486 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
00487
00488
00489
00490 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
00491 return extra_args;
00492
00493
00494 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
00495
00496 new_args = add_to_template_args (args, extra_args);
00497
00498
00499 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
00500
00501 return new_args;
00502 }
00503
00504
00505
00506 tree
00507 get_innermost_template_args (tree args, int n)
00508 {
00509 tree new_args;
00510 int extra_levels;
00511 int i;
00512
00513 gcc_assert (n >= 0);
00514
00515
00516 if (n == 1)
00517 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
00518
00519
00520
00521 extra_levels = TMPL_ARGS_DEPTH (args) - n;
00522 gcc_assert (extra_levels >= 0);
00523 if (extra_levels == 0)
00524 return args;
00525
00526
00527 new_args = make_tree_vec (n);
00528 for (i = 1; i <= n; ++i)
00529 SET_TMPL_ARGS_LEVEL (new_args, i,
00530 TMPL_ARGS_LEVEL (args, i + extra_levels));
00531
00532 return new_args;
00533 }
00534
00535
00536
00537
00538 void
00539 begin_template_parm_list (void)
00540 {
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 begin_scope (sk_template_parms, NULL);
00556 ++processing_template_decl;
00557 ++processing_template_parmlist;
00558 note_template_header (0);
00559 }
00560
00561
00562
00563
00564 static void
00565 check_specialization_scope (void)
00566 {
00567 tree scope = current_scope ();
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
00579 error ("explicit specialization in non-namespace scope %qD", scope);
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 if (current_template_parms)
00590 error ("enclosing class templates are not explicitly specialized");
00591 }
00592
00593
00594
00595 void
00596 begin_specialization (void)
00597 {
00598 begin_scope (sk_template_spec, NULL);
00599 note_template_header (1);
00600 check_specialization_scope ();
00601 }
00602
00603
00604
00605
00606 void
00607 end_specialization (void)
00608 {
00609 finish_scope ();
00610 reset_specialization ();
00611 }
00612
00613
00614
00615
00616 void
00617 reset_specialization (void)
00618 {
00619 processing_specialization = 0;
00620 template_header_count = 0;
00621 }
00622
00623
00624
00625
00626 static void
00627 note_template_header (int specialization)
00628 {
00629 processing_specialization = specialization;
00630 template_header_count++;
00631 }
00632
00633
00634
00635 void
00636 begin_explicit_instantiation (void)
00637 {
00638 gcc_assert (!processing_explicit_instantiation);
00639 processing_explicit_instantiation = true;
00640 }
00641
00642
00643 void
00644 end_explicit_instantiation (void)
00645 {
00646 gcc_assert (processing_explicit_instantiation);
00647 processing_explicit_instantiation = false;
00648 }
00649
00650
00651
00652
00653
00654
00655 static bool
00656 check_specialization_namespace (tree tmpl)
00657 {
00658 tree tpl_ns = decl_namespace_context (tmpl);
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669 if (is_associated_namespace (current_namespace, tpl_ns))
00670
00671 return true;
00672 else
00673 {
00674 pedwarn ("specialization of %qD in different namespace", tmpl);
00675 cp_pedwarn_at (" from definition of %q#D", tmpl);
00676 return false;
00677 }
00678 }
00679
00680
00681
00682
00683 void
00684 maybe_process_partial_specialization (tree type)
00685 {
00686
00687 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
00688
00689 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
00690 {
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
00703 && !COMPLETE_TYPE_P (type))
00704 {
00705 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
00706 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
00707 if (processing_template_decl)
00708 push_template_decl (TYPE_MAIN_DECL (type));
00709 }
00710 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
00711 error ("specialization of %qT after instantiation", type);
00712 }
00713 else if (CLASS_TYPE_P (type)
00714 && !CLASSTYPE_USE_TEMPLATE (type)
00715 && CLASSTYPE_TEMPLATE_INFO (type)
00716 && context && CLASS_TYPE_P (context)
00717 && CLASSTYPE_TEMPLATE_INFO (context))
00718 {
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
00736 && !COMPLETE_TYPE_P (type))
00737 {
00738 tree t;
00739
00740 if (current_namespace
00741 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
00742 {
00743 pedwarn ("specializing %q#T in different namespace", type);
00744 cp_pedwarn_at (" from definition of %q#D",
00745 CLASSTYPE_TI_TEMPLATE (type));
00746 }
00747
00748
00749
00750
00751
00752
00753 for (t = DECL_TEMPLATE_INSTANTIATIONS
00754 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
00755 t; t = TREE_CHAIN (t))
00756 if (TREE_VALUE (t) != type
00757 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
00758 error ("specialization %qT after instantiation %qT",
00759 type, TREE_VALUE (t));
00760
00761
00762
00763
00764 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
00765 CLASSTYPE_TI_ARGS (type)
00766 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
00767 }
00768 }
00769 else if (processing_specialization)
00770 error ("explicit specialization of non-template %qT", type);
00771 }
00772
00773
00774
00775
00776
00777 static inline bool
00778 optimize_specialization_lookup_p (tree tmpl)
00779 {
00780 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
00781 && DECL_CLASS_SCOPE_P (tmpl)
00782
00783
00784 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
00785
00786
00787
00788
00789
00790 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
00791 && !DECL_MEMBER_TEMPLATE_P (tmpl)
00792 && !DECL_CONV_FN_P (tmpl)
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817 static tree
00818 retrieve_specialization (tree tmpl, tree args,
00819 bool class_specializations_p)
00820 {
00821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
00822
00823
00824
00825 gcc_assert (TMPL_ARGS_DEPTH (args)
00826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
00827
00828 if (optimize_specialization_lookup_p (tmpl))
00829 {
00830 tree class_template;
00831 tree class_specialization;
00832 VEC(tree) *methods;
00833 tree fns;
00834 int idx;
00835
00836
00837
00838
00839 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
00840 class_specialization
00841 = retrieve_specialization (class_template, args,
00842 false);
00843 if (!class_specialization)
00844 return NULL_TREE;
00845
00846
00847 idx = class_method_index_for_fn (class_specialization, tmpl);
00848 if (idx == -1)
00849 return NULL_TREE;
00850
00851
00852 methods = CLASSTYPE_METHOD_VEC (class_specialization);
00853 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
00854 {
00855 tree fn = OVL_CURRENT (fns);
00856 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
00857 return fn;
00858 }
00859 return NULL_TREE;
00860 }
00861 else
00862 {
00863 tree *sp;
00864 tree *head;
00865
00866
00867
00868
00869 if (!class_specializations_p
00870 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
00871 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
00872 else
00873 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
00874 head = sp;
00875
00876 while (*sp != NULL_TREE)
00877 {
00878 tree spec = *sp;
00879
00880 if (comp_template_args (TREE_PURPOSE (spec), args))
00881 {
00882
00883
00884 if (spec != *head)
00885 {
00886 *sp = TREE_CHAIN (*sp);
00887 TREE_CHAIN (spec) = *head;
00888 *head = spec;
00889 }
00890 return TREE_VALUE (spec);
00891 }
00892 sp = &TREE_CHAIN (spec);
00893 }
00894 }
00895
00896 return NULL_TREE;
00897 }
00898
00899
00900
00901 static tree
00902 retrieve_local_specialization (tree tmpl)
00903 {
00904 tree spec = htab_find_with_hash (local_specializations, tmpl,
00905 htab_hash_pointer (tmpl));
00906 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
00907 }
00908
00909
00910
00911 int
00912 is_specialization_of (tree decl, tree tmpl)
00913 {
00914 tree t;
00915
00916 if (TREE_CODE (decl) == FUNCTION_DECL)
00917 {
00918 for (t = decl;
00919 t != NULL_TREE;
00920 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
00921 if (t == tmpl)
00922 return 1;
00923 }
00924 else
00925 {
00926 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
00927
00928 for (t = TREE_TYPE (decl);
00929 t != NULL_TREE;
00930 t = CLASSTYPE_USE_TEMPLATE (t)
00931 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
00932 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
00933 return 1;
00934 }
00935
00936 return 0;
00937 }
00938
00939
00940
00941
00942 bool
00943 is_specialization_of_friend (tree decl, tree friend)
00944 {
00945 bool need_template = true;
00946 int template_depth;
00947
00948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
00949 || TREE_CODE (decl) == TYPE_DECL);
00950
00951
00952
00953
00954 if (TREE_CODE (friend) == FUNCTION_DECL
00955 && DECL_TEMPLATE_INFO (friend)
00956 && !DECL_USE_TEMPLATE (friend))
00957 {
00958
00959 friend = DECL_TI_TEMPLATE (friend);
00960 need_template = false;
00961 }
00962 else if (TREE_CODE (friend) == TEMPLATE_DECL
00963 && !PRIMARY_TEMPLATE_P (friend))
00964 need_template = false;
00965
00966
00967 if (TREE_CODE (friend) != TEMPLATE_DECL)
00968 return false;
00969
00970 if (is_specialization_of (decl, friend))
00971 return true;
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993 template_depth = template_class_depth (DECL_CONTEXT (friend));
00994 if (template_depth
00995 && DECL_CLASS_SCOPE_P (decl)
00996 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
00997 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
00998 {
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015 tree context = DECL_CONTEXT (decl);
01016 tree args = NULL_TREE;
01017 int current_depth = 0;
01018
01019 while (current_depth < template_depth)
01020 {
01021 if (CLASSTYPE_TEMPLATE_INFO (context))
01022 {
01023 if (current_depth == 0)
01024 args = TYPE_TI_ARGS (context);
01025 else
01026 args = add_to_template_args (TYPE_TI_ARGS (context), args);
01027 current_depth++;
01028 }
01029 context = TYPE_CONTEXT (context);
01030 }
01031
01032 if (TREE_CODE (decl) == FUNCTION_DECL)
01033 {
01034 bool is_template;
01035 tree friend_type;
01036 tree decl_type;
01037 tree friend_args_type;
01038 tree decl_args_type;
01039
01040
01041
01042 is_template = DECL_TEMPLATE_INFO (decl)
01043 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
01044 if (need_template ^ is_template)
01045 return false;
01046 else if (is_template)
01047 {
01048
01049 tree friend_parms
01050 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
01051 args, tf_none);
01052 if (!comp_template_parms
01053 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
01054 friend_parms))
01055 return false;
01056
01057 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
01058 }
01059 else
01060 decl_type = TREE_TYPE (decl);
01061
01062 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
01063 tf_none, NULL_TREE);
01064 if (friend_type == error_mark_node)
01065 return false;
01066
01067
01068 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
01069 return false;
01070
01071
01072
01073 friend_args_type = TYPE_ARG_TYPES (friend_type);
01074 decl_args_type = TYPE_ARG_TYPES (decl_type);
01075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
01076 friend_args_type = TREE_CHAIN (friend_args_type);
01077 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01078 decl_args_type = TREE_CHAIN (decl_args_type);
01079
01080 return compparms (decl_args_type, friend_args_type);
01081 }
01082 else
01083 {
01084
01085 bool is_template;
01086 tree decl_type = TREE_TYPE (decl);
01087
01088
01089
01090 is_template
01091 = CLASSTYPE_TEMPLATE_INFO (decl_type)
01092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
01093
01094 if (need_template ^ is_template)
01095 return false;
01096 else if (is_template)
01097 {
01098 tree friend_parms;
01099
01100
01101 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
01102 != DECL_NAME (friend))
01103 return false;
01104
01105
01106 friend_parms
01107 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
01108 args, tf_none);
01109 return comp_template_parms
01110 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
01111 friend_parms);
01112 }
01113 else
01114 return (DECL_NAME (decl)
01115 == DECL_NAME (friend));
01116 }
01117 }
01118 return false;
01119 }
01120
01121
01122
01123
01124
01125 static tree
01126 register_specialization (tree spec, tree tmpl, tree args)
01127 {
01128 tree fn;
01129
01130 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
01131
01132 if (TREE_CODE (spec) == FUNCTION_DECL
01133 && uses_template_parms (DECL_TI_ARGS (spec)))
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145 return spec;
01146
01147
01148
01149 gcc_assert (TMPL_ARGS_DEPTH (args)
01150 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
01151
01152 fn = retrieve_specialization (tmpl, args,
01153 false);
01154
01155
01156
01157
01158
01159 if (fn == spec)
01160 return spec;
01161 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
01162 {
01163 if (DECL_TEMPLATE_INSTANTIATION (fn))
01164 {
01165 if (TREE_USED (fn)
01166 || DECL_EXPLICIT_INSTANTIATION (fn))
01167 {
01168 error ("specialization of %qD after instantiation",
01169 fn);
01170 return spec;
01171 }
01172 else
01173 {
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197 DECL_INITIAL (fn) = NULL_TREE;
01198 duplicate_decls (spec, fn);
01199
01200 return fn;
01201 }
01202 }
01203 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
01204 {
01205 if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
01206
01207
01208
01209 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
01210
01211 return fn;
01212 }
01213 }
01214
01215
01216
01217 if (DECL_TEMPLATE_SPECIALIZATION (spec)
01218 && !check_specialization_namespace (tmpl))
01219 DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
01220
01221 if (!optimize_specialization_lookup_p (tmpl))
01222 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
01223 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
01224
01225 return spec;
01226 }
01227
01228
01229
01230
01231
01232 bool
01233 reregister_specialization (tree spec, tree tmpl, tree new_spec)
01234 {
01235 tree* s;
01236
01237 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
01238 *s != NULL_TREE;
01239 s = &TREE_CHAIN (*s))
01240 if (TREE_VALUE (*s) == spec)
01241 {
01242 if (!new_spec)
01243 *s = TREE_CHAIN (*s);
01244 else
01245 TREE_VALUE (*s) = new_spec;
01246 return 1;
01247 }
01248
01249 return 0;
01250 }
01251
01252
01253
01254
01255
01256 static int
01257 eq_local_specializations (const void *p1, const void *p2)
01258 {
01259 return TREE_VALUE ((tree) p1) == (tree) p2;
01260 }
01261
01262
01263
01264 static hashval_t
01265 hash_local_specialization (const void* p1)
01266 {
01267 return htab_hash_pointer (TREE_VALUE ((tree) p1));
01268 }
01269
01270
01271
01272
01273 static void
01274 register_local_specialization (tree spec, tree tmpl)
01275 {
01276 void **slot;
01277
01278 slot = htab_find_slot_with_hash (local_specializations, tmpl,
01279 htab_hash_pointer (tmpl), INSERT);
01280 *slot = build_tree_list (spec, tmpl);
01281 }
01282
01283
01284
01285 void
01286 print_candidates (tree fns)
01287 {
01288 tree fn;
01289
01290 const char *str = "candidates are:";
01291
01292 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
01293 {
01294 tree f;
01295
01296 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
01297 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
01298 str = " ";
01299 }
01300 }
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321 static tree
01322 determine_specialization (tree template_id,
01323 tree decl,
01324 tree* targs_out,
01325 int need_member_template,
01326 int template_count)
01327 {
01328 tree fns;
01329 tree targs;
01330 tree explicit_targs;
01331 tree candidates = NULL_TREE;
01332 tree templates = NULL_TREE;
01333 int header_count;
01334 struct cp_binding_level *b;
01335
01336 *targs_out = NULL_TREE;
01337
01338 if (template_id == error_mark_node)
01339 return error_mark_node;
01340
01341 fns = TREE_OPERAND (template_id, 0);
01342 explicit_targs = TREE_OPERAND (template_id, 1);
01343
01344 if (fns == error_mark_node)
01345 return error_mark_node;
01346
01347
01348 if (BASELINK_P (fns))
01349 fns = BASELINK_FUNCTIONS (fns);
01350
01351 if (!is_overloaded_fn (fns))
01352 {
01353 error ("%qD is not a function template", fns);
01354 return error_mark_node;
01355 }
01356
01357
01358
01359 header_count = 0;
01360 for (b = current_binding_level;
01361 b->kind == sk_template_parms;
01362 b = b->level_chain)
01363 ++header_count;
01364
01365 for (; fns; fns = OVL_NEXT (fns))
01366 {
01367 tree fn = OVL_CURRENT (fns);
01368
01369 if (TREE_CODE (fn) == TEMPLATE_DECL)
01370 {
01371 tree decl_arg_types;
01372 tree fn_arg_types;
01373
01374
01375
01376
01377 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
01378 if (DECL_STATIC_FUNCTION_P (fn)
01379 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01380 decl_arg_types = TREE_CHAIN (decl_arg_types);
01381
01382
01383
01384
01385
01386
01387
01388
01389 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
01390 if (list_length (fn_arg_types) != list_length (decl_arg_types))
01391 continue;
01392
01393
01394
01395
01396 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
01397 && !same_type_p (TREE_VALUE (fn_arg_types),
01398 TREE_VALUE (decl_arg_types)))
01399 continue;
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427 if (header_count && header_count != template_count + 1)
01428 continue;
01429
01430
01431
01432 if (current_binding_level->kind == sk_template_parms
01433 && !current_binding_level->explicit_spec_p
01434 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
01435 != TREE_VEC_LENGTH (TREE_VALUE (current_template_parms))))
01436 continue;
01437
01438
01439
01440 targs = get_bindings (fn, decl, explicit_targs);
01441
01442 if (!targs)
01443
01444
01445 continue;
01446
01447
01448 templates = tree_cons (targs, fn, templates);
01449 }
01450 else if (need_member_template)
01451
01452
01453 ;
01454 else if (TREE_CODE (fn) != FUNCTION_DECL)
01455
01456
01457 ;
01458 else if (!DECL_FUNCTION_MEMBER_P (fn))
01459
01460
01461 ;
01462 else if (DECL_ARTIFICIAL (fn))
01463
01464 ;
01465 else
01466 {
01467 tree decl_arg_types;
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480 if (!DECL_TEMPLATE_INFO (fn))
01481
01482
01483 continue;
01484
01485 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
01486 TREE_TYPE (TREE_TYPE (fn))))
01487
01488 continue;
01489
01490
01491 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
01492 if (DECL_STATIC_FUNCTION_P (fn)
01493 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01494 decl_arg_types = TREE_CHAIN (decl_arg_types);
01495
01496 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
01497 decl_arg_types))
01498
01499 candidates = tree_cons (NULL_TREE, fn, candidates);
01500 }
01501 }
01502
01503 if (templates && TREE_CHAIN (templates))
01504 {
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536 tree tmpl = most_specialized (templates, decl, explicit_targs);
01537 if (tmpl && tmpl != error_mark_node)
01538 {
01539 targs = get_bindings (tmpl, decl, explicit_targs);
01540 templates = tree_cons (targs, tmpl, NULL_TREE);
01541 }
01542 }
01543
01544 if (templates == NULL_TREE && candidates == NULL_TREE)
01545 {
01546 cp_error_at ("template-id %qD for %q+D does not match any template "
01547 "declaration",
01548 template_id, decl);
01549 return error_mark_node;
01550 }
01551 else if ((templates && TREE_CHAIN (templates))
01552 || (candidates && TREE_CHAIN (candidates))
01553 || (templates && candidates))
01554 {
01555 cp_error_at ("ambiguous template specialization %qD for %q+D",
01556 template_id, decl);
01557 chainon (candidates, templates);
01558 print_candidates (candidates);
01559 return error_mark_node;
01560 }
01561
01562
01563 if (candidates)
01564 {
01565
01566
01567 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
01568 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
01569 }
01570
01571
01572 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
01573 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
01574 {
01575 *targs_out = copy_node (targs);
01576 SET_TMPL_ARGS_LEVEL (*targs_out,
01577 TMPL_ARGS_DEPTH (*targs_out),
01578 TREE_PURPOSE (templates));
01579 }
01580 else
01581 *targs_out = TREE_PURPOSE (templates);
01582 return TREE_VALUE (templates);
01583 }
01584
01585
01586
01587
01588
01589 static tree
01590 copy_default_args_to_explicit_spec_1 (tree spec_types,
01591 tree tmpl_types)
01592 {
01593 tree new_spec_types;
01594
01595 if (!spec_types)
01596 return NULL_TREE;
01597
01598 if (spec_types == void_list_node)
01599 return void_list_node;
01600
01601
01602 new_spec_types =
01603 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
01604 TREE_CHAIN (tmpl_types));
01605
01606
01607 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
01608 TREE_VALUE (spec_types),
01609 new_spec_types);
01610 }
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623 static void
01624 copy_default_args_to_explicit_spec (tree decl)
01625 {
01626 tree tmpl;
01627 tree spec_types;
01628 tree tmpl_types;
01629 tree new_spec_types;
01630 tree old_type;
01631 tree new_type;
01632 tree t;
01633 tree object_type = NULL_TREE;
01634 tree in_charge = NULL_TREE;
01635 tree vtt = NULL_TREE;
01636
01637
01638 tmpl = DECL_TI_TEMPLATE (decl);
01639 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
01640 for (t = tmpl_types; t; t = TREE_CHAIN (t))
01641 if (TREE_PURPOSE (t))
01642 break;
01643 if (!t)
01644 return;
01645
01646 old_type = TREE_TYPE (decl);
01647 spec_types = TYPE_ARG_TYPES (old_type);
01648
01649 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01650 {
01651
01652
01653 object_type = TREE_TYPE (TREE_VALUE (spec_types));
01654 spec_types = TREE_CHAIN (spec_types);
01655 tmpl_types = TREE_CHAIN (tmpl_types);
01656
01657 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
01658 {
01659
01660
01661 in_charge = spec_types;
01662 spec_types = TREE_CHAIN (spec_types);
01663 }
01664 if (DECL_HAS_VTT_PARM_P (decl))
01665 {
01666 vtt = spec_types;
01667 spec_types = TREE_CHAIN (spec_types);
01668 }
01669 }
01670
01671
01672 new_spec_types =
01673 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
01674
01675
01676 if (object_type)
01677 {
01678 if (vtt)
01679 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
01680 TREE_VALUE (vtt),
01681 new_spec_types);
01682
01683 if (in_charge)
01684
01685 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
01686 TREE_VALUE (in_charge),
01687 new_spec_types);
01688
01689 new_type = build_method_type_directly (object_type,
01690 TREE_TYPE (old_type),
01691 new_spec_types);
01692 }
01693 else
01694 new_type = build_function_type (TREE_TYPE (old_type),
01695 new_spec_types);
01696 new_type = cp_build_type_attribute_variant (new_type,
01697 TYPE_ATTRIBUTES (old_type));
01698 new_type = build_exception_variant (new_type,
01699 TYPE_RAISES_EXCEPTIONS (old_type));
01700 TREE_TYPE (decl) = new_type;
01701 }
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740 tree
01741 check_explicit_specialization (tree declarator,
01742 tree decl,
01743 int template_count,
01744 int flags)
01745 {
01746 int have_def = flags & 2;
01747 int is_friend = flags & 4;
01748 int specialization = 0;
01749 int explicit_instantiation = 0;
01750 int member_specialization = 0;
01751 tree ctype = DECL_CLASS_CONTEXT (decl);
01752 tree dname = DECL_NAME (decl);
01753 tmpl_spec_kind tsk;
01754
01755 if (is_friend)
01756 {
01757 if (!processing_specialization)
01758 tsk = tsk_none;
01759 else
01760 tsk = tsk_excessive_parms;
01761 }
01762 else
01763 tsk = current_tmpl_spec_kind (template_count);
01764
01765 switch (tsk)
01766 {
01767 case tsk_none:
01768 if (processing_specialization)
01769 {
01770 specialization = 1;
01771 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
01772 }
01773 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
01774 {
01775 if (is_friend)
01776
01777
01778
01779
01780 specialization = 1;
01781 else
01782 {
01783
01784
01785
01786 error ("template-id %qD in declaration of primary template",
01787 declarator);
01788 return decl;
01789 }
01790 }
01791 break;
01792
01793 case tsk_invalid_member_spec:
01794
01795
01796 return error_mark_node;
01797
01798 case tsk_invalid_expl_inst:
01799 error ("template parameter list used in explicit instantiation");
01800
01801
01802
01803 case tsk_expl_inst:
01804 if (have_def)
01805 error ("definition provided for explicit instantiation");
01806
01807 explicit_instantiation = 1;
01808 break;
01809
01810 case tsk_excessive_parms:
01811 case tsk_insufficient_parms:
01812 if (tsk == tsk_excessive_parms)
01813 error ("too many template parameter lists in declaration of %qD",
01814 decl);
01815 else if (template_header_count)
01816 error("too few template parameter lists in declaration of %qD", decl);
01817 else
01818 error("explicit specialization of %qD must be introduced by "
01819 "%<template <>%>", decl);
01820
01821
01822 case tsk_expl_spec:
01823 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
01824 if (ctype)
01825 member_specialization = 1;
01826 else
01827 specialization = 1;
01828 break;
01829
01830 case tsk_template:
01831 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
01832 {
01833
01834
01835
01836 if (uses_template_parms (declarator))
01837 error ("function template partial specialization %qD "
01838 "is not allowed", declarator);
01839 else
01840 error ("template-id %qD in declaration of primary template",
01841 declarator);
01842 return decl;
01843 }
01844
01845 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855 specialization = 1;
01856 break;
01857
01858 default:
01859 gcc_unreachable ();
01860 }
01861
01862 if (specialization || member_specialization)
01863 {
01864 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
01865 for (; t; t = TREE_CHAIN (t))
01866 if (TREE_PURPOSE (t))
01867 {
01868 pedwarn
01869 ("default argument specified in explicit specialization");
01870 break;
01871 }
01872 if (current_lang_name == lang_name_c)
01873 error ("template specialization with C linkage");
01874 }
01875
01876 if (specialization || member_specialization || explicit_instantiation)
01877 {
01878 tree tmpl = NULL_TREE;
01879 tree targs = NULL_TREE;
01880
01881
01882 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
01883 {
01884 tree fns;
01885
01886 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
01887 if (ctype)
01888 fns = dname;
01889 else
01890 {
01891
01892
01893 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
01894
01895
01896
01897 fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
01898 if (!fns || !is_overloaded_fn (fns))
01899 {
01900 error ("%qD is not a template function", dname);
01901 fns = error_mark_node;
01902 }
01903 }
01904
01905 declarator = lookup_template_function (fns, NULL_TREE);
01906 }
01907
01908 if (declarator == error_mark_node)
01909 return error_mark_node;
01910
01911 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
01912 {
01913 if (!explicit_instantiation)
01914
01915
01916
01917 return error_mark_node;
01918 else
01919 {
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932 ;
01933 }
01934
01935 return decl;
01936 }
01937 else if (ctype != NULL_TREE
01938 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
01939 IDENTIFIER_NODE))
01940 {
01941
01942
01943 tree name = TREE_OPERAND (declarator, 0);
01944 tree fns = NULL_TREE;
01945 int idx;
01946
01947 if (constructor_name_p (name, ctype))
01948 {
01949 int is_constructor = DECL_CONSTRUCTOR_P (decl);
01950
01951 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
01952 : !CLASSTYPE_DESTRUCTORS (ctype))
01953 {
01954
01955
01956
01957
01958
01959
01960
01961
01962 error ("specialization of implicitly-declared special member function");
01963 return error_mark_node;
01964 }
01965
01966 name = is_constructor ? ctor_identifier : dtor_identifier;
01967 }
01968
01969 if (!DECL_CONV_FN_P (decl))
01970 {
01971 idx = lookup_fnfields_1 (ctype, name);
01972 if (idx >= 0)
01973 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
01974 }
01975 else
01976 {
01977 VEC(tree) *methods;
01978 tree ovl;
01979
01980
01981
01982
01983
01984
01985 fns = NULL_TREE;
01986
01987 methods = CLASSTYPE_METHOD_VEC (ctype);
01988 if (methods)
01989 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
01990 VEC_iterate (tree, methods, idx, ovl);
01991 ++idx)
01992 {
01993 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
01994
01995 break;
01996
01997
01998
01999 for (; ovl; ovl = OVL_NEXT (ovl))
02000 fns = ovl_cons (OVL_CURRENT (ovl), fns);
02001 }
02002 }
02003
02004 if (fns == NULL_TREE)
02005 {
02006 error ("no member function %qD declared in %qT", name, ctype);
02007 return error_mark_node;
02008 }
02009 else
02010 TREE_OPERAND (declarator, 0) = fns;
02011 }
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021 tmpl = determine_specialization (declarator, decl,
02022 &targs,
02023 member_specialization,
02024 template_count);
02025
02026 if (!tmpl || tmpl == error_mark_node)
02027
02028
02029 return error_mark_node;
02030 else
02031 {
02032 tree gen_tmpl = most_general_template (tmpl);
02033
02034 if (explicit_instantiation)
02035 {
02036
02037
02038
02039 int arg_depth = TMPL_ARGS_DEPTH (targs);
02040 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
02041
02042 if (arg_depth > parm_depth)
02043 {
02044
02045
02046
02047
02048
02049 int i;
02050 tree new_targs;
02051
02052 new_targs = make_tree_vec (parm_depth);
02053 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
02054 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
02055 = TREE_VEC_ELT (targs, i);
02056 targs = new_targs;
02057 }
02058
02059 return instantiate_template (tmpl, targs, tf_error);
02060 }
02061
02062
02063
02064
02065 if (DECL_STATIC_FUNCTION_P (tmpl)
02066 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
02067 revert_static_member_fn (decl);
02068
02069
02070
02071
02072 if (tsk == tsk_template)
02073 {
02074 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
02075 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
02076 if (have_def)
02077 {
02078 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
02079 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
02080 = DECL_SOURCE_LOCATION (decl);
02081
02082
02083 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
02084 = DECL_ARGUMENTS (decl);
02085 }
02086 return tmpl;
02087 }
02088
02089
02090 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
02091
02092
02093
02094 copy_default_args_to_explicit_spec (decl);
02095
02096
02097
02098 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
02099 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
02100
02101 if (is_friend && !have_def)
02102
02103
02104
02105 SET_DECL_IMPLICIT_INSTANTIATION (decl);
02106 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
02107
02108
02109
02110 clone_function_decl (decl, 0);
02111
02112
02113
02114 decl = register_specialization (decl, gen_tmpl, targs);
02115 }
02116 }
02117
02118 return decl;
02119 }
02120
02121
02122
02123
02124
02125 int
02126 comp_template_parms (tree parms1, tree parms2)
02127 {
02128 tree p1;
02129 tree p2;
02130
02131 if (parms1 == parms2)
02132 return 1;
02133
02134 for (p1 = parms1, p2 = parms2;
02135 p1 != NULL_TREE && p2 != NULL_TREE;
02136 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
02137 {
02138 tree t1 = TREE_VALUE (p1);
02139 tree t2 = TREE_VALUE (p2);
02140 int i;
02141
02142 gcc_assert (TREE_CODE (t1) == TREE_VEC);
02143 gcc_assert (TREE_CODE (t2) == TREE_VEC);
02144
02145 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
02146 return 0;
02147
02148 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
02149 {
02150 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
02151 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
02152
02153 if (TREE_CODE (parm1) != TREE_CODE (parm2))
02154 return 0;
02155
02156 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
02157 continue;
02158 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
02159 return 0;
02160 }
02161 }
02162
02163 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
02164
02165
02166 return 0;
02167
02168 return 1;
02169 }
02170
02171
02172
02173
02174
02175
02176 void
02177 check_template_shadow (tree decl)
02178 {
02179 tree olddecl;
02180
02181
02182
02183 if (!current_template_parms)
02184 return;
02185
02186
02187 if (TREE_CODE (decl) == OVERLOAD)
02188 decl = OVL_CURRENT (decl);
02189 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
02190
02191
02192
02193 if (!olddecl)
02194 return;
02195
02196
02197
02198
02199
02200 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
02201 return;
02202
02203
02204
02205
02206 if (decl == olddecl
02207 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
02208 return;
02209
02210 cp_error_at ("declaration of %q#D", decl);
02211 cp_error_at (" shadows template parm %q#D", olddecl);
02212 }
02213
02214
02215
02216
02217 static tree
02218 build_template_parm_index (int index,
02219 int level,
02220 int orig_level,
02221 tree decl,
02222 tree type)
02223 {
02224 tree t = make_node (TEMPLATE_PARM_INDEX);
02225 TEMPLATE_PARM_IDX (t) = index;
02226 TEMPLATE_PARM_LEVEL (t) = level;
02227 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
02228 TEMPLATE_PARM_DECL (t) = decl;
02229 TREE_TYPE (t) = type;
02230 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
02231 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
02232 TREE_READONLY (t) = TREE_READONLY (decl);
02233
02234 return t;
02235 }
02236
02237
02238
02239
02240
02241
02242 static tree
02243 reduce_template_parm_level (tree index, tree type, int levels)
02244 {
02245 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
02246 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
02247 != TEMPLATE_PARM_LEVEL (index) - levels))
02248 {
02249 tree orig_decl = TEMPLATE_PARM_DECL (index);
02250 tree decl, t;
02251
02252 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
02253 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
02254 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
02255 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
02256 DECL_ARTIFICIAL (decl) = 1;
02257 SET_DECL_TEMPLATE_PARM_P (decl);
02258
02259 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
02260 TEMPLATE_PARM_LEVEL (index) - levels,
02261 TEMPLATE_PARM_ORIG_LEVEL (index),
02262 decl, type);
02263 TEMPLATE_PARM_DESCENDANTS (index) = t;
02264
02265
02266 DECL_TEMPLATE_PARMS (decl)
02267 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
02268 }
02269
02270 return TEMPLATE_PARM_DESCENDANTS (index);
02271 }
02272
02273
02274
02275
02276
02277 tree
02278 process_template_parm (tree list, tree next, bool is_non_type)
02279 {
02280 tree parm;
02281 tree decl = 0;
02282 tree defval;
02283 int idx;
02284
02285 parm = next;
02286 gcc_assert (TREE_CODE (parm) == TREE_LIST);
02287 defval = TREE_PURPOSE (parm);
02288
02289 if (list)
02290 {
02291 tree p = TREE_VALUE (tree_last (list));
02292
02293 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
02294 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
02295 else
02296 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
02297 ++idx;
02298 }
02299 else
02300 idx = 0;
02301
02302 if (is_non_type)
02303 {
02304 parm = TREE_VALUE (parm);
02305
02306 SET_DECL_TEMPLATE_PARM_P (parm);
02307
02308
02309
02310
02311
02312 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
02313
02314
02315 TREE_CONSTANT (parm) = 1;
02316 TREE_INVARIANT (parm) = 1;
02317 TREE_READONLY (parm) = 1;
02318 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
02319 TREE_TYPE (parm) = void_type_node;
02320 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
02321 TREE_CONSTANT (decl) = 1;
02322 TREE_INVARIANT (decl) = 1;
02323 TREE_READONLY (decl) = 1;
02324 DECL_INITIAL (parm) = DECL_INITIAL (decl)
02325 = build_template_parm_index (idx, processing_template_decl,
02326 processing_template_decl,
02327 decl, TREE_TYPE (parm));
02328 }
02329 else
02330 {
02331 tree t;
02332 parm = TREE_VALUE (TREE_VALUE (parm));
02333
02334 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
02335 {
02336 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
02337
02338
02339 TREE_TYPE (parm) = t;
02340 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
02341 decl = parm;
02342 }
02343 else
02344 {
02345 t = make_aggr_type (TEMPLATE_TYPE_PARM);
02346
02347 decl = build_decl (TYPE_DECL, parm, t);
02348 }
02349
02350 TYPE_NAME (t) = decl;
02351 TYPE_STUB_DECL (t) = decl;
02352 parm = decl;
02353 TEMPLATE_TYPE_PARM_INDEX (t)
02354 = build_template_parm_index (idx, processing_template_decl,
02355 processing_template_decl,
02356 decl, TREE_TYPE (parm));
02357 }
02358 DECL_ARTIFICIAL (decl) = 1;
02359 SET_DECL_TEMPLATE_PARM_P (decl);
02360 pushdecl (decl);
02361 parm = build_tree_list (defval, parm);
02362 return chainon (list, parm);
02363 }
02364
02365
02366
02367
02368
02369
02370 tree
02371 end_template_parm_list (tree parms)
02372 {
02373 int nparms;
02374 tree parm, next;
02375 tree saved_parmlist = make_tree_vec (list_length (parms));
02376
02377 current_template_parms
02378 = tree_cons (size_int (processing_template_decl),
02379 saved_parmlist, current_template_parms);
02380
02381 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
02382 {
02383 next = TREE_CHAIN (parm);
02384 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
02385 TREE_CHAIN (parm) = NULL_TREE;
02386 }
02387
02388 --processing_template_parmlist;
02389
02390 return saved_parmlist;
02391 }
02392
02393
02394
02395 void
02396 end_template_decl (void)
02397 {
02398 reset_specialization ();
02399
02400 if (! processing_template_decl)
02401 return;
02402
02403
02404 finish_scope ();
02405
02406 --processing_template_decl;
02407 current_template_parms = TREE_CHAIN (current_template_parms);
02408 }
02409
02410
02411
02412
02413 tree
02414 current_template_args (void)
02415 {
02416 tree header;
02417 tree args = NULL_TREE;
02418 int length = TMPL_PARMS_DEPTH (current_template_parms);
02419 int l = length;
02420
02421
02422
02423
02424 if (length > 1)
02425 args = make_tree_vec (length);
02426
02427 for (header = current_template_parms; header; header = TREE_CHAIN (header))
02428 {
02429 tree a = copy_node (TREE_VALUE (header));
02430 int i;
02431
02432 TREE_TYPE (a) = NULL_TREE;
02433 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
02434 {
02435 tree t = TREE_VEC_ELT (a, i);
02436
02437
02438
02439
02440 if (TREE_CODE (t) == TREE_LIST)
02441 {
02442 t = TREE_VALUE (t);
02443
02444 if (TREE_CODE (t) == TYPE_DECL
02445 || TREE_CODE (t) == TEMPLATE_DECL)
02446 t = TREE_TYPE (t);
02447 else
02448 t = DECL_INITIAL (t);
02449 TREE_VEC_ELT (a, i) = t;
02450 }
02451 }
02452
02453 if (length > 1)
02454 TREE_VEC_ELT (args, --l) = a;
02455 else
02456 args = a;
02457 }
02458
02459 return args;
02460 }
02461
02462
02463
02464
02465
02466 static tree
02467 build_template_decl (tree decl, tree parms, bool member_template_p)
02468 {
02469 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
02470 DECL_TEMPLATE_PARMS (tmpl) = parms;
02471 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
02472 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
02473 if (DECL_LANG_SPECIFIC (decl))
02474 {
02475 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
02476 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
02477 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
02478 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
02479 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
02480 if (DECL_OVERLOADED_OPERATOR_P (decl))
02481 SET_OVERLOADED_OPERATOR_CODE (tmpl,
02482 DECL_OVERLOADED_OPERATOR_P (decl));
02483 }
02484
02485 return tmpl;
02486 }
02487
02488 struct template_parm_data
02489 {
02490
02491
02492 int level;
02493
02494
02495
02496 int current_arg;
02497
02498
02499
02500
02501 int* parms;
02502
02503
02504
02505
02506 int* arg_uses_template_parms;
02507 };
02508
02509
02510
02511
02512
02513
02514
02515 static int
02516 mark_template_parm (tree t, void* data)
02517 {
02518 int level;
02519 int idx;
02520 struct template_parm_data* tpd = (struct template_parm_data*) data;
02521
02522 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
02523 {
02524 level = TEMPLATE_PARM_LEVEL (t);
02525 idx = TEMPLATE_PARM_IDX (t);
02526 }
02527 else
02528 {
02529 level = TEMPLATE_TYPE_LEVEL (t);
02530 idx = TEMPLATE_TYPE_IDX (t);
02531 }
02532
02533 if (level == tpd->level)
02534 {
02535 tpd->parms[idx] = 1;
02536 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
02537 }
02538
02539
02540
02541 return 0;
02542 }
02543
02544
02545
02546 static tree
02547 process_partial_specialization (tree decl)
02548 {
02549 tree type = TREE_TYPE (decl);
02550 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
02551 tree specargs = CLASSTYPE_TI_ARGS (type);
02552 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
02553 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
02554 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
02555 int nargs = TREE_VEC_LENGTH (inner_args);
02556 int ntparms = TREE_VEC_LENGTH (inner_parms);
02557 int i;
02558 int did_error_intro = 0;
02559 struct template_parm_data tpd;
02560 struct template_parm_data tpd2;
02561
02562
02563
02564
02565
02566
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
02593 tpd.parms = alloca (sizeof (int) * ntparms);
02594 memset (tpd.parms, 0, sizeof (int) * ntparms);
02595
02596 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
02597 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
02598 for (i = 0; i < nargs; ++i)
02599 {
02600 tpd.current_arg = i;
02601 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
02602 &mark_template_parm,
02603 &tpd,
02604 NULL);
02605 }
02606 for (i = 0; i < ntparms; ++i)
02607 if (tpd.parms[i] == 0)
02608 {
02609
02610
02611 if (!did_error_intro)
02612 {
02613 error ("template parameters not used in partial specialization:");
02614 did_error_intro = 1;
02615 }
02616
02617 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
02618 }
02619
02620
02621
02622
02623
02624 if (comp_template_args
02625 (inner_args,
02626 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
02627 (maintmpl)))))
02628 error ("partial specialization %qT does not specialize any template arguments", type);
02629
02630
02631
02632
02633
02634
02635
02636
02637
02638
02639 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
02640 tpd2.parms = 0;
02641 for (i = 0; i < nargs; ++i)
02642 {
02643 tree arg = TREE_VEC_ELT (inner_args, i);
02644 if (
02645 !TYPE_P (arg)
02646 && TREE_CODE (arg) != TEMPLATE_DECL
02647
02648
02649
02650 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
02651 {
02652 if (tpd.arg_uses_template_parms[i])
02653 error ("template argument %qE involves template parameter(s)", arg);
02654 else
02655 {
02656
02657
02658
02659 tree type =
02660 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
02661 i)));
02662
02663 if (!tpd2.parms)
02664 {
02665
02666 tpd2.arg_uses_template_parms
02667 = alloca (sizeof (int) * nargs);
02668
02669
02670
02671 tpd2.parms = alloca (sizeof (int) * nargs);
02672 tpd2.level =
02673 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
02674 }
02675
02676
02677
02678
02679 tpd2.current_arg = i;
02680 tpd2.arg_uses_template_parms[i] = 0;
02681 memset (tpd2.parms, 0, sizeof (int) * nargs);
02682 for_each_template_parm (type,
02683 &mark_template_parm,
02684 &tpd2,
02685 NULL);
02686
02687 if (tpd2.arg_uses_template_parms [i])
02688 {
02689
02690
02691
02692 int j;
02693 for (j = 0; j < nargs; ++j)
02694 if (tpd2.parms[j] != 0
02695 && tpd.arg_uses_template_parms [j])
02696 {
02697 error ("type %qT of template argument %qE depends "
02698 "on template parameter(s)",
02699 type,
02700 arg);
02701 break;
02702 }
02703 }
02704 }
02705 }
02706 }
02707
02708 if (retrieve_specialization (maintmpl, specargs,
02709 true))
02710
02711 return decl;
02712
02713 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
02714 = tree_cons (inner_args, inner_parms,
02715 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
02716 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
02717 return decl;
02718 }
02719
02720
02721
02722
02723
02724
02725 static void
02726 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
02727 {
02728 const char *msg;
02729 int last_level_to_check;
02730 tree parm_level;
02731
02732
02733
02734
02735
02736
02737
02738
02739 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
02740
02741
02742
02743 return;
02744
02745 if (current_class_type
02746 && !TYPE_BEING_DEFINED (current_class_type)
02747 && DECL_LANG_SPECIFIC (decl)
02748
02749
02750 && (DECL_FUNCTION_MEMBER_P (decl)
02751 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
02752 : DECL_FRIEND_CONTEXT (decl)
02753 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
02754 : false)
02755
02756
02757 && (!DECL_FUNCTION_MEMBER_P (decl)
02758 || DECL_INITIALIZED_IN_CLASS_P (decl)))
02759
02760
02761
02762
02763 return;
02764
02765
02766
02767
02768
02769
02770 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
02771 {
02772 tree inner_parms = TREE_VALUE (parm_level);
02773 int ntparms = TREE_VEC_LENGTH (inner_parms);
02774 int seen_def_arg_p = 0;
02775 int i;
02776
02777 for (i = 0; i < ntparms; ++i)
02778 {
02779 tree parm = TREE_VEC_ELT (inner_parms, i);
02780 if (TREE_PURPOSE (parm))
02781 seen_def_arg_p = 1;
02782 else if (seen_def_arg_p)
02783 {
02784 error ("no default argument for %qD", TREE_VALUE (parm));
02785
02786
02787 TREE_PURPOSE (parm) = error_mark_node;
02788 }
02789 }
02790 }
02791
02792 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
02793
02794
02795
02796
02797
02798
02799
02800
02801
02802
02803
02804
02805 ;
02806 else
02807
02808
02809
02810 parms = TREE_CHAIN (parms);
02811
02812
02813 if (TREE_CODE (decl) == FUNCTION_DECL)
02814 msg = "default template arguments may not be used in function templates";
02815 else if (is_partial)
02816 msg = "default template arguments may not be used in partial specializations";
02817 else
02818 msg = "default argument for template parameter for class enclosing %qD";
02819
02820 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
02821
02822
02823
02824
02825
02826
02827
02828
02829 last_level_to_check = template_class_depth (current_class_type) + 1;
02830 else
02831
02832 last_level_to_check = 0;
02833
02834 for (parm_level = parms;
02835 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
02836 parm_level = TREE_CHAIN (parm_level))
02837 {
02838 tree inner_parms = TREE_VALUE (parm_level);
02839 int i;
02840 int ntparms;
02841
02842 ntparms = TREE_VEC_LENGTH (inner_parms);
02843 for (i = 0; i < ntparms; ++i)
02844 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
02845 {
02846 if (msg)
02847 {
02848 error (msg, decl);
02849 msg = 0;
02850 }
02851
02852
02853
02854 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
02855 }
02856
02857
02858
02859 if (msg)
02860 msg = "default argument for template parameter for class enclosing %qD";
02861 }
02862 }
02863
02864
02865
02866
02867
02868
02869 static int
02870 template_parm_this_level_p (tree t, void* data)
02871 {
02872 int this_level = *(int *)data;
02873 int level;
02874
02875 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
02876 level = TEMPLATE_PARM_LEVEL (t);
02877 else
02878 level = TEMPLATE_TYPE_LEVEL (t);
02879 return level == this_level;
02880 }
02881
02882
02883
02884
02885
02886
02887
02888
02889 tree
02890 push_template_decl_real (tree decl, int is_friend)
02891 {
02892 tree tmpl;
02893 tree args;
02894 tree info;
02895 tree ctx;
02896 int primary;
02897 int is_partial;
02898 int new_template_p = 0;
02899
02900
02901 bool member_template_p = false;
02902
02903 if (decl == error_mark_node)
02904 return decl;
02905
02906
02907 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
02908 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
02909 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
02910
02911 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
02912
02913 if (is_friend)
02914
02915
02916 ctx = DECL_CONTEXT (decl);
02917 else if (CP_DECL_CONTEXT (decl)
02918 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
02919
02920
02921 ctx = CP_DECL_CONTEXT (decl);
02922 else
02923
02924
02925 ctx = current_scope ();
02926
02927 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
02928 ctx = NULL_TREE;
02929
02930 if (!DECL_CONTEXT (decl))
02931 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
02932
02933
02934 primary = template_parm_scope_p ();
02935
02936 if (primary)
02937 {
02938 if (DECL_CLASS_SCOPE_P (decl))
02939 member_template_p = true;
02940 if (current_lang_name == lang_name_c)
02941 error ("template with C linkage");
02942 else if (TREE_CODE (decl) == TYPE_DECL
02943 && ANON_AGGRNAME_P (DECL_NAME (decl)))
02944 error ("template class without a name");
02945 else if (TREE_CODE (decl) == FUNCTION_DECL)
02946 {
02947 if (DECL_DESTRUCTOR_P (decl))
02948 {
02949
02950
02951
02952 error ("destructor %qD declared as member template", decl);
02953 return error_mark_node;
02954 }
02955 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
02956 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
02957 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
02958 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
02959 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
02960 == void_list_node)))
02961 {
02962
02963
02964
02965
02966
02967 error ("invalid template declaration of %qD", decl);
02968 return decl;
02969 }
02970 }
02971 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
02972 && CLASS_TYPE_P (TREE_TYPE (decl)))
02973 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)))
02974 ;
02975 else
02976 {
02977 error ("template declaration of %q#D", decl);
02978 return error_mark_node;
02979 }
02980 }
02981
02982
02983
02984 check_default_tmpl_args (decl, current_template_parms,
02985 primary, is_partial);
02986
02987 if (is_partial)
02988 return process_partial_specialization (decl);
02989
02990 args = current_template_args ();
02991
02992 if (!ctx
02993 || TREE_CODE (ctx) == FUNCTION_DECL
02994 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
02995 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
02996 {
02997 if (DECL_LANG_SPECIFIC (decl)
02998 && DECL_TEMPLATE_INFO (decl)
02999 && DECL_TI_TEMPLATE (decl))
03000 tmpl = DECL_TI_TEMPLATE (decl);
03001
03002
03003
03004 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
03005 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
03006 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
03007 {
03008
03009
03010
03011 redeclare_class_template (TREE_TYPE (decl),
03012 current_template_parms);
03013
03014
03015 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
03016 }
03017 else
03018 {
03019 tmpl = build_template_decl (decl, current_template_parms,
03020 member_template_p);
03021 new_template_p = 1;
03022
03023 if (DECL_LANG_SPECIFIC (decl)
03024 && DECL_TEMPLATE_SPECIALIZATION (decl))
03025 {
03026
03027
03028 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
03029 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
03030 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
03031 }
03032 }
03033 }
03034 else
03035 {
03036 tree a, t, current, parms;
03037 int i;
03038
03039 if (TREE_CODE (decl) == TYPE_DECL)
03040 {
03041 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
03042 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
03043 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
03044 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
03045 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
03046 else
03047 {
03048 error ("%qD does not declare a template type", decl);
03049 return decl;
03050 }
03051 }
03052 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
03053 {
03054 error ("template definition of non-template %q#D", decl);
03055 return decl;
03056 }
03057 else
03058 tmpl = DECL_TI_TEMPLATE (decl);
03059
03060 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
03061 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
03062 && DECL_TEMPLATE_SPECIALIZATION (decl)
03063 && DECL_MEMBER_TEMPLATE_P (tmpl))
03064 {
03065 tree new_tmpl;
03066
03067
03068
03069
03070
03071
03072 args = DECL_TI_ARGS (decl);
03073
03074 new_tmpl
03075 = build_template_decl (decl, current_template_parms,
03076 member_template_p);
03077 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
03078 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
03079 DECL_TI_TEMPLATE (decl) = new_tmpl;
03080 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
03081 DECL_TEMPLATE_INFO (new_tmpl)
03082 = tree_cons (tmpl, args, NULL_TREE);
03083
03084 register_specialization (new_tmpl,
03085 most_general_template (tmpl),
03086 args);
03087 return decl;
03088 }
03089
03090
03091
03092 parms = DECL_TEMPLATE_PARMS (tmpl);
03093 i = TMPL_PARMS_DEPTH (parms);
03094 if (TMPL_ARGS_DEPTH (args) != i)
03095 {
03096 error ("expected %d levels of template parms for %q#D, got %d",
03097 i, decl, TMPL_ARGS_DEPTH (args));
03098 }
03099 else
03100 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
03101 {
03102 a = TMPL_ARGS_LEVEL (args, i);
03103 t = INNERMOST_TEMPLATE_PARMS (parms);
03104
03105 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
03106 {
03107 if (current == decl)
03108 error ("got %d template parameters for %q#D",
03109 TREE_VEC_LENGTH (a), decl);
03110 else
03111 error ("got %d template parameters for %q#T",
03112 TREE_VEC_LENGTH (a), current);
03113 error (" but %d required", TREE_VEC_LENGTH (t));
03114 return error_mark_node;
03115 }
03116
03117
03118
03119
03120 if (current == decl)
03121 current = ctx;
03122 else
03123 current = TYPE_CONTEXT (current);
03124 }
03125 }
03126
03127 DECL_TEMPLATE_RESULT (tmpl) = decl;
03128 TREE_TYPE (tmpl) = TREE_TYPE (decl);
03129
03130
03131
03132
03133
03134 if (new_template_p && !ctx
03135 && !(is_friend && template_class_depth (current_class_type) > 0))
03136 {
03137 tmpl = pushdecl_namespace_level (tmpl);
03138 if (tmpl == error_mark_node)
03139 return error_mark_node;
03140
03141
03142 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
03143 {
03144 DECL_ANTICIPATED (tmpl) = 1;
03145 DECL_FRIEND_P (tmpl) = 1;
03146 }
03147 }
03148
03149 if (primary)
03150 {
03151 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
03152 if (DECL_CONV_FN_P (tmpl))
03153 {
03154 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
03155
03156
03157
03158
03159 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
03160 depth))
03161 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
03162 }
03163 }
03164
03165
03166
03167
03168
03169 if (DECL_TEMPLATE_INFO (tmpl))
03170 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
03171
03172 info = tree_cons (tmpl, args, NULL_TREE);
03173
03174 if (DECL_IMPLICIT_TYPEDEF_P (decl))
03175 {
03176 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
03177 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
03178 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
03179
03180 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
03181 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
03182 }
03183 else if (DECL_LANG_SPECIFIC (decl))
03184 DECL_TEMPLATE_INFO (decl) = info;
03185
03186 return DECL_TEMPLATE_RESULT (tmpl);
03187 }
03188
03189 tree
03190 push_template_decl (tree decl)
03191 {
03192 return push_template_decl_real (decl, 0);
03193 }
03194
03195
03196
03197
03198
03199
03200
03201 void
03202 redeclare_class_template (tree type, tree parms)
03203 {
03204 tree tmpl;
03205 tree tmpl_parms;
03206 int i;
03207
03208 if (!TYPE_TEMPLATE_INFO (type))
03209 {
03210 error ("%qT is not a template type", type);
03211 return;
03212 }
03213
03214 tmpl = TYPE_TI_TEMPLATE (type);
03215 if (!PRIMARY_TEMPLATE_P (tmpl))
03216
03217
03218
03219 return;
03220
03221 if (!parms)
03222 {
03223 error ("template specifiers not specified in declaration of %qD",
03224 tmpl);
03225 return;
03226 }
03227
03228 parms = INNERMOST_TEMPLATE_PARMS (parms);
03229 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
03230
03231 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
03232 {
03233 cp_error_at ("previous declaration %qD", tmpl);
03234 error ("used %d template parameter(s) instead of %d",
03235 TREE_VEC_LENGTH (tmpl_parms),
03236 TREE_VEC_LENGTH (parms));
03237 return;
03238 }
03239
03240 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
03241 {
03242 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
03243 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
03244 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
03245 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
03246
03247
03248
03249 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
03250 || (TREE_CODE (tmpl_parm) != TYPE_DECL
03251 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
03252 {
03253 cp_error_at ("template parameter %q#D", tmpl_parm);
03254 error ("redeclared here as %q#D", parm);
03255 return;
03256 }
03257
03258 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
03259 {
03260
03261
03262
03263
03264 error ("redefinition of default argument for %q#D", parm);
03265 error ("%J original definition appeared here", tmpl_parm);
03266 return;
03267 }
03268
03269 if (parm_default != NULL_TREE)
03270
03271
03272 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
03273 else if (tmpl_default != NULL_TREE)
03274
03275
03276 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
03277 }
03278 }
03279
03280
03281
03282
03283 tree
03284 fold_non_dependent_expr (tree expr)
03285 {
03286
03287
03288
03289
03290
03291
03292
03293 if (processing_template_decl
03294 && !type_dependent_expression_p (expr)
03295 && !value_dependent_expression_p (expr))
03296 {
03297 HOST_WIDE_INT saved_processing_template_decl;
03298
03299 saved_processing_template_decl = processing_template_decl;
03300 processing_template_decl = 0;
03301 expr = tsubst_copy_and_build (expr,
03302 NULL_TREE,
03303 tf_error,
03304 NULL_TREE,
03305 false);
03306 processing_template_decl = saved_processing_template_decl;
03307 }
03308 return expr;
03309 }
03310
03311
03312
03313
03314
03315
03316
03317
03318
03319
03320 tree
03321 fold_decl_constant_value (tree expr)
03322 {
03323 tree const_expr = expr;
03324 do
03325 {
03326 expr = fold_non_dependent_expr (const_expr);
03327 const_expr = integral_constant_value (expr);
03328 }
03329 while (expr != const_expr);
03330
03331 return expr;
03332 }
03333
03334
03335
03336
03337
03338
03339 static tree
03340 convert_nontype_argument_function (tree type, tree expr)
03341 {
03342 tree fns = expr;
03343 tree fn, fn_no_ptr;
03344
03345 fn = instantiate_type (type, fns, tf_none);
03346 if (fn == error_mark_node)
03347 return error_mark_node;
03348
03349 fn_no_ptr = fn;
03350 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
03351 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
03352
03353
03354
03355
03356
03357
03358
03359 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
03360 {
03361 error ("%qE is not a valid template argument for type %qT "
03362 "because function %qD has not external linkage",
03363 expr, type, fn_no_ptr);
03364 return NULL_TREE;
03365 }
03366
03367 return fn;
03368 }
03369
03370
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390 static tree
03391 convert_nontype_argument (tree type, tree expr)
03392 {
03393 tree expr_type;
03394
03395
03396
03397
03398
03399
03400 if (TREE_CODE (expr) == STRING_CST)
03401 {
03402 error ("%qE is not a valid template argument for type %qT "
03403 "because string literals can never be used in this context",
03404 expr, type);
03405 return NULL_TREE;
03406 }
03407
03408
03409
03410
03411
03412
03413
03414
03415 expr = fold_non_dependent_expr (expr);
03416 expr_type = TREE_TYPE (expr);
03417
03418
03419
03420
03421
03422
03423
03424
03425 if (TREE_CODE (expr) == NOP_EXPR)
03426 {
03427 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
03428 {
03429
03430
03431
03432
03433 tree addr = TREE_OPERAND (expr, 0);
03434 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
03435 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
03436 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
03437 gcc_assert (same_type_ignoring_top_level_qualifiers_p
03438 (TREE_TYPE (expr_type),
03439 TREE_TYPE (TREE_TYPE (addr))));
03440
03441 expr = TREE_OPERAND (addr, 0);
03442 expr_type = TREE_TYPE (expr);
03443 }
03444
03445
03446
03447
03448 else if (TYPE_PTROBV_P (type))
03449 {
03450 STRIP_NOPS (expr);
03451 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
03452 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
03453
03454
03455
03456 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
03457 expr = TREE_OPERAND (expr, 0);
03458 expr_type = TREE_TYPE (expr);
03459 }
03460 }
03461
03462
03463
03464
03465
03466
03467 if (INTEGRAL_TYPE_P (type))
03468 {
03469 if (!INTEGRAL_TYPE_P (expr_type))
03470 return error_mark_node;
03471
03472 expr = fold_decl_constant_value (expr);
03473
03474
03475 if (TREE_CODE (expr) != INTEGER_CST)
03476 {
03477 error ("%qE is not a valid template argument for type %qT "
03478 "because it is a non-constant expression", expr, type);
03479 return NULL_TREE;
03480 }
03481
03482
03483
03484
03485 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
03486 if (expr == error_mark_node)
03487 return error_mark_node;
03488
03489
03490 expr = fold (expr);
03491 }
03492
03493
03494
03495
03496
03497 else if (TYPE_PTROBV_P (type))
03498 {
03499
03500
03501
03502
03503
03504
03505
03506
03507
03508
03509
03510
03511
03512 bool constant_address_p =
03513 (TREE_CODE (expr) == ADDR_EXPR
03514 || TREE_CODE (expr_type) == ARRAY_TYPE
03515 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
03516
03517 expr = decay_conversion (expr);
03518 if (expr == error_mark_node)
03519 return error_mark_node;
03520
03521 expr = perform_qualification_conversions (type, expr);
03522 if (expr == error_mark_node)
03523 return error_mark_node;
03524
03525 if (!constant_address_p)
03526 {
03527 error ("%qE is not a valid template argument for type %qT "
03528 "because it is not a constant pointer", expr, type);
03529 return NULL_TREE;
03530 }
03531 }
03532
03533
03534
03535
03536
03537
03538
03539 else if (TYPE_REF_OBJ_P (type))
03540 {
03541 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
03542 expr_type))
03543 return error_mark_node;
03544
03545 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
03546 {
03547 error ("%qE is not a valid template argument for type %qT "
03548 "because of conflicts in cv-qualification", expr, type);
03549 return NULL_TREE;
03550 }
03551
03552 if (!real_lvalue_p (expr))
03553 {
03554 error ("%qE is not a valid template argument for type %qT "
03555 "because it is not a lvalue", expr, type);
03556 return NULL_TREE;
03557 }
03558
03559
03560
03561
03562
03563
03564
03565 if (!DECL_EXTERNAL_LINKAGE_P (expr))
03566 {
03567 error ("%qE is not a valid template argument for type %qT "
03568 "because object %qD has not external linkage",
03569 expr, type, expr);
03570 return NULL_TREE;
03571 }
03572
03573 expr = build_nop (type, build_address (expr));
03574 }
03575
03576
03577
03578
03579
03580
03581
03582 else if (TYPE_PTRFN_P (type))
03583 {
03584
03585
03586
03587
03588 if (!type_unknown_p (expr_type))
03589 {
03590 expr = decay_conversion (expr);
03591 if (expr == error_mark_node)
03592 return error_mark_node;
03593 }
03594
03595 expr = convert_nontype_argument_function (type, expr);
03596 if (!expr || expr == error_mark_node)
03597 return expr;
03598 }
03599
03600
03601
03602
03603
03604
03605 else if (TYPE_REFFN_P (type))
03606 {
03607 if (TREE_CODE (expr) == ADDR_EXPR)
03608 {
03609 error ("%qE is not a valid template argument for type %qT "
03610 "because it is a pointer", expr, type);
03611 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
03612 return NULL_TREE;
03613 }
03614
03615 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
03616 if (!expr || expr == error_mark_node)
03617 return expr;
03618
03619 expr = build_nop(type, build_address (expr));
03620 }
03621
03622
03623
03624
03625
03626
03627 else if (TYPE_PTRMEMFUNC_P (type))
03628 {
03629 expr = instantiate_type (type, expr, tf_none);
03630 if (expr == error_mark_node)
03631 return error_mark_node;
03632
03633
03634
03635
03636
03637
03638
03639
03640
03641
03642 if (!same_type_p (TREE_TYPE (expr), type))
03643 {
03644
03645 gcc_assert (can_convert (type, TREE_TYPE (expr)));
03646 error ("%qE is not a valid template argument for type %qT "
03647 "because it is of type %qT", expr, type,
03648 TREE_TYPE (expr));
03649 inform ("standard conversions are not allowed in this context");
03650 return NULL_TREE;
03651 }
03652 }
03653
03654
03655
03656
03657 else if (TYPE_PTRMEM_P (type))
03658 {
03659 expr = perform_qualification_conversions (type, expr);
03660 if (expr == error_mark_node)
03661 return expr;
03662 }
03663
03664 else
03665 gcc_unreachable ();
03666
03667
03668
03669 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
03670 return expr;
03671 }
03672
03673
03674
03675
03676
03677
03678
03679
03680
03681
03682
03683
03684
03685
03686
03687
03688
03689
03690
03691 static int
03692 coerce_template_template_parms (tree parm_parms,
03693 tree arg_parms,
03694 tsubst_flags_t complain,
03695 tree in_decl,
03696 tree outer_args)
03697 {
03698 int nparms, nargs, i;
03699 tree parm, arg;
03700
03701 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
03702 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
03703
03704 nparms = TREE_VEC_LENGTH (parm_parms);
03705 nargs = TREE_VEC_LENGTH (arg_parms);
03706
03707
03708 if (nargs < nparms
03709 || (nargs > nparms
03710 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
03711 return 0;
03712
03713 for (i = 0; i < nparms; ++i)
03714 {
03715 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
03716 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
03717
03718 if (arg == NULL_TREE || arg == error_mark_node
03719 || parm == NULL_TREE || parm == error_mark_node)
03720 return 0;
03721
03722 if (TREE_CODE (arg) != TREE_CODE (parm))
03723 return 0;
03724
03725 switch (TREE_CODE (parm))
03726 {
03727 case TYPE_DECL:
03728 break;
03729
03730 case TEMPLATE_DECL:
03731
03732
03733
03734 {
03735 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
03736 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
03737
03738 if (!coerce_template_template_parms
03739 (parmparm, argparm, complain, in_decl, outer_args))
03740 return 0;
03741 }
03742 break;
03743
03744 case PARM_DECL:
03745
03746
03747
03748
03749
03750
03751
03752 if (!dependent_type_p (TREE_TYPE (arg))
03753 && !same_type_p
03754 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
03755 TREE_TYPE (arg)))
03756 return 0;
03757 break;
03758
03759 default:
03760 gcc_unreachable ();
03761 }
03762 }
03763 return 1;
03764 }
03765
03766
03767
03768
03769
03770
03771
03772
03773 static tree
03774 convert_template_argument (tree parm,
03775 tree arg,
03776 tree args,
03777 tsubst_flags_t complain,
03778 int i,
03779 tree in_decl)
03780 {
03781 tree val;
03782 tree inner_args;
03783 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
03784
03785 inner_args = INNERMOST_TEMPLATE_ARGS (args);
03786
03787 if (TREE_CODE (arg) == TREE_LIST
03788 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
03789 {
03790
03791
03792
03793
03794
03795 arg = TREE_VALUE (arg);
03796 TREE_TYPE (arg) = unknown_type_node;
03797 }
03798
03799 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
03800 requires_type = (TREE_CODE (parm) == TYPE_DECL
03801 || requires_tmpl_type);
03802
03803 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
03804 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
03805 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
03806 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
03807
03808 if (is_tmpl_type
03809 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
03810 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
03811 arg = TYPE_STUB_DECL (arg);
03812
03813 is_type = TYPE_P (arg) || is_tmpl_type;
03814
03815 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
03816 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
03817 {
03818 pedwarn ("to refer to a type member of a template parameter, "
03819 "use %<typename %E%>", arg);
03820
03821 arg = make_typename_type (TREE_OPERAND (arg, 0),
03822 TREE_OPERAND (arg, 1),
03823 typename_type,
03824 complain & tf_error);
03825 is_type = 1;
03826 }
03827 if (is_type != requires_type)
03828 {
03829 if (in_decl)
03830 {
03831 if (complain & tf_error)
03832 {
03833 error ("type/value mismatch at argument %d in template "
03834 "parameter list for %qD",
03835 i + 1, in_decl);
03836 if (is_type)
03837 error (" expected a constant of type %qT, got %qT",
03838 TREE_TYPE (parm),
03839 (is_tmpl_type ? DECL_NAME (arg) : arg));
03840 else if (requires_tmpl_type)
03841 error (" expected a class template, got %qE", arg);
03842 else
03843 error (" expected a type, got %qE", arg);
03844 }
03845 }
03846 return error_mark_node;
03847 }
03848 if (is_tmpl_type ^ requires_tmpl_type)
03849 {
03850 if (in_decl && (complain & tf_error))
03851 {
03852 error ("type/value mismatch at argument %d in template "
03853 "parameter list for %qD",
03854 i + 1, in_decl);
03855 if (is_tmpl_type)
03856 error (" expected a type, got %qT", DECL_NAME (arg));
03857 else
03858 error (" expected a class template, got %qT", arg);
03859 }
03860 return error_mark_node;
03861 }
03862
03863 if (is_type)
03864 {
03865 if (requires_tmpl_type)
03866 {
03867 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
03868
03869
03870 val = TREE_TYPE (arg);
03871 else
03872 {
03873 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
03874 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
03875
03876 if (coerce_template_template_parms (parmparm, argparm,
03877 complain, in_decl,
03878 inner_args))
03879 {
03880 val = arg;
03881
03882
03883
03884 if (val != error_mark_node
03885 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
03886 val = TREE_TYPE (val);
03887 }
03888 else
03889 {
03890 if (in_decl && (complain & tf_error))
03891 {
03892 error ("type/value mismatch at argument %d in "
03893 "template parameter list for %qD",
03894 i + 1, in_decl);
03895 error (" expected a template of type %qD, got %qD",
03896 parm, arg);
03897 }
03898
03899 val = error_mark_node;
03900 }
03901 }
03902 }
03903 else
03904 val = arg;
03905 }
03906 else
03907 {
03908 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
03909
03910 if (invalid_nontype_parm_type_p (t, complain))
03911 return error_mark_node;
03912
03913 if (!uses_template_parms (arg) && !uses_template_parms (t))
03914
03915
03916
03917
03918
03919
03920
03921
03922
03923
03924 val = convert_nontype_argument (t, arg);
03925 else
03926 val = arg;
03927
03928 if (val == NULL_TREE)
03929 val = error_mark_node;
03930 else if (val == error_mark_node && (complain & tf_error))
03931 error ("could not convert template argument %qE to %qT", arg, t);
03932 }
03933
03934 return val;
03935 }
03936
03937
03938
03939
03940
03941
03942
03943
03944
03945
03946
03947 static tree
03948 coerce_template_parms (tree parms,
03949 tree args,
03950 tree in_decl,
03951 tsubst_flags_t complain,
03952 int require_all_arguments)
03953 {
03954 int nparms, nargs, i, lost = 0;
03955 tree inner_args;
03956 tree new_args;
03957 tree new_inner_args;
03958
03959 inner_args = INNERMOST_TEMPLATE_ARGS (args);
03960 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
03961 nparms = TREE_VEC_LENGTH (parms);
03962
03963 if (nargs > nparms
03964 || (nargs < nparms
03965 && require_all_arguments
03966 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
03967 {
03968 if (complain & tf_error)
03969 {
03970 error ("wrong number of template arguments (%d, should be %d)",
03971 nargs, nparms);
03972
03973 if (in_decl)
03974 cp_error_at ("provided for %qD", in_decl);
03975 }
03976
03977 return error_mark_node;
03978 }
03979
03980 new_inner_args = make_tree_vec (nparms);
03981 new_args = add_outermost_template_args (args, new_inner_args);
03982 for (i = 0; i < nparms; i++)
03983 {
03984 tree arg;
03985 tree parm;
03986
03987
03988 parm = TREE_VEC_ELT (parms, i);
03989
03990
03991 if (i < nargs)
03992 arg = TREE_VEC_ELT (inner_args, i);
03993 else if (require_all_arguments)
03994
03995 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
03996 complain, in_decl);
03997 else
03998 break;
03999
04000 gcc_assert (arg);
04001 if (arg == error_mark_node)
04002 {
04003 if (complain & tf_error)
04004 error ("template argument %d is invalid", i + 1);
04005 }
04006 else
04007 arg = convert_template_argument (TREE_VALUE (parm),
04008 arg, new_args, complain, i,
04009 in_decl);
04010
04011 if (arg == error_mark_node)
04012 lost++;
04013 TREE_VEC_ELT (new_inner_args, i) = arg;
04014 }
04015
04016 if (lost)
04017 return error_mark_node;
04018
04019 return new_inner_args;
04020 }
04021
04022
04023
04024 static int
04025 template_args_equal (tree ot, tree nt)
04026 {
04027 if (nt == ot)
04028 return 1;
04029
04030 if (TREE_CODE (nt) == TREE_VEC)
04031
04032 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
04033 else if (TYPE_P (nt))
04034 return TYPE_P (ot) && same_type_p (ot, nt);
04035 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
04036 return 0;
04037 else
04038 return cp_tree_equal (ot, nt);
04039 }
04040
04041
04042
04043
04044 int
04045 comp_template_args (tree oldargs, tree newargs)
04046 {
04047 int i;
04048
04049 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
04050 return 0;
04051
04052 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
04053 {
04054 tree nt = TREE_VEC_ELT (newargs, i);
04055 tree ot = TREE_VEC_ELT (oldargs, i);
04056
04057 if (! template_args_equal (ot, nt))
04058 return 0;
04059 }
04060 return 1;
04061 }
04062
04063
04064
04065
04066 static char *
04067 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
04068 {
04069 static struct obstack scratch_obstack;
04070 static char *scratch_firstobj;
04071 int i, nparms;
04072
04073 if (!scratch_firstobj)
04074 gcc_obstack_init (&scratch_obstack);
04075 else
04076 obstack_free (&scratch_obstack, scratch_firstobj);
04077 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
04078
04079 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
04080 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
04081
04082 cat (name);
04083 ccat ('<');
04084 nparms = TREE_VEC_LENGTH (parms);
04085 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
04086 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
04087 for (i = 0; i < nparms; i++)
04088 {
04089 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
04090 tree arg = TREE_VEC_ELT (arglist, i);
04091
04092 if (i)
04093 ccat (',');
04094
04095 if (TREE_CODE (parm) == TYPE_DECL)
04096 {
04097 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
04098 continue;
04099 }
04100 else if (TREE_CODE (parm) == TEMPLATE_DECL)
04101 {
04102 if (TREE_CODE (arg) == TEMPLATE_DECL)
04103 {
04104
04105
04106 tree context = DECL_CONTEXT (arg);
04107 if (context)
04108 {
04109
04110
04111 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
04112 || CLASS_TYPE_P (context));
04113 cat (decl_as_string (DECL_CONTEXT (arg),
04114 TFF_PLAIN_IDENTIFIER));
04115 cat ("::");
04116 }
04117 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
04118 }
04119 else
04120
04121 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
04122 continue;
04123 }
04124 else
04125 gcc_assert (TREE_CODE (parm) == PARM_DECL);
04126
04127
04128
04129 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
04130 }
04131 {
04132 char *bufp = obstack_next_free (&scratch_obstack);
04133 int offset = 0;
04134 while (bufp[offset - 1] == ' ')
04135 offset--;
04136 obstack_blank_fast (&scratch_obstack, offset);
04137
04138
04139 if (bufp[offset - 1] == '>')
04140 ccat (' ');
04141 }
04142 ccat ('>');
04143 ccat ('\0');
04144 return (char *) obstack_base (&scratch_obstack);
04145 }
04146
04147 static tree
04148 classtype_mangled_name (tree t)
04149 {
04150 if (CLASSTYPE_TEMPLATE_INFO (t)
04151
04152
04153 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
04154 {
04155 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
04156
04157
04158
04159 if (PRIMARY_TEMPLATE_P (tmpl))
04160 {
04161 tree name = DECL_NAME (tmpl);
04162 char *mangled_name = mangle_class_name_for_template
04163 (IDENTIFIER_POINTER (name),
04164 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
04165 CLASSTYPE_TI_ARGS (t));
04166 tree id = get_identifier (mangled_name);
04167 IDENTIFIER_TEMPLATE (id) = name;
04168 return id;
04169 }
04170 }
04171
04172 return TYPE_IDENTIFIER (t);
04173 }
04174
04175 static void
04176 add_pending_template (tree d)
04177 {
04178 tree ti = (TYPE_P (d)
04179 ? CLASSTYPE_TEMPLATE_INFO (d)
04180 : DECL_TEMPLATE_INFO (d));
04181 tree pt;
04182 int level;
04183
04184 if (TI_PENDING_TEMPLATE_FLAG (ti))
04185 return;
04186
04187
04188
04189
04190 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
04191
04192 if (level)
04193 push_tinst_level (d);
04194
04195 pt = tree_cons (current_tinst_level, d, NULL_TREE);
04196 if (last_pending_template)
04197 TREE_CHAIN (last_pending_template) = pt;
04198 else
04199 pending_templates = pt;
04200
04201 last_pending_template = pt;
04202
04203 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
04204
04205 if (level)
04206 pop_tinst_level ();
04207 }
04208
04209
04210
04211
04212
04213
04214 tree
04215 lookup_template_function (tree fns, tree arglist)
04216 {
04217 tree type;
04218
04219 if (fns == error_mark_node || arglist == error_mark_node)
04220 return error_mark_node;
04221
04222 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
04223 gcc_assert (fns && (is_overloaded_fn (fns)
04224 || TREE_CODE (fns) == IDENTIFIER_NODE));
04225
04226 if (BASELINK_P (fns))
04227 {
04228 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
04229 unknown_type_node,
04230 BASELINK_FUNCTIONS (fns),
04231 arglist);
04232 return fns;
04233 }
04234
04235 type = TREE_TYPE (fns);
04236 if (TREE_CODE (fns) == OVERLOAD || !type)
04237 type = unknown_type_node;
04238
04239 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
04240 }
04241
04242
04243
04244
04245
04246
04247
04248
04249 tree
04250 maybe_get_template_decl_from_type_decl (tree decl)
04251 {
04252 return (decl != NULL_TREE
04253 && TREE_CODE (decl) == TYPE_DECL
04254 && DECL_ARTIFICIAL (decl)
04255 && CLASS_TYPE_P (TREE_TYPE (decl))
04256 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
04257 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
04258 }
04259
04260
04261
04262
04263
04264
04265
04266
04267
04268
04269
04270
04271
04272
04273
04274
04275
04276
04277
04278
04279
04280
04281
04282
04283
04284 tree
04285 lookup_template_class (tree d1,
04286 tree arglist,
04287 tree in_decl,
04288 tree context,
04289 int entering_scope,
04290 tsubst_flags_t complain)
04291 {
04292 tree template = NULL_TREE, parmlist;
04293 tree t;
04294
04295 timevar_push (TV_NAME_LOOKUP);
04296
04297 if (TREE_CODE (d1) == IDENTIFIER_NODE)
04298 {
04299 tree value = innermost_non_namespace_value (d1);
04300 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
04301 template = value;
04302 else
04303 {
04304 if (context)
04305 push_decl_namespace (context);
04306 template = lookup_name (d1, 0);
04307 template = maybe_get_template_decl_from_type_decl (template);
04308 if (context)
04309 pop_decl_namespace ();
04310 }
04311 if (template)
04312 context = DECL_CONTEXT (template);
04313 }
04314 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
04315 {
04316 tree type = TREE_TYPE (d1);
04317
04318
04319
04320 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
04321 type = TREE_TYPE (type);
04322
04323 if (CLASSTYPE_TEMPLATE_INFO (type))
04324 {
04325 template = CLASSTYPE_TI_TEMPLATE (type);
04326 d1 = DECL_NAME (template);
04327 }
04328 }
04329 else if (TREE_CODE (d1) == ENUMERAL_TYPE
04330 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
04331 {
04332 template = TYPE_TI_TEMPLATE (d1);
04333 d1 = DECL_NAME (template);
04334 }
04335 else if (TREE_CODE (d1) == TEMPLATE_DECL
04336 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
04337 {
04338 template = d1;
04339 d1 = DECL_NAME (template);
04340 context = DECL_CONTEXT (template);
04341 }
04342
04343
04344 if (! template)
04345 {
04346 if (complain & tf_error)
04347 error ("%qT is not a template", d1);
04348 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04349 }
04350
04351 if (TREE_CODE (template) != TEMPLATE_DECL
04352
04353
04354 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
04355 && !PRIMARY_TEMPLATE_P (template)))
04356 {
04357 if (complain & tf_error)
04358 {
04359 error ("non-template type %qT used as a template", d1);
04360 if (in_decl)
04361 cp_error_at ("for template declaration %qD", in_decl);
04362 }
04363 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04364 }
04365
04366 complain &= ~tf_user;
04367
04368 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
04369 {
04370
04371
04372
04373 tree parm;
04374 tree arglist2;
04375
04376 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
04377
04378
04379
04380
04381
04382
04383
04384
04385
04386
04387
04388
04389
04390
04391
04392
04393
04394 if (current_template_parms)
04395 arglist = add_to_template_args (current_template_args (), arglist);
04396
04397 arglist2 = coerce_template_parms (parmlist, arglist, template,
04398 complain, 1);
04399 if (arglist2 == error_mark_node
04400 || (!uses_template_parms (arglist2)
04401 && check_instantiated_args (template, arglist2, complain)))
04402 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04403
04404 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
04405 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
04406 }
04407 else
04408 {
04409 tree template_type = TREE_TYPE (template);
04410 tree gen_tmpl;
04411 tree type_decl;
04412 tree found = NULL_TREE;
04413 int arg_depth;
04414 int parm_depth;
04415 int is_partial_instantiation;
04416
04417 gen_tmpl = most_general_template (template);
04418 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
04419 parm_depth = TMPL_PARMS_DEPTH (parmlist);
04420 arg_depth = TMPL_ARGS_DEPTH (arglist);
04421
04422 if (arg_depth == 1 && parm_depth > 1)
04423 {
04424
04425
04426
04427
04428
04429
04430
04431
04432
04433
04434
04435
04436 arglist
04437 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
04438 arglist);
04439 arg_depth = TMPL_ARGS_DEPTH (arglist);
04440 }
04441
04442
04443 gcc_assert (parm_depth == arg_depth);
04444
04445
04446
04447 template = gen_tmpl;
04448
04449
04450
04451
04452 if (parm_depth > 1)
04453 {
04454
04455 int i;
04456 int saved_depth = TMPL_ARGS_DEPTH (arglist);
04457
04458 tree bound_args = make_tree_vec (parm_depth);
04459
04460 for (i = saved_depth,
04461 t = DECL_TEMPLATE_PARMS (template);
04462 i > 0 && t != NULL_TREE;
04463 --i, t = TREE_CHAIN (t))
04464 {
04465 tree a = coerce_template_parms (TREE_VALUE (t),
04466 arglist, template,
04467 complain, 1);
04468
04469
04470 if (a == error_mark_node)
04471 {
04472
04473 TREE_VEC_LENGTH (arglist) = saved_depth;
04474 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04475 }
04476
04477 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
04478
04479
04480
04481
04482
04483 TREE_VEC_LENGTH (arglist)--;
04484 }
04485
04486
04487 TREE_VEC_LENGTH (arglist) = saved_depth;
04488
04489 arglist = bound_args;
04490 }
04491 else
04492 arglist
04493 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
04494 INNERMOST_TEMPLATE_ARGS (arglist),
04495 template,
04496 complain, 1);
04497
04498 if (arglist == error_mark_node)
04499
04500 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04501
04502
04503
04504
04505
04506
04507
04508
04509
04510 if (comp_template_args (TYPE_TI_ARGS (template_type),
04511 arglist))
04512 {
04513 found = template_type;
04514
04515 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
04516 {
04517 tree ctx;
04518
04519 for (ctx = current_class_type;
04520 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
04521 ctx = (TYPE_P (ctx)
04522 ? TYPE_CONTEXT (ctx)
04523 : DECL_CONTEXT (ctx)))
04524 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
04525 goto found_ctx;
04526
04527
04528
04529 found = NULL_TREE;
04530 found_ctx:;
04531 }
04532 }
04533 if (found)
04534 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
04535
04536
04537 found = retrieve_specialization (template, arglist,
04538 false);
04539 if (found)
04540 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
04541
04542
04543
04544
04545
04546 is_partial_instantiation = uses_template_parms (arglist);
04547
04548
04549
04550 if (!is_partial_instantiation
04551 && check_instantiated_args (template,
04552 INNERMOST_TEMPLATE_ARGS (arglist),
04553 complain))
04554 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04555
04556 if (!is_partial_instantiation
04557 && !PRIMARY_TEMPLATE_P (template)
04558 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
04559 {
04560 found = xref_tag_from_type (TREE_TYPE (template),
04561 DECL_NAME (template),
04562 ts_global);
04563 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
04564 }
04565
04566 context = tsubst (DECL_CONTEXT (template), arglist,
04567 complain, in_decl);
04568 if (!context)
04569 context = global_namespace;
04570
04571
04572 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
04573 {
04574 if (!is_partial_instantiation)
04575 {
04576 set_current_access_from_decl (TYPE_NAME (template_type));
04577 t = start_enum (TYPE_IDENTIFIER (template_type));
04578 }
04579 else
04580
04581
04582
04583
04584 t = make_node (ENUMERAL_TYPE);
04585 }
04586 else
04587 {
04588 t = make_aggr_type (TREE_CODE (template_type));
04589 CLASSTYPE_DECLARED_CLASS (t)
04590 = CLASSTYPE_DECLARED_CLASS (template_type);
04591 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
04592 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
04593
04594
04595 if (context == current_function_decl)
04596 pushtag (DECL_NAME (template), t, ts_current);
04597 }
04598
04599
04600
04601 if (!TYPE_NAME (t))
04602 {
04603 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
04604
04605 type_decl = create_implicit_typedef (DECL_NAME (template), t);
04606 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
04607 TYPE_STUB_DECL (t) = type_decl;
04608 DECL_SOURCE_LOCATION (type_decl)
04609 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
04610 }
04611 else
04612 type_decl = TYPE_NAME (t);
04613
04614 TREE_PRIVATE (type_decl)
04615 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
04616 TREE_PROTECTED (type_decl)
04617 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
04618 DECL_IN_SYSTEM_HEADER (type_decl)
04619 = DECL_IN_SYSTEM_HEADER (template);
04620
04621
04622
04623
04624 if (parm_depth == 1 || is_partial_instantiation
04625 || !PRIMARY_TEMPLATE_P (template))
04626
04627 found = template;
04628 else
04629 {
04630
04631
04632
04633 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
04634 found; found = TREE_CHAIN (found))
04635 {
04636 int success;
04637 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
04638
04639
04640
04641 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
04642 || !uses_template_parms (TREE_VALUE (found)))
04643 continue;
04644
04645
04646
04647
04648 TREE_VEC_LENGTH (arglist)--;
04649 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
04650
04651
04652
04653 success = comp_template_args (TREE_PURPOSE (found), arglist);
04654
04655
04656 TREE_VEC_LENGTH (arglist)++;
04657 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
04658
04659 if (success)
04660 {
04661 found = tmpl;
04662 break;
04663 }
04664 }
04665
04666 if (!found)
04667 {
04668
04669
04670
04671
04672
04673
04674
04675
04676
04677 TREE_VEC_LENGTH (arglist)--;
04678 found = tsubst (template, arglist, complain, NULL_TREE);
04679 TREE_VEC_LENGTH (arglist)++;
04680 }
04681 }
04682
04683 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
04684 DECL_TEMPLATE_INSTANTIATIONS (template)
04685 = tree_cons (arglist, t,
04686 DECL_TEMPLATE_INSTANTIATIONS (template));
04687
04688 if (TREE_CODE (t) == ENUMERAL_TYPE
04689 && !is_partial_instantiation)
04690
04691
04692
04693
04694
04695
04696
04697 tsubst_enum (template_type, t, arglist);
04698
04699
04700
04701 if (TREE_CODE (t) != ENUMERAL_TYPE)
04702 DECL_NAME (type_decl) = classtype_mangled_name (t);
04703 if (is_partial_instantiation)
04704
04705
04706 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
04707
04708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
04709 }
04710 timevar_pop (TV_NAME_LOOKUP);
04711 }
04712
04713 struct pair_fn_data
04714 {
04715 tree_fn_t fn;
04716 void *data;
04717 struct pointer_set_t *visited;
04718 };
04719
04720
04721
04722 static tree
04723 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
04724 {
04725 tree t = *tp;
04726 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
04727 tree_fn_t fn = pfd->fn;
04728 void *data = pfd->data;
04729
04730 if (TYPE_P (t)
04731 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
04732 return error_mark_node;
04733
04734 switch (TREE_CODE (t))
04735 {
04736 case RECORD_TYPE:
04737 if (TYPE_PTRMEMFUNC_P (t))
04738 break;
04739
04740
04741 case UNION_TYPE:
04742 case ENUMERAL_TYPE:
04743 if (!TYPE_TEMPLATE_INFO (t))
04744 *walk_subtrees = 0;
04745 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
04746 fn, data, pfd->visited))
04747 return error_mark_node;
04748 break;
04749
04750 case METHOD_TYPE:
04751
04752
04753 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
04754 pfd->visited))
04755 return error_mark_node;
04756
04757
04758 case FUNCTION_TYPE:
04759
04760 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
04761 return error_mark_node;
04762
04763
04764
04765
04766
04767
04768
04769 {
04770 tree parm;
04771
04772 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
04773 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
04774 pfd->visited))
04775 return error_mark_node;
04776
04777
04778
04779 *walk_subtrees = 0;
04780 }
04781 break;
04782
04783 case TYPEOF_TYPE:
04784 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
04785 pfd->visited))
04786 return error_mark_node;
04787 break;
04788
04789 case FUNCTION_DECL:
04790 case VAR_DECL:
04791 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
04792 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
04793 pfd->visited))
04794 return error_mark_node;
04795
04796
04797 case PARM_DECL:
04798 case CONST_DECL:
04799 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
04800 && for_each_template_parm (DECL_INITIAL (t), fn, data,
04801 pfd->visited))
04802 return error_mark_node;
04803 if (DECL_CONTEXT (t)
04804 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
04805 pfd->visited))
04806 return error_mark_node;
04807 break;
04808
04809 case BOUND_TEMPLATE_TEMPLATE_PARM:
04810
04811 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
04812 return error_mark_node;
04813
04814
04815 case TEMPLATE_TEMPLATE_PARM:
04816 case TEMPLATE_TYPE_PARM:
04817 case TEMPLATE_PARM_INDEX:
04818 if (fn && (*fn)(t, data))
04819 return error_mark_node;
04820 else if (!fn)
04821 return error_mark_node;
04822 break;
04823
04824 case TEMPLATE_DECL:
04825
04826 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
04827 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
04828 return error_mark_node;
04829
04830
04831 *walk_subtrees = 0;
04832 break;
04833
04834 case TYPENAME_TYPE:
04835 if (!fn
04836 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
04837 data, pfd->visited))
04838 return error_mark_node;
04839 break;
04840
04841 case CONSTRUCTOR:
04842 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
04843 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
04844 (TREE_TYPE (t)), fn, data,
04845 pfd->visited))
04846 return error_mark_node;
04847 break;
04848
04849 case INDIRECT_REF:
04850 case COMPONENT_REF:
04851
04852
04853 if (!fn && !TREE_TYPE (t))
04854 return error_mark_node;
04855 break;
04856
04857 case MODOP_EXPR:
04858 case CAST_EXPR:
04859 case REINTERPRET_CAST_EXPR:
04860 case CONST_CAST_EXPR:
04861 case STATIC_CAST_EXPR:
04862 case DYNAMIC_CAST_EXPR:
04863 case ARROW_EXPR:
04864 case DOTSTAR_EXPR:
04865 case TYPEID_EXPR:
04866 case PSEUDO_DTOR_EXPR:
04867 if (!fn)
04868 return error_mark_node;
04869 break;
04870
04871 case BASELINK:
04872
04873
04874
04875 *walk_subtrees = 0;
04876 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
04877 pfd->visited))
04878 return error_mark_node;
04879 break;
04880
04881 default:
04882 break;
04883 }
04884
04885
04886 return NULL_TREE;
04887 }
04888
04889
04890
04891
04892
04893
04894
04895
04896
04897
04898 static int
04899 for_each_template_parm (tree t, tree_fn_t fn, void* data,
04900 struct pointer_set_t *visited)
04901 {
04902 struct pair_fn_data pfd;
04903 int result;
04904
04905
04906 pfd.fn = fn;
04907 pfd.data = data;
04908
04909
04910
04911
04912
04913
04914 if (visited)
04915 pfd.visited = visited;
04916 else
04917 pfd.visited = pointer_set_create ();
04918 result = walk_tree (&t,
04919 for_each_template_parm_r,
04920 &pfd,
04921 pfd.visited) != NULL_TREE;
04922
04923
04924 if (!visited)
04925 {
04926 pointer_set_destroy (pfd.visited);
04927 pfd.visited = 0;
04928 }
04929
04930 return result;
04931 }
04932
04933
04934 int (*p_uses_template_parms) (tree) = uses_template_parms;
04935
04936
04937
04938 int
04939 uses_template_parms (tree t)
04940 {
04941 bool dependent_p;
04942 int saved_processing_template_decl;
04943
04944 saved_processing_template_decl = processing_template_decl;
04945 if (!saved_processing_template_decl)
04946 processing_template_decl = 1;
04947 if (TYPE_P (t))
04948 dependent_p = dependent_type_p (t);
04949 else if (TREE_CODE (t) == TREE_VEC)
04950 dependent_p = any_dependent_template_arguments_p (t);
04951 else if (TREE_CODE (t) == TREE_LIST)
04952 dependent_p = (uses_template_parms (TREE_VALUE (t))
04953 || uses_template_parms (TREE_CHAIN (t)));
04954 else if (DECL_P (t)
04955 || EXPR_P (t)
04956 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
04957 || TREE_CODE (t) == OVERLOAD
04958 || TREE_CODE (t) == BASELINK
04959 || TREE_CODE (t) == IDENTIFIER_NODE
04960 || CONSTANT_CLASS_P (t))
04961 dependent_p = (type_dependent_expression_p (t)
04962 || value_dependent_expression_p (t));
04963 else
04964 {
04965 gcc_assert (t == error_mark_node);
04966 dependent_p = false;
04967 }
04968
04969 processing_template_decl = saved_processing_template_decl;
04970
04971 return dependent_p;
04972 }
04973
04974
04975
04976 int
04977 uses_template_parms_level (tree t, int level)
04978 {
04979 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
04980 }
04981
04982 static int tinst_depth;
04983 extern int max_tinst_depth;
04984 #ifdef GATHER_STATISTICS
04985 int depth_reached;
04986 #endif
04987 static int tinst_level_tick;
04988 static int last_template_error_tick;
04989
04990
04991
04992
04993 int
04994 push_tinst_level (tree d)
04995 {
04996 tree new;
04997
04998 if (tinst_depth >= max_tinst_depth)
04999 {
05000
05001
05002
05003 if (uses_template_parms (d))
05004 return 0;
05005
05006 last_template_error_tick = tinst_level_tick;
05007 error ("template instantiation depth exceeds maximum of %d (use "
05008 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
05009 max_tinst_depth, d);
05010
05011 print_instantiation_context ();
05012
05013 return 0;
05014 }
05015
05016 new = make_node (TINST_LEVEL);
05017 TINST_DECL (new) = d;
05018 TINST_LOCATION (new) = input_location;
05019 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
05020 TREE_CHAIN (new) = current_tinst_level;
05021 current_tinst_level = new;
05022
05023 ++tinst_depth;
05024 #ifdef GATHER_STATISTICS
05025 if (tinst_depth > depth_reached)
05026 depth_reached = tinst_depth;
05027 #endif
05028
05029 ++tinst_level_tick;
05030 return 1;
05031 }
05032
05033
05034
05035
05036 void
05037 pop_tinst_level (void)
05038 {
05039 tree old = current_tinst_level;
05040
05041
05042
05043 input_location = TINST_LOCATION (old);
05044 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
05045 current_tinst_level = TREE_CHAIN (old);
05046 --tinst_depth;
05047 ++tinst_level_tick;
05048 }
05049
05050
05051
05052
05053
05054 static void
05055 reopen_tinst_level (tree level)
05056 {
05057 tree t;
05058
05059 tinst_depth = 0;
05060 for (t = level; t; t = TREE_CHAIN (t))
05061 ++tinst_depth;
05062
05063 current_tinst_level = level;
05064 pop_tinst_level ();
05065 }
05066
05067
05068
05069
05070
05071
05072 static tree
05073 tsubst_friend_function (tree decl, tree args)
05074 {
05075 tree new_friend;
05076
05077 if (TREE_CODE (decl) == FUNCTION_DECL
05078 && DECL_TEMPLATE_INSTANTIATION (decl)
05079 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
05080
05081
05082
05083
05084
05085
05086
05087
05088 {
05089 tree template_id, arglist, fns;
05090 tree new_args;
05091 tree tmpl;
05092 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
05093
05094
05095
05096
05097 push_nested_namespace (ns);
05098 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
05099 tf_error | tf_warning, NULL_TREE);
05100 pop_nested_namespace (ns);
05101 arglist = tsubst (DECL_TI_ARGS (decl), args,
05102 tf_error | tf_warning, NULL_TREE);
05103 template_id = lookup_template_function (fns, arglist);
05104
05105 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
05106 tmpl = determine_specialization (template_id, new_friend,
05107 &new_args,
05108 0,
05109 TREE_VEC_LENGTH (args));
05110 return instantiate_template (tmpl, new_args, tf_error);
05111 }
05112
05113 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
05114
05115
05116
05117
05118
05119
05120
05121
05122
05123
05124
05125 if (new_friend == error_mark_node)
05126 return error_mark_node;
05127
05128 DECL_USE_TEMPLATE (new_friend) = 0;
05129 if (TREE_CODE (decl) == TEMPLATE_DECL)
05130 {
05131 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
05132 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
05133 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
05134 }
05135
05136
05137
05138
05139
05140 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
05141 {
05142 SET_DECL_RTL (new_friend, NULL_RTX);
05143 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
05144 }
05145
05146 if (DECL_NAMESPACE_SCOPE_P (new_friend))
05147 {
05148 tree old_decl;
05149 tree new_friend_template_info;
05150 tree new_friend_result_template_info;
05151 tree ns;
05152 int new_friend_is_defn;
05153
05154
05155
05156
05157 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
05158 new_friend_is_defn =
05159 (DECL_INITIAL (DECL_TEMPLATE_RESULT
05160 (template_for_substitution (new_friend)))
05161 != NULL_TREE);
05162 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
05163 {
05164
05165 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
05166
05167 new_friend_result_template_info
05168 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
05169 }
05170 else
05171 new_friend_result_template_info = NULL_TREE;
05172
05173
05174
05175
05176 ns = decl_namespace_context (new_friend);
05177 push_nested_namespace (ns);
05178 old_decl = pushdecl_namespace_level (new_friend);
05179 pop_nested_namespace (ns);
05180
05181 if (old_decl != new_friend)
05182 {
05183
05184
05185
05186
05187
05188
05189
05190
05191
05192
05193
05194
05195
05196
05197
05198
05199
05200
05201
05202
05203
05204
05205
05206
05207
05208
05209
05210
05211
05212
05213
05214
05215
05216
05217 if (!new_friend_is_defn)
05218
05219
05220
05221
05222 ;
05223 else
05224 {
05225
05226
05227
05228 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
05229
05230 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
05231 reregister_specialization (new_friend,
05232 most_general_template (old_decl),
05233 old_decl);
05234 else
05235 {
05236 tree t;
05237 tree new_friend_args;
05238
05239 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
05240 = new_friend_result_template_info;
05241
05242 new_friend_args = TI_ARGS (new_friend_template_info);
05243 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
05244 t != NULL_TREE;
05245 t = TREE_CHAIN (t))
05246 {
05247 tree spec = TREE_VALUE (t);
05248
05249 DECL_TI_ARGS (spec)
05250 = add_outermost_template_args (new_friend_args,
05251 DECL_TI_ARGS (spec));
05252 }
05253
05254
05255
05256
05257 t = most_general_template (old_decl);
05258 if (t != old_decl)
05259 {
05260 DECL_TEMPLATE_SPECIALIZATIONS (t)
05261 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
05262 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
05263 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
05264 }
05265 }
05266 }
05267
05268
05269
05270 new_friend = old_decl;
05271 }
05272 }
05273 else
05274 {
05275 tree context = DECL_CONTEXT (new_friend);
05276 bool dependent_p;
05277
05278
05279
05280
05281
05282
05283
05284
05285
05286
05287 ++processing_template_decl;
05288 dependent_p = dependent_type_p (context);
05289 --processing_template_decl;
05290
05291 if (!dependent_p
05292 && !complete_type_or_else (context, NULL_TREE))
05293 return error_mark_node;
05294
05295 if (COMPLETE_TYPE_P (context))
05296 {
05297
05298
05299 tree fn = check_classfn (context,
05300 new_friend, NULL_TREE);
05301
05302 if (fn)
05303 new_friend = fn;
05304 }
05305 }
05306
05307 return new_friend;
05308 }
05309
05310
05311
05312
05313
05314
05315
05316 static tree
05317 tsubst_friend_class (tree friend_tmpl, tree args)
05318 {
05319 tree friend_type;
05320 tree tmpl;
05321 tree context;
05322
05323 context = DECL_CONTEXT (friend_tmpl);
05324
05325 if (context)
05326 {
05327 if (TREE_CODE (context) == NAMESPACE_DECL)
05328 push_nested_namespace (context);
05329 else
05330 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
05331 }
05332
05333
05334 tmpl = lookup_name (DECL_NAME (friend_tmpl), 0);
05335
05336
05337
05338
05339
05340
05341
05342
05343
05344
05345
05346
05347 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
05348 {
05349 tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
05350 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
05351 }
05352
05353 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
05354 {
05355
05356
05357
05358
05359
05360
05361 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
05362 > TMPL_ARGS_DEPTH (args))
05363 {
05364 tree parms;
05365 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
05366 args, tf_error | tf_warning);
05367 redeclare_class_template (TREE_TYPE (tmpl), parms);
05368 }
05369
05370 friend_type = TREE_TYPE (tmpl);
05371 }
05372 else
05373 {
05374
05375
05376
05377 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
05378
05379
05380
05381
05382
05383 DECL_USE_TEMPLATE (tmpl) = 0;
05384 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
05385 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
05386 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
05387 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
05388
05389
05390 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
05391 }
05392
05393 if (context)
05394 {
05395 if (TREE_CODE (context) == NAMESPACE_DECL)
05396 pop_nested_namespace (context);
05397 else
05398 pop_nested_class ();
05399 }
05400
05401 return friend_type;
05402 }
05403
05404
05405
05406
05407 static int
05408 can_complete_type_without_circularity (tree type)
05409 {
05410 if (type == NULL_TREE || type == error_mark_node)
05411 return 0;
05412 else if (COMPLETE_TYPE_P (type))
05413 return 1;
05414 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
05415 return can_complete_type_without_circularity (TREE_TYPE (type));
05416 else if (CLASS_TYPE_P (type)
05417 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
05418 return 0;
05419 else
05420 return 1;
05421 }
05422
05423 tree
05424 instantiate_class_template (tree type)
05425 {
05426 tree template, args, pattern, t, member;
05427 tree typedecl;
05428 tree pbinfo;
05429 tree base_list;
05430
05431 if (type == error_mark_node)
05432 return error_mark_node;
05433
05434 if (TYPE_BEING_DEFINED (type)
05435 || COMPLETE_TYPE_P (type)
05436 || dependent_type_p (type))
05437 return type;
05438
05439
05440 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
05441 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
05442
05443
05444
05445 args = CLASSTYPE_TI_ARGS (type);
05446
05447
05448
05449 t = most_specialized_class (template, args);
05450 if (t == error_mark_node)
05451 {
05452 const char *str = "candidates are:";
05453 error ("ambiguous class template instantiation for %q#T", type);
05454 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
05455 t = TREE_CHAIN (t))
05456 {
05457 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
05458 {
05459 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
05460 str = " ";
05461 }
05462 }
05463 TYPE_BEING_DEFINED (type) = 1;
05464 return error_mark_node;
05465 }
05466
05467 if (t)
05468 pattern = TREE_TYPE (t);
05469 else
05470 pattern = TREE_TYPE (template);
05471
05472
05473
05474 if (!COMPLETE_TYPE_P (pattern))
05475 return type;
05476
05477
05478 if (! push_tinst_level (type))
05479 return type;
05480
05481
05482
05483 TYPE_BEING_DEFINED (type) = 1;
05484
05485
05486
05487 push_deferring_access_checks (dk_no_deferred);
05488
05489 push_to_top_level ();
05490
05491 if (t)
05492 {
05493
05494
05495
05496
05497
05498
05499
05500
05501
05502
05503 tree inner_args
05504 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
05505 args);
05506
05507
05508
05509
05510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
05511 {
05512 args = copy_node (args);
05513 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
05514 }
05515 else
05516 args = inner_args;
05517 }
05518
05519 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
05520
05521
05522
05523 typedecl = TYPE_MAIN_DECL (type);
05524 input_location = DECL_SOURCE_LOCATION (typedecl);
05525 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
05526
05527 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
05528 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
05529 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
05530 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
05531 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
05532 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
05533 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
05534 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
05535 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
05536 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
05537 TYPE_PACKED (type) = TYPE_PACKED (pattern);
05538 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
05539 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
05540 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern);
05541 if (ANON_AGGR_TYPE_P (pattern))
05542 SET_ANON_AGGR_TYPE_P (type);
05543
05544 pbinfo = TYPE_BINFO (pattern);
05545
05546
05547
05548
05549
05550 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
05551 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
05552 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
05553
05554 base_list = NULL_TREE;
05555 if (BINFO_N_BASE_BINFOS (pbinfo))
05556 {
05557 tree pbase_binfo;
05558 tree context = TYPE_CONTEXT (type);
05559 tree pushed_scope;
05560 int i;
05561
05562
05563
05564
05565 pushed_scope = push_scope (context ? context : global_namespace);
05566
05567
05568
05569 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
05570 {
05571 tree base;
05572 tree access = BINFO_BASE_ACCESS (pbinfo, i);
05573
05574
05575 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
05576 if (base == error_mark_node)
05577 continue;
05578
05579 base_list = tree_cons (access, base, base_list);
05580 if (BINFO_VIRTUAL_P (pbase_binfo))
05581 TREE_TYPE (base_list) = integer_type_node;
05582 }
05583
05584
05585 base_list = nreverse (base_list);
05586
05587 if (pushed_scope)
05588 pop_scope (pushed_scope);
05589 }
05590
05591
05592 xref_basetypes (type, base_list);
05593
05594
05595
05596
05597
05598
05599
05600 pushclass (type);
05601
05602
05603 for (member = CLASSTYPE_DECL_LIST (pattern);
05604 member; member = TREE_CHAIN (member))
05605 {
05606 tree t = TREE_VALUE (member);
05607
05608 if (TREE_PURPOSE (member))
05609 {
05610 if (TYPE_P (t))
05611 {
05612
05613
05614 tree tag = t;
05615 tree name = TYPE_IDENTIFIER (tag);
05616 tree newtag;
05617 bool class_template_p;
05618
05619 class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE
05620 && TYPE_LANG_SPECIFIC (tag)
05621 && CLASSTYPE_IS_TEMPLATE (tag));
05622
05623
05624
05625
05626
05627
05628 if (class_template_p)
05629 ++processing_template_decl;
05630 newtag = tsubst (tag, args, tf_error, NULL_TREE);
05631 if (class_template_p)
05632 --processing_template_decl;
05633 if (newtag == error_mark_node)
05634 continue;
05635
05636 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
05637 {
05638 if (class_template_p)
05639
05640
05641
05642
05643
05644
05645
05646
05647
05648
05649 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
05650
05651
05652
05653
05654
05655
05656 if (name)
05657 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
05658 pushtag (name, newtag, ts_current);
05659 }
05660 }
05661 else if (TREE_CODE (t) == FUNCTION_DECL
05662 || DECL_FUNCTION_TEMPLATE_P (t))
05663 {
05664
05665 tree r;
05666
05667 if (TREE_CODE (t) == TEMPLATE_DECL)
05668 ++processing_template_decl;
05669 r = tsubst (t, args, tf_error, NULL_TREE);
05670 if (TREE_CODE (t) == TEMPLATE_DECL)
05671 --processing_template_decl;
05672 set_current_access_from_decl (r);
05673 grok_special_member_properties (r);
05674 finish_member_declaration (r);
05675 }
05676 else
05677 {
05678
05679
05680 if (TREE_CODE (t) != CONST_DECL)
05681 {
05682 tree r;
05683
05684
05685
05686
05687
05688 input_location = DECL_SOURCE_LOCATION (t);
05689
05690 if (TREE_CODE (t) == TEMPLATE_DECL)
05691 ++processing_template_decl;
05692 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
05693 if (TREE_CODE (t) == TEMPLATE_DECL)
05694 --processing_template_decl;
05695 if (TREE_CODE (r) == VAR_DECL)
05696 {
05697
05698
05699
05700
05701
05702
05703
05704
05705
05706
05707
05708 finish_static_data_member_decl
05709 (r,
05710 NULL_TREE,
05711 NULL_TREE,
05712 0);
05713 if (DECL_INITIALIZED_IN_CLASS_P (r))
05714 check_static_variable_definition (r, TREE_TYPE (r));
05715 }
05716 else if (TREE_CODE (r) == FIELD_DECL)
05717 {
05718
05719
05720
05721
05722 tree rtype = TREE_TYPE (r);
05723 if (can_complete_type_without_circularity (rtype))
05724 complete_type (rtype);
05725
05726 if (!COMPLETE_TYPE_P (rtype))
05727 {
05728 cxx_incomplete_type_error (r, rtype);
05729 r = error_mark_node;
05730 }
05731 }
05732
05733
05734
05735
05736
05737 if (!(TREE_CODE (r) == TYPE_DECL
05738 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
05739 && DECL_ARTIFICIAL (r)))
05740 {
05741 set_current_access_from_decl (r);
05742 finish_member_declaration (r);
05743 }
05744 }
05745 }
05746 }
05747 else
05748 {
05749 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
05750 {
05751
05752
05753 tree friend_type = t;
05754 bool adjust_processing_template_decl = false;
05755
05756 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
05757 {
05758 friend_type = tsubst_friend_class (friend_type, args);
05759 adjust_processing_template_decl = true;
05760 }
05761 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
05762 {
05763 friend_type = tsubst (friend_type, args,
05764 tf_error | tf_warning, NULL_TREE);
05765 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
05766 friend_type = TREE_TYPE (friend_type);
05767 adjust_processing_template_decl = true;
05768 }
05769 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
05770 {
05771 friend_type = tsubst (friend_type, args,
05772 tf_error | tf_warning, NULL_TREE);
05773
05774
05775 ++processing_template_decl;
05776 if (dependent_type_p (friend_type))
05777 adjust_processing_template_decl = true;
05778 --processing_template_decl;
05779 }
05780 else if (uses_template_parms (friend_type))
05781 friend_type = tsubst (friend_type, args,
05782 tf_error | tf_warning, NULL_TREE);
05783 else if (CLASSTYPE_USE_TEMPLATE (friend_type))
05784 friend_type = friend_type;
05785 else
05786 {
05787 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
05788
05789
05790
05791 push_nested_namespace (ns);
05792 friend_type =
05793 xref_tag_from_type (friend_type, NULL_TREE,
05794 ts_global);
05795 pop_nested_namespace (ns);
05796 }
05797
05798 if (adjust_processing_template_decl)
05799
05800
05801
05802
05803
05804 ++processing_template_decl;
05805
05806 if (friend_type != error_mark_node)
05807 make_friend_class (type, friend_type, false);
05808
05809 if (adjust_processing_template_decl)
05810 --processing_template_decl;
05811 }
05812 else
05813 {
05814
05815 tree r;
05816
05817
05818
05819
05820
05821 input_location = DECL_SOURCE_LOCATION (t);
05822
05823 if (TREE_CODE (t) == TEMPLATE_DECL)
05824 {
05825 ++processing_template_decl;
05826 push_deferring_access_checks (dk_no_check);
05827 }
05828
05829 r = tsubst_friend_function (t, args);
05830 add_friend (type, r, false);
05831 if (TREE_CODE (t) == TEMPLATE_DECL)
05832 {
05833 pop_deferring_access_checks ();
05834 --processing_template_decl;
05835 }
05836 }
05837 }
05838 }
05839
05840
05841
05842
05843
05844 input_location = DECL_SOURCE_LOCATION (typedecl);
05845
05846 unreverse_member_declarations (type);
05847 finish_struct_1 (type);
05848 TYPE_BEING_DEFINED (type) = 0;
05849
05850
05851
05852
05853 if (!PRIMARY_TEMPLATE_P (template))
05854 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
05855 if (TREE_CODE (t) == FUNCTION_DECL
05856
05857
05858
05859 && DECL_TEMPLATE_INFO (t))
05860 tsubst_default_arguments (t);
05861
05862 popclass ();
05863 pop_from_top_level ();
05864 pop_deferring_access_checks ();
05865 pop_tinst_level ();
05866
05867
05868
05869
05870
05871 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
05872 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
05873
05874 return type;
05875 }
05876
05877 static tree
05878 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
05879 {
05880 tree r;
05881
05882 if (!t)
05883 r = t;
05884 else if (TYPE_P (t))
05885 r = tsubst (t, args, complain, in_decl);
05886 else
05887 {
05888 r = tsubst_expr (t, args, complain, in_decl);
05889 r = fold_non_dependent_expr (r);
05890 }
05891 return r;
05892 }
05893
05894
05895
05896 static tree
05897 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
05898 {
05899 int len = TREE_VEC_LENGTH (t);
05900 int need_new = 0, i;
05901 tree *elts = alloca (len * sizeof (tree));
05902
05903 for (i = 0; i < len; i++)
05904 {
05905 tree orig_arg = TREE_VEC_ELT (t, i);
05906 tree new_arg;
05907
05908 if (TREE_CODE (orig_arg) == TREE_VEC)
05909 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
05910 else
05911 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
05912
05913 if (new_arg == error_mark_node)
05914 return error_mark_node;
05915
05916 elts[i] = new_arg;
05917 if (new_arg != orig_arg)
05918 need_new = 1;
05919 }
05920
05921 if (!need_new)
05922 return t;
05923
05924 t = make_tree_vec (len);
05925 for (i = 0; i < len; i++)
05926 TREE_VEC_ELT (t, i) = elts[i];
05927
05928 return t;
05929 }
05930
05931
05932
05933
05934
05935
05936
05937
05938 static tree
05939 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
05940 {
05941 tree r = NULL_TREE;
05942 tree* new_parms;
05943
05944 for (new_parms = &r;
05945 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
05946 new_parms = &(TREE_CHAIN (*new_parms)),
05947 parms = TREE_CHAIN (parms))
05948 {
05949 tree new_vec =
05950 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
05951 int i;
05952
05953 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
05954 {
05955 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
05956 tree default_value = TREE_PURPOSE (tuple);
05957 tree parm_decl = TREE_VALUE (tuple);
05958
05959 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
05960 default_value = tsubst_template_arg (default_value, args,
05961 complain, NULL_TREE);
05962
05963 tuple = build_tree_list (default_value, parm_decl);
05964 TREE_VEC_ELT (new_vec, i) = tuple;
05965 }
05966
05967 *new_parms =
05968 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
05969 - TMPL_ARGS_DEPTH (args)),
05970 new_vec, NULL_TREE);
05971 }
05972
05973 return r;
05974 }
05975
05976
05977
05978
05979
05980
05981
05982 static tree
05983 tsubst_aggr_type (tree t,
05984 tree args,
05985 tsubst_flags_t complain,
05986 tree in_decl,
05987 int entering_scope)
05988 {
05989 if (t == NULL_TREE)
05990 return NULL_TREE;
05991
05992 switch (TREE_CODE (t))
05993 {
05994 case RECORD_TYPE:
05995 if (TYPE_PTRMEMFUNC_P (t))
05996 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
05997
05998
05999 case ENUMERAL_TYPE:
06000 case UNION_TYPE:
06001 if (TYPE_TEMPLATE_INFO (t))
06002 {
06003 tree argvec;
06004 tree context;
06005 tree r;
06006 bool saved_skip_evaluation;
06007
06008
06009 saved_skip_evaluation = skip_evaluation;
06010 skip_evaluation = false;
06011
06012
06013
06014 context = TYPE_CONTEXT (t);
06015 if (context)
06016 context = tsubst_aggr_type (context, args, complain,
06017 in_decl, 1);
06018
06019
06020
06021
06022
06023
06024
06025
06026
06027
06028 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
06029 complain, in_decl);
06030 if (argvec == error_mark_node)
06031 r = error_mark_node;
06032 else
06033 {
06034 r = lookup_template_class (t, argvec, in_decl, context,
06035 entering_scope, complain);
06036 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
06037 }
06038
06039 skip_evaluation = saved_skip_evaluation;
06040
06041 return r;
06042 }
06043 else
06044
06045 return t;
06046
06047 default:
06048 return tsubst (t, args, complain, in_decl);
06049 }
06050 }
06051
06052
06053
06054
06055 tree
06056 tsubst_default_argument (tree fn, tree type, tree arg)
06057 {
06058 tree saved_class_ptr = NULL_TREE;
06059 tree saved_class_ref = NULL_TREE;
06060
06061
06062
06063
06064
06065
06066
06067
06068
06069
06070
06071
06072
06073 push_access_scope (fn);
06074
06075 if (cfun)
06076 {
06077 saved_class_ptr = current_class_ptr;
06078 cp_function_chain->x_current_class_ptr = NULL_TREE;
06079 saved_class_ref = current_class_ref;
06080 cp_function_chain->x_current_class_ref = NULL_TREE;
06081 }
06082
06083 push_deferring_access_checks(dk_no_deferred);
06084 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
06085 tf_error | tf_warning, NULL_TREE);
06086 pop_deferring_access_checks();
06087
06088
06089 if (cfun)
06090 {
06091 cp_function_chain->x_current_class_ptr = saved_class_ptr;
06092 cp_function_chain->x_current_class_ref = saved_class_ref;
06093 }
06094
06095 pop_access_scope (fn);
06096
06097
06098 arg = check_default_argument (type, arg);
06099
06100 return arg;
06101 }
06102
06103
06104
06105 static void
06106 tsubst_default_arguments (tree fn)
06107 {
06108 tree arg;
06109 tree tmpl_args;
06110
06111 tmpl_args = DECL_TI_ARGS (fn);
06112
06113
06114
06115 if (uses_template_parms (tmpl_args))
06116 return;
06117
06118 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
06119 arg;
06120 arg = TREE_CHAIN (arg))
06121 if (TREE_PURPOSE (arg))
06122 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
06123 TREE_VALUE (arg),
06124 TREE_PURPOSE (arg));
06125 }
06126
06127
06128
06129
06130
06131 static tree
06132 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
06133 {
06134 location_t saved_loc;
06135 tree r = NULL_TREE;
06136 tree in_decl = t;
06137
06138
06139 saved_loc = input_location;
06140 input_location = DECL_SOURCE_LOCATION (t);
06141
06142 switch (TREE_CODE (t))
06143 {
06144 case TEMPLATE_DECL:
06145 {
06146
06147
06148
06149 tree decl = DECL_TEMPLATE_RESULT (t);
06150 tree spec;
06151 tree tmpl_args;
06152 tree full_args;
06153
06154 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
06155 {
06156
06157 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06158 if (new_type == error_mark_node)
06159 return error_mark_node;
06160
06161 r = copy_decl (t);
06162 TREE_CHAIN (r) = NULL_TREE;
06163 TREE_TYPE (r) = new_type;
06164 DECL_TEMPLATE_RESULT (r)
06165 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
06166 DECL_TEMPLATE_PARMS (r)
06167 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
06168 complain);
06169 TYPE_NAME (new_type) = r;
06170 break;
06171 }
06172
06173
06174
06175
06176
06177 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
06178 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
06179 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
06180 full_args = tsubst_template_args (tmpl_args, args,
06181 complain, in_decl);
06182
06183
06184
06185
06186 gcc_assert (full_args != tmpl_args);
06187
06188 spec = retrieve_specialization (t, full_args,
06189 true);
06190 if (spec != NULL_TREE)
06191 {
06192 r = spec;
06193 break;
06194 }
06195
06196
06197
06198
06199
06200
06201 r = copy_decl (t);
06202 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
06203 TREE_CHAIN (r) = NULL_TREE;
06204
06205 DECL_CONTEXT (r)
06206 = tsubst_aggr_type (DECL_CONTEXT (t), args,
06207 complain, in_decl,
06208 1);
06209 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
06210
06211 if (TREE_CODE (decl) == TYPE_DECL)
06212 {
06213 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06214 if (new_type == error_mark_node)
06215 return error_mark_node;
06216
06217 TREE_TYPE (r) = new_type;
06218 CLASSTYPE_TI_TEMPLATE (new_type) = r;
06219 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
06220 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
06221 }
06222 else
06223 {
06224 tree new_decl = tsubst (decl, args, complain, in_decl);
06225 if (new_decl == error_mark_node)
06226 return error_mark_node;
06227
06228 DECL_TEMPLATE_RESULT (r) = new_decl;
06229 DECL_TI_TEMPLATE (new_decl) = r;
06230 TREE_TYPE (r) = TREE_TYPE (new_decl);
06231 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
06232 }
06233
06234 SET_DECL_IMPLICIT_INSTANTIATION (r);
06235 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
06236 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
06237
06238
06239
06240
06241 DECL_TEMPLATE_PARMS (r)
06242 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
06243 complain);
06244
06245 if (PRIMARY_TEMPLATE_P (t))
06246 DECL_PRIMARY_TEMPLATE (r) = r;
06247
06248 if (TREE_CODE (decl) != TYPE_DECL)
06249
06250 register_specialization (r, t,
06251 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
06252 }
06253 break;
06254
06255 case FUNCTION_DECL:
06256 {
06257 tree ctx;
06258 tree argvec = NULL_TREE;
06259 tree *friends;
06260 tree gen_tmpl;
06261 tree type;
06262 int member;
06263 int args_depth;
06264 int parms_depth;
06265
06266
06267 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
06268
06269 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
06270 {
06271 tree spec;
06272 bool dependent_p;
06273
06274
06275
06276
06277
06278 ++processing_template_decl;
06279 dependent_p = value_dependent_expression_p (t);
06280 --processing_template_decl;
06281 if (!dependent_p)
06282 return t;
06283
06284
06285
06286
06287 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
06288 argvec = tsubst_template_args (DECL_TI_ARGS
06289 (DECL_TEMPLATE_RESULT (gen_tmpl)),
06290 args, complain, in_decl);
06291
06292
06293 spec = retrieve_specialization (gen_tmpl, argvec,
06294 false);
06295
06296 if (spec)
06297 {
06298 r = spec;
06299 break;
06300 }
06301
06302
06303
06304
06305
06306
06307
06308
06309
06310
06311
06312
06313
06314
06315
06316
06317
06318
06319
06320
06321
06322
06323
06324
06325
06326
06327
06328
06329
06330
06331
06332
06333
06334 args_depth = TMPL_ARGS_DEPTH (args);
06335 parms_depth =
06336 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
06337 if (args_depth > parms_depth
06338 && !DECL_TEMPLATE_SPECIALIZATION (t))
06339 args = get_innermost_template_args (args, parms_depth);
06340 }
06341 else
06342 {
06343
06344
06345
06346
06347
06348
06349
06350
06351
06352
06353
06354 gen_tmpl = NULL_TREE;
06355 }
06356
06357 if (DECL_CLASS_SCOPE_P (t))
06358 {
06359 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
06360 member = 2;
06361 else
06362 member = 1;
06363 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
06364 complain, t, 1);
06365 }
06366 else
06367 {
06368 member = 0;
06369 ctx = DECL_CONTEXT (t);
06370 }
06371 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06372 if (type == error_mark_node)
06373 return error_mark_node;
06374
06375
06376
06377
06378
06379 r = copy_decl (t);
06380 DECL_USE_TEMPLATE (r) = 0;
06381 TREE_TYPE (r) = type;
06382
06383 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
06384 SET_DECL_RTL (r, NULL_RTX);
06385 DECL_INITIAL (r) = NULL_TREE;
06386 DECL_CONTEXT (r) = ctx;
06387
06388 if (member && DECL_CONV_FN_P (r))
06389
06390
06391 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
06392
06393 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
06394 complain, t);
06395 DECL_RESULT (r) = NULL_TREE;
06396
06397 TREE_STATIC (r) = 0;
06398 TREE_PUBLIC (r) = TREE_PUBLIC (t);
06399 DECL_EXTERNAL (r) = 1;
06400
06401
06402
06403 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
06404 DECL_DEFER_OUTPUT (r) = 0;
06405 TREE_CHAIN (r) = NULL_TREE;
06406 DECL_PENDING_INLINE_INFO (r) = 0;
06407 DECL_PENDING_INLINE_P (r) = 0;
06408 DECL_SAVED_TREE (r) = NULL_TREE;
06409 TREE_USED (r) = 0;
06410 if (DECL_CLONED_FUNCTION (r))
06411 {
06412 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
06413 args, complain, t);
06414 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
06415 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
06416 }
06417
06418
06419
06420
06421 if (gen_tmpl)
06422 {
06423 DECL_TEMPLATE_INFO (r)
06424 = tree_cons (gen_tmpl, argvec, NULL_TREE);
06425 SET_DECL_IMPLICIT_INSTANTIATION (r);
06426 register_specialization (r, gen_tmpl, argvec);
06427
06428
06429
06430
06431
06432
06433
06434
06435
06436
06437
06438
06439 if (!member
06440 && !PRIMARY_TEMPLATE_P (gen_tmpl)
06441 && !uses_template_parms (argvec))
06442 tsubst_default_arguments (r);
06443 }
06444
06445
06446 for (friends = &DECL_BEFRIENDING_CLASSES (r);
06447 *friends;
06448 friends = &TREE_CHAIN (*friends))
06449 {
06450 *friends = copy_node (*friends);
06451 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
06452 args, complain,
06453 in_decl);
06454 }
06455
06456 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
06457 {
06458 maybe_retrofit_in_chrg (r);
06459 if (DECL_CONSTRUCTOR_P (r))
06460 grok_ctor_properties (ctx, r);
06461
06462
06463
06464 if (PRIMARY_TEMPLATE_P (gen_tmpl))
06465 clone_function_decl (r, 0);
06466 }
06467 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
06468 grok_op_properties (r, (complain & tf_error) != 0);
06469
06470 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
06471 SET_DECL_FRIEND_CONTEXT (r,
06472 tsubst (DECL_FRIEND_CONTEXT (t),
06473 args, complain, in_decl));
06474 }
06475 break;
06476
06477 case PARM_DECL:
06478 {
06479 tree type;
06480
06481 r = copy_node (t);
06482 if (DECL_TEMPLATE_PARM_P (t))
06483 SET_DECL_TEMPLATE_PARM_P (r);
06484
06485 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06486 type = type_decays_to (type);
06487 TREE_TYPE (r) = type;
06488 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
06489
06490 if (DECL_INITIAL (r))
06491 {
06492 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
06493 DECL_INITIAL (r) = TREE_TYPE (r);
06494 else
06495 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
06496 complain, in_decl);
06497 }
06498
06499 DECL_CONTEXT (r) = NULL_TREE;
06500
06501 if (!DECL_TEMPLATE_PARM_P (r))
06502 DECL_ARG_TYPE (r) = type_passed_as (type);
06503 if (TREE_CHAIN (t))
06504 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
06505 complain, TREE_CHAIN (t));
06506 }
06507 break;
06508
06509 case FIELD_DECL:
06510 {
06511 tree type;
06512
06513 r = copy_decl (t);
06514 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06515 if (type == error_mark_node)
06516 return error_mark_node;
06517 TREE_TYPE (r) = type;
06518 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
06519
06520
06521
06522 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
06523 complain, in_decl);
06524 TREE_CHAIN (r) = NULL_TREE;
06525 if (VOID_TYPE_P (type))
06526 cp_error_at ("instantiation of %qD as type %qT", r, type);
06527 }
06528 break;
06529
06530 case USING_DECL:
06531 {
06532 r = copy_node (t);
06533
06534 TREE_TYPE (r) = void_type_node;
06535 DECL_INITIAL (r)
06536 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
06537 DECL_NAME (r)
06538 = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
06539 TREE_CHAIN (r) = NULL_TREE;
06540 }
06541 break;
06542
06543 case TYPE_DECL:
06544 case VAR_DECL:
06545 {
06546 tree argvec = NULL_TREE;
06547 tree gen_tmpl = NULL_TREE;
06548 tree spec;
06549 tree tmpl = NULL_TREE;
06550 tree ctx;
06551 tree type = NULL_TREE;
06552 int local_p;
06553
06554 if (TREE_CODE (t) == TYPE_DECL)
06555 {
06556 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06557 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
06558 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
06559 {
06560
06561
06562
06563
06564
06565 r = TYPE_NAME (type);
06566 break;
06567 }
06568 }
06569
06570
06571 local_p = 0;
06572
06573 if (TYPE_P (CP_DECL_CONTEXT (t)))
06574 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
06575 complain,
06576 in_decl, 1);
06577 else if (DECL_NAMESPACE_SCOPE_P (t))
06578 ctx = DECL_CONTEXT (t);
06579 else
06580 {
06581
06582 ctx = NULL_TREE;
06583 local_p = 1;
06584 }
06585
06586
06587 if (!local_p)
06588 {
06589 tmpl = DECL_TI_TEMPLATE (t);
06590 gen_tmpl = most_general_template (tmpl);
06591 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
06592 spec = retrieve_specialization (gen_tmpl, argvec,
06593 false);
06594 }
06595 else
06596 spec = retrieve_local_specialization (t);
06597
06598 if (spec)
06599 {
06600 r = spec;
06601 break;
06602 }
06603
06604 r = copy_decl (t);
06605 if (TREE_CODE (r) == VAR_DECL)
06606 {
06607
06608
06609 DECL_DEAD_FOR_LOCAL (r) = 0;
06610 DECL_INITIALIZED_P (r) = 0;
06611 DECL_TEMPLATE_INSTANTIATED (r) = 0;
06612 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06613 if (type == error_mark_node)
06614 return error_mark_node;
06615 type = complete_type (type);
06616 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
06617 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
06618 type = check_var_type (DECL_NAME (r), type);
06619
06620 if (DECL_VALUE_EXPR (t))
06621 {
06622 tree ve = DECL_VALUE_EXPR (t);
06623 ve = tsubst_expr (ve, args, complain, in_decl);
06624 DECL_VALUE_EXPR (r) = ve;
06625 }
06626 }
06627 else if (DECL_SELF_REFERENCE_P (t))
06628 SET_DECL_SELF_REFERENCE_P (r);
06629 TREE_TYPE (r) = type;
06630 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
06631 DECL_CONTEXT (r) = ctx;
06632
06633 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
06634 SET_DECL_RTL (r, NULL_RTX);
06635
06636
06637 DECL_INITIAL (r) = NULL_TREE;
06638 SET_DECL_RTL (r, NULL_RTX);
06639 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
06640
06641 if (!local_p)
06642 {
06643
06644
06645
06646
06647 DECL_EXTERNAL (r) = 1;
06648
06649 register_specialization (r, gen_tmpl, argvec);
06650 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
06651 SET_DECL_IMPLICIT_INSTANTIATION (r);
06652 }
06653 else
06654 register_local_specialization (r, t);
06655
06656 TREE_CHAIN (r) = NULL_TREE;
06657 layout_decl (r, 0);
06658 }
06659 break;
06660
06661 default:
06662 gcc_unreachable ();
06663 }
06664
06665
06666 input_location = saved_loc;
06667
06668 return r;
06669 }
06670
06671
06672
06673 static tree
06674 tsubst_arg_types (tree arg_types,
06675 tree args,
06676 tsubst_flags_t complain,
06677 tree in_decl)
06678 {
06679 tree remaining_arg_types;
06680 tree type;
06681 tree default_arg;
06682 tree result = NULL_TREE;
06683
06684 if (!arg_types || arg_types == void_list_node)
06685 return arg_types;
06686
06687 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
06688 args, complain, in_decl);
06689 if (remaining_arg_types == error_mark_node)
06690 return error_mark_node;
06691
06692 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
06693 if (type == error_mark_node)
06694 return error_mark_node;
06695 if (VOID_TYPE_P (type))
06696 {
06697 if (complain & tf_error)
06698 {
06699 error ("invalid parameter type %qT", type);
06700 if (in_decl)
06701 cp_error_at ("in declaration %qD", in_decl);
06702 }
06703 return error_mark_node;
06704 }
06705
06706
06707
06708 type = TYPE_MAIN_VARIANT (type_decays_to (type));
06709
06710
06711
06712
06713 default_arg = TREE_PURPOSE (arg_types);
06714
06715 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
06716 {
06717
06718
06719
06720
06721 result = tree_cons (default_arg, type, remaining_arg_types);
06722 TREE_CHAIN (default_arg) = tree_cons (result, NULL_TREE,
06723 TREE_CHAIN (default_arg));
06724 }
06725 else
06726 result = hash_tree_cons (default_arg, type, remaining_arg_types);
06727
06728 return result;
06729 }
06730
06731
06732
06733
06734
06735
06736
06737
06738
06739
06740
06741
06742
06743
06744
06745
06746
06747
06748 static tree
06749 tsubst_function_type (tree t,
06750 tree args,
06751 tsubst_flags_t complain,
06752 tree in_decl)
06753 {
06754 tree return_type;
06755 tree arg_types;
06756 tree fntype;
06757
06758
06759 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
06760
06761
06762 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06763 if (return_type == error_mark_node)
06764 return error_mark_node;
06765
06766
06767
06768
06769 if (TREE_CODE (return_type) == ARRAY_TYPE
06770 || TREE_CODE (return_type) == FUNCTION_TYPE)
06771 {
06772 if (complain & tf_error)
06773 {
06774 if (TREE_CODE (return_type) == ARRAY_TYPE)
06775 error ("function returning an array");
06776 else
06777 error ("function returning a function");
06778 }
06779 return error_mark_node;
06780 }
06781
06782
06783 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
06784 complain, in_decl);
06785 if (arg_types == error_mark_node)
06786 return error_mark_node;
06787
06788
06789 if (TREE_CODE (t) == FUNCTION_TYPE)
06790 fntype = build_function_type (return_type, arg_types);
06791 else
06792 {
06793 tree r = TREE_TYPE (TREE_VALUE (arg_types));
06794 if (! IS_AGGR_TYPE (r))
06795 {
06796
06797
06798
06799
06800
06801
06802
06803 if (complain & tf_error)
06804 error ("creating pointer to member function of non-class type %qT",
06805 r);
06806 return error_mark_node;
06807 }
06808
06809 fntype = build_method_type_directly (r, return_type,
06810 TREE_CHAIN (arg_types));
06811 }
06812 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
06813 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
06814
06815 return fntype;
06816 }
06817
06818
06819
06820
06821
06822 static tree
06823 tsubst_exception_specification (tree fntype,
06824 tree args,
06825 tsubst_flags_t complain,
06826 tree in_decl)
06827 {
06828 tree specs;
06829 tree new_specs;
06830
06831 specs = TYPE_RAISES_EXCEPTIONS (fntype);
06832 new_specs = NULL_TREE;
06833 if (specs)
06834 {
06835 if (! TREE_VALUE (specs))
06836 new_specs = specs;
06837 else
06838 while (specs)
06839 {
06840 tree spec;
06841 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
06842 if (spec == error_mark_node)
06843 return spec;
06844 new_specs = add_exception_specifier (new_specs, spec, complain);
06845 specs = TREE_CHAIN (specs);
06846 }
06847 }
06848 return new_specs;
06849 }
06850
06851
06852
06853 static tree
06854 tsubst_call_declarator_parms (tree parms,
06855 tree args,
06856 tsubst_flags_t complain,
06857 tree in_decl)
06858 {
06859 tree new_parms;
06860 tree type;
06861 tree defarg;
06862
06863 if (!parms || parms == void_list_node)
06864 return parms;
06865
06866 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
06867 args, complain, in_decl);
06868
06869
06870 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
06871
06872
06873
06874 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
06875
06876
06877
06878
06879 new_parms = tree_cons (defarg, type, new_parms);
06880
06881 return new_parms;
06882 }
06883
06884
06885
06886
06887
06888
06889
06890
06891
06892
06893
06894
06895
06896
06897
06898
06899
06900 static tree
06901 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
06902 {
06903 tree type, r;
06904
06905 if (t == NULL_TREE || t == error_mark_node
06906 || t == integer_type_node
06907 || t == void_type_node
06908 || t == char_type_node
06909 || t == unknown_type_node
06910 || TREE_CODE (t) == NAMESPACE_DECL)
06911 return t;
06912
06913 if (DECL_P (t))
06914 return tsubst_decl (t, args, complain);
06915
06916 if (TREE_CODE (t) == IDENTIFIER_NODE)
06917 type = IDENTIFIER_TYPE_VALUE (t);
06918 else
06919 type = TREE_TYPE (t);
06920
06921 gcc_assert (type != unknown_type_node);
06922
06923 if (type
06924 && TREE_CODE (t) != TYPENAME_TYPE
06925 && TREE_CODE (t) != IDENTIFIER_NODE
06926 && TREE_CODE (t) != FUNCTION_TYPE
06927 && TREE_CODE (t) != METHOD_TYPE)
06928 type = tsubst (type, args, complain, in_decl);
06929 if (type == error_mark_node)
06930 return error_mark_node;
06931
06932 switch (TREE_CODE (t))
06933 {
06934 case RECORD_TYPE:
06935 case UNION_TYPE:
06936 case ENUMERAL_TYPE:
06937 return tsubst_aggr_type (t, args, complain, in_decl,
06938 0);
06939
06940 case ERROR_MARK:
06941 case IDENTIFIER_NODE:
06942 case VOID_TYPE:
06943 case REAL_TYPE:
06944 case COMPLEX_TYPE:
06945 case VECTOR_TYPE:
06946 case BOOLEAN_TYPE:
06947 case INTEGER_CST:
06948 case REAL_CST:
06949 case STRING_CST:
06950 return t;
06951
06952 case INTEGER_TYPE:
06953 if (t == integer_type_node)
06954 return t;
06955
06956 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
06957 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
06958 return t;
06959
06960 {
06961 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
06962
06963
06964
06965 max = tsubst_template_arg (omax, args, complain, in_decl);
06966 max = fold_decl_constant_value (max);
06967
06968 if (integer_zerop (omax))
06969 {
06970
06971 if (pedantic)
06972 pedwarn ("creating array with size zero");
06973 }
06974 else if (integer_zerop (max)
06975 || (TREE_CODE (max) == INTEGER_CST
06976 && INT_CST_LT (max, integer_zero_node)))
06977 {
06978
06979
06980
06981
06982
06983
06984
06985 if (complain & tf_error)
06986 error ("creating array with size zero (%qE)", max);
06987
06988 return error_mark_node;
06989 }
06990
06991 return compute_array_index_type (NULL_TREE, max);
06992 }
06993
06994 case TEMPLATE_TYPE_PARM:
06995 case TEMPLATE_TEMPLATE_PARM:
06996 case BOUND_TEMPLATE_TEMPLATE_PARM:
06997 case TEMPLATE_PARM_INDEX:
06998 {
06999 int idx;
07000 int level;
07001 int levels;
07002 tree arg = NULL_TREE;
07003
07004 r = NULL_TREE;
07005
07006 gcc_assert (TREE_VEC_LENGTH (args) > 0);
07007 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
07008 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
07009 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
07010 {
07011 idx = TEMPLATE_TYPE_IDX (t);
07012 level = TEMPLATE_TYPE_LEVEL (t);
07013 }
07014 else
07015 {
07016 idx = TEMPLATE_PARM_IDX (t);
07017 level = TEMPLATE_PARM_LEVEL (t);
07018 }
07019
07020 levels = TMPL_ARGS_DEPTH (args);
07021 if (level <= levels)
07022 arg = TMPL_ARG (args, level, idx);
07023
07024 if (arg == error_mark_node)
07025 return error_mark_node;
07026 else if (arg != NULL_TREE)
07027 {
07028 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
07029 {
07030 gcc_assert (TYPE_P (arg));
07031 return cp_build_qualified_type_real
07032 (arg, cp_type_quals (arg) | cp_type_quals (t),
07033 complain | tf_ignore_bad_quals);
07034 }
07035 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
07036 {
07037
07038
07039 tree argvec = tsubst (TYPE_TI_ARGS (t),
07040 args, complain, in_decl);
07041 if (argvec == error_mark_node)
07042 return error_mark_node;
07043
07044
07045
07046
07047
07048
07049 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
07050 arg = TYPE_NAME (arg);
07051
07052 r = lookup_template_class (arg,
07053 argvec, in_decl,
07054 DECL_CONTEXT (arg),
07055 0,
07056 complain);
07057 return cp_build_qualified_type_real
07058 (r, TYPE_QUALS (t), complain);
07059 }
07060 else
07061
07062 return arg;
07063 }
07064
07065 if (level == 1)
07066
07067
07068
07069 return t;
07070
07071
07072
07073
07074 switch (TREE_CODE (t))
07075 {
07076 case TEMPLATE_TYPE_PARM:
07077 case TEMPLATE_TEMPLATE_PARM:
07078 case BOUND_TEMPLATE_TEMPLATE_PARM:
07079 if (cp_type_quals (t))
07080 {
07081 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
07082 r = cp_build_qualified_type_real
07083 (r, cp_type_quals (t),
07084 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
07085 ? tf_ignore_bad_quals : 0));
07086 }
07087 else
07088 {
07089 r = copy_type (t);
07090 TEMPLATE_TYPE_PARM_INDEX (r)
07091 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
07092 r, levels);
07093 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
07094 TYPE_MAIN_VARIANT (r) = r;
07095 TYPE_POINTER_TO (r) = NULL_TREE;
07096 TYPE_REFERENCE_TO (r) = NULL_TREE;
07097
07098 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
07099 {
07100 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
07101 complain, in_decl);
07102 if (argvec == error_mark_node)
07103 return error_mark_node;
07104
07105 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
07106 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
07107 }
07108 }
07109 break;
07110
07111 case TEMPLATE_PARM_INDEX:
07112 r = reduce_template_parm_level (t, type, levels);
07113 break;
07114
07115 default:
07116 gcc_unreachable ();
07117 }
07118
07119 return r;
07120 }
07121
07122 case TREE_LIST:
07123 {
07124 tree purpose, value, chain;
07125
07126 if (t == void_list_node)
07127 return t;
07128
07129 purpose = TREE_PURPOSE (t);
07130 if (purpose)
07131 {
07132 purpose = tsubst (purpose, args, complain, in_decl);
07133 if (purpose == error_mark_node)
07134 return error_mark_node;
07135 }
07136 value = TREE_VALUE (t);
07137 if (value)
07138 {
07139 value = tsubst (value, args, complain, in_decl);
07140 if (value == error_mark_node)
07141 return error_mark_node;
07142 }
07143 chain = TREE_CHAIN (t);
07144 if (chain && chain != void_type_node)
07145 {
07146 chain = tsubst (chain, args, complain, in_decl);
07147 if (chain == error_mark_node)
07148 return error_mark_node;
07149 }
07150 if (purpose == TREE_PURPOSE (t)
07151 && value == TREE_VALUE (t)
07152 && chain == TREE_CHAIN (t))
07153 return t;
07154 return hash_tree_cons (purpose, value, chain);
07155 }
07156
07157 case TREE_BINFO:
07158
07159 gcc_unreachable ();
07160
07161 case TREE_VEC:
07162
07163 gcc_assert (!type);
07164 return tsubst_template_args (t, args, complain, in_decl);
07165
07166 case POINTER_TYPE:
07167 case REFERENCE_TYPE:
07168 {
07169 enum tree_code code;
07170
07171 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
07172 return t;
07173
07174 code = TREE_CODE (t);
07175
07176
07177
07178
07179
07180
07181
07182
07183
07184
07185 if (TREE_CODE (type) == REFERENCE_TYPE
07186 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
07187 {
07188 static location_t last_loc;
07189
07190
07191
07192
07193 if (complain & tf_error
07194 #ifdef USE_MAPPED_LOCATION
07195 && last_loc != input_location
07196 #else
07197 && (last_loc.line != input_line
07198 || last_loc.file != input_filename)
07199 #endif
07200 )
07201 {
07202 if (TREE_CODE (type) == VOID_TYPE)
07203 error ("forming reference to void");
07204 else
07205 error ("forming %s to reference type %qT",
07206 (code == POINTER_TYPE) ? "pointer" : "reference",
07207 type);
07208 last_loc = input_location;
07209 }
07210
07211 return error_mark_node;
07212 }
07213 else if (code == POINTER_TYPE)
07214 {
07215 r = build_pointer_type (type);
07216 if (TREE_CODE (type) == METHOD_TYPE)
07217 r = build_ptrmemfunc_type (r);
07218 }
07219 else
07220 r = build_reference_type (type);
07221 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
07222
07223 if (r != error_mark_node)
07224
07225 layout_type (r);
07226
07227 return r;
07228 }
07229 case OFFSET_TYPE:
07230 {
07231 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
07232 if (r == error_mark_node || !IS_AGGR_TYPE (r))
07233 {
07234
07235
07236
07237
07238
07239
07240
07241 if (complain & tf_error)
07242 error ("creating pointer to member of non-class type %qT", r);
07243 return error_mark_node;
07244 }
07245 if (TREE_CODE (type) == REFERENCE_TYPE)
07246 {
07247 if (complain & tf_error)
07248 error ("creating pointer to member reference type %qT", type);
07249
07250 return error_mark_node;
07251 }
07252 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
07253 if (TREE_CODE (type) == FUNCTION_TYPE)
07254 {
07255
07256
07257 tree method_type;
07258 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
07259 cp_type_quals (type));
07260 tree memptr;
07261 method_type = build_method_type_directly (this_type,
07262 TREE_TYPE (type),
07263 TYPE_ARG_TYPES (type));
07264 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
07265 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
07266 complain);
07267 }
07268 else
07269 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
07270 TYPE_QUALS (t),
07271 complain);
07272 }
07273 case FUNCTION_TYPE:
07274 case METHOD_TYPE:
07275 {
07276 tree fntype;
07277 tree specs;
07278 fntype = tsubst_function_type (t, args, complain, in_decl);
07279 if (fntype == error_mark_node)
07280 return error_mark_node;
07281
07282
07283 specs = tsubst_exception_specification (t, args, complain,
07284 in_decl);
07285 if (specs == error_mark_node)
07286 return error_mark_node;
07287 if (specs)
07288 fntype = build_exception_variant (fntype, specs);
07289 return fntype;
07290 }
07291 case ARRAY_TYPE:
07292 {
07293 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
07294 if (domain == error_mark_node)
07295 return error_mark_node;
07296
07297
07298
07299 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
07300 return t;
07301
07302
07303
07304
07305
07306
07307
07308
07309
07310
07311 if (TREE_CODE (type) == VOID_TYPE
07312 || TREE_CODE (type) == FUNCTION_TYPE
07313 || TREE_CODE (type) == REFERENCE_TYPE)
07314 {
07315 if (complain & tf_error)
07316 error ("creating array of %qT", type);
07317 return error_mark_node;
07318 }
07319 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
07320 {
07321 if (complain & tf_error)
07322 error ("creating array of %qT, which is an abstract class type",
07323 type);
07324 return error_mark_node;
07325 }
07326
07327 r = build_cplus_array_type (type, domain);
07328 return r;
07329 }
07330
07331 case PLUS_EXPR:
07332 case MINUS_EXPR:
07333 {
07334 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07335 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
07336
07337 if (e1 == error_mark_node || e2 == error_mark_node)
07338 return error_mark_node;
07339
07340 return fold (build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2));
07341 }
07342
07343 case NEGATE_EXPR:
07344 case NOP_EXPR:
07345 {
07346 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07347 if (e == error_mark_node)
07348 return error_mark_node;
07349
07350 return fold (build1 (TREE_CODE (t), TREE_TYPE (t), e));
07351 }
07352
07353 case TYPENAME_TYPE:
07354 {
07355 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
07356 in_decl, 1);
07357 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
07358 complain, in_decl);
07359
07360 if (ctx == error_mark_node || f == error_mark_node)
07361 return error_mark_node;
07362
07363 if (!IS_AGGR_TYPE (ctx))
07364 {
07365 if (complain & tf_error)
07366 error ("%qT is not a class, struct, or union type", ctx);
07367 return error_mark_node;
07368 }
07369 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
07370 {
07371
07372
07373
07374
07375
07376
07377
07378
07379 ctx = complete_type (ctx);
07380 if (!COMPLETE_TYPE_P (ctx))
07381 {
07382 if (complain & tf_error)
07383 cxx_incomplete_type_error (NULL_TREE, ctx);
07384 return error_mark_node;
07385 }
07386 }
07387
07388 f = make_typename_type (ctx, f, typename_type,
07389 (complain & tf_error) | tf_keep_type_decl);
07390 if (f == error_mark_node)
07391 return f;
07392 if (TREE_CODE (f) == TYPE_DECL)
07393 {
07394 complain |= tf_ignore_bad_quals;
07395 f = TREE_TYPE (f);
07396 }
07397
07398 if (TREE_CODE (f) != TYPENAME_TYPE)
07399 {
07400 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
07401 error ("%qT resolves to %qT, which is not an enumeration type",
07402 t, f);
07403 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
07404 error ("%qT resolves to %qT, which is is not a class type",
07405 t, f);
07406 }
07407
07408 return cp_build_qualified_type_real
07409 (f, cp_type_quals (f) | cp_type_quals (t), complain);
07410 }
07411
07412 case UNBOUND_CLASS_TEMPLATE:
07413 {
07414 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
07415 in_decl, 1);
07416 tree name = TYPE_IDENTIFIER (t);
07417 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
07418
07419 if (ctx == error_mark_node || name == error_mark_node)
07420 return error_mark_node;
07421
07422 if (parm_list)
07423 parm_list = tsubst_template_parms (parm_list, args, complain);
07424 return make_unbound_class_template (ctx, name, parm_list, complain);
07425 }
07426
07427 case INDIRECT_REF:
07428 case ADDR_EXPR:
07429 case CALL_EXPR:
07430 gcc_unreachable ();
07431
07432 case ARRAY_REF:
07433 {
07434 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07435 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
07436 if (e1 == error_mark_node || e2 == error_mark_node)
07437 return error_mark_node;
07438
07439 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
07440 }
07441
07442 case SCOPE_REF:
07443 {
07444 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07445 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
07446 if (e1 == error_mark_node || e2 == error_mark_node)
07447 return error_mark_node;
07448
07449 return build_nt (TREE_CODE (t), e1, e2);
07450 }
07451
07452 case TYPEOF_TYPE:
07453 {
07454 tree type;
07455
07456 type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
07457 complain, in_decl));
07458 return cp_build_qualified_type_real (type,
07459 cp_type_quals (t)
07460 | cp_type_quals (type),
07461 complain);
07462 }
07463
07464 default:
07465 sorry ("use of %qs in template",
07466 tree_code_name [(int) TREE_CODE (t)]);
07467 return error_mark_node;
07468 }
07469 }
07470
07471
07472
07473
07474
07475 static tree
07476 tsubst_baselink (tree baselink, tree object_type,
07477 tree args, tsubst_flags_t complain, tree in_decl)
07478 {
07479 tree name;
07480 tree qualifying_scope;
07481 tree fns;
07482 tree template_args = 0;
07483 bool template_id_p = false;
07484
07485
07486
07487
07488
07489
07490
07491
07492
07493 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
07494 fns = BASELINK_FUNCTIONS (baselink);
07495 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
07496 {
07497 template_id_p = true;
07498 template_args = TREE_OPERAND (fns, 1);
07499 fns = TREE_OPERAND (fns, 0);
07500 if (template_args)
07501 template_args = tsubst_template_args (template_args, args,
07502 complain, in_decl);
07503 }
07504 name = DECL_NAME (get_first_fn (fns));
07505 baselink = lookup_fnfields (qualifying_scope, name, 1);
07506
07507
07508
07509
07510
07511 if (BASELINK_P (baselink))
07512 fns = BASELINK_FUNCTIONS (baselink);
07513 if (!template_id_p && !really_overloaded_fn (fns))
07514 mark_used (OVL_CURRENT (fns));
07515
07516
07517 if (BASELINK_P (baselink) && template_id_p)
07518 BASELINK_FUNCTIONS (baselink)
07519 = build_nt (TEMPLATE_ID_EXPR,
07520 BASELINK_FUNCTIONS (baselink),
07521 template_args);
07522
07523 if (!object_type)
07524 object_type = current_class_type;
07525 return adjust_result_of_qualified_name_lookup (baselink,
07526 qualifying_scope,
07527 object_type);
07528 }
07529
07530
07531
07532
07533
07534
07535
07536 static tree
07537 tsubst_qualified_id (tree qualified_id, tree args,
07538 tsubst_flags_t complain, tree in_decl,
07539 bool done, bool address_p)
07540 {
07541 tree expr;
07542 tree scope;
07543 tree name;
07544 bool is_template;
07545 tree template_args;
07546
07547 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
07548
07549
07550 name = TREE_OPERAND (qualified_id, 1);
07551 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
07552 {
07553 is_template = true;
07554 template_args = TREE_OPERAND (name, 1);
07555 if (template_args)
07556 template_args = tsubst_template_args (template_args, args,
07557 complain, in_decl);
07558 name = TREE_OPERAND (name, 0);
07559 }
07560 else
07561 {
07562 is_template = false;
07563 template_args = NULL_TREE;
07564 }
07565
07566
07567
07568
07569
07570 scope = TREE_OPERAND (qualified_id, 0);
07571 if (args)
07572 {
07573 scope = tsubst (scope, args, complain, in_decl);
07574 expr = tsubst_copy (name, args, complain, in_decl);
07575 }
07576 else
07577 expr = name;
07578
07579 if (dependent_type_p (scope))
07580 return build_nt (SCOPE_REF, scope, expr);
07581
07582 if (!BASELINK_P (name) && !DECL_P (expr))
07583 {
07584 expr = lookup_qualified_name (scope, expr, 0, false);
07585 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
07586 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
07587 {
07588 if (complain & tf_error)
07589 {
07590 error ("dependent-name %qE is parsed as a non-type, but "
07591 "instantiation yields a type", qualified_id);
07592 inform ("say %<typename %E%> if a type is meant", qualified_id);
07593 }
07594 return error_mark_node;
07595 }
07596 }
07597
07598 if (DECL_P (expr))
07599 {
07600 check_accessibility_of_qualified_id (expr, NULL_TREE,
07601 scope);
07602
07603 mark_used (expr);
07604 }
07605
07606 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
07607 {
07608 if (complain & tf_error)
07609 qualified_name_lookup_error (scope,
07610 TREE_OPERAND (qualified_id, 1),
07611 expr);
07612 return error_mark_node;
07613 }
07614
07615 if (is_template)
07616 expr = lookup_template_function (expr, template_args);
07617
07618 if (expr == error_mark_node && complain & tf_error)
07619 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
07620 expr);
07621 else if (TYPE_P (scope))
07622 {
07623 expr = (adjust_result_of_qualified_name_lookup
07624 (expr, scope, current_class_type));
07625 expr = finish_qualified_id_expr (scope, expr, done, address_p);
07626 }
07627
07628 if (TREE_CODE (expr) != SCOPE_REF)
07629 expr = convert_from_reference (expr);
07630
07631 return expr;
07632 }
07633
07634
07635
07636
07637
07638 static tree
07639 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
07640 {
07641 enum tree_code code;
07642 tree r;
07643
07644 if (t == NULL_TREE || t == error_mark_node)
07645 return t;
07646
07647 code = TREE_CODE (t);
07648
07649 switch (code)
07650 {
07651 case PARM_DECL:
07652 r = retrieve_local_specialization (t);
07653 gcc_assert (r != NULL);
07654 mark_used (r);
07655 return r;
07656
07657 case CONST_DECL:
07658 {
07659 tree enum_type;
07660 tree v;
07661
07662 if (DECL_TEMPLATE_PARM_P (t))
07663 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
07664
07665
07666 if (DECL_NAMESPACE_SCOPE_P (t))
07667 return t;
07668
07669 if (args == NULL_TREE)
07670 return integral_constant_value (t);
07671
07672
07673
07674
07675
07676
07677
07678
07679
07680
07681
07682 enum_type
07683 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
07684 0);
07685
07686 for (v = TYPE_VALUES (enum_type);
07687 v != NULL_TREE;
07688 v = TREE_CHAIN (v))
07689 if (TREE_PURPOSE (v) == DECL_NAME (t))
07690 return TREE_VALUE (v);
07691
07692
07693
07694
07695 gcc_unreachable ();
07696 }
07697 return t;
07698
07699 case FIELD_DECL:
07700 if (DECL_CONTEXT (t))
07701 {
07702 tree ctx;
07703
07704 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
07705 1);
07706 if (ctx != DECL_CONTEXT (t))
07707 {
07708 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
07709 if (!r)
07710 {
07711 if (complain & tf_error)
07712 error ("using invalid field %qD", t);
07713 return error_mark_node;
07714 }
07715 return r;
07716 }
07717 }
07718
07719 return t;
07720
07721 case VAR_DECL:
07722 case FUNCTION_DECL:
07723 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
07724 || local_variable_p (t))
07725 t = tsubst (t, args, complain, in_decl);
07726 mark_used (t);
07727 return t;
07728
07729 case BASELINK:
07730 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
07731
07732 case TEMPLATE_DECL:
07733 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
07734 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
07735 args, complain, in_decl);
07736 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
07737 return tsubst (t, args, complain, in_decl);
07738 else if (DECL_CLASS_SCOPE_P (t)
07739 && uses_template_parms (DECL_CONTEXT (t)))
07740 {
07741
07742
07743
07744
07745
07746
07747
07748
07749
07750
07751
07752
07753
07754
07755
07756 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
07757 return lookup_field (context, DECL_NAME(t), 0, false);
07758 }
07759 else
07760
07761 return t;
07762
07763 case CAST_EXPR:
07764 case REINTERPRET_CAST_EXPR:
07765 case CONST_CAST_EXPR:
07766 case STATIC_CAST_EXPR:
07767 case DYNAMIC_CAST_EXPR:
07768 case NOP_EXPR:
07769 return build1
07770 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
07771 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
07772
07773 case INDIRECT_REF:
07774 case NEGATE_EXPR:
07775 case TRUTH_NOT_EXPR:
07776 case BIT_NOT_EXPR:
07777 case ADDR_EXPR:
07778 case CONVERT_EXPR:
07779 case SIZEOF_EXPR:
07780 case ALIGNOF_EXPR:
07781 case ARROW_EXPR:
07782 case THROW_EXPR:
07783 case TYPEID_EXPR:
07784 case REALPART_EXPR:
07785 case IMAGPART_EXPR:
07786 return build1
07787 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
07788 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
07789
07790 case COMPONENT_REF:
07791 {
07792 tree object;
07793 tree name;
07794
07795 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
07796 name = TREE_OPERAND (t, 1);
07797 if (TREE_CODE (name) == BIT_NOT_EXPR)
07798 {
07799 name = tsubst_copy (TREE_OPERAND (name, 0), args,
07800 complain, in_decl);
07801 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
07802 }
07803 else if (TREE_CODE (name) == SCOPE_REF
07804 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
07805 {
07806 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
07807 complain, in_decl);
07808 name = TREE_OPERAND (name, 1);
07809 name = tsubst_copy (TREE_OPERAND (name, 0), args,
07810 complain, in_decl);
07811 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
07812 name = build_nt (SCOPE_REF, base, name);
07813 }
07814 else if (TREE_CODE (name) == BASELINK)
07815 name = tsubst_baselink (name,
07816 non_reference (TREE_TYPE (object)),
07817 args, complain,
07818 in_decl);
07819 else
07820 name = tsubst_copy (name, args, complain, in_decl);
07821 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
07822 }
07823
07824 case PLUS_EXPR:
07825 case MINUS_EXPR:
07826 case MULT_EXPR:
07827 case TRUNC_DIV_EXPR:
07828 case CEIL_DIV_EXPR:
07829 case FLOOR_DIV_EXPR:
07830 case ROUND_DIV_EXPR:
07831 case EXACT_DIV_EXPR:
07832 case BIT_AND_EXPR:
07833 case BIT_IOR_EXPR:
07834 case BIT_XOR_EXPR:
07835 case TRUNC_MOD_EXPR:
07836 case FLOOR_MOD_EXPR:
07837 case TRUTH_ANDIF_EXPR:
07838 case TRUTH_ORIF_EXPR:
07839 case TRUTH_AND_EXPR:
07840 case TRUTH_OR_EXPR:
07841 case RSHIFT_EXPR:
07842 case LSHIFT_EXPR:
07843 case RROTATE_EXPR:
07844 case LROTATE_EXPR:
07845 case EQ_EXPR:
07846 case NE_EXPR:
07847 case MAX_EXPR:
07848 case MIN_EXPR:
07849 case LE_EXPR:
07850 case GE_EXPR:
07851 case LT_EXPR:
07852 case GT_EXPR:
07853 case COMPOUND_EXPR:
07854 case SCOPE_REF:
07855 case DOTSTAR_EXPR:
07856 case MEMBER_REF:
07857 case PREDECREMENT_EXPR:
07858 case PREINCREMENT_EXPR:
07859 case POSTDECREMENT_EXPR:
07860 case POSTINCREMENT_EXPR:
07861 return build_nt
07862 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07863 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
07864
07865 case ARRAY_REF:
07866 return build_nt
07867 (ARRAY_REF,
07868 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07869 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
07870 NULL_TREE, NULL_TREE);
07871
07872 case CALL_EXPR:
07873 return build_nt (code,
07874 tsubst_copy (TREE_OPERAND (t, 0), args,
07875 complain, in_decl),
07876 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
07877 in_decl),
07878 NULL_TREE);
07879
07880 case COND_EXPR:
07881 case MODOP_EXPR:
07882 case PSEUDO_DTOR_EXPR:
07883 {
07884 r = build_nt
07885 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07886 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
07887 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
07888 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
07889 return r;
07890 }
07891
07892 case NEW_EXPR:
07893 {
07894 r = build_nt
07895 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07896 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
07897 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
07898 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
07899 return r;
07900 }
07901
07902 case DELETE_EXPR:
07903 {
07904 r = build_nt
07905 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07906 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
07907 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
07908 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
07909 return r;
07910 }
07911
07912 case TEMPLATE_ID_EXPR:
07913 {
07914
07915 tree fn = TREE_OPERAND (t, 0);
07916 tree targs = TREE_OPERAND (t, 1);
07917
07918 fn = tsubst_copy (fn, args, complain, in_decl);
07919 if (targs)
07920 targs = tsubst_template_args (targs, args, complain, in_decl);
07921
07922 return lookup_template_function (fn, targs);
07923 }
07924
07925 case TREE_LIST:
07926 {
07927 tree purpose, value, chain;
07928
07929 if (t == void_list_node)
07930 return t;
07931
07932 purpose = TREE_PURPOSE (t);
07933 if (purpose)
07934 purpose = tsubst_copy (purpose, args, complain, in_decl);
07935 value = TREE_VALUE (t);
07936 if (value)
07937 value = tsubst_copy (value, args, complain, in_decl);
07938 chain = TREE_CHAIN (t);
07939 if (chain && chain != void_type_node)
07940 chain = tsubst_copy (chain, args, complain, in_decl);
07941 if (purpose == TREE_PURPOSE (t)
07942 && value == TREE_VALUE (t)
07943 && chain == TREE_CHAIN (t))
07944 return t;
07945 return tree_cons (purpose, value, chain);
07946 }
07947
07948 case RECORD_TYPE:
07949 case UNION_TYPE:
07950 case ENUMERAL_TYPE:
07951 case INTEGER_TYPE:
07952 case TEMPLATE_TYPE_PARM:
07953 case TEMPLATE_TEMPLATE_PARM:
07954 case BOUND_TEMPLATE_TEMPLATE_PARM:
07955 case TEMPLATE_PARM_INDEX:
07956 case POINTER_TYPE:
07957 case REFERENCE_TYPE:
07958 case OFFSET_TYPE:
07959 case FUNCTION_TYPE:
07960 case METHOD_TYPE:
07961 case ARRAY_TYPE:
07962 case TYPENAME_TYPE:
07963 case UNBOUND_CLASS_TEMPLATE:
07964 case TYPEOF_TYPE:
07965 case TYPE_DECL:
07966 return tsubst (t, args, complain, in_decl);
07967
07968 case IDENTIFIER_NODE:
07969 if (IDENTIFIER_TYPENAME_P (t))
07970 {
07971 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
07972 return mangle_conv_op_name_for_type (new_type);
07973 }
07974 else
07975 return t;
07976
07977 case CONSTRUCTOR:
07978 {
07979 r = build_constructor
07980 (tsubst (TREE_TYPE (t), args, complain, in_decl),
07981 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
07982 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
07983 return r;
07984 }
07985
07986 case VA_ARG_EXPR:
07987 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
07988 in_decl),
07989 tsubst (TREE_TYPE (t), args, complain, in_decl));
07990
07991 case CLEANUP_POINT_EXPR:
07992
07993
07994
07995 gcc_unreachable ();
07996
07997 default:
07998 return t;
07999 }
08000 }
08001
08002
08003
08004
08005 static tree
08006 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
08007 {
08008 tree stmt, tmp;
08009
08010 if (t == NULL_TREE || t == error_mark_node)
08011 return t;
08012
08013 if (EXPR_HAS_LOCATION (t))
08014 input_location = EXPR_LOCATION (t);
08015 if (STATEMENT_CODE_P (TREE_CODE (t)))
08016 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
08017
08018 switch (TREE_CODE (t))
08019 {
08020 case STATEMENT_LIST:
08021 {
08022 tree_stmt_iterator i;
08023 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
08024 tsubst_expr (tsi_stmt (i), args, complain, in_decl);
08025 break;
08026 }
08027
08028 case CTOR_INITIALIZER:
08029 finish_mem_initializers (tsubst_initializer_list
08030 (TREE_OPERAND (t, 0), args));
08031 break;
08032
08033 case RETURN_EXPR:
08034 finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
08035 args, complain, in_decl));
08036 break;
08037
08038 case EXPR_STMT:
08039 tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
08040 if (EXPR_STMT_STMT_EXPR_RESULT (t))
08041 finish_stmt_expr_expr (tmp, cur_stmt_expr);
08042 else
08043 finish_expr_stmt (tmp);
08044 break;
08045
08046 case USING_STMT:
08047 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
08048 args, complain, in_decl));
08049 break;
08050
08051 case DECL_EXPR:
08052 {
08053 tree decl;
08054 tree init;
08055
08056 decl = DECL_EXPR_DECL (t);
08057 if (TREE_CODE (decl) == LABEL_DECL)
08058 finish_label_decl (DECL_NAME (decl));
08059 else if (TREE_CODE (decl) == USING_DECL)
08060 {
08061 tree scope = DECL_INITIAL (decl);
08062 tree name = DECL_NAME (decl);
08063 tree decl;
08064
08065 scope = tsubst_expr (scope, args, complain, in_decl);
08066 decl = lookup_qualified_name (scope, name,
08067 false,
08068 false);
08069 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
08070 qualified_name_lookup_error (scope, name, decl);
08071 else
08072 do_local_using_decl (decl, scope, name);
08073 }
08074 else
08075 {
08076 init = DECL_INITIAL (decl);
08077 decl = tsubst (decl, args, complain, in_decl);
08078 if (decl != error_mark_node)
08079 {
08080 if (init)
08081 DECL_INITIAL (decl) = error_mark_node;
08082
08083
08084
08085
08086
08087 if (TREE_CODE (decl) == VAR_DECL)
08088 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
08089 if (TREE_CODE (decl) == VAR_DECL
08090 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
08091
08092 finish_anon_union (decl);
08093 else
08094 {
08095 maybe_push_decl (decl);
08096 if (TREE_CODE (decl) == VAR_DECL
08097 && DECL_PRETTY_FUNCTION_P (decl))
08098 {
08099
08100
08101 const char *const name
08102 = cxx_printable_name (current_function_decl, 2);
08103 init = cp_fname_init (name, &TREE_TYPE (decl));
08104 }
08105 else
08106 init = tsubst_expr (init, args, complain, in_decl);
08107 cp_finish_decl (decl, init, NULL_TREE, 0);
08108 }
08109 }
08110 }
08111
08112
08113
08114 return decl;
08115 }
08116
08117 case FOR_STMT:
08118 stmt = begin_for_stmt ();
08119 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
08120 finish_for_init_stmt (stmt);
08121 tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
08122 finish_for_cond (tmp, stmt);
08123 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
08124 finish_for_expr (tmp, stmt);
08125 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
08126 finish_for_stmt (stmt);
08127 break;
08128
08129 case WHILE_STMT:
08130 stmt = begin_while_stmt ();
08131 tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
08132 finish_while_stmt_cond (tmp, stmt);
08133 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
08134 finish_while_stmt (stmt);
08135 break;
08136
08137 case DO_STMT:
08138 stmt = begin_do_stmt ();
08139 tsubst_expr (DO_BODY (t), args, complain, in_decl);
08140 finish_do_body (stmt);
08141 tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
08142 finish_do_stmt (tmp, stmt);
08143 break;
08144
08145 case IF_STMT:
08146 stmt = begin_if_stmt ();
08147 tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
08148 finish_if_stmt_cond (tmp, stmt);
08149 tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
08150 finish_then_clause (stmt);
08151
08152 if (ELSE_CLAUSE (t))
08153 {
08154 begin_else_clause (stmt);
08155 tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
08156 finish_else_clause (stmt);
08157 }
08158
08159 finish_if_stmt (stmt);
08160 break;
08161
08162 case BIND_EXPR:
08163 if (BIND_EXPR_BODY_BLOCK (t))
08164 stmt = begin_function_body ();
08165 else
08166 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
08167 ? BCS_TRY_BLOCK : 0);
08168
08169 tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
08170
08171 if (BIND_EXPR_BODY_BLOCK (t))
08172 finish_function_body (stmt);
08173 else
08174 finish_compound_stmt (stmt);
08175 break;
08176
08177 case BREAK_STMT:
08178 finish_break_stmt ();
08179 break;
08180
08181 case CONTINUE_STMT:
08182 finish_continue_stmt ();
08183 break;
08184
08185 case SWITCH_STMT:
08186 stmt = begin_switch_stmt ();
08187 tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
08188 finish_switch_cond (tmp, stmt);
08189 tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
08190 finish_switch_stmt (stmt);
08191 break;
08192
08193 case CASE_LABEL_EXPR:
08194 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
08195 tsubst_expr (CASE_HIGH (t), args, complain,
08196 in_decl));
08197 break;
08198
08199 case LABEL_EXPR:
08200 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
08201 break;
08202
08203 case GOTO_EXPR:
08204 tmp = GOTO_DESTINATION (t);
08205 if (TREE_CODE (tmp) != LABEL_DECL)
08206
08207
08208
08209 tmp = tsubst_expr (tmp, args, complain, in_decl);
08210 else
08211 tmp = DECL_NAME (tmp);
08212 finish_goto_stmt (tmp);
08213 break;
08214
08215 case ASM_EXPR:
08216 tmp = finish_asm_stmt
08217 (ASM_VOLATILE_P (t),
08218 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
08219 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
08220 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
08221 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
08222 {
08223 tree asm_expr = tmp;
08224 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
08225 asm_expr = TREE_OPERAND (asm_expr, 0);
08226 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
08227 }
08228 break;
08229
08230 case TRY_BLOCK:
08231 if (CLEANUP_P (t))
08232 {
08233 stmt = begin_try_block ();
08234 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
08235 finish_cleanup_try_block (stmt);
08236 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
08237 complain, in_decl),
08238 stmt);
08239 }
08240 else
08241 {
08242 if (FN_TRY_BLOCK_P (t))
08243 stmt = begin_function_try_block ();
08244 else
08245 stmt = begin_try_block ();
08246
08247 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
08248
08249 if (FN_TRY_BLOCK_P (t))
08250 finish_function_try_block (stmt);
08251 else
08252 finish_try_block (stmt);
08253
08254 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
08255 if (FN_TRY_BLOCK_P (t))
08256 finish_function_handler_sequence (stmt);
08257 else
08258 finish_handler_sequence (stmt);
08259 }
08260 break;
08261
08262 case HANDLER:
08263 {
08264 tree decl;
08265
08266 stmt = begin_handler ();
08267 if (HANDLER_PARMS (t))
08268 {
08269 decl = HANDLER_PARMS (t);
08270 decl = tsubst (decl, args, complain, in_decl);
08271
08272
08273
08274 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
08275 }
08276 else
08277 decl = NULL_TREE;
08278 finish_handler_parms (decl, stmt);
08279 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
08280 finish_handler (stmt);
08281 }
08282 break;
08283
08284 case TAG_DEFN:
08285 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
08286 break;
08287
08288 default:
08289 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
08290
08291 return tsubst_copy_and_build (t, args, complain, in_decl,
08292 false);
08293 }
08294
08295 return NULL_TREE;
08296 }
08297
08298
08299
08300
08301 static tree
08302 tsubst_non_call_postfix_expression (tree t, tree args,
08303 tsubst_flags_t complain,
08304 tree in_decl)
08305 {
08306 if (TREE_CODE (t) == SCOPE_REF)
08307 t = tsubst_qualified_id (t, args, complain, in_decl,
08308 false, false);
08309 else
08310 t = tsubst_copy_and_build (t, args, complain, in_decl,
08311 false);
08312
08313 return t;
08314 }
08315
08316
08317
08318
08319 tree
08320 tsubst_copy_and_build (tree t,
08321 tree args,
08322 tsubst_flags_t complain,
08323 tree in_decl,
08324 bool function_p)
08325 {
08326 #define RECUR(NODE) \
08327 tsubst_copy_and_build (NODE, args, complain, in_decl, false)
08328
08329 tree op1;
08330
08331 if (t == NULL_TREE || t == error_mark_node)
08332 return t;
08333
08334 switch (TREE_CODE (t))
08335 {
08336 case USING_DECL:
08337 t = DECL_NAME (t);
08338
08339 case IDENTIFIER_NODE:
08340 {
08341 tree decl;
08342 cp_id_kind idk;
08343 tree qualifying_class;
08344 bool non_integral_constant_expression_p;
08345 const char *error_msg;
08346
08347 if (IDENTIFIER_TYPENAME_P (t))
08348 {
08349 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
08350 t = mangle_conv_op_name_for_type (new_type);
08351 }
08352
08353
08354 decl = lookup_name (t, 0);
08355
08356
08357
08358 if (decl == NULL_TREE)
08359 decl = error_mark_node;
08360
08361 decl = finish_id_expression (t, decl, NULL_TREE,
08362 &idk,
08363 &qualifying_class,
08364 false,
08365 false,
08366 &non_integral_constant_expression_p,
08367 &error_msg);
08368 if (error_msg)
08369 error (error_msg);
08370 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
08371 decl = unqualified_name_lookup_error (decl);
08372 return decl;
08373 }
08374
08375 case TEMPLATE_ID_EXPR:
08376 {
08377 tree object;
08378 tree template = RECUR (TREE_OPERAND (t, 0));
08379 tree targs = TREE_OPERAND (t, 1);
08380
08381 if (targs)
08382 targs = tsubst_template_args (targs, args, complain, in_decl);
08383
08384 if (TREE_CODE (template) == COMPONENT_REF)
08385 {
08386 object = TREE_OPERAND (template, 0);
08387 template = TREE_OPERAND (template, 1);
08388 }
08389 else
08390 object = NULL_TREE;
08391 template = lookup_template_function (template, targs);
08392
08393 if (object)
08394 return build3 (COMPONENT_REF, TREE_TYPE (template),
08395 object, template, NULL_TREE);
08396 else
08397 return template;
08398 }
08399
08400 case INDIRECT_REF:
08401 {
08402 tree r = RECUR (TREE_OPERAND (t, 0));
08403
08404 if (REFERENCE_REF_P (t))
08405 {
08406
08407
08408
08409 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
08410 r = convert_from_reference (r);
08411 }
08412 else
08413 r = build_x_indirect_ref (r, "unary *");
08414 return r;
08415 }
08416
08417 case NOP_EXPR:
08418 return build_nop
08419 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08420 RECUR (TREE_OPERAND (t, 0)));
08421
08422 case CAST_EXPR:
08423 return build_functional_cast
08424 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08425 RECUR (TREE_OPERAND (t, 0)));
08426
08427 case REINTERPRET_CAST_EXPR:
08428 return build_reinterpret_cast
08429 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08430 RECUR (TREE_OPERAND (t, 0)));
08431
08432 case CONST_CAST_EXPR:
08433 return build_const_cast
08434 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08435 RECUR (TREE_OPERAND (t, 0)));
08436
08437 case DYNAMIC_CAST_EXPR:
08438 return build_dynamic_cast
08439 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08440 RECUR (TREE_OPERAND (t, 0)));
08441
08442 case STATIC_CAST_EXPR:
08443 return build_static_cast
08444 (tsubst (TREE_TYPE (t), args, complain, in_decl),
08445 RECUR (TREE_OPERAND (t, 0)));
08446
08447 case POSTDECREMENT_EXPR:
08448 case POSTINCREMENT_EXPR:
08449 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
08450 args, complain, in_decl);
08451 return build_x_unary_op (TREE_CODE (t), op1);
08452
08453 case PREDECREMENT_EXPR:
08454 case PREINCREMENT_EXPR:
08455 case NEGATE_EXPR:
08456 case BIT_NOT_EXPR:
08457 case ABS_EXPR:
08458 case TRUTH_NOT_EXPR:
08459 case CONVERT_EXPR:
08460 case REALPART_EXPR:
08461 case IMAGPART_EXPR:
08462 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
08463
08464 case ADDR_EXPR:
08465 op1 = TREE_OPERAND (t, 0);
08466 if (TREE_CODE (op1) == SCOPE_REF)
08467 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
08468 true, true);
08469 else
08470 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
08471 in_decl);
08472 if (TREE_CODE (op1) == LABEL_DECL)
08473 return finish_label_address_expr (DECL_NAME (op1));
08474 return build_x_unary_op (ADDR_EXPR, op1);
08475
08476 case PLUS_EXPR:
08477 case MINUS_EXPR:
08478 case MULT_EXPR:
08479 case TRUNC_DIV_EXPR:
08480 case CEIL_DIV_EXPR:
08481 case FLOOR_DIV_EXPR:
08482 case ROUND_DIV_EXPR:
08483 case EXACT_DIV_EXPR:
08484 case BIT_AND_EXPR:
08485 case BIT_IOR_EXPR:
08486 case BIT_XOR_EXPR:
08487 case TRUNC_MOD_EXPR:
08488 case FLOOR_MOD_EXPR:
08489 case TRUTH_ANDIF_EXPR:
08490 case TRUTH_ORIF_EXPR:
08491 case TRUTH_AND_EXPR:
08492 case TRUTH_OR_EXPR:
08493 case RSHIFT_EXPR:
08494 case LSHIFT_EXPR:
08495 case RROTATE_EXPR:
08496 case LROTATE_EXPR:
08497 case EQ_EXPR:
08498 case NE_EXPR:
08499 case MAX_EXPR:
08500 case MIN_EXPR:
08501 case LE_EXPR:
08502 case GE_EXPR:
08503 case LT_EXPR:
08504 case GT_EXPR:
08505 case MEMBER_REF:
08506 case DOTSTAR_EXPR:
08507 return build_x_binary_op
08508 (TREE_CODE (t),
08509 RECUR (TREE_OPERAND (t, 0)),
08510 RECUR (TREE_OPERAND (t, 1)),
08511 NULL);
08512
08513 case SCOPE_REF:
08514 return tsubst_qualified_id (t, args, complain, in_decl, true,
08515 false);
08516 case ARRAY_REF:
08517 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
08518 args, complain, in_decl);
08519 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
08520 NULL);
08521
08522 case SIZEOF_EXPR:
08523 case ALIGNOF_EXPR:
08524 op1 = TREE_OPERAND (t, 0);
08525 if (!args)
08526 {
08527
08528
08529
08530 if (!TYPE_P (op1))
08531 op1 = TREE_TYPE (op1);
08532 }
08533 else
08534 {
08535 ++skip_evaluation;
08536 op1 = RECUR (op1);
08537 --skip_evaluation;
08538 }
08539 if (TYPE_P (op1))
08540 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
08541 else
08542 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
08543
08544 case MODOP_EXPR:
08545 {
08546 tree r = build_x_modify_expr
08547 (RECUR (TREE_OPERAND (t, 0)),
08548 TREE_CODE (TREE_OPERAND (t, 1)),
08549 RECUR (TREE_OPERAND (t, 2)));
08550
08551
08552
08553
08554
08555
08556 if (TREE_NO_WARNING (t))
08557 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
08558 return r;
08559 }
08560
08561 case ARROW_EXPR:
08562 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
08563 args, complain, in_decl);
08564
08565 if (DECL_P (op1))
08566 mark_used (op1);
08567 return build_x_arrow (op1);
08568
08569 case NEW_EXPR:
08570 return build_new
08571 (RECUR (TREE_OPERAND (t, 0)),
08572 RECUR (TREE_OPERAND (t, 1)),
08573 RECUR (TREE_OPERAND (t, 2)),
08574 RECUR (TREE_OPERAND (t, 3)),
08575 NEW_EXPR_USE_GLOBAL (t));
08576
08577 case DELETE_EXPR:
08578 return delete_sanity
08579 (RECUR (TREE_OPERAND (t, 0)),
08580 RECUR (TREE_OPERAND (t, 1)),
08581 DELETE_EXPR_USE_VEC (t),
08582 DELETE_EXPR_USE_GLOBAL (t));
08583
08584 case COMPOUND_EXPR:
08585 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
08586 RECUR (TREE_OPERAND (t, 1)));
08587
08588 case CALL_EXPR:
08589 {
08590 tree function;
08591 tree call_args;
08592 bool qualified_p;
08593 bool koenig_p;
08594
08595 function = TREE_OPERAND (t, 0);
08596
08597
08598 koenig_p = KOENIG_LOOKUP_P (t);
08599 if (TREE_CODE (function) == SCOPE_REF)
08600 {
08601 qualified_p = true;
08602 function = tsubst_qualified_id (function, args, complain, in_decl,
08603 false,
08604 false);
08605 }
08606 else
08607 {
08608 qualified_p = (TREE_CODE (function) == COMPONENT_REF
08609 && (TREE_CODE (TREE_OPERAND (function, 1))
08610 == SCOPE_REF));
08611 function = tsubst_copy_and_build (function, args, complain,
08612 in_decl,
08613 !qualified_p);
08614 if (BASELINK_P (function))
08615 qualified_p = true;
08616 }
08617
08618 call_args = RECUR (TREE_OPERAND (t, 1));
08619
08620
08621
08622
08623 if (koenig_p
08624 && ((is_overloaded_fn (function)
08625
08626
08627
08628 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
08629 || TREE_CODE (function) == IDENTIFIER_NODE))
08630 function = perform_koenig_lookup (function, call_args);
08631
08632 if (TREE_CODE (function) == IDENTIFIER_NODE)
08633 {
08634 unqualified_name_lookup_error (function);
08635 return error_mark_node;
08636 }
08637
08638
08639 if (DECL_P (function))
08640 mark_used (function);
08641
08642 if (TREE_CODE (function) == OFFSET_REF)
08643 return build_offset_ref_call_from_tree (function, call_args);
08644 if (TREE_CODE (function) == COMPONENT_REF)
08645 {
08646 if (!BASELINK_P (TREE_OPERAND (function, 1)))
08647 return finish_call_expr (function, call_args,
08648 false,
08649 false);
08650 else
08651 return (build_new_method_call
08652 (TREE_OPERAND (function, 0),
08653 TREE_OPERAND (function, 1),
08654 call_args, NULL_TREE,
08655 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
08656 }
08657 return finish_call_expr (function, call_args,
08658 qualified_p,
08659 koenig_p);
08660 }
08661
08662 case COND_EXPR:
08663 return build_x_conditional_expr
08664 (RECUR (TREE_OPERAND (t, 0)),
08665 RECUR (TREE_OPERAND (t, 1)),
08666 RECUR (TREE_OPERAND (t, 2)));
08667
08668 case PSEUDO_DTOR_EXPR:
08669 return finish_pseudo_destructor_expr
08670 (RECUR (TREE_OPERAND (t, 0)),
08671 RECUR (TREE_OPERAND (t, 1)),
08672 RECUR (TREE_OPERAND (t, 2)));
08673
08674 case TREE_LIST:
08675 {
08676 tree purpose, value, chain;
08677
08678 if (t == void_list_node)
08679 return t;
08680
08681 purpose = TREE_PURPOSE (t);
08682 if (purpose)
08683 purpose = RECUR (purpose);
08684 value = TREE_VALUE (t);
08685 if (value)
08686 value = RECUR (value);
08687 chain = TREE_CHAIN (t);
08688 if (chain && chain != void_type_node)
08689 chain = RECUR (chain);
08690 if (purpose == TREE_PURPOSE (t)
08691 && value == TREE_VALUE (t)
08692 && chain == TREE_CHAIN (t))
08693 return t;
08694 return tree_cons (purpose, value, chain);
08695 }
08696
08697 case COMPONENT_REF:
08698 {
08699 tree object;
08700 tree member;
08701
08702 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
08703 args, complain, in_decl);
08704
08705 if (DECL_P (object))
08706 mark_used (object);
08707
08708 member = TREE_OPERAND (t, 1);
08709 if (BASELINK_P (member))
08710 member = tsubst_baselink (member,
08711 non_reference (TREE_TYPE (object)),
08712 args, complain, in_decl);
08713 else
08714 member = tsubst_copy (member, args, complain, in_decl);
08715
08716 if (member == error_mark_node)
08717 return error_mark_node;
08718 else if (!CLASS_TYPE_P (TREE_TYPE (object)))
08719 {
08720 if (TREE_CODE (member) == BIT_NOT_EXPR)
08721 return finish_pseudo_destructor_expr (object,
08722 NULL_TREE,
08723 TREE_TYPE (object));
08724 else if (TREE_CODE (member) == SCOPE_REF
08725 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
08726 return finish_pseudo_destructor_expr (object,
08727 object,
08728 TREE_TYPE (object));
08729 }
08730 else if (TREE_CODE (member) == SCOPE_REF
08731 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
08732 {
08733 tree tmpl;
08734 tree args;
08735
08736
08737
08738 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
08739 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
08740 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
08741 false,
08742 false);
08743 if (BASELINK_P (member))
08744 {
08745 BASELINK_FUNCTIONS (member)
08746 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
08747 args);
08748 member = (adjust_result_of_qualified_name_lookup
08749 (member, BINFO_TYPE (BASELINK_BINFO (member)),
08750 TREE_TYPE (object)));
08751 }
08752 else
08753 {
08754 qualified_name_lookup_error (TREE_TYPE (object), tmpl,
08755 member);
08756 return error_mark_node;
08757 }
08758 }
08759 else if (TREE_CODE (member) == SCOPE_REF
08760 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
08761 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
08762 {
08763 if (complain & tf_error)
08764 {
08765 if (TYPE_P (TREE_OPERAND (member, 0)))
08766 error ("%qT is not a class or namespace",
08767 TREE_OPERAND (member, 0));
08768 else
08769 error ("%qD is not a class or namespace",
08770 TREE_OPERAND (member, 0));
08771 }
08772 return error_mark_node;
08773 }
08774 else if (TREE_CODE (member) == FIELD_DECL)
08775 return finish_non_static_data_member (member, object, NULL_TREE);
08776
08777 return finish_class_member_access_expr (object, member);
08778 }
08779
08780 case THROW_EXPR:
08781 return build_throw
08782 (RECUR (TREE_OPERAND (t, 0)));
08783
08784 case CONSTRUCTOR:
08785 {
08786 tree r;
08787 tree elts;
08788 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
08789 bool purpose_p;
08790
08791
08792 if (type && TYPE_PTRMEMFUNC_P (type))
08793 return t;
08794
08795 r = NULL_TREE;
08796
08797
08798
08799 purpose_p = !(type && IS_AGGR_TYPE (type));
08800 for (elts = CONSTRUCTOR_ELTS (t);
08801 elts;
08802 elts = TREE_CHAIN (elts))
08803 {
08804 tree purpose = TREE_PURPOSE (elts);
08805 tree value = TREE_VALUE (elts);
08806
08807 if (purpose && purpose_p)
08808 purpose = RECUR (purpose);
08809 value = RECUR (value);
08810 r = tree_cons (purpose, value, r);
08811 }
08812
08813 r = build_constructor (NULL_TREE, nreverse (r));
08814 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
08815
08816 if (type)
08817 return digest_init (type, r, 0);
08818 return r;
08819 }
08820
08821 case TYPEID_EXPR:
08822 {
08823 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
08824 if (TYPE_P (operand_0))
08825 return get_typeid (operand_0);
08826 return build_typeid (operand_0);
08827 }
08828
08829 case VAR_DECL:
08830 if (!args)
08831 return t;
08832
08833
08834 case PARM_DECL:
08835 {
08836 tree r = tsubst_copy (t, args, complain, in_decl);
08837
08838 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
08839
08840
08841 r = convert_from_reference (r);
08842 return r;
08843 }
08844
08845 case VA_ARG_EXPR:
08846 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
08847 tsubst_copy (TREE_TYPE (t), args, complain,
08848 in_decl));
08849
08850 case OFFSETOF_EXPR:
08851 return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
08852
08853 case STMT_EXPR:
08854 {
08855 tree old_stmt_expr = cur_stmt_expr;
08856 tree stmt_expr = begin_stmt_expr ();
08857
08858 cur_stmt_expr = stmt_expr;
08859 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
08860 stmt_expr = finish_stmt_expr (stmt_expr, false);
08861 cur_stmt_expr = old_stmt_expr;
08862
08863 return stmt_expr;
08864 }
08865
08866 case CONST_DECL:
08867 t = tsubst_copy (t, args, complain, in_decl);
08868
08869
08870 if (TREE_CODE (t) == CONST_DECL)
08871 return DECL_INITIAL (t);
08872 return t;
08873
08874 default:
08875 return tsubst_copy (t, args, complain, in_decl);
08876 }
08877
08878 #undef RECUR
08879 }
08880
08881
08882
08883
08884
08885
08886 static bool
08887 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
08888 {
08889 int ix, len = DECL_NTPARMS (tmpl);
08890 bool result = false;
08891
08892 for (ix = 0; ix != len; ix++)
08893 {
08894 tree t = TREE_VEC_ELT (args, ix);
08895
08896 if (TYPE_P (t))
08897 {
08898
08899
08900
08901
08902
08903 tree nt = no_linkage_check (t, false);
08904
08905 if (nt)
08906 {
08907
08908
08909 if (complain & tf_error)
08910 {
08911 if (TYPE_ANONYMOUS_P (nt))
08912 error ("%qT is/uses anonymous type", t);
08913 else
08914 error ("%qT uses local type %qT", t, nt);
08915 }
08916 result = true;
08917 }
08918
08919
08920 else if (variably_modified_type_p (t, NULL_TREE))
08921 {
08922 if (complain & tf_error)
08923 error ("%qT is a variably modified type", t);
08924 result = true;
08925 }
08926 }
08927
08928
08929 else if (TREE_TYPE (t)
08930 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
08931 && !TREE_CONSTANT (t))
08932 {
08933 if (complain & tf_error)
08934 error ("integral expression %qE is not constant", t);
08935 result = true;
08936 }
08937 }
08938 if (result && (complain & tf_error))
08939 error (" trying to instantiate %qD", tmpl);
08940 return result;
08941 }
08942
08943
08944
08945
08946 tree
08947 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
08948 {
08949 tree fndecl;
08950 tree gen_tmpl;
08951 tree spec;
08952
08953 if (tmpl == error_mark_node)
08954 return error_mark_node;
08955
08956 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
08957
08958
08959 if (DECL_CLONED_FUNCTION_P (tmpl))
08960 {
08961 tree spec;
08962 tree clone;
08963
08964 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
08965 complain);
08966 if (spec == error_mark_node)
08967 return error_mark_node;
08968
08969
08970 FOR_EACH_CLONE (clone, spec)
08971 if (DECL_NAME (clone) == DECL_NAME (tmpl))
08972 return clone;
08973
08974 gcc_unreachable ();
08975 return NULL_TREE;
08976 }
08977
08978
08979 spec = retrieve_specialization (tmpl, targ_ptr,
08980 false);
08981 if (spec != NULL_TREE)
08982 return spec;
08983
08984 gen_tmpl = most_general_template (tmpl);
08985 if (tmpl != gen_tmpl)
08986 {
08987
08988
08989
08990 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
08991 targ_ptr);
08992
08993
08994 spec = retrieve_specialization (gen_tmpl, targ_ptr,
08995 false);
08996 if (spec != NULL_TREE)
08997 return spec;
08998 }
08999
09000 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
09001 complain))
09002 return error_mark_node;
09003
09004
09005
09006
09007
09008
09009 push_deferring_access_checks (dk_deferred);
09010
09011
09012 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
09013 targ_ptr, complain, gen_tmpl);
09014
09015
09016
09017 push_access_scope (fndecl);
09018 perform_deferred_access_checks ();
09019 pop_access_scope (fndecl);
09020 pop_deferring_access_checks ();
09021
09022
09023
09024 DECL_TI_TEMPLATE (fndecl) = tmpl;
09025
09026
09027
09028
09029
09030 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
09031 clone_function_decl (fndecl, 0);
09032
09033 return fndecl;
09034 }
09035
09036
09037
09038
09039
09040
09041
09042
09043
09044
09045
09046
09047
09048
09049
09050
09051
09052
09053
09054
09055
09056
09057
09058
09059
09060
09061
09062
09063
09064
09065
09066
09067
09068
09069
09070
09071
09072 int
09073 fn_type_unification (tree fn,
09074 tree explicit_targs,
09075 tree targs,
09076 tree args,
09077 tree return_type,
09078 unification_kind_t strict,
09079 int len)
09080 {
09081 tree parms;
09082 tree fntype;
09083 int result;
09084
09085 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
09086
09087 fntype = TREE_TYPE (fn);
09088 if (explicit_targs)
09089 {
09090
09091
09092
09093
09094
09095
09096
09097
09098
09099
09100
09101
09102
09103
09104
09105
09106
09107 int i;
09108 tree converted_args;
09109 bool incomplete;
09110
09111 if (explicit_targs == error_mark_node)
09112 return 1;
09113
09114 converted_args
09115 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
09116 explicit_targs, NULL_TREE, tf_none,
09117 0));
09118 if (converted_args == error_mark_node)
09119 return 1;
09120
09121
09122
09123
09124
09125
09126
09127 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
09128 processing_template_decl += incomplete;
09129 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
09130 processing_template_decl -= incomplete;
09131
09132 if (fntype == error_mark_node)
09133 return 1;
09134
09135
09136 for (i = NUM_TMPL_ARGS (converted_args); i--;)
09137 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
09138 }
09139
09140 parms = TYPE_ARG_TYPES (fntype);
09141
09142 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
09143 parms = TREE_CHAIN (parms);
09144
09145 if (return_type)
09146 {
09147
09148 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
09149 args = tree_cons (NULL_TREE, return_type, args);
09150 if (len >= 0)
09151 ++len;
09152 }
09153
09154
09155
09156
09157
09158 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
09159 targs, parms, args, 0,
09160 strict, 1, len);
09161
09162 if (result == 0)
09163
09164
09165
09166
09167
09168
09169
09170
09171
09172 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
09173 == error_mark_node)
09174 return 1;
09175
09176 return result;
09177 }
09178
09179
09180
09181
09182
09183
09184
09185
09186 static int
09187 maybe_adjust_types_for_deduction (unification_kind_t strict,
09188 tree* parm,
09189 tree* arg)
09190 {
09191 int result = 0;
09192
09193 switch (strict)
09194 {
09195 case DEDUCE_CALL:
09196 break;
09197
09198 case DEDUCE_CONV:
09199 {
09200
09201
09202
09203 tree* temp = parm;
09204 parm = arg;
09205 arg = temp;
09206 break;
09207 }
09208
09209 case DEDUCE_EXACT:
09210
09211 return 0;
09212
09213 default:
09214 gcc_unreachable ();
09215 }
09216
09217 if (TREE_CODE (*parm) != REFERENCE_TYPE)
09218 {
09219
09220
09221
09222
09223
09224
09225
09226
09227
09228
09229
09230
09231
09232
09233
09234
09235 if (TREE_CODE (*arg) == ARRAY_TYPE)
09236 *arg = build_pointer_type (TREE_TYPE (*arg));
09237 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
09238 *arg = build_pointer_type (*arg);
09239 else
09240 *arg = TYPE_MAIN_VARIANT (*arg);
09241 }
09242
09243
09244
09245
09246
09247
09248
09249 *parm = TYPE_MAIN_VARIANT (*parm);
09250 if (TREE_CODE (*parm) == REFERENCE_TYPE)
09251 {
09252 *parm = TREE_TYPE (*parm);
09253 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
09254 }
09255
09256
09257
09258 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
09259 *arg = TREE_TYPE (*arg);
09260
09261 return result;
09262 }
09263
09264
09265
09266
09267
09268
09269
09270 static int
09271 type_unification_real (tree tparms,
09272 tree targs,
09273 tree xparms,
09274 tree xargs,
09275 int subr,
09276 unification_kind_t strict,
09277 int allow_incomplete,
09278 int xlen)
09279 {
09280 tree parm, arg;
09281 int i;
09282 int ntparms = TREE_VEC_LENGTH (tparms);
09283 int sub_strict;
09284 int saw_undeduced = 0;
09285 tree parms, args;
09286 int len;
09287
09288 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
09289 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
09290 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
09291 gcc_assert (ntparms > 0);
09292
09293 switch (strict)
09294 {
09295 case DEDUCE_CALL:
09296 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
09297 | UNIFY_ALLOW_DERIVED);
09298 break;
09299
09300 case DEDUCE_CONV:
09301 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
09302 break;
09303
09304 case DEDUCE_EXACT:
09305 sub_strict = UNIFY_ALLOW_NONE;
09306 break;
09307
09308 default:
09309 gcc_unreachable ();
09310 }
09311
09312 if (xlen == 0)
09313 return 0;
09314
09315 again:
09316 parms = xparms;
09317 args = xargs;
09318 len = xlen;
09319
09320 while (parms
09321 && parms != void_list_node
09322 && args
09323 && args != void_list_node)
09324 {
09325 parm = TREE_VALUE (parms);
09326 parms = TREE_CHAIN (parms);
09327 arg = TREE_VALUE (args);
09328 args = TREE_CHAIN (args);
09329
09330 if (arg == error_mark_node)
09331 return 1;
09332 if (arg == unknown_type_node)
09333
09334
09335 continue;
09336
09337
09338
09339
09340
09341 if (!uses_template_parms (parm))
09342 {
09343 tree type;
09344
09345 if (!TYPE_P (arg))
09346 type = TREE_TYPE (arg);
09347 else
09348 type = arg;
09349
09350 if (strict == DEDUCE_EXACT)
09351 {
09352 if (same_type_p (parm, type))
09353 continue;
09354 }
09355 else
09356
09357
09358
09359 continue;
09360
09361 return 1;
09362 }
09363
09364 if (!TYPE_P (arg))
09365 {
09366 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
09367 if (type_unknown_p (arg))
09368 {
09369
09370
09371
09372
09373
09374
09375 if (resolve_overloaded_unification
09376 (tparms, targs, parm, arg, strict, sub_strict)
09377 != 0)
09378 return 1;
09379 continue;
09380 }
09381 arg = TREE_TYPE (arg);
09382 if (arg == error_mark_node)
09383 return 1;
09384 }
09385
09386 {
09387 int arg_strict = sub_strict;
09388
09389 if (!subr)
09390 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
09391
09392 if (unify (tparms, targs, parm, arg, arg_strict))
09393 return 1;
09394 }
09395
09396
09397 if (--len == 0)
09398 goto done;
09399 }
09400
09401
09402 if (args && args != void_list_node && parms == void_list_node)
09403 return 1;
09404
09405 if (parms
09406 && parms != void_list_node
09407 && TREE_PURPOSE (parms) == NULL_TREE)
09408 return 1;
09409
09410 done:
09411 if (!subr)
09412 for (i = 0; i < ntparms; i++)
09413 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
09414 {
09415 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
09416
09417
09418
09419
09420
09421 if (TREE_CODE (tparm) == PARM_DECL
09422 && uses_template_parms (TREE_TYPE (tparm))
09423 && !saw_undeduced++)
09424 goto again;
09425
09426 if (!allow_incomplete)
09427 error ("incomplete type unification");
09428 return 2;
09429 }
09430 return 0;
09431 }
09432
09433
09434
09435
09436
09437
09438 static int
09439 resolve_overloaded_unification (tree tparms,
09440 tree targs,
09441 tree parm,
09442 tree arg,
09443 unification_kind_t strict,
09444 int sub_strict)
09445 {
09446 tree tempargs = copy_node (targs);
09447 int good = 0;
09448 bool addr_p;
09449
09450 if (TREE_CODE (arg) == ADDR_EXPR)
09451 {
09452 arg = TREE_OPERAND (arg, 0);
09453 addr_p = true;
09454 }
09455 else
09456 addr_p = false;
09457
09458 if (TREE_CODE (arg) == COMPONENT_REF)
09459
09460
09461 arg = TREE_OPERAND (arg, 1);
09462
09463 if (TREE_CODE (arg) == OFFSET_REF)
09464 arg = TREE_OPERAND (arg, 1);
09465
09466
09467 if (BASELINK_P (arg))
09468 arg = BASELINK_FUNCTIONS (arg);
09469
09470 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
09471 {
09472
09473
09474
09475
09476 tree expl_subargs = TREE_OPERAND (arg, 1);
09477 arg = TREE_OPERAND (arg, 0);
09478
09479 for (; arg; arg = OVL_NEXT (arg))
09480 {
09481 tree fn = OVL_CURRENT (arg);
09482 tree subargs, elem;
09483
09484 if (TREE_CODE (fn) != TEMPLATE_DECL)
09485 continue;
09486
09487 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
09488 expl_subargs);
09489 if (subargs)
09490 {
09491 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
09492 good += try_one_overload (tparms, targs, tempargs, parm,
09493 elem, strict, sub_strict, addr_p);
09494 }
09495 }
09496 }
09497 else
09498 {
09499 gcc_assert (TREE_CODE (arg) == OVERLOAD
09500 || TREE_CODE (arg) == FUNCTION_DECL);
09501
09502 for (; arg; arg = OVL_NEXT (arg))
09503 good += try_one_overload (tparms, targs, tempargs, parm,
09504 TREE_TYPE (OVL_CURRENT (arg)),
09505 strict, sub_strict, addr_p);
09506 }
09507
09508
09509
09510
09511
09512
09513
09514
09515
09516 if (good == 1)
09517 {
09518 int i = TREE_VEC_LENGTH (targs);
09519 for (; i--; )
09520 if (TREE_VEC_ELT (tempargs, i))
09521 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
09522 }
09523 if (good)
09524 return 0;
09525
09526 return 1;
09527 }
09528
09529
09530
09531
09532
09533
09534
09535
09536
09537 static int
09538 try_one_overload (tree tparms,
09539 tree orig_targs,
09540 tree targs,
09541 tree parm,
09542 tree arg,
09543 unification_kind_t strict,
09544 int sub_strict,
09545 bool addr_p)
09546 {
09547 int nargs;
09548 tree tempargs;
09549 int i;
09550
09551
09552
09553
09554
09555
09556
09557
09558 if (uses_template_parms (arg))
09559 return 1;
09560
09561 if (TREE_CODE (arg) == METHOD_TYPE)
09562 arg = build_ptrmemfunc_type (build_pointer_type (arg));
09563 else if (addr_p)
09564 arg = build_pointer_type (arg);
09565
09566 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
09567
09568
09569
09570
09571
09572 nargs = TREE_VEC_LENGTH (targs);
09573 tempargs = make_tree_vec (nargs);
09574
09575 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
09576 return 0;
09577
09578
09579
09580 for (i = nargs; i--; )
09581 {
09582 tree elt = TREE_VEC_ELT (tempargs, i);
09583 tree oldelt = TREE_VEC_ELT (orig_targs, i);
09584
09585 if (elt == NULL_TREE)
09586 continue;
09587 else if (uses_template_parms (elt))
09588 {
09589
09590
09591
09592 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
09593 continue;
09594 }
09595 else if (oldelt && ! template_args_equal (oldelt, elt))
09596 return 0;
09597 }
09598
09599 for (i = nargs; i--; )
09600 {
09601 tree elt = TREE_VEC_ELT (tempargs, i);
09602
09603 if (elt)
09604 TREE_VEC_ELT (targs, i) = elt;
09605 }
09606
09607 return 1;
09608 }
09609
09610
09611
09612
09613
09614
09615
09616
09617
09618
09619
09620
09621
09622
09623
09624
09625
09626
09627 static int
09628 verify_class_unification (tree targs, tree parms, tree args)
09629 {
09630 parms = tsubst (parms, add_outermost_template_args (args, targs),
09631 tf_none, NULL_TREE);
09632 if (parms == error_mark_node)
09633 return 1;
09634
09635 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
09636 }
09637
09638
09639
09640
09641
09642
09643 static tree
09644 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
09645 {
09646 tree copy_of_targs;
09647
09648 if (!CLASSTYPE_TEMPLATE_INFO (arg)
09649 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
09650 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
09651 return NULL_TREE;
09652
09653
09654
09655
09656
09657
09658
09659
09660
09661
09662
09663
09664
09665
09666
09667
09668
09669
09670
09671
09672
09673
09674
09675
09676
09677
09678
09679
09680
09681
09682
09683 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
09684
09685
09686 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
09687 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
09688 return NULL_TREE;
09689
09690 return arg;
09691 }
09692
09693
09694
09695
09696
09697
09698
09699
09700 static tree
09701 get_template_base (tree tparms, tree targs, tree parm, tree arg)
09702 {
09703 tree rval = NULL_TREE;
09704 tree binfo;
09705
09706 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
09707
09708 binfo = TYPE_BINFO (complete_type (arg));
09709 if (!binfo)
09710
09711 return NULL_TREE;
09712
09713
09714
09715 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
09716 {
09717 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
09718
09719 if (r)
09720 {
09721
09722
09723
09724
09725
09726
09727
09728
09729 if (rval && !same_type_p (r, rval))
09730 return NULL_TREE;
09731
09732 rval = r;
09733 }
09734 }
09735
09736 return rval;
09737 }
09738
09739
09740
09741 static int
09742 template_decl_level (tree decl)
09743 {
09744 switch (TREE_CODE (decl))
09745 {
09746 case TYPE_DECL:
09747 case TEMPLATE_DECL:
09748 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
09749
09750 case PARM_DECL:
09751 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
09752
09753 default:
09754 gcc_unreachable ();
09755 }
09756 return 0;
09757 }
09758
09759
09760
09761
09762
09763 static int
09764 check_cv_quals_for_unify (int strict, tree arg, tree parm)
09765 {
09766 int arg_quals = cp_type_quals (arg);
09767 int parm_quals = cp_type_quals (parm);
09768
09769 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
09770 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
09771 {
09772
09773
09774
09775
09776
09777
09778 if ((TREE_CODE (arg) == REFERENCE_TYPE
09779 || TREE_CODE (arg) == FUNCTION_TYPE
09780 || TREE_CODE (arg) == METHOD_TYPE)
09781 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
09782 return 0;
09783
09784 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
09785 && (parm_quals & TYPE_QUAL_RESTRICT))
09786 return 0;
09787 }
09788
09789 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
09790 && (arg_quals & parm_quals) != parm_quals)
09791 return 0;
09792
09793 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
09794 && (parm_quals & arg_quals) != arg_quals)
09795 return 0;
09796
09797 return 1;
09798 }
09799
09800
09801
09802
09803
09804
09805
09806
09807
09808
09809
09810
09811
09812
09813
09814
09815
09816
09817
09818
09819
09820
09821
09822
09823
09824
09825
09826
09827
09828
09829
09830
09831
09832 static int
09833 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
09834 {
09835 int idx;
09836 tree targ;
09837 tree tparm;
09838 int strict_in = strict;
09839
09840
09841
09842
09843
09844 while (TREE_CODE (parm) == NOP_EXPR)
09845 parm = TREE_OPERAND (parm, 0);
09846
09847 if (arg == error_mark_node)
09848 return 1;
09849 if (arg == unknown_type_node)
09850
09851
09852 return 0;
09853
09854
09855
09856
09857
09858 if (arg == parm && !uses_template_parms (parm))
09859 return 0;
09860
09861
09862
09863 if (TREE_CODE (arg) == TREE_CODE (parm)
09864 && TYPE_P (arg)
09865
09866
09867
09868 && TREE_CODE (arg) != ARRAY_TYPE
09869
09870
09871
09872
09873 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
09874 && !check_cv_quals_for_unify (strict_in, arg, parm))
09875 return 1;
09876
09877 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
09878 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
09879 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
09880 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
09881 strict &= ~UNIFY_ALLOW_DERIVED;
09882 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
09883 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
09884
09885 switch (TREE_CODE (parm))
09886 {
09887 case TYPENAME_TYPE:
09888 case SCOPE_REF:
09889 case UNBOUND_CLASS_TEMPLATE:
09890
09891
09892
09893 return 0;
09894
09895 case TEMPLATE_TYPE_PARM:
09896 case TEMPLATE_TEMPLATE_PARM:
09897 case BOUND_TEMPLATE_TEMPLATE_PARM:
09898 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
09899
09900 if (TEMPLATE_TYPE_LEVEL (parm)
09901 != template_decl_level (tparm))
09902
09903
09904 return (TREE_CODE (arg) == TREE_CODE (parm)
09905 && same_type_p (parm, arg)) ? 0 : 1;
09906 idx = TEMPLATE_TYPE_IDX (parm);
09907 targ = TREE_VEC_ELT (targs, idx);
09908 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
09909
09910
09911 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
09912 && TREE_CODE (tparm) != TYPE_DECL)
09913 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
09914 && TREE_CODE (tparm) != TEMPLATE_DECL))
09915 return 1;
09916
09917 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
09918 {
09919
09920
09921 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
09922 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
09923 return 1;
09924
09925 {
09926 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
09927 tree parmvec = TYPE_TI_ARGS (parm);
09928 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
09929 tree argtmplvec
09930 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
09931 int i;
09932
09933
09934
09935
09936
09937
09938
09939
09940 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
09941 == error_mark_node)
09942 return 1;
09943
09944
09945
09946
09947
09948
09949 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
09950 {
09951 if (unify (tparms, targs,
09952 TREE_VEC_ELT (parmvec, i),
09953 TREE_VEC_ELT (argvec, i),
09954 UNIFY_ALLOW_NONE))
09955 return 1;
09956 }
09957 }
09958 arg = TYPE_TI_TEMPLATE (arg);
09959
09960
09961 }
09962
09963 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
09964 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
09965 {
09966
09967
09968
09969 if (targ != NULL_TREE && template_args_equal (targ, arg))
09970 return 0;
09971 else if (targ)
09972 return 1;
09973 }
09974 else
09975 {
09976
09977
09978
09979
09980 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
09981 arg, parm))
09982 return 1;
09983
09984
09985
09986 arg = cp_build_qualified_type_real
09987 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
09988 if (arg == error_mark_node)
09989 return 1;
09990
09991
09992 if (targ != NULL_TREE && same_type_p (targ, arg))
09993 return 0;
09994 else if (targ)
09995 return 1;
09996
09997
09998
09999
10000
10001
10002
10003
10004 if (variably_modified_type_p (arg, NULL_TREE))
10005 return 1;
10006 }
10007
10008 TREE_VEC_ELT (targs, idx) = arg;
10009 return 0;
10010
10011 case TEMPLATE_PARM_INDEX:
10012 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10013
10014 if (TEMPLATE_PARM_LEVEL (parm)
10015 != template_decl_level (tparm))
10016
10017
10018 return !(TREE_CODE (arg) == TREE_CODE (parm)
10019 && cp_tree_equal (parm, arg));
10020
10021 idx = TEMPLATE_PARM_IDX (parm);
10022 targ = TREE_VEC_ELT (targs, idx);
10023
10024 if (targ)
10025 return !cp_tree_equal (targ, arg);
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10036 if (!TREE_TYPE (arg))
10037
10038
10039 ;
10040 else if (same_type_p (TREE_TYPE (arg), tparm))
10041 ;
10042 else if ((strict & UNIFY_ALLOW_INTEGER)
10043 && (TREE_CODE (tparm) == INTEGER_TYPE
10044 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10045
10046
10047
10048 arg = fold (build_nop (TREE_TYPE (parm), arg));
10049 else if (uses_template_parms (tparm))
10050
10051
10052 return 0;
10053 else
10054 return 1;
10055
10056 TREE_VEC_ELT (targs, idx) = arg;
10057 return 0;
10058
10059 case PTRMEM_CST:
10060 {
10061
10062
10063 if (TREE_CODE (arg) != PTRMEM_CST)
10064 return 1;
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10077 PTRMEM_CST_MEMBER (arg), strict);
10078 }
10079
10080 case POINTER_TYPE:
10081 {
10082 if (TREE_CODE (arg) != POINTER_TYPE)
10083 return 1;
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10096
10097
10098 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10099
10100 return unify (tparms, targs, TREE_TYPE (parm),
10101 TREE_TYPE (arg), strict);
10102 }
10103
10104 case REFERENCE_TYPE:
10105 if (TREE_CODE (arg) != REFERENCE_TYPE)
10106 return 1;
10107 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10108 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10109
10110 case ARRAY_TYPE:
10111 if (TREE_CODE (arg) != ARRAY_TYPE)
10112 return 1;
10113 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10114 != (TYPE_DOMAIN (arg) == NULL_TREE))
10115 return 1;
10116 if (TYPE_DOMAIN (parm) != NULL_TREE)
10117 {
10118 tree parm_max;
10119 tree arg_max;
10120
10121 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10122 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10123
10124
10125
10126
10127 if (TREE_CODE (parm_max) == MINUS_EXPR)
10128 {
10129 arg_max = fold (build2 (PLUS_EXPR,
10130 integer_type_node,
10131 arg_max,
10132 TREE_OPERAND (parm_max, 1)));
10133 parm_max = TREE_OPERAND (parm_max, 0);
10134 }
10135
10136 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10137 return 1;
10138 }
10139 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10140 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10141
10142 case REAL_TYPE:
10143 case COMPLEX_TYPE:
10144 case VECTOR_TYPE:
10145 case INTEGER_TYPE:
10146 case BOOLEAN_TYPE:
10147 case ENUMERAL_TYPE:
10148 case VOID_TYPE:
10149 if (TREE_CODE (arg) != TREE_CODE (parm))
10150 return 1;
10151
10152
10153
10154 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10155 return 1;
10156
10157
10158
10159 return 0;
10160
10161
10162
10163 case INTEGER_CST:
10164 while (TREE_CODE (arg) == NOP_EXPR)
10165 arg = TREE_OPERAND (arg, 0);
10166
10167 if (TREE_CODE (arg) != INTEGER_CST)
10168 return 1;
10169 return !tree_int_cst_equal (parm, arg);
10170
10171 case TREE_VEC:
10172 {
10173 int i;
10174 if (TREE_CODE (arg) != TREE_VEC)
10175 return 1;
10176 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10177 return 1;
10178 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10179 if (unify (tparms, targs,
10180 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10181 UNIFY_ALLOW_NONE))
10182 return 1;
10183 return 0;
10184 }
10185
10186 case RECORD_TYPE:
10187 case UNION_TYPE:
10188 if (TREE_CODE (arg) != TREE_CODE (parm))
10189 return 1;
10190
10191 if (TYPE_PTRMEMFUNC_P (parm))
10192 {
10193 if (!TYPE_PTRMEMFUNC_P (arg))
10194 return 1;
10195
10196 return unify (tparms, targs,
10197 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10198 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10199 strict);
10200 }
10201
10202 if (CLASSTYPE_TEMPLATE_INFO (parm))
10203 {
10204 tree t = NULL_TREE;
10205
10206 if (strict_in & UNIFY_ALLOW_DERIVED)
10207 {
10208
10209 t = try_class_unification (tparms, targs,
10210 parm, arg);
10211
10212 if (!t)
10213 {
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223 t = get_template_base (tparms, targs, parm, arg);
10224
10225 if (!t)
10226 return 1;
10227 }
10228 }
10229 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10230 && (CLASSTYPE_TI_TEMPLATE (parm)
10231 == CLASSTYPE_TI_TEMPLATE (arg)))
10232
10233
10234 t = arg;
10235 else
10236
10237 return 1;
10238
10239 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10240 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10241 }
10242 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10243 return 1;
10244 return 0;
10245
10246 case METHOD_TYPE:
10247 case FUNCTION_TYPE:
10248 if (TREE_CODE (arg) != TREE_CODE (parm))
10249 return 1;
10250
10251
10252
10253
10254
10255 if (TREE_CODE (parm) == METHOD_TYPE
10256 && (!check_cv_quals_for_unify
10257 (UNIFY_ALLOW_NONE,
10258 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10259 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10260 return 1;
10261
10262 if (unify (tparms, targs, TREE_TYPE (parm),
10263 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10264 return 1;
10265 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10266 TYPE_ARG_TYPES (arg), 1,
10267 DEDUCE_EXACT, 0, -1);
10268
10269 case OFFSET_TYPE:
10270
10271
10272 if (TYPE_PTRMEMFUNC_P (arg))
10273 {
10274 tree method_type;
10275 tree fntype;
10276 cp_cv_quals cv_quals;
10277
10278
10279 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10280 return 1;
10281
10282 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10283 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10284 return 1;
10285
10286
10287 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10288 fntype =
10289 build_function_type (TREE_TYPE (method_type),
10290 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10291
10292
10293
10294
10295 cv_quals =
10296 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10297 fntype = build_qualified_type (fntype, cv_quals);
10298 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10299 }
10300
10301 if (TREE_CODE (arg) != OFFSET_TYPE)
10302 return 1;
10303 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10304 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10305 return 1;
10306 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10307 strict);
10308
10309 case CONST_DECL:
10310 if (DECL_TEMPLATE_PARM_P (parm))
10311 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10312 if (arg != integral_constant_value (parm))
10313 return 1;
10314 return 0;
10315
10316 case FIELD_DECL:
10317 case TEMPLATE_DECL:
10318
10319 return 1;
10320
10321 default:
10322 gcc_assert (EXPR_P (parm));
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343 if (!uses_template_parms (parm)
10344 && !template_args_equal (parm, arg))
10345 return 1;
10346 else
10347 return 0;
10348 }
10349 }
10350
10351
10352
10353
10354 static void
10355 mark_definable (tree decl)
10356 {
10357 tree clone;
10358 DECL_NOT_REALLY_EXTERN (decl) = 1;
10359 FOR_EACH_CLONE (clone, decl)
10360 DECL_NOT_REALLY_EXTERN (clone) = 1;
10361 }
10362
10363
10364
10365
10366 void
10367 mark_decl_instantiated (tree result, int extern_p)
10368 {
10369 SET_DECL_EXPLICIT_INSTANTIATION (result);
10370
10371
10372
10373 if (TREE_ASM_WRITTEN (result))
10374 return;
10375
10376 if (TREE_CODE (result) != FUNCTION_DECL)
10377
10378
10379 TREE_PUBLIC (result) = 1;
10380
10381
10382 DECL_COMDAT (result) = 0;
10383
10384 if (extern_p)
10385 DECL_NOT_REALLY_EXTERN (result) = 0;
10386 else
10387 {
10388 mark_definable (result);
10389
10390 if (DECL_ARTIFICIAL (result) && flag_weak)
10391 comdat_linkage (result);
10392
10393
10394 else if (TREE_PUBLIC (result))
10395 maybe_make_one_only (result);
10396 }
10397
10398
10399
10400
10401
10402
10403 DECL_INTERFACE_KNOWN (result) = 1;
10404 }
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432 int
10433 more_specialized_fn (tree pat1, tree pat2, int len)
10434 {
10435 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10436 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10437 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10438 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10439 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10440 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10441 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10442 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10443 int better1 = 0;
10444 int better2 = 0;
10445
10446
10447 if (DECL_FUNCTION_MEMBER_P (decl1) != DECL_FUNCTION_MEMBER_P (decl2))
10448 return 0;
10449
10450
10451 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
10452 args1 = TREE_CHAIN (args1);
10453 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10454 args2 = TREE_CHAIN (args2);
10455
10456
10457 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10458 return 0;
10459
10460
10461 if (DECL_CONV_FN_P (decl1))
10462 {
10463 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10464 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10465 len++;
10466 }
10467
10468 processing_template_decl++;
10469
10470 while (len--)
10471 {
10472 tree arg1 = TREE_VALUE (args1);
10473 tree arg2 = TREE_VALUE (args2);
10474 int deduce1, deduce2;
10475 int quals1 = -1;
10476 int quals2 = -1;
10477
10478 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10479 {
10480 arg1 = TREE_TYPE (arg1);
10481 quals1 = cp_type_quals (arg1);
10482 }
10483
10484 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10485 {
10486 arg2 = TREE_TYPE (arg2);
10487 quals2 = cp_type_quals (arg2);
10488 }
10489
10490 if ((quals1 < 0) != (quals2 < 0))
10491 {
10492
10493
10494
10495
10496
10497
10498 if (quals1 >= 0)
10499 {
10500 switch (TREE_CODE (arg1))
10501 {
10502 case ARRAY_TYPE:
10503 arg1 = TREE_TYPE (arg1);
10504
10505 case FUNCTION_TYPE:
10506 arg1 = build_pointer_type (arg1);
10507 break;
10508
10509 default:
10510 break;
10511 }
10512 }
10513 else
10514 {
10515 switch (TREE_CODE (arg2))
10516 {
10517 case ARRAY_TYPE:
10518 arg2 = TREE_TYPE (arg2);
10519
10520 case FUNCTION_TYPE:
10521 arg2 = build_pointer_type (arg2);
10522 break;
10523
10524 default:
10525 break;
10526 }
10527 }
10528 }
10529
10530 arg1 = TYPE_MAIN_VARIANT (arg1);
10531 arg2 = TYPE_MAIN_VARIANT (arg2);
10532
10533 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
10534 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
10535
10536 if (!deduce1)
10537 better2 = -1;
10538 if (!deduce2)
10539 better1 = -1;
10540 if (better1 < 0 && better2 < 0)
10541
10542
10543 break;
10544
10545 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
10546 {
10547
10548
10549 if ((quals1 & quals2) == quals2)
10550 deduce1 = 0;
10551 if ((quals1 & quals2) == quals1)
10552 deduce2 = 0;
10553 }
10554 if (deduce1 && !deduce2 && !better2)
10555 better2 = 1;
10556 if (deduce2 && !deduce1 && !better1)
10557 better1 = 1;
10558
10559 args1 = TREE_CHAIN (args1);
10560 args2 = TREE_CHAIN (args2);
10561 }
10562
10563 processing_template_decl--;
10564
10565 return (better1 > 0) - (better2 > 0);
10566 }
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577 int
10578 more_specialized_class (tree pat1, tree pat2, tree full_args)
10579 {
10580 tree targs;
10581 int winner = 0;
10582
10583
10584
10585
10586
10587 ++processing_template_decl;
10588 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
10589 add_outermost_template_args (full_args, TREE_PURPOSE (pat2)));
10590 if (targs)
10591 --winner;
10592
10593 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
10594 add_outermost_template_args (full_args, TREE_PURPOSE (pat1)));
10595 if (targs)
10596 ++winner;
10597 --processing_template_decl;
10598
10599 return winner;
10600 }
10601
10602
10603
10604
10605
10606
10607
10608 static tree
10609 get_bindings_real (tree fn,
10610 tree decl,
10611 tree explicit_args,
10612 int check_rettype,
10613 int deduce,
10614 int len)
10615 {
10616 int ntparms = DECL_NTPARMS (fn);
10617 tree targs = make_tree_vec (ntparms);
10618 tree decl_type;
10619 tree decl_arg_types;
10620 int i;
10621
10622
10623
10624
10625 decl_type = TREE_TYPE (decl);
10626 if (explicit_args && uses_template_parms (decl_type))
10627 {
10628 tree tmpl;
10629 tree converted_args;
10630
10631 if (DECL_TEMPLATE_INFO (decl))
10632 tmpl = DECL_TI_TEMPLATE (decl);
10633 else
10634
10635 return NULL_TREE;
10636
10637 converted_args
10638 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10639 explicit_args, NULL_TREE,
10640 tf_none, 0));
10641 if (converted_args == error_mark_node)
10642 return NULL_TREE;
10643
10644 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10645 if (decl_type == error_mark_node)
10646 return NULL_TREE;
10647 }
10648
10649 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10650
10651 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10652 decl_arg_types = TREE_CHAIN (decl_arg_types);
10653
10654 i = fn_type_unification (fn, explicit_args, targs,
10655 decl_arg_types,
10656 (check_rettype || DECL_CONV_FN_P (fn)
10657 ? TREE_TYPE (decl_type) : NULL_TREE),
10658 deduce, len);
10659
10660 if (i != 0)
10661 return NULL_TREE;
10662
10663 return targs;
10664 }
10665
10666
10667
10668 static tree
10669 get_bindings (tree fn, tree decl, tree explicit_args)
10670 {
10671 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
10672 }
10673
10674
10675
10676
10677 static tree
10678 get_bindings_overload (tree fn, tree decl, tree explicit_args)
10679 {
10680 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
10681 }
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698 static tree
10699 get_class_bindings (tree tparms, tree parms, tree args)
10700 {
10701 int i, ntparms = TREE_VEC_LENGTH (tparms);
10702 tree vec = make_tree_vec (ntparms);
10703
10704 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10705 UNIFY_ALLOW_NONE))
10706 return NULL_TREE;
10707
10708 for (i = 0; i < ntparms; ++i)
10709 if (! TREE_VEC_ELT (vec, i))
10710 return NULL_TREE;
10711
10712 if (verify_class_unification (vec, parms, args))
10713 return NULL_TREE;
10714
10715 return vec;
10716 }
10717
10718
10719
10720
10721
10722
10723
10724
10725 tree
10726 most_specialized_instantiation (tree instantiations)
10727 {
10728 tree fn, champ;
10729
10730 if (!instantiations)
10731 return NULL_TREE;
10732
10733 ++processing_template_decl;
10734
10735 champ = instantiations;
10736 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10737 {
10738 int fate = 0;
10739
10740 if (get_bindings_real (TREE_VALUE (champ),
10741 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10742 NULL_TREE, 0, DEDUCE_EXACT, -1))
10743 fate--;
10744
10745 if (get_bindings_real (TREE_VALUE (fn),
10746 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10747 NULL_TREE, 0, DEDUCE_EXACT, -1))
10748 fate++;
10749
10750 if (fate != 1)
10751 {
10752 if (!fate)
10753
10754
10755 fn = TREE_CHAIN (fn);
10756 champ = fn;
10757 }
10758 }
10759
10760 if (champ)
10761
10762
10763 for (fn = instantiations; fn != champ; fn = TREE_CHAIN (fn))
10764 if (get_bindings_real (TREE_VALUE (champ),
10765 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
10766 NULL_TREE, 0, DEDUCE_EXACT, -1)
10767 || !get_bindings_real (TREE_VALUE (fn),
10768 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
10769 NULL_TREE, 0, DEDUCE_EXACT, -1))
10770 {
10771 champ = NULL_TREE;
10772 break;
10773 }
10774
10775 processing_template_decl--;
10776
10777 if (!champ)
10778 return error_mark_node;
10779
10780 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10781 }
10782
10783
10784
10785
10786
10787 static tree
10788 most_specialized (tree fns, tree decl, tree explicit_args)
10789 {
10790 tree candidates = NULL_TREE;
10791 tree fn, args;
10792
10793 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10794 {
10795 tree candidate = TREE_VALUE (fn);
10796
10797 args = get_bindings (candidate, decl, explicit_args);
10798 if (args)
10799 candidates = tree_cons (NULL_TREE, candidate, candidates);
10800 }
10801
10802 return most_specialized_instantiation (candidates);
10803 }
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822 tree (*p_most_general_template) (tree) = most_general_template;
10823
10824 tree
10825 most_general_template (tree decl)
10826 {
10827
10828
10829 if (TREE_CODE (decl) == FUNCTION_DECL)
10830 {
10831 if (DECL_TEMPLATE_INFO (decl)) {
10832 decl = DECL_TI_TEMPLATE (decl);
10833
10834
10835
10836 if (TREE_CODE (decl) != TEMPLATE_DECL)
10837 return NULL_TREE;
10838 } else
10839 return NULL_TREE;
10840 }
10841
10842
10843 while (DECL_TEMPLATE_INFO (decl))
10844 {
10845
10846
10847 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10848 break;
10849
10850 if (CLASS_TYPE_P (TREE_TYPE (decl))
10851 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10852 break;
10853
10854
10855 if (!DECL_NAMESPACE_SCOPE_P (decl)
10856 && DECL_CONTEXT (decl)
10857 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10858 break;
10859
10860 decl = DECL_TI_TEMPLATE (decl);
10861 }
10862
10863 return decl;
10864 }
10865
10866
10867
10868
10869
10870 static tree
10871 most_specialized_class (tree tmpl, tree args)
10872 {
10873 tree list = NULL_TREE;
10874 tree t;
10875 tree champ;
10876 int fate;
10877
10878 tmpl = most_general_template (tmpl);
10879 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10880 {
10881 tree spec_args
10882 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10883 if (spec_args)
10884 {
10885 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10886 TREE_TYPE (list) = TREE_TYPE (t);
10887 }
10888 }
10889
10890 if (! list)
10891 return NULL_TREE;
10892
10893 t = list;
10894 champ = t;
10895 t = TREE_CHAIN (t);
10896 for (; t; t = TREE_CHAIN (t))
10897 {
10898 fate = more_specialized_class (champ, t, args);
10899 if (fate == 1)
10900 ;
10901 else
10902 {
10903 if (fate == 0)
10904 {
10905 t = TREE_CHAIN (t);
10906 if (! t)
10907 return error_mark_node;
10908 }
10909 champ = t;
10910 }
10911 }
10912
10913 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10914 {
10915 fate = more_specialized_class (champ, t, args);
10916 if (fate != 1)
10917 return error_mark_node;
10918 }
10919
10920 return champ;
10921 }
10922
10923
10924
10925 void
10926 do_decl_instantiation (tree decl, tree storage)
10927 {
10928 tree result = NULL_TREE;
10929 int extern_p = 0;
10930
10931 if (!decl)
10932
10933
10934 return;
10935 else if (! DECL_LANG_SPECIFIC (decl))
10936 {
10937 error ("explicit instantiation of non-template %q#D", decl);
10938 return;
10939 }
10940 else if (TREE_CODE (decl) == VAR_DECL)
10941 {
10942
10943
10944
10945
10946
10947
10948
10949
10950 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10951 if (!result || TREE_CODE (result) != VAR_DECL)
10952 {
10953 error ("no matching template for %qD found", decl);
10954 return;
10955 }
10956 }
10957 else if (TREE_CODE (decl) != FUNCTION_DECL)
10958 {
10959 error ("explicit instantiation of %q#D", decl);
10960 return;
10961 }
10962 else
10963 result = decl;
10964
10965
10966
10967
10968
10969
10970 if (DECL_TEMPLATE_SPECIALIZATION (result))
10971 {
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982 return;
10983 }
10984 else if (DECL_EXPLICIT_INSTANTIATION (result))
10985 {
10986
10987
10988
10989
10990
10991
10992
10993
10994 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
10995 pedwarn ("duplicate explicit instantiation of %q#D", result);
10996
10997
10998 if (extern_p)
10999 return;
11000 }
11001 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11002 {
11003 error ("no matching template for %qD found", result);
11004 return;
11005 }
11006 else if (!DECL_TEMPLATE_INFO (result))
11007 {
11008 pedwarn ("explicit instantiation of non-template %q#D", result);
11009 return;
11010 }
11011
11012 if (storage == NULL_TREE)
11013 ;
11014 else if (storage == ridpointers[(int) RID_EXTERN])
11015 {
11016 if (pedantic && !in_system_header)
11017 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11018 "instantiations");
11019 extern_p = 1;
11020 }
11021 else
11022 error ("storage class %qD applied to template instantiation", storage);
11023
11024 mark_decl_instantiated (result, extern_p);
11025 if (! extern_p)
11026 instantiate_decl (result, 1, 0);
11027 }
11028
11029 void
11030 mark_class_instantiated (tree t, int extern_p)
11031 {
11032 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11033 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11034 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11035 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11036 if (! extern_p)
11037 {
11038 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11039 rest_of_type_compilation (t, 1);
11040 }
11041 }
11042
11043
11044
11045 static void
11046 bt_instantiate_type_proc (binding_entry entry, void *data)
11047 {
11048 tree storage = *(tree *) data;
11049
11050 if (IS_AGGR_TYPE (entry->type)
11051 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11052 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11053 }
11054
11055
11056
11057
11058 static void
11059 instantiate_class_member (tree decl, int extern_p)
11060 {
11061 mark_decl_instantiated (decl, extern_p);
11062 if (! extern_p)
11063 instantiate_decl (decl, 1, 1);
11064 }
11065
11066
11067
11068
11069
11070
11071 void
11072 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11073 {
11074 int extern_p = 0;
11075 int nomem_p = 0;
11076 int static_p = 0;
11077 int previous_instantiation_extern_p = 0;
11078
11079 if (TREE_CODE (t) == TYPE_DECL)
11080 t = TREE_TYPE (t);
11081
11082 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11083 {
11084 error ("explicit instantiation of non-template type %qT", t);
11085 return;
11086 }
11087
11088 complete_type (t);
11089
11090 if (!COMPLETE_TYPE_P (t))
11091 {
11092 if (complain & tf_error)
11093 error ("explicit instantiation of %q#T before definition of template",
11094 t);
11095 return;
11096 }
11097
11098 if (storage != NULL_TREE)
11099 {
11100 if (pedantic && !in_system_header)
11101 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11102 storage);
11103
11104 if (storage == ridpointers[(int) RID_INLINE])
11105 nomem_p = 1;
11106 else if (storage == ridpointers[(int) RID_EXTERN])
11107 extern_p = 1;
11108 else if (storage == ridpointers[(int) RID_STATIC])
11109 static_p = 1;
11110 else
11111 {
11112 error ("storage class %qD applied to template instantiation",
11113 storage);
11114 extern_p = 0;
11115 }
11116 }
11117
11118 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11119 {
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130 return;
11131 }
11132 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11133 {
11134
11135
11136
11137
11138
11139
11140
11141
11142 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11143
11144 if (!previous_instantiation_extern_p && !extern_p
11145 && (complain & tf_error))
11146 pedwarn ("duplicate explicit instantiation of %q#T", t);
11147
11148
11149 if (!CLASSTYPE_INTERFACE_ONLY (t))
11150 return;
11151 }
11152
11153 mark_class_instantiated (t, extern_p);
11154
11155 if (nomem_p)
11156 return;
11157
11158 {
11159 tree tmp;
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178 if (! static_p)
11179 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11180 if (TREE_CODE (tmp) == FUNCTION_DECL
11181 && DECL_TEMPLATE_INSTANTIATION (tmp))
11182 instantiate_class_member (tmp, extern_p);
11183
11184 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11185 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11186 instantiate_class_member (tmp, extern_p);
11187
11188 if (CLASSTYPE_NESTED_UTDS (t))
11189 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11190 bt_instantiate_type_proc, &storage);
11191 }
11192 }
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214 static void
11215 regenerate_decl_from_template (tree decl, tree tmpl)
11216 {
11217
11218
11219 tree args;
11220 tree code_pattern;
11221
11222 args = DECL_TI_ARGS (decl);
11223 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11224
11225
11226
11227 push_access_scope (decl);
11228
11229 if (TREE_CODE (decl) == FUNCTION_DECL)
11230 {
11231 tree decl_parm;
11232 tree pattern_parm;
11233 tree specs;
11234 int args_depth;
11235 int parms_depth;
11236
11237 args_depth = TMPL_ARGS_DEPTH (args);
11238 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11239 if (args_depth > parms_depth)
11240 args = get_innermost_template_args (args, parms_depth);
11241
11242 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11243 args, tf_error, NULL_TREE);
11244 if (specs)
11245 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11246 specs);
11247
11248
11249 decl_parm = skip_artificial_parms_for (decl,
11250 DECL_ARGUMENTS (decl));
11251 pattern_parm
11252 = skip_artificial_parms_for (code_pattern,
11253 DECL_ARGUMENTS (code_pattern));
11254 while (decl_parm)
11255 {
11256 tree parm_type;
11257 tree attributes;
11258
11259 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11260 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11261 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11262 NULL_TREE);
11263 parm_type = type_decays_to (parm_type);
11264 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11265 TREE_TYPE (decl_parm) = parm_type;
11266 attributes = DECL_ATTRIBUTES (pattern_parm);
11267 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11268 {
11269 DECL_ATTRIBUTES (decl_parm) = attributes;
11270 cplus_decl_attributes (&decl_parm, attributes, 0);
11271 }
11272 decl_parm = TREE_CHAIN (decl_parm);
11273 pattern_parm = TREE_CHAIN (pattern_parm);
11274 }
11275
11276
11277 if (DECL_DECLARED_INLINE_P (code_pattern)
11278 && !DECL_DECLARED_INLINE_P (decl))
11279 DECL_DECLARED_INLINE_P (decl) = 1;
11280 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11281 DECL_INLINE (decl) = 1;
11282 }
11283 else if (TREE_CODE (decl) == VAR_DECL)
11284 DECL_INITIAL (decl) =
11285 tsubst_expr (DECL_INITIAL (code_pattern), args,
11286 tf_error, DECL_TI_TEMPLATE (decl));
11287 else
11288 gcc_unreachable ();
11289
11290 pop_access_scope (decl);
11291 }
11292
11293
11294
11295
11296 tree
11297 template_for_substitution (tree decl)
11298 {
11299 tree tmpl = DECL_TI_TEMPLATE (decl);
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311 while (
11312
11313 DECL_TEMPLATE_INSTANTIATION (tmpl)
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332 || (TREE_CODE (decl) == FUNCTION_DECL
11333 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11334 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11335 {
11336
11337
11338
11339
11340
11341 gcc_assert (TREE_CODE (decl) != VAR_DECL
11342 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
11343
11344
11345 tmpl = DECL_TI_TEMPLATE (tmpl);
11346 }
11347
11348 return tmpl;
11349 }
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361 tree
11362 instantiate_decl (tree d, int defer_ok, int undefined_ok)
11363 {
11364 tree tmpl = DECL_TI_TEMPLATE (d);
11365 tree gen_args;
11366 tree args;
11367 tree td;
11368 tree code_pattern;
11369 tree spec;
11370 tree gen_tmpl;
11371 int pattern_defined;
11372 int need_push;
11373 location_t saved_loc = input_location;
11374 bool external_p;
11375
11376
11377
11378 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11379 || TREE_CODE (d) == VAR_DECL);
11380
11381
11382
11383
11384
11385
11386 if (TREE_CODE (d) == VAR_DECL)
11387 defer_ok = 0;
11388
11389
11390
11391 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11392 d = DECL_CLONED_FUNCTION (d);
11393
11394 if (DECL_TEMPLATE_INSTANTIATED (d))
11395
11396
11397
11398
11399
11400 return d;
11401
11402
11403
11404
11405
11406
11407 gen_tmpl = most_general_template (tmpl);
11408 gen_args = DECL_TI_ARGS (d);
11409 spec = retrieve_specialization (gen_tmpl, gen_args,
11410 false);
11411 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11412 return spec;
11413
11414
11415 if (! push_tinst_level (d))
11416 return d;
11417
11418 timevar_push (TV_PARSE);
11419
11420
11421
11422 td = template_for_substitution (d);
11423 code_pattern = DECL_TEMPLATE_RESULT (td);
11424
11425 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11426 || DECL_TEMPLATE_SPECIALIZATION (td))
11427
11428
11429
11430 args = get_innermost_template_args
11431 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11432 else
11433 args = gen_args;
11434
11435 if (TREE_CODE (d) == FUNCTION_DECL)
11436 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
11437 else
11438 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
11439
11440
11441 push_deferring_access_checks (dk_no_deferred);
11442
11443
11444
11445
11446 if (pattern_defined
11447 && !DECL_INTERFACE_KNOWN (d)
11448 && !DECL_NOT_REALLY_EXTERN (d))
11449 mark_definable (d);
11450
11451 input_location = DECL_SOURCE_LOCATION (d);
11452
11453 if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok)
11454 {
11455 DECL_NOT_REALLY_EXTERN (d) = 0;
11456 DECL_INTERFACE_KNOWN (d) = 0;
11457 SET_DECL_IMPLICIT_INSTANTIATION (d);
11458 }
11459
11460 if (!defer_ok)
11461 {
11462
11463
11464 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
11465 tree type = TREE_TYPE (gen);
11466
11467
11468
11469
11470 push_access_scope (d);
11471
11472 if (TREE_CODE (gen) == FUNCTION_DECL)
11473 {
11474 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
11475 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
11476 tf_error | tf_warning, d);
11477
11478
11479
11480
11481 type = TREE_TYPE (type);
11482 }
11483 tsubst (type, gen_args, tf_error | tf_warning, d);
11484
11485 pop_access_scope (d);
11486 }
11487
11488
11489
11490
11491 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
11492
11493 if (external_p
11494
11495
11496 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
11497
11498
11499 && ! (TREE_CODE (d) == VAR_DECL
11500 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
11501 goto out;
11502
11503
11504 if (
11505
11506 ! pattern_defined
11507
11508 || defer_ok
11509
11510
11511
11512
11513 || (external_p && TREE_CODE (d) == VAR_DECL))
11514 {
11515
11516
11517 if (TREE_CODE (d) == VAR_DECL
11518 && !DECL_INITIAL (d)
11519 && DECL_INITIAL (code_pattern))
11520 {
11521 push_nested_class (DECL_CONTEXT (d));
11522 DECL_INITIAL (d)
11523 = tsubst_expr (DECL_INITIAL (code_pattern),
11524 args,
11525 tf_error | tf_warning, NULL_TREE);
11526 pop_nested_class ();
11527 }
11528
11529
11530
11531 input_location = saved_loc;
11532
11533 if (at_eof && !pattern_defined
11534 && DECL_EXPLICIT_INSTANTIATION (d))
11535
11536
11537
11538
11539
11540
11541
11542 pedwarn
11543 ("explicit instantiation of %qD but no definition available", d);
11544
11545
11546
11547 if (!(external_p && TREE_CODE (d) == VAR_DECL))
11548 add_pending_template (d);
11549 goto out;
11550 }
11551
11552
11553 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
11554 {
11555
11556
11557
11558
11559 if (pch_file)
11560 add_pending_template (d);
11561
11562
11563
11564 if (!(TREE_CODE (d) == FUNCTION_DECL
11565 && flag_inline_trees
11566 && DECL_DECLARED_INLINE_P (d)))
11567 goto out;
11568 }
11569
11570 need_push = !cfun || !global_bindings_p ();
11571 if (need_push)
11572 push_to_top_level ();
11573
11574
11575
11576 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11577
11578
11579
11580 regenerate_decl_from_template (d, td);
11581
11582
11583
11584 input_location = DECL_SOURCE_LOCATION (d);
11585
11586 if (TREE_CODE (d) == VAR_DECL)
11587 {
11588
11589
11590 SET_DECL_RTL (d, NULL_RTX);
11591 DECL_IN_AGGR_P (d) = 0;
11592
11593
11594
11595
11596 DECL_EXTERNAL (d) = 0;
11597
11598
11599 push_nested_class (DECL_CONTEXT (d));
11600 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
11601 pop_nested_class ();
11602 }
11603 else if (TREE_CODE (d) == FUNCTION_DECL)
11604 {
11605 htab_t saved_local_specializations;
11606 tree subst_decl;
11607 tree tmpl_parm;
11608 tree spec_parm;
11609
11610
11611
11612 saved_local_specializations = local_specializations;
11613
11614
11615 local_specializations = htab_create (37,
11616 hash_local_specialization,
11617 eq_local_specializations,
11618 NULL);
11619
11620
11621 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
11622
11623
11624 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11625 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11626 spec_parm = DECL_ARGUMENTS (d);
11627 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11628 {
11629 register_local_specialization (spec_parm, tmpl_parm);
11630 spec_parm = skip_artificial_parms_for (d, spec_parm);
11631 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11632 }
11633 while (tmpl_parm)
11634 {
11635 register_local_specialization (spec_parm, tmpl_parm);
11636 tmpl_parm = TREE_CHAIN (tmpl_parm);
11637 spec_parm = TREE_CHAIN (spec_parm);
11638 }
11639 gcc_assert (!spec_parm);
11640
11641
11642 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11643 tf_error | tf_warning, tmpl);
11644
11645
11646 htab_delete (local_specializations);
11647 local_specializations = saved_local_specializations;
11648
11649
11650 d = finish_function (0);
11651 expand_or_defer_fn (d);
11652 }
11653
11654
11655 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11656
11657 if (need_push)
11658 pop_from_top_level ();
11659
11660 out:
11661 input_location = saved_loc;
11662 pop_deferring_access_checks ();
11663 pop_tinst_level ();
11664
11665 timevar_pop (TV_PARSE);
11666
11667 return d;
11668 }
11669
11670
11671
11672
11673
11674 void
11675 instantiate_pending_templates (int retries)
11676 {
11677 tree *t;
11678 tree last = NULL_TREE;
11679 int reconsider;
11680 location_t saved_loc = input_location;
11681 int saved_in_system_header = in_system_header;
11682
11683
11684
11685
11686 if (pending_templates && retries >= max_tinst_depth)
11687 {
11688 cp_error_at ("template instantiation depth exceeds maximum of %d"
11689 " (use -ftemplate-depth-NN to increase the maximum)"
11690 " instantiating %q+D, possibly from virtual table"
11691 " generation",
11692 max_tinst_depth, TREE_VALUE (pending_templates));
11693 return;
11694 }
11695
11696 do
11697 {
11698 reconsider = 0;
11699
11700 t = &pending_templates;
11701 while (*t)
11702 {
11703 tree instantiation = TREE_VALUE (*t);
11704
11705 reopen_tinst_level (TREE_PURPOSE (*t));
11706
11707 if (TYPE_P (instantiation))
11708 {
11709 tree fn;
11710
11711 if (!COMPLETE_TYPE_P (instantiation))
11712 {
11713 instantiate_class_template (instantiation);
11714 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11715 for (fn = TYPE_METHODS (instantiation);
11716 fn;
11717 fn = TREE_CHAIN (fn))
11718 if (! DECL_ARTIFICIAL (fn))
11719 instantiate_decl (fn, 0,
11720 0);
11721 if (COMPLETE_TYPE_P (instantiation))
11722 reconsider = 1;
11723 }
11724
11725 if (COMPLETE_TYPE_P (instantiation))
11726
11727
11728 *t = TREE_CHAIN (*t);
11729 else
11730 {
11731 last = *t;
11732 t = &TREE_CHAIN (*t);
11733 }
11734 }
11735 else
11736 {
11737 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11738 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11739 {
11740 instantiation = instantiate_decl (instantiation,
11741 0,
11742 0);
11743 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11744 reconsider = 1;
11745 }
11746
11747 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11748 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11749
11750
11751 *t = TREE_CHAIN (*t);
11752 else
11753 {
11754 last = *t;
11755 t = &TREE_CHAIN (*t);
11756 }
11757 }
11758 tinst_depth = 0;
11759 current_tinst_level = NULL_TREE;
11760 }
11761 last_pending_template = last;
11762 }
11763 while (reconsider);
11764
11765 input_location = saved_loc;
11766 in_system_header = saved_in_system_header;
11767 }
11768
11769
11770
11771
11772
11773
11774 static tree
11775 tsubst_initializer_list (tree t, tree argvec)
11776 {
11777 tree inits = NULL_TREE;
11778
11779 for (; t; t = TREE_CHAIN (t))
11780 {
11781 tree decl;
11782 tree init;
11783
11784 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11785 NULL_TREE);
11786 decl = expand_member_init (decl);
11787 if (decl && !DECL_P (decl))
11788 in_base_initializer = 1;
11789
11790 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11791 NULL_TREE);
11792 in_base_initializer = 0;
11793
11794 if (decl)
11795 {
11796 init = build_tree_list (decl, init);
11797 TREE_CHAIN (init) = inits;
11798 inits = init;
11799 }
11800 }
11801 return inits;
11802 }
11803
11804
11805
11806 static void
11807 set_current_access_from_decl (tree decl)
11808 {
11809 if (TREE_PRIVATE (decl))
11810 current_access_specifier = access_private_node;
11811 else if (TREE_PROTECTED (decl))
11812 current_access_specifier = access_protected_node;
11813 else
11814 current_access_specifier = access_public_node;
11815 }
11816
11817
11818
11819
11820
11821 static void
11822 tsubst_enum (tree tag, tree newtag, tree args)
11823 {
11824 tree e;
11825
11826 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11827 {
11828 tree value;
11829 tree decl;
11830
11831 decl = TREE_VALUE (e);
11832
11833
11834 value = tsubst_expr (DECL_INITIAL (decl),
11835 args, tf_error | tf_warning,
11836 NULL_TREE);
11837
11838
11839 set_current_access_from_decl (decl);
11840
11841
11842 build_enumerator (DECL_NAME (decl), value, newtag);
11843 }
11844
11845 finish_enum (newtag);
11846 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11847 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11848 }
11849
11850
11851
11852
11853
11854
11855 tree
11856 get_mostly_instantiated_function_type (tree decl)
11857 {
11858 tree fn_type;
11859 tree tmpl;
11860 tree targs;
11861 tree tparms;
11862 int parm_depth;
11863
11864 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11865 targs = DECL_TI_ARGS (decl);
11866 tparms = DECL_TEMPLATE_PARMS (tmpl);
11867 parm_depth = TMPL_PARMS_DEPTH (tparms);
11868
11869
11870
11871 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
11872
11873 fn_type = TREE_TYPE (tmpl);
11874
11875 if (parm_depth == 1)
11876
11877 ;
11878 else
11879 {
11880 int i, save_access_control;
11881 tree partial_args;
11882
11883
11884
11885 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11886 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11887 SET_TMPL_ARGS_LEVEL (partial_args, i,
11888 TMPL_ARGS_LEVEL (targs, i));
11889 SET_TMPL_ARGS_LEVEL (partial_args,
11890 TMPL_ARGS_DEPTH (targs),
11891 make_tree_vec (DECL_NTPARMS (tmpl)));
11892
11893
11894
11895 save_access_control = flag_access_control;
11896 flag_access_control = 0;
11897
11898 ++processing_template_decl;
11899
11900
11901 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11902 --processing_template_decl;
11903
11904
11905
11906
11907
11908 TREE_VEC_LENGTH (partial_args)--;
11909 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11910
11911 flag_access_control = save_access_control;
11912 }
11913
11914 return fn_type;
11915 }
11916
11917
11918
11919 int
11920 problematic_instantiation_changed (void)
11921 {
11922 return last_template_error_tick != tinst_level_tick;
11923 }
11924
11925
11926 void
11927 record_last_problematic_instantiation (void)
11928 {
11929 last_template_error_tick = tinst_level_tick;
11930 }
11931
11932 tree
11933 current_instantiation (void)
11934 {
11935 return current_tinst_level;
11936 }
11937
11938
11939
11940
11941
11942 static int
11943 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
11944 {
11945 if (INTEGRAL_TYPE_P (type))
11946 return 0;
11947 else if (POINTER_TYPE_P (type))
11948 return 0;
11949 else if (TYPE_PTR_TO_MEMBER_P (type))
11950 return 0;
11951 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11952 return 0;
11953 else if (TREE_CODE (type) == TYPENAME_TYPE)
11954 return 0;
11955
11956 if (complain & tf_error)
11957 error ("%q#T is not a valid type for a template constant parameter", type);
11958 return 1;
11959 }
11960
11961
11962
11963
11964 static bool
11965 dependent_type_p_r (tree type)
11966 {
11967 tree scope;
11968
11969
11970
11971
11972
11973
11974
11975
11976 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
11977 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
11978 return true;
11979
11980
11981
11982 if (TREE_CODE (type) == TYPENAME_TYPE)
11983 return true;
11984
11985
11986 type = TYPE_MAIN_VARIANT (type);
11987
11988 if (TYPE_PTR_TO_MEMBER_P (type))
11989 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11990 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11991 (type)));
11992 else if (TREE_CODE (type) == POINTER_TYPE
11993 || TREE_CODE (type) == REFERENCE_TYPE)
11994 return dependent_type_p (TREE_TYPE (type));
11995 else if (TREE_CODE (type) == FUNCTION_TYPE
11996 || TREE_CODE (type) == METHOD_TYPE)
11997 {
11998 tree arg_type;
11999
12000 if (dependent_type_p (TREE_TYPE (type)))
12001 return true;
12002 for (arg_type = TYPE_ARG_TYPES (type);
12003 arg_type;
12004 arg_type = TREE_CHAIN (arg_type))
12005 if (dependent_type_p (TREE_VALUE (arg_type)))
12006 return true;
12007 return false;
12008 }
12009
12010
12011
12012 if (TREE_CODE (type) == ARRAY_TYPE)
12013 {
12014 if (TYPE_DOMAIN (type)
12015 && ((value_dependent_expression_p
12016 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12017 || (type_dependent_expression_p
12018 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12019 return true;
12020 return dependent_type_p (TREE_TYPE (type));
12021 }
12022
12023
12024
12025 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12026 return true;
12027
12028
12029 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12030 && (any_dependent_template_arguments_p
12031 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12032 return true;
12033
12034
12035
12036
12037 if (TREE_CODE (type) == TYPEOF_TYPE)
12038 return true;
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051 scope = TYPE_CONTEXT (type);
12052 if (scope && TYPE_P (scope))
12053 return dependent_type_p (scope);
12054 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12055 return type_dependent_expression_p (scope);
12056
12057
12058 return false;
12059 }
12060
12061
12062
12063
12064 bool
12065 dependent_type_p (tree type)
12066 {
12067
12068
12069 if (!processing_template_decl)
12070 return false;
12071
12072
12073
12074 if (!type)
12075 return true;
12076
12077
12078 if (type == error_mark_node)
12079 return false;
12080
12081
12082
12083 if (!TYPE_DEPENDENT_P_VALID (type))
12084 {
12085 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12086 TYPE_DEPENDENT_P_VALID (type) = 1;
12087 }
12088
12089 return TYPE_DEPENDENT_P (type);
12090 }
12091
12092
12093
12094 static bool
12095 dependent_scope_ref_p (tree expression, bool criterion (tree))
12096 {
12097 tree scope;
12098 tree name;
12099
12100 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12101
12102 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12103 return true;
12104
12105 scope = TREE_OPERAND (expression, 0);
12106 name = TREE_OPERAND (expression, 1);
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116 if (DECL_P (name)
12117 && currently_open_class (scope)
12118 && !criterion (name))
12119 return false;
12120 if (dependent_type_p (scope))
12121 return true;
12122
12123 return false;
12124 }
12125
12126
12127
12128
12129 bool
12130 value_dependent_expression_p (tree expression)
12131 {
12132 if (!processing_template_decl)
12133 return false;
12134
12135
12136 if (TREE_CODE (expression) == IDENTIFIER_NODE
12137 || (DECL_P (expression)
12138 && type_dependent_expression_p (expression)))
12139 return true;
12140
12141 if ((TREE_CODE (expression) == CONST_DECL
12142 && DECL_TEMPLATE_PARM_P (expression))
12143 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
12144 return true;
12145
12146
12147 if (TREE_CODE (expression) == VAR_DECL
12148 && DECL_INITIAL (expression)
12149 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12150 && value_dependent_expression_p (DECL_INITIAL (expression)))
12151 return true;
12152
12153
12154
12155 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12156 || TREE_CODE (expression) == STATIC_CAST_EXPR
12157 || TREE_CODE (expression) == CONST_CAST_EXPR
12158 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12159 || TREE_CODE (expression) == CAST_EXPR)
12160 {
12161 tree type = TREE_TYPE (expression);
12162 if (dependent_type_p (type))
12163 return true;
12164
12165 expression = TREE_OPERAND (expression, 0);
12166 if (!expression)
12167 {
12168
12169
12170
12171 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12172
12173 return false;
12174 }
12175 if (TREE_CODE (expression) == TREE_LIST)
12176 {
12177 do
12178 {
12179 if (value_dependent_expression_p (TREE_VALUE (expression)))
12180 return true;
12181 expression = TREE_CHAIN (expression);
12182 }
12183 while (expression);
12184 return false;
12185 }
12186 else
12187 return value_dependent_expression_p (expression);
12188 }
12189
12190
12191 if (TREE_CODE (expression) == SIZEOF_EXPR
12192 || TREE_CODE (expression) == ALIGNOF_EXPR)
12193 {
12194 expression = TREE_OPERAND (expression, 0);
12195 if (TYPE_P (expression))
12196 return dependent_type_p (expression);
12197 return type_dependent_expression_p (expression);
12198 }
12199 if (TREE_CODE (expression) == SCOPE_REF)
12200 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12201 if (TREE_CODE (expression) == COMPONENT_REF)
12202 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12203 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12204
12205
12206
12207
12208
12209
12210
12211 if (TREE_CODE (expression) == CALL_EXPR)
12212 {
12213 tree function = TREE_OPERAND (expression, 0);
12214 tree args = TREE_OPERAND (expression, 1);
12215
12216 if (value_dependent_expression_p (function))
12217 return true;
12218 else if (! args)
12219 return false;
12220 else if (TREE_CODE (args) == TREE_LIST)
12221 {
12222 do
12223 {
12224 if (value_dependent_expression_p (TREE_VALUE (args)))
12225 return true;
12226 args = TREE_CHAIN (args);
12227 }
12228 while (args);
12229 return false;
12230 }
12231 else
12232 return value_dependent_expression_p (args);
12233 }
12234
12235
12236 if (EXPR_P (expression))
12237 {
12238 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12239 {
12240 case tcc_reference:
12241 case tcc_unary:
12242 return (value_dependent_expression_p
12243 (TREE_OPERAND (expression, 0)));
12244 case tcc_comparison:
12245 case tcc_binary:
12246 return ((value_dependent_expression_p
12247 (TREE_OPERAND (expression, 0)))
12248 || (value_dependent_expression_p
12249 (TREE_OPERAND (expression, 1))));
12250 case tcc_expression:
12251 {
12252 int i;
12253 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12254
12255
12256
12257
12258 if (TREE_OPERAND (expression, i)
12259 && (value_dependent_expression_p
12260 (TREE_OPERAND (expression, i))))
12261 return true;
12262 return false;
12263 }
12264 case tcc_statement:
12265
12266 return false;
12267
12268 default:
12269 gcc_unreachable ();
12270 }
12271 }
12272
12273
12274 return false;
12275 }
12276
12277
12278
12279
12280 bool
12281 type_dependent_expression_p (tree expression)
12282 {
12283 if (!processing_template_decl)
12284 return false;
12285
12286 if (expression == error_mark_node)
12287 return false;
12288
12289
12290 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12291 return true;
12292
12293
12294 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12295 || TREE_CODE (expression) == SIZEOF_EXPR
12296 || TREE_CODE (expression) == ALIGNOF_EXPR
12297 || TREE_CODE (expression) == TYPEID_EXPR
12298 || TREE_CODE (expression) == DELETE_EXPR
12299 || TREE_CODE (expression) == VEC_DELETE_EXPR
12300 || TREE_CODE (expression) == THROW_EXPR)
12301 return false;
12302
12303
12304
12305 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12306 || TREE_CODE (expression) == STATIC_CAST_EXPR
12307 || TREE_CODE (expression) == CONST_CAST_EXPR
12308 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12309 || TREE_CODE (expression) == CAST_EXPR)
12310 return dependent_type_p (TREE_TYPE (expression));
12311
12312
12313
12314 if (TREE_CODE (expression) == NEW_EXPR
12315 || TREE_CODE (expression) == VEC_NEW_EXPR)
12316 {
12317
12318
12319
12320 tree type = TREE_OPERAND (expression, 1);
12321 if (TREE_CODE (type) == TREE_LIST)
12322
12323
12324 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12325 || value_dependent_expression_p
12326 (TREE_OPERAND (TREE_VALUE (type), 1));
12327 else
12328 return dependent_type_p (type);
12329 }
12330
12331 if (TREE_CODE (expression) == SCOPE_REF
12332 && dependent_scope_ref_p (expression,
12333 type_dependent_expression_p))
12334 return true;
12335
12336 if (TREE_CODE (expression) == FUNCTION_DECL
12337 && DECL_LANG_SPECIFIC (expression)
12338 && DECL_TEMPLATE_INFO (expression)
12339 && (any_dependent_template_arguments_p
12340 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12341 return true;
12342
12343 if (TREE_CODE (expression) == TEMPLATE_DECL
12344 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12345 return false;
12346
12347 if (TREE_TYPE (expression) == unknown_type_node)
12348 {
12349 if (TREE_CODE (expression) == ADDR_EXPR)
12350 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
12351 if (TREE_CODE (expression) == COMPONENT_REF
12352 || TREE_CODE (expression) == OFFSET_REF)
12353 {
12354 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12355 return true;
12356 expression = TREE_OPERAND (expression, 1);
12357 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12358 return false;
12359 }
12360
12361 if (TREE_CODE (expression) == SCOPE_REF)
12362 return false;
12363
12364 if (TREE_CODE (expression) == BASELINK)
12365 expression = BASELINK_FUNCTIONS (expression);
12366
12367 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12368 {
12369 if (any_dependent_template_arguments_p
12370 (TREE_OPERAND (expression, 1)))
12371 return true;
12372 expression = TREE_OPERAND (expression, 0);
12373 }
12374 gcc_assert (TREE_CODE (expression) == OVERLOAD
12375 || TREE_CODE (expression) == FUNCTION_DECL);
12376
12377 while (expression)
12378 {
12379 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12380 return true;
12381 expression = OVL_NEXT (expression);
12382 }
12383 return false;
12384 }
12385
12386 return (dependent_type_p (TREE_TYPE (expression)));
12387 }
12388
12389
12390
12391
12392 bool
12393 any_type_dependent_arguments_p (tree args)
12394 {
12395 while (args)
12396 {
12397 tree arg = TREE_VALUE (args);
12398
12399 if (type_dependent_expression_p (arg))
12400 return true;
12401 args = TREE_CHAIN (args);
12402 }
12403 return false;
12404 }
12405
12406
12407
12408 static bool
12409 dependent_template_arg_p (tree arg)
12410 {
12411 if (!processing_template_decl)
12412 return false;
12413
12414 if (TREE_CODE (arg) == TEMPLATE_DECL
12415 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12416 return dependent_template_p (arg);
12417 else if (TYPE_P (arg))
12418 return dependent_type_p (arg);
12419 else
12420 return (type_dependent_expression_p (arg)
12421 || value_dependent_expression_p (arg));
12422 }
12423
12424
12425
12426
12427 bool
12428 any_dependent_template_arguments_p (tree args)
12429 {
12430 int i;
12431 int j;
12432
12433 if (!args)
12434 return false;
12435
12436 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
12437 {
12438 tree level = TMPL_ARGS_LEVEL (args, i + 1);
12439 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
12440 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
12441 return true;
12442 }
12443
12444 return false;
12445 }
12446
12447
12448
12449 bool
12450 dependent_template_p (tree tmpl)
12451 {
12452 if (TREE_CODE (tmpl) == OVERLOAD)
12453 {
12454 while (tmpl)
12455 {
12456 if (dependent_template_p (OVL_FUNCTION (tmpl)))
12457 return true;
12458 tmpl = OVL_CHAIN (tmpl);
12459 }
12460 return false;
12461 }
12462
12463
12464 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
12465 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
12466 return true;
12467
12468 if (TREE_CODE (tmpl) == SCOPE_REF
12469 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
12470 return true;
12471
12472 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
12473 return dependent_type_p (DECL_CONTEXT (tmpl));
12474 return false;
12475 }
12476
12477
12478
12479 bool
12480 dependent_template_id_p (tree tmpl, tree args)
12481 {
12482 return (dependent_template_p (tmpl)
12483 || any_dependent_template_arguments_p (args));
12484 }
12485
12486
12487
12488
12489
12490
12491
12492 tree
12493 resolve_typename_type (tree type, bool only_current_p)
12494 {
12495 tree scope;
12496 tree name;
12497 tree decl;
12498 int quals;
12499 tree pushed_scope;
12500
12501 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
12502
12503 scope = TYPE_CONTEXT (type);
12504 name = TYPE_IDENTIFIER (type);
12505
12506
12507
12508 if (TREE_CODE (scope) == TYPENAME_TYPE)
12509 scope = resolve_typename_type (scope, only_current_p);
12510
12511
12512 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
12513 return error_mark_node;
12514
12515
12516 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
12517 return type;
12518
12519
12520 if (only_current_p && !currently_open_class (scope))
12521 return error_mark_node;
12522
12523
12524 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
12525
12526
12527
12528 pushed_scope = push_scope (scope);
12529
12530 decl = lookup_member (scope, name, 0, true);
12531
12532 quals = cp_type_quals (type);
12533
12534
12535 if (!decl)
12536 type = error_mark_node;
12537 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
12538 && TREE_CODE (decl) == TYPE_DECL)
12539 type = TREE_TYPE (decl);
12540 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
12541 && DECL_CLASS_TEMPLATE_P (decl))
12542 {
12543 tree tmpl;
12544 tree args;
12545
12546 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
12547 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
12548
12549 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
12550 0, tf_error | tf_user);
12551 }
12552 else
12553 type = error_mark_node;
12554
12555 if (type != error_mark_node && quals)
12556 type = cp_build_qualified_type (type, quals);
12557
12558 if (pushed_scope)
12559 pop_scope (pushed_scope);
12560
12561 return type;
12562 }
12563
12564
12565
12566
12567
12568 tree
12569 build_non_dependent_expr (tree expr)
12570 {
12571 tree inner_expr;
12572
12573
12574
12575 if (null_ptr_cst_p (expr))
12576 return expr;
12577
12578
12579 inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
12580 TREE_OPERAND (expr, 0) : expr);
12581 if (is_overloaded_fn (inner_expr)
12582 || TREE_CODE (inner_expr) == OFFSET_REF)
12583 return expr;
12584
12585 if (TREE_CODE (expr) == VAR_DECL)
12586 return expr;
12587
12588
12589
12590 if (TREE_CODE (expr) == STRING_CST)
12591 return expr;
12592
12593
12594 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
12595 return expr;
12596
12597
12598
12599
12600
12601 if (TREE_CODE (expr) == THROW_EXPR)
12602 return expr;
12603
12604 if (TREE_CODE (expr) == COND_EXPR)
12605 return build3 (COND_EXPR,
12606 TREE_TYPE (expr),
12607 TREE_OPERAND (expr, 0),
12608 (TREE_OPERAND (expr, 1)
12609 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
12610 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
12611 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
12612 if (TREE_CODE (expr) == COMPOUND_EXPR
12613 && !COMPOUND_EXPR_OVERLOADED (expr))
12614 return build2 (COMPOUND_EXPR,
12615 TREE_TYPE (expr),
12616 TREE_OPERAND (expr, 0),
12617 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
12634 }
12635
12636
12637
12638
12639
12640 tree
12641 build_non_dependent_args (tree args)
12642 {
12643 tree a;
12644 tree new_args;
12645
12646 new_args = NULL_TREE;
12647 for (a = args; a; a = TREE_CHAIN (a))
12648 new_args = tree_cons (NULL_TREE,
12649 build_non_dependent_expr (TREE_VALUE (a)),
12650 new_args);
12651 return nreverse (new_args);
12652 }
12653
12654 #include "gt-cp-pt.h"