Enforce mutability declarations in classes; correct shapes for classes

1. Enforce mutability declarations on class fields. Don't allow any
mutation of class fields not declared as mutable (except inside the
constructor).

2. Handle classes correctly in shape (treat classes like records).
This commit is contained in:
Tim Chevalier 2012-03-27 22:08:48 -07:00
parent c9102ee7a1
commit edb747ceed
15 changed files with 187 additions and 40 deletions

View file

@ -78,6 +78,7 @@ const tag_path_len: uint = 0x41u;
const tag_path_elt_mod: uint = 0x42u;
const tag_path_elt_name: uint = 0x43u;
const tag_item_field: uint = 0x44u;
const tag_class_mut: uint = 0x45u;
// used to encode crate_ctxt side tables
enum astencode_tag { // Reserves 0x50 -- 0x6f

View file

@ -118,6 +118,18 @@ fn class_member_id(d: ebml::doc, cdata: cmd) -> ast::def_id {
ret translate_def_id(cdata, parse_def_id(ebml::doc_data(tagdoc)));
}
fn field_mutability(d: ebml::doc) -> ast::class_mutability {
// Use maybe_get_doc in case it's a method
option::maybe(ebml::maybe_get_doc(d, tag_class_mut),
ast::class_immutable,
{|d|
alt ebml::doc_as_u8(d) as char {
'm' { ast::class_mutable }
_ { ast::class_immutable }
}
})
}
fn variant_disr_val(d: ebml::doc) -> option<int> {
option::chain(ebml::maybe_get_doc(d, tag_disr_val)) {|val_doc|
int::parse_buf(ebml::doc_data(val_doc), 10u)
@ -435,9 +447,9 @@ fn get_class_members(cdata: cmd, id: ast::node_id,
if p(f) {
let name = item_name(an_item);
let did = class_member_id(an_item, cdata);
let mt = field_mutability(an_item);
result += [{ident: name, id: did, privacy:
// This won't work for methods, argh
family_to_privacy(f)}];
family_to_privacy(f), mutability: mt}];
}
}
result

View file

@ -47,6 +47,13 @@ fn encode_named_def_id(ebml_w: ebml::writer, name: str, id: def_id) {
}
}
fn encode_mutability(ebml_w: ebml::writer, mt: class_mutability) {
ebml_w.wr_tag(tag_class_mut) {||
ebml_w.writer.write([alt mt { class_immutable { 'i' }
class_mutable { 'm' } } as u8]);
}
}
type entry<T> = {val: T, pos: uint};
fn encode_enum_variant_paths(ebml_w: ebml::writer, variants: [variant],
@ -370,7 +377,7 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::writer,
/* We encode both private and public fields -- need to include
private fields to get the offsets right */
alt ci.node.decl {
instance_var(nm, _, _, id) {
instance_var(nm, _, mt, id) {
*index += [{val: id, pos: ebml_w.writer.tell()}];
ebml_w.start_tag(tag_items_data_item);
#debug("encode_info_for_class: doing %s %d", nm, id);
@ -378,7 +385,7 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::writer,
encode_name(ebml_w, nm);
encode_path(ebml_w, path, ast_map::path_name(nm));
encode_type(ecx, ebml_w, node_id_to_type(tcx, id));
/* TODO: mutability */
encode_mutability(ebml_w, mt);
encode_def_id(ebml_w, local_def(id));
ebml_w.end_tag();
}