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