1 // Copyright 2016 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.
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.
11 use dep_graph::{DepGraph, DepNode};
12 use hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
13 use rustc_data_structures::bitvec::BitVector;
16 use syntax::codemap::CodeMap;
17 use syntax_pos::{BytePos, FileMap};
30 pub struct CachingCodemapView<'tcx> {
31 codemap: &'tcx CodeMap,
32 line_cache: [CacheEntry; 3],
35 dep_tracking_reads: BitVector,
38 impl<'tcx> CachingCodemapView<'tcx> {
39 pub fn new<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> CachingCodemapView<'tcx> {
40 let codemap = tcx.sess.codemap();
41 let files = codemap.files_untracked();
42 let first_file = files[0].clone();
43 let entry = CacheEntry {
46 line_start: BytePos(0),
53 dep_graph: tcx.dep_graph.clone(),
55 line_cache: [entry.clone(), entry.clone(), entry.clone()],
57 dep_tracking_reads: BitVector::new(files.len()),
61 pub fn byte_pos_to_line_and_col(&mut self,
63 -> Option<(Rc<FileMap>, usize, BytePos)> {
66 // Check if the position is in one of the cached lines
67 for cache_entry in self.line_cache.iter_mut() {
68 if pos >= cache_entry.line_start && pos < cache_entry.line_end {
69 cache_entry.time_stamp = self.time_stamp;
70 if self.dep_tracking_reads.insert(cache_entry.file_index) {
71 self.dep_graph.read(dep_node(cache_entry));
74 return Some((cache_entry.file.clone(),
75 cache_entry.line_number,
76 pos - cache_entry.line_start));
82 for index in 1 .. self.line_cache.len() {
83 if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
88 let cache_entry = &mut self.line_cache[oldest];
90 // If the entry doesn't point to the correct file, fix it up
91 if pos < cache_entry.file.start_pos || pos >= cache_entry.file.end_pos {
93 let files = self.codemap.files_untracked();
96 let file_index = self.codemap.lookup_filemap_idx(pos);
97 let file = files[file_index].clone();
99 if pos >= file.start_pos && pos < file.end_pos {
100 cache_entry.file = file;
101 cache_entry.file_index = file_index;
115 let line_index = cache_entry.file.lookup_line(pos).unwrap();
116 let line_bounds = cache_entry.file.line_bounds(line_index);
118 cache_entry.line_number = line_index + 1;
119 cache_entry.line_start = line_bounds.0;
120 cache_entry.line_end = line_bounds.1;
121 cache_entry.time_stamp = self.time_stamp;
123 if self.dep_tracking_reads.insert(cache_entry.file_index) {
124 self.dep_graph.read(dep_node(cache_entry));
127 return Some((cache_entry.file.clone(),
128 cache_entry.line_number,
129 pos - cache_entry.line_start));
133 fn dep_node(cache_entry: &CacheEntry) -> DepNode<DefId> {
135 krate: CrateNum::from_u32(cache_entry.file.crate_of_origin),
136 index: CRATE_DEF_INDEX,
138 let name = Arc::new(cache_entry.file.name.clone());
139 DepNode::FileMap(def_id, name)