diff options
| author | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-12-05 03:51:11 +0000 |
|---|---|---|
| committer | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-12-22 06:14:35 +0000 |
| commit | f10f50b42639718b2580d10802f05f2b6ff209d5 (patch) | |
| tree | 19b61435b37e14f97a74e18b6db32c507e7b1a58 /src/libsyntax/ast.rs | |
| parent | 164619a8cfe6d376d25bd3a6a9a5f2856c8de64d (diff) | |
| download | rust-f10f50b42639718b2580d10802f05f2b6ff209d5.tar.gz rust-f10f50b42639718b2580d10802f05f2b6ff209d5.zip | |
Refactor how global paths are represented (for both ast and hir).
Diffstat (limited to 'src/libsyntax/ast.rs')
| -rw-r--r-- | src/libsyntax/ast.rs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index fdd82225b97..648a82ffb76 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -111,10 +111,8 @@ pub struct LifetimeDef { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub struct Path { pub span: Span, - /// A `::foo` path, is relative to the crate root rather than current - /// module (like paths in an import). - pub global: bool, /// The segments in the path: the things separated by `::`. + /// Global paths begin with `keywords::CrateRoot`. pub segments: Vec<PathSegment>, } @@ -136,10 +134,22 @@ impl Path { pub fn from_ident(s: Span, identifier: Ident) -> Path { Path { span: s, - global: false, segments: vec![identifier.into()], } } + + pub fn default_to_global(mut self) -> Path { + let name = self.segments[0].identifier.name; + if !self.is_global() && name != "$crate" && + name != keywords::SelfValue.name() && name != keywords::Super.name() { + self.segments.insert(0, PathSegment::crate_root()); + } + self + } + + pub fn is_global(&self) -> bool { + !self.segments.is_empty() && self.segments[0].identifier.name == keywords::CrateRoot.name() + } } /// A segment of a path: an identifier, an optional lifetime, and a set of types. @@ -166,6 +176,15 @@ impl From<Ident> for PathSegment { } } +impl PathSegment { + pub fn crate_root() -> Self { + PathSegment { + identifier: keywords::CrateRoot.ident(), + parameters: None, + } + } +} + /// Parameters of a path segment. /// /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
