1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
extern crate aster;
extern crate syntex_syntax as syntax;
extern crate hyper;
extern crate json;
use std::io::prelude::*;
use std::fs::File;
use hyper::Client;
use aster::*;
use syntax::print::pprust::*;
fn main() {
let client = Client::new();
let mut res = client.get(
"http://www.khronos.org/registry/spir-v/api/1.1/spirv.json"
).send().expect("could not get language definitions");
let mut data = String::new();
res.read_to_string(&mut data).expect("could not read response");
let spirv = json::parse(&data).expect("could not parse input data");
let code = spirv["spv"]["enum"].members()
.filter_map(|enum_| {
if enum_["Name"] == "Op" {
return None;
}
let builder = AstBuilder::new();
let name = enum_["Name"].as_str().expect("name is not a string");
let mut entries: Vec<_> = enum_["Values"].entries().collect();
entries.sort_by(|&(_, a), &(_, b)| a.as_u32().unwrap().cmp(&b.as_u32().unwrap()));
let mut result = String::new();
if enum_["Type"] == "Value" {
let enum_ = builder.item()
.attr()
.allow(vec!["non_camel_case_types"])
.attr()
.list("derive")
.word("Debug")
.word("Copy")
.word("Clone")
.build()
.pub_().enum_(name)
.ids(
entries.iter()
.map(|&(name, _)| name)
)
.build();
let impl_ = builder.item()
.impl_()
.trait_()
.id("Into<u32>")
.build()
.item("into")
.method().fn_decl()
.self_().value()
.return_().u32()
.block()
.expr().match_().self_()
.with_arms(
entries.iter()
.map(|&(entry, val)|
builder.arm().pat().path().id(name).id(entry).build()
.body().u32(val.as_u32().unwrap())
)
)
.build()
.build_ty(builder.ty().id(name));
result = result + &item_to_string(&enum_) + "\n" + &item_to_string(&impl_) + "\n";
} else {
let struct_ = builder.item()
.attr()
.allow(vec!["non_snake_case"])
.attr()
.list("derive")
.word("Debug")
.word("Copy")
.word("Clone")
.word("Default")
.build()
.pub_().struct_(name)
.with_fields(
entries.iter()
.map(|&(name, _)|
builder.struct_field(name)
.pub_().ty().bool()
)
)
.build();
let impl_ = builder.item()
.impl_()
.trait_()
.id("Into<u32>")
.build()
.item("into")
.method().fn_decl()
.self_().value()
.return_().u32()
.block()
.stmt().let_().mut_id("value").expr().u32(0)
.with_stmts(
entries.iter()
.map(|&(name, value)|
builder.stmt()
.expr().if_().field(name).self_()
.then().stmt()
.expr().bit_or_assign().id("value").u32(1 << value.as_u32().unwrap())
.build().build()
)
)
.expr().id("value")
.build_ty(builder.ty().id(name));
result = result + &item_to_string(&struct_) + "\n" + &item_to_string(&impl_) + "\n";
};
Some(result)
})
.fold(String::new(), |acc, itm| acc + &itm + "\n");
let mut out = File::create(
concat!(env!("OUT_DIR"), "/spirv.rs")
).expect("could not create output file");
out.write_fmt(format_args!("{}", code)).expect("could not write output to file");
}