2012-05-12 19:29:02 -07:00
|
|
|
import std::{map, smallintmap};
|
2012-03-16 15:14:37 -07:00
|
|
|
import result::result;
|
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;
|
2012-05-21 23:34:34 -07:00
|
|
|
import syntax::{ast, ast_map};
|
2011-08-21 21:44:41 -07:00
|
|
|
import syntax::ast_util;
|
2012-05-23 00:38:39 -07:00
|
|
|
import syntax::ast_util::{is_local, local_def, split_class_items,
|
|
|
|
new_def_hash};
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::codemap::span;
|
2011-07-07 22:18:38 -07:00
|
|
|
import metadata::csearch;
|
2012-03-15 19:19:32 -07:00
|
|
|
import util::ppaux::region_to_str;
|
2012-04-14 18:45:03 -07:00
|
|
|
import util::ppaux::vstore_to_str;
|
2012-05-03 09:17:58 -07:00
|
|
|
import util::ppaux::{ty_to_str, tys_to_str, ty_constr_to_str};
|
2012-06-04 18:34:11 -07:00
|
|
|
import middle::lint::{get_warning_level, vecs_not_implicitly_copyable,
|
|
|
|
ignore};
|
2012-06-06 14:19:52 -07:00
|
|
|
import syntax::ast::*;
|
|
|
|
import syntax::print::pprust::*;
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
export tv_vid, tvi_vid, region_vid, vid;
|
2012-04-01 14:28:30 -07:00
|
|
|
export br_hashmap;
|
2012-03-28 13:42:33 -07:00
|
|
|
export is_instantiable;
|
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-06-08 14:37:55 -07:00
|
|
|
export class_items_as_fields, class_items_as_mutable_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 ctxt;
|
2012-04-26 16:02:01 -07:00
|
|
|
export deref, deref_sty;
|
|
|
|
export index, index_sty;
|
2011-06-19 02:34:20 -07:00
|
|
|
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;
|
2012-03-19 10:19:00 -07:00
|
|
|
export field_ty;
|
2012-05-09 06:09:58 -07:00
|
|
|
export fold_ty, fold_sty_to_ty, fold_region, fold_regions;
|
2012-04-30 10:37:58 -07:00
|
|
|
export fold_regions_and_ty, walk_regions_and_ty;
|
2011-06-19 02:34:20 -07:00
|
|
|
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 get_element_type;
|
2012-06-01 17:06:27 -07:00
|
|
|
export has_dtor;
|
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-27 22:08:48 -07:00
|
|
|
export lookup_class_field, lookup_class_fields;
|
2012-03-21 12:42:34 -07:00
|
|
|
export lookup_class_method_by_name;
|
2012-03-19 10:19:00 -07:00
|
|
|
export lookup_field_type;
|
2011-06-19 02:34:20 -07:00
|
|
|
export lookup_item_type;
|
2012-03-26 09:59:59 -07:00
|
|
|
export lookup_public_fields;
|
2011-06-19 02:34:20 -07:00
|
|
|
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;
|
2011-06-19 22:41:21 +02:00
|
|
|
export stmt_node_id;
|
2011-06-19 02:34:20 -07:00
|
|
|
export sty;
|
2012-05-09 06:09:58 -07:00
|
|
|
export subst, subst_tps, substs_is_noop, substs_to_str, substs;
|
2011-06-19 02:34:20 -07:00
|
|
|
export t;
|
2011-12-22 14:24:36 +01:00
|
|
|
export new_ty_hash;
|
2012-06-12 12:05:26 -07:00
|
|
|
export enum_variants, substd_enum_variants, enum_is_univariant;
|
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;
|
2012-05-15 17:59:55 -07:00
|
|
|
export ty_dtor;
|
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;
|
2012-05-23 14:12:40 -07:00
|
|
|
export ty_fn_proto, ty_fn_ret, ty_fn_ret_style, tys_in_fn_ty;
|
2012-02-07 11:25:04 +01:00
|
|
|
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;
|
2012-04-09 17:32:49 -07:00
|
|
|
export ty_estr, mk_estr;
|
|
|
|
export ty_evec, mk_evec;
|
|
|
|
export vstore, vstore_fixed, vstore_uniq, vstore_box, vstore_slice;
|
2012-02-07 11:25:04 +01:00
|
|
|
export ty_nil, mk_nil, type_is_nil;
|
|
|
|
export ty_iface, mk_iface;
|
|
|
|
export ty_res, mk_res;
|
2012-04-02 10:48:32 -07:00
|
|
|
export ty_param, mk_param, ty_params_to_tys;
|
2012-03-21 15:42:20 +01:00
|
|
|
export ty_ptr, mk_ptr, mk_mut_ptr, mk_imm_ptr, mk_nil_ptr, type_is_unsafe_ptr;
|
2012-03-08 14:05:16 -08:00
|
|
|
export ty_rptr, mk_rptr;
|
2012-02-07 11:25:04 +01:00
|
|
|
export ty_rec, mk_rec;
|
|
|
|
export ty_enum, mk_enum, type_is_enum;
|
|
|
|
export ty_tup, mk_tup;
|
|
|
|
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;
|
2012-04-30 10:37:58 -07:00
|
|
|
export ty_var, mk_var, type_is_var;
|
2012-06-01 14:56:33 -07:00
|
|
|
export ty_var_integral, mk_var_integral, type_is_var_integral;
|
2012-05-09 06:09:58 -07:00
|
|
|
export ty_self, mk_self, type_has_self;
|
2012-05-15 19:04:33 -07:00
|
|
|
export region, bound_region, encl_region;
|
2012-05-09 06:09:58 -07:00
|
|
|
export get, type_has_params, type_needs_infer, type_has_regions;
|
2012-04-25 17:18:34 -07:00
|
|
|
export type_has_resources, type_id;
|
2012-05-09 06:09:58 -07:00
|
|
|
export tbox_has_flag;
|
2011-06-19 02:34:20 -07:00
|
|
|
export ty_var_id;
|
2012-03-08 16:10:25 +01:00
|
|
|
export ty_to_def_id;
|
2011-06-19 02:34:20 -07:00
|
|
|
export ty_fn_args;
|
2011-07-19 17:52:34 -07:00
|
|
|
export type_constr;
|
2012-05-29 16:22:22 -07:00
|
|
|
export kind, kind_implicitly_copyable, kind_sendable, kind_copyable;
|
|
|
|
export kind_noncopyable, kind_const;
|
2012-05-23 18:57:56 -07:00
|
|
|
export kind_can_be_copied, kind_can_be_sent, kind_can_be_implicitly_copied;
|
|
|
|
export proto_kind, kind_lteq, type_kind;
|
2012-05-24 14:24:09 -07:00
|
|
|
export operators;
|
2012-04-14 18:45:03 -07:00
|
|
|
export type_err, terr_vstore_kind;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_err_to_str;
|
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;
|
2012-04-10 18:34:21 -07:00
|
|
|
export type_is_slice;
|
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;
|
2012-04-26 16:02:01 -07:00
|
|
|
export type_autoderef, deref, deref_sty;
|
2011-06-19 02:34:20 -07:00
|
|
|
export type_param;
|
2012-03-23 17:52:20 -07:00
|
|
|
export type_needs_unwind_cleanup;
|
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 variant_info;
|
2012-03-12 16:31:22 +01:00
|
|
|
export walk_ty, maybe_walk_ty;
|
2012-03-10 20:35:41 -08:00
|
|
|
export occurs_check;
|
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;
|
2012-03-13 14:12:44 -07:00
|
|
|
export ast_ty_to_ty_cache_entry;
|
2012-03-23 11:37:10 -07:00
|
|
|
export atttce_unresolved, atttce_resolved;
|
2012-03-22 20:06:01 -07:00
|
|
|
export mach_sty;
|
2012-04-20 15:46:57 -07:00
|
|
|
export ty_sort_str;
|
2012-04-23 16:01:03 -07:00
|
|
|
export normalize_ty;
|
2012-05-24 23:44:58 -07:00
|
|
|
export to_str;
|
2012-05-31 14:02:39 -07:00
|
|
|
export borrow, serialize_borrow, deserialize_borrow;
|
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,
|
2012-03-28 18:50:33 -07:00
|
|
|
purity: ast::purity,
|
2012-05-08 16:06:24 +02:00
|
|
|
vis: ast::visibility};
|
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
|
|
|
|
2012-04-09 17:32:49 -07:00
|
|
|
enum vstore {
|
|
|
|
vstore_fixed(uint),
|
|
|
|
vstore_uniq,
|
|
|
|
vstore_box,
|
|
|
|
vstore_slice(region)
|
|
|
|
}
|
|
|
|
|
2012-03-19 10:19:00 -07:00
|
|
|
type field_ty = {
|
2012-03-06 08:02:13 -08:00
|
|
|
ident: ident,
|
2012-03-19 10:19:00 -07:00
|
|
|
id: def_id,
|
2012-05-08 16:06:24 +02:00
|
|
|
vis: ast::visibility,
|
2012-03-27 22:08:48 -07:00
|
|
|
mutability: ast::class_mutability
|
2012-03-06 08:02:13 -08: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>};
|
|
|
|
|
2012-03-13 14:12:44 -07:00
|
|
|
enum ast_ty_to_ty_cache_entry {
|
|
|
|
atttce_unresolved, /* not resolved yet */
|
2012-03-23 11:37:10 -07:00
|
|
|
atttce_resolved(t) /* resolved to a type, irrespective of region */
|
2012-03-13 14:12:44 -07:00
|
|
|
}
|
|
|
|
|
2012-05-31 14:02:39 -07:00
|
|
|
#[auto_serialize]
|
|
|
|
type borrow = {
|
|
|
|
scope_id: ast::node_id,
|
|
|
|
mutbl: ast::mutability
|
|
|
|
};
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
type ctxt =
|
2012-05-14 20:41:33 -07:00
|
|
|
@{diag: syntax::diagnostic::span_handler,
|
|
|
|
interner: hashmap<intern_key, t_box>,
|
2012-03-26 18:35:18 -07:00
|
|
|
mut next_id: uint,
|
2012-06-04 18:34:11 -07:00
|
|
|
vecs_implicitly_copyable: bool,
|
2012-05-22 15:05:44 -07:00
|
|
|
cstore: metadata::cstore::cstore,
|
2011-07-27 14:19:39 +02:00
|
|
|
sess: session::session,
|
|
|
|
def_map: resolve::def_map,
|
2012-05-15 21:19:35 -07:00
|
|
|
region_map: middle::region::region_map,
|
2012-03-29 19:25:50 -07:00
|
|
|
|
|
|
|
// Stores the types for various nodes in the AST. Note that this table
|
|
|
|
// is not guaranteed to be populated until after typeck. See
|
|
|
|
// typeck::fn_ctxt for details.
|
2011-07-27 14:19:39 +02:00
|
|
|
node_types: node_type_table,
|
2012-03-29 19:25:50 -07:00
|
|
|
|
|
|
|
// Stores the type parameters which were substituted to obtain the type
|
|
|
|
// of this node. This only applies to nodes that refer to entities
|
|
|
|
// parameterized by type parameters, such as generic fns, types, or
|
|
|
|
// other items.
|
2012-01-30 17:28:30 +01:00
|
|
|
node_type_substs: hashmap<node_id, [t]>,
|
2012-03-29 19:25:50 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
items: ast_map::map,
|
2012-05-16 18:24:00 -07:00
|
|
|
intrinsic_ifaces: hashmap<str, (ast::def_id, t)>,
|
2011-07-27 14:19:39 +02:00
|
|
|
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>,
|
2012-03-23 17:52:20 -07:00
|
|
|
needs_unwind_cleanup_cache: hashmap<t, bool>,
|
2011-12-28 18:11:33 +01:00
|
|
|
kind_cache: hashmap<t, kind>,
|
2012-03-13 14:12:44 -07:00
|
|
|
ast_ty_to_ty_cache: hashmap<@ast::ty, ast_ty_to_ty_cache_entry>,
|
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>,
|
2012-03-23 17:28:24 -07:00
|
|
|
inferred_modes: hashmap<ast::node_id, ast::mode>,
|
2012-04-26 16:02:01 -07:00
|
|
|
// maps the id of borrowed expr to scope of borrowed ptr
|
2012-05-31 14:02:39 -07:00
|
|
|
borrowings: hashmap<ast::node_id, borrow>,
|
2012-04-24 18:23:54 -07:00
|
|
|
normalized_cache: hashmap<t, t>};
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2012-05-09 06:09:58 -07:00
|
|
|
enum tbox_flag {
|
|
|
|
has_params = 1,
|
|
|
|
has_self = 2,
|
|
|
|
needs_infer = 4,
|
|
|
|
has_regions = 8,
|
|
|
|
has_resources = 16,
|
|
|
|
|
|
|
|
// a meta-flag: subst may be required if the type has parameters, a self
|
|
|
|
// type, or references bound regions
|
|
|
|
needs_subst = 1 | 2 | 8
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
type t_box = @{struct: sty,
|
|
|
|
id: uint,
|
2012-05-09 06:09:58 -07:00
|
|
|
flags: uint,
|
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;
|
2012-03-20 15:12:30 -07:00
|
|
|
unsafe::forget(t2);
|
2012-02-03 15:15:28 +01:00
|
|
|
t3
|
|
|
|
}
|
|
|
|
|
2012-05-09 06:09:58 -07:00
|
|
|
fn tbox_has_flag(tb: t_box, flag: tbox_flag) -> bool {
|
|
|
|
(tb.flags & (flag as uint)) != 0u
|
|
|
|
}
|
|
|
|
fn type_has_params(t: t) -> bool { tbox_has_flag(get(t), has_params) }
|
|
|
|
fn type_has_self(t: t) -> bool { tbox_has_flag(get(t), has_self) }
|
|
|
|
fn type_needs_infer(t: t) -> bool { tbox_has_flag(get(t), needs_infer) }
|
|
|
|
fn type_has_regions(t: t) -> bool { tbox_has_flag(get(t), has_regions) }
|
|
|
|
fn type_has_resources(t: t) -> bool { tbox_has_flag(get(t), has_resources) }
|
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
|
|
|
}
|
|
|
|
|
2012-05-24 23:44:58 -07:00
|
|
|
type fn_ty = {purity: ast::purity,
|
|
|
|
proto: ast::proto,
|
2011-12-23 16:09:52 +01:00
|
|
|
inputs: [arg],
|
|
|
|
output: t,
|
|
|
|
ret_style: ret_style,
|
|
|
|
constraints: [@constr]};
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
// See discussion at head of region.rs
|
2012-03-08 14:05:16 -08:00
|
|
|
enum region {
|
2012-04-01 14:28:30 -07:00
|
|
|
re_bound(bound_region),
|
|
|
|
re_free(node_id, bound_region),
|
|
|
|
re_scope(node_id),
|
|
|
|
re_var(region_vid),
|
2012-04-15 22:06:19 -07:00
|
|
|
re_static // effectively `top` in the region lattice
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
enum bound_region {
|
2012-04-15 22:06:19 -07:00
|
|
|
br_self, // The self region for classes, impls
|
|
|
|
br_anon, // The anonymous region parameter for a given function.
|
|
|
|
br_named(str) // A named region parameter.
|
2012-03-08 14:05:16 -08:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
type opt_region = option<region>;
|
|
|
|
|
|
|
|
// The type substs represents the kinds of things that can be substituted into
|
|
|
|
// a type. There may be at most one region parameter (self_r), along with
|
|
|
|
// some number of type parameters (tps).
|
|
|
|
//
|
|
|
|
// The region parameter is present on nominative types (enums, resources,
|
|
|
|
// classes) that are declared as having a region parameter. If the type is
|
|
|
|
// declared as `enum foo&`, then self_r should always be non-none. If the
|
|
|
|
// type is declared as `enum foo`, then self_r will always be none. In the
|
|
|
|
// latter case, typeck::ast_ty_to_ty() will reject any references to `&T` or
|
|
|
|
// `&self.T` within the type and report an error.
|
|
|
|
type substs = {
|
|
|
|
self_r: opt_region,
|
2012-05-09 06:09:58 -07:00
|
|
|
self_ty: option<ty::t>,
|
2012-04-18 21:26:25 -07:00
|
|
|
tps: [t]
|
|
|
|
};
|
|
|
|
|
2010-12-21 12:13:51 -08:00
|
|
|
// NB: If you change this, you'll probably want to change the corresponding
|
2012-06-08 17:13:56 -07:00
|
|
|
// AST structure in libsyntax/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-04-09 17:32:49 -07:00
|
|
|
ty_estr(vstore),
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(def_id, substs),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_box(mt),
|
|
|
|
ty_uniq(mt),
|
|
|
|
ty_vec(mt),
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(mt, vstore),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_ptr(mt),
|
2012-03-08 14:05:16 -08:00
|
|
|
ty_rptr(region, mt),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_rec([field]),
|
|
|
|
ty_fn(fn_ty),
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_iface(def_id, substs),
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_class(def_id, substs),
|
|
|
|
ty_res(def_id, t, substs),
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_tup([t]),
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
ty_var(tv_vid), // type variable during typechecking
|
|
|
|
ty_var_integral(tvi_vid), // type variable during typechecking, for
|
|
|
|
// integral types only
|
2012-01-30 11:52:34 +01:00
|
|
|
ty_param(uint, def_id), // type parameter
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_self, // special, implicit `self` type parameter
|
2012-06-07 17:06:07 -07:00
|
|
|
ty_constr(t, [@type_constr]),
|
2012-01-19 17:56:05 -08:00
|
|
|
|
2012-06-07 17:06:07 -07:00
|
|
|
// "Fake" types, used for trans purposes
|
2012-01-19 17:56:05 -08:00
|
|
|
ty_type, // type_desc*
|
2012-03-08 13:01:08 -08:00
|
|
|
ty_opaque_box, // used by monomorphizer to represent any @ box
|
2012-01-19 17:56:05 -08:00
|
|
|
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
|
|
|
|
2012-04-14 18:45:03 -07:00
|
|
|
enum terr_vstore_kind {
|
|
|
|
terr_vec, terr_str
|
|
|
|
}
|
|
|
|
|
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),
|
2012-05-24 23:44:58 -07:00
|
|
|
terr_purity_mismatch(purity, purity),
|
2012-03-22 20:06:01 -07:00
|
|
|
terr_mutability,
|
|
|
|
terr_proto_mismatch(ast::proto, ast::proto),
|
2012-01-19 17:56:05 -08:00
|
|
|
terr_box_mutability,
|
2012-02-21 17:02:02 +01:00
|
|
|
terr_ptr_mutability,
|
2012-03-08 15:54:36 -08:00
|
|
|
terr_ref_mutability,
|
2012-01-19 17:56:05 -08:00
|
|
|
terr_vec_mutability,
|
|
|
|
terr_tuple_size(uint, uint),
|
2012-03-13 20:46:16 -04:00
|
|
|
terr_ty_param_size(uint, uint),
|
2012-01-19 17:56:05 -08:00
|
|
|
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),
|
2012-04-06 06:58:41 -07:00
|
|
|
terr_regions_differ(region, region),
|
2012-04-14 18:45:03 -07:00
|
|
|
terr_vstores_differ(terr_vstore_kind, vstore, vstore),
|
2012-04-06 06:58:41 -07:00
|
|
|
terr_in_field(@type_err, str),
|
2012-05-09 06:09:58 -07:00
|
|
|
terr_sorts(t, t),
|
|
|
|
terr_self_substs
|
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,
|
2012-05-24 10:23:46 -07:00
|
|
|
bound_const,
|
2012-01-19 17:56:05 -08:00
|
|
|
bound_iface(t),
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
enum tv_vid = uint;
|
|
|
|
enum tvi_vid = uint;
|
2012-04-01 14:28:30 -07:00
|
|
|
enum region_vid = uint;
|
|
|
|
|
|
|
|
iface vid {
|
|
|
|
fn to_uint() -> uint;
|
|
|
|
fn to_str() -> str;
|
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
impl of vid for tv_vid {
|
2012-04-01 14:28:30 -07:00
|
|
|
fn to_uint() -> uint { *self }
|
|
|
|
fn to_str() -> str { #fmt["<V%u>", self.to_uint()] }
|
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
impl of vid for tvi_vid {
|
|
|
|
fn to_uint() -> uint { *self }
|
|
|
|
fn to_str() -> str { #fmt["<V (integral) %u>", self.to_uint()] }
|
|
|
|
}
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
impl of vid for region_vid {
|
|
|
|
fn to_uint() -> uint { *self }
|
|
|
|
fn to_str() -> str { #fmt["<R%u>", self.to_uint()] }
|
|
|
|
}
|
|
|
|
|
2012-05-24 23:44:58 -07:00
|
|
|
impl of to_str::to_str for purity {
|
|
|
|
fn to_str() -> str {
|
|
|
|
purity_to_str(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 12:00:40 +01:00
|
|
|
fn param_bounds_to_kind(bounds: param_bounds) -> kind {
|
2012-05-22 12:46:13 -07:00
|
|
|
let mut kind = kind_noncopyable();
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*bounds) {|bound|
|
2011-12-28 17:50:12 +01:00
|
|
|
alt bound {
|
2012-01-18 22:37:22 -08:00
|
|
|
bound_copy {
|
2012-05-29 16:22:22 -07:00
|
|
|
kind = raise_kind(kind, kind_implicitly_copyable());
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
2012-05-23 10:15:06 -07:00
|
|
|
bound_send { kind = raise_kind(kind, kind_send_only()); }
|
2012-05-24 14:24:09 -07:00
|
|
|
bound_const { kind = raise_kind(kind, kind_const()); }
|
|
|
|
bound_iface(_) {}
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
kind
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
type ty_param_bounds_and_ty = {bounds: @[param_bounds],
|
|
|
|
rp: ast::region_param,
|
|
|
|
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
|
|
|
}
|
2012-03-14 12:07:23 -07:00
|
|
|
ret map::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> {
|
2012-03-14 12:07:23 -07:00
|
|
|
map::hashmap({|&&t: t| type_id(t)},
|
2012-02-03 15:15:28 +01:00
|
|
|
{|&&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,
|
2012-03-09 16:39:54 -08:00
|
|
|
freevars: freevars::freevar_map,
|
2012-05-15 21:19:35 -07:00
|
|
|
region_map: middle::region::region_map) -> ctxt {
|
2012-03-14 12:07:23 -07:00
|
|
|
let interner = map::hashmap({|&&k: intern_key|
|
2012-02-10 10:28:35 -08:00
|
|
|
hash_type_structure(k.struct) +
|
2012-05-23 00:45:18 -07:00
|
|
|
option::map_default(k.o_def_id, 0u, ast_util::hash_def)
|
2012-02-03 15:15:28 +01:00
|
|
|
}, {|&&a, &&b| a == b});
|
2012-06-04 18:34:11 -07:00
|
|
|
let vecs_implicitly_copyable =
|
|
|
|
get_warning_level(s.warning_settings.default_settings,
|
|
|
|
vecs_not_implicitly_copyable) == ignore;
|
2012-05-14 20:41:33 -07:00
|
|
|
@{diag: s.diagnostic(),
|
|
|
|
interner: interner,
|
2012-03-26 18:35:18 -07:00
|
|
|
mut next_id: 0u,
|
2012-06-04 18:34:11 -07:00
|
|
|
vecs_implicitly_copyable: vecs_implicitly_copyable,
|
2012-05-22 15:05:44 -07:00
|
|
|
cstore: s.cstore,
|
2012-02-03 15:15:28 +01:00
|
|
|
sess: s,
|
|
|
|
def_map: dm,
|
2012-03-09 16:39:54 -08:00
|
|
|
region_map: region_map,
|
2012-02-03 15:15:28 +01:00
|
|
|
node_types: @smallintmap::mk(),
|
2012-03-14 12:07:23 -07:00
|
|
|
node_type_substs: map::int_hash(),
|
2012-02-03 15:15:28 +01:00
|
|
|
items: amap,
|
2012-05-16 18:24:00 -07:00
|
|
|
intrinsic_ifaces: map::str_hash(),
|
2012-02-03 15:15:28 +01:00
|
|
|
freevars: freevars,
|
2012-05-23 00:38:39 -07:00
|
|
|
tcache: ast_util::new_def_hash(),
|
2012-02-03 15:15:28 +01:00
|
|
|
rcache: mk_rcache(),
|
|
|
|
short_names_cache: new_ty_hash(),
|
|
|
|
needs_drop_cache: new_ty_hash(),
|
2012-03-23 17:52:20 -07:00
|
|
|
needs_unwind_cleanup_cache: new_ty_hash(),
|
2012-02-03 15:15:28 +01:00
|
|
|
kind_cache: new_ty_hash(),
|
2012-03-14 12:07:23 -07:00
|
|
|
ast_ty_to_ty_cache: map::hashmap(
|
2012-03-12 15:52:30 -07:00
|
|
|
ast_util::hash_ty, ast_util::eq_ty),
|
2012-02-03 15:15:28 +01:00
|
|
|
enum_var_cache: new_def_hash(),
|
|
|
|
iface_method_cache: new_def_hash(),
|
2012-03-14 12:07:23 -07:00
|
|
|
ty_param_bounds: map::int_hash(),
|
2012-03-23 17:28:24 -07:00
|
|
|
inferred_modes: map::int_hash(),
|
2012-04-24 18:23:54 -07:00
|
|
|
borrowings: map::int_hash(),
|
|
|
|
normalized_cache: new_ty_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); } }
|
|
|
|
_ {}
|
|
|
|
}
|
2012-05-09 06:09:58 -07:00
|
|
|
let mut flags = 0u;
|
|
|
|
fn rflags(r: region) -> uint {
|
|
|
|
(has_regions as uint) | {
|
|
|
|
alt r {
|
|
|
|
ty::re_var(_) {needs_infer as uint}
|
|
|
|
_ {0u}
|
|
|
|
}
|
2012-04-21 10:04:58 -07:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 06:09:58 -07:00
|
|
|
fn sflags(substs: substs) -> uint {
|
|
|
|
let mut f = 0u;
|
|
|
|
for substs.tps.each {|tt| f |= get(tt).flags; }
|
|
|
|
substs.self_r.iter { |r| f |= rflags(r) }
|
|
|
|
ret f;
|
2012-04-21 10:04:58 -07:00
|
|
|
}
|
|
|
|
alt st {
|
|
|
|
ty_estr(vstore_slice(r)) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= rflags(r);
|
2012-04-09 17:32:49 -07:00
|
|
|
}
|
2012-04-21 10:04:58 -07:00
|
|
|
ty_evec(mt, vstore_slice(r)) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= rflags(r);
|
|
|
|
flags |= get(mt.ty).flags;
|
2012-04-09 17:32:49 -07:00
|
|
|
}
|
2012-01-18 22:37:22 -08:00
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_str | ty_estr(_) | ty_type | ty_opaque_closure_ptr(_) |
|
2012-02-07 11:25:04 +01:00
|
|
|
ty_opaque_box {}
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_param(_, _) { flags |= has_params as uint; }
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_var(_) | ty_var_integral(_) { flags |= needs_infer as uint; }
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_self { flags |= has_self as uint; }
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_enum(_, substs) | ty_class(_, substs) | ty_iface(_, substs) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= sflags(substs);
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_box(m) | ty_uniq(m) | ty_vec(m) | ty_evec(m, _) | ty_ptr(m) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= get(m.ty).flags;
|
2012-03-12 13:24:37 -07:00
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
ty_rptr(r, m) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= rflags(r);
|
|
|
|
flags |= get(m.ty).flags;
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
|
|
|
ty_rec(flds) {
|
2012-05-09 06:09:58 -07:00
|
|
|
for flds.each {|f| flags |= get(f.mt.ty).flags; }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-08-15 12:08:05 +02:00
|
|
|
ty_tup(ts) {
|
2012-05-09 06:09:58 -07:00
|
|
|
for ts.each {|tt| flags |= get(tt).flags; }
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2011-12-23 16:09:52 +01:00
|
|
|
ty_fn(f) {
|
2012-05-09 06:09:58 -07:00
|
|
|
for f.inputs.each {|a| flags |= get(a.ty).flags; }
|
|
|
|
flags |= get(f.output).flags;
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(_, tt, substs) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= (has_resources as uint);
|
|
|
|
flags |= get(tt).flags;
|
|
|
|
flags |= sflags(substs);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_constr(tt, _) {
|
2012-05-09 06:09:58 -07:00
|
|
|
flags |= get(tt).flags;
|
2011-12-19 13:52:58 +01:00
|
|
|
}
|
2011-04-25 16:17:14 -07:00
|
|
|
}
|
2012-05-09 06:09:58 -07:00
|
|
|
let t = @{struct: st, id: cx.next_id, flags: flags, 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-04-26 16:02:01 -07:00
|
|
|
fn mk_u8(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u8)) }
|
|
|
|
|
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-04-09 17:32:49 -07:00
|
|
|
fn mk_estr(cx: ctxt, t: vstore) -> t {
|
|
|
|
mk_t(cx, ty_estr(t))
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
fn mk_enum(cx: ctxt, did: ast::def_id, substs: substs) -> t {
|
|
|
|
mk_t(cx, ty_enum(did, substs))
|
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-03-08 14:05:16 -08:00
|
|
|
fn mk_rptr(cx: ctxt, r: region, tm: mt) -> t { mk_t(cx, ty_rptr(r, tm)) }
|
|
|
|
|
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-03-21 15:42:20 +01:00
|
|
|
fn mk_imm_ptr(cx: ctxt, ty: t) -> t {
|
|
|
|
mk_ptr(cx, {ty: ty, mutbl: ast::m_imm})
|
|
|
|
}
|
|
|
|
|
2012-03-16 12:57:07 +01:00
|
|
|
fn mk_nil_ptr(cx: ctxt) -> t {
|
|
|
|
mk_ptr(cx, {ty: mk_nil(cx), mutbl: ast::m_imm})
|
|
|
|
}
|
|
|
|
|
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-04-09 17:32:49 -07:00
|
|
|
fn mk_evec(cx: ctxt, tm: mt, t: vstore) -> t {
|
|
|
|
mk_t(cx, ty_evec(tm, t))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-04-24 15:52:52 -07:00
|
|
|
fn mk_iface(cx: ctxt, did: ast::def_id, substs: substs) -> t {
|
|
|
|
mk_t(cx, ty_iface(did, substs))
|
2011-12-20 16:33:55 +01:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
fn mk_class(cx: ctxt, class_id: ast::def_id, substs: substs) -> t {
|
|
|
|
mk_t(cx, ty_class(class_id, substs))
|
2012-02-09 14:16:12 -08:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
fn mk_res(cx: ctxt, did: ast::def_id,
|
|
|
|
inner: t, substs: substs) -> t {
|
|
|
|
mk_t(cx, ty_res(did, inner, substs))
|
2011-06-24 18:10:40 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
fn mk_var(cx: ctxt, v: tv_vid) -> t { mk_t(cx, ty_var(v)) }
|
2011-04-20 18:52:04 -07:00
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
fn mk_var_integral(cx: ctxt, v: tvi_vid) -> t {
|
|
|
|
mk_t(cx, ty_var_integral(v))
|
|
|
|
}
|
2012-06-01 14:56:33 -07:00
|
|
|
|
2012-05-09 06:09:58 -07:00
|
|
|
fn mk_self(cx: ctxt) -> t { mk_t(cx, ty_self) }
|
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-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-05-15 19:04:33 -07:00
|
|
|
// Returns the narrowest lifetime enclosing the evaluation of the expression
|
|
|
|
// with id `id`.
|
|
|
|
fn encl_region(cx: ctxt, id: ast::node_id) -> ty::region {
|
2012-05-15 21:19:35 -07:00
|
|
|
alt cx.region_map.find(id) {
|
2012-05-15 19:04:33 -07:00
|
|
|
some(encl_scope) {ty::re_scope(encl_scope)}
|
|
|
|
none {ty::re_static}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 16:31:22 +01:00
|
|
|
fn walk_ty(ty: t, f: fn(t)) {
|
|
|
|
maybe_walk_ty(ty, {|t| f(t); true});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_walk_ty(ty: t, f: fn(t) -> bool) {
|
|
|
|
if !f(ty) { ret; }
|
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-05-09 06:09:58 -07:00
|
|
|
ty_str | ty_estr(_) | ty_type | ty_opaque_box | ty_self |
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_opaque_closure_ptr(_) | ty_var(_) | ty_var_integral(_) |
|
|
|
|
ty_param(_, _) {
|
2012-05-09 06:09:58 -07:00
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_box(tm) | ty_vec(tm) | ty_evec(tm, _) |
|
|
|
|
ty_ptr(tm) | ty_rptr(_, tm) {
|
2012-03-12 16:31:22 +01:00
|
|
|
maybe_walk_ty(tm.ty, f);
|
2012-03-08 14:05:16 -08:00
|
|
|
}
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_enum(_, substs) | ty_class(_, substs) |
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_iface(_, substs) {
|
2012-04-18 21:26:25 -07:00
|
|
|
for substs.tps.each {|subty| maybe_walk_ty(subty, f); }
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(fields) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for fields.each {|fl| maybe_walk_ty(fl.mt.ty, f); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-04-06 20:01:43 +02:00
|
|
|
ty_tup(ts) { for ts.each {|tt| maybe_walk_ty(tt, f); } }
|
2012-01-31 12:38:22 +01:00
|
|
|
ty_fn(ft) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for ft.inputs.each {|a| maybe_walk_ty(a.ty, f); }
|
2012-03-12 16:31:22 +01:00
|
|
|
maybe_walk_ty(ft.output, f);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(_, sub, substs) {
|
2012-03-12 16:31:22 +01:00
|
|
|
maybe_walk_ty(sub, f);
|
2012-04-18 21:26:25 -07:00
|
|
|
for substs.tps.each {|tp| maybe_walk_ty(tp, f); }
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-03-12 16:31:22 +01:00
|
|
|
ty_constr(sub, _) { maybe_walk_ty(sub, f); }
|
|
|
|
ty_uniq(tm) { maybe_walk_ty(tm.ty, f); }
|
2011-04-15 12:23:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
fn fold_sty_to_ty(tcx: ty::ctxt, sty: sty, foldop: fn(t) -> t) -> t {
|
|
|
|
mk_t(tcx, fold_sty(sty, foldop))
|
2011-06-09 11:20:47 -07:00
|
|
|
}
|
2011-04-15 12:23:00 -07:00
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
fn fold_sty(sty: sty, fldop: fn(t) -> t) -> sty {
|
2012-04-18 21:26:25 -07:00
|
|
|
fn fold_substs(substs: substs, fldop: fn(t) -> t) -> substs {
|
|
|
|
{self_r: substs.self_r,
|
2012-05-09 06:09:58 -07:00
|
|
|
self_ty: substs.self_ty.map { |t| fldop(t) },
|
2012-04-18 21:26:25 -07:00
|
|
|
tps: substs.tps.map { |t| fldop(t) }}
|
|
|
|
}
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
alt sty {
|
|
|
|
ty_box(tm) {
|
|
|
|
ty_box({ty: fldop(tm.ty), mutbl: tm.mutbl})
|
|
|
|
}
|
|
|
|
ty_uniq(tm) {
|
|
|
|
ty_uniq({ty: fldop(tm.ty), mutbl: tm.mutbl})
|
|
|
|
}
|
|
|
|
ty_ptr(tm) {
|
|
|
|
ty_ptr({ty: fldop(tm.ty), mutbl: tm.mutbl})
|
|
|
|
}
|
|
|
|
ty_vec(tm) {
|
|
|
|
ty_vec({ty: fldop(tm.ty), mutbl: tm.mutbl})
|
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(tm, vst) {
|
|
|
|
ty_evec({ty: fldop(tm.ty), mutbl: tm.mutbl}, vst)
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(tid, substs) {
|
|
|
|
ty_enum(tid, fold_substs(substs, fldop))
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_iface(did, substs) {
|
|
|
|
ty_iface(did, fold_substs(substs, fldop))
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
ty_rec(fields) {
|
|
|
|
let new_fields = vec::map(fields) {|fl|
|
|
|
|
let new_ty = fldop(fl.mt.ty);
|
|
|
|
let new_mt = {ty: new_ty, mutbl: fl.mt.mutbl};
|
|
|
|
{ident: fl.ident, mt: new_mt}
|
|
|
|
};
|
|
|
|
ty_rec(new_fields)
|
|
|
|
}
|
|
|
|
ty_tup(ts) {
|
|
|
|
let new_ts = vec::map(ts) {|tt| fldop(tt) };
|
|
|
|
ty_tup(new_ts)
|
|
|
|
}
|
|
|
|
ty_fn(f) {
|
|
|
|
let new_args = vec::map(f.inputs) {|a|
|
|
|
|
let new_ty = fldop(a.ty);
|
|
|
|
{mode: a.mode, ty: new_ty}
|
|
|
|
};
|
|
|
|
let new_output = fldop(f.output);
|
|
|
|
ty_fn({inputs: new_args, output: new_output with f})
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(did, subty, substs) {
|
|
|
|
ty_res(did, fldop(subty),
|
|
|
|
fold_substs(substs, fldop))
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
ty_rptr(r, tm) {
|
|
|
|
ty_rptr(r, {ty: fldop(tm.ty), mutbl: tm.mutbl})
|
|
|
|
}
|
|
|
|
ty_constr(subty, cs) {
|
|
|
|
ty_constr(fldop(subty), cs)
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_class(did, substs) {
|
|
|
|
ty_class(did, fold_substs(substs, fldop))
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_str | ty_estr(_) | ty_type | ty_opaque_closure_ptr(_) |
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_opaque_box | ty_var(_) | ty_var_integral(_) | ty_param(*) | ty_self {
|
2012-04-01 14:28:30 -07:00
|
|
|
sty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-23 11:37:10 -07:00
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
// Folds types from the bottom up.
|
|
|
|
fn fold_ty(cx: ctxt, t0: t, fldop: fn(t) -> t) -> t {
|
|
|
|
let sty = fold_sty(get(t0).struct) {|t| fold_ty(cx, t, fldop) };
|
|
|
|
fldop(mk_t(cx, sty))
|
|
|
|
}
|
2012-02-02 16:50:17 -08:00
|
|
|
|
2012-04-30 10:37:58 -07:00
|
|
|
fn walk_regions_and_ty(
|
|
|
|
cx: ctxt,
|
|
|
|
ty: t,
|
|
|
|
walkr: fn(r: region),
|
|
|
|
walkt: fn(t: t) -> bool) {
|
|
|
|
|
|
|
|
if (walkt(ty)) {
|
|
|
|
fold_regions_and_ty(
|
|
|
|
cx, ty,
|
|
|
|
{ |r| walkr(r); r },
|
|
|
|
{ |t| walkt(t); walk_regions_and_ty(cx, t, walkr, walkt); t },
|
|
|
|
{ |t| walkt(t); walk_regions_and_ty(cx, t, walkr, walkt); t });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
fn fold_regions_and_ty(
|
|
|
|
cx: ctxt,
|
|
|
|
ty: t,
|
|
|
|
fldr: fn(r: region) -> region,
|
|
|
|
fldfnt: fn(t: t) -> t,
|
|
|
|
fldt: fn(t: t) -> t) -> t {
|
|
|
|
|
|
|
|
fn fold_substs(
|
|
|
|
substs: substs,
|
|
|
|
fldr: fn(r: region) -> region,
|
|
|
|
fldt: fn(t: t) -> t) -> substs {
|
|
|
|
|
|
|
|
{self_r: substs.self_r.map { |r| fldr(r) },
|
2012-05-09 06:09:58 -07:00
|
|
|
self_ty: substs.self_ty.map { |t| fldt(t) },
|
2012-04-18 21:26:25 -07:00
|
|
|
tps: substs.tps.map { |t| fldt(t) }}
|
|
|
|
}
|
|
|
|
|
|
|
|
let tb = ty::get(ty);
|
|
|
|
alt tb.struct {
|
|
|
|
ty::ty_rptr(r, mt) {
|
|
|
|
let m_r = fldr(r);
|
|
|
|
let m_t = fldt(mt.ty);
|
|
|
|
ty::mk_rptr(cx, m_r, {ty: m_t, mutbl: mt.mutbl})
|
|
|
|
}
|
|
|
|
ty_estr(vstore_slice(r)) {
|
|
|
|
let m_r = fldr(r);
|
|
|
|
ty::mk_estr(cx, vstore_slice(m_r))
|
|
|
|
}
|
|
|
|
ty_evec(mt, vstore_slice(r)) {
|
|
|
|
let m_r = fldr(r);
|
|
|
|
let m_t = fldt(mt.ty);
|
|
|
|
ty::mk_evec(cx, {ty: m_t, mutbl: mt.mutbl}, vstore_slice(m_r))
|
|
|
|
}
|
|
|
|
ty_enum(def_id, substs) {
|
|
|
|
ty::mk_enum(cx, def_id, fold_substs(substs, fldr, fldt))
|
|
|
|
}
|
|
|
|
ty_class(def_id, substs) {
|
|
|
|
ty::mk_class(cx, def_id, fold_substs(substs, fldr, fldt))
|
|
|
|
}
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_iface(def_id, substs) {
|
|
|
|
ty::mk_iface(cx, def_id, fold_substs(substs, fldr, fldt))
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(def_id, t, substs) {
|
|
|
|
ty::mk_res(cx, def_id, fldt(t),
|
|
|
|
fold_substs(substs, fldr, fldt))
|
|
|
|
}
|
|
|
|
sty @ ty_fn(_) {
|
|
|
|
fold_sty_to_ty(cx, sty) {|t|
|
|
|
|
fldfnt(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sty {
|
|
|
|
fold_sty_to_ty(cx, sty) {|t|
|
|
|
|
fldt(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-15 22:06:19 -07:00
|
|
|
// n.b. this function is intended to eventually replace fold_region() below,
|
|
|
|
// that is why its name is so similar.
|
|
|
|
fn fold_regions(
|
|
|
|
cx: ctxt,
|
|
|
|
ty: t,
|
|
|
|
fldr: fn(r: region, in_fn: bool) -> region) -> t {
|
|
|
|
|
|
|
|
fn do_fold(cx: ctxt, ty: t, in_fn: bool,
|
|
|
|
fldr: fn(region, bool) -> region) -> t {
|
2012-04-18 21:26:25 -07:00
|
|
|
if !type_has_regions(ty) { ret ty; }
|
|
|
|
fold_regions_and_ty(
|
|
|
|
cx, ty,
|
|
|
|
{ |r| fldr(r, in_fn) },
|
|
|
|
{ |t| do_fold(cx, t, true, fldr) },
|
|
|
|
{ |t| do_fold(cx, t, in_fn, fldr) })
|
2012-04-15 22:06:19 -07:00
|
|
|
}
|
|
|
|
do_fold(cx, ty, false, fldr)
|
|
|
|
}
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
fn fold_region(cx: ctxt, t0: t, fldop: fn(region, bool) -> region) -> t {
|
|
|
|
fn do_fold(cx: ctxt, t0: t, under_r: bool,
|
|
|
|
fldop: fn(region, bool) -> region) -> t {
|
|
|
|
let tb = get(t0);
|
2012-05-09 06:09:58 -07:00
|
|
|
if !tbox_has_flag(tb, has_regions) { ret t0; }
|
2012-03-23 11:37:10 -07:00
|
|
|
alt tb.struct {
|
2012-04-01 14:28:30 -07:00
|
|
|
ty_rptr(r, {ty: t1, mutbl: m}) {
|
|
|
|
let m_r = fldop(r, under_r);
|
|
|
|
let m_t1 = do_fold(cx, t1, true, fldop);
|
|
|
|
ty::mk_rptr(cx, m_r, {ty: m_t1, mutbl: m})
|
2012-03-23 11:37:10 -07:00
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_estr(vstore_slice(r)) {
|
|
|
|
let m_r = fldop(r, under_r);
|
|
|
|
ty::mk_estr(cx, vstore_slice(m_r))
|
|
|
|
}
|
|
|
|
ty_evec({ty: t1, mutbl: m}, vstore_slice(r)) {
|
|
|
|
let m_r = fldop(r, under_r);
|
|
|
|
let m_t1 = do_fold(cx, t1, true, fldop);
|
|
|
|
ty::mk_evec(cx, {ty: m_t1, mutbl: m}, vstore_slice(m_r))
|
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
ty_fn(_) {
|
|
|
|
// do not recurse into functions, which introduce fresh bindings
|
|
|
|
t0
|
2012-03-23 11:37:10 -07:00
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
sty {
|
|
|
|
fold_sty_to_ty(cx, sty) {|t|
|
|
|
|
do_fold(cx, t, under_r, fldop)
|
2012-03-26 19:00:11 -07:00
|
|
|
}
|
2012-03-23 11:37:10 -07:00
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
2012-02-23 17:44:03 +01:00
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
do_fold(cx, t0, false, fldop)
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
// Substitute *only* type parameters. Used in trans where regions are erased.
|
|
|
|
fn subst_tps(cx: ctxt, tps: [t], typ: t) -> t {
|
|
|
|
if tps.len() == 0u { ret typ; }
|
|
|
|
let tb = ty::get(typ);
|
2012-05-09 06:09:58 -07:00
|
|
|
if !tbox_has_flag(tb, has_params) { ret typ; }
|
2012-04-01 14:28:30 -07:00
|
|
|
alt tb.struct {
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_param(idx, _) { tps[idx] }
|
|
|
|
sty { fold_sty_to_ty(cx, sty) {|t| subst_tps(cx, tps, t) } }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn substs_is_noop(substs: substs) -> bool {
|
2012-05-09 06:09:58 -07:00
|
|
|
substs.tps.len() == 0u &&
|
|
|
|
substs.self_r.is_none() &&
|
|
|
|
substs.self_ty.is_none()
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn substs_to_str(cx: ctxt, substs: substs) -> str {
|
2012-05-09 06:09:58 -07:00
|
|
|
#fmt["substs(self_r=%s, self_ty=%s, tps=%?)",
|
2012-04-18 21:26:25 -07:00
|
|
|
substs.self_r.map_default("none", { |r| region_to_str(cx, r) }),
|
2012-05-09 06:09:58 -07:00
|
|
|
substs.self_ty.map_default("none", { |t| ty_to_str(cx, t) }),
|
2012-04-18 21:26:25 -07:00
|
|
|
substs.tps.map { |t| ty_to_str(cx, t) }]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subst(cx: ctxt,
|
|
|
|
substs: substs,
|
|
|
|
typ: t) -> t {
|
|
|
|
|
|
|
|
#debug["subst(substs=%s, typ=%s)",
|
|
|
|
substs_to_str(cx, substs),
|
|
|
|
ty_to_str(cx, typ)];
|
2012-04-21 10:04:58 -07:00
|
|
|
|
|
|
|
if substs_is_noop(substs) { ret typ; }
|
2012-04-18 21:26:25 -07:00
|
|
|
let r = do_subst(cx, substs, typ);
|
|
|
|
#debug[" r = %s", ty_to_str(cx, r)];
|
|
|
|
ret r;
|
|
|
|
|
|
|
|
fn do_subst(cx: ctxt,
|
|
|
|
substs: substs,
|
|
|
|
typ: t) -> t {
|
|
|
|
let tb = get(typ);
|
2012-05-09 06:09:58 -07:00
|
|
|
if !tbox_has_flag(tb, needs_subst) { ret typ; }
|
2012-04-18 21:26:25 -07:00
|
|
|
alt tb.struct {
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_param(idx, _) {substs.tps[idx]}
|
|
|
|
ty_self {substs.self_ty.get()}
|
2012-04-18 21:26:25 -07:00
|
|
|
_ {
|
|
|
|
fold_regions_and_ty(
|
|
|
|
cx, typ,
|
|
|
|
{ |r|
|
|
|
|
alt r {
|
2012-05-09 06:09:58 -07:00
|
|
|
re_bound(br_self) {substs.self_r.get()}
|
|
|
|
_ {r}
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ |t| do_subst(cx, substs, t) },
|
|
|
|
{ |t| do_subst(cx, substs, t) })
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
}
|
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 }
|
2012-04-30 10:37:58 -07:00
|
|
|
|
|
|
|
fn type_is_var(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
|
|
|
ty_var(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
2010-12-21 12:13:51 -08:00
|
|
|
|
2012-06-01 14:56:33 -07:00
|
|
|
fn type_is_var_integral(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
|
|
|
ty_var_integral(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-18 21:26:25 -07:00
|
|
|
ty_rec(_) | ty_class(_, _) | ty_tup(_) | ty_enum(_, _) | ty_fn(_) |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_iface(_, _) | ty_res(_, _, _) | ty_evec(_, vstore_fixed(_))
|
|
|
|
| ty_estr(vstore_fixed(_)) { 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-04-09 17:32:49 -07:00
|
|
|
ty_str | ty_estr(_) | ty_vec(_) | ty_evec(_, _) { true }
|
|
|
|
_ { false }
|
2011-03-02 16:42:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 17:32:49 -07:00
|
|
|
fn type_is_str(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
|
|
|
ty_str | ty_estr(_) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
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-04-09 17:32:49 -07:00
|
|
|
ty_str | ty_estr(_) { ret mk_mach_uint(cx, ast::ty_u8); }
|
|
|
|
ty_vec(mt) | ty_evec(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
|
|
|
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-04-13 19:07:47 -07:00
|
|
|
pure fn type_is_region_ptr(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
|
|
|
ty_rptr(_, _) { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 18:34:21 -07:00
|
|
|
pure fn type_is_slice(ty: t) -> bool {
|
|
|
|
alt get(ty).struct {
|
|
|
|
ty_evec(_, vstore_slice(_)) | ty_estr(vstore_slice(_)) { true }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_vec(_) | ty_evec(_, _) { true }
|
|
|
|
ty_str | ty_estr(_) { 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; }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_vec(_) | ty_evec(_, vstore_uniq) { true }
|
|
|
|
ty_str | ty_estr(vstore_uniq) { 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-03-15 17:42:33 +01:00
|
|
|
ty_type | ty_ptr(_) | ty_rptr(_, _) { 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) ||
|
2012-04-13 19:07:47 -07:00
|
|
|
type_is_unique(ty) || type_is_region_ptr(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
|
|
|
}
|
|
|
|
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut 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-04-09 17:32:49 -07:00
|
|
|
ty_type | ty_ptr(_) | ty_rptr(_, _) |
|
|
|
|
ty_estr(vstore_fixed(_)) | ty_estr(vstore_slice(_)) |
|
|
|
|
ty_evec(_, vstore_slice(_)) { false }
|
|
|
|
ty_evec(mt, vstore_fixed(_)) { type_needs_drop(cx, mt.ty) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(flds) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for flds.each {|f| if type_needs_drop(cx, f.mt.ty) { accum = true; } }
|
2011-11-22 13:16:23 +01:00
|
|
|
accum
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_class(did, substs) {
|
2012-06-01 14:46:37 -07:00
|
|
|
// Any class with a dtor needs a drop
|
|
|
|
option::is_some(ty_dtor(cx, did)) || {
|
|
|
|
for vec::each(ty::class_items_as_fields(cx, did, substs)) {|f|
|
|
|
|
if type_needs_drop(cx, f.mt.ty) { accum = true; }
|
|
|
|
}
|
|
|
|
accum
|
|
|
|
}
|
2012-03-27 22:08:48 -07:00
|
|
|
}
|
|
|
|
|
2011-08-15 11:40:26 +02:00
|
|
|
ty_tup(elts) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for elts.each {|m| if type_needs_drop(cx, m) { accum = true; } }
|
2011-11-22 13:16:23 +01:00
|
|
|
accum
|
2011-08-15 11:40:26 +02:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-01-25 14:34:31 +01:00
|
|
|
let variants = enum_variants(cx, did);
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*variants) {|variant|
|
|
|
|
for variant.args.each {|aty|
|
2011-07-27 14:19:39 +02:00
|
|
|
// Perform any type parameter substitutions.
|
2012-04-18 21:26:25 -07:00
|
|
|
let arg_ty = subst(cx, substs, 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-03-23 17:52:20 -07:00
|
|
|
// Some things don't need cleanups during unwinding because the
|
|
|
|
// task can free them all at once later. Currently only things
|
|
|
|
// that only contain scalars and shared boxes can avoid unwind
|
|
|
|
// cleanups.
|
|
|
|
fn type_needs_unwind_cleanup(cx: ctxt, ty: t) -> bool {
|
|
|
|
alt cx.needs_unwind_cleanup_cache.find(ty) {
|
|
|
|
some(result) { ret result; }
|
|
|
|
none { }
|
|
|
|
}
|
|
|
|
|
2012-03-26 16:25:29 -07:00
|
|
|
let tycache = new_ty_hash();
|
|
|
|
let needs_unwind_cleanup =
|
|
|
|
type_needs_unwind_cleanup_(cx, ty, tycache, false);
|
|
|
|
cx.needs_unwind_cleanup_cache.insert(ty, needs_unwind_cleanup);
|
|
|
|
ret needs_unwind_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_needs_unwind_cleanup_(cx: ctxt, ty: t,
|
|
|
|
tycache: map::hashmap<t, ()>,
|
|
|
|
encountered_box: bool) -> bool {
|
|
|
|
|
2012-03-23 17:52:20 -07:00
|
|
|
// Prevent infinite recursion
|
2012-03-26 16:25:29 -07:00
|
|
|
alt tycache.find(ty) {
|
|
|
|
some(_) { ret false; }
|
|
|
|
none { tycache.insert(ty, ()); }
|
|
|
|
}
|
2012-03-23 17:52:20 -07:00
|
|
|
|
2012-03-26 16:25:29 -07:00
|
|
|
let mut encountered_box = encountered_box;
|
2012-03-23 17:52:20 -07:00
|
|
|
let mut needs_unwind_cleanup = false;
|
|
|
|
maybe_walk_ty(ty) {|ty|
|
2012-04-24 14:16:50 -07:00
|
|
|
let old_encountered_box = encountered_box;
|
|
|
|
let result = alt get(ty).struct {
|
2012-03-26 16:25:29 -07:00
|
|
|
ty_box(_) | ty_opaque_box {
|
|
|
|
encountered_box = true;
|
|
|
|
true
|
|
|
|
}
|
2012-03-23 17:52:20 -07:00
|
|
|
ty_nil | ty_bot | ty_bool |
|
|
|
|
ty_int(_) | ty_uint(_) | ty_float(_) |
|
2012-03-26 16:25:29 -07:00
|
|
|
ty_rec(_) | ty_tup(_) | ty_ptr(_) {
|
2012-03-23 17:52:20 -07:00
|
|
|
true
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*enum_variants(cx, did)) {|v|
|
|
|
|
for v.args.each {|aty|
|
2012-04-18 21:26:25 -07:00
|
|
|
let t = subst(cx, substs, aty);
|
2012-03-26 16:25:29 -07:00
|
|
|
needs_unwind_cleanup |=
|
|
|
|
type_needs_unwind_cleanup_(cx, t, tycache,
|
|
|
|
encountered_box);
|
2012-03-23 17:52:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
!needs_unwind_cleanup
|
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_uniq(_) | ty_str | ty_vec(_) | ty_res(_, _, _) |
|
|
|
|
ty_estr(vstore_uniq) |
|
|
|
|
ty_estr(vstore_box) |
|
|
|
|
ty_evec(_, vstore_uniq) |
|
|
|
|
ty_evec(_, vstore_box)
|
|
|
|
{
|
2012-03-26 16:25:29 -07:00
|
|
|
// Once we're inside a box, the annihilator will find
|
|
|
|
// it and destroy it.
|
|
|
|
if !encountered_box {
|
|
|
|
needs_unwind_cleanup = true;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2012-03-23 17:52:20 -07:00
|
|
|
_ {
|
|
|
|
needs_unwind_cleanup = true;
|
|
|
|
false
|
|
|
|
}
|
2012-04-24 14:16:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
encountered_box = old_encountered_box;
|
|
|
|
result
|
2012-03-23 17:52:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret needs_unwind_cleanup;
|
|
|
|
}
|
|
|
|
|
2012-05-22 12:46:13 -07:00
|
|
|
enum kind { kind_(u32) }
|
|
|
|
|
2012-06-01 17:06:27 -07:00
|
|
|
// *ALL* implicitly copiable things must be copiable
|
2012-05-23 18:57:56 -07:00
|
|
|
const KIND_MASK_COPY : u32 = 0b00000000000000000000000000000001u32;
|
|
|
|
const KIND_MASK_SEND : u32 = 0b00000000000000000000000000000010u32;
|
|
|
|
const KIND_MASK_CONST : u32 = 0b00000000000000000000000000000100u32;
|
|
|
|
const KIND_MASK_IMPLICIT : u32 = 0b00000000000000000000000000001000u32;
|
2012-05-22 12:46:13 -07:00
|
|
|
|
|
|
|
fn kind_noncopyable() -> kind {
|
|
|
|
kind_(0u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind_copyable() -> kind {
|
|
|
|
kind_(KIND_MASK_COPY)
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:57:56 -07:00
|
|
|
fn kind_implicitly_copyable() -> kind {
|
|
|
|
kind_(KIND_MASK_IMPLICIT | KIND_MASK_COPY)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind_implicitly_sendable() -> kind {
|
|
|
|
kind_(KIND_MASK_IMPLICIT | KIND_MASK_COPY | KIND_MASK_SEND)
|
|
|
|
}
|
|
|
|
|
2012-05-22 12:46:13 -07:00
|
|
|
fn kind_sendable() -> kind {
|
|
|
|
kind_(KIND_MASK_COPY | KIND_MASK_SEND)
|
|
|
|
}
|
2011-12-28 18:11:33 +01:00
|
|
|
|
2012-05-22 14:10:32 -07:00
|
|
|
fn kind_send_only() -> kind {
|
|
|
|
kind_(KIND_MASK_SEND)
|
|
|
|
}
|
|
|
|
|
2012-05-24 14:24:09 -07:00
|
|
|
fn kind_const() -> kind {
|
|
|
|
kind_(KIND_MASK_CONST)
|
|
|
|
}
|
|
|
|
|
2012-05-23 10:15:06 -07:00
|
|
|
fn kind_top() -> kind {
|
|
|
|
kind_(0xffffffffu32)
|
|
|
|
}
|
|
|
|
|
2012-05-24 14:24:09 -07:00
|
|
|
fn remove_const(k: kind, tm: mt) -> kind {
|
|
|
|
if tm.mutbl == ast::m_mutbl {
|
|
|
|
k - kind_const()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:57:56 -07:00
|
|
|
fn remove_implicit(k: kind) -> kind {
|
|
|
|
k - kind_(KIND_MASK_IMPLICIT)
|
|
|
|
}
|
|
|
|
|
2012-06-01 19:54:16 -07:00
|
|
|
fn remove_copyable(k: kind) -> kind {
|
|
|
|
k - kind_(KIND_MASK_COPY)
|
|
|
|
}
|
|
|
|
|
2012-05-24 14:24:09 -07:00
|
|
|
impl operators for kind {
|
|
|
|
fn &(other: kind) -> kind {
|
|
|
|
lower_kind(self, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn |(other: kind) -> kind {
|
|
|
|
raise_kind(self, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn -(other: kind) -> kind {
|
|
|
|
kind_(*self & !*other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:57:56 -07:00
|
|
|
// Using these query functions is preferable to direct comparison or matching
|
2011-12-28 18:11:33 +01:00
|
|
|
// against the kind constants, as we may modify the kind hierarchy in the
|
|
|
|
// future.
|
2012-05-23 18:57:56 -07:00
|
|
|
pure fn kind_can_be_implicitly_copied(k: kind) -> bool {
|
|
|
|
*k & KIND_MASK_IMPLICIT != 0u32
|
|
|
|
}
|
|
|
|
|
2011-12-28 18:11:33 +01:00
|
|
|
pure fn kind_can_be_copied(k: kind) -> bool {
|
2012-05-22 12:46:13 -07:00
|
|
|
*k & KIND_MASK_COPY != 0u32
|
2011-12-28 18:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn kind_can_be_sent(k: kind) -> bool {
|
2012-05-22 12:46:13 -07:00
|
|
|
*k & KIND_MASK_SEND != 0u32
|
2011-12-28 18:11:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn proto_kind(p: proto) -> kind {
|
|
|
|
alt p {
|
2012-05-22 12:46:13 -07:00
|
|
|
ast::proto_any { kind_noncopyable() }
|
|
|
|
ast::proto_block { kind_noncopyable() }
|
2012-05-23 18:57:56 -07:00
|
|
|
ast::proto_box { kind_implicitly_copyable() }
|
2012-05-22 12:46:13 -07:00
|
|
|
ast::proto_uniq { kind_sendable() }
|
2012-05-23 18:57:56 -07:00
|
|
|
ast::proto_bare { kind_implicitly_sendable() | kind_const() }
|
2011-12-28 18:11:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 18:15:35 +01:00
|
|
|
fn kind_lteq(a: kind, b: kind) -> bool {
|
2012-05-22 12:46:13 -07:00
|
|
|
*a & *b == *a
|
2011-11-15 18:15:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_kind(a: kind, b: kind) -> kind {
|
2012-05-23 10:15:06 -07:00
|
|
|
kind_(*a & *b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raise_kind(a: kind, b: kind) -> kind {
|
2012-05-22 14:10:32 -07:00
|
|
|
kind_(*a | *b)
|
2011-11-15 18:15:35 +01:00
|
|
|
}
|
|
|
|
|
2012-04-09 17:32:49 -07:00
|
|
|
#[test]
|
|
|
|
fn test_kinds() {
|
2012-05-23 18:57:56 -07:00
|
|
|
// The kind "lattice" is defined by the subset operation on the
|
|
|
|
// set of permitted operations.
|
2012-05-22 12:46:13 -07:00
|
|
|
assert kind_lteq(kind_sendable(), kind_sendable());
|
|
|
|
assert kind_lteq(kind_copyable(), kind_sendable());
|
|
|
|
assert kind_lteq(kind_copyable(), kind_copyable());
|
|
|
|
assert kind_lteq(kind_noncopyable(), kind_sendable());
|
|
|
|
assert kind_lteq(kind_noncopyable(), kind_copyable());
|
|
|
|
assert kind_lteq(kind_noncopyable(), kind_noncopyable());
|
2012-05-23 18:57:56 -07:00
|
|
|
assert kind_lteq(kind_copyable(), kind_implicitly_copyable());
|
|
|
|
assert kind_lteq(kind_copyable(), kind_implicitly_sendable());
|
|
|
|
assert kind_lteq(kind_sendable(), kind_implicitly_sendable());
|
|
|
|
assert !kind_lteq(kind_sendable(), kind_implicitly_copyable());
|
|
|
|
assert !kind_lteq(kind_copyable(), kind_send_only());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the most permissive kind that a composite object containing a field
|
|
|
|
// with the given mutability can have.
|
|
|
|
// This is used to prevent objects containing mutable state from being
|
|
|
|
// implicitly copied.
|
|
|
|
fn mutability_kind(m: mutability) -> kind {
|
|
|
|
alt (m) {
|
|
|
|
m_mutbl | m_const { remove_implicit(kind_top()) }
|
|
|
|
m_imm { kind_top() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mutable_type_kind(cx: ctxt, ty: mt) -> kind {
|
|
|
|
lower_kind(mutability_kind(ty.mutbl), type_kind(cx, ty.ty))
|
2012-04-09 17:32:49 -07: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.
|
2012-05-23 18:57:56 -07:00
|
|
|
cx.kind_cache.insert(ty, kind_top());
|
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-05-23 18:57:56 -07:00
|
|
|
ty_ptr(_) { kind_implicitly_sendable() | kind_const() }
|
2012-06-04 18:34:11 -07:00
|
|
|
// Implicit copyability of strs is configurable
|
|
|
|
ty_str {
|
|
|
|
if cx.vecs_implicitly_copyable {
|
|
|
|
kind_implicitly_sendable() | kind_const()
|
|
|
|
} else { kind_sendable() | kind_const() }
|
|
|
|
}
|
2011-12-28 18:11:33 +01:00
|
|
|
ty_fn(f) { proto_kind(f.proto) }
|
2012-04-09 17:32:49 -07:00
|
|
|
|
|
|
|
// Those with refcounts raise noncopyable to copyable,
|
|
|
|
// lower sendable to copyable. Therefore just set result to copyable.
|
2012-05-24 14:24:09 -07:00
|
|
|
ty_box(tm) {
|
|
|
|
if tm.mutbl == ast::m_mutbl {
|
2012-05-23 18:57:56 -07:00
|
|
|
kind_implicitly_copyable()
|
2012-05-24 14:24:09 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let k = type_kind(cx, tm.ty);
|
|
|
|
if kind_lteq(kind_const(), k) {
|
2012-05-23 18:57:56 -07:00
|
|
|
kind_implicitly_copyable() | kind_const()
|
2012-05-24 14:24:09 -07:00
|
|
|
}
|
2012-05-23 18:57:56 -07:00
|
|
|
else { kind_implicitly_copyable() }
|
2012-05-24 14:24:09 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 17:58:40 -07:00
|
|
|
ty_iface(_, _) { kind_implicitly_copyable() }
|
2012-05-23 18:57:56 -07:00
|
|
|
ty_rptr(_, _) { kind_implicitly_copyable() }
|
2012-04-09 17:32:49 -07:00
|
|
|
|
2012-05-23 18:57:56 -07:00
|
|
|
// Unique boxes and vecs have the kind of their contained type,
|
|
|
|
// but unique boxes can't be implicitly copyable.
|
|
|
|
ty_uniq(tm) {
|
|
|
|
remove_implicit(remove_const(type_kind(cx, tm.ty), tm))
|
|
|
|
}
|
2012-06-04 18:34:11 -07:00
|
|
|
// Implicit copyability of vecs is configurable
|
|
|
|
ty_vec(tm) {
|
|
|
|
let k = if cx.vecs_implicitly_copyable {
|
|
|
|
mutable_type_kind(cx, tm)
|
|
|
|
} else { remove_implicit(type_kind(cx, tm.ty)) };
|
|
|
|
remove_const(k, tm)
|
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
|
|
|
|
// Slice and refcounted evecs are copyable; uniques and interiors
|
2012-05-23 18:57:56 -07:00
|
|
|
// depend on the their contained type, but aren't implicitly copyable.
|
2012-05-24 14:24:09 -07:00
|
|
|
ty_evec(tm, vstore_box) |
|
|
|
|
ty_evec(tm, vstore_slice(_)) {
|
|
|
|
if kind_lteq(kind_const(), type_kind(cx, tm.ty)) {
|
2012-05-23 18:57:56 -07:00
|
|
|
kind_implicitly_copyable() | kind_const()
|
2012-05-24 14:24:09 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-05-23 18:57:56 -07:00
|
|
|
kind_implicitly_copyable()
|
2012-05-24 14:24:09 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(tm, vstore_uniq) |
|
2012-05-23 18:57:56 -07:00
|
|
|
ty_evec(tm, vstore_fixed(_)) {
|
|
|
|
remove_implicit(remove_const(type_kind(cx, tm.ty), tm))
|
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
|
|
|
|
// All estrs are copyable; uniques and interiors are sendable.
|
|
|
|
ty_estr(vstore_box) |
|
2012-05-23 18:57:56 -07:00
|
|
|
ty_estr(vstore_slice(_)) { kind_implicitly_copyable() | kind_const() }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_estr(vstore_uniq) |
|
2012-05-24 14:24:09 -07:00
|
|
|
ty_estr(vstore_fixed(_)) { kind_sendable() | kind_const() }
|
2012-04-09 17:32:49 -07:00
|
|
|
|
2011-07-27 17:49:00 -07:00
|
|
|
// Records lower to the lowest of their members.
|
|
|
|
ty_rec(flds) {
|
2012-05-24 14:24:09 -07:00
|
|
|
let mut lowest = kind_top();
|
2012-04-06 20:01:43 +02:00
|
|
|
for flds.each {|f|
|
2012-05-23 18:57:56 -07:00
|
|
|
lowest = lower_kind(lowest, mutable_type_kind(cx, f.mt));
|
2012-05-24 14:24:09 -07:00
|
|
|
lowest = remove_const(lowest, f.mt);
|
2012-04-06 20:01:43 +02:00
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
lowest
|
2011-07-27 17:49:00 -07:00
|
|
|
}
|
2012-04-13 12:22:35 -07:00
|
|
|
ty_class(did, substs) {
|
2012-06-01 17:06:27 -07:00
|
|
|
// Classes are sendable if all their fields are sendable,
|
|
|
|
// likewise for copyable...
|
2012-05-23 18:57:56 -07:00
|
|
|
// also factor out this code, copied from the records case
|
|
|
|
let mut lowest = kind_top();
|
|
|
|
let flds = class_items_as_fields(cx, did, substs);
|
|
|
|
for flds.each {|f|
|
|
|
|
lowest = lower_kind(lowest, mutable_type_kind(cx, f.mt));
|
|
|
|
}
|
2012-06-01 17:06:27 -07:00
|
|
|
// ...but classes with dtors are never copyable (they can be
|
|
|
|
// sendable)
|
|
|
|
if ty::has_dtor(cx, did) {
|
2012-06-01 19:54:16 -07:00
|
|
|
lowest = remove_copyable(lowest);
|
2012-06-01 17:06:27 -07:00
|
|
|
}
|
2012-05-23 18:57:56 -07:00
|
|
|
lowest
|
2012-04-13 12:22:35 -07:00
|
|
|
}
|
2011-08-18 18:06:00 -07:00
|
|
|
// Tuples lower to the lowest of their members.
|
|
|
|
ty_tup(tys) {
|
2012-05-24 14:24:09 -07:00
|
|
|
let mut lowest = kind_top();
|
2012-04-06 20:01:43 +02:00
|
|
|
for tys.each {|ty| lowest = lower_kind(lowest, type_kind(cx, ty)); }
|
2011-11-15 18:15:35 +01:00
|
|
|
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.
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-05-24 14:24:09 -07:00
|
|
|
let mut lowest = kind_top();
|
2012-04-04 19:07:52 -07:00
|
|
|
let variants = enum_variants(cx, did);
|
|
|
|
if vec::len(*variants) == 0u {
|
2012-05-22 12:46:13 -07:00
|
|
|
lowest = kind_noncopyable();
|
2012-04-04 19:07:52 -07:00
|
|
|
} else {
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*variants) {|variant|
|
|
|
|
for variant.args.each {|aty|
|
2012-04-04 19:07:52 -07:00
|
|
|
// Perform any type parameter substitutions.
|
2012-04-18 21:26:25 -07:00
|
|
|
let arg_ty = subst(cx, substs, aty);
|
2012-04-04 19:07:52 -07:00
|
|
|
lowest = lower_kind(lowest, type_kind(cx, arg_ty));
|
2012-05-22 12:46:13 -07:00
|
|
|
if lowest == kind_noncopyable() { break; }
|
2012-04-04 19:07:52 -07:00
|
|
|
}
|
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
|
|
|
}
|
2012-05-31 10:51:58 -07:00
|
|
|
ty_res(did, inner, tps) {
|
|
|
|
let inner = subst(cx, tps, inner);
|
|
|
|
(kind_const() & type_kind(cx, inner)) | kind_send_only()
|
|
|
|
}
|
2012-01-02 12:13:26 +01:00
|
|
|
ty_param(_, did) {
|
2012-05-23 18:57:56 -07:00
|
|
|
// FIXME: type params shouldn't be implicitly copyable (#2449)
|
|
|
|
let k = param_bounds_to_kind(cx.ty_param_bounds.get(did.node));
|
|
|
|
if kind_can_be_copied(k)
|
|
|
|
{ raise_kind(k, kind_implicitly_copyable()) } else { k }
|
2012-01-02 12:13:26 +01:00
|
|
|
}
|
2011-11-15 18:15:35 +01:00
|
|
|
ty_constr(t, _) { type_kind(cx, t) }
|
2012-05-24 14:24:09 -07:00
|
|
|
// FIXME: is self ever const?
|
2012-05-22 12:46:13 -07:00
|
|
|
ty_self { kind_noncopyable() }
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_var(_) | ty_var_integral(_) {
|
|
|
|
cx.sess.bug("Asked to compute kind of a type variable");
|
|
|
|
}
|
2012-06-01 17:58:40 -07:00
|
|
|
ty_type | ty_opaque_closure_ptr(_) | ty_opaque_box {
|
|
|
|
cx.sess.bug("Asked to compute kind of fictitious type");
|
|
|
|
}
|
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-05-15 20:25:00 -07:00
|
|
|
// True if instantiating an instance of `ty` requires an instance of `r_ty`.
|
2012-03-28 13:42:33 -07:00
|
|
|
fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
|
|
|
|
|
|
|
|
fn type_requires(cx: ctxt, seen: @mut [def_id],
|
|
|
|
r_ty: t, ty: t) -> bool {
|
|
|
|
#debug["type_requires(%s, %s)?",
|
|
|
|
ty_to_str(cx, r_ty),
|
|
|
|
ty_to_str(cx, ty)];
|
|
|
|
|
|
|
|
let r = {
|
|
|
|
get(r_ty).struct == get(ty).struct ||
|
|
|
|
subtypes_require(cx, seen, r_ty, ty)
|
|
|
|
};
|
|
|
|
|
|
|
|
#debug["type_requires(%s, %s)? %b",
|
|
|
|
ty_to_str(cx, r_ty),
|
|
|
|
ty_to_str(cx, ty),
|
|
|
|
r];
|
|
|
|
ret r;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subtypes_require(cx: ctxt, seen: @mut [def_id],
|
|
|
|
r_ty: t, ty: t) -> bool {
|
|
|
|
#debug["subtypes_require(%s, %s)?",
|
|
|
|
ty_to_str(cx, r_ty),
|
|
|
|
ty_to_str(cx, ty)];
|
|
|
|
|
|
|
|
let r = alt get(ty).struct {
|
|
|
|
ty_nil |
|
|
|
|
ty_bot |
|
|
|
|
ty_bool |
|
|
|
|
ty_int(_) |
|
|
|
|
ty_uint(_) |
|
|
|
|
ty_float(_) |
|
|
|
|
ty_str |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_estr(_) |
|
2012-03-28 13:42:33 -07:00
|
|
|
ty_fn(_) |
|
|
|
|
ty_var(_) |
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_var_integral(_) |
|
2012-03-28 13:42:33 -07:00
|
|
|
ty_param(_, _) |
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_self |
|
2012-03-28 13:42:33 -07:00
|
|
|
ty_type |
|
|
|
|
ty_opaque_box |
|
|
|
|
ty_opaque_closure_ptr(_) |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(_, _) |
|
2012-03-28 13:42:33 -07:00
|
|
|
ty_vec(_) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_constr(t, _) {
|
|
|
|
type_requires(cx, seen, r_ty, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_box(mt) |
|
|
|
|
ty_uniq(mt) |
|
|
|
|
ty_rptr(_, mt) {
|
2012-05-14 16:55:01 -07:00
|
|
|
ret type_requires(cx, seen, r_ty, mt.ty);
|
2012-03-28 13:42:33 -07:00
|
|
|
}
|
|
|
|
|
2012-05-03 17:07:57 -07:00
|
|
|
ty_ptr(mt) {
|
|
|
|
false // unsafe ptrs can always be NULL
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:42:33 -07:00
|
|
|
ty_rec(fields) {
|
|
|
|
vec::any(fields) {|field|
|
|
|
|
type_requires(cx, seen, r_ty, field.mt.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_iface(_, _) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_class(did, _) if vec::contains(*seen, did) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_class(did, substs) {
|
2012-03-28 13:42:33 -07:00
|
|
|
vec::push(*seen, did);
|
2012-06-05 16:20:12 -07:00
|
|
|
let r = vec::any(class_items_as_fields(cx, did, substs)) {|f|
|
|
|
|
type_requires(cx, seen, r_ty, f.mt.ty)};
|
2012-03-28 13:42:33 -07:00
|
|
|
vec::pop(*seen);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_res(did, _, _) if vec::contains(*seen, did) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(did, sub, substs) {
|
2012-03-28 13:42:33 -07:00
|
|
|
vec::push(*seen, did);
|
2012-04-18 21:26:25 -07:00
|
|
|
let sty = subst(cx, substs, sub);
|
2012-03-28 13:42:33 -07:00
|
|
|
let r = type_requires(cx, seen, r_ty, sty);
|
|
|
|
vec::pop(*seen);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_tup(ts) {
|
|
|
|
vec::any(ts) {|t|
|
|
|
|
type_requires(cx, seen, r_ty, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_enum(did, _) if vec::contains(*seen, did) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-03-28 13:42:33 -07:00
|
|
|
vec::push(*seen, did);
|
|
|
|
let vs = enum_variants(cx, did);
|
|
|
|
let r = vec::len(*vs) > 0u && vec::all(*vs) {|variant|
|
|
|
|
vec::any(variant.args) {|aty|
|
2012-04-18 21:26:25 -07:00
|
|
|
let sty = subst(cx, substs, aty);
|
2012-03-28 13:42:33 -07:00
|
|
|
type_requires(cx, seen, r_ty, sty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
vec::pop(*seen);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#debug["subtypes_require(%s, %s)? %b",
|
|
|
|
ty_to_str(cx, r_ty),
|
|
|
|
ty_to_str(cx, ty),
|
|
|
|
r];
|
|
|
|
|
|
|
|
ret r;
|
|
|
|
}
|
|
|
|
|
|
|
|
let seen = @mut [];
|
|
|
|
!subtypes_require(cx, seen, r_ty, r_ty)
|
|
|
|
}
|
|
|
|
|
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-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*enum_variants(cx, did)) {|variant|
|
|
|
|
for variant.args.each {|aty|
|
2012-04-18 21:26:25 -07:00
|
|
|
let sty = subst(cx, substs, aty);
|
2011-08-22 13:39:32 +02:00
|
|
|
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) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for fields.each {|field|
|
2011-08-22 13:39:32 +02:00
|
|
|
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) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for ts.each {|tt|
|
2011-08-22 13:39:32 +02:00
|
|
|
if type_structurally_contains(cx, tt, test) { ret true; }
|
|
|
|
}
|
2011-08-15 11:40:26 +02:00
|
|
|
ret false;
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(_, sub, substs) {
|
|
|
|
let sty = subst(cx, substs, sub);
|
2011-08-22 13:39:32 +02:00
|
|
|
ret type_structurally_contains(cx, sty, test);
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(mt, vstore_fixed(_)) {
|
|
|
|
ret type_structurally_contains(cx, mt.ty, test);
|
|
|
|
}
|
2011-08-22 13:39:32 +02:00
|
|
|
_ { ret false; }
|
2011-07-27 14:19:39 +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
|
2012-03-26 18:35:18 -07:00
|
|
|
// distinguished from the value itself. I.e. types with mut content that's
|
2011-11-18 09:46:44 +01:00
|
|
|
// 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 }
|
2012-04-09 17:32:49 -07:00
|
|
|
|
|
|
|
ty_evec(_, _) | ty_estr(_) {
|
|
|
|
cx.sess.unimpl("estr/evec in type_allows_implicit_copy");
|
|
|
|
}
|
|
|
|
|
2011-09-12 14:43:41 +02:00
|
|
|
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) {
|
2012-03-26 12:39:20 +02:00
|
|
|
vec::any(fields, {|f| f.mt.mutbl != ast::m_imm})
|
2011-09-12 14:43:41 +02:00
|
|
|
}
|
|
|
|
_ { false }
|
2012-01-11 09:58:05 -08:00
|
|
|
}
|
2012-05-22 12:46:13 -07: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|
|
2012-03-26 12:39:20 +02:00
|
|
|
alt sty {
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_uniq(_) |
|
|
|
|
ty_vec(_) |
|
|
|
|
ty_evec(_, vstore_uniq) |
|
|
|
|
ty_str |
|
|
|
|
ty_estr(vstore_uniq) { true }
|
2012-03-26 12:39:20 +02:00
|
|
|
_ { false }
|
|
|
|
}
|
2011-09-12 14:43:41 +02:00
|
|
|
});
|
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 {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut 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-03-15 17:42:33 +01:00
|
|
|
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-03-08 17:22:10 -08:00
|
|
|
ty_iface(_, _) | ty_rptr(_,_) | ty_opaque_box { result = false; }
|
2011-08-19 15:16:48 -07:00
|
|
|
// Structural types
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-01-25 14:34:31 +01:00
|
|
|
let variants = enum_variants(cx, did);
|
2012-04-06 20:01:43 +02:00
|
|
|
for vec::each(*variants) {|variant|
|
2011-08-19 15:16:48 -07:00
|
|
|
let tup_ty = mk_tup(cx, variant.args);
|
|
|
|
|
|
|
|
// Perform any type parameter substitutions.
|
2012-04-18 21:26:25 -07:00
|
|
|
let tup_ty = subst(cx, substs, tup_ty);
|
2011-08-19 15:16:48 -07:00
|
|
|
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) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for flds.each {|f|
|
2011-08-19 15:16:48 -07:00
|
|
|
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) {
|
2012-04-06 20:01:43 +02:00
|
|
|
for elts.each {|elt| if !type_is_pod(cx, elt) { result = false; } }
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_estr(vstore_fixed(_)) { result = true; }
|
|
|
|
ty_evec(mt, vstore_fixed(_)) {
|
|
|
|
result = type_is_pod(cx, mt.ty);
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_res(_, inner, substs) {
|
|
|
|
result = type_is_pod(cx, subst(cx, substs, inner));
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|
|
|
|
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-05-17 17:20:58 -07:00
|
|
|
ty_class(did, substs) {
|
|
|
|
result = vec::any(lookup_class_fields(cx, did)) { |f|
|
|
|
|
let fty = ty::lookup_item_type(cx, f.id);
|
|
|
|
let sty = subst(cx, substs, fty.ty);
|
|
|
|
type_is_pod(cx, sty)
|
|
|
|
};
|
|
|
|
}
|
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-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-01-25 14:34:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-04-26 16:02:01 -07:00
|
|
|
// Returns the type and mutability of *t.
|
|
|
|
//
|
|
|
|
// The parameter `expl` indicates if this is an *explicit* dereference. Some
|
|
|
|
// types---notably unsafe ptrs---can only be dereferenced explicitly.
|
|
|
|
fn deref(cx: ctxt, t: t, expl: bool) -> option<mt> {
|
|
|
|
deref_sty(cx, get(t).struct, expl)
|
|
|
|
}
|
|
|
|
fn deref_sty(cx: ctxt, sty: sty, expl: bool) -> option<mt> {
|
|
|
|
alt sty {
|
|
|
|
ty_rptr(_, mt) | ty_box(mt) | ty_uniq(mt) {
|
|
|
|
some(mt)
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_ptr(mt) if expl {
|
|
|
|
some(mt)
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_res(_, inner, substs) {
|
|
|
|
let inner = subst(cx, substs, inner);
|
|
|
|
some({ty: inner, mutbl: ast::m_imm})
|
|
|
|
}
|
|
|
|
|
|
|
|
ty_enum(did, substs) {
|
|
|
|
let variants = enum_variants(cx, did);
|
|
|
|
if vec::len(*variants) == 1u && vec::len(variants[0].args) == 1u {
|
|
|
|
let v_t = subst(cx, substs, variants[0].args[0]);
|
|
|
|
some({ty: v_t, mutbl: ast::m_imm})
|
|
|
|
} else {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ { none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
fn type_autoderef(cx: ctxt, t: t) -> t {
|
2012-04-26 16:02:01 -07:00
|
|
|
let mut t = t;
|
2012-03-10 20:35:41 -08:00
|
|
|
loop {
|
2012-04-26 16:02:01 -07:00
|
|
|
alt deref(cx, t, false) {
|
|
|
|
none { ret t; }
|
|
|
|
some(mt) { t = mt.ty; }
|
2011-07-01 01:48:32 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-26 16:02:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the type and mutability of t[i]
|
|
|
|
fn index(cx: ctxt, t: t) -> option<mt> {
|
|
|
|
index_sty(cx, get(t).struct)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index_sty(cx: ctxt, sty: sty) -> option<mt> {
|
|
|
|
alt sty {
|
|
|
|
ty_vec(mt) | ty_evec(mt, _) { some(mt) }
|
|
|
|
ty_str | ty_estr(_) { some({ty: mk_u8(cx), mutbl: ast::m_imm}) }
|
|
|
|
_ { none }
|
|
|
|
}
|
2011-07-01 01:48:32 -07:00
|
|
|
}
|
|
|
|
|
2012-04-01 14:28:30 -07:00
|
|
|
fn hash_bound_region(br: bound_region) -> uint {
|
|
|
|
alt br { // no idea if this is any good
|
2012-04-15 22:06:19 -07:00
|
|
|
ty::br_self { 0u }
|
|
|
|
ty::br_anon { 1u }
|
|
|
|
ty::br_named(str) { str::hash(str) }
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn br_hashmap<V:copy>() -> hashmap<bound_region, V> {
|
|
|
|
map::hashmap(hash_bound_region,
|
|
|
|
{|&&a: bound_region, &&b: bound_region| a == b })
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = id;
|
2012-04-06 20:01:43 +02:00
|
|
|
for subtys.each {|s| h = (h << 2u) + type_id(s) }
|
2012-02-03 15:15:28 +01:00
|
|
|
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 {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = id;
|
2012-02-03 15:15:28 +01:00
|
|
|
h = (h << 2u) + hash_def(h, c.node.id);
|
|
|
|
// FIXME this makes little sense
|
2012-04-06 20:01:43 +02:00
|
|
|
for c.node.args.each {|a|
|
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
|
|
|
}
|
2012-03-08 14:05:16 -08:00
|
|
|
fn hash_region(r: region) -> uint {
|
2012-04-01 14:28:30 -07:00
|
|
|
alt r { // no idea if this is any good
|
|
|
|
re_bound(br) { (hash_bound_region(br)) << 2u | 0u }
|
|
|
|
re_free(id, br) { ((id as uint) << 4u) |
|
|
|
|
(hash_bound_region(br)) << 2u | 1u }
|
|
|
|
re_scope(id) { ((id as uint) << 2u) | 2u }
|
|
|
|
re_var(id) { (id.to_uint() << 2u) | 3u }
|
2012-04-15 22:06:19 -07:00
|
|
|
re_bot { 4u }
|
2012-03-08 14:05:16 -08:00
|
|
|
}
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
fn hash_substs(h: uint, substs: substs) -> uint {
|
|
|
|
let h = hash_subtys(h, substs.tps);
|
|
|
|
h + substs.self_r.map_default(0u, hash_region)
|
|
|
|
}
|
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-04-09 17:32:49 -07:00
|
|
|
ty_estr(_) { 16u }
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_str { 17u }
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(did, substs) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = hash_def(18u, did);
|
2012-04-18 21:26:25 -07:00
|
|
|
hash_substs(h, substs)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_box(mt) { hash_subty(19u, mt.ty) }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(mt, _) { hash_subty(20u, mt.ty) }
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_vec(mt) { hash_subty(21u, mt.ty) }
|
2012-06-12 10:29:17 -07:00
|
|
|
ty_tup(ts) { hash_subtys(25u, ts) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_rec(fields) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = 26u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for fields.each {|f| h = hash_subty(h, f.mt.ty); }
|
2012-02-03 15:15:28 +01:00
|
|
|
h
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_fn(f) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = 27u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for f.inputs.each {|a| h = hash_subty(h, a.ty); }
|
2012-02-03 15:15:28 +01:00
|
|
|
hash_subty(h, f.output)
|
|
|
|
}
|
2012-06-12 10:29:17 -07:00
|
|
|
ty_self { 28u }
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_var(v) { hash_uint(29u, v.to_uint()) }
|
|
|
|
ty_var_integral(v) { hash_uint(30u, v.to_uint()) }
|
2012-02-03 15:15:28 +01:00
|
|
|
ty_param(pid, did) { hash_def(hash_uint(31u, pid), did) }
|
|
|
|
ty_type { 32u }
|
|
|
|
ty_bot { 34u }
|
|
|
|
ty_ptr(mt) { hash_subty(35u, mt.ty) }
|
2011-07-27 14:19:39 +02:00
|
|
|
ty_constr(t, cs) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = hash_subty(36u, t);
|
2012-04-06 20:01:43 +02:00
|
|
|
for cs.each {|c| h = (h << 2u) + hash_type_constr(h, c); }
|
2012-02-03 15:15:28 +01:00
|
|
|
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) }
|
2012-06-12 10:29:17 -07:00
|
|
|
ty_res(did, sub, substs) {
|
|
|
|
let mut h = hash_subty(hash_def(38u, did), sub);
|
|
|
|
hash_substs(h, substs)
|
|
|
|
}
|
2012-04-24 15:52:52 -07:00
|
|
|
ty_iface(did, substs) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut h = hash_def(40u, did);
|
2012-04-24 15:52:52 -07:00
|
|
|
hash_substs(h, substs)
|
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-04-18 21:26:25 -07:00
|
|
|
ty_class(did, substs) {
|
|
|
|
let mut h = hash_def(45u, did);
|
|
|
|
hash_substs(h, substs)
|
2012-02-09 14:16:12 -08:00
|
|
|
}
|
2012-06-12 10:29:17 -07:00
|
|
|
ty_rptr(region, mt) {
|
|
|
|
let mut h = (46u << 2u) + hash_region(region);
|
|
|
|
hash_subty(h, mt.ty)
|
|
|
|
}
|
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 {
|
2012-03-22 14:56:56 -07:00
|
|
|
ast::carg_lit(m) { ret const_eval::lit_eq(l, m); } _ { ret false; }
|
2011-11-22 11:49:29 +01:00
|
|
|
}
|
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 {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut i: uint = 0u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for a.each {|arg|
|
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; }
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut i = 0u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for cs.each {|c| 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 }
|
2012-03-28 12:54:06 -07:00
|
|
|
none { cx.sess.bug(#fmt("node_id_to_type: unbound node ID %s",
|
|
|
|
ast_map::node_id_to_str(cx.items, id))); }
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
2012-05-23 14:12:40 -07:00
|
|
|
// Returns a vec of all the input and output types of fty.
|
|
|
|
fn tys_in_fn_ty(fty: fn_ty) -> [t] {
|
|
|
|
fty.inputs.map({|a| a.ty}) + [fty.output]
|
|
|
|
}
|
|
|
|
|
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-06-08 10:17:02 -07:00
|
|
|
fn ty_var_id(typ: t) -> tv_vid {
|
2012-02-03 15:15:28 +01:00
|
|
|
alt get(typ).struct {
|
2012-06-08 10:17:02 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
fn ty_var_integral_id(typ: t) -> tvi_vid {
|
|
|
|
alt get(typ).struct {
|
|
|
|
ty_var_integral(vid) { ret vid; }
|
|
|
|
_ { #error("ty_var_integral_id called on ty other than \
|
|
|
|
ty_var_integral");
|
|
|
|
fail; }
|
|
|
|
}
|
|
|
|
}
|
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> {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut i = 0u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for fields.each {|f| if f.ident == id { ret some(i); } i += 1u; }
|
2011-12-18 19:30:40 +01:00
|
|
|
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> {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut i = 0u;
|
2012-04-06 20:01:43 +02:00
|
|
|
for meths.each {|m| if m.ident == id { ret some(i); } i += 1u; }
|
2011-12-18 19:30:40 +01:00
|
|
|
ret none;
|
2010-12-21 12:13:51 -08:00
|
|
|
}
|
|
|
|
|
2012-06-08 10:17:02 -07:00
|
|
|
fn occurs_check(tcx: ctxt, sp: span, vid: tv_vid, rt: t) {
|
|
|
|
|
|
|
|
// Returns a vec of all the type variables occurring in `ty`. It may
|
|
|
|
// contain duplicates. (Integral type vars aren't counted.)
|
|
|
|
fn vars_in_type(ty: t) -> [tv_vid] {
|
|
|
|
let mut rslt = [];
|
|
|
|
walk_ty(ty) {|ty|
|
|
|
|
alt get(ty).struct { ty_var(v) { rslt += [v]; } _ { } }
|
|
|
|
}
|
|
|
|
rslt
|
|
|
|
}
|
|
|
|
|
2012-02-03 15:15:28 +01:00
|
|
|
// Fast path
|
2012-05-09 06:09:58 -07:00
|
|
|
if !type_needs_infer(rt) { ret; }
|
2011-08-19 15:16:48 -07:00
|
|
|
|
2011-08-03 18:06:57 -07:00
|
|
|
// Occurs check!
|
2012-06-08 10:17:02 -07:00
|
|
|
if vec::contains(vars_in_type(rt), vid) {
|
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-10 20:35:41 -08:00
|
|
|
(sp, "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.");
|
2012-03-10 20:35:41 -08:00
|
|
|
}
|
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)
|
2012-03-13 14:39:28 -07:00
|
|
|
-> result<ast::mode, type_err> {
|
2012-02-02 16:50:17 -08:00
|
|
|
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(_) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-06 06:58:41 -07:00
|
|
|
fn ty_sort_str(cx: ctxt, t: t) -> str {
|
|
|
|
alt get(t).struct {
|
|
|
|
ty_nil | ty_bot | ty_bool | ty_int(_) |
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_uint(_) | ty_float(_) | ty_estr(_) | ty_str |
|
|
|
|
ty_type | ty_opaque_box | ty_opaque_closure_ptr(_) {
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_to_str(cx, t)
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_enum(id, _) { #fmt["enum %s", item_path_str(cx, id)] }
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_box(_) { "@-ptr" }
|
|
|
|
ty_uniq(_) { "~-ptr" }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_evec(_, _) | ty_vec(_) { "vector" }
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_ptr(_) { "*-ptr" }
|
|
|
|
ty_rptr(_, _) { "&-ptr" }
|
|
|
|
ty_rec(_) { "record" }
|
|
|
|
ty_fn(_) { "fn" }
|
2012-04-18 21:26:25 -07:00
|
|
|
ty_iface(id, _) { #fmt["iface %s", item_path_str(cx, id)] }
|
|
|
|
ty_class(id, _) { #fmt["class %s", item_path_str(cx, id)] }
|
|
|
|
ty_res(id, _, _) { #fmt["resource %s", item_path_str(cx, id)] }
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_tup(_) { "tuple" }
|
|
|
|
ty_var(_) { "variable" }
|
2012-06-01 14:56:33 -07:00
|
|
|
ty_var_integral(_) { "integral variable" }
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_param(_, _) { "type parameter" }
|
2012-05-09 06:09:58 -07:00
|
|
|
ty_self { "self" }
|
2012-04-06 06:58:41 -07:00
|
|
|
ty_constr(t, _) { ty_sort_str(cx, t) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 19:19:32 -07:00
|
|
|
fn type_err_to_str(cx: ctxt, err: type_err) -> str {
|
2012-04-14 18:45:03 -07:00
|
|
|
fn terr_vstore_kind_to_str(k: terr_vstore_kind) -> str {
|
|
|
|
alt k { terr_vec { "[]" } terr_str { "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-05-24 23:44:58 -07:00
|
|
|
terr_purity_mismatch(f1, f2) {
|
|
|
|
ret #fmt["expected %s fn but found %s fn", f1.to_str(), f2.to_str()];
|
|
|
|
}
|
2012-03-22 20:06:01 -07:00
|
|
|
terr_proto_mismatch(e, a) {
|
|
|
|
ret #fmt["closure protocol mismatch (%s vs %s)",
|
|
|
|
proto_to_str(e), proto_to_str(a)];
|
|
|
|
}
|
|
|
|
terr_mutability { ret "values differ in mutability"; }
|
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"; }
|
2012-03-08 15:54:36 -08:00
|
|
|
terr_ref_mutability { ret "references differ in mutability"; }
|
2012-03-13 20:46:16 -04:00
|
|
|
terr_ty_param_size(e_sz, a_sz) {
|
|
|
|
ret "expected a type with " + uint::to_str(e_sz, 10u) +
|
|
|
|
" type params but found one with " + uint::to_str(a_sz, 10u) +
|
|
|
|
" type params";
|
|
|
|
}
|
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) {
|
2012-04-06 06:58:41 -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
|
|
|
}
|
2012-04-06 06:58:41 -07:00
|
|
|
terr_regions_differ(subregion, superregion) {
|
2012-04-17 08:40:06 -07:00
|
|
|
ret #fmt("references with lifetime %s do not necessarily \
|
|
|
|
outlive references with lifetime %s",
|
2012-03-15 19:19:32 -07:00
|
|
|
region_to_str(cx, subregion),
|
|
|
|
region_to_str(cx, superregion));
|
2012-03-11 13:28:43 -07:00
|
|
|
}
|
2012-04-14 18:45:03 -07:00
|
|
|
terr_vstores_differ(k, e_vs, a_vs) {
|
|
|
|
ret #fmt("%s storage differs: expected %s but found %s",
|
|
|
|
terr_vstore_kind_to_str(k),
|
|
|
|
vstore_to_str(cx, e_vs),
|
|
|
|
vstore_to_str(cx, a_vs));
|
|
|
|
}
|
2012-04-06 06:58:41 -07:00
|
|
|
terr_in_field(err, fname) {
|
|
|
|
ret #fmt("in field `%s`, %s", fname, type_err_to_str(cx, *err));
|
|
|
|
}
|
|
|
|
terr_sorts(exp, act) {
|
|
|
|
ret #fmt("%s vs %s", ty_sort_str(cx, exp), ty_sort_str(cx, act));
|
|
|
|
}
|
2012-05-09 06:09:58 -07:00
|
|
|
terr_self_substs {
|
|
|
|
ret "inconsistent self substitution"; // XXX this is more of a bug
|
|
|
|
}
|
2010-12-21 17:47:13 -08: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-04-02 10:48:32 -07:00
|
|
|
ast::def_fn(_, _) | ast::def_variant(_, _) | ast::def_class(_)
|
|
|
|
{ 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-04-13 12:22:35 -07:00
|
|
|
alt cx.items.find(id.node) {
|
|
|
|
some(ast_map::node_item(@{node: ast::item_impl(
|
2012-04-24 15:52:52 -07:00
|
|
|
_, _, some(@{id: id, _}), _, _), _}, _)) {
|
2012-04-13 12:22:35 -07:00
|
|
|
some(node_id_to_type(cx, id))
|
|
|
|
}
|
2012-05-14 14:13:32 -07:00
|
|
|
some(ast_map::node_item(@{node: ast::item_class(_, _, _, _, _, _),
|
2012-04-13 12:22:35 -07:00
|
|
|
_},_)) {
|
|
|
|
alt cx.def_map.find(id.node) {
|
|
|
|
some(def_ty(iface_id)) {
|
|
|
|
some(node_id_to_type(cx, id.node))
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
cx.sess.bug("impl_iface: iface ref isn't in iface map \
|
|
|
|
and isn't bound to a def_ty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { none }
|
2012-03-07 12:54:00 +01:00
|
|
|
}
|
2012-01-05 10:57:19 +01:00
|
|
|
} else {
|
|
|
|
csearch::get_impl_iface(cx, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 21:44:28 -04:00
|
|
|
fn ty_to_def_id(ty: t) -> option<ast::def_id> {
|
|
|
|
alt get(ty).struct {
|
2012-03-08 16:10:25 +01:00
|
|
|
ty_iface(id, _) | ty_class(id, _) | ty_res(id, _, _) | ty_enum(id, _) {
|
2012-05-22 21:44:28 -04:00
|
|
|
some(id)
|
2012-03-08 16:10:25 +01:00
|
|
|
}
|
2012-05-22 21:44:28 -04:00
|
|
|
_ { none }
|
2012-03-08 16:10:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-18 21:26:25 -07:00
|
|
|
fn substd_enum_variants(cx: ctxt,
|
|
|
|
id: ast::def_id,
|
|
|
|
substs: substs) -> [variant_info] {
|
2012-02-07 21:19:53 -08:00
|
|
|
vec::map(*enum_variants(cx, id)) { |variant_info|
|
|
|
|
let substd_args = vec::map(variant_info.args) {|aty|
|
2012-04-18 21:26:25 -07:00
|
|
|
subst(cx, substs, aty)
|
2012-02-07 21:19:53 -08:00
|
|
|
};
|
|
|
|
|
2012-04-18 21:26:25 -07:00
|
|
|
let substd_ctor_ty = subst(cx, substs, variant_info.ctor_ty);
|
2012-02-07 21:19:53 -08:00
|
|
|
|
|
|
|
@{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-05-15 17:59:55 -07:00
|
|
|
/* If class_id names a class with a dtor, return some(the dtor's id).
|
|
|
|
Otherwise return none. */
|
|
|
|
fn ty_dtor(cx: ctxt, class_id: def_id) -> option<def_id> {
|
|
|
|
if is_local(class_id) {
|
|
|
|
alt cx.items.find(class_id.node) {
|
|
|
|
some(ast_map::node_item(@{node: ast::item_class(_, _, _, _,
|
|
|
|
some(dtor), _), _}, _))
|
|
|
|
{ some(local_def(dtor.node.id)) }
|
2012-05-22 21:44:28 -04:00
|
|
|
_ { none }
|
2012-05-15 17:59:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
csearch::class_dtor(cx.sess.cstore, class_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 17:06:27 -07:00
|
|
|
fn has_dtor(cx: ctxt, class_id: def_id) -> bool {
|
|
|
|
option::is_some(ty_dtor(cx, class_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)]
|
|
|
|
}
|
|
|
|
|
2012-04-10 10:52:06 -07:00
|
|
|
ast_map::node_ctor(nm, _, _, path) {
|
|
|
|
*path + [ast_map::path_name(nm)]
|
2012-03-12 16:31:22 +01:00
|
|
|
}
|
2012-05-14 14:13:32 -07:00
|
|
|
ast_map::node_dtor(_, _, _, path) {
|
|
|
|
*path + [ast_map::path_name("dtor")]
|
|
|
|
}
|
|
|
|
|
2012-03-12 16:31:22 +01:00
|
|
|
|
2012-02-10 06:01:32 -08:00
|
|
|
ast_map::node_expr(_) | ast_map::node_arg(_, _) |
|
2012-03-15 18:46:18 -07:00
|
|
|
ast_map::node_local(_) | ast_map::node_export(_, _) |
|
|
|
|
ast_map::node_block(_) {
|
2012-02-10 06:01:32 -08:00
|
|
|
cx.sess.bug(#fmt["cannot find item_path for node %?", node]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:05:26 -07:00
|
|
|
fn enum_is_univariant(cx: ctxt, id: ast::def_id) -> bool {
|
|
|
|
vec::len(*enum_variants(cx, id)) == 1u
|
|
|
|
}
|
|
|
|
|
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 */ }
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
|
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-04-18 21:26:25 -07:00
|
|
|
ast_map::node_item(@{node: ast::item_enum(variants, _, _), _}, _) {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut 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);
|
2012-04-18 21:26:25 -07:00
|
|
|
let arg_tys = {
|
|
|
|
if vec::len(variant.node.args) > 0u {
|
|
|
|
ty_fn_args(ctor_ty).map { |a| a.ty }
|
|
|
|
} else { [] }
|
|
|
|
};
|
2012-01-16 02:36:47 -07:00
|
|
|
alt variant.node.disr_expr {
|
|
|
|
some (ex) {
|
|
|
|
// FIXME: issue #1417
|
2012-03-22 14:56:56 -07:00
|
|
|
disr_val = alt const_eval::eval_const_expr(cx, ex) {
|
|
|
|
const_eval::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);
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut 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];
|
2012-05-23 00:38:39 -07:00
|
|
|
if ast_util::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
|
|
|
}
|
|
|
|
|
2012-03-19 10:19:00 -07:00
|
|
|
// Look up a field ID, whether or not it's local
|
2012-04-02 10:48:32 -07:00
|
|
|
// Takes a list of type substs in case the class is generic
|
|
|
|
fn lookup_field_type(tcx: ctxt, class_id: def_id, id: def_id,
|
2012-04-18 21:26:25 -07:00
|
|
|
substs: substs) -> ty::t {
|
|
|
|
|
2012-04-02 10:48:32 -07:00
|
|
|
let t = if id.crate == ast::local_crate {
|
2012-03-19 10:19:00 -07:00
|
|
|
node_id_to_type(tcx, id.node)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alt tcx.tcache.find(id) {
|
2012-04-02 10:48:32 -07:00
|
|
|
some(tpt) { tpt.ty }
|
2012-03-19 10:19:00 -07:00
|
|
|
none {
|
|
|
|
let tpt = csearch::get_field_type(tcx, class_id, id);
|
|
|
|
// ok b/c fields are monomorphic
|
|
|
|
// TODO: Comment might be a lie, what if it mentions
|
|
|
|
// class-bound ty params?
|
|
|
|
tcx.tcache.insert(id, tpt);
|
2012-04-02 10:48:32 -07:00
|
|
|
tpt.ty
|
2012-03-19 10:19:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-02 10:48:32 -07:00
|
|
|
};
|
2012-04-18 21:26:25 -07:00
|
|
|
subst(tcx, substs, t)
|
2012-03-19 10:19:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-21 12:42:34 -07:00
|
|
|
// Look up the list of field names and IDs for a given class
|
2012-03-03 17:49:23 -08:00
|
|
|
// Fails if the id is not bound to a class.
|
2012-03-19 10:19:00 -07:00
|
|
|
fn lookup_class_fields(cx: ctxt, did: ast::def_id) -> [field_ty] {
|
2012-03-16 20:36:07 -07:00
|
|
|
if did.crate == ast::local_crate {
|
2012-03-03 17:49:23 -08:00
|
|
|
alt cx.items.find(did.node) {
|
|
|
|
some(ast_map::node_item(i,_)) {
|
2012-03-06 08:02:13 -08:00
|
|
|
alt i.node {
|
2012-05-14 14:13:32 -07:00
|
|
|
ast::item_class(_, _, items, _, _, _) {
|
2012-03-19 10:19:00 -07:00
|
|
|
class_field_tys(items)
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
2012-03-06 08:02:13 -08:00
|
|
|
_ { cx.sess.bug("class ID bound to non-class"); }
|
|
|
|
}
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
2012-04-10 10:52:06 -07:00
|
|
|
_ {
|
|
|
|
cx.sess.bug(#fmt("class ID not bound to an item: %s",
|
|
|
|
ast_map::node_id_to_str(cx.items, did.node)));
|
|
|
|
}
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
2012-03-06 08:02:13 -08:00
|
|
|
}
|
2012-03-16 20:36:07 -07:00
|
|
|
else {
|
2012-03-19 10:19:00 -07:00
|
|
|
ret csearch::get_class_fields(cx, did);
|
2012-03-06 08:02:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:08:48 -07:00
|
|
|
fn lookup_class_field(cx: ctxt, parent: ast::def_id, field_id: ast::def_id)
|
|
|
|
-> field_ty {
|
|
|
|
alt vec::find(lookup_class_fields(cx, parent))
|
|
|
|
{|f| f.id.node == field_id.node} {
|
|
|
|
some(t) { t }
|
|
|
|
none { cx.sess.bug("class ID not found in parent's fields"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 09:59:59 -07:00
|
|
|
fn lookup_public_fields(cx: ctxt, did: ast::def_id) -> [field_ty] {
|
|
|
|
vec::filter(lookup_class_fields(cx, did), is_public)
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn is_public(f: field_ty) -> bool {
|
2012-05-08 16:06:24 +02:00
|
|
|
alt f.vis {
|
|
|
|
public { true }
|
|
|
|
private { false }
|
2012-03-26 09:59:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 12:42:34 -07:00
|
|
|
// Look up the list of method names and IDs for a given class
|
|
|
|
// Fails if the id is not bound to a class.
|
|
|
|
fn lookup_class_method_ids(cx: ctxt, did: ast::def_id)
|
2012-05-08 16:06:24 +02:00
|
|
|
: is_local(did) -> [{name: ident, id: node_id, vis: visibility}] {
|
2012-03-21 12:42:34 -07:00
|
|
|
alt cx.items.find(did.node) {
|
2012-05-14 14:13:32 -07:00
|
|
|
some(ast_map::node_item(@{node: item_class(_,_,items,_,_,_), _}, _)) {
|
2012-03-21 12:42:34 -07:00
|
|
|
let (_,ms) = split_class_items(items);
|
2012-03-28 18:50:33 -07:00
|
|
|
vec::map(ms, {|m| {name: m.ident, id: m.id,
|
2012-05-08 16:06:24 +02:00
|
|
|
vis: m.vis}})
|
2012-03-21 12:42:34 -07:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
cx.sess.bug("lookup_class_method_ids: id not bound to a class");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given a class def_id and a method name, return the method's
|
2012-03-26 09:59:59 -07:00
|
|
|
def_id. Needed so we can do static dispatch for methods
|
2012-03-28 18:50:33 -07:00
|
|
|
Doesn't care about the method's privacy. (It's assumed that
|
|
|
|
the caller already checked that.)
|
|
|
|
*/
|
2012-03-21 12:42:34 -07:00
|
|
|
fn lookup_class_method_by_name(cx:ctxt, did: ast::def_id, name: ident,
|
2012-03-26 09:59:59 -07:00
|
|
|
sp: span) -> def_id {
|
2012-03-21 12:42:34 -07:00
|
|
|
if check is_local(did) {
|
|
|
|
let ms = lookup_class_method_ids(cx, did);
|
2012-04-06 20:01:43 +02:00
|
|
|
for ms.each {|m|
|
2012-03-28 18:50:33 -07:00
|
|
|
if m.name == name {
|
2012-03-21 12:42:34 -07:00
|
|
|
ret ast_util::local_def(m.id);
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 18:50:33 -07:00
|
|
|
cx.sess.span_fatal(sp, #fmt("Class doesn't have a method \
|
2012-03-26 09:59:59 -07:00
|
|
|
named %s", name));
|
2012-03-21 12:42:34 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-03-22 18:03:12 -07:00
|
|
|
csearch::get_class_method(cx.sess.cstore, did, name)
|
2012-03-21 12:42:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-28 18:50:33 -07:00
|
|
|
fn class_field_tys(items: [@class_member]) -> [field_ty] {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut rslt = [];
|
2012-04-06 20:01:43 +02:00
|
|
|
for items.each {|it|
|
2012-03-28 18:50:33 -07:00
|
|
|
alt it.node {
|
2012-05-08 16:06:24 +02:00
|
|
|
instance_var(nm, _, cm, id, vis) {
|
2012-03-26 09:59:59 -07:00
|
|
|
rslt += [{ident: nm, id: ast_util::local_def(id),
|
2012-05-08 16:06:24 +02:00
|
|
|
vis: vis, mutability: cm}];
|
2012-03-06 08:02:13 -08:00
|
|
|
}
|
2012-03-28 18:50:33 -07:00
|
|
|
class_method(_) { }
|
2012-03-06 08:02:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rslt
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a list of fields corresponding to the class's items
|
|
|
|
// (as if the class was a record). trans uses this
|
2012-04-02 10:48:32 -07:00
|
|
|
// Takes a list of substs with which to instantiate field types
|
2012-06-08 14:37:55 -07:00
|
|
|
// Keep in mind that this function reports that all fields are
|
|
|
|
// mutable, regardless of how they were declared. It's meant to
|
|
|
|
// be used in trans.
|
|
|
|
fn class_items_as_mutable_fields(cx:ctxt, did: ast::def_id,
|
|
|
|
substs: substs) -> [field] {
|
|
|
|
class_item_fields(cx, did, substs, {|_mt| m_mutbl})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as class_items_as_mutable_fields, but doesn't change
|
|
|
|
// mutability.
|
2012-04-18 21:26:25 -07:00
|
|
|
fn class_items_as_fields(cx:ctxt, did: ast::def_id,
|
|
|
|
substs: substs) -> [field] {
|
2012-06-08 14:37:55 -07:00
|
|
|
class_item_fields(cx, did, substs, {|mt| alt mt {
|
|
|
|
class_mutable { m_mutbl }
|
|
|
|
class_immutable { m_imm }}})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn class_item_fields(cx:ctxt, did: ast::def_id,
|
|
|
|
substs: substs, frob_mutability: fn(class_mutability) -> mutability)
|
|
|
|
-> [field] {
|
2012-03-15 09:47:03 -04:00
|
|
|
let mut rslt = [];
|
2012-04-06 20:01:43 +02:00
|
|
|
for lookup_class_fields(cx, did).each {|f|
|
2012-03-26 18:35:18 -07:00
|
|
|
// consider all instance vars mut, because the
|
2012-03-19 10:19:00 -07:00
|
|
|
// constructor may mutate all vars
|
2012-04-02 10:48:32 -07:00
|
|
|
rslt += [{ident: f.ident, mt:
|
|
|
|
{ty: lookup_field_type(cx, did, f.id, substs),
|
2012-06-08 14:37:55 -07:00
|
|
|
mutbl: frob_mutability(f.mutability)}}];
|
2012-03-03 17:49:23 -08:00
|
|
|
}
|
|
|
|
rslt
|
|
|
|
}
|
|
|
|
|
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 }
|
2012-05-22 14:59:15 -07:00
|
|
|
ast::shl { opcat_shift }
|
|
|
|
ast::shr { opcat_shift }
|
2012-01-18 22:37:22 -08:00
|
|
|
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 }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_int(_) | ty_uint(_) { tycat_int }
|
2011-12-07 21:06:12 +01:00
|
|
|
ty_float(_) { tycat_float }
|
2012-04-09 17:32:49 -07:00
|
|
|
ty_estr(_) | ty_str { tycat_str }
|
|
|
|
ty_evec(_, _) | ty_vec(_) { tycat_vec }
|
|
|
|
ty_rec(_) | ty_tup(_) | 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-02 10:48:32 -07:00
|
|
|
fn ty_params_to_tys(tcx: ty::ctxt, tps: [ast::ty_param]) -> [t] {
|
|
|
|
vec::from_fn(tps.len(), {|i|
|
|
|
|
ty::mk_param(tcx, i, ast_util::local_def(tps[i].id))
|
|
|
|
})
|
|
|
|
}
|
2012-04-23 16:01:03 -07:00
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
Returns an equivalent type with all the typedefs and self regions removed.
|
|
|
|
"]
|
|
|
|
fn normalize_ty(cx: ctxt, t: t) -> t {
|
2012-04-24 18:23:54 -07:00
|
|
|
alt cx.normalized_cache.find(t) {
|
|
|
|
some(t) { ret t; }
|
|
|
|
none { }
|
|
|
|
}
|
|
|
|
|
2012-04-23 16:01:03 -07:00
|
|
|
let t = alt get(t).struct {
|
|
|
|
ty_enum(did, r) {
|
|
|
|
alt r.self_r {
|
|
|
|
some(_) {
|
|
|
|
// This enum has a self region. Get rid of it
|
2012-05-09 06:09:58 -07:00
|
|
|
mk_enum(cx, did, {self_r: none,
|
|
|
|
self_ty: none,
|
|
|
|
tps: r.tps})
|
2012-04-23 16:01:03 -07:00
|
|
|
}
|
|
|
|
none { t }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { t }
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME #2187: This also reduced int types to their compatible machine
|
|
|
|
// types, which isn't necessary after #2187
|
|
|
|
let t = mk_t(cx, mach_sty(cx.sess.targ_cfg, t));
|
|
|
|
|
|
|
|
let sty = fold_sty(get(t).struct) {|t| normalize_ty(cx, t) };
|
2012-04-24 18:23:54 -07:00
|
|
|
let t_norm = mk_t(cx, sty);
|
|
|
|
cx.normalized_cache.insert(t, t_norm);
|
|
|
|
ret t_norm;
|
2012-04-23 16:01:03 -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:
|