diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2018-12-09 21:05:22 +0200 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2019-03-15 09:26:13 +0200 |
| commit | 7d211e5b1287f9140418c4e46461c451d1da42e1 (patch) | |
| tree | 5a53bfa8e39173433fe526b289c48d1c1abe327c /src/librustc_codegen_utils | |
| parent | 7505bb6bbb7d66f445d7df7afc7c397dadea26f8 (diff) | |
| download | rust-7d211e5b1287f9140418c4e46461c451d1da42e1.tar.gz rust-7d211e5b1287f9140418c4e46461c451d1da42e1.zip | |
rustc: rewrite ty::item_path to be more functional than mutation-oriented.
Diffstat (limited to 'src/librustc_codegen_utils')
| -rw-r--r-- | src/librustc_codegen_utils/symbol_names.rs | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index 70ab185660c..13f8e13c328 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -92,7 +92,7 @@ use rustc::hir::Node; use rustc::hir::CodegenFnAttrFlags; use rustc::hir::map::definitions::DefPathData; use rustc::ich::NodeIdHashingMode; -use rustc::ty::item_path::{self, ItemPathBuffer, RootMode}; +use rustc::ty::item_path::{self, ItemPathPrinter, RootMode}; use rustc::ty::query::Providers; use rustc::ty::subst::SubstsRef; use rustc::ty::{self, Ty, TyCtxt, TypeFoldable}; @@ -223,11 +223,9 @@ fn get_symbol_hash<'a, 'tcx>( } fn def_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> ty::SymbolName { - let mut buffer = SymbolPathBuffer::new(tcx); item_path::with_forced_absolute_paths(|| { - tcx.push_item_path(&mut buffer, def_id); - }); - buffer.into_interned() + tcx.push_item_path(&mut SymbolPath::new(tcx), def_id).into_interned() + }) } fn symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName { @@ -319,7 +317,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance let hash = get_symbol_hash(tcx, def_id, instance, instance_ty, substs); - let mut buf = SymbolPathBuffer::from_interned(tcx.def_symbol_name(def_id), tcx); + let mut buf = SymbolPath::from_interned(tcx.def_symbol_name(def_id), tcx); if instance.is_vtable_shim() { buf.push("{{vtable-shim}}"); @@ -342,15 +340,15 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance // To be able to work on all platforms and get *some* reasonable output, we // use C++ name-mangling. #[derive(Debug)] -struct SymbolPathBuffer { +struct SymbolPath { result: String, temp_buf: String, strict_naming: bool, } -impl SymbolPathBuffer { +impl SymbolPath { fn new(tcx: TyCtxt<'_, '_, '_>) -> Self { - let mut result = SymbolPathBuffer { + let mut result = SymbolPath { result: String::with_capacity(64), temp_buf: String::with_capacity(16), strict_naming: tcx.has_strict_asm_symbol_naming(), @@ -360,7 +358,7 @@ impl SymbolPathBuffer { } fn from_interned(symbol: ty::SymbolName, tcx: TyCtxt<'_, '_, '_>) -> Self { - let mut result = SymbolPathBuffer { + let mut result = SymbolPath { result: String::with_capacity(64), temp_buf: String::with_capacity(16), strict_naming: tcx.has_strict_asm_symbol_naming(), @@ -375,19 +373,6 @@ impl SymbolPathBuffer { } } - fn finish(mut self, hash: u64) -> String { - // E = end name-sequence - let _ = write!(self.result, "17h{:016x}E", hash); - self.result - } -} - -impl ItemPathBuffer for SymbolPathBuffer { - fn root_mode(&self) -> &RootMode { - const ABSOLUTE: &RootMode = &RootMode::Absolute; - ABSOLUTE - } - fn push(&mut self, text: &str) { self.temp_buf.clear(); let need_underscore = sanitize(&mut self.temp_buf, text, self.strict_naming); @@ -401,6 +386,40 @@ impl ItemPathBuffer for SymbolPathBuffer { } self.result.push_str(&self.temp_buf); } + + fn finish(mut self, hash: u64) -> String { + // E = end name-sequence + let _ = write!(self.result, "17h{:016x}E", hash); + self.result + } +} + +#[derive(Debug)] +struct SymbolPathPrinter; + +impl ItemPathPrinter for SymbolPathPrinter { + type Path = SymbolPath; + + fn root_mode(&self) ->RootMode { + RootMode::Absolute + } + + fn path_crate(&self, name: Option<&str>) -> Self::Path { + let mut path = SymbolPath::new(); + if let Some(name) = name { + path.push(name); + } + path + } + fn path_impl(&self, text: &str) -> Self::Path { + let mut path = SymbolPath::new(); + path.push(text); + path + } + fn path_append(&self, mut path: Self::Path, text: &str) -> Self::Path { + path.push(text); + path + } } // Name sanitation. LLVM will happily accept identifiers with weird names, but |
