Skip to content

Commit b70b7c2

Browse files
committed
move def-id to rustc crate
1 parent 7a3a1be commit b70b7c2

File tree

4 files changed

+57
-38
lines changed

4 files changed

+57
-38
lines changed

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub mod middle {
117117
pub mod dataflow;
118118
pub mod dead;
119119
pub mod def;
120+
pub mod def_id;
120121
pub mod dependency_format;
121122
pub mod effect;
122123
pub mod entry;

src/librustc/middle/def_id.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use syntax::ast::{CrateNum, NodeId};
12+
use std::cell::Cell;
13+
use std::fmt;
14+
15+
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
16+
RustcDecodable, Hash, Copy)]
17+
pub struct DefId {
18+
pub krate: CrateNum,
19+
pub node: NodeId,
20+
}
21+
22+
fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
23+
24+
thread_local!(pub static DEF_ID_DEBUG: Cell<fn(DefId, &mut fmt::Formatter) -> fmt::Result> =
25+
Cell::new(default_def_id_debug));
26+
27+
impl fmt::Debug for DefId {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29+
try!(write!(f, "DefId {{ krate: {}, node: {} }}",
30+
self.krate, self.node));
31+
DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f))
32+
}
33+
}
34+
35+
impl DefId {
36+
pub fn local(id: NodeId) -> DefId {
37+
DefId { krate: LOCAL_CRATE, node: id }
38+
}
39+
40+
/// Read the node id, asserting that this def-id is krate-local.
41+
pub fn local_id(&self) -> NodeId {
42+
assert_eq!(self.krate, LOCAL_CRATE);
43+
self.node
44+
}
45+
46+
pub fn is_local(&self) -> bool {
47+
self.krate == LOCAL_CRATE
48+
}
49+
}
50+
51+
52+
/// Item definitions in the currently-compiled crate would have the CrateNum
53+
/// LOCAL_CRATE in their DefId.
54+
pub const LOCAL_CRATE: CrateNum = 0;
55+

src/libsyntax/ast.rs

+1-32
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
6565
use print::pprust;
6666
use ptr::P;
6767

68-
use std::cell::Cell;
6968
use std::fmt;
7069
use std::rc::Rc;
7170
use serialize::{Encodable, Decodable, Encoder, Decoder};
@@ -371,37 +370,7 @@ pub type CrateNum = u32;
371370

372371
pub type NodeId = u32;
373372

374-
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
375-
RustcDecodable, Hash, Copy)]
376-
pub struct DefId {
377-
pub krate: CrateNum,
378-
pub node: NodeId,
379-
}
380-
381-
fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
382-
383-
thread_local!(pub static DEF_ID_DEBUG: Cell<fn(DefId, &mut fmt::Formatter) -> fmt::Result> =
384-
Cell::new(default_def_id_debug));
385-
386-
impl fmt::Debug for DefId {
387-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
388-
try!(write!(f, "DefId {{ krate: {}, node: {} }}",
389-
self.krate, self.node));
390-
DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f))
391-
}
392-
}
393-
394-
impl DefId {
395-
/// Read the node id, asserting that this def-id is krate-local.
396-
pub fn local_id(&self) -> NodeId {
397-
assert_eq!(self.krate, LOCAL_CRATE);
398-
self.node
399-
}
400-
}
401-
402-
/// Item definitions in the currently-compiled crate would have the CrateNum
403-
/// LOCAL_CRATE in their DefId.
404-
pub const LOCAL_CRATE: CrateNum = 0;
373+
/// Node id used to represent the root of the crate.
405374
pub const CRATE_NODE_ID: NodeId = 0;
406375

407376
/// When parsing and doing expansions, we initially give all AST nodes this AST

src/libsyntax/ast_util.rs

-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ pub fn path_name_i(idents: &[Ident]) -> String {
2828
idents.iter().map(|i| i.to_string()).collect::<Vec<String>>().join("::")
2929
}
3030

31-
pub fn local_def(id: NodeId) -> DefId {
32-
ast::DefId { krate: LOCAL_CRATE, node: id }
33-
}
34-
35-
pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }
36-
3731
pub fn stmt_id(s: &Stmt) -> NodeId {
3832
match s.node {
3933
StmtDecl(_, id) => id,

0 commit comments

Comments
 (0)