2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-25 20:36:18 -05:00
|
|
|
// Detecting language items.
|
|
|
|
//
|
|
|
|
// Language items are items that represent concepts intrinsic to the language
|
|
|
|
// itself. Examples are:
|
|
|
|
//
|
2013-07-10 19:31:53 -05:00
|
|
|
// * Traits that specify "kinds"; e.g. "Freeze", "Send".
|
2012-07-25 20:36:18 -05:00
|
|
|
//
|
2013-06-05 16:52:27 -05:00
|
|
|
// * Traits that represent operators; e.g. "Add", "Sub", "Index".
|
2012-07-25 20:36:18 -05:00
|
|
|
//
|
|
|
|
// * Functions called by the compiler itself.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
use driver::session::Session;
|
2013-03-26 15:38:07 -05:00
|
|
|
use metadata::csearch::each_lang_item;
|
|
|
|
use metadata::cstore::iter_crate_data;
|
2013-12-11 14:40:51 -06:00
|
|
|
use middle::ty::{BuiltinBound, BoundFreeze, BoundPod, BoundSend, BoundSized};
|
2013-10-22 17:13:18 -05:00
|
|
|
use syntax::ast;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::ast_util::local_def;
|
2013-07-19 06:51:37 -05:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2013-08-13 08:30:08 -05:00
|
|
|
use syntax::visit;
|
|
|
|
use syntax::visit::Visitor;
|
2012-07-25 20:36:18 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::HashMap;
|
2013-09-29 19:34:36 -05:00
|
|
|
use std::iter::Enumerate;
|
|
|
|
use std::vec;
|
2012-07-25 20:36:18 -05:00
|
|
|
|
2013-12-05 22:24:25 -06:00
|
|
|
// The actual lang items defined come at the end of this file in one handy table.
|
|
|
|
// So you probably just want to nip down to the end.
|
|
|
|
macro_rules! lets_do_this {
|
|
|
|
(
|
|
|
|
There are $num_lang_items:expr lang items.
|
|
|
|
$( $num:pat, $variant:ident, $name:expr, $method:ident; )*
|
|
|
|
) => {
|
|
|
|
|
2013-01-06 14:05:34 -06:00
|
|
|
pub enum LangItem {
|
2013-12-05 22:24:25 -06:00
|
|
|
$($variant),*
|
2013-01-06 14:05:34 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub struct LanguageItems {
|
2013-12-05 22:24:25 -06:00
|
|
|
items: [Option<ast::DefId>, ..$num_lang_items]
|
2012-08-27 16:08:37 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl LanguageItems {
|
2013-03-21 21:07:54 -05:00
|
|
|
pub fn new() -> LanguageItems {
|
2012-08-27 16:08:37 -05:00
|
|
|
LanguageItems {
|
2013-12-05 22:24:25 -06:00
|
|
|
items: [ None, ..$num_lang_items ]
|
2012-08-27 16:08:37 -05:00
|
|
|
}
|
2012-07-25 20:36:18 -05:00
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn items<'a>(&'a self) -> Enumerate<vec::VecIterator<'a, Option<ast::DefId>>> {
|
2013-09-29 19:34:36 -05:00
|
|
|
self.items.iter().enumerate()
|
2013-05-03 12:08:08 -05:00
|
|
|
}
|
2013-01-07 12:51:53 -06:00
|
|
|
|
2013-03-21 21:07:54 -05:00
|
|
|
pub fn item_name(index: uint) -> &'static str {
|
2013-01-07 12:51:53 -06:00
|
|
|
match index {
|
2013-12-05 22:24:25 -06:00
|
|
|
$( $num => $name, )*
|
2013-01-07 12:51:53 -06:00
|
|
|
_ => "???"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn require(&self, it: LangItem) -> Result<ast::DefId, ~str> {
|
2013-07-15 22:42:13 -05:00
|
|
|
match self.items[it as uint] {
|
|
|
|
Some(id) => Ok(id),
|
2013-09-28 00:38:08 -05:00
|
|
|
None => Err(format!("requires `{}` lang_item",
|
2013-07-15 22:42:13 -05:00
|
|
|
LanguageItems::item_name(it as uint)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn to_builtin_kind(&self, id: ast::DefId) -> Option<BuiltinBound> {
|
2013-08-16 15:57:42 -05:00
|
|
|
if Some(id) == self.freeze_trait() {
|
|
|
|
Some(BoundFreeze)
|
|
|
|
} else if Some(id) == self.send_trait() {
|
|
|
|
Some(BoundSend)
|
|
|
|
} else if Some(id) == self.sized_trait() {
|
|
|
|
Some(BoundSized)
|
2013-12-11 14:40:51 -06:00
|
|
|
} else if Some(id) == self.pod_trait() {
|
|
|
|
Some(BoundPod)
|
2013-08-16 15:57:42 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2013-08-14 17:13:16 -05:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:24:25 -06:00
|
|
|
$(
|
|
|
|
pub fn $method(&self) -> Option<ast::DefId> {
|
|
|
|
self.items[$variant as uint]
|
|
|
|
}
|
|
|
|
)*
|
2013-01-06 14:05:34 -06:00
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
struct LanguageItemCollector {
|
2013-03-15 14:24:24 -05:00
|
|
|
items: LanguageItems,
|
2012-07-25 20:36:18 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
session: Session,
|
2012-07-25 20:36:18 -05:00
|
|
|
|
2013-09-29 19:34:23 -05:00
|
|
|
item_refs: HashMap<&'static str, uint>,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
struct LanguageItemVisitor<'a> {
|
|
|
|
this: &'a mut LanguageItemCollector,
|
2013-08-13 08:30:08 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Visitor<()> for LanguageItemVisitor<'a> {
|
2013-10-22 17:13:18 -05:00
|
|
|
fn visit_item(&mut self, item: @ast::item, _: ()) {
|
|
|
|
match extract(item.attrs) {
|
|
|
|
Some(value) => {
|
|
|
|
let item_index = self.this.item_refs.find_equiv(&value).map(|x| *x);
|
|
|
|
|
|
|
|
match item_index {
|
|
|
|
Some(item_index) => {
|
|
|
|
self.this.collect_item(item_index, local_def(item.id))
|
2013-08-13 08:30:08 -05:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
None => {}
|
2013-08-13 08:30:08 -05:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2013-08-13 08:30:08 -05:00
|
|
|
|
|
|
|
visit::walk_item(self, item, ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
impl LanguageItemCollector {
|
|
|
|
pub fn new(session: Session) -> LanguageItemCollector {
|
2013-06-27 08:04:22 -05:00
|
|
|
let mut item_refs = HashMap::new();
|
|
|
|
|
2013-12-05 22:24:25 -06:00
|
|
|
$( item_refs.insert($name, $variant as uint); )*
|
2013-06-27 08:04:22 -05:00
|
|
|
|
|
|
|
LanguageItemCollector {
|
|
|
|
session: session,
|
|
|
|
items: LanguageItems::new(),
|
|
|
|
item_refs: item_refs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn collect_item(&mut self, item_index: uint, item_def_id: ast::DefId) {
|
2013-01-07 12:51:53 -06:00
|
|
|
// Check for duplicates.
|
|
|
|
match self.items.items[item_index] {
|
|
|
|
Some(original_def_id) if original_def_id != item_def_id => {
|
2013-09-28 00:38:08 -05:00
|
|
|
self.session.err(format!("duplicate entry for `{}`",
|
2013-01-07 12:51:53 -06:00
|
|
|
LanguageItems::item_name(item_index)));
|
|
|
|
}
|
|
|
|
Some(_) | None => {
|
|
|
|
// OK.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matched.
|
|
|
|
self.items.items[item_index] = Some(item_def_id);
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn collect_local_language_items(&mut self, crate: &ast::Crate) {
|
|
|
|
let mut v = LanguageItemVisitor { this: self };
|
|
|
|
visit::walk_crate(&mut v, crate, ());
|
2012-07-25 20:36:18 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn collect_external_language_items(&mut self) {
|
2012-07-25 20:36:18 -05:00
|
|
|
let crate_store = self.session.cstore;
|
2013-11-21 17:42:55 -06:00
|
|
|
iter_crate_data(crate_store, |crate_number, _crate_metadata| {
|
|
|
|
each_lang_item(crate_store, crate_number, |node_id, item_index| {
|
2013-10-22 17:13:18 -05:00
|
|
|
let def_id = ast::DefId { crate: crate_number, node: node_id };
|
2013-01-07 12:51:53 -06:00
|
|
|
self.collect_item(item_index, def_id);
|
2013-08-02 01:17:20 -05:00
|
|
|
true
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
|
|
|
})
|
2012-07-25 20:36:18 -05:00
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn collect(&mut self, crate: &ast::Crate) {
|
|
|
|
self.collect_local_language_items(crate);
|
2012-07-25 20:36:18 -05:00
|
|
|
self.collect_external_language_items();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
pub fn extract(attrs: &[ast::Attribute]) -> Option<@str> {
|
|
|
|
for attribute in attrs.iter() {
|
|
|
|
match attribute.name_str_pair() {
|
|
|
|
Some((key, value)) if "lang" == key => {
|
|
|
|
return Some(value);
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(..) | None => {}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect_language_items(crate: &ast::Crate,
|
2013-01-30 15:44:24 -06:00
|
|
|
session: Session)
|
|
|
|
-> LanguageItems {
|
2013-10-22 17:13:18 -05:00
|
|
|
let mut collector = LanguageItemCollector::new(session);
|
|
|
|
collector.collect(crate);
|
2013-11-28 14:22:53 -06:00
|
|
|
let LanguageItemCollector { items, .. } = collector;
|
2013-06-14 19:55:38 -05:00
|
|
|
session.abort_if_errors();
|
2013-03-15 14:24:24 -05:00
|
|
|
items
|
2012-07-25 20:36:18 -05:00
|
|
|
}
|
2013-12-05 22:24:25 -06:00
|
|
|
|
|
|
|
// End of the macro
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lets_do_this! {
|
2013-12-15 19:17:07 -06:00
|
|
|
There are 43 lang items.
|
2013-12-05 22:24:25 -06:00
|
|
|
|
|
|
|
// ID, Variant name, Name, Method name;
|
|
|
|
0, FreezeTraitLangItem, "freeze", freeze_trait;
|
|
|
|
1, SendTraitLangItem, "send", send_trait;
|
|
|
|
2, SizedTraitLangItem, "sized", sized_trait;
|
2013-12-11 14:40:51 -06:00
|
|
|
3, PodTraitLangItem, "pod", pod_trait;
|
|
|
|
|
|
|
|
4, DropTraitLangItem, "drop", drop_trait;
|
|
|
|
|
|
|
|
5, AddTraitLangItem, "add", add_trait;
|
|
|
|
6, SubTraitLangItem, "sub", sub_trait;
|
|
|
|
7, MulTraitLangItem, "mul", mul_trait;
|
|
|
|
8, DivTraitLangItem, "div", div_trait;
|
|
|
|
9, RemTraitLangItem, "rem", rem_trait;
|
|
|
|
10, NegTraitLangItem, "neg", neg_trait;
|
|
|
|
11, NotTraitLangItem, "not", not_trait;
|
|
|
|
12, BitXorTraitLangItem, "bitxor", bitxor_trait;
|
|
|
|
13, BitAndTraitLangItem, "bitand", bitand_trait;
|
|
|
|
14, BitOrTraitLangItem, "bitor", bitor_trait;
|
|
|
|
15, ShlTraitLangItem, "shl", shl_trait;
|
|
|
|
16, ShrTraitLangItem, "shr", shr_trait;
|
|
|
|
17, IndexTraitLangItem, "index", index_trait;
|
|
|
|
|
|
|
|
18, EqTraitLangItem, "eq", eq_trait;
|
|
|
|
19, OrdTraitLangItem, "ord", ord_trait;
|
|
|
|
|
|
|
|
20, StrEqFnLangItem, "str_eq", str_eq_fn;
|
|
|
|
21, UniqStrEqFnLangItem, "uniq_str_eq", uniq_str_eq_fn;
|
|
|
|
22, FailFnLangItem, "fail_", fail_fn;
|
|
|
|
23, FailBoundsCheckFnLangItem, "fail_bounds_check", fail_bounds_check_fn;
|
|
|
|
24, ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
|
|
|
|
25, ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
|
|
|
|
26, ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
|
|
|
|
27, MallocFnLangItem, "malloc", malloc_fn;
|
|
|
|
28, FreeFnLangItem, "free", free_fn;
|
|
|
|
29, BorrowAsImmFnLangItem, "borrow_as_imm", borrow_as_imm_fn;
|
|
|
|
30, BorrowAsMutFnLangItem, "borrow_as_mut", borrow_as_mut_fn;
|
|
|
|
31, ReturnToMutFnLangItem, "return_to_mut", return_to_mut_fn;
|
|
|
|
32, CheckNotBorrowedFnLangItem, "check_not_borrowed", check_not_borrowed_fn;
|
|
|
|
33, StrDupUniqFnLangItem, "strdup_uniq", strdup_uniq_fn;
|
|
|
|
34, RecordBorrowFnLangItem, "record_borrow", record_borrow_fn;
|
|
|
|
35, UnrecordBorrowFnLangItem, "unrecord_borrow", unrecord_borrow_fn;
|
|
|
|
|
|
|
|
36, StartFnLangItem, "start", start_fn;
|
|
|
|
|
|
|
|
37, TyDescStructLangItem, "ty_desc", ty_desc;
|
|
|
|
38, TyVisitorTraitLangItem, "ty_visitor", ty_visitor;
|
|
|
|
39, OpaqueStructLangItem, "opaque", opaque;
|
|
|
|
|
|
|
|
40, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;
|
|
|
|
|
|
|
|
41, TypeIdLangItem, "type_id", type_id;
|
2013-12-15 19:17:07 -06:00
|
|
|
|
|
|
|
42, EhPersonalityLangItem, "eh_personality", eh_personality_fn;
|
2013-12-05 22:24:25 -06:00
|
|
|
}
|
2013-12-11 14:40:51 -06:00
|
|
|
|