Skip to content

Commit

Permalink
rustc: Implement foreign constants.
Browse files Browse the repository at this point in the history
This is needed for a lot of Apple libraries, as Apple tends to put a lot of
globals in dynamic libraries.
  • Loading branch information
pcwalton committed Aug 25, 2012
1 parent bb5c079 commit 8ef4551
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 71 deletions.
1 change: 1 addition & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ type foreign_item =
#[auto_serialize]
enum foreign_item_ {
foreign_item_fn(fn_decl, purity, ~[ty_param]),
foreign_item_const(@ty)
}

// The data we save and restore about an inlined item or method. This is not
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ fn noop_fold_foreign_item(&&ni: @foreign_item, fld: ast_fold)
purity,
fold_ty_params(typms, fld))
}
foreign_item_const(t) => {
foreign_item_const(fld.fold_ty(t))
}
},
id: fld.new_id(ni.id),
span: fld.new_span(ni.span)};
Expand Down
76 changes: 47 additions & 29 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ import ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
expr_move, expr_path, expr_rec, expr_repeat, expr_ret, expr_swap,
expr_struct, expr_tup, expr_unary, expr_unary_move, expr_vec,
expr_vstore, expr_while, extern_fn, field, fn_decl, foreign_item,
foreign_item_fn, foreign_mod, ident, impure_fn, infer, inherited,
init_assign, init_move, initializer, item, item_,
item_class, item_const, item_enum, item_fn, item_foreign_mod,
item_impl, item_mac, item_mod, item_trait, item_ty, lit, lit_,
lit_bool, lit_float, lit_int, lit_int_unsuffixed, lit_nil,
lit_str, lit_uint, local, m_const, m_imm, m_mutbl, mac_, mac_aq,
mac_ellipsis, mac_invoc, mac_invoc_tt, mac_var, matcher,
match_nonterminal, match_seq, match_tok, method, mode, mt, mul,
mutability, named_field, neg, noreturn, not, pat, pat_box,
pat_enum, pat_ident, pat_lit, pat_range, pat_rec, pat_struct,
pat_tup, pat_uniq, pat_wild, path, private, proto, proto_bare,
proto_block, proto_box, proto_uniq, provided, public, pure_fn,
purity, re_anon, re_named, region, rem, required, ret_style,
return_val, self_ty, shl, shr, stmt, stmt_decl, stmt_expr,
stmt_semi, struct_def, struct_field, struct_variant_kind,
subtract, sty_box, sty_by_ref, sty_region, sty_static, sty_uniq,
sty_value, token_tree, trait_method, trait_ref, tt_delim, tt_seq,
tt_tok, tt_nonterminal, ty, ty_, ty_bot, ty_box, ty_field, ty_fn,
ty_infer, ty_mac, ty_method, ty_nil, ty_param, ty_param_bound,
ty_path, ty_ptr, ty_rec, ty_rptr, ty_tup, ty_u32, ty_uniq,
ty_vec, ty_fixed_length, tuple_variant_kind, unchecked_blk, uniq,
unnamed_field, unsafe_blk, unsafe_fn, variant, view_item,
view_item_, view_item_export, view_item_import, view_item_use,
view_path, view_path_glob, view_path_list, view_path_simple,
visibility, vstore, vstore_box, vstore_fixed, vstore_slice,
vstore_uniq};
foreign_item_const, foreign_item_fn, foreign_mod, ident,
impure_fn, infer, inherited, init_assign, init_move, initializer,
item, item_, item_class, item_const, item_enum, item_fn,
item_foreign_mod, item_impl, item_mac, item_mod, item_trait,
item_ty, lit, lit_, lit_bool, lit_float, lit_int,
lit_int_unsuffixed, lit_nil, lit_str, lit_uint, local, m_const,
m_imm, m_mutbl, mac_, mac_aq, mac_ellipsis, mac_invoc,
mac_invoc_tt, mac_var, matcher, match_nonterminal, match_seq,
match_tok, method, mode, mt, mul, mutability, named_field, neg,
noreturn, not, pat, pat_box, pat_enum, pat_ident, pat_lit,
pat_range, pat_rec, pat_struct, pat_tup, pat_uniq, pat_wild,
path, private, proto, proto_bare, proto_block, proto_box,
proto_uniq, provided, public, pure_fn, purity, re_anon, re_named,
region, rem, required, ret_style, return_val, self_ty, shl, shr,
stmt, stmt_decl, stmt_expr, stmt_semi, struct_def, struct_field,
struct_variant_kind, subtract, sty_box, sty_by_ref, sty_region,
sty_static, sty_uniq, sty_value, token_tree, trait_method,
trait_ref, tt_delim, tt_seq, tt_tok, tt_nonterminal, ty, ty_,
ty_bot, ty_box, ty_field, ty_fn, ty_infer, ty_mac, ty_method,
ty_nil, ty_param, ty_param_bound, ty_path, ty_ptr, ty_rec,
ty_rptr, ty_tup, ty_u32, ty_uniq, ty_vec, ty_fixed_length,
tuple_variant_kind, unchecked_blk, uniq, unnamed_field,
unsafe_blk, unsafe_fn, variant, view_item, view_item_,
view_item_export, view_item_import, view_item_use, view_path,
view_path_glob, view_path_list, view_path_simple, visibility,
vstore, vstore_box, vstore_fixed, vstore_slice, vstore_uniq};

