2012-02-09 15:30:19 +01:00
|
|
|
import std::{ufind, map, smallintmap};
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::map::hashmap;
|
|
|
|
import driver::session;
|
2012-01-12 17:59:49 +01:00
|
|
|
import session::session;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::ast;
|
2011-07-19 17:52:34 -07:00
|
|
|
import syntax::ast::*;
|
2011-08-21 21:44:41 -07:00
|
|
|
import syntax::ast_util;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::codemap::span;
|
2011-07-07 22:18:38 -07:00
|
|
|
import metadata::csearch;
|
2011-06-15 15:14:30 -07:00
|
|
|
import util::common::*;
|
2011-07-05 11:48:19 +02:00
|
|
|
import util::ppaux::ty_to_str;
|
2011-07-19 17:52:34 -07:00
|
|
|
import util::ppaux::ty_constr_to_str;
|
|
|
|
import syntax::print::pprust::*;
|
2011-05-16 13:58:13 -07:00
|
|
|
|
2011-06-19 22:41:21 +02:00
|
|
|
export node_id_to_type;
|
|
|
|
export node_id_to_type_params;
|
2011-06-19 02:34:20 -07:00
|
|
|
export arg;
|
|
|
|
export args_eq;
|
2011-07-21 15:59:41 -07:00
|
|
|
export ast_constr_to_constr;
|
2011-06-19 02:34:20 -07:00
|
|
|
export block_ty;
|
2012-03-03 17:49:23 -08:00
|
|
|
export class_item_type;
|
|
|
|
export class_items_as_fields;
|
2011-07-19 17:52:34 -07:00
|
|
|
export constr;
|
2011-06-19 02:34:20 -07:00
|
|
|
export constr_general;
|
|
|
|
export constr_table;
|
|
|
|
export count_ty_params;
|
|
|
|
export ctxt;
|
|
|
|
export def_has_ty_params;
|
|
|
|
export expr_has_ty_params;
|
|
|
|
export expr_ty;
|
2011-07-29 16:40:23 -07:00
|
|
|
export expr_ty_params_and_ty;
|
2011-11-15 18:15:35 +01:00
|
|
|
export expr_is_lval;
|
2011-06-19 02:34:20 -07:00
|
|
|
export fold_ty;
|
|
|
|
export field;
|
|
|
|
export field_idx;
|
2011-09-13 12:14:30 +02:00
|
|
|
export get_field;
|
2012-01-30 21:00:57 -08:00
|
|
|
export get_fields;
|
2011-06-19 02:34:20 -07:00
|
|
|
export fm_general;
|
|
|
|
export get_element_type;
|
2011-06-24 14:19:58 -07:00
|
|
|
export is_binopable;
|
2011-08-24 17:24:58 -07:00
|
|
|
export is_pred_ty;
|
2012-03-03 17:49:23 -08:00
|
|
|
export lookup_class_items;
|
2011-06-19 02:34:20 -07:00
|
|
|
export lookup_item_type;
|
|
|
|
export method;
|
|
|
|
export method_idx;
|
2012-02-09 14:16:12 -08:00
|
|
|
export mk_class;
|
2011-06-19 02:34:20 -07:00
|
|
|
export mk_ctxt;
|
2012-02-10 10:28:35 -08:00
|
|
|
export mk_with_id, type_def_id;
|
2011-06-19 02:34:20 -07:00
|
|
|
export mt;
|
|
|
|
export node_type_table;
|
|
|
|
export pat_ty;
|
|
|
|
export sequence_element_type;
|
|
|
|
export sort_methods;
|
2011-06-19 22:41:21 +02:00
|
|
|
export stmt_node_id;
|
2011-06-19 02:34:20 -07:00
|
|
|
export sty;
|
|
|
|
export substitute_type_params;
|
|
|
|
export t;
|
2011-12-22 14:24:36 +01:00
|
|
|
export new_ty_hash;
|
2012-02-07 21:19:53 -08:00
|
|
|
export enum_variants, substd_enum_variants;
|
2012-01-05 10:57:19 +01:00
|
|
|
export iface_methods, store_iface_methods, impl_iface;
|
2012-01-25 14:34:31 +01:00
|
|
|
export enum_variant_with_id;
|
2011-12-28 17:50:12 +01:00
|
|
|
export ty_param_bounds_and_ty;
|
2012-02-07 11:25:04 +01:00
|
|
|
export ty_bool, mk_bool, type_is_bool;
|
|
|
|
export ty_bot, mk_bot, type_is_bot;
|
|
|
|
export ty_box, mk_box, mk_imm_box, type_is_box, type_is_boxed;
|
|
|
|
export ty_constr, mk_constr;
|
|
|
|
export ty_opaque_closure_ptr, mk_opaque_closure_ptr;
|
|
|
|
export ty_opaque_box, mk_opaque_box;
|
2011-07-19 20:15:27 -07:00
|
|
|
export ty_constr_arg;
|
2012-02-07 11:25:04 +01:00
|
|
|
export ty_float, mk_float, mk_mach_float, type_is_fp;
|
|
|
|
export ty_fn, fn_ty, mk_fn;
|
|
|
|
export ty_fn_proto, ty_fn_ret, ty_fn_ret_style;
|
|
|
|
export ty_int, mk_int, mk_mach_int, mk_char;
|
|
|
|
export ty_str, mk_str, type_is_str;
|
|
|
|
export ty_vec, mk_vec, type_is_vec;
|
|
|
|
export ty_nil, mk_nil, type_is_nil;
|
|
|
|
export ty_iface, mk_iface;
|
|
|
|
export ty_res, mk_res;
|
|
|
|
export ty_param, mk_param;
|
|
|
|
export ty_ptr, mk_ptr, mk_mut_ptr, type_is_unsafe_ptr;
|
|
|
|
export ty_rec, mk_rec;
|
|
|
|
export ty_enum, mk_enum, type_is_enum;
|
|
|
|
export ty_tup, mk_tup;
|
|
|
|
export ty_send_type, mk_send_type;
|
|
|
|
export ty_type, mk_type;
|
|
|
|
export ty_uint, mk_uint, mk_mach_uint;
|
|
|
|
export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box;
|
|
|
|
export ty_var, mk_var;
|
|
|
|
export ty_self, mk_self;
|
|
|
|
export get, type_has_params, type_has_vars, type_id;
|
2012-01-13 10:58:31 +01:00
|
|
|
export same_type;
|
2011-06-19 02:34:20 -07:00
|
|
|
export ty_var_id;
|
|
|
|
export ty_fn_args;
|
2011-07-19 17:52:34 -07:00
|
|
|
export type_constr;
|
2011-12-28 18:11:33 +01:00
|
|
|
export kind, kind_sendable, kind_copyable, kind_noncopyable;
|
|
|
|
export kind_can_be_copied, kind_can_be_sent, proto_kind, kind_lteq, type_kind;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_err;
|
|
|
|
export type_err_to_str;
|
|
|
|
export type_has_dynamic_size;
|
2011-07-29 12:53:58 +02:00
|
|
|
export type_needs_drop;
|
2011-09-07 15:13:19 +02:00
|
|
|
export type_allows_implicit_copy;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_is_integral;
|
2011-12-02 13:42:51 +01:00
|
|
|
export type_is_numeric;
|
2011-08-04 10:46:10 -07:00
|
|
|
export type_is_pod;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_is_scalar;
|
2011-10-06 12:26:12 +02:00
|
|
|
export type_is_immediate;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_is_sequence;
|
|
|
|
export type_is_signed;
|
|
|
|
export type_is_structural;
|
2011-06-25 12:22:50 +02:00
|
|
|
export type_is_copyable;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_is_tup_like;
|
2011-08-16 12:38:42 -07:00
|
|
|
export type_is_unique;
|
2012-01-10 18:33:26 -07:00
|
|
|
export type_is_c_like_enum;
|
2012-01-27 16:01:04 +01:00
|
|
|
export type_structurally_contains;
|
2011-09-07 15:13:19 +02:00
|
|
|
export type_structurally_contains_uniques;
|
2011-07-01 01:48:32 -07:00
|
|
|
export type_autoderef;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_param;
|
2012-02-03 17:10:29 -08:00
|
|
|
export canon_mode;
|
2012-02-02 16:50:17 -08:00
|
|
|
export resolved_mode;
|
|
|
|
export arg_mode;
|
|
|
|
export unify_mode;
|
|
|
|
export set_default_mode;
|
2011-06-19 02:34:20 -07:00
|
|
|
export unify;
|
|
|
|
export variant_info;
|
|
|
|
export walk_ty;
|
2011-08-03 18:06:57 -07:00
|
|
|
export occurs_check_fails;
|
2011-12-15 11:06:48 -08:00
|
|
|
export closure_kind;
|
2012-01-10 06:49:15 -08:00
|
|
|
export ck_block;
|
|
|
|
export ck_box;
|
|
|
|
export ck_uniq;
|
2012-01-02 12:00:40 +01:00
|
|
|
export param_bound, param_bounds, bound_copy, bound_send, bound_iface;
|
2011-12-28 17:50:12 +01:00
|
|
|
export param_bounds_to_kind;
|
2012-02-02 16:50:17 -08:00
|
|
|
export default_arg_mode_for_ty;
|
2012-02-10 06:01:32 -08:00
|
|
|
export item_path;
|
2012-02-14 15:21:53 -08:00
|
|
|
export item_path_str;
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Data types
|
2011-05-09 12:27:03 -07:00
|
|
|
|
2012-02-02 16:50:17 -08:00
|
|
|
// Note: after typeck, you should use resolved_mode() to convert this mode
|
|
|
|
// into an rmode, which will take into account the results of mode inference.
|
2012-01-30 21:00:57 -08:00
|
|
|
type arg = {mode: ast::mode, ty: t};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
type field = {ident: ast::ident, mt: mt};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-01-02 12:00:40 +01:00
|
|
|
type param_bounds = @[param_bound];
|
|
|
|
|
2012-02-13 18:56:09 +01:00
|
|
|
type method = {ident: ast::ident,
|
|
|
|
tps: @[param_bounds],
|
|
|
|
fty: fn_ty,
|
|
|
|
purity: ast::purity};
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2011-08-12 07:15:18 -07:00
|
|
|
type constr_table = hashmap<ast::node_id, [constr]>;
|
2011-05-19 17:21:21 -07:00
|
|
|
|
2012-02-15 11:25:39 -08:00
|
|
|
type mt = {ty: t, mutbl: ast::mutability};
|
2011-03-17 17:39:47 -07:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-04-25 12:15:55 -07:00
|
|
|
// Contains information needed to resolve types and (in the future) look up
|
|
|
|
// the types of AST nodes.
|
2012-02-03 15:15:28 +01:00
|
|
|
type creader_cache = hashmap<{cnum: int, pos: uint, len: uint}, t>;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-10 10:28:35 -08:00
|
|
|
type intern_key = {struct: sty, o_def_id: option<ast::def_id>};
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
type ctxt =
|
2012-02-10 10:28:35 -08:00
|
|
|
@{interner: hashmap<intern_key, t_box>,
|
2012-02-03 15:15:28 +01:00
|
|
|
mutable next_id: uint,
|
2011-07-27 14:19:39 +02:00
|
|
|
sess: session::session,
|
|
|
|
def_map: resolve::def_map,
|
|
|
|
node_types: node_type_table,
|
2012-01-30 17:28:30 +01:00
|
|
|
node_type_substs: hashmap<node_id, [t]>,
|
2011-07-27 14:19:39 +02:00
|
|
|
items: ast_map::map,
|
|
|
|
freevars: freevars::freevar_map,
|
|
|
|
tcache: type_cache,
|
|
|
|
rcache: creader_cache,
|
2011-09-02 15:34:58 -07:00
|
|
|
short_names_cache: hashmap<t, @str>,
|
2011-11-22 13:16:23 +01:00
|
|
|
needs_drop_cache: hashmap<t, bool>,
|
2011-12-28 18:11:33 +01:00
|
|
|
kind_cache: hashmap<t, kind>,
|
2012-01-31 17:05:20 -08:00
|
|
|
ast_ty_to_ty_cache: hashmap<@ast::ty, option<t>>,
|
2012-01-25 14:34:31 +01:00
|
|
|
enum_var_cache: hashmap<def_id, @[variant_info]>,
|
2011-12-28 17:50:12 +01:00
|
|
|
iface_method_cache: hashmap<def_id, @[method]>,
|
2012-02-02 16:50:17 -08:00
|
|
|
ty_param_bounds: hashmap<ast::node_id, param_bounds>,
|
|
|
|
inferred_modes: hashmap<ast::node_id, ast::mode>};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
type t_box = @{struct: sty,
|
|
|
|
id: uint,
|
|
|
|
has_params: bool,
|
|
|
|
has_vars: bool,
|
2012-02-10 10:28:35 -08:00
|
|
|
o_def_id: option<ast::def_id>};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
// To reduce refcounting cost, we're representing types as unsafe pointers
|
|
|
|
// throughout the compiler. These are simply casted t_box values. Use ty::get
|
|
|
|
// to cast them back to a box. (Without the cast, compiler performance suffers
|
|
|
|
// ~15%.) This does mean that a t value relies on the ctxt to keep its box
|
|
|
|
// alive, and using ty::get is unsafe when the ctxt is no longer alive.
|
|
|
|
enum t_opaque {}
|
|
|
|
type t = *t_opaque;
|
2011-05-11 17:05:39 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn get(t: t) -> t_box unsafe {
|
|
|
|
let t2 = unsafe::reinterpret_cast::<t, t_box>(t);
|
|
|
|
let t3 = t2;
|
|
|
|
unsafe::leak(t2);
|
|
|
|
t3
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_has_params(t: t) -> bool { get(t).has_params }
|
|
|
|
fn type_has_vars(t: t) -> bool { get(t).has_vars }
|
2012-02-10 10:28:35 -08:00
|
|
|
fn type_def_id(t: t) -> option<ast::def_id> { get(t).o_def_id }
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_id(t: t) -> uint { get(t).id }
|
2011-04-20 10:51:41 -07:00
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
enum closure_kind {
|
2012-01-19 17:56:05 -08:00
|
|
|
ck_block,
|
|
|
|
ck_box,
|
|
|
|
ck_uniq,
|
2011-12-15 11:06:48 -08:00
|
|
|
}
|
|
|
|
|
2011-12-23 16:09:52 +01:00
|
|
|
type fn_ty = {proto: ast::proto,
|
|
|
|
inputs: [arg],
|
|
|
|
output: t,
|
|
|
|
ret_style: ret_style,
|
|
|
|
constraints: [@constr]};
|
|
|
|
|
2010-12-21 12:13:51 -08:00
|
|
|
// NB: If you change this, you'll probably want to change the corresponding
|
2011-05-12 17:24:54 +02:00
|
|
|
// AST structure in front/ast::rs as well.
|
2012-01-19 14:24:03 -08:00
|
|
|
enum sty {
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_nil,
|
|
|
|
ty_bot,
|
|
|
|
ty_bool,
|
|
|
|
ty_int(ast::int_ty),
|
|
|
|
ty_uint(ast::uint_ty),
|
|
|
|
ty_float(ast::float_ty),
|
|
|
|
ty_str,
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(def_id, [t]),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_box(mt),
|
|
|
|
ty_uniq(mt),
|
|
|
|
ty_vec(mt),
|
|
|
|
ty_ptr(mt),
|
|
|
|
ty_rec([field]),
|
|
|
|
ty_fn(fn_ty),
|
|
|
|
ty_iface(def_id, [t]),
|
2012-02-09 14:16:12 -08:00
|
|
|
ty_class(def_id, [t]),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_res(def_id, t, [t]),
|
|
|
|
ty_tup([t]),
|
|
|
|
|
2012-01-30 11:52:34 +01:00
|
|
|
ty_var(int), // type variable during typechecking
|
|
|
|
ty_param(uint, def_id), // type parameter
|
|
|
|
ty_self([t]), // interface method self type
|
2012-01-19 17:56:05 -08:00
|
|
|
|
|
|
|
ty_type, // type_desc*
|
|
|
|
ty_send_type, // type_desc* that has been cloned into exchange heap
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_opaque_box, // used by monomorphizer to represend any @ box
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_constr(t, [@type_constr]),
|
|
|
|
ty_opaque_closure_ptr(closure_kind), // ptr to env for fn, fn@, fn~
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2011-07-19 17:52:34 -07:00
|
|
|
// In the middle end, constraints have a def_id attached, referring
|
|
|
|
// to the definition of the operator in the constraint.
|
2011-08-12 06:36:51 -07:00
|
|
|
type constr_general<ARG> = spanned<constr_general_<ARG, def_id>>;
|
2011-11-30 13:38:38 +01:00
|
|
|
type type_constr = constr_general<@path>;
|
2011-08-12 07:15:18 -07:00
|
|
|
type constr = constr_general<uint>;
|
2010-12-21 17:47:13 -08:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Data structures used in type unification
|
2012-01-19 14:24:03 -08:00
|
|
|
enum type_err {
|
2012-01-19 17:56:05 -08:00
|
|
|
terr_mismatch,
|
|
|
|
terr_ret_style_mismatch(ast::ret_style, ast::ret_style),
|
|
|
|
terr_box_mutability,
|
2012-02-21 17:02:02 +01:00
|
|
|
terr_ptr_mutability,
|
2012-01-19 17:56:05 -08:00
|
|
|
terr_vec_mutability,
|
|
|
|
terr_tuple_size(uint, uint),
|
|
|
|
terr_record_size(uint, uint),
|
|
|
|
terr_record_mutability,
|
|
|
|
terr_record_fields(ast::ident, ast::ident),
|
|
|
|
terr_arg_count,
|
|
|
|
terr_mode_mismatch(mode, mode),
|
|
|
|
terr_constr_len(uint, uint),
|
|
|
|
terr_constr_mismatch(@type_constr, @type_constr),
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
enum param_bound {
|
2012-01-19 17:56:05 -08:00
|
|
|
bound_copy,
|
|
|
|
bound_send,
|
|
|
|
bound_iface(t),
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 12:00:40 +01:00
|
|
|
fn param_bounds_to_kind(bounds: param_bounds) -> kind {
|
2011-12-28 18:11:33 +01:00
|
|
|
let kind = kind_noncopyable;
|
2011-12-28 17:50:12 +01:00
|
|
|
for bound in *bounds {
|
|
|
|
alt bound {
|
2012-01-18 22:37:22 -08:00
|
|
|
bound_copy {
|
2011-12-28 18:11:33 +01:00
|
|
|
if kind != kind_sendable { kind = kind_copyable; }
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
bound_send { kind = kind_sendable; }
|
2011-12-28 17:50:12 +01:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
|
2012-01-02 12:09:26 +01:00
|
|
|
type ty_param_bounds_and_ty = {bounds: @[param_bounds], ty: t};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-12-28 17:50:12 +01:00
|
|
|
type type_cache = hashmap<ast::def_id, ty_param_bounds_and_ty>;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-01-30 17:28:30 +01:00
|
|
|
type node_type_table = @smallintmap::smallintmap<t>;
|
2011-05-12 16:36:47 -07:00
|
|
|
|
2011-04-29 15:26:28 +00:00
|
|
|
fn mk_rcache() -> creader_cache {
|
2011-07-27 14:19:39 +02:00
|
|
|
type val = {cnum: int, pos: uint, len: uint};
|
2011-09-12 11:27:30 +02:00
|
|
|
fn hash_cache_entry(k: val) -> uint {
|
2011-07-26 14:06:02 +02:00
|
|
|
ret (k.cnum as uint) + k.pos + k.len;
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-09-12 11:27:30 +02:00
|
|
|
fn eq_cache_entries(a: val, b: val) -> bool {
|
2011-07-26 14:06:02 +02:00
|
|
|
ret a.cnum == b.cnum && a.pos == b.pos && a.len == b.len;
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-07-25 15:07:48 +02:00
|
|
|
ret map::mk_hashmap(hash_cache_entry, eq_cache_entries);
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-04-25 12:15:55 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn new_ty_hash<V: copy>() -> map::hashmap<t, V> {
|
|
|
|
map::mk_hashmap({|&&t: t| type_id(t)},
|
|
|
|
{|&&a: t, &&b: t| type_id(a) == type_id(b)})
|
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2011-12-20 04:17:47 +08:00
|
|
|
fn mk_ctxt(s: session::session, dm: resolve::def_map, amap: ast_map::map,
|
2011-07-27 14:19:39 +02:00
|
|
|
freevars: freevars::freevar_map) -> ctxt {
|
2012-02-10 10:28:35 -08:00
|
|
|
let interner = map::mk_hashmap({|&&k: intern_key|
|
|
|
|
hash_type_structure(k.struct) +
|
|
|
|
option::maybe(0u, k.o_def_id, ast_util::hash_def_id)
|
2012-02-03 15:15:28 +01:00
|
|
|
}, {|&&a, &&b| a == b});
|
|
|
|
@{interner: interner,
|
|
|
|
mutable next_id: 0u,
|
|
|
|
sess: s,
|
|
|
|
def_map: dm,
|
|
|
|
node_types: @smallintmap::mk(),
|
|
|
|
node_type_substs: map::new_int_hash(),
|
|
|
|
items: amap,
|
|
|
|
freevars: freevars,
|
|
|
|
tcache: new_def_hash(),
|
|
|
|
rcache: mk_rcache(),
|
|
|
|
short_names_cache: new_ty_hash(),
|
|
|
|
needs_drop_cache: new_ty_hash(),
|
|
|
|
kind_cache: new_ty_hash(),
|
|
|
|
ast_ty_to_ty_cache: map::mk_hashmap(ast_util::hash_ty, ast_util::eq_ty),
|
|
|
|
enum_var_cache: new_def_hash(),
|
|
|
|
iface_method_cache: new_def_hash(),
|
|
|
|
ty_param_bounds: map::new_int_hash(),
|
|
|
|
inferred_modes: map::new_int_hash()}
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-05-11 17:05:39 -07:00
|
|
|
|
|
|
|
|
2011-04-20 10:51:41 -07:00
|
|
|
// Type constructors
|
2012-02-10 10:28:35 -08:00
|
|
|
fn mk_t(cx: ctxt, st: sty) -> t { mk_t_with_id(cx, st, none) }
|
2012-02-03 15:15:28 +01:00
|
|
|
|
|
|
|
// Interns a type/name combination, stores the resulting box in cx.interner,
|
|
|
|
// and returns the box as cast to an unsafe ptr (see comments for t above).
|
2012-02-10 10:28:35 -08:00
|
|
|
fn mk_t_with_id(cx: ctxt, st: sty, o_def_id: option<ast::def_id>) -> t {
|
|
|
|
let key = {struct: st, o_def_id: o_def_id};
|
2012-02-03 15:15:28 +01:00
|
|
|
alt cx.interner.find(key) {
|
|
|
|
some(t) { unsafe { ret unsafe::reinterpret_cast(t); } }
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
let has_params = false, has_vars = false;
|
|
|
|
fn derive_flags(&has_params: bool, &has_vars: bool, tt: t) {
|
|
|
|
let t = get(tt);
|
|
|
|
has_params |= t.has_params;
|
|
|
|
has_vars |= t.has_vars;
|
2011-04-25 16:17:14 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
alt st {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_str | ty_type | ty_send_type | ty_opaque_closure_ptr(_) |
|
|
|
|
ty_opaque_box {}
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_param(_, _) { has_params = true; }
|
2012-01-30 11:52:34 +01:00
|
|
|
ty_var(_) | ty_self(_) { has_vars = true; }
|
2012-02-09 14:16:12 -08:00
|
|
|
ty_enum(_, tys) | ty_iface(_, tys) | ty_class(_, tys) {
|
2012-02-03 15:15:28 +01:00
|
|
|
for tt in tys { derive_flags(has_params, has_vars, tt); }
|
|
|
|
}
|
|
|
|
ty_box(m) | ty_uniq(m) | ty_vec(m) | ty_ptr(m) {
|
|
|
|
derive_flags(has_params, has_vars, m.ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_rec(flds) {
|
2012-02-03 15:15:28 +01:00
|
|
|
for f in flds { derive_flags(has_params, has_vars, f.mt.ty); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-15 12:08:05 +02:00
|
|
|
ty_tup(ts) {
|
2012-02-03 15:15:28 +01:00
|
|
|
for tt in ts { derive_flags(has_params, has_vars, tt); }
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
ty_fn(f) {
|
2012-02-03 15:15:28 +01:00
|
|
|
for a in f.inputs { derive_flags(has_params, has_vars, a.ty); }
|
|
|
|
derive_flags(has_params, has_vars, f.output);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_res(_, tt, tps) {
|
2012-02-03 15:15:28 +01:00
|
|
|
derive_flags(has_params, has_vars, tt);
|
|
|
|
for tt in tps { derive_flags(has_params, has_vars, tt); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_constr(tt, _) {
|
|
|
|
derive_flags(has_params, has_vars, tt);
|
2011-12-19 13:52:58 +01:00
|
|
|
}
|
2011-04-25 16:17:14 -07:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
let t = @{struct: st,
|
|
|
|
id: cx.next_id,
|
|
|
|
has_params: has_params,
|
|
|
|
has_vars: has_vars,
|
2012-02-10 10:28:35 -08:00
|
|
|
o_def_id: o_def_id};
|
2012-02-03 15:15:28 +01:00
|
|
|
cx.interner.insert(key, t);
|
|
|
|
cx.next_id += 1u;
|
|
|
|
unsafe { unsafe::reinterpret_cast(t) }
|
2011-05-11 17:05:39 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_nil(cx: ctxt) -> t { mk_t(cx, ty_nil) }
|
2011-04-21 19:30:53 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_bot(cx: ctxt) -> t { mk_t(cx, ty_bot) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_bool(cx: ctxt) -> t { mk_t(cx, ty_bool) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_int(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i)) }
|
2011-04-25 16:17:14 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_float(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f)) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_uint(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u)) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_mach_int(cx: ctxt, tm: ast::int_ty) -> t { mk_t(cx, ty_int(tm)) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_mach_uint(cx: ctxt, tm: ast::uint_ty) -> t { mk_t(cx, ty_uint(tm)) }
|
2011-04-20 10:51:41 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_mach_float(cx: ctxt, tm: ast::float_ty) -> t { mk_t(cx, ty_float(tm)) }
|
2011-12-07 21:06:12 +01:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_char(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_char)) }
|
2011-12-07 21:06:12 +01:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_str(cx: ctxt) -> t { mk_t(cx, ty_str) }
|
2011-04-20 11:23:36 -07:00
|
|
|
|
2012-01-25 14:34:31 +01:00
|
|
|
fn mk_enum(cx: ctxt, did: ast::def_id, tys: [t]) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
mk_t(cx, ty_enum(did, tys))
|
2011-04-20 18:52:04 -07:00
|
|
|
}
|
2011-04-20 11:23:36 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_box(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_box(tm)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2012-02-15 11:25:39 -08:00
|
|
|
fn mk_imm_box(cx: ctxt, ty: t) -> t { mk_box(cx, {ty: ty,
|
|
|
|
mutbl: ast::m_imm}) }
|
2011-09-21 18:54:54 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_uniq(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_uniq(tm)) }
|
2011-08-10 17:23:46 -07:00
|
|
|
|
2012-02-15 11:25:39 -08:00
|
|
|
fn mk_imm_uniq(cx: ctxt, ty: t) -> t { mk_uniq(cx, {ty: ty,
|
|
|
|
mutbl: ast::m_imm}) }
|
2011-06-03 15:02:58 -04:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_ptr(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_ptr(tm)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2012-02-15 11:25:39 -08:00
|
|
|
fn mk_mut_ptr(cx: ctxt, ty: t) -> t { mk_ptr(cx, {ty: ty,
|
|
|
|
mutbl: ast::m_mutbl}) }
|
2011-08-02 16:24:38 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_vec(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_vec(tm)) }
|
2011-06-09 16:23:19 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_rec(cx: ctxt, fs: [field]) -> t { mk_t(cx, ty_rec(fs)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn mk_constr(cx: ctxt, t: t, cs: [@type_constr]) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
mk_t(cx, ty_constr(t, cs))
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_tup(cx: ctxt, ts: [t]) -> t { mk_t(cx, ty_tup(ts)) }
|
2011-08-15 11:40:26 +02:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_fn(cx: ctxt, fty: fn_ty) -> t { mk_t(cx, ty_fn(fty)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2011-12-20 16:33:55 +01:00
|
|
|
fn mk_iface(cx: ctxt, did: ast::def_id, tys: [t]) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
mk_t(cx, ty_iface(did, tys))
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:16:12 -08:00
|
|
|
fn mk_class(cx: ctxt, class_id: ast::def_id, tys: [t]) -> t {
|
|
|
|
mk_t(cx, ty_class(class_id, tys))
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn mk_res(cx: ctxt, did: ast::def_id, inner: t, tps: [t]) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
mk_t(cx, ty_res(did, inner, tps))
|
2011-06-24 18:10:40 +02:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_var(cx: ctxt, v: int) -> t { mk_t(cx, ty_var(v)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_self(cx: ctxt, tps: [t]) -> t { mk_t(cx, ty_self(tps)) }
|
2012-01-30 11:52:34 +01:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_param(cx: ctxt, n: uint, k: def_id) -> t { mk_t(cx, ty_param(n, k)) }
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_type(cx: ctxt) -> t { mk_t(cx, ty_type) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn mk_send_type(cx: ctxt) -> t { mk_t(cx, ty_send_type) }
|
2011-12-15 11:06:48 -08:00
|
|
|
|
2012-01-05 16:19:12 -08:00
|
|
|
fn mk_opaque_closure_ptr(cx: ctxt, ck: closure_kind) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
mk_t(cx, ty_opaque_closure_ptr(ck))
|
2011-12-15 11:06:48 -08:00
|
|
|
}
|
|
|
|
|
2012-02-07 11:25:04 +01:00
|
|
|
fn mk_opaque_box(cx: ctxt) -> t { mk_t(cx, ty_opaque_box) }
|
|
|
|
|
2012-02-10 10:28:35 -08:00
|
|
|
fn mk_with_id(cx: ctxt, base: t, def_id: ast::def_id) -> t {
|
|
|
|
mk_t_with_id(cx, get(base).struct, some(def_id))
|
2011-12-23 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converts s to its machine type equivalent
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn mach_sty(cfg: @session::config, t: t) -> sty {
|
|
|
|
alt get(t).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_int(ast::ty_i) { ty_int(cfg.int_type) }
|
|
|
|
ty_uint(ast::ty_u) { ty_uint(cfg.uint_type) }
|
|
|
|
ty_float(ast::ty_f) { ty_float(cfg.float_type) }
|
2011-12-23 01:57:35 +01:00
|
|
|
s { s }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn default_arg_mode_for_ty(ty: ty::t) -> ast::rmode {
|
|
|
|
if ty::type_is_immediate(ty) { ast::by_val }
|
2012-02-02 16:50:17 -08:00
|
|
|
else { ast::by_ref }
|
|
|
|
}
|
|
|
|
|
2012-01-31 12:38:22 +01:00
|
|
|
fn walk_ty(cx: ctxt, ty: t, f: fn(t)) {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_str | ty_send_type | ty_type | ty_opaque_box |
|
2012-01-31 12:38:22 +01:00
|
|
|
ty_opaque_closure_ptr(_) | ty_var(_) | ty_param(_, _) {}
|
|
|
|
ty_box(tm) | ty_vec(tm) | ty_ptr(tm) { walk_ty(cx, tm.ty, f); }
|
2012-02-09 14:16:12 -08:00
|
|
|
ty_enum(_, subtys) | ty_iface(_, subtys) | ty_class(_, subtys)
|
|
|
|
| ty_self(subtys) {
|
2012-01-31 12:38:22 +01:00
|
|
|
for subty: t in subtys { walk_ty(cx, subty, f); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_rec(fields) {
|
2012-01-31 12:38:22 +01:00
|
|
|
for fl: field in fields { walk_ty(cx, fl.mt.ty, f); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
ty_tup(ts) { for tt in ts { walk_ty(cx, tt, f); } }
|
|
|
|
ty_fn(ft) {
|
|
|
|
for a: arg in ft.inputs { walk_ty(cx, a.ty, f); }
|
|
|
|
walk_ty(cx, ft.output, f);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_res(_, sub, tps) {
|
2012-01-31 12:38:22 +01:00
|
|
|
walk_ty(cx, sub, f);
|
|
|
|
for tp: t in tps { walk_ty(cx, tp, f); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
ty_constr(sub, _) { walk_ty(cx, sub, f); }
|
|
|
|
ty_uniq(tm) { walk_ty(cx, tm.ty, f); }
|
2011-04-15 12:23:00 -07:00
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
f(ty);
|
2011-04-15 12:23:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
enum fold_mode {
|
2012-01-19 17:56:05 -08:00
|
|
|
fm_var(fn@(int) -> t),
|
|
|
|
fm_param(fn@(uint, def_id) -> t),
|
|
|
|
fm_general(fn@(t) -> t),
|
2011-06-09 11:20:47 -07:00
|
|
|
}
|
2011-04-15 12:23:00 -07:00
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn fold_ty(cx: ctxt, fld: fold_mode, ty_0: t) -> t {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ty = ty_0;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
let tb = get(ty);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt fld {
|
2012-02-03 15:15:28 +01:00
|
|
|
fm_var(_) { if !tb.has_vars { ret ty; } }
|
|
|
|
fm_param(_) { if !tb.has_params { ret ty; } }
|
2011-07-27 14:19:39 +02:00
|
|
|
fm_general(_) {/* no fast path */ }
|
|
|
|
}
|
2012-02-02 16:50:17 -08:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
alt tb.struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_str | ty_type | ty_send_type | ty_opaque_closure_ptr(_) |
|
|
|
|
ty_opaque_box {}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_box(tm) {
|
2012-02-15 11:25:39 -08:00
|
|
|
ty = mk_box(cx, {ty: fold_ty(cx, fld, tm.ty), mutbl: tm.mutbl});
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-09-21 18:54:54 -07:00
|
|
|
ty_uniq(tm) {
|
2012-02-15 11:25:39 -08:00
|
|
|
ty = mk_uniq(cx, {ty: fold_ty(cx, fld, tm.ty), mutbl: tm.mutbl});
|
2011-09-21 18:54:54 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_ptr(tm) {
|
2012-02-15 11:25:39 -08:00
|
|
|
ty = mk_ptr(cx, {ty: fold_ty(cx, fld, tm.ty), mutbl: tm.mutbl});
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-18 14:11:06 -07:00
|
|
|
ty_vec(tm) {
|
2012-02-15 11:25:39 -08:00
|
|
|
ty = mk_vec(cx, {ty: fold_ty(cx, fld, tm.ty), mutbl: tm.mutbl});
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(tid, subtys) {
|
|
|
|
ty = mk_enum(cx, tid, vec::map(subtys, {|t| fold_ty(cx, fld, t) }));
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
|
|
|
ty_iface(did, subtys) {
|
|
|
|
ty = mk_iface(cx, did, vec::map(subtys, {|t| fold_ty(cx, fld, t) }));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-30 11:52:34 +01:00
|
|
|
ty_self(subtys) {
|
|
|
|
ty = mk_self(cx, vec::map(subtys, {|t| fold_ty(cx, fld, t) }));
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(fields) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let new_fields: [field] = [];
|
2011-08-15 21:54:52 -07:00
|
|
|
for fl: field in fields {
|
2011-07-27 14:19:39 +02:00
|
|
|
let new_ty = fold_ty(cx, fld, fl.mt.ty);
|
2012-02-15 11:25:39 -08:00
|
|
|
let new_mt = {ty: new_ty, mutbl: fl.mt.mutbl};
|
2011-08-19 15:16:48 -07:00
|
|
|
new_fields += [{ident: fl.ident, mt: new_mt}];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-12-19 13:52:58 +01:00
|
|
|
ty = mk_rec(cx, new_fields);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-15 12:08:05 +02:00
|
|
|
ty_tup(ts) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let new_ts = [];
|
|
|
|
for tt in ts { new_ts += [fold_ty(cx, fld, tt)]; }
|
2011-12-19 13:52:58 +01:00
|
|
|
ty = mk_tup(cx, new_ts);
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
ty_fn(f) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let new_args: [arg] = [];
|
2011-12-23 16:09:52 +01:00
|
|
|
for a: arg in f.inputs {
|
2011-07-27 14:19:39 +02:00
|
|
|
let new_ty = fold_ty(cx, fld, a.ty);
|
2011-08-19 15:16:48 -07:00
|
|
|
new_args += [{mode: a.mode, ty: new_ty}];
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
ty = mk_fn(cx, {inputs: new_args,
|
|
|
|
output: fold_ty(cx, fld, f.output)
|
|
|
|
with f});
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_res(did, subty, tps) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let new_tps = [];
|
|
|
|
for tp: t in tps { new_tps += [fold_ty(cx, fld, tp)]; }
|
2011-12-19 13:52:58 +01:00
|
|
|
ty = mk_res(cx, did, fold_ty(cx, fld, subty), new_tps);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_var(id) {
|
|
|
|
alt fld { fm_var(folder) { ty = folder(id); } _ {/* no-op */ } }
|
|
|
|
}
|
2011-12-29 11:23:35 +01:00
|
|
|
ty_param(id, did) {
|
2011-12-29 13:12:52 +01:00
|
|
|
alt fld { fm_param(folder) { ty = folder(id, did); } _ {} }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-03 15:50:05 -08:00
|
|
|
ty_constr(subty, cs) {
|
|
|
|
ty = mk_constr(cx, fold_ty(cx, fld, subty), cs);
|
|
|
|
}
|
|
|
|
_ {
|
2012-03-05 16:27:27 -08:00
|
|
|
cx.sess.bug("unsupported sort of type in fold_ty");
|
2012-01-03 15:50:05 -08:00
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
2012-02-23 17:44:03 +01:00
|
|
|
alt tb.o_def_id {
|
|
|
|
some(did) { ty = mk_t_with_id(cx, get(ty).struct, some(did)); }
|
|
|
|
_ {}
|
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2011-06-09 11:20:47 -07:00
|
|
|
// If this is a general type fold, then we need to run it now.
|
2011-07-27 14:19:39 +02:00
|
|
|
alt fld { fm_general(folder) { ret folder(ty); } _ { ret ty; } }
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Type utilities
|
2011-07-20 19:04:45 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_nil(ty: t) -> bool { get(ty).struct == ty_nil }
|
2011-05-14 19:02:30 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_bot(ty: t) -> bool { get(ty).struct == ty_bot }
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_bool(ty: t) -> bool { get(ty).struct == ty_bool }
|
2011-04-19 15:22:57 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_structural(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-03-03 17:49:23 -08:00
|
|
|
ty_rec(_) | ty_class(_,_) | ty_tup(_) | ty_enum(_, _) | ty_fn(_) |
|
2012-02-10 11:32:03 +01:00
|
|
|
ty_iface(_, _) | ty_res(_, _, _) { true }
|
2011-12-20 16:33:55 +01:00
|
|
|
_ { false }
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn type_is_copyable(cx: ctxt, ty: t) -> bool {
|
2011-12-28 18:11:33 +01:00
|
|
|
ret kind_can_be_copied(type_kind(cx, ty));
|
2011-06-25 12:22:50 +02:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_sequence(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { ret true; }
|
2011-08-18 14:11:06 -07:00
|
|
|
ty_vec(_) { ret true; }
|
2011-07-27 14:19:39 +02:00
|
|
|
_ { ret false; }
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_str(ty: t) -> bool { get(ty).struct == ty_str }
|
2011-06-20 13:46:44 -07:00
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn sequence_element_type(cx: ctxt, ty: t) -> t {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { ret mk_mach_uint(cx, ast::ty_u8); }
|
2011-08-18 14:11:06 -07:00
|
|
|
ty_vec(mt) { ret mt.ty; }
|
2011-09-02 15:34:58 -07:00
|
|
|
_ { cx.sess.bug("sequence_element_type called on non-sequence value"); }
|
2011-03-03 18:18:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_tup_like(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-01-18 15:42:00 -08:00
|
|
|
ty_rec(_) | ty_tup(_) { true }
|
2011-09-17 10:17:40 -07:00
|
|
|
_ { false }
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn get_element_type(ty: t, i: uint) -> t {
|
|
|
|
alt get(ty).struct {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_rec(flds) { ret flds[i].mt.ty; }
|
|
|
|
ty_tup(ts) { ret ts[i]; }
|
2012-02-03 15:15:28 +01:00
|
|
|
_ { fail "get_element_type called on invalid type"; }
|
2011-01-19 16:29:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_box(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-09-21 14:00:11 -07:00
|
|
|
ty_box(_) { ret true; }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_boxed(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-02-10 11:32:03 +01:00
|
|
|
ty_box(_) | ty_opaque_box { true }
|
2012-02-07 11:25:04 +01:00
|
|
|
_ { false }
|
2011-09-21 14:00:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_unique_box(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-09-21 14:00:11 -07:00
|
|
|
ty_uniq(_) { ret true; }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-18 00:41:25 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_unsafe_ptr(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-11-02 12:15:50 +01:00
|
|
|
ty_ptr(_) { ret true; }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_vec(ty: t) -> bool {
|
|
|
|
ret alt get(ty).struct {
|
2011-09-02 15:34:58 -07:00
|
|
|
ty_vec(_) { true }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { true }
|
2011-09-02 15:34:58 -07:00
|
|
|
_ { false }
|
|
|
|
};
|
2011-08-25 10:18:02 +02:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_unique(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-08-25 10:18:02 +02:00
|
|
|
ty_uniq(_) { ret true; }
|
|
|
|
ty_vec(_) { true }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { true }
|
2011-09-02 15:34:58 -07:00
|
|
|
_ { ret false; }
|
|
|
|
}
|
2011-08-16 12:38:42 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn type_is_scalar(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
|
2012-02-01 11:54:14 +01:00
|
|
|
ty_send_type | ty_type | ty_ptr(_) { true }
|
2011-12-07 21:06:12 +01:00
|
|
|
_ { false }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-05-19 18:28:09 -07:00
|
|
|
}
|
|
|
|
|
2011-10-06 12:26:12 +02:00
|
|
|
// FIXME maybe inline this for speed?
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_immediate(ty: t) -> bool {
|
|
|
|
ret type_is_scalar(ty) || type_is_boxed(ty) ||
|
|
|
|
type_is_unique(ty);
|
2011-10-06 12:26:12 +02:00
|
|
|
}
|
|
|
|
|
2011-11-22 13:16:23 +01:00
|
|
|
fn type_needs_drop(cx: ctxt, ty: t) -> bool {
|
|
|
|
alt cx.needs_drop_cache.find(ty) {
|
2011-07-27 14:19:39 +02:00
|
|
|
some(result) { ret result; }
|
2012-01-18 22:37:22 -08:00
|
|
|
none {/* fall through */ }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
2011-11-22 13:16:23 +01:00
|
|
|
let accum = false;
|
2012-02-03 15:15:28 +01:00
|
|
|
let result = alt get(ty).struct {
|
2011-07-27 14:19:39 +02:00
|
|
|
// scalar types
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
|
2012-02-01 11:54:14 +01:00
|
|
|
ty_type | ty_ptr(_) { false }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(flds) {
|
2011-11-22 13:16:23 +01:00
|
|
|
for f in flds { if type_needs_drop(cx, f.mt.ty) { accum = true; } }
|
|
|
|
accum
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ty_tup(elts) {
|
2011-11-22 13:16:23 +01:00
|
|
|
for m in elts { if type_needs_drop(cx, m) { accum = true; } }
|
|
|
|
accum
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
let variants = enum_variants(cx, did);
|
2011-12-15 17:14:58 -08:00
|
|
|
for variant in *variants {
|
2011-11-22 13:16:23 +01:00
|
|
|
for aty in variant.args {
|
2011-07-27 14:19:39 +02:00
|
|
|
// Perform any type parameter substitutions.
|
|
|
|
let arg_ty = substitute_type_params(cx, tps, aty);
|
2011-11-22 13:16:23 +01:00
|
|
|
if type_needs_drop(cx, arg_ty) { accum = true; }
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
2011-11-22 13:16:23 +01:00
|
|
|
if accum { break; }
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
2011-11-22 13:16:23 +01:00
|
|
|
accum
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-11-22 13:16:23 +01:00
|
|
|
_ { true }
|
|
|
|
};
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2011-11-22 13:16:23 +01:00
|
|
|
cx.needs_drop_cache.insert(ty, result);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret result;
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2012-01-19 17:56:05 -08:00
|
|
|
enum kind { kind_sendable, kind_copyable, kind_noncopyable, }
|
2011-12-28 18:11:33 +01:00
|
|
|
|
|
|
|
// Using these query functons is preferable to direct comparison or matching
|
|
|
|
// against the kind constants, as we may modify the kind hierarchy in the
|
|
|
|
// future.
|
|
|
|
pure fn kind_can_be_copied(k: kind) -> bool {
|
|
|
|
ret alt k {
|
2012-01-18 22:37:22 -08:00
|
|
|
kind_sendable { true }
|
|
|
|
kind_copyable { true }
|
|
|
|
kind_noncopyable { false }
|
2011-12-28 18:11:33 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn kind_can_be_sent(k: kind) -> bool {
|
|
|
|
ret alt k {
|
2012-01-18 22:37:22 -08:00
|
|
|
kind_sendable { true }
|
|
|
|
kind_copyable { false }
|
|
|
|
kind_noncopyable { false }
|
2011-12-28 18:11:33 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn proto_kind(p: proto) -> kind {
|
|
|
|
alt p {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::proto_any { kind_noncopyable }
|
|
|
|
ast::proto_block { kind_noncopyable }
|
|
|
|
ast::proto_box { kind_copyable }
|
|
|
|
ast::proto_uniq { kind_sendable }
|
|
|
|
ast::proto_bare { kind_sendable }
|
2011-12-28 18:11:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 18:15:35 +01:00
|
|
|
fn kind_lteq(a: kind, b: kind) -> bool {
|
|
|
|
alt a {
|
2012-01-18 22:37:22 -08:00
|
|
|
kind_noncopyable { true }
|
|
|
|
kind_copyable { b != kind_noncopyable }
|
|
|
|
kind_sendable { b == kind_sendable }
|
2011-11-15 18:15:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_kind(a: kind, b: kind) -> kind {
|
2012-02-03 15:15:28 +01:00
|
|
|
if kind_lteq(a, b) { a } else { b }
|
2011-11-15 18:15:35 +01:00
|
|
|
}
|
|
|
|
|
2011-12-28 18:11:33 +01:00
|
|
|
fn type_kind(cx: ctxt, ty: t) -> kind {
|
2011-07-27 17:49:00 -07:00
|
|
|
alt cx.kind_cache.find(ty) {
|
|
|
|
some(result) { ret result; }
|
2012-01-18 22:37:22 -08:00
|
|
|
none {/* fall through */ }
|
2011-07-27 17:49:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a default in case we loop back on self recursively.
|
2011-12-28 18:11:33 +01:00
|
|
|
cx.kind_cache.insert(ty, kind_sendable);
|
2011-07-27 17:49:00 -07:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
let result = alt get(ty).struct {
|
2011-11-15 18:15:35 +01:00
|
|
|
// Scalar and unique types are sendable
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
|
2012-02-01 11:54:14 +01:00
|
|
|
ty_ptr(_) | ty_send_type | ty_str { kind_sendable }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_type { kind_copyable }
|
2011-12-28 18:11:33 +01:00
|
|
|
ty_fn(f) { proto_kind(f.proto) }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_opaque_closure_ptr(ck_block) { kind_noncopyable }
|
|
|
|
ty_opaque_closure_ptr(ck_box) { kind_copyable }
|
|
|
|
ty_opaque_closure_ptr(ck_uniq) { kind_sendable }
|
2011-07-27 21:23:40 -07:00
|
|
|
// Those with refcounts-to-inner raise pinned to shared,
|
|
|
|
// lower unique to shared. Therefore just set result to shared.
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_box(_) | ty_iface(_, _) | ty_opaque_box { kind_copyable }
|
2011-11-23 13:25:26 +01:00
|
|
|
// Boxes and unique pointers raise pinned to shared.
|
|
|
|
ty_vec(tm) | ty_uniq(tm) { type_kind(cx, tm.ty) }
|
2011-07-27 17:49:00 -07:00
|
|
|
// Records lower to the lowest of their members.
|
|
|
|
ty_rec(flds) {
|
2011-12-28 18:11:33 +01:00
|
|
|
let lowest = kind_sendable;
|
2011-11-15 18:15:35 +01:00
|
|
|
for f in flds { lowest = lower_kind(lowest, type_kind(cx, f.mt.ty)); }
|
|
|
|
lowest
|
2011-07-27 17:49:00 -07:00
|
|
|
}
|
2011-08-18 18:06:00 -07:00
|
|
|
// Tuples lower to the lowest of their members.
|
|
|
|
ty_tup(tys) {
|
2011-12-28 18:11:33 +01:00
|
|
|
let lowest = kind_sendable;
|
2011-11-15 18:15:35 +01:00
|
|
|
for ty in tys { lowest = lower_kind(lowest, type_kind(cx, ty)); }
|
|
|
|
lowest
|
2011-08-18 18:06:00 -07:00
|
|
|
}
|
2012-01-25 14:34:31 +01:00
|
|
|
// Enums lower to the lowest of their variants.
|
|
|
|
ty_enum(did, tps) {
|
2011-12-28 18:11:33 +01:00
|
|
|
let lowest = kind_sendable;
|
2012-01-25 14:34:31 +01:00
|
|
|
for variant in *enum_variants(cx, did) {
|
2011-11-15 18:15:35 +01:00
|
|
|
for aty in variant.args {
|
2011-07-27 17:49:00 -07:00
|
|
|
// Perform any type parameter substitutions.
|
|
|
|
let arg_ty = substitute_type_params(cx, tps, aty);
|
2011-11-15 18:15:35 +01:00
|
|
|
lowest = lower_kind(lowest, type_kind(cx, arg_ty));
|
2011-12-28 18:11:33 +01:00
|
|
|
if lowest == kind_noncopyable { break; }
|
2011-07-27 17:49:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
lowest
|
2011-07-27 17:49:00 -07:00
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
// Resources are always noncopyable.
|
2011-12-28 18:11:33 +01:00
|
|
|
ty_res(did, inner, tps) { kind_noncopyable }
|
2012-01-02 12:13:26 +01:00
|
|
|
ty_param(_, did) {
|
|
|
|
param_bounds_to_kind(cx.ty_param_bounds.get(did.node))
|
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
ty_constr(t, _) { type_kind(cx, t) }
|
2012-03-05 16:27:27 -08:00
|
|
|
_ { cx.sess.bug("bad type in type_kind"); }
|
2011-11-15 18:15:35 +01:00
|
|
|
};
|
2011-07-27 17:49:00 -07:00
|
|
|
|
|
|
|
cx.kind_cache.insert(ty, result);
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-01-23 14:59:00 -08:00
|
|
|
fn type_structurally_contains(cx: ctxt, ty: t, test: fn(sty) -> bool) ->
|
2011-09-02 15:34:58 -07:00
|
|
|
bool {
|
2012-02-03 15:15:28 +01:00
|
|
|
let sty = get(ty).struct;
|
2011-08-22 13:39:32 +02:00
|
|
|
if test(sty) { ret true; }
|
|
|
|
alt sty {
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
for variant in *enum_variants(cx, did) {
|
2011-08-22 13:39:32 +02:00
|
|
|
for aty in variant.args {
|
|
|
|
let sty = substitute_type_params(cx, tps, aty);
|
|
|
|
if type_structurally_contains(cx, sty, test) { ret true; }
|
|
|
|
}
|
2011-06-24 14:31:52 -04:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
ty_rec(fields) {
|
2011-08-22 13:39:32 +02:00
|
|
|
for field in fields {
|
|
|
|
if type_structurally_contains(cx, field.mt.ty, test) { ret true; }
|
2011-06-30 14:46:17 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ret false;
|
|
|
|
}
|
2011-08-15 12:08:05 +02:00
|
|
|
ty_tup(ts) {
|
2011-08-22 13:39:32 +02:00
|
|
|
for tt in ts {
|
|
|
|
if type_structurally_contains(cx, tt, test) { ret true; }
|
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ret false;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_res(_, sub, tps) {
|
2011-08-22 13:39:32 +02:00
|
|
|
let sty = substitute_type_params(cx, tps, sub);
|
|
|
|
ret type_structurally_contains(cx, sty, test);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-22 13:39:32 +02:00
|
|
|
_ { ret false; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 11:23:35 +01:00
|
|
|
pure fn type_has_dynamic_size(cx: ctxt, ty: t) -> bool unchecked {
|
2011-09-02 18:59:22 -07:00
|
|
|
/* type_structurally_contains can't be declared pure
|
|
|
|
because it takes a function argument. But it should be
|
|
|
|
referentially transparent, since a given type's size should
|
|
|
|
never change once it's created.
|
|
|
|
(It would be interesting to think about how to make such properties
|
|
|
|
actually checkable. It seems to me like a lot of properties
|
|
|
|
that the type context tracks about types should be immutable.)
|
|
|
|
*/
|
2012-02-03 15:15:28 +01:00
|
|
|
type_has_params(ty) && type_structurally_contains(cx, ty) {|sty|
|
2012-01-05 10:44:59 -08:00
|
|
|
alt sty {
|
2012-01-05 16:19:12 -08:00
|
|
|
ty_param(_, _) { true }
|
2012-01-05 10:44:59 -08:00
|
|
|
_ { false }
|
|
|
|
}
|
2012-01-11 09:58:05 -08:00
|
|
|
}
|
2011-08-22 13:39:32 +02:00
|
|
|
}
|
|
|
|
|
2011-11-18 09:46:44 +01:00
|
|
|
// Returns true for noncopyable types and types where a copy of a value can be
|
|
|
|
// distinguished from the value itself. I.e. types with mutable content that's
|
|
|
|
// not shared through a pointer.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn type_allows_implicit_copy(cx: ctxt, ty: t) -> bool {
|
2012-01-11 09:58:05 -08:00
|
|
|
ret !type_structurally_contains(cx, ty, {|sty|
|
|
|
|
alt sty {
|
2011-09-12 14:43:41 +02:00
|
|
|
ty_param(_, _) { true }
|
|
|
|
ty_vec(mt) {
|
2012-02-15 11:25:39 -08:00
|
|
|
mt.mutbl != ast::m_imm
|
2011-09-12 14:43:41 +02:00
|
|
|
}
|
|
|
|
ty_rec(fields) {
|
|
|
|
for field in fields {
|
2012-02-15 11:25:39 -08:00
|
|
|
if field.mt.mutbl != ast::m_imm {
|
2011-09-12 14:43:41 +02:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ { false }
|
2012-01-11 09:58:05 -08:00
|
|
|
}
|
2011-12-28 18:11:33 +01:00
|
|
|
}) && type_kind(cx, ty) != kind_noncopyable;
|
2011-09-12 11:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn type_structurally_contains_uniques(cx: ctxt, ty: t) -> bool {
|
2012-01-11 09:58:05 -08:00
|
|
|
ret type_structurally_contains(cx, ty, {|sty|
|
2011-09-12 14:43:41 +02:00
|
|
|
ret alt sty {
|
|
|
|
ty_uniq(_) { ret true; }
|
|
|
|
ty_vec(_) { true }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { true }
|
2011-09-12 14:43:41 +02:00
|
|
|
_ { ret false; }
|
|
|
|
};
|
|
|
|
});
|
2011-09-12 11:27:30 +02:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_integral(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_int(_) | ty_uint(_) | ty_bool { true }
|
2011-12-07 21:06:12 +01:00
|
|
|
_ { false }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_fp(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_float(_) { true }
|
|
|
|
_ { false }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_numeric(ty: t) -> bool {
|
|
|
|
ret type_is_integral(ty) || type_is_fp(ty);
|
2011-12-02 13:42:51 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_signed(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_int(_) { true }
|
|
|
|
_ { false }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 20:47:03 +01:00
|
|
|
// Whether a type is Plain Old Data -- meaning it does not contain pointers
|
|
|
|
// that the cycle collector might care about.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn type_is_pod(cx: ctxt, ty: t) -> bool {
|
2011-08-04 10:46:10 -07:00
|
|
|
let result = true;
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(ty).struct {
|
2011-08-19 15:16:48 -07:00
|
|
|
// Scalar types
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
|
2012-02-01 11:54:14 +01:00
|
|
|
ty_send_type | ty_type | ty_ptr(_) { result = true; }
|
2011-08-19 15:16:48 -07:00
|
|
|
// Boxed types
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str | ty_box(_) | ty_uniq(_) | ty_vec(_) | ty_fn(_) |
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_iface(_, _) | ty_opaque_box { result = false; }
|
2011-08-19 15:16:48 -07:00
|
|
|
// Structural types
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
let variants = enum_variants(cx, did);
|
2011-12-15 17:14:58 -08:00
|
|
|
for variant: variant_info in *variants {
|
2011-08-19 15:16:48 -07:00
|
|
|
let tup_ty = mk_tup(cx, variant.args);
|
|
|
|
|
|
|
|
// Perform any type parameter substitutions.
|
|
|
|
tup_ty = substitute_type_params(cx, tps, tup_ty);
|
|
|
|
if !type_is_pod(cx, tup_ty) { result = false; }
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
ty_rec(flds) {
|
|
|
|
for f: field in flds {
|
|
|
|
if !type_is_pod(cx, f.mt.ty) { result = false; }
|
2011-08-04 10:46:10 -07:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
ty_tup(elts) {
|
|
|
|
for elt in elts { if !type_is_pod(cx, elt) { result = false; } }
|
|
|
|
}
|
|
|
|
ty_res(_, inner, tps) {
|
|
|
|
result = type_is_pod(cx, substitute_type_params(cx, tps, inner));
|
|
|
|
}
|
|
|
|
ty_constr(subt, _) { result = type_is_pod(cx, subt); }
|
|
|
|
ty_param(_, _) { result = false; }
|
2012-01-30 21:00:57 -08:00
|
|
|
ty_opaque_closure_ptr(_) { result = true; }
|
2012-01-30 11:52:34 +01:00
|
|
|
_ { cx.sess.bug("unexpected type in type_is_pod"); }
|
2011-08-04 10:46:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_is_enum(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(_, _) { ret true; }
|
2012-01-10 14:55:54 -07:00
|
|
|
_ { ret false;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
// Whether a type is enum like, that is a enum type with only nullary
|
2012-01-10 14:55:54 -07:00
|
|
|
// constructors
|
2012-01-10 18:33:26 -07:00
|
|
|
fn type_is_c_like_enum(cx: ctxt, ty: t) -> bool {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(ty).struct {
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
let variants = enum_variants(cx, did);
|
2012-01-10 14:55:54 -07:00
|
|
|
let some_n_ary = vec::any(*variants, {|v| vec::len(v.args) > 0u});
|
|
|
|
ret !some_n_ary;
|
|
|
|
}
|
|
|
|
_ { ret false;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_param(ty: t) -> option<uint> {
|
|
|
|
alt get(ty).struct {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_param(id, _) { ret some(id); }
|
2011-07-27 14:19:39 +02:00
|
|
|
_ {/* fall through */ }
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
2011-07-21 15:46:03 -07:00
|
|
|
ret none;
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2011-08-18 15:00:12 -07:00
|
|
|
// Returns a vec of all the type variables
|
2011-08-03 18:06:57 -07:00
|
|
|
// occurring in t. It may contain duplicates.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn vars_in_type(cx: ctxt, ty: t) -> [int] {
|
2012-01-31 12:38:22 +01:00
|
|
|
let rslt = [];
|
|
|
|
walk_ty(cx, ty) {|ty|
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(ty).struct { ty_var(v) { rslt += [v]; } _ { } }
|
2011-08-03 18:06:57 -07:00
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
rslt
|
2011-08-03 18:06:57 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_autoderef(cx: ctxt, t: t) -> t {
|
2011-09-29 11:18:40 +02:00
|
|
|
let t1 = t;
|
2011-07-27 14:19:39 +02:00
|
|
|
while true {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(t1).struct {
|
2011-09-29 11:18:40 +02:00
|
|
|
ty_box(mt) | ty_uniq(mt) { t1 = mt.ty; }
|
|
|
|
ty_res(_, inner, tps) {
|
2011-07-27 14:19:39 +02:00
|
|
|
t1 = substitute_type_params(cx, tps, inner);
|
|
|
|
}
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
let variants = enum_variants(cx, did);
|
2011-12-15 17:14:58 -08:00
|
|
|
if vec::len(*variants) != 1u || vec::len(variants[0].args) != 1u {
|
2011-07-27 14:19:39 +02:00
|
|
|
break;
|
2011-07-01 13:57:03 +02:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
t1 = substitute_type_params(cx, tps, variants[0].args[0]);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { break; }
|
2011-07-01 01:48:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret t1;
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:24:36 +01:00
|
|
|
// Type hashing.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn hash_type_structure(st: sty) -> uint {
|
2012-02-03 15:15:28 +01:00
|
|
|
fn hash_uint(id: uint, n: uint) -> uint { (id << 2u) + n }
|
2011-07-27 14:19:39 +02:00
|
|
|
fn hash_def(id: uint, did: ast::def_id) -> uint {
|
2012-02-03 15:15:28 +01:00
|
|
|
let h = (id << 2u) + (did.crate as uint);
|
|
|
|
(h << 2u) + (did.node as uint)
|
2011-04-20 14:34:17 -07:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
fn hash_subty(id: uint, subty: t) -> uint { (id << 2u) + type_id(subty) }
|
2011-12-15 11:06:48 -08:00
|
|
|
fn hash_subtys(id: uint, subtys: [t]) -> uint {
|
|
|
|
let h = id;
|
2012-02-03 15:15:28 +01:00
|
|
|
for s in subtys { h = (h << 2u) + type_id(s) }
|
|
|
|
h
|
2011-12-15 11:06:48 -08:00
|
|
|
}
|
2011-09-12 11:27:30 +02:00
|
|
|
fn hash_type_constr(id: uint, c: @type_constr) -> uint {
|
2011-07-27 14:19:39 +02:00
|
|
|
let h = id;
|
2012-02-03 15:15:28 +01:00
|
|
|
h = (h << 2u) + hash_def(h, c.node.id);
|
|
|
|
// FIXME this makes little sense
|
|
|
|
for a in c.node.args {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt a.node {
|
2012-02-03 15:15:28 +01:00
|
|
|
carg_base { h += h << 2u; }
|
|
|
|
carg_lit(_) { fail "lit args not implemented yet"; }
|
|
|
|
carg_ident(p) { h += h << 2u; }
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
h
|
2011-04-20 14:34:17 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
alt st {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil { 0u } ty_bool { 1u }
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_int(t) {
|
|
|
|
alt t {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::ty_i { 2u } ast::ty_char { 3u } ast::ty_i8 { 4u }
|
|
|
|
ast::ty_i16 { 5u } ast::ty_i32 { 6u } ast::ty_i64 { 7u }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_uint(t) {
|
|
|
|
alt t {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::ty_u { 8u } ast::ty_u8 { 9u } ast::ty_u16 { 10u }
|
|
|
|
ast::ty_u32 { 11u } ast::ty_u64 { 12u }
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty_float(t) {
|
2012-01-18 22:37:22 -08:00
|
|
|
alt t { ast::ty_f { 13u } ast::ty_f32 { 14u } ast::ty_f64 { 15u } }
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_str { 17u }
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(did, tys) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let h = hash_def(18u, did);
|
2012-02-03 15:15:28 +01:00
|
|
|
for typ: t in tys { h = hash_subty(h, typ); }
|
|
|
|
h
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_box(mt) { hash_subty(19u, mt.ty) }
|
|
|
|
ty_vec(mt) { hash_subty(21u, mt.ty) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(fields) {
|
|
|
|
let h = 26u;
|
2012-02-03 15:15:28 +01:00
|
|
|
for f in fields { h = hash_subty(h, f.mt.ty); }
|
|
|
|
h
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_tup(ts) { hash_subtys(25u, ts) }
|
|
|
|
ty_fn(f) {
|
|
|
|
let h = 27u;
|
|
|
|
for a in f.inputs { h = hash_subty(h, a.ty); }
|
|
|
|
hash_subty(h, f.output)
|
|
|
|
}
|
|
|
|
ty_var(v) { hash_uint(30u, v as uint) }
|
|
|
|
ty_param(pid, did) { hash_def(hash_uint(31u, pid), did) }
|
2012-01-30 11:52:34 +01:00
|
|
|
ty_self(ts) {
|
|
|
|
let h = 28u;
|
2012-02-03 15:15:28 +01:00
|
|
|
for t in ts { h = hash_subty(h, t); }
|
|
|
|
h
|
2012-01-30 11:52:34 +01:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_type { 32u }
|
|
|
|
ty_bot { 34u }
|
|
|
|
ty_ptr(mt) { hash_subty(35u, mt.ty) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_res(did, sub, tps) {
|
|
|
|
let h = hash_subty(hash_def(18u, did), sub);
|
2012-02-03 15:15:28 +01:00
|
|
|
hash_subtys(h, tps)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_constr(t, cs) {
|
2011-12-19 13:52:58 +01:00
|
|
|
let h = hash_subty(36u, t);
|
2012-02-03 15:15:28 +01:00
|
|
|
for c in cs { h = (h << 2u) + hash_type_constr(h, c); }
|
|
|
|
h
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_uniq(mt) { hash_subty(37u, mt.ty) }
|
|
|
|
ty_send_type { 38u }
|
2011-12-20 16:33:55 +01:00
|
|
|
ty_iface(did, tys) {
|
2012-01-05 16:19:12 -08:00
|
|
|
let h = hash_def(40u, did);
|
|
|
|
for typ: t in tys { h = hash_subty(h, typ); }
|
2012-02-03 15:15:28 +01:00
|
|
|
h
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_opaque_closure_ptr(ck_block) { 41u }
|
|
|
|
ty_opaque_closure_ptr(ck_box) { 42u }
|
|
|
|
ty_opaque_closure_ptr(ck_uniq) { 43u }
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_opaque_box { 44u }
|
2012-02-09 14:16:12 -08:00
|
|
|
ty_class(did, tys) {
|
|
|
|
let h = hash_def(45u, did);
|
|
|
|
for typ: t in tys { h = hash_subty(h, typ); }
|
|
|
|
h
|
|
|
|
}
|
2011-04-20 14:34:17 -07:00
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2012-01-23 14:59:00 -08:00
|
|
|
fn arg_eq<T>(eq: fn(T, T) -> bool,
|
2012-01-11 09:58:05 -08:00
|
|
|
a: @sp_constr_arg<T>,
|
|
|
|
b: @sp_constr_arg<T>)
|
2011-09-12 11:27:30 +02:00
|
|
|
-> bool {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt a.node {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::carg_base {
|
|
|
|
alt b.node { ast::carg_base { ret true; } _ { ret false; } }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ast::carg_ident(s) {
|
|
|
|
alt b.node { ast::carg_ident(t) { ret eq(s, t); } _ { ret false; } }
|
|
|
|
}
|
|
|
|
ast::carg_lit(l) {
|
2011-11-22 11:49:29 +01:00
|
|
|
alt b.node {
|
|
|
|
ast::carg_lit(m) { ret ast_util::lit_eq(l, m); } _ { ret false; }
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 14:59:00 -08:00
|
|
|
fn args_eq<T>(eq: fn(T, T) -> bool,
|
2012-01-11 09:58:05 -08:00
|
|
|
a: [@sp_constr_arg<T>],
|
2011-09-12 11:27:30 +02:00
|
|
|
b: [@sp_constr_arg<T>]) -> bool {
|
2011-07-27 14:19:39 +02:00
|
|
|
let i: uint = 0u;
|
2011-08-12 07:15:18 -07:00
|
|
|
for arg: @sp_constr_arg<T> in a {
|
2011-08-19 15:16:48 -07:00
|
|
|
if !arg_eq(eq, arg, b[i]) { ret false; }
|
2011-06-09 09:48:16 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn constr_eq(c: @constr, d: @constr) -> bool {
|
2011-12-22 14:24:36 +01:00
|
|
|
fn eq_int(&&x: uint, &&y: uint) -> bool { ret x == y; }
|
2011-06-15 11:19:50 -07:00
|
|
|
ret path_to_str(c.node.path) == path_to_str(d.node.path) &&
|
2011-06-16 16:55:46 -07:00
|
|
|
// FIXME: hack
|
2011-06-15 11:19:50 -07:00
|
|
|
args_eq(eq_int, c.node.args, d.node.args);
|
2011-06-09 09:48:16 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn constrs_eq(cs: [@constr], ds: [@constr]) -> bool {
|
2011-08-15 16:38:23 -07:00
|
|
|
if vec::len(cs) != vec::len(ds) { ret false; }
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-08-19 15:16:48 -07:00
|
|
|
for c: @constr in cs { if !constr_eq(c, ds[i]) { ret false; } i += 1u; }
|
2011-06-09 09:48:16 -07:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn node_id_to_type(cx: ctxt, id: ast::node_id) -> t {
|
2012-03-03 17:49:23 -08:00
|
|
|
alt smallintmap::find(*cx.node_types, id as uint) {
|
|
|
|
some(t) { t }
|
|
|
|
none { cx.sess.bug(#fmt("node_id_to_type: unbound node ID %?", id)); }
|
|
|
|
}
|
2011-05-13 16:40:21 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn node_id_to_type_params(cx: ctxt, id: ast::node_id) -> [t] {
|
2012-01-30 17:28:30 +01:00
|
|
|
alt cx.node_type_substs.find(id) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { ret []; }
|
2012-01-30 17:28:30 +01:00
|
|
|
some(ts) { ret ts; }
|
2011-05-13 16:40:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn node_id_has_type_params(cx: ctxt, id: ast::node_id) -> bool {
|
2012-01-30 17:28:30 +01:00
|
|
|
ret cx.node_type_substs.contains_key(id);
|
2011-05-13 16:53:35 -07:00
|
|
|
}
|
|
|
|
|
2011-04-12 15:09:50 -07:00
|
|
|
// Returns the number of distinct type parameters in the given type.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn count_ty_params(cx: ctxt, ty: t) -> uint {
|
2012-01-31 12:38:22 +01:00
|
|
|
let param_indices = [];
|
|
|
|
walk_ty(cx, ty) {|t|
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(t).struct {
|
2011-08-19 15:16:48 -07:00
|
|
|
ty_param(param_idx, _) {
|
2012-01-31 12:38:22 +01:00
|
|
|
if !vec::any(param_indices, {|i| i == param_idx}) {
|
|
|
|
param_indices += [param_idx];
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
_ {}
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
}
|
2012-01-31 12:38:22 +01:00
|
|
|
vec::len(param_indices)
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// Type accessors for substructures of types
|
2012-02-03 15:15:28 +01:00
|
|
|
fn ty_fn_args(fty: t) -> [arg] {
|
|
|
|
alt get(fty).struct {
|
|
|
|
ty_fn(f) { f.inputs }
|
|
|
|
_ { fail "ty_fn_args() called on non-fn type"; }
|
2011-02-18 17:30:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn ty_fn_proto(fty: t) -> ast::proto {
|
|
|
|
alt get(fty).struct {
|
|
|
|
ty_fn(f) { f.proto }
|
|
|
|
_ { fail "ty_fn_proto() called on non-fn type"; }
|
2011-07-29 20:51:18 +02:00
|
|
|
}
|
2011-01-03 18:22:39 -08:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
pure fn ty_fn_ret(fty: t) -> t {
|
|
|
|
alt get(fty).struct {
|
|
|
|
ty_fn(f) { f.output }
|
|
|
|
_ { fail "ty_fn_ret() called on non-fn type"; }
|
2011-02-18 17:30:57 -08:00
|
|
|
}
|
2011-01-03 18:22:39 -08:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn ty_fn_ret_style(fty: t) -> ast::ret_style {
|
|
|
|
alt get(fty).struct {
|
|
|
|
ty_fn(f) { f.ret_style }
|
|
|
|
_ { fail "ty_fn_ret_style() called on non-fn type"; }
|
2011-09-14 14:34:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn is_fn_ty(fty: t) -> bool {
|
|
|
|
alt get(fty).struct {
|
|
|
|
ty_fn(_) { ret true; }
|
2011-07-27 14:19:39 +02:00
|
|
|
_ { ret false; }
|
2011-02-18 17:30:57 -08:00
|
|
|
}
|
2011-01-03 18:22:39 -08:00
|
|
|
}
|
|
|
|
|
2011-08-24 17:24:58 -07:00
|
|
|
// Just checks whether it's a fn that returns bool,
|
|
|
|
// not its purity.
|
2012-02-03 15:15:28 +01:00
|
|
|
fn is_pred_ty(fty: t) -> bool {
|
|
|
|
is_fn_ty(fty) && type_is_bool(ty_fn_ret(fty))
|
2011-08-24 17:24:58 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn ty_var_id(typ: t) -> int {
|
|
|
|
alt get(typ).struct {
|
|
|
|
ty_var(vid) { ret vid; }
|
2011-12-22 14:42:52 -08:00
|
|
|
_ { #error("ty_var_id called on non-var ty"); fail; }
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-03 18:22:39 -08:00
|
|
|
|
2010-12-21 12:13:51 -08:00
|
|
|
// Type accessors for AST nodes
|
2011-09-12 11:27:30 +02:00
|
|
|
fn block_ty(cx: ctxt, b: ast::blk) -> t {
|
2011-06-19 22:41:21 +02:00
|
|
|
ret node_id_to_type(cx, b.node.id);
|
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
|
|
|
|
|
2011-04-08 21:27:54 -07:00
|
|
|
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
|
|
|
|
// doesn't provide type parameter substitutions.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn pat_ty(cx: ctxt, pat: @ast::pat) -> t {
|
2012-01-30 17:28:30 +01:00
|
|
|
ret node_id_to_type(cx, pat.id);
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-04-08 21:27:54 -07:00
|
|
|
// Returns the type of an expression as a monotype.
|
|
|
|
//
|
|
|
|
// NB: This type doesn't provide type parameter substitutions; e.g. if you
|
|
|
|
// ask for the type of "id" in "id(3)", it will return "fn(&int) -> int"
|
2011-08-22 12:45:18 +02:00
|
|
|
// instead of "fn(t) -> T with T = int". If this isn't what you want, see
|
2011-04-08 21:27:54 -07:00
|
|
|
// expr_ty_params_and_ty() below.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn expr_ty(cx: ctxt, expr: @ast::expr) -> t {
|
2012-01-30 17:28:30 +01:00
|
|
|
ret node_id_to_type(cx, expr.id);
|
2011-04-08 21:27:54 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn expr_ty_params_and_ty(cx: ctxt, expr: @ast::expr) -> {params: [t], ty: t} {
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {params: node_id_to_type_params(cx, expr.id),
|
|
|
|
ty: node_id_to_type(cx, expr.id)};
|
2011-04-08 21:27:54 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn expr_has_ty_params(cx: ctxt, expr: @ast::expr) -> bool {
|
2011-06-21 22:16:40 +02:00
|
|
|
ret node_id_has_type_params(cx, expr.id);
|
2011-04-08 21:27:54 -07:00
|
|
|
}
|
|
|
|
|
2012-01-13 10:58:31 +01:00
|
|
|
fn expr_is_lval(method_map: typeck::method_map, e: @ast::expr) -> bool {
|
2011-11-15 18:15:35 +01:00
|
|
|
alt e.node {
|
2012-01-26 15:23:04 +01:00
|
|
|
ast::expr_path(_) | ast::expr_unary(ast::deref, _) { true }
|
|
|
|
ast::expr_field(_, _, _) | ast::expr_index(_, _) {
|
|
|
|
!method_map.contains_key(e.id)
|
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn stmt_node_id(s: @ast::stmt) -> ast::node_id {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt s.node {
|
2012-01-04 14:16:41 -08:00
|
|
|
ast::stmt_decl(_, id) | stmt_expr(_, id) | stmt_semi(_, id) {
|
|
|
|
ret id;
|
|
|
|
}
|
2011-05-17 19:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 17:05:20 -08:00
|
|
|
fn field_idx(id: ast::ident, fields: [field]) -> option<uint> {
|
2011-12-18 19:30:40 +01:00
|
|
|
let i = 0u;
|
|
|
|
for f in fields { if f.ident == id { ret some(i); } i += 1u; }
|
|
|
|
ret none;
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:33:00 +01:00
|
|
|
fn get_field(rec_ty: t, id: ast::ident) -> field {
|
2012-02-21 15:59:48 +01:00
|
|
|
alt check vec::find(get_fields(rec_ty), {|f| str::eq(f.ident, id) }) {
|
2012-02-09 14:33:00 +01:00
|
|
|
some(f) { f }
|
2012-01-30 21:00:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 14:33:00 +01:00
|
|
|
fn get_fields(rec_ty:t) -> [field] {
|
2012-02-21 15:59:48 +01:00
|
|
|
alt check get(rec_ty).struct {
|
2012-02-09 14:33:00 +01:00
|
|
|
ty_rec(fields) { fields }
|
2011-09-13 12:14:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 17:05:20 -08:00
|
|
|
fn method_idx(id: ast::ident, meths: [method]) -> option<uint> {
|
2011-12-18 19:30:40 +01:00
|
|
|
let i = 0u;
|
|
|
|
for m in meths { if m.ident == id { ret some(i); } i += 1u; }
|
|
|
|
ret none;
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn sort_methods(meths: [method]) -> [method] {
|
|
|
|
fn method_lteq(a: method, b: method) -> bool {
|
2012-02-03 03:28:49 -08:00
|
|
|
ret str::le(a.ident, b.ident);
|
2011-03-17 11:40:05 -07:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
ret std::sort::merge_sort(bind method_lteq(_, _), meths);
|
2011-03-17 11:40:05 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 17:05:20 -08:00
|
|
|
fn occurs_check_fails(tcx: ctxt, sp: option<span>, vid: int, rt: t) ->
|
2011-08-19 15:16:48 -07:00
|
|
|
bool {
|
2012-02-03 15:15:28 +01:00
|
|
|
// Fast path
|
|
|
|
if !type_has_vars(rt) { ret false; }
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2011-08-03 18:06:57 -07:00
|
|
|
// Occurs check!
|
2012-02-11 17:54:49 -08:00
|
|
|
if vec::contains(vars_in_type(tcx, rt), vid) {
|
2011-08-03 18:06:57 -07:00
|
|
|
alt sp {
|
2011-08-19 15:16:48 -07:00
|
|
|
some(s) {
|
2011-08-03 18:06:57 -07:00
|
|
|
// Maybe this should be span_err -- however, there's an
|
|
|
|
// assertion later on that the type doesn't contain
|
|
|
|
// variables, so in this case we have to be sure to die.
|
2011-09-12 12:39:38 +02:00
|
|
|
tcx.sess.span_fatal
|
2012-03-05 16:27:27 -08:00
|
|
|
(s, "type inference failed because I \
|
2011-09-12 12:39:38 +02:00
|
|
|
could not find a type\n that's both of the form "
|
2012-02-03 15:15:28 +01:00
|
|
|
+ ty_to_str(tcx, mk_var(tcx, vid)) +
|
2011-09-12 12:39:38 +02:00
|
|
|
" and of the form " + ty_to_str(tcx, rt) +
|
2012-03-05 16:27:27 -08:00
|
|
|
" - such a type would have to be infinitely large.");
|
2011-08-03 18:06:57 -07:00
|
|
|
}
|
|
|
|
_ { ret true; }
|
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
} else { ret false; }
|
2011-08-03 18:06:57 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-02-02 16:50:17 -08:00
|
|
|
// Maintains a little union-set tree for inferred modes. `canon()` returns
|
|
|
|
// the current head value for `m0`.
|
|
|
|
fn canon<T:copy>(tbl: hashmap<ast::node_id, ast::inferable<T>>,
|
|
|
|
m0: ast::inferable<T>) -> ast::inferable<T> {
|
|
|
|
alt m0 {
|
|
|
|
ast::infer(id) {
|
|
|
|
alt tbl.find(id) {
|
|
|
|
none { m0 }
|
|
|
|
some(m1) {
|
|
|
|
let cm1 = canon(tbl, m1);
|
|
|
|
// path compression:
|
|
|
|
if cm1 != m1 { tbl.insert(id, cm1); }
|
|
|
|
cm1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { m0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maintains a little union-set tree for inferred modes. `resolve_mode()`
|
|
|
|
// returns the current head value for `m0`.
|
|
|
|
fn canon_mode(cx: ctxt, m0: ast::mode) -> ast::mode {
|
|
|
|
canon(cx.inferred_modes, m0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the head value for mode, failing if `m` was a infer(_) that
|
|
|
|
// was never inferred. This should be safe for use after typeck.
|
|
|
|
fn resolved_mode(cx: ctxt, m: ast::mode) -> ast::rmode {
|
|
|
|
alt canon_mode(cx, m) {
|
|
|
|
ast::infer(_) {
|
|
|
|
cx.sess.bug(#fmt["mode %? was never resolved", m]);
|
|
|
|
}
|
|
|
|
ast::expl(m0) { m0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn arg_mode(cx: ctxt, a: arg) -> ast::rmode { resolved_mode(cx, a.mode) }
|
2012-02-02 16:50:17 -08:00
|
|
|
|
|
|
|
// Unifies `m1` and `m2`. Returns unified value or failure code.
|
|
|
|
fn unify_mode(cx: ctxt, m1: ast::mode, m2: ast::mode)
|
|
|
|
-> result::t<ast::mode, type_err> {
|
|
|
|
alt (canon_mode(cx, m1), canon_mode(cx, m2)) {
|
|
|
|
(m1, m2) if (m1 == m2) {
|
|
|
|
result::ok(m1)
|
|
|
|
}
|
|
|
|
(ast::infer(id1), ast::infer(id2)) {
|
|
|
|
cx.inferred_modes.insert(id2, m1);
|
|
|
|
result::ok(m1)
|
|
|
|
}
|
|
|
|
(ast::infer(id), m) | (m, ast::infer(id)) {
|
|
|
|
cx.inferred_modes.insert(id, m);
|
|
|
|
result::ok(m1)
|
|
|
|
}
|
|
|
|
(m1, m2) {
|
|
|
|
result::err(terr_mode_mismatch(m1, m2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If `m` was never unified, unifies it with `m_def`. Returns the final value
|
|
|
|
// for `m`.
|
|
|
|
fn set_default_mode(cx: ctxt, m: ast::mode, m_def: ast::rmode) {
|
|
|
|
alt canon_mode(cx, m) {
|
|
|
|
ast::infer(id) {
|
|
|
|
cx.inferred_modes.insert(id, ast::expl(m_def));
|
|
|
|
}
|
|
|
|
ast::expl(_) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 14:52:33 -08:00
|
|
|
// Type unification via Robinson's algorithm (Robinson 1965). Implemented as
|
|
|
|
// described in Hoder and Voronkov:
|
|
|
|
//
|
|
|
|
// http://www.cs.man.ac.uk/~hoderk/ubench/unification_full.pdf
|
2011-05-14 21:13:47 -04:00
|
|
|
mod unify {
|
2011-06-19 02:34:20 -07:00
|
|
|
export fixup_result;
|
|
|
|
export fixup_vars;
|
|
|
|
export fix_ok;
|
|
|
|
export fix_err;
|
|
|
|
export mk_var_bindings;
|
|
|
|
export resolve_type_structure;
|
|
|
|
export resolve_type_var;
|
|
|
|
export result;
|
|
|
|
export unify;
|
|
|
|
export ures_ok;
|
|
|
|
export ures_err;
|
|
|
|
export var_bindings;
|
2012-01-04 11:32:26 +01:00
|
|
|
export precise, in_bindings;
|
2011-06-19 02:34:20 -07:00
|
|
|
|
2012-01-19 17:56:05 -08:00
|
|
|
enum result { ures_ok(t), ures_err(type_err), }
|
|
|
|
enum union_result { unres_ok, unres_err(type_err), }
|
2012-01-19 14:24:03 -08:00
|
|
|
enum fixup_result {
|
2012-01-19 17:56:05 -08:00
|
|
|
fix_ok(t), // fixup succeeded
|
|
|
|
fix_err(int), // fixup failed because a type variable was unresolved
|
2011-05-19 16:06:23 -07:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
type var_bindings =
|
2011-08-12 07:15:18 -07:00
|
|
|
{sets: ufind::ufind, types: smallintmap::smallintmap<t>};
|
2011-05-20 18:36:35 -07:00
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
enum unify_style {
|
2012-01-19 17:56:05 -08:00
|
|
|
precise,
|
|
|
|
in_bindings(@var_bindings),
|
2011-12-29 13:12:52 +01:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
type uctxt = {st: unify_style, tcx: ctxt};
|
2011-05-18 11:53:26 -07:00
|
|
|
|
2011-05-20 18:36:35 -07:00
|
|
|
fn mk_var_bindings() -> @var_bindings {
|
2011-08-13 00:09:25 -07:00
|
|
|
ret @{sets: ufind::make(), types: smallintmap::mk::<t>()};
|
2011-05-18 11:53:26 -07:00
|
|
|
}
|
|
|
|
|
2011-05-20 18:36:35 -07:00
|
|
|
// Unifies two sets.
|
2012-02-03 15:15:28 +01:00
|
|
|
fn union(cx: @uctxt, set_a: uint, set_b: uint,
|
2011-10-17 18:12:25 -07:00
|
|
|
variance: variance) -> union_result {
|
2011-12-29 13:12:52 +01:00
|
|
|
let vb = alt cx.st {
|
|
|
|
in_bindings(vb) { vb }
|
2012-03-05 16:27:27 -08:00
|
|
|
_ { cx.tcx.sess.bug("someone forgot to document an invariant \
|
2012-01-30 21:00:57 -08:00
|
|
|
in union"); }
|
2011-12-29 13:12:52 +01:00
|
|
|
};
|
2012-01-05 17:11:34 +01:00
|
|
|
ufind::grow(vb.sets, math::max(set_a, set_b) + 1u);
|
2011-12-22 14:52:30 +01:00
|
|
|
let root_a = ufind::find(vb.sets, set_a);
|
|
|
|
let root_b = ufind::find(vb.sets, set_b);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2012-01-11 09:58:05 -08:00
|
|
|
let replace_type = (
|
|
|
|
fn@(vb: @var_bindings, t: t) {
|
|
|
|
ufind::union(vb.sets, set_a, set_b);
|
|
|
|
let root_c: uint = ufind::find(vb.sets, set_a);
|
|
|
|
smallintmap::insert::<t>(vb.types, root_c, t);
|
|
|
|
}
|
|
|
|
);
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2011-12-22 14:52:30 +01:00
|
|
|
alt smallintmap::find(vb.types, root_a) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none {
|
2011-12-22 14:52:30 +01:00
|
|
|
alt smallintmap::find(vb.types, root_b) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { ufind::union(vb.sets, set_a, set_b); ret unres_ok; }
|
2011-12-22 14:52:30 +01:00
|
|
|
some(t_b) { replace_type(vb, t_b); ret unres_ok; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
some(t_a) {
|
2011-12-22 14:52:30 +01:00
|
|
|
alt smallintmap::find(vb.types, root_b) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { replace_type(vb, t_a); ret unres_ok; }
|
2011-07-27 14:19:39 +02:00
|
|
|
some(t_b) {
|
2011-10-17 18:12:25 -07:00
|
|
|
alt unify_step(cx, t_a, t_b, variance) {
|
2011-12-22 14:52:30 +01:00
|
|
|
ures_ok(t_c) { replace_type(vb, t_c); ret unres_ok; }
|
2011-07-27 14:19:39 +02:00
|
|
|
ures_err(terr) { ret unres_err(terr); }
|
2011-05-19 16:06:23 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-05-19 16:06:23 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-05-19 16:06:23 -07:00
|
|
|
}
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
2011-10-17 15:06:19 -07:00
|
|
|
|
2012-02-21 17:02:02 +01:00
|
|
|
fn record_var_binding(cx: @uctxt, key: int, typ: t, variance: variance)
|
|
|
|
-> result {
|
|
|
|
let vb = alt check cx.st { in_bindings(vb) { vb } };
|
2011-12-22 14:52:30 +01:00
|
|
|
ufind::grow(vb.sets, (key as uint) + 1u);
|
|
|
|
let root = ufind::find(vb.sets, key as uint);
|
2011-07-27 14:19:39 +02:00
|
|
|
let result_type = typ;
|
2011-12-22 14:52:30 +01:00
|
|
|
alt smallintmap::find(vb.types, root) {
|
2011-07-27 14:19:39 +02:00
|
|
|
some(old_type) {
|
2011-10-17 20:51:34 -07:00
|
|
|
alt unify_step(cx, old_type, typ, variance) {
|
2011-07-27 14:19:39 +02:00
|
|
|
ures_ok(unified_type) { result_type = unified_type; }
|
|
|
|
rs { ret rs; }
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
none {/* fall through */ }
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
smallintmap::insert(vb.types, root, result_type);
|
|
|
|
ret ures_ok(mk_var(cx.tcx, key));
|
2011-05-19 16:06:23 -07:00
|
|
|
}
|
|
|
|
|
2010-12-21 17:47:13 -08:00
|
|
|
// Simple structural type comparison.
|
2012-02-03 15:15:28 +01:00
|
|
|
fn struct_cmp(cx: @uctxt, expected: t, actual: t) -> result {
|
2011-12-23 01:57:35 +01:00
|
|
|
let tcx = cx.tcx;
|
2012-01-12 17:59:49 +01:00
|
|
|
let cfg = tcx.sess.targ_cfg;
|
2012-02-03 15:15:28 +01:00
|
|
|
if mach_sty(cfg, expected) == mach_sty(cfg, actual) {
|
2010-12-21 17:47:13 -08:00
|
|
|
ret ures_ok(expected);
|
|
|
|
}
|
2011-05-20 18:36:35 -07:00
|
|
|
ret ures_err(terr_mismatch);
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
|
|
|
|
2011-07-19 17:52:34 -07:00
|
|
|
// Right now this just checks that the lists of constraints are
|
|
|
|
// pairwise equal.
|
2011-08-22 12:45:18 +02:00
|
|
|
fn unify_constrs(base_t: t, expected: [@type_constr],
|
2011-09-12 11:27:30 +02:00
|
|
|
actual: [@type_constr]) -> result {
|
2011-08-15 16:38:23 -07:00
|
|
|
let expected_len = vec::len(expected);
|
|
|
|
let actual_len = vec::len(actual);
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
if expected_len != actual_len {
|
2011-07-19 17:52:34 -07:00
|
|
|
ret ures_err(terr_constr_len(expected_len, actual_len));
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
|
|
|
let rslt;
|
2011-08-15 21:54:52 -07:00
|
|
|
for c: @type_constr in expected {
|
2011-08-19 15:16:48 -07:00
|
|
|
rslt = unify_constr(base_t, c, actual[i]);
|
2011-07-27 14:19:39 +02:00
|
|
|
alt rslt { ures_ok(_) { } ures_err(_) { ret rslt; } }
|
2011-07-19 17:52:34 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret ures_ok(base_t);
|
|
|
|
}
|
2011-08-22 12:45:18 +02:00
|
|
|
fn unify_constr(base_t: t, expected: @type_constr,
|
2011-09-12 11:27:30 +02:00
|
|
|
actual_constr: @type_constr) -> result {
|
2011-07-27 14:19:39 +02:00
|
|
|
let ok_res = ures_ok(base_t);
|
|
|
|
let err_res = ures_err(terr_constr_mismatch(expected, actual_constr));
|
|
|
|
if expected.node.id != actual_constr.node.id { ret err_res; }
|
2011-08-15 16:38:23 -07:00
|
|
|
let expected_arg_len = vec::len(expected.node.args);
|
|
|
|
let actual_arg_len = vec::len(actual_constr.node.args);
|
2011-07-27 14:19:39 +02:00
|
|
|
if expected_arg_len != actual_arg_len { ret err_res; }
|
|
|
|
let i = 0u;
|
|
|
|
let actual;
|
2011-08-15 21:54:52 -07:00
|
|
|
for a: @ty_constr_arg in expected.node.args {
|
2011-08-19 15:16:48 -07:00
|
|
|
actual = actual_constr.node.args[i];
|
2011-07-27 14:19:39 +02:00
|
|
|
alt a.node {
|
2012-01-18 22:37:22 -08:00
|
|
|
carg_base {
|
|
|
|
alt actual.node { carg_base { } _ { ret err_res; } }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
carg_lit(l) {
|
|
|
|
alt actual.node {
|
|
|
|
carg_lit(m) { if l != m { ret err_res; } }
|
|
|
|
_ { ret err_res; }
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
carg_ident(p) {
|
|
|
|
alt actual.node {
|
|
|
|
carg_ident(q) { if p.node != q.node { ret err_res; } }
|
|
|
|
_ { ret err_res; }
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret ok_res;
|
|
|
|
}
|
|
|
|
|
2011-03-18 11:49:06 -07:00
|
|
|
// Unifies two mutability flags.
|
2011-10-17 20:51:34 -07:00
|
|
|
fn unify_mut(expected: ast::mutability, actual: ast::mutability,
|
|
|
|
variance: variance) ->
|
2012-01-31 17:05:20 -08:00
|
|
|
option<(ast::mutability, variance)> {
|
2011-10-17 20:51:34 -07:00
|
|
|
|
2011-10-18 10:35:33 -07:00
|
|
|
// If you're unifying on something mutable then we have to
|
|
|
|
// be invariant on the inner type
|
|
|
|
let newvariance = alt expected {
|
2012-02-15 11:25:39 -08:00
|
|
|
ast::m_mutbl {
|
2011-10-18 10:35:33 -07:00
|
|
|
variance_transform(variance, invariant)
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
variance_transform(variance, covariant)
|
|
|
|
}
|
|
|
|
};
|
2011-10-17 20:51:34 -07:00
|
|
|
|
|
|
|
if expected == actual { ret some((expected, newvariance)); }
|
|
|
|
if variance == covariant {
|
2012-02-15 11:25:39 -08:00
|
|
|
if expected == ast::m_const {
|
2011-10-17 20:51:34 -07:00
|
|
|
ret some((actual, newvariance));
|
|
|
|
}
|
|
|
|
} else if variance == contravariant {
|
2012-02-15 11:25:39 -08:00
|
|
|
if actual == ast::m_const {
|
2011-10-17 20:51:34 -07:00
|
|
|
ret some((expected, newvariance));
|
|
|
|
}
|
|
|
|
}
|
2011-07-21 15:46:03 -07:00
|
|
|
ret none;
|
2011-03-18 11:49:06 -07:00
|
|
|
}
|
2011-10-14 17:34:41 -07:00
|
|
|
fn unify_fn_proto(e_proto: ast::proto, a_proto: ast::proto,
|
2012-01-31 17:05:20 -08:00
|
|
|
variance: variance) -> option<result> {
|
2011-12-16 13:50:22 -08:00
|
|
|
// Prototypes form a diamond-shaped partial order:
|
|
|
|
//
|
|
|
|
// block
|
|
|
|
// ^ ^
|
|
|
|
// shared send
|
|
|
|
// ^ ^
|
|
|
|
// bare
|
|
|
|
//
|
|
|
|
// where "^" means "subtype of" (forgive the abuse of the term
|
|
|
|
// subtype).
|
|
|
|
fn sub_proto(p_sub: ast::proto, p_sup: ast::proto) -> bool {
|
|
|
|
ret alt (p_sub, p_sup) {
|
2012-01-18 22:37:22 -08:00
|
|
|
(_, ast::proto_any) { true }
|
|
|
|
(ast::proto_bare, _) { true }
|
2011-12-16 13:50:22 -08:00
|
|
|
|
2012-01-09 16:12:37 -08:00
|
|
|
// Equal prototypes are always subprotos:
|
2011-12-16 13:50:22 -08:00
|
|
|
(_, _) { p_sub == p_sup }
|
2011-12-09 16:56:48 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-16 13:50:22 -08:00
|
|
|
ret alt variance {
|
2012-01-18 22:37:22 -08:00
|
|
|
invariant if e_proto == a_proto { none }
|
|
|
|
covariant if sub_proto(a_proto, e_proto) { none }
|
|
|
|
contravariant if sub_proto(e_proto, a_proto) { none }
|
2011-12-16 13:50:22 -08:00
|
|
|
_ { some(ures_err(terr_mismatch)) }
|
|
|
|
};
|
2011-10-14 17:34:41 -07:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
fn unify_args(cx: @uctxt, e_args: [arg], a_args: [arg],
|
|
|
|
variance: variance) -> either::t<result, [arg]> {
|
2011-12-23 16:09:52 +01:00
|
|
|
if !vec::same_length(e_args, a_args) {
|
|
|
|
ret either::left(ures_err(terr_arg_count));
|
|
|
|
}
|
|
|
|
// The variance changes (flips basically) when descending
|
|
|
|
// into arguments of function types
|
|
|
|
let variance = variance_transform(variance, contravariant);
|
|
|
|
// Would use vec::map2(), but for the need to return in case of
|
|
|
|
// error:
|
|
|
|
let i = 0u, result = [];
|
|
|
|
for expected_input in e_args {
|
|
|
|
let actual_input = a_args[i];
|
|
|
|
i += 1u;
|
2012-02-02 16:50:17 -08:00
|
|
|
|
2011-12-23 16:09:52 +01:00
|
|
|
// Unify the result modes.
|
2012-02-02 16:50:17 -08:00
|
|
|
let result_mode =
|
|
|
|
alt unify_mode(cx.tcx, expected_input.mode,
|
|
|
|
actual_input.mode) {
|
|
|
|
result::err(err) { ret either::left(ures_err(err)); }
|
|
|
|
result::ok(m) { m }
|
|
|
|
};
|
2011-10-14 17:34:41 -07:00
|
|
|
|
2011-12-23 16:09:52 +01:00
|
|
|
alt unify_step(cx, expected_input.ty, actual_input.ty,
|
|
|
|
variance) {
|
|
|
|
ures_ok(rty) { result += [{mode: result_mode, ty: rty}]; }
|
|
|
|
err { ret either::left(err); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
either::right(result)
|
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
fn unify_fn(cx: @uctxt, e_f: fn_ty, a_f: fn_ty, variance: variance)
|
2011-12-23 16:09:52 +01:00
|
|
|
-> result {
|
|
|
|
alt unify_fn_proto(e_f.proto, a_f.proto, variance) {
|
2011-10-14 17:34:41 -07:00
|
|
|
some(err) { ret err; }
|
2012-01-18 22:37:22 -08:00
|
|
|
none { /* fall through */ }
|
2011-10-14 17:34:41 -07:00
|
|
|
}
|
|
|
|
|
2011-12-23 16:09:52 +01:00
|
|
|
if a_f.ret_style != ast::noreturn && a_f.ret_style != e_f.ret_style {
|
2011-09-14 11:01:42 +02:00
|
|
|
/* even though typestate checking is mostly
|
|
|
|
responsible for checking control flow annotations,
|
|
|
|
this check is necessary to ensure that the
|
|
|
|
annotation in an object method matches the
|
|
|
|
declared object type */
|
2011-12-23 16:09:52 +01:00
|
|
|
ret ures_err(terr_ret_style_mismatch(e_f.ret_style,
|
|
|
|
a_f.ret_style));
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
let result_ins = alt unify_args(cx, e_f.inputs, a_f.inputs,
|
|
|
|
variance) {
|
|
|
|
either::left(err) { ret err; }
|
|
|
|
either::right(ts) { ts }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check the output.
|
|
|
|
alt unify_step(cx, e_f.output, a_f.output, variance) {
|
|
|
|
ures_ok(rty) {
|
|
|
|
ures_ok(mk_fn(cx.tcx, {proto: e_f.proto,
|
|
|
|
inputs: result_ins,
|
|
|
|
output: rty
|
|
|
|
with a_f}))
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
x { x }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-31 10:26:29 -08:00
|
|
|
|
2011-06-08 18:39:34 -07:00
|
|
|
// If the given type is a variable, returns the structure of that type.
|
2012-02-03 15:15:28 +01:00
|
|
|
fn resolve_type_structure(vb: @var_bindings, typ: t) ->
|
2011-06-15 11:19:50 -07:00
|
|
|
fixup_result {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(typ).struct {
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_var(vid) {
|
2011-08-19 15:16:48 -07:00
|
|
|
if vid as uint >= ufind::set_count(vb.sets) { ret fix_err(vid); }
|
2011-08-15 16:21:55 -07:00
|
|
|
let root_id = ufind::find(vb.sets, vid as uint);
|
2011-08-13 00:09:25 -07:00
|
|
|
alt smallintmap::find::<t>(vb.types, root_id) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { ret fix_err(vid); }
|
2011-07-27 14:19:39 +02:00
|
|
|
some(rt) { ret fix_ok(rt); }
|
2011-06-08 18:39:34 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ { ret fix_ok(typ); }
|
2011-06-08 18:39:34 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:12:25 -07:00
|
|
|
|
|
|
|
// Specifies the allowable subtyping between expected and actual types
|
2012-01-19 14:24:03 -08:00
|
|
|
enum variance {
|
2011-10-17 18:12:25 -07:00
|
|
|
// Actual may be a subtype of expected
|
2012-01-19 17:56:05 -08:00
|
|
|
covariant,
|
2011-10-17 18:12:25 -07:00
|
|
|
// Actual may be a supertype of expected
|
2012-01-19 17:56:05 -08:00
|
|
|
contravariant,
|
2011-10-17 18:12:25 -07:00
|
|
|
// Actual must be the same type as expected
|
2012-01-19 17:56:05 -08:00
|
|
|
invariant,
|
2011-10-17 18:12:25 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:32:49 -07:00
|
|
|
// The calculation for recursive variance
|
|
|
|
// "Taming the Wildcards: Combining Definition- and Use-Site Variance"
|
|
|
|
// by John Altidor, et. al.
|
|
|
|
//
|
|
|
|
// I'm just copying the table from figure 1 - haven't actually
|
|
|
|
// read the paper (yet).
|
|
|
|
fn variance_transform(a: variance, b: variance) -> variance {
|
|
|
|
alt a {
|
2012-01-18 22:37:22 -08:00
|
|
|
covariant {
|
2011-10-17 18:32:49 -07:00
|
|
|
alt b {
|
2012-01-18 22:37:22 -08:00
|
|
|
covariant { covariant }
|
|
|
|
contravariant { contravariant }
|
|
|
|
invariant { invariant }
|
2011-10-17 18:32:49 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
contravariant {
|
2011-10-17 18:32:49 -07:00
|
|
|
alt b {
|
2012-01-18 22:37:22 -08:00
|
|
|
covariant { contravariant }
|
|
|
|
contravariant { covariant }
|
|
|
|
invariant { invariant }
|
2011-10-17 18:32:49 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
invariant {
|
2011-10-17 18:32:49 -07:00
|
|
|
alt b {
|
2012-01-18 22:37:22 -08:00
|
|
|
covariant { invariant }
|
|
|
|
contravariant { invariant }
|
|
|
|
invariant { invariant }
|
2011-10-17 18:32:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn unify_tps(cx: @uctxt, expected_tps: [t], actual_tps: [t],
|
2012-02-21 17:02:02 +01:00
|
|
|
variance: variance, finish: fn([t]) -> t) -> result {
|
2011-12-20 16:33:55 +01:00
|
|
|
let result_tps = [], i = 0u;
|
|
|
|
for exp in expected_tps {
|
|
|
|
let act = actual_tps[i];
|
|
|
|
i += 1u;
|
|
|
|
let result = unify_step(cx, exp, act, variance);
|
|
|
|
alt result {
|
|
|
|
ures_ok(rty) { result_tps += [rty]; }
|
|
|
|
_ { ret result; }
|
|
|
|
}
|
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
ures_ok(finish(result_tps))
|
|
|
|
}
|
|
|
|
fn unify_mt(cx: @uctxt, e_mt: mt, a_mt: mt, variance: variance,
|
|
|
|
mut_err: type_err, finish: fn(ctxt, mt) -> t) -> result {
|
|
|
|
alt unify_mut(e_mt.mutbl, a_mt.mutbl, variance) {
|
|
|
|
none { ures_err(mut_err) }
|
|
|
|
some((mutt, var)) {
|
|
|
|
alt unify_step(cx, e_mt.ty, a_mt.ty, var) {
|
|
|
|
ures_ok(result_sub) {
|
|
|
|
ures_ok(finish(cx.tcx, {ty: result_sub, mutbl: mutt}))
|
|
|
|
}
|
|
|
|
err { err }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn unify_step(cx: @uctxt, expected: t, actual: t,
|
2011-10-17 18:12:25 -07:00
|
|
|
variance: variance) -> result {
|
2011-04-22 10:51:32 -07:00
|
|
|
// Fast path.
|
2011-12-22 14:24:36 +01:00
|
|
|
if expected == actual { ret ures_ok(expected); }
|
2011-04-22 10:51:32 -07:00
|
|
|
|
2012-02-21 17:02:02 +01:00
|
|
|
alt (get(expected).struct, get(actual).struct) {
|
|
|
|
(ty_var(e_id), ty_var(a_id)) {
|
|
|
|
alt union(cx, e_id as uint, a_id as uint, variance) {
|
|
|
|
unres_ok { ures_ok(actual) }
|
|
|
|
unres_err(err) { ures_err(err) }
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(_, ty_var(a_id)) {
|
|
|
|
let v = variance_transform(variance, contravariant);
|
|
|
|
record_var_binding(cx, a_id, expected, v)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_var(e_id), _) {
|
|
|
|
let v = variance_transform(variance, covariant);
|
|
|
|
record_var_binding(cx, e_id, actual, v)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(_, ty_bot) { ures_ok(expected) }
|
|
|
|
(ty_bot, _) { ures_ok(actual) }
|
|
|
|
(ty_nil, _) | (ty_bool, _) | (ty_int(_), _) | (ty_uint(_), _) |
|
|
|
|
(ty_float(_), _) | (ty_str, _) | (ty_send_type, _) {
|
|
|
|
struct_cmp(cx, expected, actual)
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_param(e_n, _), ty_param(a_n, _)) if e_n == a_n {
|
|
|
|
ures_ok(expected)
|
2011-12-29 11:23:35 +01:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_enum(e_id, e_tps), ty_enum(a_id, a_tps)) if e_id == a_id {
|
|
|
|
unify_tps(cx, e_tps, a_tps, variance, {|tps|
|
|
|
|
mk_enum(cx.tcx, e_id, tps)
|
|
|
|
})
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_iface(e_id, e_tps), ty_iface(a_id, a_tps)) if e_id == a_id {
|
|
|
|
unify_tps(cx, e_tps, a_tps, variance, {|tps|
|
|
|
|
mk_iface(cx.tcx, e_id, tps)
|
|
|
|
})
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_class(e_id, e_tps), ty_class(a_id, a_tps)) if e_id == a_id {
|
|
|
|
unify_tps(cx, e_tps, a_tps, variance, {|tps|
|
|
|
|
mk_class(cx.tcx, e_id, tps)
|
|
|
|
})
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_box(e_mt), ty_box(a_mt)) {
|
|
|
|
unify_mt(cx, e_mt, a_mt, variance, terr_box_mutability, mk_box)
|
2011-08-10 17:23:46 -07:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_uniq(e_mt), ty_uniq(a_mt)) {
|
|
|
|
unify_mt(cx, e_mt, a_mt, variance, terr_box_mutability, mk_uniq)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_vec(e_mt), ty_vec(a_mt)) {
|
|
|
|
unify_mt(cx, e_mt, a_mt, variance, terr_vec_mutability, mk_vec)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_ptr(e_mt), ty_ptr(a_mt)) {
|
|
|
|
unify_mt(cx, e_mt, a_mt, variance, terr_ptr_mutability, mk_ptr)
|
|
|
|
}
|
|
|
|
(ty_res(e_id, e_inner, e_tps), ty_res(a_id, a_inner, a_tps))
|
|
|
|
if e_id == a_id {
|
|
|
|
alt unify_step(cx, e_inner, a_inner, variance) {
|
|
|
|
ures_ok(res_inner) {
|
|
|
|
unify_tps(cx, e_tps, a_tps, variance, {|tps|
|
|
|
|
mk_res(cx.tcx, a_id, res_inner, tps)
|
|
|
|
})
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
err { err }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_rec(e_fields), ty_rec(a_fields)) {
|
|
|
|
let e_len = e_fields.len(), a_len = a_fields.len();
|
|
|
|
if e_len != a_len {
|
|
|
|
ret ures_err(terr_record_size(e_len, a_len));
|
|
|
|
}
|
|
|
|
let result_fields = [], i = 0u;
|
|
|
|
while i < a_len {
|
|
|
|
let e_field = e_fields[i], a_field = a_fields[i];
|
|
|
|
if e_field.ident != a_field.ident {
|
|
|
|
ret ures_err(terr_record_fields(e_field.ident,
|
|
|
|
a_field.ident));
|
2011-02-23 11:59:07 -05:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
alt unify_mt(cx, e_field.mt, a_field.mt, variance,
|
|
|
|
terr_record_mutability, {|cx, mt|
|
|
|
|
result_fields += [{mt: mt with e_field}];
|
|
|
|
mk_nil(cx)
|
|
|
|
}) {
|
|
|
|
ures_ok(_) {}
|
|
|
|
err { ret err; }
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
i += 1u;
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
ures_ok(mk_rec(cx.tcx, result_fields))
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_tup(e_elems), ty_tup(a_elems)) {
|
|
|
|
let e_len = e_elems.len(), a_len = a_elems.len();
|
|
|
|
if e_len != a_len { ret ures_err(terr_tuple_size(e_len, a_len)); }
|
|
|
|
let result_elems = [], i = 0u;
|
|
|
|
while i < a_len {
|
|
|
|
alt unify_step(cx, e_elems[i], a_elems[i], variance) {
|
|
|
|
ures_ok(rty) { result_elems += [rty]; }
|
|
|
|
err { ret err; }
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
i += 1u;
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
ures_ok(mk_tup(cx.tcx, result_elems))
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_fn(e_fty), ty_fn(a_fty)) {
|
|
|
|
unify_fn(cx, e_fty, a_fty, variance)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_constr(e_t, e_constrs), ty_constr(a_t, a_constrs)) {
|
2011-07-27 14:19:39 +02:00
|
|
|
// unify the base types...
|
2012-02-21 17:02:02 +01:00
|
|
|
alt unify_step(cx, e_t, a_t, variance) {
|
|
|
|
ures_ok(rty) {
|
|
|
|
// FIXME: probably too restrictive --
|
|
|
|
// requires the constraints to be syntactically equal
|
|
|
|
unify_constrs(expected, e_constrs, a_constrs)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
err { err }
|
2011-07-19 17:52:34 -07:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
(ty_constr(e_t, _), _) {
|
|
|
|
// If the actual type is *not* a constrained type,
|
|
|
|
// then we go ahead and just ignore the constraints on
|
|
|
|
// the expected type. typestate handles the rest.
|
|
|
|
unify_step(cx, e_t, actual, variance)
|
2012-02-20 15:42:21 -08:00
|
|
|
}
|
2012-02-21 17:02:02 +01:00
|
|
|
_ { ures_err(terr_mismatch) }
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
|
|
|
}
|
2011-12-29 13:12:52 +01:00
|
|
|
fn unify(expected: t, actual: t, st: unify_style,
|
2012-02-03 15:15:28 +01:00
|
|
|
tcx: ctxt) -> result {
|
2011-12-29 13:12:52 +01:00
|
|
|
let cx = @{st: st, tcx: tcx};
|
2011-10-17 18:12:25 -07:00
|
|
|
ret unify_step(cx, expected, actual, covariant);
|
2011-05-20 18:36:35 -07:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
fn dump_var_bindings(tcx: ctxt, vb: @var_bindings) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-08-13 00:09:25 -07:00
|
|
|
while i < vec::len::<ufind::node>(vb.sets.nodes) {
|
2011-09-02 15:34:58 -07:00
|
|
|
let sets = "";
|
2011-07-27 14:19:39 +02:00
|
|
|
let j = 0u;
|
2012-01-31 17:05:20 -08:00
|
|
|
while j < vec::len::<option<uint>>(vb.sets.nodes) {
|
2011-09-01 18:49:10 -07:00
|
|
|
if ufind::find(vb.sets, j) == i { sets += #fmt[" %u", j]; }
|
2011-05-20 18:36:35 -07:00
|
|
|
j += 1u;
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
let typespec;
|
2011-08-13 00:09:25 -07:00
|
|
|
alt smallintmap::find::<t>(vb.types, i) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { typespec = ""; }
|
2011-09-02 15:34:58 -07:00
|
|
|
some(typ) { typespec = " =" + ty_to_str(tcx, typ); }
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
2011-12-22 14:42:52 -08:00
|
|
|
#error("set %u:%s%s", i, typespec, sets);
|
2011-05-20 18:36:35 -07:00
|
|
|
i += 1u;
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 18:36:35 -07:00
|
|
|
// Fixups and substitutions
|
2011-08-03 18:06:57 -07:00
|
|
|
// Takes an optional span - complain about occurs check violations
|
|
|
|
// iff the span is present (so that if we already know we're going
|
|
|
|
// to error anyway, we don't complain)
|
2012-02-03 15:15:28 +01:00
|
|
|
fn fixup_vars(tcx: ctxt, sp: option<span>, vb: @var_bindings,
|
2011-08-19 15:16:48 -07:00
|
|
|
typ: t) -> fixup_result {
|
2012-02-03 15:15:28 +01:00
|
|
|
fn subst_vars(tcx: ctxt, sp: option<span>, vb: @var_bindings,
|
2012-01-31 17:05:20 -08:00
|
|
|
unresolved: @mutable option<int>,
|
2012-01-27 18:58:52 +01:00
|
|
|
vars_seen: std::list::list<int>, vid: int) -> t {
|
2011-08-03 18:06:57 -07:00
|
|
|
// Should really return a fixup_result instead of a t, but fold_ty
|
|
|
|
// doesn't allow returning anything but a t.
|
2011-08-15 16:21:55 -07:00
|
|
|
if vid as uint >= ufind::set_count(vb.sets) {
|
2011-07-21 15:46:03 -07:00
|
|
|
*unresolved = some(vid);
|
2012-02-03 15:15:28 +01:00
|
|
|
ret mk_var(tcx, vid);
|
2011-06-09 11:20:47 -07:00
|
|
|
}
|
2011-08-15 16:21:55 -07:00
|
|
|
let root_id = ufind::find(vb.sets, vid as uint);
|
2011-08-13 00:09:25 -07:00
|
|
|
alt smallintmap::find::<t>(vb.types, root_id) {
|
2012-02-03 15:15:28 +01:00
|
|
|
none { *unresolved = some(vid); ret mk_var(tcx, vid); }
|
2011-07-27 14:19:39 +02:00
|
|
|
some(rt) {
|
2012-01-27 18:58:52 +01:00
|
|
|
let give_up = false;
|
|
|
|
std::list::iter(vars_seen) {|v|
|
|
|
|
if v == vid {
|
|
|
|
give_up = true;
|
|
|
|
option::may(sp) {|sp|
|
|
|
|
tcx.sess.span_fatal(
|
|
|
|
sp, "can not instantiate infinite type");
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 18:06:57 -07:00
|
|
|
}
|
2012-01-27 18:58:52 +01:00
|
|
|
// Return the type unchanged, so we can error out
|
|
|
|
// downstream
|
|
|
|
if give_up { ret rt; }
|
|
|
|
ret fold_ty(tcx, fm_var(bind subst_vars(
|
|
|
|
tcx, sp, vb, unresolved, std::list::cons(vid, @vars_seen),
|
|
|
|
_)), rt);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-04-08 14:53:16 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-13 00:09:25 -07:00
|
|
|
let unresolved = @mutable none::<int>;
|
2012-01-27 18:58:52 +01:00
|
|
|
let rty = fold_ty(tcx, fm_var(bind subst_vars(
|
|
|
|
tcx, sp, vb, unresolved, std::list::nil, _)), typ);
|
2011-07-27 14:19:39 +02:00
|
|
|
let ur = *unresolved;
|
|
|
|
alt ur {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { ret fix_ok(rty); }
|
2011-07-27 14:19:39 +02:00
|
|
|
some(var_id) { ret fix_err(var_id); }
|
2011-06-07 23:03:41 -07:00
|
|
|
}
|
2011-04-08 14:53:16 -07:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
fn resolve_type_var(tcx: ctxt, sp: option<span>, vb: @var_bindings,
|
2011-09-12 11:27:30 +02:00
|
|
|
vid: int) -> fixup_result {
|
2011-08-15 16:21:55 -07:00
|
|
|
if vid as uint >= ufind::set_count(vb.sets) { ret fix_err(vid); }
|
|
|
|
let root_id = ufind::find(vb.sets, vid as uint);
|
2011-08-13 00:09:25 -07:00
|
|
|
alt smallintmap::find::<t>(vb.types, root_id) {
|
2012-01-18 22:37:22 -08:00
|
|
|
none { ret fix_err(vid); }
|
2011-08-03 18:06:57 -07:00
|
|
|
some(rt) { ret fixup_vars(tcx, sp, vb, rt); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 14:52:30 +01:00
|
|
|
fn same_type(cx: ctxt, a: t, b: t) -> bool {
|
2011-12-29 13:12:52 +01:00
|
|
|
alt unify::unify(a, b, unify::precise, cx) {
|
2011-12-22 14:52:30 +01:00
|
|
|
unify::ures_ok(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_err_to_str(err: type_err) -> str {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt err {
|
2012-01-18 22:37:22 -08:00
|
|
|
terr_mismatch { ret "types differ"; }
|
2011-09-14 11:01:42 +02:00
|
|
|
terr_ret_style_mismatch(expect, actual) {
|
|
|
|
fn to_str(s: ast::ret_style) -> str {
|
|
|
|
alt s {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::noreturn { "non-returning" }
|
2012-01-19 01:03:57 -08:00
|
|
|
ast::return_val { "return-by-value" }
|
2011-09-14 11:01:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret to_str(actual) + " function found where " + to_str(expect) +
|
|
|
|
" function was expected";
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
terr_box_mutability { ret "boxed values differ in mutability"; }
|
|
|
|
terr_vec_mutability { ret "vectors differ in mutability"; }
|
2012-02-21 17:02:02 +01:00
|
|
|
terr_ptr_mutability { ret "pointers differ in mutability"; }
|
2011-08-15 11:40:26 +02:00
|
|
|
terr_tuple_size(e_sz, a_sz) {
|
2011-09-02 15:34:58 -07:00
|
|
|
ret "expected a tuple with " + uint::to_str(e_sz, 10u) +
|
|
|
|
" elements but found one with " + uint::to_str(a_sz, 10u) +
|
|
|
|
" elements";
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
terr_record_size(e_sz, a_sz) {
|
2011-09-02 15:34:58 -07:00
|
|
|
ret "expected a record with " + uint::to_str(e_sz, 10u) +
|
|
|
|
" fields but found one with " + uint::to_str(a_sz, 10u) +
|
|
|
|
" fields";
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
terr_record_mutability { ret "record elements differ in mutability"; }
|
2011-07-27 14:19:39 +02:00
|
|
|
terr_record_fields(e_fld, a_fld) {
|
2011-09-02 15:34:58 -07:00
|
|
|
ret "expected a record with field '" + e_fld +
|
|
|
|
"' but found one with field '" + a_fld + "'";
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
terr_arg_count { ret "incorrect number of function parameters"; }
|
2011-07-27 14:19:39 +02:00
|
|
|
terr_mode_mismatch(e_mode, a_mode) {
|
2012-02-02 16:50:17 -08:00
|
|
|
ret "expected argument mode " + mode_to_str(e_mode) + " but found " +
|
|
|
|
mode_to_str(a_mode);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
terr_constr_len(e_len, a_len) {
|
2012-03-05 16:27:27 -08:00
|
|
|
ret "expected a type with " + uint::str(e_len) +
|
2011-09-02 15:34:58 -07:00
|
|
|
" constraints, but found one with " + uint::str(a_len) +
|
|
|
|
" constraints";
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
terr_constr_mismatch(e_constr, a_constr) {
|
2012-03-05 16:27:27 -08:00
|
|
|
ret "expected a type with constraint " + ty_constr_to_str(e_constr) +
|
2011-09-02 15:34:58 -07:00
|
|
|
" but found one with constraint " +
|
|
|
|
ty_constr_to_str(a_constr);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2010-12-21 17:47:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 18:36:35 -07:00
|
|
|
// Replaces type parameters in the given type using the given list of
|
|
|
|
// substitions.
|
2011-09-12 11:27:30 +02:00
|
|
|
fn substitute_type_params(cx: ctxt, substs: [ty::t], typ: t) -> t {
|
2012-01-14 16:05:07 -08:00
|
|
|
// Precondition? idx < vec::len(substs)
|
2012-01-30 11:52:34 +01:00
|
|
|
fold_ty(cx, fm_param({|idx, _id| substs[idx]}), typ)
|
2011-04-08 21:27:54 -07:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn def_has_ty_params(def: ast::def) -> bool {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt def {
|
2012-01-26 10:29:47 +01:00
|
|
|
ast::def_fn(_, _) | ast::def_variant(_, _) { true }
|
2012-02-07 11:25:04 +01:00
|
|
|
_ { false }
|
2011-03-30 17:23:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 08:45:18 +01:00
|
|
|
fn store_iface_methods(cx: ctxt, id: ast::node_id, ms: @[method]) {
|
|
|
|
cx.iface_method_cache.insert(ast_util::local_def(id), ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iface_methods(cx: ctxt, id: ast::def_id) -> @[method] {
|
|
|
|
alt cx.iface_method_cache.find(id) {
|
|
|
|
some(ms) { ret ms; }
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
// Local interfaces are supposed to have been added explicitly.
|
|
|
|
assert id.crate != ast::local_crate;
|
2012-01-05 13:57:27 +01:00
|
|
|
let result = csearch::get_iface_methods(cx, id);
|
2011-12-22 08:45:18 +01:00
|
|
|
cx.iface_method_cache.insert(id, result);
|
|
|
|
result
|
|
|
|
}
|
2011-05-19 17:21:21 -07:00
|
|
|
|
2012-01-31 17:05:20 -08:00
|
|
|
fn impl_iface(cx: ctxt, id: ast::def_id) -> option<t> {
|
2012-01-05 10:57:19 +01:00
|
|
|
if id.crate == ast::local_crate {
|
2012-03-07 12:54:00 +01:00
|
|
|
alt cx.items.get(id.node) {
|
|
|
|
ast_map::node_item(@{node: ast::item_impl(
|
|
|
|
_, some(@{node: ast::ty_path(_, id), _}), _, _), _}, _) {
|
|
|
|
some(node_id_to_type(cx, id))
|
|
|
|
}
|
|
|
|
_ { none }
|
|
|
|
}
|
2012-01-05 10:57:19 +01:00
|
|
|
} else {
|
|
|
|
csearch::get_impl_iface(cx, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 14:34:31 +01:00
|
|
|
// Enum information
|
2012-02-03 15:15:28 +01:00
|
|
|
type variant_info = @{args: [t], ctor_ty: t, name: str,
|
2012-01-15 21:42:10 -08:00
|
|
|
id: ast::def_id, disr_val: int};
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2012-02-07 21:19:53 -08:00
|
|
|
fn substd_enum_variants(cx: ctxt, id: ast::def_id, tps: [ty::t])
|
|
|
|
-> [variant_info] {
|
|
|
|
vec::map(*enum_variants(cx, id)) { |variant_info|
|
|
|
|
let substd_args = vec::map(variant_info.args) {|aty|
|
|
|
|
substitute_type_params(cx, tps, aty)
|
|
|
|
};
|
|
|
|
|
|
|
|
let substd_ctor_ty =
|
|
|
|
substitute_type_params(cx, tps, variant_info.ctor_ty);
|
|
|
|
|
|
|
|
@{args: substd_args, ctor_ty: substd_ctor_ty with *variant_info}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 15:21:53 -08:00
|
|
|
fn item_path_str(cx: ctxt, id: ast::def_id) -> str {
|
|
|
|
ast_map::path_to_str(item_path(cx, id))
|
|
|
|
}
|
|
|
|
|
2012-02-10 06:01:32 -08:00
|
|
|
fn item_path(cx: ctxt, id: ast::def_id) -> ast_map::path {
|
|
|
|
if id.crate != ast::local_crate {
|
|
|
|
csearch::get_item_path(cx, id)
|
|
|
|
} else {
|
|
|
|
let node = cx.items.get(id.node);
|
|
|
|
alt node {
|
|
|
|
ast_map::node_item(item, path) {
|
|
|
|
let item_elt = alt item.node {
|
|
|
|
item_mod(_) | item_native_mod(_) {
|
|
|
|
ast_map::path_mod(item.ident)
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
ast_map::path_name(item.ident)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*path + [item_elt]
|
|
|
|
}
|
|
|
|
|
2012-03-07 12:21:08 +01:00
|
|
|
ast_map::node_native_item(nitem, _, path) {
|
2012-02-10 06:01:32 -08:00
|
|
|
*path + [ast_map::path_name(nitem.ident)]
|
|
|
|
}
|
|
|
|
|
2012-02-10 16:38:33 -08:00
|
|
|
ast_map::node_method(method, _, path) {
|
2012-02-10 06:01:32 -08:00
|
|
|
*path + [ast_map::path_name(method.ident)]
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_map::node_variant(variant, _, path) {
|
|
|
|
vec::init(*path) + [ast_map::path_name(variant.node.name)]
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_map::node_expr(_) | ast_map::node_arg(_, _) |
|
2012-03-07 12:21:08 +01:00
|
|
|
ast_map::node_local(_) | ast_map::node_ctor(_) |
|
2012-03-06 13:57:07 +01:00
|
|
|
ast_map::node_export(_, _) {
|
2012-02-10 06:01:32 -08:00
|
|
|
cx.sess.bug(#fmt["cannot find item_path for node %?", node]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 14:34:31 +01:00
|
|
|
fn enum_variants(cx: ctxt, id: ast::def_id) -> @[variant_info] {
|
|
|
|
alt cx.enum_var_cache.find(id) {
|
2011-12-22 16:23:49 +01:00
|
|
|
some(variants) { ret variants; }
|
2011-12-15 16:27:18 -08:00
|
|
|
_ { /* fallthrough */ }
|
|
|
|
}
|
2011-12-22 16:23:49 +01:00
|
|
|
let result = if ast::local_crate != id.crate {
|
2012-01-25 14:34:31 +01:00
|
|
|
@csearch::get_enum_variants(cx, id)
|
2011-12-22 16:23:49 +01:00
|
|
|
} else {
|
2012-01-16 02:36:47 -07:00
|
|
|
// FIXME: Now that the variants are run through the type checker (to
|
2012-01-16 21:04:02 -07:00
|
|
|
// check the disr_expr if it exists), this code should likely be
|
|
|
|
// moved there to avoid having to call eval_const_expr twice.
|
2011-12-22 16:23:49 +01:00
|
|
|
alt cx.items.get(id.node) {
|
2012-02-03 09:53:37 +01:00
|
|
|
ast_map::node_item(@{node: ast::item_enum(variants, _), _}, _) {
|
2012-01-16 02:36:47 -07:00
|
|
|
let disr_val = -1;
|
2011-12-22 08:45:18 +01:00
|
|
|
@vec::map(variants, {|variant|
|
2012-01-30 17:28:30 +01:00
|
|
|
let ctor_ty = node_id_to_type(cx, variant.node.id);
|
2011-12-22 08:45:18 +01:00
|
|
|
let arg_tys = if vec::len(variant.node.args) > 0u {
|
2012-02-03 15:15:28 +01:00
|
|
|
vec::map(ty_fn_args(ctor_ty), {|a| a.ty})
|
2011-12-22 08:45:18 +01:00
|
|
|
} else { [] };
|
2012-01-16 02:36:47 -07:00
|
|
|
alt variant.node.disr_expr {
|
|
|
|
some (ex) {
|
|
|
|
// FIXME: issue #1417
|
|
|
|
disr_val = alt syntax::ast_util::eval_const_expr(ex) {
|
|
|
|
ast_util::const_int(val) {val as int}
|
2012-01-30 21:00:57 -08:00
|
|
|
_ { cx.sess.bug("tag_variants: bad disr expr"); }
|
2012-01-16 02:36:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {disr_val += 1;}
|
|
|
|
}
|
2011-12-22 08:45:18 +01:00
|
|
|
@{args: arg_tys,
|
|
|
|
ctor_ty: ctor_ty,
|
2012-01-15 21:42:10 -08:00
|
|
|
name: variant.node.name,
|
2012-01-10 14:50:40 -07:00
|
|
|
id: ast_util::local_def(variant.node.id),
|
2012-01-16 02:36:47 -07:00
|
|
|
disr_val: disr_val
|
2012-01-10 14:50:40 -07:00
|
|
|
}
|
2011-12-22 08:45:18 +01:00
|
|
|
})
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-01-30 21:00:57 -08:00
|
|
|
_ { cx.sess.bug("tag_variants: id not bound to an enum"); }
|
2011-05-19 17:21:21 -07:00
|
|
|
}
|
2011-12-22 16:23:49 +01:00
|
|
|
};
|
2012-01-25 14:34:31 +01:00
|
|
|
cx.enum_var_cache.insert(id, result);
|
2011-12-22 16:23:49 +01:00
|
|
|
result
|
2011-05-19 17:21:21 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
// Returns information about the enum variant with the given ID:
|
2012-01-25 14:34:31 +01:00
|
|
|
fn enum_variant_with_id(cx: ctxt, enum_id: ast::def_id,
|
|
|
|
variant_id: ast::def_id) -> variant_info {
|
|
|
|
let variants = enum_variants(cx, enum_id);
|
2011-07-27 14:19:39 +02:00
|
|
|
let i = 0u;
|
2011-12-15 17:14:58 -08:00
|
|
|
while i < vec::len::<variant_info>(*variants) {
|
2011-08-19 15:16:48 -07:00
|
|
|
let variant = variants[i];
|
2011-07-27 14:19:39 +02:00
|
|
|
if def_eq(variant.id, variant_id) { ret variant; }
|
2011-05-19 17:21:21 -07:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-01-25 14:34:31 +01:00
|
|
|
cx.sess.bug("enum_variant_with_id(): no variant exists with that ID");
|
2011-05-19 17:21:21 -07:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-03-30 17:23:25 -07:00
|
|
|
// If the given item is in an external crate, looks up its type and adds it to
|
|
|
|
// the type cache. Returns the type parameters and type.
|
2011-12-28 17:50:12 +01:00
|
|
|
fn lookup_item_type(cx: ctxt, did: ast::def_id) -> ty_param_bounds_and_ty {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt cx.tcache.find(did) {
|
|
|
|
some(tpt) { ret tpt; }
|
2012-01-18 22:37:22 -08:00
|
|
|
none {
|
2012-02-02 12:37:17 +01:00
|
|
|
// The item is in this crate. The caller should have added it to the
|
|
|
|
// type cache already
|
|
|
|
assert did.crate != ast::local_crate;
|
2011-07-27 14:19:39 +02:00
|
|
|
let tyt = csearch::get_type(cx, did);
|
|
|
|
cx.tcache.insert(did, tyt);
|
|
|
|
ret tyt;
|
|
|
|
}
|
2011-03-30 17:23:25 -07:00
|
|
|
}
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the list of items for a given class (in the item map).
|
|
|
|
// Fails if the id is not bound to a class.
|
|
|
|
fn lookup_class_items(cx: ctxt, did: ast::def_id) -> [@class_item] {
|
|
|
|
alt cx.items.find(did.node) {
|
|
|
|
some(ast_map::node_item(i,_)) {
|
|
|
|
alt i.node {
|
|
|
|
ast::item_class(_, items, _) {
|
|
|
|
items
|
|
|
|
}
|
|
|
|
_ { cx.sess.bug("class ID bound to non-class"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { cx.sess.bug("class ID not bound to an item"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a list of fields corresponding to the class's items
|
|
|
|
// (as if the class was a record). trans uses this
|
|
|
|
fn class_items_as_fields(cx:ctxt, did: ast::def_id) -> [field] {
|
|
|
|
let rslt = [];
|
|
|
|
for ci in lookup_class_items(cx, did) {
|
|
|
|
rslt += [alt ci.node.decl {
|
|
|
|
instance_var(i, _, _, id) {
|
|
|
|
// consider all instance vars mutable, because the
|
|
|
|
// constructor may mutate all vars
|
|
|
|
{ident: i, mt: {ty: node_id_to_type(cx, id),
|
|
|
|
mutbl: m_mutbl}}
|
|
|
|
}
|
|
|
|
class_method(it) {
|
|
|
|
{ident:it.ident, mt: {ty: node_id_to_type(cx, it.id),
|
|
|
|
mutbl: m_const}}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
rslt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks up the type for a given class item. Must be called
|
|
|
|
// post-typechecking.
|
|
|
|
fn class_item_type(cx: ctxt, ci: @ast::class_item) -> t {
|
|
|
|
alt ci.node.decl {
|
|
|
|
ast::instance_var(_,_,_,id) { node_id_to_type(cx, id) }
|
|
|
|
// TODO: only works for local classes
|
|
|
|
ast::class_method(it) { lookup_item_type(cx,
|
|
|
|
ast_util::local_def(it.id)).ty }
|
|
|
|
}
|
2011-03-30 17:23:25 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
|
2011-07-27 14:19:39 +02:00
|
|
|
const tycat_other: int = 0;
|
|
|
|
const tycat_bool: int = 1;
|
|
|
|
const tycat_int: int = 2;
|
|
|
|
const tycat_float: int = 3;
|
|
|
|
const tycat_str: int = 4;
|
|
|
|
const tycat_vec: int = 5;
|
|
|
|
const tycat_struct: int = 6;
|
2011-08-02 18:04:24 -07:00
|
|
|
const tycat_bot: int = 7;
|
2011-07-27 14:19:39 +02:00
|
|
|
|
|
|
|
const opcat_add: int = 0;
|
|
|
|
const opcat_sub: int = 1;
|
|
|
|
const opcat_mult: int = 2;
|
|
|
|
const opcat_shift: int = 3;
|
|
|
|
const opcat_rel: int = 4;
|
|
|
|
const opcat_eq: int = 5;
|
|
|
|
const opcat_bit: int = 6;
|
|
|
|
const opcat_logic: int = 7;
|
|
|
|
|
|
|
|
fn opcat(op: ast::binop) -> int {
|
|
|
|
alt op {
|
2012-01-18 22:37:22 -08:00
|
|
|
ast::add { opcat_add }
|
|
|
|
ast::subtract { opcat_sub }
|
|
|
|
ast::mul { opcat_mult }
|
|
|
|
ast::div { opcat_mult }
|
|
|
|
ast::rem { opcat_mult }
|
|
|
|
ast::and { opcat_logic }
|
|
|
|
ast::or { opcat_logic }
|
|
|
|
ast::bitxor { opcat_bit }
|
|
|
|
ast::bitand { opcat_bit }
|
|
|
|
ast::bitor { opcat_bit }
|
|
|
|
ast::lsl { opcat_shift }
|
|
|
|
ast::lsr { opcat_shift }
|
|
|
|
ast::asr { opcat_shift }
|
|
|
|
ast::eq { opcat_eq }
|
|
|
|
ast::ne { opcat_eq }
|
|
|
|
ast::lt { opcat_rel }
|
|
|
|
ast::le { opcat_rel }
|
|
|
|
ast::ge { opcat_rel }
|
|
|
|
ast::gt { opcat_rel }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn tycat(ty: t) -> int {
|
|
|
|
alt get(ty).struct {
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_bool { tycat_bool }
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_int(_) { tycat_int }
|
|
|
|
ty_uint(_) { tycat_int }
|
|
|
|
ty_float(_) { tycat_float }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_str { tycat_str }
|
2011-08-18 14:11:06 -07:00
|
|
|
ty_vec(_) { tycat_vec }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(_) { tycat_struct }
|
2011-08-15 11:40:26 +02:00
|
|
|
ty_tup(_) { tycat_struct }
|
2012-01-25 14:34:31 +01:00
|
|
|
ty_enum(_, _) { tycat_struct }
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_bot { tycat_bot }
|
2011-07-27 14:19:39 +02:00
|
|
|
_ { tycat_other }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const t: bool = true;
|
|
|
|
const f: bool = false;
|
2011-06-24 14:19:58 -07:00
|
|
|
|
|
|
|
/*. add, shift, bit
|
|
|
|
. sub, rel, logic
|
|
|
|
. mult, eq, */
|
2011-09-02 15:34:58 -07:00
|
|
|
/*other*/
|
|
|
|
/*bool*/
|
|
|
|
/*int*/
|
|
|
|
/*float*/
|
|
|
|
/*str*/
|
|
|
|
/*vec*/
|
|
|
|
/*bot*/
|
2011-08-19 15:16:48 -07:00
|
|
|
let tbl =
|
|
|
|
[[f, f, f, f, t, t, f, f], [f, f, f, f, t, t, t, t],
|
|
|
|
[t, t, t, t, t, t, t, f], [t, t, t, f, t, t, f, f],
|
|
|
|
[t, f, f, f, t, t, f, f], [t, f, f, f, t, t, f, f],
|
|
|
|
[f, f, f, f, t, t, f, f], [t, t, t, t, t, t, t, t]]; /*struct*/
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
ret tbl[tycat(ty)][opcat(op)];
|
2011-06-24 14:19:58 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn ast_constr_to_constr<T>(tcx: ctxt, c: @ast::constr_general<T>) ->
|
|
|
|
@constr_general<T> {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt tcx.def_map.find(c.node.id) {
|
2012-01-18 22:37:22 -08:00
|
|
|
some(ast::def_fn(pred_id, ast::pure_fn)) {
|
2011-08-21 21:44:41 -07:00
|
|
|
ret @ast_util::respan(c.span,
|
|
|
|
{path: c.node.path,
|
|
|
|
args: c.node.args,
|
|
|
|
id: pred_id});
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
_ {
|
2011-09-12 11:27:30 +02:00
|
|
|
tcx.sess.span_fatal(c.span,
|
2012-03-05 16:27:27 -08:00
|
|
|
"predicate " + path_to_str(c.node.path) +
|
2011-09-12 12:39:38 +02:00
|
|
|
" is unbound or bound to a non-function or an \
|
2011-09-02 15:34:58 -07:00
|
|
|
impure function");
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-21 15:59:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 17:42:28 -08:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|