From a08e589390993d27bf8504c1c8c54586bbfa3d37 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 6 Feb 2012 15:29:56 +0100 Subject: [PATCH] Handle built-in typenames in the resolve pass, rather than in parser Closes #1728 Comments out a section of debuginfo.rs. This code was already broken (only being called when --xg was passed, and only working on trivial programs). --- src/comp/front/test.rs | 5 +- src/comp/middle/debuginfo.rs | 106 +++++++++++++++++++++----------- src/comp/middle/lint.rs | 27 ++++---- src/comp/middle/resolve.rs | 62 +++++++++++++------ src/comp/middle/typeck.rs | 49 ++++++++------- src/comp/syntax/ast.rs | 19 +++--- src/comp/syntax/ast_util.rs | 1 + src/comp/syntax/fold.rs | 3 +- src/comp/syntax/parse/parser.rs | 71 ++++++--------------- src/comp/syntax/print/pprust.rs | 13 +--- 10 files changed, 188 insertions(+), 168 deletions(-) diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs index 5d74165c4b6..779ab5ed9ff 100644 --- a/src/comp/front/test.rs +++ b/src/comp/front/test.rs @@ -377,8 +377,9 @@ fn mk_test_wrapper(cx: test_ctxt, } fn mk_main(cx: test_ctxt) -> @ast::item { - - let args_mt: ast::mt = {ty: @nospan(ast::ty_str), mut: ast::imm}; + let str_pt = @nospan({global: false, idents: ["str"], types: []}); + let str_ty = @nospan(ast::ty_path(str_pt, cx.sess.next_node_id())); + let args_mt: ast::mt = {ty: str_ty, mut: ast::imm}; let args_ty: ast::ty = nospan(ast::ty_vec(args_mt)); let args_arg: ast::arg = diff --git a/src/comp/middle/debuginfo.rs b/src/comp/middle/debuginfo.rs index b5a30d7573e..49304b6bd27 100644 --- a/src/comp/middle/debuginfo.rs +++ b/src/comp/middle/debuginfo.rs @@ -230,8 +230,8 @@ fn create_block(cx: @block_ctxt) -> @metadata { let cx = cx; while option::is_none(cx.block_span) { alt cx.parent { - parent_none { fail "BAD"; /*break;*/ } parent_some(b) { cx = b; } + parent_none { fail; } } } let sp = option::get(cx.block_span); @@ -275,7 +275,7 @@ fn size_and_align_of() -> (int, int) { (sys::size_of::() as int, sys::align_of::() as int) } -fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) +fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: ast::prim_ty, span: span) -> @metadata { let cache = get_cache(cx); let tg = BasicTypeDescriptorTag; @@ -285,7 +285,7 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) option::none {} } - let (name, (size, align), encoding) = alt ty.node { + let (name, (size, align), encoding) = alt ty { ast::ty_bool {("bool", size_and_align_of::(), DW_ATE_boolean)} ast::ty_int(m) { alt m { ast::ty_char {("char", size_and_align_of::(), DW_ATE_unsigned)} @@ -307,11 +307,10 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) ast::ty_f32 {("f32", size_and_align_of::(), DW_ATE_float)} ast::ty_f64 {("f64", size_and_align_of::(), DW_ATE_float)} }} - _ { cx.tcx.sess.span_bug(ty.span, - "create_basic_type: unhandled type"); } + _ { fail; } }; - let fname = filename_from_span(cx, ty.span); + let fname = filename_from_span(cx, span); let file_node = create_file(cx, fname); let cu_node = create_compile_unit(cx, fname); let lldata = [lltag(tg), @@ -415,7 +414,7 @@ fn create_record(cx: @crate_ctxt, t: ty::t, fields: [ast::ty_field], for field in fields { let field_t = ty::get_field(ccx_tcx(cx), t, field.node.ident).mt.ty; let ty_md = create_ty(cx, field_t, field.node.mt.ty); - let (size, align) = member_size_and_align(field.node.mt.ty); + let (size, align) = member_size_and_align(cx.tcx, field.node.mt.ty); add_member(scx, field.node.ident, line_from_span(cx.sess.codemap, field.span) as int, size as int, align as int, ty_md.node); @@ -439,8 +438,8 @@ fn create_boxed_type(cx: @crate_ctxt, outer: ty::t, _inner: ty::t, //let cu_node = create_compile_unit_metadata(cx, fname); let tcx = ccx_tcx(cx); let uint_t = ty::mk_uint(tcx); - let uint_ty = @{node: ast::ty_uint(ast::ty_u), span: span}; - let refcount_type = create_basic_type(cx, uint_t, uint_ty); + let refcount_type = create_basic_type(cx, uint_t, + ast::ty_uint(ast::ty_u), span); let scx = create_structure(file_node, ty_to_str(ccx_tcx(cx), outer), 0); add_member(scx, "refcnt", 0, sys::size_of::() as int, sys::align_of::() as int, refcount_type.node); @@ -492,14 +491,14 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t, let elem_ty_md = create_ty(cx, elem_t, elem_ty); let tcx = ccx_tcx(cx); let scx = create_structure(file_node, ty_to_str(tcx, vec_t), 0); - let uint_ty = @{node: ast::ty_uint(ast::ty_u), span: vec_ty_span}; - let size_t_type = create_basic_type(cx, ty::mk_uint(tcx), uint_ty); + let size_t_type = create_basic_type(cx, ty::mk_uint(tcx), + ast::ty_uint(ast::ty_u), vec_ty_span); add_member(scx, "fill", 0, sys::size_of::() as int, sys::align_of::() as int, size_t_type.node); add_member(scx, "alloc", 0, sys::size_of::() as int, sys::align_of::() as int, size_t_type.node); let subrange = llmdnode([lltag(SubrangeTag), lli64(0), lli64(0)]); - let (arr_size, arr_align) = member_size_and_align(elem_ty); + let (arr_size, arr_align) = member_size_and_align(tcx, elem_ty); let data_ptr = create_composite_type(ArrayTypeTag, "", file_node.node, 0, arr_size, arr_align, 0, option::some(elem_ty_md.node), @@ -510,36 +509,46 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t, ret @{node: llnode, data: {hash: ty::type_id(vec_t)}}; } -fn member_size_and_align(ty: @ast::ty) -> (int, int) { +fn member_size_and_align(tcx: ty::ctxt, ty: @ast::ty) -> (int, int) { alt ty.node { - ast::ty_bool { size_and_align_of::() } - ast::ty_int(m) { alt m { - ast::ty_char { size_and_align_of::() } - ast::ty_i { size_and_align_of::() } - ast::ty_i8 { size_and_align_of::() } - ast::ty_i16 { size_and_align_of::() } - ast::ty_i32 { size_and_align_of::() } - ast::ty_i64 { size_and_align_of::() } - }} - ast::ty_uint(m) { alt m { - ast::ty_u { size_and_align_of::() } - ast::ty_u8 { size_and_align_of::() } - ast::ty_u16 { size_and_align_of::() } - ast::ty_u32 { size_and_align_of::() } - ast::ty_u64 { size_and_align_of::() } - }} - ast::ty_float(m) { alt m { - ast::ty_f { size_and_align_of::() } - ast::ty_f32 { size_and_align_of::() } - ast::ty_f64 { size_and_align_of::() } - }} + ast::ty_path(_, id) { + alt tcx.def_map.get(id) { + ast::def_prim_ty(nty) { + alt nty { + ast::ty_bool { size_and_align_of::() } + ast::ty_int(m) { alt m { + ast::ty_char { size_and_align_of::() } + ast::ty_i { size_and_align_of::() } + ast::ty_i8 { size_and_align_of::() } + ast::ty_i16 { size_and_align_of::() } + ast::ty_i32 { size_and_align_of::() } + ast::ty_i64 { size_and_align_of::() } + }} + ast::ty_uint(m) { alt m { + ast::ty_u { size_and_align_of::() } + ast::ty_u8 { size_and_align_of::() } + ast::ty_u16 { size_and_align_of::() } + ast::ty_u32 { size_and_align_of::() } + ast::ty_u64 { size_and_align_of::() } + }} + ast::ty_float(m) { alt m { + ast::ty_f { size_and_align_of::() } + ast::ty_f32 { size_and_align_of::() } + ast::ty_f64 { size_and_align_of::() } + }} + _ { fail; } + } + } + _ { fail; } + } + } ast::ty_box(_) | ast::ty_uniq(_) { size_and_align_of::() } ast::ty_rec(fields) { let total_size = 0; for field in fields { - let (size, _) = member_size_and_align(field.node.mt.ty); + let (size, _) = member_size_and_align(tcx, field.node.mt.ty); total_size += size; } (total_size, 64) //XXX different align for other arches? @@ -551,7 +560,7 @@ fn member_size_and_align(ty: @ast::ty) -> (int, int) { } } -fn create_ty(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) +fn create_ty(_cx: @crate_ctxt, _t: ty::t, _ty: @ast::ty) -> @metadata { /*let cache = get_cache(cx); alt cached_metadata::<@metadata>( @@ -560,6 +569,19 @@ fn create_ty(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) option::none {} }*/ + /* FIXME I am disabling this code as part of the patch that moves + * recognition of named builtin types into resolve. I tried to fix + * it, but it seems to already be broken -- it's only called when + * --xg is given, and compiling with --xg fails on trivial programs. + * + * Generating an ast::ty from a ty::t seems like it should not be + * needed. It is only done to track spans, but you will not get the + * right spans anyway -- types tend to refer to stuff defined + * elsewhere, not be self-contained. + */ + + fail; + /* fn t_to_ty(cx: @crate_ctxt, t: ty::t, span: span) -> @ast::ty { let ty = alt ty::get(t).struct { ty::ty_nil { ast::ty_nil } @@ -628,8 +650,18 @@ fn t_to_ty(cx: @crate_ctxt, t: ty::t, span: span) -> @ast::ty { ret create_pointer_type(cx, t, ty.span, v); } - _ { ret create_basic_type(cx, t, ty); } + ast::ty_path(_, id) { + alt ccx_tcx(cx).def_map.get(id) { + ast::def_prim_ty(pty) { + ret create_basic_type(cx, t, pty, ty.span); + } + _ {} + } + } + + _ {} }; + */ } fn filename_from_span(cx: @crate_ctxt, sp: codemap::span) -> str { diff --git a/src/comp/middle/lint.rs b/src/comp/middle/lint.rs index f2f5666a346..ff1b97b9587 100644 --- a/src/comp/middle/lint.rs +++ b/src/comp/middle/lint.rs @@ -99,17 +99,22 @@ fn check_native_fn(tcx: ty::ctxt, decl: ast::fn_decl) { let tys = vec::map(decl.inputs) {|a| a.ty }; for ty in (tys + [decl.output]) { alt ty.node { - ast::ty_int(ast::ty_i) { - tcx.sess.span_warn( - ty.span, - "found rust type `int` in native module, while \ - ctypes::c_int or ctypes::long should be used"); - } - ast::ty_uint(ast::ty_u) { - tcx.sess.span_warn( - ty.span, - "found rust type `uint` in native module, while \ - ctypes::c_uint or ctypes::ulong should be used"); + ast::ty_path(_, id) { + alt tcx.def_map.get(id) { + ast::def_prim_ty(ast::ty_int(ast::ty_i)) { + tcx.sess.span_warn( + ty.span, + "found rust type `int` in native module, while \ + ctypes::c_int or ctypes::long should be used"); + } + ast::def_prim_ty(ast::ty_uint(ast::ty_u)) { + tcx.sess.span_warn( + ty.span, + "found rust type `uint` in native module, while \ + ctypes::c_uint or ctypes::ulong should be used"); + } + _ { } + } } _ { } } diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 043e28b57c0..7fbc2ad0c05 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -31,6 +31,7 @@ // them, storing the resulting def in the AST nodes. enum scope { + scope_toplevel, scope_crate, scope_item(@ast::item), scope_bare_fn(ast::fn_decl, node_id, [ast::ty_param]), @@ -44,6 +45,10 @@ enum scope { type scopes = list; +fn top_scope() -> scopes { + cons(scope_crate, @cons(scope_toplevel, @nil)) +} + enum import_state { todo(ast::node_id, ast::ident, @[ast::ident], span, scopes), is_glob(@[ast::ident], scopes, span), @@ -190,7 +195,7 @@ fn map_crate(e: @env, c: @ast::crate) { visit_item: bind index_i(e, _, _, _), visit_block: visit_block_with_scope with *visit::default_visitor::()}; - visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v_map_mod)); + visit::visit_crate(*c, top_scope(), visit::mk_vt(v_map_mod)); // Register the top-level mod e.mod_map.insert(ast::crate_node_id, @@ -257,8 +262,7 @@ fn index_i(e: @env, i: @ast::item, sc: scopes, v: vt) { visit_block: visit_block_with_scope, visit_item: bind visit_item_with_scope(e, _, _, _) with *visit::default_visitor::()}; - visit::visit_crate(*c, cons(scope_crate, @nil), - visit::mk_vt(v_link_glob)); + visit::visit_crate(*c, top_scope(), visit::mk_vt(v_link_glob)); fn link_glob(e: @env, vi: @ast::view_item, sc: scopes, _v: vt) { alt vi.node { //if it really is a glob import, that is @@ -344,7 +348,7 @@ fn resolve_names(e: @env, c: @ast::crate) { visit_constr: bind walk_constr(e, _, _, _, _, _), visit_fn: bind visit_fn_with_scope(e, _, _, _, _, _, _, _) with *visit::default_visitor()}; - visit::visit_crate(*c, cons(scope_crate, @nil), visit::mk_vt(v)); + visit::visit_crate(*c, top_scope(), visit::mk_vt(v)); e.used_imports.track = false; e.sess.abort_if_errors(); @@ -808,10 +812,7 @@ fn lookup_path_strict(e: env, sc: scopes, sp: span, pth: ast::path_, let n_idents = vec::len(pth.idents); let headns = if n_idents == 1u { ns } else { ns_module }; - let first_scope; - if pth.global { - first_scope = list::cons(scope_crate, @list::nil); - } else { first_scope = sc; } + let first_scope = if pth.global { top_scope() } else { sc }; let dcur = lookup_in_scope_strict(e, first_scope, sp, pth.idents[0], headns); @@ -879,6 +880,29 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace) fn in_scope(e: env, sp: span, name: ident, s: scope, ns: namespace) -> option { alt s { + scope_toplevel { + if ns == ns_type { + ret some(ast::def_prim_ty(alt name { + "bool" { ast::ty_bool } + "int" { ast::ty_int(ast::ty_i) } + "uint" { ast::ty_uint(ast::ty_u) } + "float" { ast::ty_float(ast::ty_f) } + "str" { ast::ty_str } + "char" { ast::ty_int(ast::ty_char) } + "i8" { ast::ty_int(ast::ty_i8) } + "i16" { ast::ty_int(ast::ty_i16) } + "i32" { ast::ty_int(ast::ty_i32) } + "i64" { ast::ty_int(ast::ty_i64) } + "u8" { ast::ty_uint(ast::ty_u8) } + "u16" { ast::ty_uint(ast::ty_u16) } + "u32" { ast::ty_uint(ast::ty_u32) } + "u64" { ast::ty_uint(ast::ty_u64) } + "f32" { ast::ty_float(ast::ty_f32) } + "f64" { ast::ty_float(ast::ty_f64) } + _ { ret none; } + })); + } + } scope_crate { ret lookup_in_local_mod(e, ast::crate_node_id, sp, name, ns, inside); @@ -950,7 +974,7 @@ fn in_scope(e: env, sp: span, name: ident, s: scope, ns: namespace) -> } } } - ret none::; + ret none; } let left_fn = false; let closing = []; @@ -959,7 +983,7 @@ fn in_scope(e: env, sp: span, name: ident, s: scope, ns: namespace) -> let sc = sc; while true { alt copy sc { - nil { ret none::; } + nil { ret none; } cons(hd, tl) { let fnd = in_scope(e, sp, name, hd, ns); if !is_none(fnd) { @@ -1019,7 +1043,7 @@ fn lookup_in_ty_params(e: env, name: ident, ty_params: [ast::ty_param]) } { ret some(ast::def_ty_param(local_def(tp.id), n)); } n += 1u; } - ret none::; + ret none; } fn lookup_in_pat(e: env, name: ident, pat: @ast::pat) -> option { @@ -1043,10 +1067,10 @@ fn lookup_in_fn(e: env, name: ident, decl: ast::fn_decl, ret some(ast::def_arg(local_def(a.id), a.mode)); } } - ret none::; + ret none; } ns_type { ret lookup_in_ty_params(e, name, ty_params); } - _ { ret none::; } + _ { ret none; } } } @@ -1261,7 +1285,7 @@ fn lookup_in_local_mod(e: env, node_id: node_id, sp: span, id: ident, let info = e.mod_map.get(node_id); if dr == outside && !is_exported(e, id, option::get(info.m)) { // if we're in a native mod, then dr==inside, so info.m is some _mod - ret none::; // name is not visible + ret none; // name is not visible } alt info.index.find(id) { none { } @@ -1334,7 +1358,7 @@ fn lookup_glob_in_mod(e: env, info: @indexed_mod, sp: span, id: ident, } alt info.glob_imported_names.get(id) { glob_resolving(sp) { - ret none::; + ret none; } glob_resolved(val, typ, md) { ret alt wanted_ns { @@ -1361,7 +1385,7 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) -> ret some(ast::def_variant(local_def(parent_id), local_def(vid))); } - _ { ret none::; } + _ { ret none; } } } mie_native_item(native_item) { @@ -1375,7 +1399,7 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) -> } } } - ret none::; + ret none; } @@ -1473,7 +1497,7 @@ fn ns_for_def(d: def) -> namespace { ast::def_upvar(_, _, _) | ast::def_self(_) { ns_val(ns_any_value) } ast::def_mod(_) | ast::def_native_mod(_) { ns_module } ast::def_ty(_) | ast::def_binding(_) | ast::def_use(_) | - ast::def_ty_param(_, _) { ns_type } + ast::def_ty_param(_, _) | ast::def_prim_ty(_) { ns_type } } } @@ -1497,7 +1521,7 @@ fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) -> e.ext_map.insert(def_id_of_def(d), ids); if ns_ok(ns, ns_for_def(d)) { ret some(d); } } - ret none::; + ret none; } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 3b9c7451c5f..08f67f5f5b8 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -116,7 +116,7 @@ fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) -> let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, id.node)); ret {bounds: @[], ty: typ}; } - ast::def_ty(_) { + ast::def_ty(_) | ast::def_prim_ty(_) { fcx.ccx.tcx.sess.span_fatal(sp, "expected value but found type"); } ast::def_upvar(_, inner, _) { @@ -273,30 +273,24 @@ fn instantiate(tcx: ty::ctxt, sp: span, mode: mode, ty_param_bounds_and_ty.ty); ret typ; } - let typ; - alt ast_ty.node { - ast::ty_nil { typ = ty::mk_nil(tcx); } - ast::ty_bot { typ = ty::mk_bot(tcx); } - ast::ty_bool { typ = ty::mk_bool(tcx); } - ast::ty_int(it) { typ = ty::mk_mach_int(tcx, it); } - ast::ty_uint(uit) { typ = ty::mk_mach_uint(tcx, uit); } - ast::ty_float(ft) { typ = ty::mk_mach_float(tcx, ft); } - ast::ty_str { typ = ty::mk_str(tcx); } + let typ = alt ast_ty.node { + ast::ty_nil { ty::mk_nil(tcx) } + ast::ty_bot { ty::mk_bot(tcx) } ast::ty_box(mt) { - typ = ty::mk_box(tcx, ast_mt_to_mt(tcx, mode, mt)); + ty::mk_box(tcx, ast_mt_to_mt(tcx, mode, mt)) } ast::ty_uniq(mt) { - typ = ty::mk_uniq(tcx, ast_mt_to_mt(tcx, mode, mt)); + ty::mk_uniq(tcx, ast_mt_to_mt(tcx, mode, mt)) } ast::ty_vec(mt) { - typ = ty::mk_vec(tcx, ast_mt_to_mt(tcx, mode, mt)); + ty::mk_vec(tcx, ast_mt_to_mt(tcx, mode, mt)) } ast::ty_ptr(mt) { - typ = ty::mk_ptr(tcx, ast_mt_to_mt(tcx, mode, mt)); + ty::mk_ptr(tcx, ast_mt_to_mt(tcx, mode, mt)) } ast::ty_tup(fields) { let flds = vec::map(fields, bind ast_ty_to_ty(tcx, mode, _)); - typ = ty::mk_tup(tcx, flds); + ty::mk_tup(tcx, flds) } ast::ty_rec(fields) { let flds: [field] = []; @@ -304,22 +298,31 @@ fn instantiate(tcx: ty::ctxt, sp: span, mode: mode, let tm = ast_mt_to_mt(tcx, mode, f.node.mt); flds += [{ident: f.node.ident, mt: tm}]; } - typ = ty::mk_rec(tcx, flds); + ty::mk_rec(tcx, flds) } ast::ty_fn(proto, decl) { - typ = ty::mk_fn(tcx, ty_of_fn_decl(tcx, mode, proto, decl)); + ty::mk_fn(tcx, ty_of_fn_decl(tcx, mode, proto, decl)) } ast::ty_path(path, id) { alt tcx.def_map.get(id) { ast::def_ty(id) { - typ = instantiate(tcx, ast_ty.span, mode, id, path.node.types); + instantiate(tcx, ast_ty.span, mode, id, path.node.types) + } + ast::def_prim_ty(nty) { + alt nty { + ast::ty_bool { ty::mk_bool(tcx) } + ast::ty_int(it) { ty::mk_mach_int(tcx, it) } + ast::ty_uint(uit) { ty::mk_mach_uint(tcx, uit) } + ast::ty_float(ft) { ty::mk_mach_float(tcx, ft) } + ast::ty_str { ty::mk_str(tcx) } + } } ast::def_ty_param(id, n) { if vec::len(path.node.types) > 0u { tcx.sess.span_err(ast_ty.span, "provided type parameters to \ a type parameter"); } - typ = ty::mk_param(tcx, n, id); + ty::mk_param(tcx, n, id) } ast::def_self(iface_id) { alt tcx.items.get(iface_id.node) { @@ -328,9 +331,9 @@ fn instantiate(tcx: ty::ctxt, sp: span, mode: mode, tcx.sess.span_err(ast_ty.span, "incorrect number of type \ parameter to self type"); } - typ = ty::mk_self(tcx, vec::map(path.node.types, {|ast_ty| + ty::mk_self(tcx, vec::map(path.node.types, {|ast_ty| ast_ty_to_ty(tcx, mode, ast_ty) - })); + })) } _ { fail; } } @@ -346,7 +349,7 @@ fn instantiate(tcx: ty::ctxt, sp: span, mode: mode, for constr: @ast::ty_constr in cs { out_cs += [ty::ast_constr_to_constr(tcx, constr)]; } - typ = ty::mk_constr(tcx, ast_ty_to_ty(tcx, mode, t), out_cs); + ty::mk_constr(tcx, ast_ty_to_ty(tcx, mode, t), out_cs) } ast::ty_infer { alt mode { @@ -359,7 +362,7 @@ fn instantiate(tcx: ty::ctxt, sp: span, mode: mode, tcx.sess.span_bug(ast_ty.span, "found `ty_mac` in unexpected place"); } - } + }; tcx.ast_ty_to_ty_cache.insert(ast_ty, some(typ)); ret typ; } diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 7215c538ec1..fd70d642501 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -42,6 +42,7 @@ enum def { def_local(def_id, let_style), def_variant(def_id /* enum */, def_id /* variant */), def_ty(def_id), + def_prim_ty(prim_ty), def_ty_param(def_id, uint), def_binding(def_id), def_use(def_id), @@ -327,20 +328,18 @@ enum float_ty { ty_f, ty_f32, ty_f64, } type ty = spanned; -enum ty_ { - ty_nil, - ty_bot, /* return type of ! functions and type of - ret/fail/break/cont. there is no syntax - for this type. */ - - /* bot represents the value of functions that don't return a value - locally to their context. in contrast, things like log that do - return, but don't return a meaningful value, have result type nil. */ - ty_bool, +// Not represented directly in the AST, referred to by name through a ty_path. +enum prim_ty { ty_int(int_ty), ty_uint(uint_ty), ty_float(float_ty), ty_str, + ty_bool, +} + +enum ty_ { + ty_nil, + ty_bot, /* bottom type */ ty_box(mt), ty_uniq(mt), ty_vec(mt), diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs index 767a2431bd3..6e44b56d0a0 100644 --- a/src/comp/syntax/ast_util.rs +++ b/src/comp/syntax/ast_util.rs @@ -31,6 +31,7 @@ fn def_id_of_def(d: def) -> def_id { def_native_mod(id) | def_const(id) | def_arg(id, _) | def_local(id, _) | def_variant(_, id) | def_ty(id) | def_ty_param(id, _) | def_binding(id) | def_use(id) | def_upvar(id, _, _) { id } + def_prim_ty(_) { fail; } } } diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index 6402571eda5..f7151e7e838 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -446,8 +446,7 @@ fn fold_field(f: ty_field, fld: ast_fold) -> ty_field { span: fld.new_span(f.span)} } alt t { - ty_nil | ty_bot | ty_bool | ty_str {t} - ty_int(_) | ty_uint(_) | ty_float(_) {t} + ty_nil | ty_bot {t} ty_box(mt) {ty_box(fold_mt(mt, fld))} ty_uniq(mt) {ty_uniq(fold_mt(mt, fld))} ty_vec(mt) {ty_vec(fold_mt(mt, fld))} diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index f0375d52c17..65611c2f30f 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -152,7 +152,6 @@ fn bad_expr_word_table() -> hashmap { "assert", "claim", "native", "fn", "pure", "unsafe", "import", "export", "let", "const", "log", "copy", "impl", "iface", "enum", - "m32", "m64", "m128", "f80", "f16", "f128", "class", "trait"] { words.insert(word, ()); } @@ -427,96 +426,62 @@ fn parse_ty(p: parser, colons_before_params: bool) -> @ast::ty { none {} } - let t: ast::ty_; - // FIXME: do something with this - - if eat_word(p, "bool") { - t = ast::ty_bool; - } else if eat_word(p, "int") { - t = ast::ty_int(ast::ty_i); - } else if eat_word(p, "uint") { - t = ast::ty_uint(ast::ty_u); - } else if eat_word(p, "float") { - t = ast::ty_float(ast::ty_f); - } else if eat_word(p, "str") { - t = ast::ty_str; - } else if eat_word(p, "char") { - t = ast::ty_int(ast::ty_char); - } else if eat_word(p, "i8") { - t = ast::ty_int(ast::ty_i8); - } else if eat_word(p, "i16") { - t = ast::ty_int(ast::ty_i16); - } else if eat_word(p, "i32") { - t = ast::ty_int(ast::ty_i32); - } else if eat_word(p, "i64") { - t = ast::ty_int(ast::ty_i64); - } else if eat_word(p, "u8") { - t = ast::ty_uint(ast::ty_u8); - } else if eat_word(p, "u16") { - t = ast::ty_uint(ast::ty_u16); - } else if eat_word(p, "u32") { - t = ast::ty_uint(ast::ty_u32); - } else if eat_word(p, "u64") { - t = ast::ty_uint(ast::ty_u64); - } else if eat_word(p, "f32") { - t = ast::ty_float(ast::ty_f32); - } else if eat_word(p, "f64") { - t = ast::ty_float(ast::ty_f64); - } else if p.token == token::LPAREN { + let t = if p.token == token::LPAREN { p.bump(); if p.token == token::RPAREN { p.bump(); - t = ast::ty_nil; + ast::ty_nil } else { let ts = [parse_ty(p, false)]; while p.token == token::COMMA { p.bump(); ts += [parse_ty(p, false)]; } - if vec::len(ts) == 1u { - t = ts[0].node; - } else { t = ast::ty_tup(ts); } + let t = if vec::len(ts) == 1u { ts[0].node } + else { ast::ty_tup(ts) }; expect(p, token::RPAREN); + t } } else if p.token == token::AT { p.bump(); - t = ast::ty_box(parse_mt(p)); + ast::ty_box(parse_mt(p)) } else if p.token == token::TILDE { p.bump(); - t = ast::ty_uniq(parse_mt(p)); + ast::ty_uniq(parse_mt(p)) } else if p.token == token::BINOP(token::STAR) { p.bump(); - t = ast::ty_ptr(parse_mt(p)); + ast::ty_ptr(parse_mt(p)) } else if p.token == token::LBRACE { let elems = parse_seq(token::LBRACE, token::RBRACE, seq_sep_opt(token::COMMA), parse_ty_field, p); if vec::len(elems.node) == 0u { unexpected(p, token::RBRACE); } let hi = elems.span.hi; - t = ast::ty_rec(elems.node); + + let t = ast::ty_rec(elems.node); if p.token == token::COLON { p.bump(); - t = ast::ty_constr(@spanned(lo, hi, t), - parse_type_constraints(p)); - } + ast::ty_constr(@spanned(lo, hi, t), parse_type_constraints(p)) + } else { t } } else if p.token == token::LBRACKET { expect(p, token::LBRACKET); - t = ast::ty_vec(parse_mt(p)); + let t = ast::ty_vec(parse_mt(p)); expect(p, token::RBRACKET); + t } else if eat_word(p, "fn") { let proto = parse_fn_ty_proto(p); alt proto { ast::proto_bare { p.warn("fn is deprecated, use native fn"); } _ { /* fallthrough */ } } - t = ast::ty_fn(proto, parse_ty_fn(p)); + ast::ty_fn(proto, parse_ty_fn(p)) } else if eat_word(p, "native") { expect_word(p, "fn"); - t = ast::ty_fn(ast::proto_bare, parse_ty_fn(p)); + ast::ty_fn(ast::proto_bare, parse_ty_fn(p)) } else if p.token == token::MOD_SEP || is_ident(p.token) { let path = parse_path(p); - t = ast::ty_path(path, p.get_id()); - } else { p.fatal("expecting type"); } + ast::ty_path(path, p.get_id()) + } else { p.fatal("expecting type"); }; ret parse_ty_postfix(t, p, colons_before_params, lo); } diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 864702d2ba1..121a57b24f7 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -137,7 +137,7 @@ fn test_res_to_str() { let decl: ast::fn_decl = { inputs: [{ mode: ast::expl(ast::by_val), - ty: @ast_util::respan(ast_util::dummy_sp(), ast::ty_bool), + ty: @ast_util::respan(ast_util::dummy_sp(), ast::ty_nil), ident: "b", id: 0 }], @@ -146,7 +146,7 @@ fn test_res_to_str() { cf: ast::return_val, constraints: [] }; - assert res_to_str(decl, "a", []) == "resource a(b: bool)"; + assert res_to_str(decl, "a", []) == "resource a(b: ())"; } fn block_to_str(blk: ast::blk) -> str { @@ -317,16 +317,7 @@ fn print_type(s: ps, &&ty: @ast::ty) { ibox(s, 0u); alt ty.node { ast::ty_nil { word(s.s, "()"); } - ast::ty_bool { word(s.s, "bool"); } ast::ty_bot { word(s.s, "!"); } - ast::ty_int(ast::ty_i) { word(s.s, "int"); } - ast::ty_int(ast::ty_char) { word(s.s, "char"); } - ast::ty_int(t) { word(s.s, ast_util::int_ty_to_str(t)); } - ast::ty_uint(ast::ty_u) { word(s.s, "uint"); } - ast::ty_uint(t) { word(s.s, ast_util::uint_ty_to_str(t)); } - ast::ty_float(ast::ty_f) { word(s.s, "float"); } - ast::ty_float(t) { word(s.s, ast_util::float_ty_to_str(t)); } - ast::ty_str { word(s.s, "str"); } ast::ty_box(mt) { word(s.s, "@"); print_mt(s, mt); } ast::ty_uniq(mt) { word(s.s, "~"); print_mt(s, mt); } ast::ty_vec(mt) {