aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/call.c
diff options
context:
space:
mode:
authorMark Mitchell <mark@codesourcery.com>2004-01-04 22:42:22 +0000
committerMark Mitchell <mmitchel@gcc.gnu.org>2004-01-04 22:42:22 +0000
commit4f8163b19cd1e0b4e5d3e10426d08ad1e54cf1ed (patch)
tree2f162239ad6abe8e01544300565821a85d22ac4b /gcc/cp/call.c
parent5a4b3afd3de39682fec1cc2a5d3fb94b6df972c7 (diff)
downloadgcc-4f8163b19cd1e0b4e5d3e10426d08ad1e54cf1ed.zip
gcc-4f8163b19cd1e0b4e5d3e10426d08ad1e54cf1ed.tar.gz
gcc-4f8163b19cd1e0b4e5d3e10426d08ad1e54cf1ed.tar.bz2
re PR c++/12226 (g++ fails to enforce accessibility requirement for copy constructor)
PR c++/12226 * call.c (CHECK_COPY_CONSTRUCTOR_P): New macro. (reference_binding): Set it when appropriate. (build_temp): New function, split out from ... (convert_like_real): ... here. Honor CHECK_COPY_CONSTRUCTOR_P. (initialize_reference): Likewise. PR c++/12226 * g++.dg/init/copy7.c: New test. PR c++/12226 * testsuite/27_io/basic_filebuf/4.cc: Remove use of invalid copy constructor. * testsuite/27_io/basic_fstream/4.cc: Likewise. * testsuite/27_io/basic_ifstream/4.cc: Likewise. * testsuite/27_io/basic_ios/4.cc: Likewise. * testsuite/27_io/basic_iostream/4.cc: Likewise. * testsuite/27_io/basic_istream/4.cc: Likewise. * testsuite/27_io/basic_istingstream/4.cc: Likewise. * testsuite/27_io/basic_ofstream/4.cc: Likewise. * testsuite/27_io/basic_ostream/4.cc: Likewise. * testsuite/27_io/basic_ostringstream/4.cc: Likewise. * testsuite/27_io/basic_stringbuf/5.cc: Likewise. * testsuite/27_io/basic_stringstream/4.cc: Likewise. PR c++/13536 * parser.c (cp_parser): Add in_type_id_in_expr_p. (cp_parser_new): Initialize it. (cp_parser_postfix_expression): Set it. (cp_parser_sizeof_operand): Likewise. (cp_parser_parameteR_declaration): Do not commit early to tenative parsers when in_type_id_in_expr_p is set. PR c++/13536 * g++.dg/parse/cast1.C: New test. From-SVN: r75397
Diffstat (limited to 'gcc/cp/call.c')
-rw-r--r--gcc/cp/call.c124
1 files changed, 79 insertions, 45 deletions
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 5e75925..46ebca5 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -497,6 +497,10 @@ struct z_candidate GTY(()) {
should be created to hold the result of the conversion. */
#define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
+/* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
+ be accessible, even though it is not being used. */
+#define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
+
#define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
#define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
@@ -1176,7 +1180,9 @@ reference_binding (tree rto, tree rfrom, tree expr, int flags)
if (CLASS_TYPE_P (from) && compatible_p)
{
conv = build1 (IDENTITY_CONV, from, expr);
- return direct_reference_binding (rto, conv);
+ conv = direct_reference_binding (rto, conv);
+ CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
+ return conv;
}
/* [dcl.init.ref]
@@ -3952,6 +3958,34 @@ enforce_access (tree basetype_path, tree decl)
return true;
}
+/* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
+ bitwise or of LOOKUP_* values. If any errors are warnings are
+ generated, set *DIAGNOSTIC_FN to "error" or "warning",
+ respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
+ to NULL. */
+
+static tree
+build_temp (tree expr, tree type, int flags,
+ void (**diagnostic_fn)(const char *, ...))
+{
+ int savew, savee;
+
+ savew = warningcount, savee = errorcount;
+ expr = build_special_member_call (NULL_TREE,
+ complete_ctor_identifier,
+ build_tree_list (NULL_TREE, expr),
+ TYPE_BINFO (type),
+ flags);
+ if (warningcount > savew)
+ *diagnostic_fn = warning;
+ else if (errorcount > savee)
+ *diagnostic_fn = error;
+ else
+ *diagnostic_fn = NULL;
+ return expr;
+}
+
+
/* Perform the conversions in CONVS on the expression EXPR. FN and
ARGNUM are used for diagnostics. ARGNUM is zero based, -1
indicates the `this' argument of a method. INNER is nonzero when
@@ -3964,9 +3998,8 @@ static tree
convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
bool issue_conversion_warnings)
{
- int savew, savee;
-
tree totype = TREE_TYPE (convs);
+ void (*diagnostic_fn)(const char *, ...);
if (ICS_BAD_FLAG (convs)
&& TREE_CODE (convs) != USER_CONV
@@ -4038,35 +4071,24 @@ convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
if (IS_AGGR_TYPE (totype)
&& (inner >= 0 || !lvalue_p (expr)))
{
- savew = warningcount, savee = errorcount;
- expr = build_special_member_call
- (NULL_TREE, complete_ctor_identifier,
- build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
- /* Core issue 84, now a DR, says that we don't allow UDCs
- for these args (which deliberately breaks copy-init of an
- auto_ptr<Base> from an auto_ptr<Derived>). */
- LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
-
- /* Tell the user where this failing constructor call came from. */
- if (fn)
+ expr = (build_temp
+ (expr, totype,
+ /* Core issue 84, now a DR, says that we don't
+ allow UDCs for these args (which deliberately
+ breaks copy-init of an auto_ptr<Base> from an
+ auto_ptr<Derived>). */
+ LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
+ &diagnostic_fn));
+
+ if (diagnostic_fn)
{
- if (warningcount > savew)
- warning
+ if (fn)
+ diagnostic_fn
(" initializing argument %P of `%D' from result of `%D'",
argnum, fn, convfn);
- else if (errorcount > savee)
- error
- (" initializing argument %P of `%D' from result of `%D'",
- argnum, fn, convfn);
- }
- else
- {
- if (warningcount > savew)
- warning (" initializing temporary from result of `%D'",
- convfn);
- else if (errorcount > savee)
- error (" initializing temporary from result of `%D'",
- convfn);
+ else
+ diagnostic_fn
+ (" initializing temporary from result of `%D'", convfn);
}
expr = build_cplus_new (totype, expr);
}
@@ -4081,7 +4103,13 @@ convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
if (inner >= 0
&& TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
expr = decl_constant_value (expr);
- return expr;
+ if (CHECK_COPY_CONSTRUCTOR_P (convs))
+ /* Generate a temporary copy purely to generate the required
+ diagnostics. */
+ build_temp (build_dummy_object (totype), totype,
+ LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
+ &diagnostic_fn);
+ return expr;
case AMBIG_CONV:
/* Call build_user_type_conversion again for the error. */
return build_user_type_conversion
@@ -4108,11 +4136,17 @@ convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
{
/* We are going to bind a reference directly to a base-class
subobject of EXPR. */
- tree base_ptr = build_pointer_type (totype);
-
+ if (CHECK_COPY_CONSTRUCTOR_P (convs))
+ /* Generate a temporary copy purely to generate the required
+ diagnostics. */
+ build_temp (build_dummy_object (TREE_TYPE (expr)),
+ TREE_TYPE (expr),
+ LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
+ &diagnostic_fn);
/* Build an expression for `*((base*) &expr)'. */
expr = build_unary_op (ADDR_EXPR, expr, 0);
- expr = perform_implicit_conversion (base_ptr, expr);
+ expr = perform_implicit_conversion (build_pointer_type (totype),
+ expr);
expr = build_indirect_ref (expr, "implicit conversion");
return expr;
}
@@ -4120,18 +4154,10 @@ convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
/* Copy-initialization where the cv-unqualified version of the source
type is the same class as, or a derived class of, the class of the
destination [is treated as direct-initialization]. [dcl.init] */
- savew = warningcount, savee = errorcount;
- expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
- build_tree_list (NULL_TREE, expr),
- TYPE_BINFO (totype),
- LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
- if (fn)
- {
- if (warningcount > savew)
- warning (" initializing argument %P of `%D'", argnum, fn);
- else if (errorcount > savee)
- error (" initializing argument %P of `%D'", argnum, fn);
- }
+ expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
+ &diagnostic_fn);
+ if (diagnostic_fn && fn)
+ diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
return build_cplus_new (totype, expr);
case REF_BIND:
@@ -6205,6 +6231,14 @@ initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
remember that the conversion was required. */
if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
{
+ void (*diagnostic_fn) (const char *, ...);
+ if (CHECK_COPY_CONSTRUCTOR_P (conv))
+ /* Generate a temporary copy purely to generate the required
+ diagnostics. */
+ build_temp (build_dummy_object (TREE_TYPE (expr)),
+ TREE_TYPE (expr),
+ LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
+ &diagnostic_fn);
base_conv_type = TREE_TYPE (conv);
conv = TREE_OPERAND (conv, 0);
}