Skip to content

Commit 7bdab1e

Browse files
committed
Revert "remove ctor from ast"
This reverts commit ed3689d.
1 parent 79603f5 commit 7bdab1e

30 files changed

+651
-120
lines changed

src/libsyntax/ast.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ enum def {
135135
@def, // closed over def
136136
node_id, // expr node that creates the closure
137137
node_id), // id for the block/body of the closure expr
138-
def_class(def_id),
138+
def_class(def_id, bool /* has constructor */),
139139
def_typaram_binder(node_id), /* class, impl or trait that has ty params */
140140
def_region(node_id),
141141
def_label(node_id)
@@ -235,9 +235,9 @@ impl def : cmp::Eq {
235235
_ => false
236236
}
237237
}
238-
def_class(e0a) => {
238+
def_class(e0a, e1a) => {
239239
match (*other) {
240-
def_class(e0b) => e0a == e0b,
240+
def_class(e0b, e1b) => e0a == e0b && e1a == e1b,
241241
_ => false
242242
}
243243
}
@@ -1462,6 +1462,8 @@ type struct_def = {
14621462
fields: ~[@struct_field], /* fields */
14631463
methods: ~[@method], /* methods */
14641464
/* (not including ctor or dtor) */
1465+
/* ctor is optional, and will soon go away */
1466+
ctor: Option<class_ctor>,
14651467
/* dtor is optional */
14661468
dtor: Option<class_dtor>
14671469
};
@@ -1561,6 +1563,7 @@ enum inlined_item {
15611563
ii_item(@item),
15621564
ii_method(def_id /* impl id */, @method),
15631565
ii_foreign(@foreign_item),
1566+
ii_ctor(class_ctor, ident, ~[ty_param], def_id /* parent id */),
15641567
ii_dtor(class_dtor, ident, ~[ty_param], def_id /* parent id */)
15651568
}
15661569

src/libsyntax/ast_map.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ enum ast_node {
7171
// order they are introduced.
7272
node_arg(arg, uint),
7373
node_local(uint),
74+
// Constructor for a class
75+
// def_id is parent id
76+
node_ctor(ident, ~[ty_param], @class_ctor, def_id, @path),
7477
// Destructor for a class
7578
node_dtor(~[ty_param], @class_dtor, def_id, @path),
7679
node_block(blk),
@@ -129,7 +132,7 @@ fn map_decoded_item(diag: span_handler,
129132
// don't decode and instantiate the impl, but just the method, we have to
130133
// add it to the table now:
131134
match ii {
132-
ii_item(*) | ii_dtor(*) => { /* fallthrough */ }
135+
ii_item(*) | ii_ctor(*) | ii_dtor(*) => { /* fallthrough */ }
133136
ii_foreign(i) => {
134137
cx.map.insert(i.id, node_foreign_item(i, foreign_abi_rust_intrinsic,
135138
@path));
@@ -152,6 +155,18 @@ fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
152155
cx.local_id += 1u;
153156
}
154157
match fk {
158+
visit::fk_ctor(nm, attrs, tps, self_id, parent_id) => {
159+
let ct = @{node: {id: id,
160+
attrs: attrs,
161+
self_id: self_id,
162+
dec: /* FIXME (#2543) */ copy decl,
163+
body: /* FIXME (#2543) */ copy body},
164+
span: sp};
165+
cx.map.insert(id, node_ctor(/* FIXME (#2543) */ copy nm,
166+
/* FIXME (#2543) */ copy tps,
167+
ct, parent_id,
168+
@/* FIXME (#2543) */ copy cx.path));
169+
}
155170
visit::fk_dtor(tps, attrs, self_id, parent_id) => {
156171
let dt = @{node: {id: id, attrs: attrs, self_id: self_id,
157172
body: /* FIXME (#2543) */ copy body}, span: sp};
@@ -367,6 +382,9 @@ fn node_id_to_str(map: map, id: node_id, itr: @ident_interner) -> ~str {
367382
Some(node_local(_)) => { // add more info here
368383
fmt!("local (id=%?)", id)
369384
}
385+
Some(node_ctor(*)) => { // add more info here
386+
fmt!("node_ctor (id=%?)", id)
387+
}
370388
Some(node_dtor(*)) => { // add more info here
371389
fmt!("node_dtor (id=%?)", id)
372390
}

src/libsyntax/ast_util.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pure fn def_id_of_def(d: def) -> def_id {
5757
def_fn(id, _) | def_static_method(id, _) | def_mod(id) |
5858
def_foreign_mod(id) | def_const(id) |
5959
def_variant(_, id) | def_ty(id) | def_ty_param(id, _) |
60-
def_use(id) | def_class(id) => {
60+
def_use(id) | def_class(id, _) => {
6161
id
6262
}
6363
def_arg(id, _) | def_local(id, _) | def_self(id) |
@@ -339,6 +339,7 @@ impl inlined_item: inlined_item_utils {
339339
ii_item(i) => /* FIXME (#2543) */ copy i.ident,
340340
ii_foreign(i) => /* FIXME (#2543) */ copy i.ident,
341341
ii_method(_, m) => /* FIXME (#2543) */ copy m.ident,
342+
ii_ctor(_, nm, _, _) => /* FIXME (#2543) */ copy nm,
342343
ii_dtor(_, nm, _, _) => /* FIXME (#2543) */ copy nm
343344
}
344345
}
@@ -348,6 +349,7 @@ impl inlined_item: inlined_item_utils {
348349
ii_item(i) => i.id,
349350
ii_foreign(i) => i.id,
350351
ii_method(_, m) => m.id,
352+
ii_ctor(ctor, _, _, _) => ctor.node.id,
351353
ii_dtor(dtor, _, _, _) => dtor.node.id
352354
}
353355
}
@@ -357,6 +359,9 @@ impl inlined_item: inlined_item_utils {
357359
ii_item(i) => v.visit_item(i, e, v),
358360
ii_foreign(i) => v.visit_foreign_item(i, e, v),
359361
ii_method(_, m) => visit::visit_method_helper(m, e, v),
362+
ii_ctor(ctor, nm, tps, parent_id) => {
363+
visit::visit_class_ctor_helper(ctor, nm, tps, parent_id, e, v);
364+
}
360365
ii_dtor(dtor, _, tps, parent_id) => {
361366
visit::visit_class_dtor_helper(dtor, tps, parent_id, e, v);
362367
}
@@ -490,6 +495,12 @@ fn id_visitor(vfn: fn@(node_id)) -> visit::vt<()> {
490495
vfn(id);
491496

492497
match fk {
498+
visit::fk_ctor(_, _, tps, self_id, parent_id) => {
499+
for vec::each(tps) |tp| { vfn(tp.id); }
500+
vfn(id);
501+
vfn(self_id);
502+
vfn(parent_id.node);
503+
}
493504
visit::fk_dtor(tps, _, self_id, parent_id) => {
494505
for vec::each(tps) |tp| { vfn(tp.id); }
495506
vfn(id);

src/libsyntax/fold.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ fn noop_fold_item_underscore(i: item_, fld: ast_fold) -> item_ {
271271

272272
fn fold_struct_def(struct_def: @ast::struct_def, fld: ast_fold)
273273
-> @ast::struct_def {
274+
let resulting_optional_constructor;
275+
match struct_def.ctor {
276+
None => {
277+
resulting_optional_constructor = None;
278+
}
279+
Some(constructor) => {
280+
resulting_optional_constructor = Some({
281+
node: {
282+
body: fld.fold_block(constructor.node.body),
283+
dec: fold_fn_decl(constructor.node.dec, fld),
284+
id: fld.new_id(constructor.node.id),
285+
.. constructor.node
286+
},
287+
.. constructor
288+
});
289+
}
290+
}
274291
let dtor = do option::map(&struct_def.dtor) |dtor| {
275292
let dtor_body = fld.fold_block(dtor.node.body);
276293
let dtor_id = fld.new_id(dtor.node.id);
@@ -281,6 +298,7 @@ fn fold_struct_def(struct_def: @ast::struct_def, fld: ast_fold)
281298
traits: vec::map(struct_def.traits, |p| fold_trait_ref(*p, fld)),
282299
fields: vec::map(struct_def.fields, |f| fold_struct_field(*f, fld)),
283300
methods: vec::map(struct_def.methods, |m| fld.fold_method(*m)),
301+
ctor: resulting_optional_constructor,
284302
dtor: dtor
285303
};
286304
}
@@ -567,6 +585,7 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
567585
|f| fld.fold_struct_field(*f)),
568586
methods: vec::map(struct_def.methods,
569587
|m| fld.fold_method(*m)),
588+
ctor: None,
570589
dtor: dtor
571590
})
572591
}

src/libsyntax/parse/parser.rs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ enum class_member {
115115
So that we can distinguish a class ctor or dtor
116116
from other class members
117117
*/
118-
enum class_contents { dtor_decl(blk, ~[attribute], codemap::span),
118+
enum class_contents { ctor_decl(fn_decl, ~[attribute], blk, codemap::span),
119+
dtor_decl(blk, ~[attribute], codemap::span),
119120
members(~[@class_member]) }
120121

121122
type arg_or_capture_item = Either<arg, capture_item>;
@@ -2682,13 +2683,30 @@ impl parser {
26822683

26832684
let mut fields: ~[@struct_field];
26842685
let mut methods: ~[@method] = ~[];
2686+
let mut the_ctor: Option<(fn_decl, ~[attribute], blk, codemap::span)>
2687+
= None;
26852688
let mut the_dtor: Option<(blk, ~[attribute], codemap::span)> = None;
2689+
let ctor_id = self.get_id();
26862690

26872691
if self.eat(token::LBRACE) {
26882692
// It's a record-like struct.
26892693
fields = ~[];
26902694
while self.token != token::RBRACE {
26912695
match self.parse_class_item() {
2696+
ctor_decl(a_fn_decl, attrs, blk, s) => {
2697+
match the_ctor {
2698+
Some((_, _, _, s_first)) => {
2699+
self.span_note(s, #fmt("Duplicate constructor \
2700+
declaration for class %s",
2701+
*self.interner.get(class_name)));
2702+
self.span_fatal(copy s_first, ~"First constructor \
2703+
declared here");
2704+
}
2705+
None => {
2706+
the_ctor = Some((a_fn_decl, attrs, blk, s));
2707+
}
2708+
}
2709+
}
26922710
dtor_decl(blk, attrs, s) => {
26932711
match the_dtor {
26942712
Some((_, _, s_first)) => {
@@ -2746,14 +2764,36 @@ impl parser {
27462764
self_id: self.get_id(),
27472765
body: d_body},
27482766
span: d_s}};
2749-
(class_name,
2750-
item_class(@{
2751-
traits: traits,
2752-
fields: move fields,
2753-
methods: move methods,
2754-
dtor: actual_dtor
2755-
}, ty_params),
2756-
None)
2767+
match the_ctor {
2768+
Some((ct_d, ct_attrs, ct_b, ct_s)) => {
2769+
(class_name,
2770+
item_class(@{
2771+
traits: traits,
2772+
fields: move fields,
2773+
methods: move methods,
2774+
ctor: Some({
2775+
node: {id: ctor_id,
2776+
attrs: ct_attrs,
2777+
self_id: self.get_id(),
2778+
dec: ct_d,
2779+
body: ct_b},
2780+
span: ct_s}),
2781+
dtor: actual_dtor
2782+
}, ty_params),
2783+
None)
2784+
}
2785+
None => {
2786+
(class_name,
2787+
item_class(@{
2788+
traits: traits,
2789+
fields: move fields,
2790+
methods: move methods,
2791+
ctor: None,
2792+
dtor: actual_dtor
2793+
}, ty_params),
2794+
None)
2795+
}
2796+
}
27572797
}
27582798

27592799
fn token_is_pound_or_doc_comment(++tok: token::token) -> bool {
@@ -3057,6 +3097,12 @@ impl parser {
30573097
let mut methods: ~[@method] = ~[];
30583098
while self.token != token::RBRACE {
30593099
match self.parse_class_item() {
3100+
ctor_decl(*) => {
3101+
self.span_fatal(copy self.span,
3102+
~"deprecated explicit \
3103+
constructors are not allowed \
3104+
here");
3105+
}
30603106
dtor_decl(blk, attrs, s) => {
30613107
match the_dtor {
30623108
Some((_, _, s_first)) => {
@@ -3097,6 +3143,7 @@ impl parser {
30973143
traits: ~[],
30983144
fields: move fields,
30993145
methods: move methods,
3146+
ctor: None,
31003147
dtor: actual_dtor
31013148
};
31023149
}

src/libsyntax/print/pprust.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,18 @@ fn print_struct(s: ps, struct_def: @ast::struct_def, tps: ~[ast::ty_param],
653653
}
654654
bopen(s);
655655
hardbreak_if_not_bol(s);
656+
do struct_def.ctor.iter |ctor| {
657+
maybe_print_comment(s, ctor.span.lo);
658+
print_outer_attributes(s, ctor.node.attrs);
659+
// Doesn't call head because there shouldn't be a space after new.
660+
cbox(s, indent_unit);
661+
ibox(s, 4);
662+
word(s.s, ~"new(");
663+
print_fn_args(s, ctor.node.dec, ~[], None);
664+
word(s.s, ~")");
665+
space(s.s);
666+
print_block(s, ctor.node.body);
667+
}
656668
do struct_def.dtor.iter |dtor| {
657669
hardbreak_if_not_bol(s);
658670
maybe_print_comment(s, dtor.span.lo);

src/libsyntax/visit.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@ enum fn_kind {
1717
fk_method(ident, ~[ty_param], @method),
1818
fk_anon(proto, capture_clause), //< an anonymous function like fn@(...)
1919
fk_fn_block(capture_clause), //< a block {||...}
20+
fk_ctor(ident, ~[attribute], ~[ty_param], node_id /* self id */,
21+
def_id /* parent class id */), // class constructor
2022
fk_dtor(~[ty_param], ~[attribute], node_id /* self id */,
2123
def_id /* parent class id */) // class destructor
2224

2325
}
2426

2527
fn name_of_fn(fk: fn_kind) -> ident {
2628
match fk {
27-
fk_item_fn(name, _, _) | fk_method(name, _, _) => {
28-
/* FIXME (#2543) */ copy name
29-
}
29+
fk_item_fn(name, _, _) | fk_method(name, _, _)
30+
| fk_ctor(name, _, _, _, _) => /* FIXME (#2543) */ copy name,
3031
fk_anon(*) | fk_fn_block(*) => parse::token::special_idents::anon,
3132
fk_dtor(*) => parse::token::special_idents::dtor
3233
}
3334
}
3435

3536
fn tps_of_fn(fk: fn_kind) -> ~[ty_param] {
3637
match fk {
37-
fk_item_fn(_, tps, _) | fk_method(_, tps, _) |
38-
fk_dtor(tps, _, _, _) => {
39-
/* FIXME (#2543) */ copy tps
40-
}
41-
fk_anon(*) | fk_fn_block(*) => ~[]
38+
fk_item_fn(_, tps, _) | fk_method(_, tps, _)
39+
| fk_ctor(_, _, tps, _, _) | fk_dtor(tps, _, _, _) => {
40+
/* FIXME (#2543) */ copy tps
41+
}
42+
fk_anon(*) | fk_fn_block(*) => ~[]
4243
}
4344
}
4445

@@ -290,6 +291,17 @@ fn visit_method_helper<E>(m: @method, e: E, v: vt<E>) {
290291
m.decl, m.body, m.span, m.id, e, v);
291292
}
292293

294+
// Similar logic to the comment on visit_method_helper - Tim
295+
fn visit_class_ctor_helper<E>(ctor: class_ctor, nm: ident, tps: ~[ty_param],
296+
parent_id: def_id, e: E, v: vt<E>) {
297+
v.visit_fn(fk_ctor(/* FIXME (#2543) */ copy nm,
298+
ctor.node.attrs,
299+
/* FIXME (#2543) */ copy tps,
300+
ctor.node.self_id, parent_id),
301+
ctor.node.dec, ctor.node.body, ctor.span, ctor.node.id, e, v)
302+
303+
}
304+
293305
fn visit_class_dtor_helper<E>(dtor: class_dtor, tps: ~[ty_param],
294306
parent_id: def_id, e: E, v: vt<E>) {
295307
v.visit_fn(fk_dtor(/* FIXME (#2543) */ copy tps, dtor.node.attrs,
@@ -318,7 +330,7 @@ fn visit_trait_method<E>(m: trait_method, e: E, v: vt<E>) {
318330
}
319331
}
320332

321-
fn visit_struct_def<E>(sd: @struct_def, _nm: ast::ident, tps: ~[ty_param],
333+
fn visit_struct_def<E>(sd: @struct_def, nm: ast::ident, tps: ~[ty_param],
322334
id: node_id, e: E, v: vt<E>) {
323335
for sd.fields.each |f| {
324336
v.visit_struct_field(*f, e, v);
@@ -329,6 +341,9 @@ fn visit_struct_def<E>(sd: @struct_def, _nm: ast::ident, tps: ~[ty_param],
329341
for sd.traits.each |p| {
330342
visit_path(p.path, e, v);
331343
}
344+
do option::iter(&sd.ctor) |ctor| {
345+
visit_class_ctor_helper(*ctor, nm, tps, ast_util::local_def(id), e, v);
346+
};
332347
do option::iter(&sd.dtor) |dtor| {
333348
visit_class_dtor_helper(*dtor, tps, ast_util::local_def(id), e, v)
334349
};

0 commit comments

Comments
 (0)