export file_type;
export parser;
Expand Down Expand Up @@ -2843,6 +2843,21 @@ struct parser {
span: mk_sp(lo, hi)};
}

fn parse_item_foreign_const(+attrs: ~[attribute]) -> @foreign_item {
let lo = self.span.lo;
self.expect_keyword(~"const");
let ident = self.parse_ident();
self.expect(token::COLON);
let ty = self.parse_ty(false);
let hi = self.span.hi;
self.expect(token::SEMI);
return @{ident: ident,
attrs: attrs,
node: foreign_item_const(move ty),
id: self.get_id(),
span: mk_sp(lo, hi)};
}

fn parse_fn_purity() -> purity {
if self.eat_keyword(~"fn") { impure_fn }
else if self.eat_keyword(~"pure") {
Expand All @@ -2855,9 +2870,12 @@ struct parser {
else { self.unexpected(); }
}

fn parse_foreign_item(+attrs: ~[attribute]) ->
@foreign_item {
self.parse_item_foreign_fn(attrs)
fn parse_foreign_item(+attrs: ~[attribute]) -> @foreign_item {
if self.is_keyword(~"const") {
self.parse_item_foreign_const(move attrs)
} else {
self.parse_item_foreign_fn(move attrs)
}
}

fn parse_foreign_mod_items(+first_item_attrs: ~[attribute]) ->
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ fn print_foreign_item(s: ps, item: @ast::foreign_item) {
word(s.s, ~";");
end(s); // end the outer fn box
}
ast::foreign_item_const(t) => {
head(s, ~"const");
print_ident(s, item.ident);
word_space(s, ~":");
print_type(s, t);
word(s.s, ~";");
end(s); // end the head-ibox
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ fn visit_foreign_item<E>(ni: @foreign_item, e: E, v: vt<E>) {
v.visit_ty_params(tps, e, v);
visit_fn_decl(fd, e, v);
}
foreign_item_const(t) => {
v.visit_ty(t, e, v);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/rustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,14 @@ fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
}
encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
}
foreign_item_const(t) => {
encode_def_id(ebml_w, local_def(nitem.id));
encode_family(ebml_w, 'c');
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id));
encode_symbol(ecx, ebml_w, nitem.id);
encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
ebml_w.end_tag();
}
}
ebml_w.end_tag();
}
Expand Down
1 change: 1 addition & 0 deletions src/rustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ fn check_item_ctypes(cx: ty::ctxt, it: @ast::item) {
ast::foreign_item_fn(decl, _, tps) => {
check_foreign_fn(cx, it.id, decl);
}
ast::foreign_item_const(*) => {} // XXX: Not implemented.
}
}
}
Expand Down
55 changes: 30 additions & 25 deletions src/rustc/middle/resolve3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ import syntax::ast::{enum_variant_kind, expr, expr_again, expr_assign_op};
import syntax::ast::{expr_binary, expr_break, expr_cast, expr_field, expr_fn};
import syntax::ast::{expr_fn_block, expr_index, expr_loop};
import syntax::ast::{expr_path, expr_struct, expr_unary, fn_decl};
import syntax::ast::{foreign_item, foreign_item_fn, ge, gt, ident, trait_ref};
import syntax::ast::{impure_fn, inherited, item, item_class, item_const};
import syntax::ast::{item_enum, item_fn, item_mac, item_foreign_mod};
import syntax::ast::{item_impl, item_mod, item_trait, item_ty, le, local};
import syntax::ast::{local_crate, lt, method, mul, ne, neg, node_id, pat};
import syntax::ast::{pat_enum, pat_ident, path, prim_ty, pat_box, pat_uniq};
import syntax::ast::{foreign_item, foreign_item_const, foreign_item_fn, ge};
import syntax::ast::{gt, ident, impure_fn, inherited, item, item_class};
import syntax::ast::{item_const, item_enum, item_fn, item_foreign_mod};
import syntax::ast::{item_impl, item_mac, item_mod, item_trait, item_ty, le};
import syntax::ast::{local, local_crate, lt, method, mul, ne, neg, node_id};
import syntax::ast::{pat, pat_enum, pat_ident, path, prim_ty, pat_box};
import syntax::ast::{pat_lit, pat_range, pat_rec, pat_struct, pat_tup};
import syntax::ast::{pat_wild, private, provided, public, required, rem};
import syntax::ast::{self_ty_};
import syntax::ast::{shl, stmt_decl, struct_field, struct_variant_kind};
import syntax::ast::{sty_static, subtract, tuple_variant_kind, ty};
import syntax::ast::{ty_bool, ty_char, ty_f, ty_f32, ty_f64, ty_float, ty_i};
import syntax::ast::{ty_i16, ty_i32, ty_i64, ty_i8, ty_int, ty_param};
import syntax::ast::{ty_path, ty_str, ty_u, ty_u16, ty_u32, ty_u64, ty_u8};
import syntax::ast::{ty_uint, variant, view_item, view_item_export};
import syntax::ast::{view_item_import, view_item_use, view_path_glob};
import syntax::ast::{view_path_list, view_path_simple, visibility};
import syntax::ast::{pat_uniq, pat_wild, private, provided, public, required};
import syntax::ast::{rem, self_ty_, shl, stmt_decl, struct_field};
import syntax::ast::{struct_variant_kind, sty_static, subtract, trait_ref};
import syntax::ast::{tuple_variant_kind, ty, ty_bool, ty_char, ty_f, ty_f32};
import syntax::ast::{ty_f64, ty_float, ty_i, ty_i16, ty_i32, ty_i64, ty_i8};
import syntax::ast::{ty_int, ty_param, ty_path, ty_str, ty_u, ty_u16, ty_u32};
import syntax::ast::{ty_u64, ty_u8, ty_uint, variant, view_item};
import syntax::ast::{view_item_export, view_item_import, view_item_use};
import syntax::ast::{view_path_glob, view_path_list, view_path_simple};
import syntax::ast::{visibility};
import syntax::ast_util::{def_id_of_def, dummy_sp, local_def, new_def_hash};
import syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
import syntax::attr::{attr_metas, contains_name};
Expand Down Expand Up @@ -1213,26 +1213,27 @@ struct Resolver {
vt<ReducedGraphParent>) {

let name = foreign_item.ident;
let (name_bindings, new_parent) =
self.add_child(name, parent, ~[ValueNS], foreign_item.span);

match foreign_item.node {
foreign_item_fn(fn_decl, purity, type_parameters) => {
let (name_bindings, new_parent) = self.add_child(name, parent,
~[ValueNS], foreign_item.span);

let def = def_fn(local_def(foreign_item.id), purity);
(*name_bindings).define_value(Public, def, foreign_item.span);

do self.with_type_parameter_rib
(HasTypeParameters(&type_parameters,
foreign_item.id,
0u,
NormalRibKind)) || {

(HasTypeParameters(&type_parameters, foreign_item.id,
0u, NormalRibKind)) {
visit_foreign_item(foreign_item, new_parent, visitor);
}
}
}
foreign_item_const(item_type) => {
let def = def_const(local_def(foreign_item.id));
(*name_bindings).define_value(Public, def, foreign_item.span);

visit_foreign_item(foreign_item, new_parent, visitor);
}
}
}

