2015-04-21 21:01:19 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-07-02 01:20:07 +02:00
|
|
|
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItem_};
|
2015-06-24 01:11:29 +02:00
|
|
|
use syntax::codemap::{CodeMap, Span, BytePos};
|
|
|
|
|
|
|
|
use comment::FindUncommented;
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-06-23 15:58:58 +02:00
|
|
|
use SKIP_ANNOTATION;
|
2015-06-05 16:56:59 +02:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
// Computes the length of a string's last line, minus offset.
|
|
|
|
#[inline]
|
|
|
|
pub fn extra_offset(text: &str, offset: usize) -> usize {
|
|
|
|
match text.rfind('\n') {
|
|
|
|
// 1 for newline character
|
|
|
|
Some(idx) => text.len() - idx - 1 - offset,
|
2015-08-16 15:58:17 +12:00
|
|
|
None => text.len(),
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:11:29 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
|
|
|
|
let snippet = codemap.span_to_snippet(original).unwrap();
|
|
|
|
|
|
|
|
original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
|
|
|
|
}
|
|
|
|
|
2015-04-21 21:01:19 +12:00
|
|
|
#[inline]
|
|
|
|
pub fn make_indent(width: usize) -> String {
|
|
|
|
let mut indent = String::with_capacity(width);
|
|
|
|
for _ in 0..width {
|
|
|
|
indent.push(' ')
|
|
|
|
}
|
|
|
|
indent
|
|
|
|
}
|
2015-05-29 12:41:26 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn format_visibility(vis: Visibility) -> &'static str {
|
|
|
|
match vis {
|
|
|
|
Visibility::Public => "pub ",
|
2015-08-16 15:58:17 +12:00
|
|
|
Visibility::Inherited => "",
|
2015-05-29 12:41:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 19:40:22 +02:00
|
|
|
|
2015-07-24 19:54:38 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn format_mutability(mutability: ast::Mutability) -> &'static str {
|
|
|
|
match mutability {
|
|
|
|
ast::Mutability::MutMutable => "mut ",
|
2015-08-16 15:58:17 +12:00
|
|
|
ast::Mutability::MutImmutable => "",
|
2015-07-24 19:54:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 20:00:22 +12:00
|
|
|
// The width of the first line in s.
|
|
|
|
#[inline]
|
|
|
|
pub fn first_line_width(s: &str) -> usize {
|
|
|
|
match s.find('\n') {
|
|
|
|
Some(n) => n,
|
|
|
|
None => s.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The width of the last line in s.
|
|
|
|
#[inline]
|
|
|
|
pub fn last_line_width(s: &str) -> usize {
|
|
|
|
match s.rfind('\n') {
|
2015-08-17 09:41:45 +12:00
|
|
|
Some(n) => s.len() - n - 1,
|
2015-08-14 20:00:22 +12:00
|
|
|
None => s.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-06-23 15:58:58 +02:00
|
|
|
fn is_skip(meta_item: &MetaItem) -> bool {
|
|
|
|
match meta_item.node {
|
|
|
|
MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn contains_skip(attrs: &[Attribute]) -> bool {
|
|
|
|
attrs.iter().any(|a| is_skip(&a.node.value))
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:20:07 +02:00
|
|
|
// Find the end of a TyParam
|
2015-08-14 20:00:22 +12:00
|
|
|
#[inline]
|
2015-07-02 01:20:07 +02:00
|
|
|
pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
|
|
|
|
typaram.bounds.last().map(|bound| match *bound {
|
|
|
|
ast::RegionTyParamBound(ref lt) => lt.span,
|
|
|
|
ast::TraitTyParamBound(ref prt, _) => prt.span,
|
|
|
|
}).unwrap_or(typaram.span).hi
|
|
|
|
}
|
|
|
|
|
2015-06-09 01:42:29 +02:00
|
|
|
#[inline]
|
|
|
|
#[cfg(target_pointer_width="64")]
|
|
|
|
// Based on the trick layed out at
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
|
2015-06-22 16:49:00 +02:00
|
|
|
x = x.wrapping_sub(1);
|
2015-06-09 01:42:29 +02:00
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
x |= x >> 32;
|
2015-06-22 16:49:00 +02:00
|
|
|
x.wrapping_add(1)
|
2015-06-09 01:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:56:59 +02:00
|
|
|
#[inline]
|
2015-06-09 01:42:29 +02:00
|
|
|
#[cfg(target_pointer_width="32")]
|
|
|
|
pub fn round_up_to_power_of_two(mut x: usize) -> usize {
|
2015-06-22 16:49:00 +02:00
|
|
|
x = x.wrapping_sub(1);
|
2015-06-09 01:42:29 +02:00
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
2015-06-22 16:49:00 +02:00
|
|
|
x.wrapping_add(1)
|
2015-06-09 01:42:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 19:40:22 +02:00
|
|
|
// Macro for deriving implementations of Decodable for enums
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! impl_enum_decodable {
|
|
|
|
( $e:ident, $( $x:ident ),* ) => {
|
2015-06-09 01:42:29 +02:00
|
|
|
impl ::rustc_serialize::Decodable for $e {
|
|
|
|
fn decode<D: ::rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
2015-06-08 19:40:22 +02:00
|
|
|
let s = try!(d.read_str());
|
|
|
|
match &*s {
|
|
|
|
$(
|
|
|
|
stringify!($x) => Ok($e::$x),
|
|
|
|
)*
|
|
|
|
_ => Err(d.error("Bad variant")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 21:41:19 +02:00
|
|
|
|
|
|
|
impl ::std::str::FromStr for $e {
|
|
|
|
type Err = &'static str;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match &*s {
|
|
|
|
$(
|
|
|
|
stringify!($x) => Ok($e::$x),
|
|
|
|
)*
|
|
|
|
_ => Err("Bad variant"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 19:40:22 +02:00
|
|
|
};
|
|
|
|
}
|
2015-06-09 01:42:29 +02:00
|
|
|
|
2015-06-22 02:53:28 +02:00
|
|
|
// Same as try!, but for Option
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! try_opt {
|
|
|
|
($expr:expr) => (match $expr {
|
|
|
|
Some(val) => val,
|
|
|
|
None => { return None; }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-09 01:42:29 +02:00
|
|
|
#[test]
|
|
|
|
fn power_rounding() {
|
2015-06-22 16:49:00 +02:00
|
|
|
assert_eq!(0, round_up_to_power_of_two(0));
|
2015-06-09 01:42:29 +02:00
|
|
|
assert_eq!(1, round_up_to_power_of_two(1));
|
|
|
|
assert_eq!(64, round_up_to_power_of_two(33));
|
|
|
|
assert_eq!(256, round_up_to_power_of_two(256));
|
|
|
|
}
|