示例#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;
}
示例#2
0
文件: expr-id.cpp 项目: NoraAl/banjo
// Perform unqualified lookup.
//
// TODO: Allow an lookup to fail, indicating that we could not find
// a name. In function calls, this could be used to perform class
// lookup for member functions.
Expr&
make_reference(Context& cxt, Simple_id& id)
{
  Decl_list decls = unqualified_lookup(cxt, id);
  if (decls.size() == 1)
    return make_decl_ref(cxt, decls.front());
  else
    return make_ovl_ref(cxt, id, std::move(decls));
}
示例#3
0
// Perform unqualified lookup.
Expr&
make_reference(Context& cxt, Simple_id& id)
{
  Decl_list decls = unqualified_lookup(cxt, id);
  if (decls.size() == 1)
    return make_reference(cxt, decls.front());

  // TODO: Return a reference to an overload set.
  banjo_unhandled_case(id);
}