fn build_reduced_graph_for_block(block: blk,
Expand Down Expand Up @@ -2953,6 +2954,10 @@ struct Resolver {
visitor);
}
}
foreign_item_const(item_type) => {
visit_foreign_item(foreign_item, (),
visitor);
}
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/rustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5414,9 +5414,22 @@ fn get_item_val(ccx: @crate_ctxt, id: ast::node_id) -> ValueRef {
}
ast_map::node_foreign_item(ni, _, pth) => {
exprt = true;
register_fn(ccx, ni.span,
vec::append(*pth, ~[path_name(ni.ident)]),
ni.id)
match ni.node {
ast::foreign_item_fn(*) => {
register_fn(ccx, ni.span,
vec::append(*pth, ~[path_name(ni.ident)]),
ni.id)
}
ast::foreign_item_const(*) => {
let typ = ty::node_id_to_type(ccx.tcx, ni.id);
let ident = ccx.sess.parse_sess.interner.get(ni.ident);
let g = do str::as_c_str(*ident) |buf| {
llvm::LLVMAddGlobal(ccx.llmod, type_of(ccx, typ), buf)
};
ccx.item_symbols.insert(ni.id, copy *ident);
g
}
}
}
ast_map::node_ctor(nm, _, ctor, _, pt) => {
let my_path = vec::append(*pt, ~[path_name(nm)]);
Expand Down
1 change: 1 addition & 0 deletions src/rustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ fn trans_foreign_mod(ccx: @crate_ctxt,
}
}
}
ast::foreign_item_const(*) => {}
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/rustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,8 @@ fn convert_foreign(ccx: @crate_ctxt, i: @ast::foreign_item) {
// type of the foreign item. We simply write it into the node type
// table.
let tpt = ty_of_foreign_item(ccx, i);
match i.node {
ast::foreign_item_fn(*) => {
write_ty_to_tcx(ccx.tcx, i.id, tpt.ty);
ccx.tcx.tcache.insert(local_def(i.id), tpt);
}
}
write_ty_to_tcx(ccx.tcx, i.id, tpt.ty);
ccx.tcx.tcache.insert(local_def(i.id), tpt);
}

