@@ -26107,10 +26107,12 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2610726107 new_con->variant.address.offset -=
2610826108 (a_targ_ptrdiff_t)prev_bcp->offset;
2610926109 if (type_is(tp, tk_void) ||
26110- same_entities(tp, prev_bcp->derived_class)) {
26110+ (same_entities(tp, prev_bcp->derived_class) &&
26111+ prev_bcp->has_public_derivation)) {
2611126112 /* A cast to the most-derived class (either by casting to void*
2611226113 or because the destination type happens to match the most-
26113- derived class type). */
26114+ derived class type, of which the operand is a public base
26115+ class subobject). */
2611426116 remove_trailing_subobject_path_entry(new_con);
2611526117 clear_runtime_constant_address(result_storage, new_con);
2611626118 goto done;
@@ -26123,7 +26125,28 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2612326125 bcdp = bcdp->next;
2612426126 for (; dsp != tail->next; dsp = dsp->next) {
2612526127 if (same_entities(dsp->base_class->type, tp)) {
26126- if (new_bcp == NULL) {
26128+ a_derivation_step_ptr step;
26129+ a_type_ptr step_class = dsp->base_class->type;
26130+ a_boolean is_public_base = TRUE;
26131+ /* The operand must be a public base class subobject of the
26132+ destination subobject (but not necessarily of the
26133+ most-derived object). The base classes of the derivation
26134+ steps are relative to the most-derived class, so check the
26135+ corresponding direct base classes instead. */
26136+ for (step = dsp->next; step != tail->next;
26137+ step = step->next) {
26138+ a_base_class_ptr direct_bcp =
26139+ find_direct_base_class_of(
26140+ step_class, step->base_class->type);
26141+ if (!direct_bcp->has_public_derivation) {
26142+ is_public_base = FALSE;
26143+ } /* if */
26144+ step_class = step->base_class->type;
26145+ } /* for */
26146+ if (!is_public_base) {
26147+ /* Ignore this derivation path. Another path may reach
26148+ the same virtual base class subobject publicly. */
26149+ } else if (new_bcp == NULL) {
2612726150 new_bcp = dsp->base_class;
2612826151 } else if (dsp->base_class != new_bcp) {
2612926152 /* The subobject on the derivation path is not unambiguous.
@@ -26149,7 +26172,11 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2614926172 &expr->position, new_bcp->type, ips);
2615026173 } /* if */
2615126174 goto done;
26152- } else if (!is_accessible_base_class(new_bcp)) {
26175+ } else if (!prev_bcp->has_public_derivation ||
26176+ !new_bcp->has_public_derivation) {
26177+ /* The operand must be a public base class subobject of the
26178+ most-derived object, and the destination a public base
26179+ class of the most-derived type. */
2615326180 if (pointer_case) {
2615426181 clear_address(result_storage, NULL);
2615526182 } else {
@@ -26199,6 +26226,7 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2619926226 a_base_class_ptr bcp, baseward_bcp;
2620026227 a_type_ptr dtp;
2620126228 a_byte *subobj = opnd_addr->address;
26229+ a_boolean is_public_base = TRUE;
2620226230 /* Check for the null address case first. */
2620326231 if (subobj == NULL) {
2620426232 /* Casting a null pointer value results in a null pointer value. */
@@ -26215,8 +26243,8 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2621526243 do_constexpr_fail(result);
2621626244 goto done;
2621726245 } /* if */
26218- if (type_is(tp, tk_void) &&
26219- (baseward_bcp = find_base_in_type(opnd_type, tp))!= NULL) {
26246+ if (! type_is(tp, tk_void) &&
26247+ (baseward_bcp = find_base_in_type(opnd_type, tp)) != NULL) {
2622026248 /* The dynamic cast is actually a (static) derived-to-base cast. */
2622126249 if (baseward_bcp->ambiguous) {
2622226250 do_constexpr_fail(result);
@@ -26240,13 +26268,17 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2624026268 a_byte_count offset;
2624126269 get_mapped_byte_count(&persistent_map, bcp, offset);
2624226270 result_addr->address -= offset;
26271+ if (!bcp->has_public_derivation) is_public_base = FALSE;
2624326272 dtp = bcp->derived_class;
2624426273 if (same_entities(dtp, tp)) {
2624526274 /* We found a derived subobject of the right type. Since types with
2624626275 virtual bases cannot be literal types (and thus cannot be stored
2624726276 in interpreter memory), there is no concern about this being
26248- ambiguous. */
26249- goto done;
26277+ ambiguous. The cast succeeds only if the operand is a public base
26278+ class subobject of it; otherwise, it also is not a public base
26279+ class subobject of the most-derived object, and so the cast fails
26280+ below. */
26281+ if (is_public_base) goto done;
2625026282 } /* if */
2625126283 if (is_initialized(result_addr)) {
2625226284 bcp = *(a_base_class_ptr*)result_addr->address;
@@ -26263,11 +26295,13 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2626326295 goto done;
2626426296 } /* if */
2626526297 /* We've reached the most derived type (dtp) without encountering the
26266- destination type. This might still be a "sideways" cast. */
26298+ destination type as a class of which the operand is a public base
26299+ class subobject. This might still be a "sideways" cast. */
2626726300 baseward_bcp = find_base_in_type(dtp, tp);
2626826301 if (baseward_bcp != NULL) {
2626926302 /* There is a subobject of the right kind: Verify that it is
26270- unambiguous and accessible. */
26303+ unambiguous and that it and the operand are public base class
26304+ subobjects of the most-derived object. */
2627126305 if (baseward_bcp->ambiguous) {
2627226306 if (pointer_case) {
2627326307 clear_address(result_storage, NULL);
@@ -26277,7 +26311,7 @@ represented by an entry of type a_constant (ck_address or ck_integer).
2627726311 &expr->position, tp, ips);
2627826312 } /* if */
2627926313 goto done;
26280- } else if (!is_accessible_base_class( baseward_bcp) ) {
26314+ } else if (!is_public_base || ! baseward_bcp->has_public_derivation ) {
2628126315 if (pointer_case) {
2628226316 clear_address(result_storage, NULL);
2628326317 } else {
0 commit comments