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