Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Minimize use of DUMMY_SP #298

Merged
merged 1 commit into from
Jun 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ioreg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ plugin = true

[dev-dependencies.volatile_cell]
path = "../volatile_cell"

[dependencies]
syntaxext_lint = "*"
15 changes: 7 additions & 8 deletions ioreg/src/builder/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use syntax::ast;
use syntax::ptr::P;
use syntax::codemap::DUMMY_SP;
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::ext::quote::rt::ToTokens;
Expand All @@ -33,7 +32,7 @@ pub struct BuildAccessors<'a> {

impl<'a> node::RegVisitor for BuildAccessors<'a> {
fn visit_prim_reg(&mut self, path: &Vec<String>, reg: &node::Reg,
_width: &node::RegWidth, fields: &Vec<node::Field>) {
fields: &Vec<node::Field>) {
if fields.iter().any(|f| f.access != node::Access::WriteOnly) {
let item = build_get_fn(self.cx, path, reg);
self.builder.push_item(item);
Expand All @@ -60,7 +59,7 @@ fn build_field_accessors(cx: &ExtCtxt, path: &Vec<String>,
-> Option<P<ast::Item>>
{
let reg_ty: P<ast::Ty> =
cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));

let items = match field.access {
node::Access::ReadWrite => vec!(build_field_set_fn(cx, path, reg, field),
Expand Down Expand Up @@ -103,7 +102,7 @@ fn build_get_fn(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg)
-> P<ast::Item>
{
let reg_ty: P<ast::Ty> =
cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let getter_ty = utils::getter_name(cx, path);

let docstring = format!("Fetch the value of the `{}` register",
Expand All @@ -126,7 +125,7 @@ fn build_field_set_fn(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg, field: &node::Field)
-> P<ast::ImplItem>
{
let reg_ty = cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
let reg_ty = cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let fn_name =
cx.ident_of((String::from_str("set_")+field.name.node.as_str()).as_str());
let field_ty: P<ast::Ty> =
Expand Down Expand Up @@ -161,7 +160,7 @@ fn build_field_get_fn(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg, field: &node::Field)
-> P<ast::ImplItem>
{
let reg_ty = cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
let reg_ty = cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
utils::unwrap_impl_item({
let fn_name = cx.ident_of(field.name.node.as_str());
let field_ty: P<ast::Ty> =
Expand Down Expand Up @@ -190,10 +189,10 @@ fn build_field_get_fn(cx: &ExtCtxt, path: &Vec<String>,
}

fn build_field_clear_fn(cx: &ExtCtxt, path: &Vec<String>,
_reg: &node::Reg, field: &node::Field)
reg: &node::Reg, field: &node::Field)
-> P<ast::ImplItem>
{
let reg_ty = cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
let reg_ty = cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let fn_name =
cx.ident_of((String::from_str("clear_")+field.name.node.as_str()).as_str());
let setter_ty = utils::setter_name(cx, path);
Expand Down
42 changes: 26 additions & 16 deletions ioreg/src/builder/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::ops::Deref;
use syntax::ast;
use syntax::ptr::P;
use syntax::ext::base::ExtCtxt;
use syntax::codemap::DUMMY_SP;
use syntax::codemap::{respan, Span};
use syntax::ext::build::AstBuilder;
use syntax::ext::quote::rt::ToTokens;
use syntax::parse::token;
Expand All @@ -43,7 +43,7 @@ impl<'a> BuildGetters<'a> {

impl<'a> node::RegVisitor for BuildGetters<'a> {
fn visit_prim_reg(&mut self, path: &Vec<String>,
reg: &node::Reg, _width: &node::RegWidth,
reg: &node::Reg,
fields: &Vec<node::Field>) {
if fields.iter().any(|f| f.access != node::Access::WriteOnly) {
let it = build_type(self.cx, path, reg);
Expand All @@ -61,6 +61,13 @@ impl<'a> node::RegVisitor for BuildGetters<'a> {
}
}

fn reg_ty_span(reg: &node::Reg) -> Span {
match reg.ty {
node::RegType::RegPrim(ref width, _) => width.span,
_ => reg.name.span,
}
}

fn build_type(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg) -> P<ast::Item>
{
Expand All @@ -87,11 +94,12 @@ fn build_type(cx: &ExtCtxt, path: &Vec<String>,
P(item)
}

fn build_new(cx: &ExtCtxt, path: &Vec<String>) -> P<ast::ImplItem> {
fn build_new(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg) -> P<ast::ImplItem> {
let reg_ty: P<ast::Ty> =
cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let getter_ident = utils::getter_name(cx, path);
let getter_ty: P<ast::Ty> = cx.ty_ident(DUMMY_SP,
let getter_ty: P<ast::Ty> = cx.ty_ident(reg_ty_span(reg),
getter_ident);
let item = quote_item!(cx,
impl $getter_ty {
Expand All @@ -111,11 +119,13 @@ fn build_new(cx: &ExtCtxt, path: &Vec<String>) -> P<ast::ImplItem> {
fn from_primitive(cx: &ExtCtxt, path: &Vec<String>, _: &node::Reg,
field: &node::Field, prim: P<ast::Expr>)
-> P<ast::Expr> {
// Use bit_range_field for the span because it is to blame for the
// type of the register
match field.ty.node {
node::FieldType::UIntField => prim,
node::FieldType::BoolField =>
cx.expr_binary(DUMMY_SP, ast::BiNe,
prim, utils::expr_int(cx, 0)),
cx.expr_binary(field.bit_range_span, ast::BiNe,
prim, utils::expr_int(cx, respan(field.bit_range_span, 0))),
node::FieldType::EnumField {opt_name: _, variants: ref vars} => {
let mut arms: Vec<ast::Arm> = Vec::new();
for v in vars.iter() {
Expand All @@ -124,38 +134,38 @@ fn from_primitive(cx: &ExtCtxt, path: &Vec<String>, _: &node::Reg,
let enum_ident = cx.ident_of(name.connect("_").as_str());
let val_ident = cx.ident_of(v.name.node.as_str());
let body = cx.expr_path(
cx.path(DUMMY_SP, vec!(enum_ident, val_ident)));
cx.path(v.name.span, vec!(enum_ident, val_ident)));
let val: u64 = v.value.node;
let lit = cx.expr_lit(
DUMMY_SP,
v.value.span,
ast::LitInt(val, ast::UnsuffixedIntLit(ast::Plus)));
let arm = ast::Arm {
attrs: vec!(),
pats: vec!(
P(ast::Pat {
id: ast::DUMMY_NODE_ID,
span: DUMMY_SP,
span: lit.span,
node: ast::PatLit(lit),
})
),
guard: None,
body: cx.expr_some(DUMMY_SP, body),
body: cx.expr_some(body.span, body),
};
arms.push(arm);
}
let wild_arm = ast::Arm {
attrs: vec!(),
pats: vec!(cx.pat_wild(DUMMY_SP)),
pats: vec!(cx.pat_wild(field.name.span)),
guard: None,
body: cx.expr_none(DUMMY_SP),
body: cx.expr_none(field.name.span),
};
arms.push(wild_arm);
let opt_expr = cx.expr_match(
DUMMY_SP,
field.name.span,
prim,
arms);
cx.expr_method_call(
DUMMY_SP,
field.name.span,
opt_expr,
cx.ident_of("unwrap"),
Vec::new())
Expand All @@ -166,7 +176,7 @@ fn from_primitive(cx: &ExtCtxt, path: &Vec<String>, _: &node::Reg,
fn build_impl(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg,
fields: &Vec<node::Field>) -> P<ast::Item> {
let getter_ty = utils::getter_name(cx, path);
let new = build_new(cx, path);
let new = build_new(cx, path, reg);
let getters: Vec<P<ast::ImplItem>> =
FromIterator::from_iter(
fields.iter()
Expand Down
14 changes: 10 additions & 4 deletions ioreg/src/builder/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ pub struct BuildRegStructs<'a> {

impl<'a> node::RegVisitor for BuildRegStructs<'a> {
fn visit_prim_reg(&mut self, path: &Vec<String>, reg: &node::Reg,
width: &node::RegWidth, fields: &Vec<node::Field>) {
fields: &Vec<node::Field>) {
let width = match reg.ty {
node::RegType::RegPrim(ref width, _) => width.node,
_ => panic!("visit_prim_reg called with non-primitive register"),
};
for field in fields.iter() {
for item in build_field_type(self.cx, path, reg, field).into_iter() {
self.builder.push_item(item);
}
}

for item in build_reg_struct(self.cx, path, reg, width).into_iter() {
for item in build_reg_struct(self.cx, path, reg, &width).into_iter() {
self.builder.push_item(item);
}
}
Expand Down Expand Up @@ -76,7 +80,8 @@ fn build_field_type(cx: &ExtCtxt, path: &Vec<String>,
utils::list_attribute(cx, "allow",
vec!("dead_code",
"non_camel_case_types",
"missing_docs")));
"missing_docs"),
field.name.span));
let ty_item: P<ast::Item> = P(ast::Item {
ident: name,
id: ast::DUMMY_NODE_ID,
Expand Down Expand Up @@ -144,7 +149,8 @@ fn build_enum_variant(cx: &ExtCtxt, variant: &node::Variant)
attrs: vec!(doc_attr),
kind: ast::TupleVariantKind(Vec::new()),
id: ast::DUMMY_NODE_ID,
disr_expr: Some(utils::expr_int(cx, variant.value.node as i64)),
disr_expr: Some(utils::expr_int(cx, respan(variant.value.span,
variant.value.node as i64))),
vis: ast::Inherited,
}
)
Expand Down
11 changes: 5 additions & 6 deletions ioreg/src/builder/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::ops::Deref;
use syntax::ast;
use syntax::ptr::P;
use syntax::ext::base::ExtCtxt;
use syntax::codemap::DUMMY_SP;
use syntax::ext::build::AstBuilder;
use syntax::ext::quote::rt::ToTokens;
use syntax::parse::token;
Expand All @@ -43,7 +42,7 @@ impl<'a> BuildSetters<'a> {

impl<'a> node::RegVisitor for BuildSetters<'a> {
fn visit_prim_reg<'b>(&'b mut self, path: &Vec<String>,
reg: &'b node::Reg, _width: &node::RegWidth, fields: &Vec<node::Field>)
reg: &'b node::Reg, fields: &Vec<node::Field>)
{
if fields.iter().any(|f| f.access != node::Access::ReadOnly) {
let it = build_type(self.cx, path, reg, fields);
Expand All @@ -64,7 +63,7 @@ fn build_type(cx: &ExtCtxt, path: &Vec<String>,
let packed_ty = utils::reg_primitive_type(cx, reg)
.expect("Unexpected non-primitive register");
let name = utils::setter_name(cx, path);
let reg_ty = cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
let reg_ty = cx.ty_ident(reg.name.span, utils::path_ident(cx, path));

let reg_doc = match reg.docstring {
Some(d) => token::get_ident(d.node).to_string(),
Expand All @@ -89,10 +88,10 @@ fn build_type(cx: &ExtCtxt, path: &Vec<String>,
P(item)
}

fn build_new<'a>(cx: &'a ExtCtxt, path: &Vec<String>)
fn build_new<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg)
-> P<ast::ImplItem> {
let reg_ty: P<ast::Ty> =
cx.ty_ident(DUMMY_SP, utils::path_ident(cx, path));
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let setter_ident = utils::setter_name(cx, path);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ident<'a> {
Expand Down Expand Up @@ -164,7 +163,7 @@ fn build_done(ctx: &ExtCtxt, path: &Vec<String>) -> P<ast::ImplItem> {
fn build_impl(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg,
fields: &Vec<node::Field>) -> P<ast::Item>
{
let new = build_new(cx, path);
let new = build_new(cx, path, reg);
let setter_ident = utils::setter_name(cx, path);
let methods: Vec<P<ast::ImplItem>> =
FromIterator::from_iter(
Expand Down
19 changes: 11 additions & 8 deletions ioreg/src/builder/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::iter::FromIterator;
use syntax::ast;
use syntax::ptr::P;
use syntax::ast_util::empty_generics;
use syntax::codemap::{DUMMY_SP, dummy_spanned};
use syntax::codemap::{DUMMY_SP, dummy_spanned, respan, Spanned};
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::parse::token;
Expand Down Expand Up @@ -86,21 +86,21 @@ impl<'a> BuildUnionTypes<'a> {
}
}

fn expr_u64(cx: &ExtCtxt, n: u64) -> P<ast::Expr> {
cx.expr_lit(DUMMY_SP, ast::LitInt(n as u64, ast::UnsignedIntLit(ast::TyUs)))
fn expr_u64(cx: &ExtCtxt, n: Spanned<u64>) -> P<ast::Expr> {
cx.expr_lit(n.span, ast::LitInt(n.node as u64, ast::UnsignedIntLit(ast::TyUs)))
}

/// Returns the type of the field representing the given register
/// within a `RegGroup` struct
fn reg_struct_type(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg)
-> P<ast::Ty> {
let base_ty_path = cx.path_ident(DUMMY_SP, utils::path_ident(cx, path));
let base_ty_path = cx.path_ident(reg.name.span, utils::path_ident(cx, path));
let base_ty: P<ast::Ty> = cx.ty_path(base_ty_path);
match reg.count.node {
1 => base_ty,
n =>
cx.ty(DUMMY_SP,
ast::TyFixedLengthVec(base_ty, expr_u64(cx, n as u64))),
cx.ty(reg.count.span,
ast::TyFixedLengthVec(base_ty, expr_u64(cx, respan(reg.count.span, n as u64)))),
}
}

Expand Down Expand Up @@ -138,6 +138,8 @@ impl<'a> BuildUnionTypes<'a> {
}

/// Build field for padding or a register
// Dummy spans allowed here because u8 doesn't come from anywhere
#[allow(dummy_span)]
fn build_pad_or_reg(&self, path: &Vec<String>, reg_or_pad: RegOrPadding,
index: usize) -> ast::StructField {
match reg_or_pad {
Expand All @@ -150,7 +152,7 @@ impl<'a> BuildUnionTypes<'a> {
let ty: P<ast::Ty> =
self.cx.ty(
DUMMY_SP,
ast::TyFixedLengthVec(u8_ty, expr_u64(self.cx, length)));
ast::TyFixedLengthVec(u8_ty, expr_u64(self.cx, respan(DUMMY_SP, length))));
dummy_spanned(
ast::StructField_ {
kind: ast::NamedField(
Expand Down Expand Up @@ -183,7 +185,8 @@ impl<'a> BuildUnionTypes<'a> {
utils::list_attribute(self.cx, "allow",
vec!("non_camel_case_types",
"dead_code",
"missing_docs")),
"missing_docs"),
reg.name.span),
);
match reg.docstring {
Some(docstring) =>
Expand Down
Loading