Example #1
0
// Perform at most one value conversion from the following set:
//
//    - integer conversions
//    - float conversions
//    - numeric conversions (int to float)
//    - boolean conversions (type to bool)
//
// Note that all such conversions take value expressions as operands: a
// reference-to-value conversion must have been previously applied to
// a reference expressions.
//
// TODO: Implement integer and floating point promotion.
//
// TODO: Support conversion of T[N] to T[].
//
// TODO: Support pointer conversions.
//
// TODO: Support character conversions separately from integer
// conversions?
Expr&
convert_value(Context& cxt, Expr& e, Type& t)
{
  try {
    // Note that errors here are not fatal. If we can't find a conversion,
    // that doesn't mean the program is ill-formed.
    Suppress_diagnostics guard(cxt);

    debug(e);
    debug(t);
  
    // Search for a list of conversion operators for the given type.
    Name& id = cxt.get_conversion_id(t);  
    Decl_list decls = unqualified_lookup(cxt, id);

    // FIXME: What do I do with ambiguous resolutions? Fail to convert?
    Resolution res = resolve_call(cxt, decls, e);
    Function_decl& decl = res.function();
    Expr_list& args = res.arguments();

    // Build a call to the selected conversion.
    Expr& fn = cxt.make_reference(decl.type(), decl);
    Call_expr& conv = cxt.make_call(decl.return_type(), fn, std::move(args));
    conv.res_ = &decl;
    return conv;
  }
  catch (...) {
    return e;
  }

  return e;
}