for (auto &referenced_param : function_params)
{
auto &tyty_param = fntype->param_at (i++);
- auto param_tyty = tyty_param.second;
+ auto param_tyty = tyty_param.get_type ();
auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);
location_t param_locus = referenced_param.get_locus ();
const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (base);
auto ¶m = fn->param_at (index);
- *result = param.second;
+ *result = param.get_type ();
return true;
};
// assignments are coercion sites so lets convert the rvalue if
// necessary, offset from the already adjusted implicit self
bool ok;
- TyTy::BaseType *expected = fntype->param_at (i + 1).second;
+ TyTy::BaseType *expected = fntype->param_at (i + 1).get_type ();
TyTy::BaseType *actual = nullptr;
ok = ctx->get_tyctx ()->lookup_type (
{
for (auto &parm : fntype->get_params ())
{
- auto &referenced_param = parm.first;
- auto ¶m_tyty = parm.second;
+ auto &referenced_param = parm.get_pattern ();
+ auto param_tyty = parm.get_type ();
auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty);
- location_t param_locus = referenced_param->get_locus ();
+ location_t param_locus = referenced_param.get_locus ();
Bvariable *compiled_param_var
- = CompileFnParam::compile (ctx, fndecl, *referenced_param,
+ = CompileFnParam::compile (ctx, fndecl, referenced_param,
compiled_param_type, param_locus);
compiled_param_variables->push_back (compiled_param_var);
rust_error_at (fntype->get_locus (),
"cannot transmute between types of different sizes, or "
"dependently-sized types");
- rust_inform (fntype->get_ident ().locus, "source type: %qs (%lu bits)",
- fntype->get_params ().at (0).second->as_string ().c_str (),
- (unsigned long) source_size);
+ rust_inform (
+ fntype->get_ident ().locus, "source type: %qs (%lu bits)",
+ fntype->get_params ().at (0).get_type ()->as_string ().c_str (),
+ (unsigned long) source_size);
rust_inform (fntype->get_ident ().locus, "target type: %qs (%lu bits)",
fntype->get_return_type ()->as_string ().c_str (),
(unsigned long) target_size);
// TODO: make sure this is actually helping the compiler optimize
rust_assert (fntype->get_params ().size () == 1);
- rust_assert (fntype->param_at (0).second->get_kind ()
+ rust_assert (fntype->param_at (0).get_type ()->get_kind ()
== TyTy::TypeKind::BOOL);
tree lookup = NULL_TREE;
for (auto ¶m_pair : type.get_params ())
{
- auto param_tyty = param_pair.second;
+ auto param_tyty = param_pair.get_type ();
auto compiled_param_type
= TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode);
auto compiled_param = Backend::typed_identifier (
- param_pair.first->as_string (), compiled_param_type,
+ param_pair.get_pattern ().as_string (), compiled_param_type,
ctx->get_mappings ().lookup_location (param_tyty->get_ref ()));
parameters.push_back (compiled_param);
for (auto &stmt : expr.get_statements ())
stmt->accept_vis (*this);
- auto &last_expr = expr.get_final_expr ();
- last_expr.accept_vis (*this);
+ if (expr.has_final_expr ())
+ expr.get_final_expr ().accept_vis (*this);
}
void
void
PrivacyReporter::visit (HIR::BreakExpr &expr)
{
- auto &break_expr = expr.get_expr ();
- break_expr.accept_vis (*this);
+ if (expr.has_break_expr ())
+ expr.get_expr ().accept_vis (*this);
}
void
void
PrivacyReporter::visit (HIR::ReturnExpr &expr)
{
- expr.get_expr ().accept_vis (*this);
+ if (expr.has_expr ())
+ expr.get_expr ().accept_vis (*this);
}
void
context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (),
&method_type);
- auto fn = *static_cast<TyTy::FnType *> (method_type);
+ auto &fn = static_cast<TyTy::FnType &> (*method_type);
auto method = mappings.lookup_hir_implitem (fn.get_ref ());
if (!unsafe_context.is_in_context () && method)
bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; }
+ bool has_final_expr () { return expr != nullptr; }
Expr &get_final_expr () { return *expr; }
std::vector<std::unique_ptr<Stmt> > &get_statements () { return statements; }
void accept_vis (HIRFullVisitor &vis) override;
void accept_vis (HIRExpressionVisitor &vis) override;
+ bool has_expr () { return return_expr != nullptr; }
Expr &get_expr () { return *return_expr; }
ExprType get_expression_type () const override final
Pattern &get_param_name () { return *param_name; }
- std::unique_ptr<Pattern> take_param_name () { return std::move (param_name); }
-
Type &get_type () { return *type; }
const Analysis::NodeMapping &get_mappings () const { return mappings; }
virtual void accept_vis (HIRTypeVisitor &vis) = 0;
- virtual Analysis::NodeMapping get_mappings () const { return mappings; }
+ virtual const Analysis::NodeMapping &get_mappings () const
+ {
+ return mappings;
+ }
virtual location_t get_locus () const { return locus; }
protected:
for (size_t i = 0; i < arguments.size (); i++)
{
TyTy::BaseType *arg = arguments.at (i);
- TyTy::BaseType *param = fn.get_params ().at (i + 1).second;
+ TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type ();
TyTy::BaseType *coerced
= try_coercion (0, TyTy::TyWithLocation (param),
TyTy::TyWithLocation (arg), UNDEF_LOCATION);
// typecheck the self
unify_site (expr.get_mappings ().get_hirid (),
- TyTy::TyWithLocation (fnparam.second),
+ TyTy::TyWithLocation (fnparam.get_type ()),
TyTy::TyWithLocation (adjusted_self), expr.get_locus ());
if (rhs == nullptr)
{
rust_assert (type->num_params () == 2);
auto &fnparam = type->param_at (1);
unify_site (expr.get_mappings ().get_hirid (),
- TyTy::TyWithLocation (fnparam.second),
+ TyTy::TyWithLocation (fnparam.get_type ()),
TyTy::TyWithLocation (rhs), expr.get_locus ());
}
function.get_return_type ().get_mappings ().get_hirid ());
}
- std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
- std::unique_ptr<HIR::IdentifierPattern> param_pattern = nullptr;
+ std::vector<TyTy::FnParam> params;
for (auto ¶m : function.get_function_params ())
{
// get the name as well required for later on
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);
- param_pattern = Rust::make_unique<HIR::IdentifierPattern> (
+ auto param_pattern = Rust::make_unique<HIR::IdentifierPattern> (
HIR::IdentifierPattern (mapping, param.get_param_name (),
UNDEF_LOCATION, false, Mutability::Imm,
std::unique_ptr<HIR::Pattern> (nullptr)));
- params.push_back (
- std::pair<HIR::Pattern *, TyTy::BaseType *> (param_pattern.get (),
- param_tyty));
+ params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty));
context->insert_type (param.get_mappings (), param_tyty);
function.get_return_type ().get_mappings ().get_hirid ());
}
- std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
- std::unique_ptr<HIR::IdentifierPattern> self_pattern = nullptr;
+ std::vector<TyTy::FnParam> params;
if (function.is_method ())
{
// these are implicit mappings and not used
// reuse the HIR identifier pattern which requires it
HIR::SelfParam &self_param = function.get_self_param ();
// FIXME: which location should be used for Rust::Identifier for `self`?
- self_pattern = Rust::make_unique<HIR::IdentifierPattern> (
- HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (),
- self_param.is_ref (), self_param.get_mut (),
- std::unique_ptr<HIR::Pattern> (nullptr)));
+ std::unique_ptr<HIR::Pattern> self_pattern
+ = Rust::make_unique<HIR::IdentifierPattern> (
+ HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (),
+ self_param.is_ref (), self_param.get_mut (),
+ std::unique_ptr<HIR::Pattern> (nullptr)));
// might have a specified type
TyTy::BaseType *self_type = nullptr;
}
context->insert_type (self_param.get_mappings (), self_type);
- params.push_back (
- std::pair<HIR::Pattern *, TyTy::BaseType *> (self_pattern.get (),
- self_type));
+ params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
}
for (auto ¶m : function.get_function_params ())
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
params.push_back (
- std::pair<HIR::Pattern *, TyTy::BaseType *> (¶m.get_param_name (),
- param_tyty));
+ TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}
tl::optional<CanonicalPath> canonical_path;
function.get_return_type ().get_mappings ().get_hirid ());
}
- std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *>> params;
+ std::vector<TyTy::FnParam> params;
for (auto ¶m : function.get_function_params ())
{
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());
- params.emplace_back (¶m.get_param_name (), param_tyty);
-
context->insert_type (param.get_mappings (), param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
+ params.push_back (
+ TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}
auto path = CanonicalPath::create_empty ();
function.get_return_type ().get_mappings ().get_hirid ());
}
- std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params;
+ std::vector<TyTy::FnParam> params;
- std::unique_ptr<HIR::IdentifierPattern> self_pattern = nullptr;
if (function.is_method ())
{
// these are implicit mappings and not used
// for compilation to know parameter names. The types are ignored
// but we reuse the HIR identifier pattern which requires it
HIR::SelfParam &self_param = function.get_self ();
- self_pattern
+ std::unique_ptr<HIR::Pattern> self_pattern
= Rust::make_unique<HIR::IdentifierPattern> (HIR::IdentifierPattern (
mapping, {"self"}, self_param.get_locus (), self_param.is_ref (),
self_param.is_mut () ? Mutability::Mut : Mutability::Imm,
}
context->insert_type (self_param.get_mappings (), self_type);
- params.push_back (
- std::pair<HIR::Pattern *, TyTy::BaseType *> (self_pattern.get (),
- self_type));
+ params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
}
for (auto ¶m : function.get_function_params ())
{
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());
- params.push_back (
- std::pair<HIR::Pattern *, TyTy::BaseType *> (¶m.get_param_name (),
- param_tyty));
-
context->insert_type (param.get_mappings (), param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
+ // FIXME: Should we take the name ? Use a shared pointer instead ?
+ params.push_back (
+ TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
}
auto &mappings = Analysis::Mappings::get ();
function.get_function_name ().as_string (), ident,
function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG
: TyTy::FnType::FNTYPE_DEFAULT_FLAGS,
- ABI::RUST, params, ret_type, substitutions,
+ ABI::RUST, std::move (params), ret_type, substitutions,
TyTy::SubstitutionArgumentMappings::empty (
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);
if (i < type.num_params ())
{
auto &fnparam = type.param_at (i);
- auto &fn_param_pattern = fnparam.first;
- BaseType *param_ty = fnparam.second;
+ auto &fn_param_pattern = fnparam.get_pattern ();
+ BaseType *param_ty = fnparam.get_type ();
location_t param_locus
- = fn_param_pattern == nullptr
+ = fnparam.has_pattern ()
? mappings.lookup_location (param_ty->get_ref ())
- : fn_param_pattern->get_locus ();
+ : fn_param_pattern.get_locus ();
HirId coercion_side_id = argument->get_mappings ().get_hirid ();
auto resolved_argument_type
location_t arg_locus = argument.get_locus ();
auto &fnparam = type.param_at (i);
- HIR::Pattern *fn_param_pattern = fnparam.first;
- BaseType *param_ty = fnparam.second;
+ HIR::Pattern &fn_param_pattern = fnparam.get_pattern ();
+ BaseType *param_ty = fnparam.get_type ();
location_t param_locus
- = fn_param_pattern == nullptr
+ = fnparam.has_pattern ()
? mappings.lookup_location (param_ty->get_ref ())
- : fn_param_pattern->get_locus ();
+ : fn_param_pattern.get_locus ();
auto argument_expr_tyty = argument.get_argument_type ();
HirId coercion_side_id = argument.get_mappings ().get_hirid ();
for (size_t i = 0; i < base->num_params (); i++)
{
- auto a = base->param_at (i).second;
- auto b = type.param_at (i).second;
+ auto a = base->param_at (i).get_type ();
+ auto b = type.param_at (i).get_type ();
if (!a->can_eq (b, emit_error_flag))
{
for (size_t i = 0; i < base->num_params (); i++)
{
auto this_param = base->get_param_type_at (i);
- auto other_param = type.param_at (i).second;
+ auto other_param = type.param_at (i).get_type ();
if (!this_param->can_eq (other_param, emit_error_flag))
{
BaseCmp::visit (type);
}
else if (auto fn = x->try_as<const FnType> ())
{
- std::vector<std::pair<HIR::Pattern *, BaseType *>> cloned_params;
+ std::vector<TyTy::FnParam> cloned_params;
for (auto &p : fn->get_params ())
- cloned_params.push_back (std::pair<HIR::Pattern *, BaseType *> (
- p.first, p.second->monomorphized_clone ()));
+ cloned_params.push_back (p.clone ());
BaseType *retty = fn->get_return_type ()->monomorphized_clone ();
return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (),
{
for (const auto ¶m : fn->get_params ())
{
- if (!param.second->is_concrete ())
+ if (!param.get_type ()->is_concrete ())
return false;
}
return fn->get_return_type ()->is_concrete ();
std::string params_str = "";
for (auto ¶m : params)
{
- auto &pattern = param.first;
- auto ty = param.second;
- params_str += pattern->as_string () + " " + ty->as_string ();
+ auto &pattern = param.get_pattern ();
+ auto ty = param.get_type ();
+ params_str += pattern.as_string () + " " + ty->as_string ();
params_str += ",";
}
if (get_kind () != other.get_kind ())
return false;
- auto other2 = static_cast<const FnType &> (other);
+ auto &other2 = static_cast<const FnType &> (other);
if (get_identifier ().compare (other2.get_identifier ()) != 0)
return false;
for (size_t i = 0; i < num_params (); i++)
{
- auto lhs = param_at (i).second;
- auto rhs = other2.param_at (i).second;
+ auto lhs = param_at (i).get_type ();
+ auto rhs = other2.param_at (i).get_type ();
if (!lhs->is_equal (*rhs))
return false;
}
BaseType *
FnType::clone () const
{
- std::vector<std::pair<HIR::Pattern *, BaseType *>> cloned_params;
+ std::vector<TyTy::FnParam> cloned_params;
for (auto &p : params)
- cloned_params.push_back (
- std::pair<HIR::Pattern *, BaseType *> (p.first, p.second->clone ()));
+ cloned_params.push_back (p.clone ());
return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (),
ident, flags, abi, std::move (cloned_params),
for (auto ¶m : fn->get_params ())
{
- auto fty = param.second;
+ auto fty = param.get_type ();
bool is_param_ty = fty->get_kind () == TypeKind::PARAM;
if (is_param_ty)
{
auto new_field = argt->clone ();
new_field->set_ref (fty->get_ref ());
- param.second = new_field;
+ param.set_type (new_field);
}
else
{
auto new_field = concrete->clone ();
new_field->set_ref (fty->get_ref ());
- param.second = new_field;
+ param.set_type (new_field);
}
}
ReprOptions repr;
};
+class FnParam
+{
+public:
+ FnParam (std::unique_ptr<HIR::Pattern> pattern, BaseType *type)
+ : pattern (std::move (pattern)), type (type)
+ {}
+
+ FnParam (const FnParam &) = delete;
+ FnParam (FnParam &&) = default;
+ FnParam &operator= (FnParam &&) = default;
+
+ HIR::Pattern &get_pattern () { return *pattern; }
+ const HIR::Pattern &get_pattern () const { return *pattern; }
+
+ bool has_pattern () { return pattern != nullptr; }
+ BaseType *get_type () const { return type; }
+ void set_type (BaseType *new_type) { type = new_type; }
+
+ FnParam clone () const
+ {
+ return FnParam (pattern->clone_pattern (), type->monomorphized_clone ());
+ }
+
+private:
+ std::unique_ptr<HIR::Pattern> pattern;
+ BaseType *type;
+};
+
class FnType : public CallableTypeInterface, public SubstitutionRef
{
public:
static const uint8_t FNTYPE_IS_VARADIC_FLAG = 0X04;
FnType (HirId ref, DefId id, std::string identifier, RustIdent ident,
- uint8_t flags, ABI abi,
- std::vector<std::pair<HIR::Pattern *, BaseType *>> params,
- BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
+ uint8_t flags, ABI abi, std::vector<FnParam> params, BaseType *type,
+ std::vector<SubstitutionParamMapping> subst_refs,
SubstitutionArgumentMappings substitution_argument_mappings,
RegionConstraints region_constraints,
std::set<HirId> refs = std::set<HirId> ())
: CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs),
SubstitutionRef (std::move (subst_refs), substitution_argument_mappings,
region_constraints),
- params (params), type (type), flags (flags), identifier (identifier),
- id (id), abi (abi)
+ params (std::move (params)), type (type), flags (flags),
+ identifier (identifier), id (id), abi (abi)
{
LocalDefId local_def_id = id.localDefId;
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
}
FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier,
- RustIdent ident, uint8_t flags, ABI abi,
- std::vector<std::pair<HIR::Pattern *, BaseType *>> params,
+ RustIdent ident, uint8_t flags, ABI abi, std::vector<FnParam> params,
BaseType *type, std::vector<SubstitutionParamMapping> subst_refs,
SubstitutionArgumentMappings substitution_argument_mappings,
RegionConstraints region_constraints,
rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID);
}
+ FnType (const FnType &) = delete;
+ FnType (FnType &&) = default;
+
void accept_vis (TyVisitor &vis) override;
void accept_vis (TyConstVisitor &vis) const override;
BaseType *get_self_type () const
{
rust_assert (is_method ());
- return param_at (0).second;
+ return param_at (0).get_type ();
}
- std::vector<std::pair<HIR::Pattern *, BaseType *>> &get_params ()
- {
- return params;
- }
+ std::vector<FnParam> &get_params () { return params; }
- const std::vector<std::pair<HIR::Pattern *, BaseType *>> &get_params () const
- {
- return params;
- }
+ const std::vector<FnParam> &get_params () const { return params; }
- std::pair<HIR::Pattern *, BaseType *> ¶m_at (size_t idx)
- {
- return params.at (idx);
- }
+ FnParam ¶m_at (size_t idx) { return params.at (idx); }
- const std::pair<HIR::Pattern *, BaseType *> ¶m_at (size_t idx) const
- {
- return params.at (idx);
- }
+ const FnParam ¶m_at (size_t idx) const { return params.at (idx); }
BaseType *clone () const final override;
WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override
{
- return param_at (index).second;
+ return param_at (index).get_type ();
}
WARN_UNUSED_RESULT BaseType *get_return_type () const override
}
private:
- std::vector<std::pair<HIR::Pattern *, BaseType *>> params;
+ std::vector<FnParam> params;
BaseType *type;
uint8_t flags;
std::string identifier;
for (size_t i = 0; i < ltype->num_params (); i++)
{
- auto a = ltype->param_at (i).second;
- auto b = type.param_at (i).second;
+ auto a = ltype->param_at (i).get_type ();
+ auto b = type.param_at (i).get_type ();
auto unified_param
= UnifyRules::Resolve (TyTy::TyWithLocation (a),
for (size_t i = 0; i < ltype->num_params (); i++)
{
auto this_param = ltype->get_param_type_at (i);
- auto other_param = type.param_at (i).second;
+ auto other_param = type.param_at (i).get_type ();
auto unified_param
= UnifyRules::Resolve (TyTy::TyWithLocation (this_param),