Skip to content

Commit

Permalink
Change binary autogen to use tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Skepfyr committed Aug 8, 2019
1 parent 63e77fc commit a1cc4de
Show file tree
Hide file tree
Showing 7 changed files with 860 additions and 540 deletions.
448 changes: 210 additions & 238 deletions autogen/binary.rs

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions autogen/build.rs
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![recursion_limit="128"]

mod binary;
mod header;
Expand All @@ -29,14 +30,14 @@ use std::{
};
use utils::write_copyright_autogen_comment;

fn write(path: &PathBuf, contents: String) {
fn write<T: ToString>(path: &PathBuf, contents: T) {
let mut f = fs::File::create(path)
.expect(&format!("cannot open file: {:?}", path));
write_copyright_autogen_comment(&mut f);
write!(f, "{}", contents).unwrap()
write!(f, "{}", contents.to_string()).unwrap()
}

fn write_formatted(path: &PathBuf, contents: String) {
fn write_formatted<T: ToString>(path: &PathBuf, contents: T) {
write(path, contents);
match process::Command::new("rustfmt")
.arg(path)
Expand Down Expand Up @@ -152,22 +153,22 @@ fn main() {
);

// Path to the generated decoding errors.
write(
write_formatted(
&autogen_src_dir.join("../rspirv/binary/autogen_error.rs"),
binary::gen_operand_decode_errors(&grammar.operand_kinds),
);
// Path to the generated operand decoding methods.
write(
write_formatted(
&autogen_src_dir.join("../rspirv/binary/autogen_decode_operand.rs"),
binary::gen_operand_decode_methods(&grammar.operand_kinds),
);
// Path to the generated operand parsing methods.
write(
write_formatted(
&autogen_src_dir.join("../rspirv/binary/autogen_parse_operand.rs"),
binary::gen_operand_parse_methods(&grammar.operand_kinds),
);
// Path to the generated operand parsing methods.
write(
write_formatted(
&autogen_src_dir.join("../rspirv/binary/autogen_disas_operand.rs"),
binary::gen_disas_bit_enum_operands(&grammar.operand_kinds),
);
Expand Down
14 changes: 10 additions & 4 deletions autogen/utils.rs
Expand Up @@ -17,6 +17,8 @@ use crate::structs;
use std::fs;
use std::io::Write;

use proc_macro2::{Ident, Span};

#[cfg_attr(rustfmt, rustfmt_skip)]
static COPYRIGHT : &'static str = "\
// Copyright 2016 Google Inc.
Expand All @@ -40,7 +42,6 @@ static AUTOGEN_COMMENT : &'static str = "\
// DO NOT MODIFY!";

pub static RUSTFMT_SKIP: &'static str = "#[cfg_attr(rustfmt, rustfmt_skip)]";
pub static RUSTFMT_SKIP_BANG: &'static str = "#![cfg_attr(rustfmt, rustfmt_skip)]";

pub fn write_copyright_autogen_comment(file: &mut fs::File) {
file.write_all(COPYRIGHT.as_bytes()).unwrap();
Expand All @@ -49,6 +50,11 @@ pub fn write_copyright_autogen_comment(file: &mut fs::File) {
file.write_all(b"\n\n").unwrap();
}

/// Converts the given string into an `Ident`, with call-site span.
pub fn as_ident(ident: &str) -> Ident {
Ident::new(ident, Span::call_site())
}

/// Converts the given `symbol` to use snake case style.
pub fn snake_casify(symbol: &str) -> String {
let re = regex::Regex::new(r"(?P<l>[a-z])(?P<u>[A-Z])").unwrap();
Expand All @@ -57,15 +63,15 @@ pub fn snake_casify(symbol: &str) -> String {

/// Returns the corresponding operand kind in data representation for the
/// given operand `kind` in the grammar.
pub fn get_mr_operand_kind(kind: &str) -> &str {
if kind == "LiteralInteger" {
pub fn get_mr_operand_kind(kind: &str) -> Ident {
as_ident(if kind == "LiteralInteger" {
"LiteralInt32"
} else if kind == "LiteralContextDependentNumber" {
// TODO: should use the correct type to decode
"LiteralInt32"
} else {
kind
}
})
}

/// Returns the underlying type used in operand kind enums for the operand
Expand Down

0 comments on commit a1cc4de

Please sign in to comment.