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