Commit 64b83f2a by Steven Grady Committed by Jason Merrill

rtti.c (build_dynamic_cast_1): Improve diagnostics.

        * rtti.c (build_dynamic_cast_1): Improve diagnostics.

Co-Authored-By: Jason Merrill <jason@casey.cygnus.com>

From-SVN: r32599
parent d05165c3
2000-03-16 Steven Grady <grady@digitaldeck.com>
Jason Merrill <jason@casey.cygnus.com>
* rtti.c (build_dynamic_cast_1): Improve diagnostics.
2000-03-16 Nathan Sidwell <nathan@codesourcery.com> 2000-03-16 Nathan Sidwell <nathan@codesourcery.com>
* decl2.c (grokfield): Bail out if type is error_mark_node. * decl2.c (grokfield): Bail out if type is error_mark_node.
......
...@@ -548,69 +548,98 @@ build_dynamic_cast_1 (type, expr) ...@@ -548,69 +548,98 @@ build_dynamic_cast_1 (type, expr)
{ {
enum tree_code tc = TREE_CODE (type); enum tree_code tc = TREE_CODE (type);
tree exprtype; tree exprtype;
enum tree_code ec;
tree dcast_fn; tree dcast_fn;
tree old_expr = expr; tree old_expr = expr;
char* errstr = NULL;
if (TREE_CODE (expr) == OFFSET_REF) /* T shall be a pointer or reference to a complete class type, or
expr = resolve_offset_ref (expr); `pointer to cv void''. */
exprtype = TREE_TYPE (expr);
assert (exprtype != NULL_TREE);
ec = TREE_CODE (exprtype);
switch (tc) switch (tc)
{ {
case POINTER_TYPE: case POINTER_TYPE:
if (ec == REFERENCE_TYPE) if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
{
expr = convert_from_reference (expr);
exprtype = TREE_TYPE (expr);
ec = TREE_CODE (exprtype);
}
if (ec != POINTER_TYPE)
goto fail;
if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
goto fail;
if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE)
goto fail;
if (!at_least_as_qualified_p (TREE_TYPE (type),
TREE_TYPE (exprtype)))
goto fail;
if (TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
break; break;
/* else fall through */
case REFERENCE_TYPE: case REFERENCE_TYPE:
if (TREE_CODE (TREE_TYPE (type)) != RECORD_TYPE) if (! IS_AGGR_TYPE (TREE_TYPE (type)))
goto fail; {
errstr = "target is not pointer or reference to class";
goto fail;
}
if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE) if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
goto fail; {
errstr = "target is not pointer or reference to complete type";
goto fail;
}
break; break;
/* else fall through */
default: default:
errstr = "target is not pointer or reference";
goto fail; goto fail;
} }
/* Apply trivial conversion T -> T& for dereferenced ptrs. */ if (TREE_CODE (expr) == OFFSET_REF)
if (ec == RECORD_TYPE) expr = resolve_offset_ref (expr);
exprtype = TREE_TYPE (expr);
assert (exprtype != NULL_TREE);
if (tc == POINTER_TYPE)
expr = convert_from_reference (expr);
else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
{ {
/* Apply trivial conversion T -> T& for dereferenced ptrs. */
exprtype = build_reference_type (exprtype); exprtype = build_reference_type (exprtype);
expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT, expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
LOOKUP_NORMAL, NULL_TREE); LOOKUP_NORMAL, NULL_TREE);
ec = REFERENCE_TYPE;
} }
if (tc == REFERENCE_TYPE) exprtype = TREE_TYPE (expr);
if (tc == POINTER_TYPE)
{
/* If T is a pointer type, v shall be an rvalue of a pointer to
complete class type, and the result is an rvalue of type T. */
if (TREE_CODE (exprtype) != POINTER_TYPE)
{
errstr = "source is not a pointer";
goto fail;
}
if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
{
errstr = "source is not a pointer to class";
goto fail;
}
if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE)
{
errstr = "source is a pointer to incomplete type";
goto fail;
}
}
else
{ {
if (ec != REFERENCE_TYPE) /* T is a reference type, v shall be an lvalue of a complete class
goto fail; type, and the result is an lvalue of the type referred to by T. */
if (TREE_CODE (TREE_TYPE (exprtype)) != RECORD_TYPE)
goto fail; if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
{
errstr = "source is not of class type";
goto fail;
}
if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE) if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype))) == NULL_TREE)
goto fail; {
if (!at_least_as_qualified_p (TREE_TYPE (type), errstr = "source is of incomplete class type";
TREE_TYPE (exprtype))) goto fail;
goto fail; }
}
/* The dynamic_cast operator shall not cast away constness. */
if (!at_least_as_qualified_p (TREE_TYPE (type),
TREE_TYPE (exprtype)))
{
errstr = "conversion casts away constness";
goto fail;
} }
/* If *type is an unambiguous accessible base class of *exprtype, /* If *type is an unambiguous accessible base class of *exprtype,
...@@ -669,7 +698,7 @@ build_dynamic_cast_1 (type, expr) ...@@ -669,7 +698,7 @@ build_dynamic_cast_1 (type, expr)
/* If we got here, we can't convert statically. Therefore, /* If we got here, we can't convert statically. Therefore,
dynamic_cast<D&>(b) (b an object) cannot succeed. */ dynamic_cast<D&>(b) (b an object) cannot succeed. */
if (ec == REFERENCE_TYPE) if (tc == REFERENCE_TYPE)
{ {
if (TREE_CODE (old_expr) == VAR_DECL if (TREE_CODE (old_expr) == VAR_DECL
&& TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE) && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
...@@ -717,7 +746,7 @@ build_dynamic_cast_1 (type, expr) ...@@ -717,7 +746,7 @@ build_dynamic_cast_1 (type, expr)
tree expr2 = build_headof (expr1); tree expr2 = build_headof (expr1);
tree td1 = expr; tree td1 = expr;
if (ec == POINTER_TYPE) if (tc == POINTER_TYPE)
td1 = build_indirect_ref (td1, NULL_PTR); td1 = build_indirect_ref (td1, NULL_PTR);
td1 = get_tinfo_decl_dynamic (td1); td1 = get_tinfo_decl_dynamic (td1);
...@@ -802,13 +831,12 @@ build_dynamic_cast_1 (type, expr) ...@@ -802,13 +831,12 @@ build_dynamic_cast_1 (type, expr)
return ifnonnull (expr, result); return ifnonnull (expr, result);
} }
} }
else
cp_error ("dynamic_cast from non-polymorphic type `%#T'", exprtype); errstr = "source type is not polymorphic";
return error_mark_node;
fail: fail:
cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T'", cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
expr, exprtype, type); expr, exprtype, type, errstr);
return error_mark_node; return error_mark_node;
} }
...@@ -1275,7 +1303,7 @@ tinfo_base_init (desc, target) ...@@ -1275,7 +1303,7 @@ tinfo_base_init (desc, target)
init = tree_cons (NULL_TREE, decay_conversion (name_string), init); init = tree_cons (NULL_TREE, decay_conversion (name_string), init);
init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse(init)); init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1; TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
init = tree_cons (NULL_TREE, init, NULL_TREE); init = tree_cons (NULL_TREE, init, NULL_TREE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment