Annotation of gcc/cp-cvt.c, revision 1.1.1.3

1.1       root        1: /* Language-level data type conversion for GNU C++.
                      2:    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
                      3:    Hacked by Michael Tiemann ([email protected])
                      4: 
                      5: This file is part of GNU CC.
                      6: 
                      7: GNU CC is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU General Public License as published by
                      9: the Free Software Foundation; either version 2, or (at your option)
                     10: any later version.
                     11: 
                     12: GNU CC is distributed in the hope that it will be useful,
                     13: but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: GNU General Public License for more details.
                     16: 
                     17: You should have received a copy of the GNU General Public License
                     18: along with GNU CC; see the file COPYING.  If not, write to
                     19: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
                     20: 
                     21: 
                     22: /* This file contains the functions for converting C expressions
                     23:    to different data types.  The only entry point is `convert'.
                     24:    Every language front end must have a `convert' function
                     25:    but what kind of conversions it does will depend on the language.  */
                     26: 
                     27: #include "config.h"
                     28: #include "tree.h"
                     29: #include "cp-tree.h"
                     30: #include "cp-class.h"
                     31: #include "assert.h"
                     32: 
                     33: #define NULL 0
                     34: 
                     35: extern int flag_int_enum_equivalence;
                     36: 
                     37: /* Change of width--truncation and extension of integers or reals--
                     38:    is represented with NOP_EXPR.  Proper functioning of many things
                     39:    assumes that no other conversions can be NOP_EXPRs.
                     40: 
                     41:    Conversion between integer and pointer is represented with CONVERT_EXPR.
                     42:    Converting integer to real uses FLOAT_EXPR
                     43:    and real to integer uses FIX_TRUNC_EXPR.
                     44: 
                     45:    Here is a list of all the functions that assume that widening and
                     46:    narrowing is always done with a NOP_EXPR:
                     47:      In c-convert.c, convert_to_integer.
                     48:      In c-typeck.c, build_binary_op_nodefault (boolean ops),
                     49:         and truthvalue_conversion.
                     50:      In expr.c: expand_expr, for operands of a MULT_EXPR.
                     51:      In fold-const.c: fold.
                     52:      In tree.c: get_narrower and get_unwidened.
                     53: 
                     54:    C++: in multiple-inheritance, converting between pointers may involve
                     55:    adjusting them by a delta stored within the class definition.  */
                     56: 
                     57: /* Subroutines of `convert'.  */
                     58: 
                     59: static tree
                     60: convert_to_pointer (type, expr)
                     61:      tree type, expr;
                     62: {
                     63:   register tree intype = TREE_TYPE (expr);
                     64:   register enum tree_code form = TREE_CODE (intype);
                     65:   
                     66:   if (integer_zerop (expr))
                     67:     {
                     68:       if (type == TREE_TYPE (null_pointer_node))
                     69:        return null_pointer_node;
                     70:       expr = build_int_2 (0, 0);
                     71:       TREE_TYPE (expr) = type;
                     72:       return expr;
                     73:     }
                     74: 
                     75:   if (form == POINTER_TYPE)
                     76:     {
                     77:       intype = TYPE_MAIN_VARIANT (intype);
                     78: 
                     79:       if (TYPE_MAIN_VARIANT (type) != intype
1.1.1.2   root       80:          && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
                     81:          && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
1.1       root       82:        {
                     83:          enum tree_code code = PLUS_EXPR;
                     84:          tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
                     85:          if (binfo == error_mark_node)
                     86:            return error_mark_node;
                     87:          if (binfo == NULL_TREE)
                     88:            {
                     89:              binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
                     90:              if (binfo == error_mark_node)
                     91:                return error_mark_node;
                     92:              code = MINUS_EXPR;
                     93:            }
                     94:          if (binfo)
                     95:            {
                     96:              if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
                     97:                  || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
                     98:                  || ! BINFO_OFFSET_ZEROP (binfo))
                     99:                {
                    100:                  /* Need to get the path we took.  */
                    101:                  tree path;
                    102: 
                    103:                  if (code == PLUS_EXPR)
                    104:                    get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
                    105:                  else
                    106:                    get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
                    107:                  return build_vbase_path (code, type, expr, path, 0);
                    108:                }
                    109:            }
                    110:        }
                    111:       return build1 (NOP_EXPR, type, expr);
                    112:     }
                    113: 
                    114:   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
                    115:     {
                    116:       if (type_precision (intype) == POINTER_SIZE)
                    117:        return build1 (CONVERT_EXPR, type, expr);
                    118:       return convert_to_pointer (type,
                    119:                                 convert (type_for_size (POINTER_SIZE, 0),
                    120:                                          expr));
                    121:     }
                    122: 
                    123:   assert (form != OFFSET_TYPE);
                    124: 
                    125:   if (IS_AGGR_TYPE (intype))
                    126:     {
                    127:       /* If we cannot convert to the specific pointer type,
                    128:         try to convert to the type `void *'.  */
                    129:       tree rval;
                    130:       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
                    131:       if (rval)
                    132:        {
                    133:          if (rval == error_mark_node)
                    134:            error ("ambiguous pointer conversion");
                    135:          return rval;
                    136:        }
                    137:     }
                    138: 
                    139:   error ("cannot convert to a pointer type");
                    140: 
                    141:   return null_pointer_node;
                    142: }
                    143: 
                    144: /* Like convert, except permit conversions to take place which
                    145:    are not normally allowed due to visibility restrictions
                    146:    (such as conversion from sub-type to private super-type).  */
                    147: static tree
                    148: convert_to_pointer_force (type, expr)
                    149:      tree type, expr;
                    150: {
                    151:   register tree intype = TREE_TYPE (expr);
                    152:   register enum tree_code form = TREE_CODE (intype);
                    153:   
                    154:   if (integer_zerop (expr))
                    155:     {
                    156:       if (type == TREE_TYPE (null_pointer_node))
                    157:        return null_pointer_node;
                    158:       expr = build_int_2 (0, 0);
                    159:       TREE_TYPE (expr) = type;
                    160:       return expr;
                    161:     }
                    162: 
                    163:   if (form == POINTER_TYPE)
                    164:     {
                    165:       intype = TYPE_MAIN_VARIANT (intype);
                    166: 
                    167:       if (TYPE_MAIN_VARIANT (type) != intype
1.1.1.2   root      168:          && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
                    169:          && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
1.1       root      170:        {
                    171:          enum tree_code code = PLUS_EXPR;
                    172:          tree path;
                    173:          int distance = get_base_distance (TREE_TYPE (type),
                    174:                                            TREE_TYPE (intype), 0, &path);
                    175:          if (distance == -2)
                    176:            {
                    177:            ambig:
                    178:              error_with_aggr_type (TREE_TYPE (type), "type `%s' is ambiguous baseclass of `%s'",
                    179:                                    TYPE_NAME_STRING (TREE_TYPE (intype)));
                    180:              return error_mark_node;
                    181:            }
                    182:          if (distance == -1)
                    183:            {
                    184:              distance = get_base_distance (TREE_TYPE (intype),
                    185:                                            TREE_TYPE (type), 0, &path);
                    186:              if (distance == -2)
                    187:                goto ambig;
                    188:              if (distance < 0)
                    189:                /* Doesn't need any special help from us.  */
                    190:                return build1 (NOP_EXPR, type, expr);
                    191: 
                    192:              code = MINUS_EXPR;
                    193:            }
                    194:          return build_vbase_path (code, type, expr, path, 0);
                    195:        }
                    196:       return build1 (NOP_EXPR, type, expr);
                    197:     }
                    198: 
                    199:   return convert_to_pointer (type, expr);
                    200: }
                    201: 
                    202: /* We are passing something to a function which requires a reference.
                    203:    The type we are interested in is in TYPE. The initial
                    204:    value we have to begin with is in ARG.
                    205: 
                    206:    FLAGS controls how we manage visibility checking.  */
                    207: static tree
                    208: build_up_reference (type, arg, flags)
                    209:      tree type, arg;
                    210:      int flags;
                    211: {
                    212:   tree rval, targ;
                    213:   int literal_flag = 0;
                    214:   tree argtype = TREE_TYPE (arg), basetype = argtype;
                    215:   tree target_type = TREE_TYPE (type);
                    216:   tree binfo = NULL_TREE;
                    217: 
                    218:   assert (TREE_CODE (type) == REFERENCE_TYPE);
                    219:   if (flags != 0
                    220:       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
                    221:       && IS_AGGR_TYPE (argtype)
                    222:       && IS_AGGR_TYPE (target_type))
                    223:     {
                    224:       binfo = get_binfo (target_type, argtype,
                    225:                              (flags & LOOKUP_PROTECTED_OK) ? 3 : 2);
                    226:       if ((flags & LOOKUP_PROTECT) && binfo == error_mark_node)
                    227:        return error_mark_node;
                    228:       if (basetype == NULL_TREE)
                    229:        return error_not_base_type (target_type, argtype);
                    230:       basetype = BINFO_TYPE (binfo);
                    231:     }
                    232: 
1.1.1.3 ! root      233:   /* Pass along const and volatile down into the type. */
        !           234:   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
        !           235:     target_type = build_type_variant (target_type, TYPE_READONLY (type),
        !           236:                                      TYPE_VOLATILE (type));
1.1       root      237:   targ = arg;
                    238:   if (TREE_CODE (targ) == SAVE_EXPR)
                    239:     targ = TREE_OPERAND (targ, 0);
                    240: 
                    241:   switch (TREE_CODE (targ))
                    242:     {
                    243:     case INDIRECT_REF:
                    244:       /* This is a call to a constructor which did not know what it was
                    245:         initializing until now: it needs to initialize a temporary.  */
                    246:       if (TREE_HAS_CONSTRUCTOR (targ))
                    247:        {
                    248:          tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
                    249:          TREE_HAS_CONSTRUCTOR (targ) = 0;
                    250:          return build_up_reference (type, temp, flags);
                    251:        }
                    252:       /* Let &* cancel out to simplify resulting code.
                    253:          Also, throw away intervening NOP_EXPRs.  */
                    254:       arg = TREE_OPERAND (targ, 0);
                    255:       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
                    256:          || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
                    257:        arg = TREE_OPERAND (arg, 0);
                    258: 
1.1.1.3 ! root      259:       /* in doing a &*, we have to get rid of the const'ness on the pointer
        !           260:         value.  Haven't thought about volatile here.  Pointers come to mind
        !           261:         here.  */
        !           262:       if (TREE_READONLY (arg))
        !           263:        {
        !           264:          arg = copy_node (arg);
        !           265:          TREE_READONLY (arg) = 0;
        !           266:        }
        !           267: 
1.1       root      268:       rval = build1 (CONVERT_EXPR, type, arg);
                    269:       TREE_REFERENCE_EXPR (rval) = 1;
1.1.1.3 ! root      270: 
        !           271:       /* propagate the const flag on something like:
        !           272: 
        !           273:         class Base {
        !           274:         public:
        !           275:           int foo;
        !           276:         };
        !           277: 
        !           278:       class Derived : public Base {
        !           279:       public:
        !           280:        int bar;
        !           281:       };
        !           282: 
        !           283:       void func(Base&);
        !           284: 
        !           285:       void func2(const Derived& d) {
        !           286:        func(d);
        !           287:       }
        !           288: 
        !           289:         on the d parameter.  The below could have been avoided, if the flags
        !           290:         were down in the tree, not sure why they are not.  */
        !           291:       /* The below code may have to be propagated to other parts of this
        !           292:         switch.  */
        !           293:       if (TREE_READONLY (targ) && !TREE_READONLY (arg)
        !           294:          && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
        !           295:          && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
        !           296:        {
        !           297:          TREE_READONLY (arg) = TREE_READONLY (targ);
        !           298:        }
1.1       root      299:       literal_flag = TREE_CONSTANT (arg);
1.1.1.3 ! root      300: 
        !           301:       goto done_but_maybe_warn;
1.1       root      302: 
                    303:       /* Get this out of a register if we happened to be in one by accident.
                    304:         Also, build up references to non-lvalues it we must.  */
                    305:       /* For &x[y], return (&) x+y */
                    306:     case ARRAY_REF:
                    307:       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
                    308:        return error_mark_node;
                    309:       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
                    310:                              TREE_OPERAND (targ, 1));
                    311:       TREE_TYPE (rval) = type;
                    312:       if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
                    313:          && staticp (TREE_OPERAND (targ, 0)))
                    314:        TREE_CONSTANT (rval) = 1;
                    315:       goto done;
                    316: 
                    317:     case SCOPE_REF:
                    318:       /* Could be a reference to a static member.  */
                    319:       {
                    320:        tree field = TREE_OPERAND (targ, 1);
                    321:        if (TREE_STATIC (field))
                    322:          {
                    323:            rval = build1 (ADDR_EXPR, type, field);
                    324:            literal_flag = 1;
                    325:            goto done;
                    326:          }
                    327:       }
                    328:       /* we should have farmed out member pointers above.  */
                    329:       assert (0);
                    330: 
                    331:     case COMPONENT_REF:
                    332:       rval = build_component_addr (targ, build_pointer_type (argtype),
                    333:                                   "attempt to make a reference to bit-field structure member `%s'");
                    334:       TREE_TYPE (rval) = type;
                    335:       literal_flag = staticp (TREE_OPERAND (targ, 0));
1.1.1.3 ! root      336: 
1.1       root      337:       goto done_but_maybe_warn;
                    338: 
                    339:       /* Anything not already handled and not a true memory reference
                    340:         needs to have a reference built up.  Do so silently for
                    341:         things like integers and return values from function,
                    342:         but complain if we need a reference to something declared
                    343:         as `register'.  */
                    344: 
                    345:     case RESULT_DECL:
                    346:       if (staticp (targ))
                    347:        literal_flag = 1;
                    348:       TREE_ADDRESSABLE (targ) = 1;
                    349:       put_var_into_stack (targ);
                    350:       break;
                    351: 
                    352:     case PARM_DECL:
                    353:       if (targ == current_class_decl)
                    354:        {
                    355:          error ("address of `this' not available");
                    356:          TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
                    357:          put_var_into_stack (targ);
                    358:          break;
                    359:        }
                    360:       /* Fall through.  */
                    361:     case VAR_DECL:
                    362:     case CONST_DECL:
                    363:       if (TREE_REGDECL (targ) && !TREE_ADDRESSABLE (targ))
                    364:        warning ("address needed to build reference for `%s', which is declared `register'",
                    365:                 IDENTIFIER_POINTER (DECL_NAME (targ)));
                    366:       else if (staticp (targ))
                    367:        literal_flag = 1;
                    368: 
                    369:       TREE_ADDRESSABLE (targ) = 1;
                    370:       put_var_into_stack (targ);
                    371:       break;
                    372: 
                    373:     case COMPOUND_EXPR:
                    374:       {
                    375:        tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
                    376:                                                  LOOKUP_PROTECT);
                    377:        rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
                    378:        TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
                    379:        return rval;
                    380:       }
                    381: 
                    382:     case MODIFY_EXPR:
                    383:     case INIT_EXPR:
                    384:       {
                    385:        tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
                    386:                                                  LOOKUP_PROTECT);
                    387:        rval = build (COMPOUND_EXPR, type, arg, real_reference);
                    388:        TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
                    389:        return rval;
                    390:       }
                    391: 
                    392:     case COND_EXPR:
                    393:       return build (COND_EXPR, type,
                    394:                    TREE_OPERAND (targ, 0),
                    395:                    build_up_reference (type, TREE_OPERAND (targ, 1), LOOKUP_PROTECT),
                    396:                    build_up_reference (type, TREE_OPERAND (targ, 2), LOOKUP_PROTECT));
                    397: 
                    398:     case WITH_CLEANUP_EXPR:
                    399:       return build (WITH_CLEANUP_EXPR, type,
                    400:                    build_up_reference (type, TREE_OPERAND (targ, 0), LOOKUP_PROTECT),
                    401:                    0, TREE_OPERAND (targ, 2));
                    402: 
                    403:     case BIND_EXPR:
                    404:       arg = TREE_OPERAND (targ, 1);
                    405:       if (arg == NULL_TREE)
                    406:        {
                    407:          compiler_error ("({ ... }) expression not expanded when needed for reference");
                    408:          return error_mark_node;
                    409:        }
                    410:       rval = build1 (ADDR_EXPR, type, arg);
                    411:       TREE_REFERENCE_EXPR (rval) = 1;
                    412:       return rval;
                    413: 
                    414:     default:
                    415:       break;
                    416:     }
                    417: 
                    418:   if (TREE_ADDRESSABLE (targ) == 0)
                    419:     {
                    420:       tree temp;
                    421: 
                    422:       if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
                    423:        {
                    424:          temp = build_cplus_new (argtype, targ, 1);
                    425:          rval = build1 (ADDR_EXPR, type, temp);
                    426:          goto done;
                    427:        }
                    428:       else
                    429:        {
                    430:          temp = get_temp_name (argtype, 0);
                    431:          if (global_bindings_p ())
                    432:            {
                    433:              /* Give this new temp some rtl and initialize it.  */
                    434:              DECL_INITIAL (temp) = targ;
                    435:              TREE_STATIC (temp) = 1;
                    436:              finish_decl (temp, targ, NULL_TREE, 0);
                    437:              /* Do this after declaring it static.  */
                    438:              rval = build_unary_op (ADDR_EXPR, temp, 0);
                    439:              literal_flag = TREE_CONSTANT (rval);
                    440:              goto done;
                    441:            }
                    442:          else
                    443:            {
                    444:              rval = build_unary_op (ADDR_EXPR, temp, 0);
                    445:              /* Put a value into the rtl.  */
                    446:              if (IS_AGGR_TYPE (argtype))
                    447:                {
                    448:                  /* This may produce surprising results,
                    449:                     since we commit to initializing the temp
                    450:                     when the temp may not actually get used.  */
                    451:                  expand_aggr_init (temp, targ, 0);
                    452:                  TREE_TYPE (rval) = type;
                    453:                  literal_flag = TREE_CONSTANT (rval);
                    454:                  goto done;
                    455:                }
                    456:              else
                    457:                {
                    458:                  if (binfo && !BINFO_OFFSET_ZEROP (binfo))
                    459:                    rval = convert_pointer_to (target_type, rval);
                    460:                  else
                    461:                    TREE_TYPE (rval) = type;
                    462:                  return build (COMPOUND_EXPR, type,
                    463:                                build (MODIFY_EXPR, argtype, temp, arg), rval);
                    464:                }
                    465:            }
                    466:        }
                    467:     }
                    468:   else
                    469:     {
                    470:       if (TREE_CODE (arg) == SAVE_EXPR)
1.1.1.3 ! root      471:        my_friendly_abort (5);
1.1       root      472:       rval = build1 (ADDR_EXPR, type, arg);
                    473:     }
                    474: 
                    475:  done_but_maybe_warn:
                    476:   if (TREE_READONLY (arg)
                    477:       && ! TYPE_READONLY (target_type))
                    478:     readonly_warning_or_error (arg, "conversion to reference");
                    479: 
                    480:  done:
                    481:   if (TYPE_USES_COMPLEX_INHERITANCE (argtype))
                    482:     {
                    483:       TREE_TYPE (rval) = TYPE_POINTER_TO (argtype);
                    484:       rval = convert_pointer_to (target_type, rval);
                    485:       TREE_TYPE (rval) = type;
                    486:     }
                    487:   TREE_CONSTANT (rval) = literal_flag;
                    488:   return rval;
                    489: }
                    490: 
                    491: /* For C++: Only need to do one-level references, but cannot
                    492:    get tripped up on signed/unsigned differences.
                    493: 
                    494:    If DECL is NULL_TREE it means convert as though casting (by force).
                    495:    If it is ERROR_MARK_NODE, it means the conversion is implicit,
                    496:    and that temporaries may be created.
                    497:    Otherwise, DECL is a _DECL node which can be used in error reporting.  */
                    498: tree
                    499: convert_to_reference (decl, reftype, expr, strict, flags)
                    500:      tree decl;
                    501:      tree reftype, expr;
                    502:      int strict, flags;
                    503: {
                    504:   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
                    505:   register tree intype = TREE_TYPE (expr);
                    506:   register enum tree_code form = TREE_CODE (intype);
                    507: 
                    508:   if (form == REFERENCE_TYPE)
                    509:     intype = TREE_TYPE (intype);
                    510:   intype = TYPE_MAIN_VARIANT (intype);
                    511: 
                    512:   /* @@ Probably need to have a check for X(X&) here.  */
                    513: 
                    514:   if (IS_AGGR_TYPE (intype))
                    515:     {
                    516:       tree rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
                    517:       if (rval)
                    518:        {
                    519:          if (rval == error_mark_node)
                    520:            error ("ambiguous pointer conversion");
                    521:          return rval;
                    522:        }
1.1.1.3 ! root      523:       else if (type != intype
        !           524:               && (rval = build_type_conversion (CONVERT_EXPR, type, expr, 1)))
1.1       root      525:        {
1.1.1.3 ! root      526:          if (rval == error_mark_node)
        !           527:            return rval;
1.1       root      528:          if (TYPE_NEEDS_DESTRUCTOR (type))
                    529:            {
                    530:              rval = convert_to_reference (NULL_TREE, reftype, rval, strict, flags);
                    531:            }
                    532:          else
                    533:            {
                    534:              decl = get_temp_name (type, 0);
                    535:              rval = build (INIT_EXPR, type, decl, rval);
                    536:              rval = build (COMPOUND_EXPR, reftype, rval,
                    537:                            convert_to_reference (NULL_TREE, reftype, decl,
                    538:                                                  strict, flags));
                    539:            }
                    540:          return rval;
                    541:        }
                    542: 
                    543:       if (form == REFERENCE_TYPE
                    544:          && type != intype
                    545:          && TYPE_USES_COMPLEX_INHERITANCE (intype))
                    546:        {
                    547:          /* If it may move around, build a fresh reference.  */
                    548:          expr = convert_from_reference (expr);
                    549:          form = TREE_CODE (TREE_TYPE (expr));
                    550:        }
                    551:     }
                    552: 
                    553:   /* @@ Perhaps this should try to go through a constructor first
                    554:      @@ for proper initialization, but I am not sure when that
                    555:      @@ is needed or desirable.
                    556: 
                    557:      @@ The second disjunct is provided to make references behave
                    558:      @@ as some people think they should, i.e., an interconvertability
                    559:      @@ between references to builtin types (such as short and
                    560:      @@ unsigned short).  There should be no conversion between
                    561:      @@ types whose codes are different, or whose sizes are different.  */
                    562: 
                    563:   if (((IS_AGGR_TYPE (type) || IS_AGGR_TYPE (intype))
                    564:        && comptypes (type, intype, strict))
                    565:       || (!IS_AGGR_TYPE (type)
                    566:          && TREE_CODE (type) == TREE_CODE (intype)
                    567:          && int_size_in_bytes (type) == int_size_in_bytes (intype)))
                    568:     {
                    569:       /* If EXPR is of aggregate type, and is really a CALL_EXPR,
                    570:         then we don't need to convert it to reference type if
                    571:         it is only being used to initialize DECL which is also
                    572:         of the same aggregate type.  */
                    573:       if (form == REFERENCE_TYPE
                    574:          || (decl != NULL_TREE && decl != error_mark_node
                    575:              && IS_AGGR_TYPE (type)
                    576:              && TREE_CODE (expr) == CALL_EXPR
                    577:              && TYPE_MAIN_VARIANT (type) == intype))
                    578:        {
                    579:          if (decl && decl != error_mark_node)
                    580:            {
                    581:              tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
                    582:              tree e2;
                    583: 
                    584:              TREE_SIDE_EFFECTS (e1) = 1;
                    585:              if (form == REFERENCE_TYPE)
                    586:                e2 = build1 (NOP_EXPR, reftype, decl);
                    587:              else
                    588:                {
                    589:                  e2 = build_unary_op (ADDR_EXPR, decl, 0);
                    590:                  TREE_TYPE (e2) = reftype;
                    591:                  TREE_REFERENCE_EXPR (e2) = 1;
                    592:                }
                    593:              return build_compound_expr (tree_cons (NULL_TREE, e1,
                    594:                                                     build_tree_list (NULL_TREE, e2)));
                    595:            }
                    596:          expr = copy_node (expr);
                    597:          TREE_TYPE (expr) = reftype;
                    598:          return expr;
                    599:        }
                    600:       if (decl == error_mark_node)
                    601:        flags |= LOOKUP_PROTECTED_OK;
                    602:       return build_up_reference (reftype, expr, flags);
                    603:     }
                    604: 
                    605:   /* Definitely need to go through a constructor here.  */
                    606:   if (TYPE_HAS_CONSTRUCTOR (type))
                    607:     {
                    608:       tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, expr), TYPE_BINFO (type), LOOKUP_NORMAL);
                    609:       tree rval;
                    610: 
                    611:       if (init == error_mark_node)
                    612:        return error_mark_node;
                    613:       rval = build_cplus_new (type, init, 1);
                    614:       if (decl == error_mark_node)
                    615:        flags |= LOOKUP_PROTECTED_OK;
                    616:       return build_up_reference (reftype, rval, flags);
                    617:     }
                    618: 
                    619:   assert (form != OFFSET_TYPE);
                    620: 
                    621:   error ("cannot convert to a reference type");
                    622: 
                    623:   return error_mark_node;
                    624: }
                    625: 
                    626: /* We are using a reference VAL for its value. Bash that reference all the
                    627:    way down to its lowest form. */
                    628: tree
                    629: convert_from_reference (val)
                    630:      tree val;
                    631: {
                    632:   tree type = TREE_TYPE (val);
                    633: 
                    634:   if (TREE_CODE (type) == OFFSET_TYPE)
                    635:     type = TREE_TYPE (type);
                    636:  if (TREE_CODE (type) == REFERENCE_TYPE)
                    637:     {
                    638:       tree target_type = TREE_TYPE (type);
                    639: 
                    640:       /* This can happen if we cast to a reference type.  */
                    641:       if (TREE_CODE (val) == ADDR_EXPR)
                    642:        {
                    643:          val = build1 (NOP_EXPR, build_pointer_type (target_type), val);
                    644:          val = build_indirect_ref (val, 0);
                    645:          return val;
                    646:        }
                    647: 
                    648:       val = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (target_type), val);
                    649: 
                    650:       TREE_THIS_VOLATILE (val) = TYPE_VOLATILE (target_type);
                    651:       TREE_SIDE_EFFECTS (val) = TYPE_VOLATILE (target_type);
                    652:       TREE_READONLY (val) = TYPE_READONLY (target_type);
                    653:     }
                    654:   return val;
                    655: }
                    656: 
                    657: static tree
                    658: convert_to_real (type, expr)
                    659:      tree type, expr;
                    660: {
                    661:   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
                    662:   extern int flag_float_store;
                    663: 
                    664:   if (form == REAL_TYPE)
                    665:     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
                    666:                  type, expr);
                    667: 
                    668:   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
                    669:     return build1 (FLOAT_EXPR, type, expr);
                    670: 
                    671:   assert (form != OFFSET_TYPE);
                    672: 
                    673:   if (form == POINTER_TYPE)
                    674:     error ("pointer value used where a floating point value was expected");
                    675:   /* C++: check to see if we can convert this aggregate type
                    676:      into the required scalar type.  */
                    677:   else if (IS_AGGR_TYPE (TREE_TYPE (expr)))
                    678:     {
                    679:       tree rval;
                    680:       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
                    681:       if (rval)
                    682:        return rval;
                    683:       else
                    684:        error ("aggregate value used where a floating point value was expected");
                    685:     }
                    686: 
                    687:   {
                    688:     register tree tem = make_node (REAL_CST);
                    689:     TREE_TYPE (tem) = type;
                    690:     TREE_REAL_CST (tem) = 0;
                    691:     return tem;
                    692:   }
                    693: }
                    694: 
                    695: /* The result of this is always supposed to be a newly created tree node
                    696:    not in use in any existing structure.  */
                    697: 
                    698: static tree
                    699: convert_to_integer (type, expr)
                    700:      tree type, expr;
                    701: {
                    702:   register tree intype = TREE_TYPE (expr);
                    703:   register enum tree_code form = TREE_CODE (intype);
                    704:   extern tree build_binary_op_nodefault ();
                    705:   extern tree build_unary_op ();
                    706: 
                    707:   if (form == POINTER_TYPE)
                    708:     {
                    709:       if (integer_zerop (expr))
                    710:        expr = integer_zero_node;
                    711:       else
                    712:        expr = fold (build1 (CONVERT_EXPR,
                    713:                             type_for_size (POINTER_SIZE, 0), expr));
                    714:       intype = TREE_TYPE (expr);
                    715:       form = TREE_CODE (intype);
                    716:       if (intype == type)
                    717:        return expr;
                    718:     }
                    719: 
                    720:   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
                    721:     {
                    722:       register unsigned outprec = TYPE_PRECISION (type);
                    723:       register unsigned inprec = TYPE_PRECISION (intype);
                    724:       register enum tree_code ex_form = TREE_CODE (expr);
                    725: 
                    726:       if (flag_int_enum_equivalence == 0
                    727:          && TREE_CODE (type) == ENUMERAL_TYPE
                    728:          && form == INTEGER_TYPE)
                    729:        {
                    730:          extern int flag_pedantic_errors;
                    731: 
                    732:          if (pedantic)
                    733:            pedwarn ("anachronistic conversion from integer type to enumeral type `%s'",
                    734:                     TYPE_NAME_STRING (type));
                    735:          if (flag_pedantic_errors)
                    736:            return error_mark_node;
                    737:        }
                    738: 
                    739:       if (outprec >= inprec)
                    740:        return build1 (NOP_EXPR, type, expr);
                    741: 
                    742: /* Here detect when we can distribute the truncation down past some arithmetic.
                    743:    For example, if adding two longs and converting to an int,
                    744:    we can equally well convert both to ints and then add.
                    745:    For the operations handled here, such truncation distribution
                    746:    is always safe.
                    747:    It is desirable in these cases:
                    748:    1) when truncating down to full-word from a larger size
                    749:    2) when truncating takes no work.
                    750:    3) when at least one operand of the arithmetic has been extended
                    751:    (as by C's default conversions).  In this case we need two conversions
                    752:    if we do the arithmetic as already requested, so we might as well
                    753:    truncate both and then combine.  Perhaps that way we need only one.
                    754: 
                    755:    Note that in general we cannot do the arithmetic in a type
                    756:    shorter than the desired result of conversion, even if the operands
                    757:    are both extended from a shorter type, because they might overflow
                    758:    if combined in that type.  The exceptions to this--the times when
                    759:    two narrow values can be combined in their narrow type even to
                    760:    make a wider result--are handled by "shorten" in build_binary_op.  */
                    761: 
                    762:       switch (ex_form)
                    763:        {
                    764:        case RSHIFT_EXPR:
                    765:          /* We can pass truncation down through right shifting
                    766:             when the shift count is a negative constant.  */
                    767:          if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
                    768:              || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
                    769:            break;
                    770:          goto trunc1;
                    771: 
                    772:        case LSHIFT_EXPR:
                    773:          /* We can pass truncation down through left shifting
                    774:             when the shift count is a positive constant.  */
                    775:          if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
                    776:              || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
                    777:            break;
                    778:          /* In this case, shifting is like multiplication.  */
                    779:          goto trunc1;
                    780: 
                    781:        case MAX_EXPR:
                    782:        case MIN_EXPR:
                    783:        case MULT_EXPR:
                    784:          {
                    785:            tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
                    786:            tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
                    787: 
                    788:            /* Don't distribute unless the output precision is at least as big
                    789:               as the actual inputs.  Otherwise, the comparison of the
                    790:               truncated values will be wrong.  */
                    791:            if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
                    792:                && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
                    793:                /* If signedness of arg0 and arg1 don't match,
                    794:                   we can't necessarily find a type to compare them in.  */
                    795:                && (TREE_UNSIGNED (TREE_TYPE (arg0))
                    796:                    == TREE_UNSIGNED (TREE_TYPE (arg1))))
                    797:              goto trunc1;
                    798:            break;
                    799:          }
                    800: 
                    801:        case PLUS_EXPR:
                    802:        case MINUS_EXPR:
                    803:        case BIT_AND_EXPR:
                    804:        case BIT_IOR_EXPR:
                    805:        case BIT_XOR_EXPR:
                    806:        case BIT_ANDTC_EXPR:
                    807:        trunc1:
                    808:          {
                    809:            tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
                    810:            tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
                    811: 
                    812:            if (outprec >= BITS_PER_WORD
                    813:                || TRULY_NOOP_TRUNCATION (outprec, inprec)
                    814:                || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
                    815:                || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
                    816:              {
                    817:                /* Do the arithmetic in type TYPEX,
                    818:                   then convert result to TYPE.  */
                    819:                register tree typex = type;
                    820: 
                    821:                /* Can't do arithmetic in enumeral types
                    822:                   so use an integer type that will hold the values.  */
                    823:                if (TREE_CODE (typex) == ENUMERAL_TYPE)
                    824:                  typex = type_for_size (TYPE_PRECISION (typex),
                    825:                                         TREE_UNSIGNED (typex));
                    826: 
                    827:                /* But now perhaps TYPEX is as wide as INPREC.
                    828:                   In that case, do nothing special here.
                    829:                   (Otherwise would recurse infinitely in convert.  */
                    830:                if (TYPE_PRECISION (typex) != inprec)
                    831:                  {
                    832:                    /* Don't do unsigned arithmetic where signed was wanted,
                    833:                       or vice versa.
                    834:                       Exception: if the original operands were unsigned
                    835:                       then can safely do the work as unsigned.
                    836:                       And we may need to do it as unsigned
                    837:                       if we truncate to the original size.  */
                    838:                    typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
                    839:                              || TREE_UNSIGNED (TREE_TYPE (arg0)))
                    840:                             ? unsigned_type (typex) : signed_type (typex));
                    841:                    return convert (type,
                    842:                                    build_binary_op_nodefault (ex_form,
                    843:                                                               convert (typex, arg0),
                    844:                                                               convert (typex, arg1),
                    845:                                                               ex_form));
                    846:                  }
                    847:              }
                    848:          }
                    849:          break;
                    850: 
                    851:        case EQ_EXPR:
                    852:        case NE_EXPR:
                    853:        case GT_EXPR:
                    854:        case GE_EXPR:
                    855:        case LT_EXPR:
                    856:        case LE_EXPR:
                    857:        case TRUTH_AND_EXPR:
                    858:        case TRUTH_ANDIF_EXPR:
                    859:        case TRUTH_OR_EXPR:
                    860:        case TRUTH_ORIF_EXPR:
                    861:        case TRUTH_NOT_EXPR:
                    862:          /* If we want result of comparison converted to a byte,
                    863:             we can just regard it as a byte, since it is 0 or 1.  */
                    864:          TREE_TYPE (expr) = type;
                    865:          return expr;
                    866: 
                    867:        case NEGATE_EXPR:
                    868:        case BIT_NOT_EXPR:
                    869:        case ABS_EXPR:
                    870:          {
                    871:            register tree typex = type;
                    872: 
                    873:            /* Can't do arithmetic in enumeral types
                    874:               so use an integer type that will hold the values.  */
                    875:            if (TREE_CODE (typex) == ENUMERAL_TYPE)
                    876:              typex = type_for_size (TYPE_PRECISION (typex),
                    877:                                     TREE_UNSIGNED (typex));
                    878: 
                    879:            /* But now perhaps TYPEX is as wide as INPREC.
                    880:               In that case, do nothing special here.
                    881:               (Otherwise would recurse infinitely in convert.  */
                    882:            if (TYPE_PRECISION (typex) != inprec)
                    883:              {
                    884:                /* Don't do unsigned arithmetic where signed was wanted,
                    885:                   or vice versa.  */
                    886:                typex = (TREE_UNSIGNED (TREE_TYPE (expr))
                    887:                         ? unsigned_type (typex) : signed_type (typex));
                    888:                return convert (type,
                    889:                                build_unary_op (ex_form,
                    890:                                                convert (typex, TREE_OPERAND (expr, 0)),
                    891:                                                1));
                    892:              }
                    893:          }
                    894: 
                    895:        case NOP_EXPR:
                    896:          /* If truncating after truncating, might as well do all at once.
                    897:             If truncating after extending, we may get rid of wasted work.  */
                    898:          return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
                    899: 
                    900:        case COND_EXPR:
                    901:          /* Can treat the two alternative values like the operands
                    902:             of an arithmetic expression.  */
                    903:          {
                    904:            tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
                    905:            tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
                    906: 
                    907:            if (outprec >= BITS_PER_WORD
                    908:                || TRULY_NOOP_TRUNCATION (outprec, inprec)
                    909:                || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
                    910:                || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
                    911:              {
                    912:                /* Do the arithmetic in type TYPEX,
                    913:                   then convert result to TYPE.  */
                    914:                register tree typex = type;
                    915: 
                    916:                /* Can't do arithmetic in enumeral types
                    917:                   so use an integer type that will hold the values.  */
                    918:                if (TREE_CODE (typex) == ENUMERAL_TYPE)
                    919:                  typex = type_for_size (TYPE_PRECISION (typex),
                    920:                                         TREE_UNSIGNED (typex));
                    921: 
                    922:                /* But now perhaps TYPEX is as wide as INPREC.
                    923:                   In that case, do nothing special here.
                    924:                   (Otherwise would recurse infinitely in convert.  */
                    925:                if (TYPE_PRECISION (typex) != inprec)
                    926:                  {
                    927:                    /* Don't do unsigned arithmetic where signed was wanted,
                    928:                       or vice versa.  */
                    929:                    typex = (TREE_UNSIGNED (TREE_TYPE (expr))
                    930:                             ? unsigned_type (typex) : signed_type (typex));
                    931:                    return convert (type,
                    932:                                    fold (build (COND_EXPR, typex,
                    933:                                                 TREE_OPERAND (expr, 0),
                    934:                                                 convert (typex, arg1),
                    935:                                                 convert (typex, arg2))));
                    936:                  }
                    937:              }
                    938:          }
                    939: 
                    940:        }
                    941: 
                    942:       return build1 (NOP_EXPR, type, expr);
                    943:     }
                    944: 
                    945:   if (form == REAL_TYPE)
                    946:     return build1 (FIX_TRUNC_EXPR, type, expr);
                    947: 
                    948:   if (form == OFFSET_TYPE)
                    949:     error_with_decl (TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)),
                    950:                     "pointer-to-member expression object not composed with type `%s' object");
                    951:   else
                    952:     {
                    953:       if (IS_AGGR_TYPE (intype))
                    954:        {
                    955:          tree rval;
                    956:          rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
                    957:          if (rval) return rval;
                    958:        }
                    959: 
                    960:       error ("aggregate value used where an integer was expected");
                    961:     }
                    962: 
                    963:   {
                    964:     register tree tem = build_int_2 (0, 0);
                    965:     TREE_TYPE (tem) = type;
                    966:     return tem;
                    967:   }
                    968: }
                    969: 
                    970: /* See if there is a constructor of type TYPE which will convert
                    971:    EXPR.  The reference manual seems to suggest (8.5.6) that we need
                    972:    not worry about finding constructors for base classes, then converting
                    973:    to the derived class.
                    974: 
                    975:    MSGP is a pointer to a message that would be an appropriate error
                    976:    string.  If MSGP is NULL, then we are not interested in reporting
                    977:    errors.  */
                    978: tree
                    979: convert_to_aggr (type, expr, msgp, protect)
                    980:      tree type, expr;
                    981:      char **msgp;
                    982: {
                    983:   tree basetype = type;
                    984:   tree name = TYPE_IDENTIFIER (basetype);
                    985:   tree function, fndecl, fntype, parmtypes, parmlist, result;
                    986:   tree method_name;
                    987:   enum visibility_type visibility;
                    988:   int can_be_private, can_be_protected;
                    989: 
                    990:   if (! TYPE_HAS_CONSTRUCTOR (basetype))
                    991:     {
                    992:       if (msgp)
                    993:        *msgp = "type `%s' does not have a constructor";
                    994:       return error_mark_node;
                    995:     }
                    996: 
                    997:   visibility = visibility_public;
                    998:   can_be_private = 0;
                    999:   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
                   1000: 
                   1001:   parmlist = build_tree_list (NULL_TREE, expr);
                   1002:   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
                   1003: 
                   1004:   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
                   1005:     {
                   1006:       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
                   1007:       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
                   1008:     }
                   1009: 
                   1010:   /* The type of the first argument will be filled in inside the loop.  */
                   1011:   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
                   1012:   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
                   1013: 
1.1.1.2   root     1014:   method_name = build_decl_overload (name, parmtypes, 1);
1.1       root     1015: 
                   1016:   /* constructors are up front.  */
                   1017:   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
                   1018:   if (TYPE_HAS_DESTRUCTOR (basetype))
                   1019:     fndecl = DECL_CHAIN (fndecl);
                   1020: 
                   1021:   while (fndecl)
                   1022:     {
                   1023:       if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
                   1024:        {
                   1025:          function = fndecl;
                   1026:          if (protect)
                   1027:            {
                   1028:              if (TREE_PRIVATE (fndecl))
                   1029:                {
                   1030:                  can_be_private =
                   1031:                    (basetype == current_class_type
                   1032:                     || is_friend (basetype, current_function_decl)
                   1033:                     || purpose_member (basetype, DECL_VISIBILITY (fndecl)));
                   1034:                  if (! can_be_private)
                   1035:                    goto found;
                   1036:                }
                   1037:              else if (TREE_PROTECTED (fndecl))
                   1038:                {
                   1039:                  if (! can_be_protected)
                   1040:                    goto found;
                   1041:                }
                   1042:            }
                   1043:          goto found_and_ok;
                   1044:        }
                   1045:       fndecl = DECL_CHAIN (fndecl);
                   1046:     }
                   1047: 
                   1048:   /* No exact conversion was found.  See if an approximate
                   1049:      one will do.  */
                   1050:   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
                   1051:   if (TYPE_HAS_DESTRUCTOR (basetype))
                   1052:     fndecl = DECL_CHAIN (fndecl);
                   1053: 
                   1054:   {
                   1055:     int saw_private = 0;
                   1056:     int saw_protected = 0;
                   1057:     struct candidate *candidates =
                   1058:       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
                   1059:     struct candidate *cp = candidates;
                   1060: 
                   1061:     while (fndecl)
                   1062:       {
                   1063:        function = fndecl;
                   1064:        cp->harshness = (unsigned short *)alloca (3 * sizeof (short));
                   1065:        compute_conversion_costs (fndecl, parmlist, cp, 2);
                   1066:        if (cp->evil == 0)
                   1067:          {
                   1068:            cp->u.field = fndecl;
                   1069:            if (protect)
                   1070:              {
                   1071:                if (TREE_PRIVATE (fndecl))
                   1072:                  visibility = visibility_private;
                   1073:                else if (TREE_PROTECTED (fndecl))
                   1074:                  visibility = visibility_protected;
                   1075:                else
                   1076:                  visibility = visibility_public;
                   1077:              }
                   1078:            else
                   1079:              visibility = visibility_public;
                   1080: 
                   1081:            if (visibility == visibility_private
                   1082:                ? (basetype == current_class_type
                   1083:                   || is_friend (basetype, cp->function)
                   1084:                   || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
                   1085:                : visibility == visibility_protected
                   1086:                ? (can_be_protected
                   1087:                   || purpose_member (basetype, DECL_VISIBILITY (fndecl)))
                   1088:                : 1)
                   1089:              {
                   1090:                if (cp->user == 0 && cp->b_or_d == 0
                   1091:                    && cp->easy <= 1)
                   1092:                  {
                   1093:                    goto found_and_ok;
                   1094:                  }
                   1095:                cp++;
                   1096:              }
                   1097:            else
                   1098:              {
                   1099:                if (visibility == visibility_private)
                   1100:                  saw_private = 1;
                   1101:                else
                   1102:                  saw_protected = 1;
                   1103:              }
                   1104:          }
                   1105:        fndecl = DECL_CHAIN (fndecl);
                   1106:       }
                   1107:     if (cp - candidates)
                   1108:       {
                   1109:        /* Rank from worst to best.  Then cp will point to best one.
                   1110:           Private fields have their bits flipped.  For unsigned
                   1111:           numbers, this should make them look very large.
                   1112:           If the best alternate has a (signed) negative value,
                   1113:           then all we ever saw were private members.  */
                   1114:        if (cp - candidates > 1)
                   1115:          qsort (candidates,    /* char *base */
                   1116:                 cp - candidates, /* int nel */
                   1117:                 sizeof (struct candidate), /* int width */
                   1118:                 rank_for_overload); /* int (*compar)() */
                   1119: 
                   1120:        --cp;
                   1121:        if (cp->evil > 1)
                   1122:          {
                   1123:            if (msgp)
                   1124:              *msgp = "ambiguous type conversion possible for `%s'";
                   1125:            return error_mark_node;
                   1126:          }
                   1127: 
                   1128:        function = cp->function;
                   1129:        fndecl = cp->u.field;
                   1130:        goto found_and_ok;
                   1131:       }
                   1132:     else if (msgp)
                   1133:       {
                   1134:        if (saw_private)
                   1135:          if (saw_protected)
                   1136:            *msgp = "only private and protected conversions apply";
                   1137:          else
                   1138:            *msgp = "only private conversions apply";
                   1139:        else if (saw_protected)
                   1140:          *msgp = "only protected conversions apply";
                   1141:       }
                   1142:     return error_mark_node;
                   1143:   }
                   1144:   /* NOTREACHED */
                   1145: 
                   1146:  not_found:
                   1147:   if (msgp) *msgp = "no appropriate conversion to type `%s'";
                   1148:   return error_mark_node;
                   1149:  found:
                   1150:   if (visibility == visibility_private)
                   1151:     if (! can_be_private)
                   1152:       {
                   1153:        if (msgp)
                   1154:          *msgp = TREE_PRIVATE (fndecl)
                   1155:            ? "conversion to type `%s' is private"
                   1156:            : "conversion to type `%s' is from private base class";
                   1157:        return error_mark_node;
                   1158:       }
                   1159:   if (visibility == visibility_protected)
                   1160:     if (! can_be_protected)
                   1161:       {
                   1162:        if (msgp)
                   1163:          *msgp = TREE_PRIVATE (fndecl)
                   1164:            ? "conversion to type `%s' is protected"
                   1165:            : "conversion to type `%s' is from protected base class";
                   1166:        return error_mark_node;
                   1167:       }
                   1168:   function = fndecl;
                   1169:  found_and_ok:
                   1170: 
                   1171:   /* It will convert, but we don't do anything about it yet.  */
                   1172:   if (msgp == 0)
                   1173:     return NULL_TREE;
                   1174: 
                   1175:   fntype = TREE_TYPE (function);
                   1176:   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
                   1177:     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
                   1178:   else
                   1179:     function = default_conversion (function);
                   1180: 
                   1181:   result = build_nt (CALL_EXPR, function,
                   1182:                     convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
                   1183:                                        parmlist, NULL_TREE, LOOKUP_NORMAL),
                   1184:                     NULL_TREE);
                   1185:   TREE_TYPE (result) = TREE_TYPE (fntype);
                   1186:   TREE_SIDE_EFFECTS (result) = 1;
                   1187:   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
                   1188:   return result;
                   1189: }
                   1190: 
                   1191: /* Call this when we know (for any reason) that expr is
                   1192:    not, in fact, zero.  */
                   1193: tree
                   1194: convert_pointer_to (binfo, expr)
                   1195:      tree binfo, expr;
                   1196: {
                   1197:   register tree intype = TREE_TYPE (expr);
                   1198:   register enum tree_code form = TREE_CODE (intype);
                   1199:   tree ptr_type;
                   1200:   tree type, rval;
                   1201: 
                   1202:   if (TREE_CODE (binfo) == TREE_VEC)
                   1203:     type = BINFO_TYPE (binfo);
                   1204:   else if (IS_AGGR_TYPE (binfo))
                   1205:     {
                   1206:       type = binfo;
                   1207:       binfo = TYPE_BINFO (binfo);
                   1208:     }
                   1209:   else
                   1210:     {
                   1211:       type = binfo;
                   1212:       binfo = NULL_TREE;
                   1213:     }
                   1214: 
                   1215:   ptr_type = build_pointer_type (type);
                   1216:   if (ptr_type == TYPE_MAIN_VARIANT (intype))
                   1217:     return expr;
                   1218: 
                   1219:   if (intype == error_mark_node)
                   1220:     return error_mark_node;
                   1221: 
                   1222:   assert (!integer_zerop (expr));
                   1223: 
                   1224:   if (IS_AGGR_TYPE (type)
                   1225:       && IS_AGGR_TYPE (TREE_TYPE (intype))
                   1226:       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
                   1227:     {
                   1228:       tree path;
                   1229:       int distance = get_base_distance (type, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
                   1230: 
1.1.1.3 ! root     1231:       /* This function shouldn't be called with unqualified arguments
        !          1232:         but if it is, give them an error message that they can read.
        !          1233:         */
        !          1234:       if (distance < 0)
        !          1235:        {
        !          1236:          error ("cannot convert a pointer of type `%s'",
        !          1237:                 TYPE_NAME_STRING (TREE_TYPE (intype)));
        !          1238:          error_with_aggr_type (type, "to a pointer of type `%s'");
        !          1239: 
        !          1240:          return error_mark_node;
        !          1241:        }
1.1       root     1242: 
                   1243:       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
                   1244:     }
                   1245:   rval = build1 (NOP_EXPR, ptr_type,
                   1246:                 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
                   1247:   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
                   1248:   return rval;
                   1249: }
                   1250: 
                   1251: /* Same as above, but don't abort if we get an "ambiguous" baseclass.
                   1252:    There's only one virtual baseclass we are looking for, and once
                   1253:    we find one such virtual baseclass, we have found them all.  */
                   1254: 
                   1255: tree
                   1256: convert_pointer_to_vbase (binfo, expr)
                   1257:      tree binfo;
                   1258:      tree expr;
                   1259: {
                   1260:   tree intype = TREE_TYPE (TREE_TYPE (expr));
                   1261:   tree binfos = TYPE_BINFO_BASETYPES (intype);
                   1262:   int i;
                   1263: 
                   1264:   for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
                   1265:     {
                   1266:       tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
                   1267:       if (BINFO_TYPE (binfo) == basetype)
                   1268:        return convert_pointer_to (binfo, expr);
                   1269:       if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
                   1270:        return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
                   1271:     }
1.1.1.3 ! root     1272:   my_friendly_abort (6);
1.1       root     1273:   /* NOTREACHED */
                   1274:   return NULL_TREE;
                   1275: }
                   1276: 
                   1277: /* Create an expression whose value is that of EXPR,
                   1278:    converted to type TYPE.  The TREE_TYPE of the value
                   1279:    is always TYPE.  This function implements all reasonable
                   1280:    conversions; callers should filter out those that are
                   1281:    not permitted by the language being compiled.  */
                   1282: 
                   1283: tree
                   1284: convert (type, expr)
                   1285:      tree type, expr;
                   1286: {
                   1287:   register tree e = expr;
                   1288:   register enum tree_code code = TREE_CODE (type);
                   1289: 
                   1290:   if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
                   1291:     return expr;
                   1292:   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
                   1293:     return error_mark_node;
                   1294:   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
                   1295:     {
                   1296:       error ("void value not ignored as it ought to be");
                   1297:       return error_mark_node;
                   1298:     }
                   1299:   if (code == VOID_TYPE)
                   1300:     {
                   1301:       tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
                   1302:       /* If we can convert to void type via a type conversion, do so.  */
                   1303:       if (rval)
                   1304:        return rval;
                   1305:       return build1 (CONVERT_EXPR, type, e);
                   1306:     }
                   1307: #if 0
                   1308:   /* This is incorrect.  A truncation can't be stripped this way.
                   1309:      Extensions will be stripped by the use of get_unwidened.  */
                   1310:   if (TREE_CODE (expr) == NOP_EXPR)
                   1311:     return convert (type, TREE_OPERAND (expr, 0));
                   1312: #endif
                   1313: 
                   1314:   /* Just convert to the type of the member.  */
                   1315:   if (code == OFFSET_TYPE)
                   1316:     {
                   1317:       type = TREE_TYPE (type);
                   1318:       code = TREE_CODE (type);
                   1319:     }
                   1320: 
                   1321:   /* C++ */
                   1322:   if (code == REFERENCE_TYPE)
                   1323:     return fold (convert_to_reference (error_mark_node, type, e, -1, LOOKUP_NORMAL));
                   1324:   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
                   1325:     e = convert_from_reference (e);
                   1326: 
                   1327:   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
                   1328:     return fold (convert_to_integer (type, e));
                   1329:   if (code == POINTER_TYPE)
                   1330:     return fold (convert_to_pointer (type, e));
                   1331:   if (code == REAL_TYPE)
                   1332:     return fold (convert_to_real (type, e));
                   1333: 
                   1334:   /* New C++ semantics:  since assignment is now based on
                   1335:      memberwise copying,  if the rhs type is derived from the
                   1336:      lhs type, then we may still do a conversion.  */
                   1337:   if (IS_AGGR_TYPE_CODE (code))
                   1338:     {
                   1339:       tree dtype = TREE_TYPE (e);
                   1340: 
                   1341:       if (TREE_CODE (dtype) == REFERENCE_TYPE)
                   1342:        {
                   1343:          e = convert_from_reference (e);
                   1344:          dtype = TREE_TYPE (e);
                   1345:        }
                   1346:       dtype = TYPE_MAIN_VARIANT (dtype);
                   1347: 
                   1348:       /* Conversion between aggregate types.  New C++ semantics allow
                   1349:         objects of derived type to be cast to objects of base type.
1.1.1.2   root     1350:         Old semantics only allowed this between pointers.
1.1       root     1351: 
                   1352:         There may be some ambiguity between using a constructor
                   1353:         vs. using a type conversion operator when both apply.  */
                   1354: 
                   1355:       if (IS_AGGR_TYPE (dtype))
                   1356:        {
                   1357:          tree binfo;
                   1358: 
                   1359:          tree conversion = TYPE_HAS_CONVERSION (dtype)
                   1360:            ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
                   1361: 
                   1362:          if (TYPE_HAS_CONSTRUCTOR (type))
                   1363:            {
                   1364:              tree rval = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, e), TYPE_BINFO (type),
                   1365:                                             conversion ? LOOKUP_NO_CONVERSION : 0);
                   1366: 
                   1367:              if (rval != error_mark_node)
                   1368:                {
                   1369:                  if (conversion)
                   1370:                    {
                   1371:                      error ("both constructor and type conversion operator apply");
                   1372:                      return error_mark_node;
                   1373:                    }
                   1374:                  /* call to constructor successful.  */
                   1375:                  rval = build_cplus_new (type, rval, 0);
                   1376:                  return rval;
                   1377:                }
                   1378:            }
                   1379:          /* Type conversion successful/applies.  */
                   1380:          if (conversion)
                   1381:            {
                   1382:              if (conversion == error_mark_node)
                   1383:                error ("ambiguous pointer conversion");
                   1384:              return conversion;
                   1385:            }
                   1386: 
                   1387:          /* now try normal C++ assignment semantics.  */
                   1388:          binfo = TYPE_BINFO (dtype);
                   1389:          if (BINFO_TYPE (binfo) == type
                   1390:              || (binfo = get_binfo (type, dtype, 1)))
                   1391:            {
                   1392:              if (binfo == error_mark_node)
                   1393:                return error_mark_node;
                   1394:            }
                   1395:          if (binfo != NULL_TREE)
                   1396:            {
                   1397:              if (lvalue_p (e))
                   1398:                {
                   1399:                  e = build_unary_op (ADDR_EXPR, e, 0);
                   1400: 
                   1401:                  if (! BINFO_OFFSET_ZEROP (binfo))
                   1402:                    e = build (PLUS_EXPR, TYPE_POINTER_TO (type),
                   1403:                               e, BINFO_OFFSET (binfo));
                   1404:                  return build1 (INDIRECT_REF, type, e);
                   1405:                }
                   1406: 
                   1407:              sorry ("addressable aggregates");
                   1408:              return error_mark_node;
                   1409:            }
                   1410:          error ("conversion between incompatible aggregate types requested");
                   1411:          return error_mark_node;
                   1412:        }
                   1413:       /* conversion from non-aggregate to aggregate type requires constructor.  */
                   1414:       else if (TYPE_HAS_CONSTRUCTOR (type))
                   1415:        {
                   1416:          tree rval;
                   1417:          tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, e), TYPE_BINFO (type), LOOKUP_NORMAL);
                   1418:          if (init == error_mark_node)
                   1419:            {
                   1420:              error_with_aggr_type (type, "in conversion to type `%s'");
                   1421:              return error_mark_node;
                   1422:            }
                   1423:          rval = build_cplus_new (type, init, 0);
                   1424:          return rval;
                   1425:        }
                   1426:     }
                   1427: 
                   1428:   /* If TYPE or TREE_TYPE (EXPR) is not on the permanent_obstack,
                   1429:      then the it won't be hashed and hence compare as not equal,
                   1430:      even when it is.  */
                   1431:   if (code == ARRAY_TYPE
                   1432:       && TREE_TYPE (TREE_TYPE (expr)) == TREE_TYPE (type)
                   1433:       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (expr)), TYPE_DOMAIN (type)))
                   1434:     return expr;
                   1435: 
                   1436:   error ("conversion to non-scalar type requested");
                   1437:   return error_mark_node;
                   1438: }
                   1439: 
                   1440: /* Like convert, except permit conversions to take place which
                   1441:    are not normally allowed due to visibility restrictions
                   1442:    (such as conversion from sub-type to private super-type).  */
                   1443: tree
                   1444: convert_force (type, expr)
                   1445:      tree type;
                   1446:      tree expr;
                   1447: {
                   1448:   register tree e = expr;
                   1449:   register enum tree_code code = TREE_CODE (type);
                   1450: 
                   1451:   if (code == REFERENCE_TYPE)
                   1452:     return fold (convert_to_reference (0, type, e, -1, 0));
                   1453:   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
                   1454:     e = convert_from_reference (e);
                   1455: 
                   1456:   if (code == POINTER_TYPE)
                   1457:     return fold (convert_to_pointer_force (type, e));
                   1458: 
                   1459:   {
                   1460:     int old_equiv = flag_int_enum_equivalence;
                   1461:     flag_int_enum_equivalence = 1;
                   1462:     e = convert (type, e);
                   1463:     flag_int_enum_equivalence = old_equiv;
                   1464:   }
                   1465:   return e;
                   1466: }
                   1467: 
                   1468: /* Subroutine of build_type_conversion.  */
                   1469: static tree
                   1470: build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
                   1471:      tree xtype, basetype;
                   1472:      tree expr;
                   1473:      tree typename;
                   1474:      int for_sure;
                   1475: {
                   1476:   tree first_arg = expr;
                   1477:   tree rval;
                   1478:   int flags;
                   1479: 
                   1480:   if (for_sure == 0)
                   1481:     {
                   1482:       if (! lvalue_p (expr))
                   1483:        first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
                   1484:       flags = LOOKUP_PROTECT;
                   1485:     }
                   1486:   else
                   1487:     flags = LOOKUP_NORMAL;
                   1488: 
                   1489:   rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
                   1490:   if (rval == error_mark_node)
                   1491:     {
                   1492:       if (for_sure == 0)
                   1493:        return NULL_TREE;
                   1494:       return error_mark_node;
                   1495:     }
                   1496:   if (first_arg != expr)
                   1497:     {
                   1498:       expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr,
                   1499:                                 LOOKUP_COMPLAIN);
                   1500:       TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
                   1501:     }
                   1502:   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
                   1503:       && TREE_CODE (xtype) != REFERENCE_TYPE)
                   1504:     rval = default_conversion (rval);
                   1505: 
                   1506:   if (pedantic
                   1507:       && TREE_TYPE (xtype)
                   1508:       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
                   1509:          > TREE_READONLY (TREE_TYPE (xtype))))
                   1510:     pedwarn ("user-defined conversion casting away `const'");
                   1511:   return convert (xtype, rval);
                   1512: }
                   1513: 
                   1514: /* Convert an aggregate EXPR to type XTYPE.  If a conversion
                   1515:    exists, return the attempted conversion.  This may
                   1516:    return ERROR_MARK_NODE if the conversion is not
                   1517:    allowed (references private members, etc).
                   1518:    If no conversion exists, NULL_TREE is returned.
                   1519: 
                   1520:    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
                   1521:    to take place immediately.  Otherwise, we build a SAVE_EXPR
                   1522:    which can be evaluated if the results are ever needed.
                   1523: 
                   1524:    If FOR_SURE >= 2, then we only look for exact conversions.
                   1525: 
                   1526:    TYPE may be a reference type, in which case we first look
                   1527:    for something that will convert to a reference type.  If
                   1528:    that fails, we will try to look for something of the
                   1529:    reference's target type, and then return a reference to that.  */
                   1530: tree
                   1531: build_type_conversion (code, xtype, expr, for_sure)
                   1532:      enum tree_code code;
                   1533:      tree xtype, expr;
                   1534:      int for_sure;
                   1535: {
                   1536:   /* C++: check to see if we can convert this aggregate type
                   1537:      into the required scalar type.  */
                   1538:   tree type, type_default;
                   1539:   tree typename = build_typename_overload (xtype), *typenames;
                   1540:   int n_variants = 0;
                   1541:   tree basetype, save_basetype;
                   1542:   tree rval;
                   1543:   int exact_conversion = for_sure >= 2;
                   1544:   for_sure &= 1;
                   1545: 
                   1546:   if (expr == error_mark_node)
                   1547:     return error_mark_node;
                   1548: 
                   1549:   basetype = TREE_TYPE (expr);
                   1550:   if (TREE_CODE (basetype) == REFERENCE_TYPE)
                   1551:     basetype = TREE_TYPE (basetype);
                   1552: 
                   1553:   basetype = TYPE_MAIN_VARIANT (basetype);
                   1554:   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
                   1555:     return 0;
                   1556: 
                   1557:   if (TREE_CODE (xtype) == POINTER_TYPE
                   1558:       || TREE_CODE (xtype) == REFERENCE_TYPE)
                   1559:     {
                   1560:       /* Prepare to match a variant of this type.  */
                   1561:       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
                   1562:       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
                   1563:        n_variants++;
                   1564:       typenames = (tree *)alloca (n_variants * sizeof (tree));
                   1565:       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
                   1566:           type; n_variants++, type = TYPE_NEXT_VARIANT (type))
                   1567:        {
                   1568:          if (type == TREE_TYPE (xtype))
                   1569:            typenames[n_variants] = typename;
                   1570:          else if (TREE_CODE (xtype) == POINTER_TYPE)
                   1571:            typenames[n_variants] = build_typename_overload (build_pointer_type (type));
                   1572:          else
                   1573:            typenames[n_variants] = build_typename_overload (build_reference_type (type));
                   1574:        }
                   1575:     }
                   1576: 
                   1577:   save_basetype = basetype;
                   1578:   type = xtype;
                   1579: 
                   1580:   while (TYPE_HAS_CONVERSION (basetype))
                   1581:     {
                   1582:       int i;
                   1583:       if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
                   1584:        return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1585:       for (i = 0; i < n_variants; i++)
                   1586:        if (typenames[i] != typename
                   1587:            && lookup_fnfields (TYPE_BINFO (basetype), typenames[i], 0))
                   1588:          return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
                   1589: 
                   1590:       if (TYPE_BINFO_BASETYPES (basetype))
                   1591:        basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1592:       else break;
                   1593:     }
                   1594: 
                   1595:   if (TREE_CODE (type) == REFERENCE_TYPE)
                   1596:     {
                   1597:       tree first_arg = expr;
                   1598:       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
                   1599:       basetype = save_basetype;
                   1600: 
                   1601:       /* May need to build a temporary for this.  */
                   1602:       while (TYPE_HAS_CONVERSION (basetype))
                   1603:        {
                   1604:          if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
                   1605:            {
                   1606:              int flags;
                   1607: 
                   1608:              if (for_sure == 0)
                   1609:                {
                   1610:                  if (! lvalue_p (expr))
                   1611:                    first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
                   1612:                  flags = LOOKUP_PROTECT;
                   1613:                }
                   1614:              else
                   1615:                flags = LOOKUP_NORMAL;
                   1616:              rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
                   1617:              if (rval == error_mark_node)
                   1618:                {
                   1619:                  if (for_sure == 0)
                   1620:                    return NULL_TREE;
                   1621:                  return error_mark_node;
                   1622:                }
                   1623:              TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
                   1624: 
                   1625:              if (IS_AGGR_TYPE (type))
                   1626:                {
                   1627:                  tree init = build_method_call (NULL_TREE, TYPE_IDENTIFIER (type), build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
                   1628:                  tree temp = build_cplus_new (type, init, 1);
                   1629:                  return build_up_reference (TYPE_REFERENCE_TO (type), temp,
                   1630:                                             LOOKUP_COMPLAIN);
                   1631:                }
                   1632:              return convert (xtype, rval);
                   1633:            }
                   1634:          if (TYPE_BINFO_BASETYPES (basetype))
                   1635:            basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1636:          else break;
                   1637:        }
                   1638:       /* No free conversions for reference types, right?.  */
                   1639:       return NULL_TREE;
                   1640:     }
                   1641: 
                   1642:   if (exact_conversion)
                   1643:     return NULL_TREE;
                   1644: 
                   1645:   /* No perfect match found, try default.  */
                   1646:   if (code == CONVERT_EXPR && TREE_CODE (type) == POINTER_TYPE)
                   1647:     type_default = ptr_type_node;
                   1648:   else if (type == void_type_node)
                   1649:     return NULL_TREE;
                   1650:   else
                   1651:     {
                   1652:       extern tree default_conversion ();
                   1653:       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
                   1654:       if (tmp == error_mark_node)
                   1655:        return NULL_TREE;
                   1656:       type_default = TREE_TYPE (tmp);
                   1657:     }
                   1658: 
                   1659:   basetype = save_basetype;
                   1660: 
                   1661:   if (type_default != type)
                   1662:     {
                   1663:       type = type_default;
                   1664:       typename = build_typename_overload (type);
                   1665: 
                   1666:       while (TYPE_HAS_CONVERSION (basetype))
                   1667:        {
                   1668:          if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
                   1669:            return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1670:          if (TYPE_BINFO_BASETYPES (basetype))
                   1671:            basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1672:          else break;
                   1673:        }
                   1674:     }
                   1675: 
                   1676:  try_pointer:
                   1677: 
                   1678:   if (type == ptr_type_node)
                   1679:     {
                   1680:       /* Try converting to some other pointer type
                   1681:         with which void* is compatible, or in situations
                   1682:         in which void* is appropriate (such as &&,||, and !).  */
                   1683: 
                   1684:       while (TYPE_HAS_CONVERSION (basetype))
                   1685:        {
                   1686:          if (CLASSTYPE_CONVERSION (basetype, ptr_conv) != 0)
                   1687:            {
                   1688:              if (CLASSTYPE_CONVERSION (basetype, ptr_conv) == error_mark_node)
                   1689:                return error_mark_node;
                   1690:              typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, ptr_conv));
                   1691:              return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1692:            }
                   1693:          if (TYPE_BINFO_BASETYPES (basetype))
                   1694:            basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1695:          else break;
                   1696:        }
                   1697:     }
                   1698:   if (TREE_CODE (type) == POINTER_TYPE
                   1699:       && TYPE_READONLY (TREE_TYPE (type))
                   1700:       && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
                   1701:     {
                   1702:       /* Try converting to some other pointer type
                   1703:         with which const void* is compatible.  */
                   1704: 
                   1705:       while (TYPE_HAS_CONVERSION (basetype))
                   1706:        {
                   1707:          if (CLASSTYPE_CONVERSION (basetype, constptr_conv) != 0)
                   1708:            {
                   1709:              if (CLASSTYPE_CONVERSION (basetype, constptr_conv) == error_mark_node)
                   1710:                return error_mark_node;
                   1711:              typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, constptr_conv));
                   1712:              return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1713:            }
                   1714:          if (TYPE_BINFO_BASETYPES (basetype))
                   1715:            basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1716:          else break;
                   1717:        }
                   1718:     }
                   1719:   /* Use the longer or shorter conversion that is appropriate.  Have
                   1720:      to check against 0 because the conversion may come from a baseclass.  */
                   1721:   if (TREE_CODE (type) == INTEGER_TYPE
                   1722:       && TYPE_HAS_INT_CONVERSION (basetype)
                   1723:       && CLASSTYPE_CONVERSION (basetype, int_conv) != 0
                   1724:       && CLASSTYPE_CONVERSION (basetype, int_conv) != error_mark_node)
                   1725:     {
                   1726:       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, int_conv));
                   1727:       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1728:     }
                   1729:   if (TREE_CODE (type) == REAL_TYPE
                   1730:       && TYPE_HAS_REAL_CONVERSION (basetype)
                   1731:       && CLASSTYPE_CONVERSION (basetype, real_conv) != 0
                   1732:       && CLASSTYPE_CONVERSION (basetype, real_conv) != error_mark_node)
                   1733:     {
                   1734:       typename = DECL_NAME (CLASSTYPE_CONVERSION (basetype, real_conv));
                   1735:       return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1736:     }
                   1737: 
                   1738:   /* THIS IS A KLUDGE.  */
                   1739:   if (TREE_CODE (type) != POINTER_TYPE
                   1740:       && (code == TRUTH_ANDIF_EXPR
                   1741:          || code == TRUTH_ORIF_EXPR
                   1742:          || code == TRUTH_NOT_EXPR))
                   1743:     {
                   1744:       /* Here's when we can convert to a pointer.  */
                   1745:       type = ptr_type_node;
                   1746:       goto try_pointer;
                   1747:     }
                   1748: 
                   1749:   /* THESE ARE TOTAL KLUDGES.  */
                   1750:   /* Default promotion yields no new alternatives, try
                   1751:      conversions which are anti-default, such as
                   1752: 
                   1753:      double -> float or int -> unsigned or unsigned -> long
                   1754: 
                   1755:      */
