From 5dfe5d7d17dc9eefd8e4dd0684e6d8405d2e2759 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Sun, 7 May 2023 12:02:16 -0400 Subject: c++: various code cleanups * Harden some tree accessor macros and fix a couple of bad PLACEHOLDER_TYPE_CONSTRAINTS accesses uncovered by this. * Use strip_innermost_template_args in outer_template_args. * Add !processing_template_decl early exit tests to some dependence predicates. gcc/cp/ChangeLog: * cp-tree.h (PLACEHOLDER_TYPE_CONSTRAINTS_INFO): Harden via TEMPLATE_TYPE_PARM_CHECK. (TPARMS_PRIMARY_TEMPLATE): Harden via TREE_VEC_CHECK. (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL): Harden via TEMPLATE_TEMPLATE_PARM_CHECK. * cxx-pretty-print.cc (cxx_pretty_printer::simple_type_specifier): Guard PLACEHOLDER_TYPE_CONSTRAINTS access. * error.cc (dump_type) : Use separate variable to store CLASS_PLACEHOLDER_TEMPLATE result. * pt.cc (outer_template_args): Use strip_innermost_template_args. (any_type_dependent_arguments_p): Exit early if !processing_template_decl. Use range-based for. (any_dependent_template_arguments_p): Likewise. --- gcc/cp/cp-tree.h | 6 +++--- gcc/cp/cxx-pretty-print.cc | 5 +++-- gcc/cp/error.cc | 4 ++-- gcc/cp/pt.cc | 30 ++++++++++++------------------ 4 files changed, 20 insertions(+), 25 deletions(-) (limited to 'gcc') diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 406a550..714b6d5 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -1636,7 +1636,7 @@ check_constraint_info (tree t) holds the set of template parameters that were in-scope when this 'auto' was formed. */ #define PLACEHOLDER_TYPE_CONSTRAINTS_INFO(NODE) \ - DECL_SIZE_UNIT (TYPE_NAME (NODE)) + DECL_SIZE_UNIT (TYPE_NAME (TEMPLATE_TYPE_PARM_CHECK (NODE))) /* The constraints on the 'auto' placeholder type NODE. */ #define PLACEHOLDER_TYPE_CONSTRAINTS(NODE) \ @@ -5084,7 +5084,7 @@ get_vec_init_expr (tree t) templates are primary, too. */ /* Returns the primary template corresponding to these parameters. */ -#define TPARMS_PRIMARY_TEMPLATE(NODE) (TREE_TYPE (NODE)) +#define TPARMS_PRIMARY_TEMPLATE(NODE) (TREE_TYPE (TREE_VEC_CHECK (NODE))) #define DECL_PRIMARY_TEMPLATE(NODE) \ (TPARMS_PRIMARY_TEMPLATE (DECL_INNERMOST_TEMPLATE_PARMS (NODE))) @@ -6098,7 +6098,7 @@ const unsigned int STF_STRIP_DEPENDENT = 1U << 1; #define TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL(NODE) \ ((TREE_CODE (NODE) == BOUND_TEMPLATE_TEMPLATE_PARM) \ ? TYPE_TI_TEMPLATE (NODE) \ - : TYPE_NAME (NODE)) + : TYPE_NAME (TEMPLATE_TEMPLATE_PARM_CHECK (NODE))) /* in lex.cc */ diff --git a/gcc/cp/cxx-pretty-print.cc b/gcc/cp/cxx-pretty-print.cc index 4cda27f..950295e 100644 --- a/gcc/cp/cxx-pretty-print.cc +++ b/gcc/cp/cxx-pretty-print.cc @@ -1364,8 +1364,9 @@ cxx_pretty_printer::simple_type_specifier (tree t) case TEMPLATE_PARM_INDEX: case BOUND_TEMPLATE_TEMPLATE_PARM: pp_cxx_unqualified_id (this, t); - if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t)) - pp_cxx_constrained_type_spec (this, c); + if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) + if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t)) + pp_cxx_constrained_type_spec (this, c); break; case TYPENAME_TYPE: diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index a5d8889..1cfa4f1 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -639,8 +639,8 @@ dump_type (cxx_pretty_printer *pp, tree t, int flags) pp_cxx_cv_qualifier_seq (pp, t); if (template_placeholder_p (t)) { - t = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t)); - pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t)); + tree tmpl = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t)); + pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (tmpl)); pp_string (pp, "<...auto...>"); } else if (TYPE_IDENTIFIER (t)) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index de95e12..bb4e275 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -4987,9 +4987,7 @@ outer_template_args (tree tmpl) return args; if (TMPL_ARGS_DEPTH (args) == 1) return NULL_TREE; - args = copy_node (args); - --TREE_VEC_LENGTH (args); - return args; + return strip_innermost_template_args (args, 1); } /* Update the declared TYPE by doing any lookups which were thought to be @@ -28634,14 +28632,13 @@ type_dependent_expression_p_push (tree expr) bool any_type_dependent_arguments_p (const vec *args) { - unsigned int i; - tree arg; + if (!processing_template_decl || !args) + return false; + + for (tree arg : *args) + if (type_dependent_expression_p (arg)) + return true; - FOR_EACH_VEC_SAFE_ELT (args, i, arg) - { - if (type_dependent_expression_p (arg)) - return true; - } return false; } @@ -28804,19 +28801,16 @@ any_template_arguments_need_structural_equality_p (tree args) bool any_dependent_template_arguments_p (const_tree args) { - int i; - int j; - - if (!args) - return false; if (args == error_mark_node) return true; + if (!processing_template_decl || !args) + return false; - for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) + for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i) { const_tree level = TMPL_ARGS_LEVEL (args, i + 1); - for (j = 0; j < TREE_VEC_LENGTH (level); ++j) - if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) + for (tree arg : tree_vec_range (CONST_CAST_TREE (level))) + if (dependent_template_arg_p (arg)) return true; } -- cgit v1.1