Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sr: refactor codegen with matches over if/else #50

Merged
merged 1 commit into from
Jul 21, 2019
Merged
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
67 changes: 30 additions & 37 deletions codegen/sr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@ use proc_macro2::{Ident, Span, TokenStream};
/// Returns the corresponding Rust type used in structured representation
/// for the given operand kind in the SPIR-V JSON grammar.
pub fn get_operand_type_sr_tokens(kind: &str) -> TokenStream {
if kind.starts_with("Id") {
quote! { spirv::Word }
} else if kind == "LiteralInteger" || kind == "LiteralExtInstInteger" {
quote! { u32 }
} else if kind == "LiteralSpecConstantOpInteger" {
quote! { spirv::Op }
} else if kind == "LiteralContextDependentNumber" {
panic!("this kind is not expected to be handled here")
} else if kind == "LiteralString" {
quote! { String }
} else if kind == "PairLiteralIntegerIdRef" {
quote! { (u32, spirv::Word) }
} else if kind == "PairIdRefLiteralInteger" {
quote! { (spirv::Word, u32) }
} else if kind == "PairIdRefIdRef" {
quote! { (spirv::Word, spirv::Word) }
} else {
let kind = Ident::new(kind, Span::call_site());
quote! { spirv::#kind }
match kind {
"IdMemorySemantics" | "IdScope" | "IdRef" | "IdResult" => quote! { spirv::Word },
"LiteralInteger" | "LiteralExtInstInteger" => quote! { u32 },
"LiteralSpecConstantOpInteger" => quote! { spirv::Op },
"LiteralContextDependentNumber" => panic!("this kind is not expected to be handled here"),
"LiteralString" => quote! { String },
"PairLiteralIntegerIdRef" => quote! { (u32, spirv::Word) },
"PairIdRefLiteralInteger" => quote! { (spirv::Word, u32) },
"PairIdRefIdRef" => quote! { (spirv::Word, spirv::Word) },
_ => {
let kind = Ident::new(kind, Span::call_site());
quote! { spirv::#kind }
}
}
}

Expand Down Expand Up @@ -101,23 +95,27 @@ pub fn gen_sr_decoration(grammar: &structs::Grammar) -> String {
tokens.to_string()
}

pub fn get_operand_type_ident(grammar: &structs::Operand) -> TokenStream {
let ty = if grammar.kind == "IdRef" {
if grammar.name == "'Length'" {
pub fn get_quantified_type_tokens(ty: TokenStream, quantifier: &str) -> TokenStream {
match quantifier {
"" => quote! { #ty },
"?" => quote! { Option<#ty> },
"*" => quote! { Vec<#ty> },
other => panic!("wrong quantifier: {}", other),
}
}

pub fn get_operand_type_ident(operand: &structs::Operand) -> TokenStream {
let ty = if operand.kind == "IdRef" {
if operand.name == "'Length'" {
quote! { ConstantToken }
} else {
quote! { TypeToken }
}
} else {
get_operand_type_sr_tokens(&grammar.kind)
get_operand_type_sr_tokens(&operand.kind)
};
if grammar.quantifier.is_empty() {
quote! { #ty }
} else if grammar.quantifier == "?" {
quote! { Option<#ty> }
} else {
quote! { Vec<#ty> }
}

get_quantified_type_tokens(ty, &operand.quantifier)
}

fn get_type_fn_name(name: &str) -> String {
Expand Down Expand Up @@ -300,13 +298,8 @@ pub fn gen_sr_instruction(grammar: &structs::Grammar) -> String {
} else {
let field_name = get_operand_name_sr_tokens(operand);
let field_type = get_operand_type_sr_tokens(&operand.kind);
if operand.quantifier == "" {
Some(quote! { #field_name : #field_type })
} else if operand.quantifier == "?" {
Some(quote! { #field_name : Option<#field_type> })
} else {
Some(quote! { #field_name : Vec<#field_type> })
}
let quantified = get_quantified_type_tokens(field_type, &operand.quantifier);
Some(quote! { #field_name : #quantified })
}
}).collect();
let params = if params.is_empty() {
Expand Down