1.1.1.3 ! root     1756:   if (type_default == type
        !          1757:       && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE))
1.1       root     1758:     {
                   1759:       int not_again = 0;
                   1760: 
                   1761:       if (type == double_type_node)
                   1762:        typename = build_typename_overload (float_type_node);
                   1763:       else if (type == integer_type_node)
                   1764:        typename = build_typename_overload (unsigned_type_node);
                   1765:       else if (type == unsigned_type_node)
                   1766:        typename = build_typename_overload (long_integer_type_node);
                   1767: 
                   1768:     again:
                   1769:       basetype = save_basetype;
                   1770:       while (TYPE_HAS_CONVERSION (basetype))
                   1771:        {
                   1772:          if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
                   1773:            return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1774:          if (TYPE_BINFO_BASETYPES (basetype))
                   1775:            basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1776:          else break;
                   1777:        }
1.1.1.2   root     1778:       if (! not_again)
1.1       root     1779:        {
1.1.1.2   root     1780:          if (type == integer_type_node)
                   1781:            {
                   1782:              typename = build_typename_overload (long_integer_type_node);
                   1783:              not_again = 1;
                   1784:              goto again;
                   1785:            }
                   1786:          else
                   1787:            {
                   1788:              typename = build_typename_overload (integer_type_node);
                   1789:              not_again = 1;
                   1790:              goto again;
                   1791:            }
1.1       root     1792:        }
                   1793:     }
                   1794: 
                   1795:   /* Now, try C promotions...
                   1796: 
                   1797:      float -> int
                   1798:      int -> float, void *
                   1799:      void * -> int
                   1800: 
                   1801:      Truthvalue conversions let us try to convert
                   1802:      to pointer if we were going for int, and to int
                   1803:      if we were looking for pointer.  */
                   1804: 
                   1805:     basetype = save_basetype;
                   1806:     if (TREE_CODE (type) == REAL_TYPE
                   1807:        || (TREE_CODE (type) == POINTER_TYPE
                   1808:            && (code == TRUTH_ANDIF_EXPR
                   1809:                || code == TRUTH_ORIF_EXPR
                   1810:                || code == TRUTH_NOT_EXPR)))
                   1811:       type = integer_type_node;
                   1812:     else if (TREE_CODE (type) == INTEGER_TYPE)
                   1813:       if (TYPE_HAS_REAL_CONVERSION (basetype))
                   1814:        type = double_type_node;
                   1815:       else
                   1816:        return NULL_TREE;
                   1817:     else
                   1818:       return NULL_TREE;
                   1819: 
                   1820:     typename = build_typename_overload (type);
                   1821:     while (TYPE_HAS_CONVERSION (basetype))
                   1822:       {
                   1823:        if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
                   1824:          {
                   1825:            rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
                   1826:            return rval;
                   1827:          }
                   1828:        if (TYPE_BINFO_BASETYPES (basetype))
                   1829:          basetype = TYPE_BINFO_BASETYPE (basetype, 0);
                   1830:        else
                   1831:          break;
                   1832:       }
                   1833: 
                   1834:   return NULL_TREE;
                   1835: }
                   1836: 
                   1837: /* Must convert two aggregate types to non-aggregate type.
                   1838:    Attempts to find a non-ambiguous, "best" type conversion.
                   1839: 
                   1840:    Return 1 on success, 0 on failure.
                   1841: 
                   1842:    @@ What are the real semantics of this supposed to be??? */
                   1843: int
                   1844: build_default_binary_type_conversion (code, arg1, arg2)
                   1845:      enum tree_code code;
                   1846:      tree *arg1, *arg2;
                   1847: {
                   1848:   tree type1 = TREE_TYPE (*arg1);
                   1849:   tree type2 = TREE_TYPE (*arg2);
                   1850:   char *name1, *name2;
                   1851: 
                   1852:   if (TREE_CODE (type1) == REFERENCE_TYPE)
                   1853:     type1 = TREE_TYPE (type1);
                   1854:   if (TREE_CODE (type2) == REFERENCE_TYPE)
                   1855:     type2 = TREE_TYPE (type2);
                   1856: 
                   1857:   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
                   1858:     {
                   1859:       tree decl = typedecl_for_tag (type1);
                   1860:       if (decl)
1.1.1.2   root     1861:        error ("type conversion nonexistent for type `%s'",
1.1       root     1862:               IDENTIFIER_POINTER (DECL_NAME (decl)));
                   1863:       else
1.1.1.2   root     1864:        error ("type conversion nonexistent for non-C++ type");
1.1       root     1865:       return 0;
                   1866:     }
                   1867:   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
                   1868:     {
                   1869:       tree decl = typedecl_for_tag (type2);
                   1870:       if (decl)
1.1.1.2   root     1871:        error ("type conversion nonexistent for type `%s'",
1.1       root     1872:               IDENTIFIER_POINTER (decl));
                   1873:       else
1.1.1.2   root     1874:        error ("type conversion nonexistent for non-C++ type");
1.1       root     1875:       return 0;
                   1876:     }
                   1877: 
                   1878:   name1 = TYPE_NAME_STRING (type1);
                   1879:   name2 = TYPE_NAME_STRING (type2);
                   1880: 
                   1881:   if (!IS_AGGR_TYPE (type1) || !TYPE_HAS_CONVERSION (type1))
                   1882:     {
                   1883:       if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
                   1884:        error ("type conversion required for binary operation on types `%s' and `%s'",
                   1885:               name1, name2);
                   1886:       else
                   1887:        error ("type conversion required for type `%s'", name1);
                   1888:       return 0;
                   1889:     }
                   1890:   else if (!IS_AGGR_TYPE (type2) || !TYPE_HAS_CONVERSION (type2))
                   1891:     {
                   1892:       error ("type conversion required for type `%s'", name2);
                   1893:       return 0;
                   1894:     }
                   1895: 
                   1896:   if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
                   1897:     warning ("ambiguous type conversion for type `%s', defaulting to int", name1);
                   1898:   if (TYPE_HAS_INT_CONVERSION (type1))
                   1899:     {
                   1900:       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
                   1901:       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
                   1902:     }
                   1903:   else if (TYPE_HAS_REAL_CONVERSION (type1))
                   1904:     {
                   1905:       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
                   1906:       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
                   1907:     }
                   1908:   else
                   1909:     {
                   1910:       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
                   1911:       if (*arg1 == error_mark_node)
                   1912:        error ("ambiguous pointer conversion");
                   1913:       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
                   1914:       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
                   1915:        error ("ambiguous pointer conversion");
                   1916:     }
                   1917:   if (*arg1 == 0)
                   1918:     {
                   1919:       if (*arg2 == 0 && type1 != type2)
                   1920:        error ("default type conversion for types `%s' and `%s' failed",
                   1921:               name1, name2);
                   1922:       else
                   1923:        error ("default type conversion for type `%s' failed", name1);
                   1924:       return 0;
                   1925:     }
                   1926:   else if (*arg2 == 0)
                   1927:     {
                   1928:       error ("default type conversion for type `%s' failed", name2);
                   1929:       return 0;
                   1930:     }
                   1931:   return 1;
                   1932: }
                   1933: 
                   1934: /* Must convert two aggregate types to non-aggregate type.
                   1935:    Attempts to find a non-ambiguous, "best" type conversion.
                   1936: 
                   1937:    Return 1 on success, 0 on failure.
                   1938: 
                   1939:    The type of the argument is expected to be of aggregate type here.
                   1940: 
                   1941:    @@ What are the real semantics of this supposed to be??? */
                   1942: int
                   1943: build_default_unary_type_conversion (code, arg)
                   1944:      enum tree_code code;
                   1945:      tree *arg;
                   1946: {
                   1947:   tree type = TREE_TYPE (*arg);
                   1948:   tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
                   1949:     ? TYPE_IDENTIFIER (type) : TYPE_NAME (type);
                   1950:   char *name = IDENTIFIER_POINTER (id);
                   1951: 
                   1952:   if (! TYPE_HAS_CONVERSION (type))
                   1953:     {
                   1954:       error ("type conversion required for type `%s'", name);
                   1955:       return 0;
                   1956:     }
                   1957: 
                   1958:   if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
                   1959:     warning ("ambiguous type conversion for type `%s', defaulting to int", name);
                   1960:   if (TYPE_HAS_INT_CONVERSION (type))
                   1961:     *arg = build_type_conversion (code, integer_type_node, *arg, 1);
                   1962:   else if (TYPE_HAS_REAL_CONVERSION (type))
                   1963:     *arg = build_type_conversion (code, double_type_node, *arg, 1);
                   1964:   else
                   1965:     {
                   1966:       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
                   1967:       if (*arg == error_mark_node)
                   1968:        error ("ambiguous pointer conversion");
                   1969:     }
                   1970:   if (*arg == 0)
                   1971:     {
                   1972:       error ("default type conversion for type `%s' failed", name);
                   1973:       return 0;
                   1974:     }
                   1975:   return 1;
                   1976: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.