fn ty_of_method(ccx: @crate_ctxt,
Expand Down Expand Up @@ -695,6 +691,14 @@ fn ty_of_foreign_item(ccx: @crate_ctxt, it: @ast::foreign_item)
return ty_of_foreign_fn_decl(ccx, fn_decl, purity, params,
local_def(it.id));
}
ast::foreign_item_const(t) => {
let rb = in_binding_rscope(empty_rscope);
return {
bounds: @~[],
region_param: none,
ty: ast_ty_to_ty(ccx, rb, t)
};
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/rustdoc/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,19 @@ fn nmoddoc_from_mod(
itemdoc: doc::itemdoc,
module_: ast::foreign_mod
) -> doc::nmoddoc {
let mut fns = ~[];
for module_.items.each |item| {
let itemdoc = mk_itemdoc(item.id, to_str(item.ident));
match item.node {
ast::foreign_item_fn(*) => {
vec::push(fns, fndoc_from_fn(itemdoc));
}
ast::foreign_item_const(*) => {} // XXX: Not implemented.
}
}
{
item: itemdoc,
fns: do vec::map(module_.items) |item| {
let itemdoc = mk_itemdoc(item.id, to_str(item.ident));
match item.node {
ast::foreign_item_fn(*) => {
fndoc_from_fn(itemdoc)
}
}
},
fns: fns,
index: none
}
}
Expand Down

0 comments on commit 8ef4551

Please sign in to comment.