]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/save/span_utils.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / librustc_trans / save / span_utils.rs
1 // Copyright 2012-2014 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 rustc::session::Session;
12
13 use save::generated_code;
14
15 use std::cell::Cell;
16 use std::env;
17 use std::path::Path;
18
19 use syntax::ast;
20 use syntax::codemap::*;
21 use syntax::parse::lexer;
22 use syntax::parse::lexer::{Reader, StringReader};
23 use syntax::parse::token;
24 use syntax::parse::token::{keywords, Token};
25
26 #[derive(Clone)]
27 pub struct SpanUtils<'a> {
28 pub sess: &'a Session,
29 pub err_count: Cell<isize>,
30 }
31
32 impl<'a> SpanUtils<'a> {
33 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
34 SpanUtils {
35 sess: sess,
36 err_count: Cell::new(0),
37 }
38 }
39
40 pub fn make_path_string(file_name: &str) -> String {
41 let path = Path::new(file_name);
42 if path.is_absolute() {
43 path.clone().display().to_string()
44 } else {
45 env::current_dir().unwrap().join(&path).display().to_string()
46 }
47 }
48
49 // Standard string for extents/location.
50 #[rustfmt_skip]
51 pub fn extent_str(&self, span: Span) -> String {
52 let lo_loc = self.sess.codemap().lookup_char_pos(span.lo);
53 let hi_loc = self.sess.codemap().lookup_char_pos(span.hi);
54 let lo_pos = self.sess.codemap().bytepos_to_file_charpos(span.lo);
55 let hi_pos = self.sess.codemap().bytepos_to_file_charpos(span.hi);
56 let lo_pos_byte = self.sess.codemap().lookup_byte_offset(span.lo).pos;
57 let hi_pos_byte = self.sess.codemap().lookup_byte_offset(span.hi).pos;
58
59 format!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
60 file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
61 SpanUtils::make_path_string(&lo_loc.file.name),
62 lo_loc.line, lo_loc.col.to_usize(), lo_pos.to_usize(), lo_pos_byte.to_usize(),
63 hi_loc.line, hi_loc.col.to_usize(), hi_pos.to_usize(), hi_pos_byte.to_usize())
64 }
65
66 // sub_span starts at span.lo, so we need to adjust the positions etc.
67 // If sub_span is None, we don't need to adjust.
68 pub fn make_sub_span(&self, span: Span, sub_span: Option<Span>) -> Option<Span> {
69 let loc = self.sess.codemap().lookup_char_pos(span.lo);
70 assert!(!generated_code(span),
71 "generated code; we should not be processing this `{}` in {}, line {}",
72 self.snippet(span),
73 loc.file.name,
74 loc.line);
75
76 match sub_span {
77 None => None,
78 Some(sub) => {
79 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
80 let base = pos + fm.start_pos;
81 Some(Span {
82 lo: base + self.sess.codemap().lookup_byte_offset(sub.lo).pos,
83 hi: base + self.sess.codemap().lookup_byte_offset(sub.hi).pos,
84 expn_id: NO_EXPANSION,
85 })
86 }
87 }
88 }
89
90 pub fn snippet(&self, span: Span) -> String {
91 match self.sess.codemap().span_to_snippet(span) {
92 Ok(s) => s,
93 Err(_) => String::new(),
94 }
95 }
96
97 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
98 // sadness - we don't have spans for sub-expressions nor access to the tokens
99 // so in order to get extents for the function name itself (which dxr expects)
100 // we need to re-tokenise the fn definition
101
102 // Note: this is a bit awful - it adds the contents of span to the end of
103 // the codemap as a new filemap. This is mostly OK, but means we should
104 // not iterate over the codemap. Also, any spans over the new filemap
105 // are incompatible with spans over other filemaps.
106 let filemap = self.sess
107 .codemap()
108 .new_filemap(String::from("<anon-dxr>"), self.snippet(span));
109 let s = self.sess;
110 lexer::StringReader::new(s.diagnostic(), filemap)
111 }
112
113 // Re-parses a path and returns the span for the last identifier in the path
114 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
115 let mut result = None;
116
117 let mut toks = self.retokenise_span(span);
118 let mut bracket_count = 0;
119 loop {
120 let ts = toks.real_token();
121 if ts.tok == token::Eof {
122 return self.make_sub_span(span, result)
123 }
124 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
125 result = Some(ts.sp);
126 }
127
128 bracket_count += match ts.tok {
129 token::Lt => 1,
130 token::Gt => -1,
131 token::BinOp(token::Shr) => -2,
132 _ => 0,
133 }
134 }
135 }
136
137 // Return the span for the first identifier in the path.
138 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
139 let mut toks = self.retokenise_span(span);
140 let mut bracket_count = 0;
141 loop {
142 let ts = toks.real_token();
143 if ts.tok == token::Eof {
144 return None;
145 }
146 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
147 return self.make_sub_span(span, Some(ts.sp));
148 }
149
150 bracket_count += match ts.tok {
151 token::Lt => 1,
152 token::Gt => -1,
153 token::BinOp(token::Shr) => -2,
154 _ => 0,
155 }
156 }
157 }
158
159 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
160 // any brackets, or the last span.
161 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
162 let mut toks = self.retokenise_span(span);
163 let mut prev = toks.real_token();
164 let mut result = None;
165 let mut bracket_count = 0;
166 let mut last_span = None;
167 while prev.tok != token::Eof {
168 last_span = None;
169 let mut next = toks.real_token();
170
171 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
172 bracket_count == 0 && prev.tok.is_ident() {
173 result = Some(prev.sp);
174 }
175
176 if bracket_count == 0 && next.tok == token::ModSep {
177 let old = prev;
178 prev = next;
179 next = toks.real_token();
180 if next.tok == token::Lt && old.tok.is_ident() {
181 result = Some(old.sp);
182 }
183 }
184
185 bracket_count += match prev.tok {
186 token::OpenDelim(token::Paren) | token::Lt => 1,
187 token::CloseDelim(token::Paren) | token::Gt => -1,
188 token::BinOp(token::Shr) => -2,
189 _ => 0,
190 };
191
192 if prev.tok.is_ident() && bracket_count == 0 {
193 last_span = Some(prev.sp);
194 }
195 prev = next;
196 }
197 if result.is_none() && last_span.is_some() {
198 return self.make_sub_span(span, last_span);
199 }
200 return self.make_sub_span(span, result);
201 }
202
203 // Return the span for the last ident before a `<` and outside any
204 // brackets, or the last span.
205 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
206 let mut toks = self.retokenise_span(span);
207 let mut prev = toks.real_token();
208 let mut result = None;
209 let mut bracket_count = 0;
210 loop {
211 let next = toks.real_token();
212
213 if (next.tok == token::Lt || next.tok == token::Colon) && bracket_count == 0 &&
214 prev.tok.is_ident() {
215 result = Some(prev.sp);
216 }
217
218 bracket_count += match prev.tok {
219 token::Lt => 1,
220 token::Gt => -1,
221 token::BinOp(token::Shl) => 2,
222 token::BinOp(token::Shr) => -2,
223 _ => 0,
224 };
225
226 if next.tok == token::Eof {
227 break;
228 }
229 prev = next;
230 }
231 if bracket_count != 0 {
232 let loc = self.sess.codemap().lookup_char_pos(span.lo);
233 self.sess.span_bug(span,
234 &format!("Mis-counted brackets when breaking path? Parsing '{}' \
235 in {}, line {}",
236 self.snippet(span),
237 loc.file.name,
238 loc.line));
239 }
240 if result.is_none() && prev.tok.is_ident() && bracket_count == 0 {
241 return self.make_sub_span(span, Some(prev.sp));
242 }
243 self.make_sub_span(span, result)
244 }
245
246 // Reparse span and return an owned vector of sub spans of the first limit
247 // identifier tokens in the given nesting level.
248 // example with Foo<Bar<T,V>, Bar<T,V>>
249 // Nesting = 0: all idents outside of brackets: [Foo]
250 // Nesting = 1: idents within one level of brackets: [Bar, Bar]
251 pub fn spans_with_brackets(&self, span: Span, nesting: isize, limit: isize) -> Vec<Span> {
252 let mut result: Vec<Span> = vec!();
253
254 let mut toks = self.retokenise_span(span);
255 // We keep track of how many brackets we're nested in
256 let mut bracket_count: isize = 0;
257 let mut found_ufcs_sep = false;
258 loop {
259 let ts = toks.real_token();
260 if ts.tok == token::Eof {
261 if bracket_count != 0 {
262 let loc = self.sess.codemap().lookup_char_pos(span.lo);
263 self.sess.span_bug(span,
264 &format!("Mis-counted brackets when breaking path? \
265 Parsing '{}' in {}, line {}",
266 self.snippet(span),
267 loc.file.name,
268 loc.line));
269 }
270 return result
271 }
272 if (result.len() as isize) == limit {
273 return result;
274 }
275 bracket_count += match ts.tok {
276 token::Lt => 1,
277 token::Gt => -1,
278 token::BinOp(token::Shl) => 2,
279 token::BinOp(token::Shr) => -2,
280 _ => 0,
281 };
282
283 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
284
285 // The root cause of this hack is that the AST representation of
286 // qpaths is horrible. It treats <A as B>::C as a path with two
287 // segments, B and C and notes that there is also a self type A at
288 // position 0. Because we don't have spans for individual idents,
289 // only the whole path, we have to iterate over the tokens in the
290 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
291 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
292 // the start of the first ident to the end of the path.
293 if !found_ufcs_sep && bracket_count == -1 {
294 found_ufcs_sep = true;
295 bracket_count += 1;
296 }
297 if ts.tok.is_ident() && bracket_count == nesting {
298 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
299 }
300 }
301 }
302
303 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
304 let mut toks = self.retokenise_span(span);
305 let mut prev = toks.real_token();
306 loop {
307 if prev.tok == token::Eof {
308 return None;
309 }
310 let next = toks.real_token();
311 if next.tok == tok {
312 return self.make_sub_span(span, Some(prev.sp));
313 }
314 prev = next;
315 }
316 }
317
318 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
319 let mut toks = self.retokenise_span(span);
320 loop {
321 let next = toks.real_token();
322 if next.tok == token::Eof {
323 return None;
324 }
325 if next.tok == tok {
326 return self.make_sub_span(span, Some(next.sp));
327 }
328 }
329 }
330
331 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
332 self.sub_span_after(span, |t| t.is_keyword(keyword))
333 }
334
335 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
336 self.sub_span_after(span, |t| t == tok)
337 }
338
339 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
340 let mut toks = self.retokenise_span(span);
341 loop {
342 let ts = toks.real_token();
343 if ts.tok == token::Eof {
344 return None;
345 }
346 if f(ts.tok) {
347 let ts = toks.real_token();
348 if ts.tok == token::Eof {
349 return None
350 } else {
351 return self.make_sub_span(span, Some(ts.sp));
352 }
353 }
354 }
355 }
356
357
358 // Returns a list of the spans of idents in a path.
359 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
360 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
361 if generated_code(path.span) {
362 return vec!();
363 }
364
365 self.spans_with_brackets(path.span, 0, -1)
366 }
367
368 // Return an owned vector of the subspans of the param identifier
369 // tokens found in span.
370 pub fn spans_for_ty_params(&self, span: Span, number: isize) -> Vec<Span> {
371 if generated_code(span) {
372 return vec!();
373 }
374 // Type params are nested within one level of brackets:
375 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
376 self.spans_with_brackets(span, 1, number)
377 }
378
379 pub fn report_span_err(&self, kind: &str, span: Span) {
380 let loc = self.sess.codemap().lookup_char_pos(span.lo);
381 info!("({}) Could not find sub_span in `{}` in {}, line {}",
382 kind,
383 self.snippet(span),
384 loc.file.name,
385 loc.line);
386 self.err_count.set(self.err_count.get() + 1);
387 if self.err_count.get() > 1000 {
388 self.sess.bug("span errors reached 1000, giving up");
389 }
390 }
391 }