-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
1413 lines (1288 loc) · 49.9 KB
/
Copy pathbuild.rs
File metadata and controls
1413 lines (1288 loc) · 49.9 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use convert_case::{Case, Casing};
use glob::glob;
use regex::Regex;
use std::env;
use std::path::{Path, PathBuf};
use syn::parse::ParseStream;
use syn::{
Token, parse_quote,
spanned::Spanned,
visit::{self, Visit},
visit_mut::VisitMut,
};
fn main() {
let build_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let out_dir =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR always present in build scripts"));
let c_dir = build_dir.join("libpg_query");
println!("cargo:rerun-if-changed=libpg_query");
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=pg_raw_parse");
// Bindgen args that are needed both for the C bindings and the node enum
// codegen
let bindgen = bindgen::builder()
.header("wrapper.h")
.generate_comments(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.allowlist_file(
c_dir
.join("src/postgres/include/nodes/parsenodes.h")
.to_str()
.unwrap(),
)
.allowlist_file(
c_dir
.join("src/postgres/include/nodes/primnodes.h")
.to_str()
.unwrap(),
)
.derive_copy(false)
.default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop)
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.clang_arg(format!("-I{}", build_dir.join("include").display()))
.clang_arg(format!("-I{}", c_dir.display()))
.clang_arg(format!(
"-I{}",
c_dir.join("src/postgres/include").display()
));
let mut node_bindings = String::new();
bindgen
.clone()
// Exclude Node types that aren't parse nodes and would require
// additional logic to support
.blocklist_type("Const")
.blocklist_type("Expr")
.blocklist_type("JsonTablePath")
.blocklist_type("JsonTablePlan")
.blocklist_type("RelabelType")
.blocklist_type("Var")
// Yes, we want doc comments
.clang_arg("-fparse-all-comments")
.derive_debug(false)
.generate()
.unwrap()
// SAFETY: YOLO
.write(Box::new(unsafe { node_bindings.as_mut_vec() }))
.unwrap();
let node_structs =
generate_node_structs(&node_bindings, &out_dir.join("nodes_raw.rs")).unwrap();
generate_node_enum(&node_structs, &out_dir.join("node_enum_raw.rs")).unwrap();
generate_transformer(&node_structs, &out_dir.join("transform_raw.rs")).unwrap();
let mut makefunc_bindings = String::new();
bindgen
.clone()
.allowlist_file(
c_dir
.join("src/postgres/include/nodes/makefuncs.h")
.to_str()
.unwrap(),
)
.allowlist_file(
c_dir
.join("src/postgres/include/nodes/value.h")
.to_str()
.unwrap(),
)
.blocklist_item("makeColumnDef") // Has more logic than we want
.blocklist_item("makeFromExpr") // Declared by libpg_query, not defined
.blocklist_item("makeFuncExpr") // Declared by libpg_query, not defined
.blocklist_item("makeTargetEntry") // Declared by libpg_query, not defined
.blocklist_item("makeTypeName") // We map to the list form, not unqualified
.generate()
.unwrap()
// SAFETY: YOLO
.write(Box::new(unsafe { makefunc_bindings.as_mut_vec() }))
.unwrap();
let make_funcs = generate_make_funcs(&makefunc_bindings, &node_structs, &out_dir).unwrap();
let mut bindgen = bindgen
.allowlist_item("Node")
.allowlist_item("MemoryContext")
.allowlist_item("pg_query_init")
.allowlist_item("AllocSetContextCreateInternal")
.allowlist_item("ALLOCSET_DEFAULT_MINSIZE")
.allowlist_item("ALLOCSET_DEFAULT_INITSIZE")
.allowlist_item("ALLOCSET_DEFAULT_MAXSIZE")
.allowlist_item("get_top_memory_context")
.allowlist_item("MemoryContextSwitchTo")
.allowlist_item("MemoryContextDelete")
.allowlist_item("PgQueryError")
.allowlist_item("pg_query_free_error")
.allowlist_item("pg_query_raw_parse")
.allowlist_item("PgQueryParseMode")
.allowlist_item("wrapped_raw_expression_tree_walker_impl")
.override_abi(
bindgen::Abi::CUnwind,
"wrapped_raw_expression_tree_walker_impl",
)
.allowlist_item("StringInfo")
.allowlist_item("wrapped_raw_deparse")
.allowlist_item("wrapped_pnstrdup")
.allowlist_item("list_copy")
.allowlist_item("lappend")
.allowlist_item("list_insert_nth")
.allowlist_item("list_concat")
.allowlist_item("wrapped_copy_object")
.allowlist_item("newNode")
.allowlist_item("pg_query_normalize")
.allowlist_item("pg_query_free_normalize_result")
.wrap_static_fns(true)
.wrap_static_fns_path(out_dir.join("wrap_static_fns"));
for struct_name in &node_structs {
bindgen = bindgen.blocklist_item(struct_name.name.to_string());
}
for make_func_name in &make_funcs {
bindgen = bindgen.allowlist_item(make_func_name);
}
bindgen
.generate()
.unwrap()
.write_to_file(out_dir.join("bindings.rs"))
.unwrap();
let mut build = cc::Build::new();
build
.files(glob("libpg_query/src/*.c").unwrap().map(Result::unwrap))
.files(
glob("libpg_query/src/postgres/*.c")
.unwrap()
.map(Result::unwrap),
)
.file(out_dir.join("wrap_static_fns.c"))
.file(build_dir.join("copy_pg_error.c"))
// Unfortunately, the linker expects protobuf functions to be present
// even if we're never using them
.file(c_dir.join("vendor/protobuf-c/protobuf-c.c"))
.file(c_dir.join("vendor/xxhash/xxhash.c"))
.file(c_dir.join("protobuf/pg_query.pb-c.c"))
.include(&*c_dir)
.include(c_dir.join("vendor"))
.include(c_dir.join("src/postgres/include"))
.include(c_dir.join("src/include"))
.include(build_dir)
.include(build_dir.join("include"))
.warnings(false)
.compile("pg_raw_parse");
}
struct NodeStruct {
attrs: Vec<syn::Attribute>,
name: syn::Ident,
fields: Vec<NodeField>,
}
impl NodeStruct {
fn tag_expr(&self) -> syn::Expr {
let tag_name = syn::Ident::new(&format!("T_{}", &self.name), self.name.span());
parse_quote!(raw::NodeTag::#tag_name)
}
fn mut_ref_name(&self) -> syn::Ident {
syn::Ident::new(&format!("{}Mut", &self.name), self.name.span())
}
fn transform_function_name(&self) -> syn::Ident {
syn::Ident::new(
&format!("transform_{}", self.name.to_string().to_case(Case::Snake)),
self.name.span(),
)
}
}
struct NodeField {
attrs: Vec<syn::Attribute>,
name: syn::Ident,
ty: NodeFieldType,
}
impl NodeField {
fn vis(&self) -> syn::Visibility {
match self.ty {
NodeFieldType::Primitive(_) | NodeFieldType::PrimitiveSkipConstructor(_) => {
parse_quote!(pub)
}
NodeFieldType::CString => parse_quote!(pub(crate)),
NodeFieldType::Private(_) if self.name == "type_" => parse_quote!(pub(crate)),
_ => syn::Visibility::Inherited,
}
}
fn accessor_method(&self) -> Option<syn::ImplItem> {
use NodeFieldType::*;
let fattrs = &self.attrs;
let fname = &self.name;
let ret = self.ty(&parse_quote!('_));
match &self.ty {
Private(_) | Primitive(_) | PrimitiveSkipConstructor(_) => None,
Node | CastNode(_) | List | CastList(_) => Some(parse_quote! {
#(#fattrs)*
#[inline]
pub fn #fname(&self) -> #ret {
// SAFETY: The lifetime is not longer than self
unsafe { crate::FromNodePtr::from_raw(self.#fname.cast()) }
}
}),
CString => Some(parse_quote! {
#(#fattrs)*
#[inline]
pub fn #fname(&self) -> #ret {
if self.#fname.is_null() {
None
} else {
// SAFETY: PG will always give us a valid string or NULL
Some(
unsafe { std::ffi::CStr::from_ptr(self.#fname) }
.to_str()
.expect("Parsing is always done in UTF-8"),
)
}
}
}),
ConstVal => Some(parse_quote! {
#(#fattrs)*
#[inline]
pub fn #fname(&self) -> #ret {
if self.isnull {
None
} else {
Some(ConstValue::from_raw(&self.val))
}
}
}),
}
}
fn mut_accessor(&self, mem: &syn::Lifetime) -> Option<syn::ItemFn> {
use NodeFieldType::*;
let fname = &self.name;
let func_name = syn::Ident::new(&format!("{fname}_mut"), fname.span());
let inner = &self.ty(mem);
match &self.ty {
// No reason to provide for these, user can just `&mut node.field`
Private(_) | Primitive(_) | PrimitiveSkipConstructor(_) => None,
Node | CastNode(_) | List | CastList(_) => Some(parse_quote! {
#[inline]
pub fn #func_name(&mut self) -> <#inner as FromNodeMut<#mem>>::MutRef<'_> {
// SAFETY: The pointer will always be valid or NULL.
// The lifetime won't outlive self
unsafe {
<#inner as FromNodeMut>::from_raw_mut(&mut self.mut_ref.#fname, self.id)
}
}
}),
// We can't provide mutable strings, and there's no reason to.
CString => None,
// Mutable access to this would basically be mutating either a
// primitive or a string. We don't provide mut accessors for either
// of those, so we don't provide them for this
ConstVal => None,
}
}
fn setter_method(&self, self_expr: syn::Expr, mem: &syn::Lifetime) -> Option<syn::ImplItem> {
let fname = &self.name;
let func_name = syn::Ident::new(&format!("set_{}", fname), fname.span());
let ty = self.setter_ty(mem)?;
let set_expr = self.setter_expr();
Some(parse_quote! {
#[inline]
pub fn #func_name(&mut self, #fname: #ty) {
#self_expr.#fname = #set_expr;
}
})
}
fn debug_expr(&self, debug_expr: syn::Expr) -> syn::Expr {
use NodeFieldType::*;
let fname = &self.name;
let value_expr: syn::Expr = match &self.ty {
Primitive(_) | PrimitiveSkipConstructor(_) => parse_quote!(&self.#fname),
Node | CastNode(_) | List | CastList(_) | CString | ConstVal => {
parse_quote!(&self.#fname())
}
Private(_) => return debug_expr,
};
parse_quote!(#debug_expr.field(stringify!(#fname), #value_expr))
}
fn raw_ty(&self) -> syn::Type {
self.ty.raw_ty()
}
fn ty(&self, lifetime: &syn::Lifetime) -> syn::Type {
self.ty.ty(lifetime)
}
fn constructor_ty(&self, lifetime: &syn::Lifetime) -> Option<syn::Type> {
self.ty.constructor_ty(lifetime)
}
fn setter_ty(&self, lifetime: &syn::Lifetime) -> Option<syn::Type> {
use NodeFieldType::*;
match self.ty {
CString => Some(parse_quote!(Option<PgStr<#lifetime>>)),
_ => self.constructor_ty(lifetime),
}
}
fn as_raw_expr(&self) -> syn::Expr {
use NodeFieldType::*;
let fname = &self.name;
match self.ty {
Primitive(_) | ConstVal => parse_quote!(#fname),
Private(_) | PrimitiveSkipConstructor(_) => parse_quote!(Default::default()),
Node | CastNode(_) | List | CastList(_) => parse_quote!(#fname.into_ptr().cast()),
CString => parse_quote! {
#fname
.map(|s| self.copy_string(s).into_ptr())
.unwrap_or(ptr::null_mut())
},
}
}
fn setter_expr(&self) -> syn::Expr {
use NodeFieldType::*;
let fname = &self.name;
match self.ty {
CString => parse_quote! {
#fname
.map(|f| f.into_ptr())
.unwrap_or(std::ptr::null_mut())
},
PrimitiveSkipConstructor(_) => parse_quote!(#fname),
_ => self.as_raw_expr(),
}
}
fn transform_stmt(
&self,
lt: &syn::Lifetime,
transformer: &syn::Expr,
node: &syn::Expr,
) -> Option<syn::Stmt> {
use NodeFieldType::*;
let mut_accessor = self.mut_accessor(lt)?.sig.ident;
match &self.ty {
Node => {
Some(parse_quote!(#transformer.transform_node(Assignable(#node.#mut_accessor()));))
}
CastNode(inner) => {
let trans_fname =
syn::Ident::new(&format!("transform_{}", snake_case(inner)), inner.span());
Some(parse_quote! {
if let Some(node) = #node.#mut_accessor() {
#transformer.#trans_fname(node);
}
})
}
List => Some(parse_quote!(#transformer.transform_list(#node.#mut_accessor());)),
CastList(inner) => {
let trans_fname = syn::Ident::new(
&format!("transform_{}_list", snake_case(inner)),
inner.span(),
);
Some(parse_quote!(#transformer.#trans_fname(#node.#mut_accessor());))
}
Private(_) | Primitive(_) | PrimitiveSkipConstructor(_) | ConstVal | CString => None,
}
}
}
#[derive(Clone)]
enum NodeFieldType {
Private(syn::Type),
Node,
CastNode(syn::Ident),
List,
CastList(syn::Ident),
CString,
ConstVal,
Primitive(syn::Type),
PrimitiveSkipConstructor(syn::Type),
}
impl NodeFieldType {
fn raw_ty(&self) -> syn::Type {
match self {
Self::Private(t) | Self::Primitive(t) | Self::PrimitiveSkipConstructor(t) => t.clone(),
Self::Node | Self::CastNode(_) | Self::List | Self::CastList(_) => {
parse_quote!(*mut Node)
}
Self::CString => parse_quote!(*mut std::ffi::c_char),
Self::ConstVal => parse_quote!(ValUnion),
}
}
fn ty(&self, lifetime: &syn::Lifetime) -> syn::Type {
match self {
Self::Private(t) | Self::Primitive(t) | Self::PrimitiveSkipConstructor(t) => t.clone(),
Self::Node => parse_quote!(crate::Node<#lifetime>),
Self::CastNode(t) => parse_quote!(Option<&#lifetime crate::nodes::#t>),
Self::List => parse_quote!(&#lifetime crate::list::NodeList),
Self::CastList(t) => parse_quote!(&#lifetime crate::list::CastNodeList<#t>),
Self::CString => parse_quote!(Option<&#lifetime str>),
Self::ConstVal => parse_quote!(Option<crate::const_val::ConstValue<#lifetime>>),
}
}
fn constructor_ty(&self, mem: &syn::Lifetime) -> Option<syn::Type> {
let inner = self.ty(&parse_quote!('_));
match self {
Self::Private(_) | Self::PrimitiveSkipConstructor(_) => None,
Self::Primitive(_) => Some(inner),
Self::ConstVal => Some(parse_quote!(crate::raw::ValUnion)),
Self::Node | Self::CastNode(_) | Self::List | Self::CastList(_) => {
Some(parse_quote!(Unique<#mem, #inner>))
}
// Strings get copied in constructors so we can ignore the input LT
Self::CString => Some(parse_quote!(Option<&str>)),
}
}
}
/// Generates the structs for each node and writes them to the given path.
/// Returns a list of the struct names generated
fn generate_node_structs(
bindings: &str,
path: &Path,
) -> Result<Vec<NodeStruct>, Box<dyn std::error::Error>> {
let file = syn::parse_file(bindings)?;
let mut out_file = syn::File {
shebang: None,
attrs: Vec::new(),
items: Vec::new(),
};
// We're relying on missing imports triggering an error to tell us about
// any fields that need special handling, so we don't want to glob import
// the C bindings. But any type aliases to primitives are fine
let type_aliases_to_import = file.items.iter().filter_map(|item| match item {
syn::Item::Type(t) if is_primitive_alias(t) => Some(&t.ident),
syn::Item::Mod(m) if let Some(name) = enum_module_name(m) => Some(name),
_ => None,
});
out_file.items.push(parse_quote! {
pub use crate::raw::{#(#type_aliases_to_import,)*};
});
let raw_node_structs = file
.items
.iter()
.filter_map(|item| match item {
syn::Item::Struct(s) => Some(s),
_ => None,
})
.filter(|s| {
// We don't want a Node enum variant for Node
s.ident != "Node" &&
// List is its own thing, not a type of Node
s.ident != "List" &&
matches!(
s.fields.iter().next(),
Some(syn::Field {
ty: typ,
..
}) if *typ == parse_quote!(NodeTag::Type)
|| *typ == parse_quote!(Expr)
)
});
let local_struct_names = raw_node_structs
.clone()
.map(|s| &s.ident)
.collect::<Vec<_>>();
let struct_name_regex = local_struct_names
.iter()
.rev()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.join("|");
let type_comment_regex = Regex::new(&format!("[lL]ist \\(?of (#{struct_name_regex})")).unwrap();
let node_structs = raw_node_structs
.map(|s| build_node_struct(s, &type_comment_regex))
.collect::<Vec<_>>();
struct ReferencesLocalStruct<'a> {
local_structs: &'a [syn::Type],
found: bool,
}
impl<'ast> Visit<'ast> for ReferencesLocalStruct<'_> {
fn visit_type(&mut self, node: &'ast syn::Type) {
if self.local_structs.contains(node) {
self.found = true
}
visit::visit_type(self, node);
}
}
let local_struct_types: Vec<syn::Type> = local_struct_names
.iter()
.map(|i| parse_quote!(#i))
.collect();
let references_local_struct = |c| {
let mut visitor = ReferencesLocalStruct {
local_structs: &local_struct_types,
found: false,
};
visitor.visit_item_const(c);
visitor.found
};
// Keep the safety consts from bindgen
let safety_consts = file.items.iter().filter_map(|item| match item {
i @ syn::Item::Const(c) if c.ident == "_" && references_local_struct(c) => Some(i.clone()),
_ => None,
});
struct FixCasing;
impl VisitMut for FixCasing {
fn visit_macro_mut(&mut self, i: &mut syn::Macro) {
if i.path == parse_quote!(::std::mem::offset_of) {
let (ty, field) = i
.parse_body_with(|input: ParseStream| {
let ty = input.parse::<syn::Type>()?;
input.parse::<Token![,]>()?;
let field = input.parse::<syn::Ident>()?;
Ok((ty, field))
})
.unwrap();
let field = snake_case(&field);
i.tokens = parse_quote!(#ty, #field);
}
}
}
let safety_consts = safety_consts.map(|mut c| {
FixCasing.visit_item_mut(&mut c);
c
});
out_file.items.extend(safety_consts);
for s in &node_structs {
let sattrs = &s.attrs;
let sname = &s.name;
let smut = s.mut_ref_name();
for f in &s.fields {
if let NodeFieldType::CastNode(t) = &f.ty
&& !local_struct_names.contains(&t)
{
panic!(
"{sname}.{} is a pointer to {t:?}, which is not a Node. It needs special handling",
f.name
)
}
}
let fattrs = s.fields.iter().map(|f| &f.attrs);
let fvis = s.fields.iter().map(|f| f.vis());
let fnames = s.fields.iter().map(|f| &f.name);
let ftys = s.fields.iter().map(|f| f.raw_ty());
out_file.items.push(parse_quote! {
#(#sattrs)*
pub struct #sname {
#(#(#fattrs)* #fvis #fnames: #ftys,)*
}
});
let accessors = s.fields.iter().filter_map(|f| f.accessor_method());
out_file.items.push(parse_quote! {
impl #sname {
#(#accessors)*
}
});
let debug_expr = s.fields.iter().fold(
parse_quote!(f.debug_struct(stringify!(#sname))),
|expr, field| field.debug_expr(expr),
);
out_file.items.push(parse_quote! {
impl fmt::Debug for #sname {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#debug_expr.finish_non_exhaustive()
}
}
});
out_file.items.push(parse_quote! {
impl<'a> TryFrom<crate::Node<'a>> for &'a #sname {
type Error = crate::Node<'a>;
fn try_from(node: crate::Node<'a>) -> Result<Self, Self::Error> {
match node {
crate::Node::#sname(n) => Ok(n),
n => Err(n),
}
}
}
});
out_file.items.push(parse_quote! {
impl<'a> From<&'a #sname> for crate::Node<'a> {
fn from(node: &'a #sname) -> Self {
Self::#sname(node)
}
}
});
out_file.items.push(parse_quote! {
impl<'a> From<Option<&'a #sname>> for crate::Node<'a> {
fn from(node: Option<&'a #sname>) -> Self {
match node {
Some(node) => Self::from(node),
None => Self::None,
}
}
}
});
let tag = s.tag_expr();
out_file.items.push(parse_quote! {
impl crate::ConstructableNode for #sname {
const TAG: NodeTag::Type = #tag;
}
});
out_file.items.push(parse_quote! {
impl crate::FromNodePtr for &#sname {
unsafe fn from_ptr(tag: NodeTag::Type, ptr: Option<NonNull<Node>>) -> Self {
#sname::check_tag(tag);
let p = ptr.expect("Unexpected NULL ptr").cast();
// SAFETY: We've checked the tag
unsafe { p.as_ref() }
}
}
});
out_file.items.push(parse_quote! {
impl<'mem> crate::FromNodeMut<'mem> for &#sname {
type MutRef<'mutref> = #smut<'mem, 'mutref>;
unsafe fn from_ptr_mut<'mutref>(
tag: Option<NodeTag::Type>,
ptr: &'mutref mut *mut Node,
id: Id<'mem>,
) -> Self::MutRef<'mutref> {
#sname::check_tag(tag.expect("from_ptr_mut called on a NULL pointer"));
// SAFETY: Caller is responsible for making this safe
let mut_ref = unsafe {
std::ptr::from_mut(ptr)
.cast::<&mut #sname>()
.as_mut()
.unwrap()
};
#smut { id, mut_ref }
}
}
});
out_file.items.push(parse_quote! {
impl crate::AsNodeRef for #sname {
type AsRef<'b> = &'b #sname;
type List = crate::list::CastNodeList<#sname>;
}
});
out_file.items.push(parse_quote! {
// SAFETY: Self is a type of node
unsafe impl crate::AsNodePtr for &#sname {
fn as_ptr(self) -> *mut Node {
std::ptr::from_ref(self).cast_mut().cast()
}
}
});
out_file.items.push(parse_quote! {
// SAFETY: No reason we can't share nodes across threads
unsafe impl Send for #sname {}
});
out_file.items.push(parse_quote! {
// SAFETY: No reason we can't share nodes across threads
unsafe impl Sync for #sname {}
});
out_file.items.push(parse_quote! {
#[allow(non_camel_case_types)]
pub struct #smut<'mem, 'mutref> {
pub(crate) id: Id<'mem>,
mut_ref: &'mutref mut &'mutref mut #sname,
}
});
let setters = s
.fields
.iter()
.filter_map(|f| f.setter_method(parse_quote!(self.mut_ref), &parse_quote!('mem)));
let mut_accessors = s
.fields
.iter()
.filter_map(|f| f.mut_accessor(&parse_quote!('mem)));
out_file.items.push(parse_quote! {
impl<'mem, 'mutref> #smut<'mem, 'mutref> {
#(#setters)*
#(#mut_accessors)*
pub fn replace(self, node: Unique<'mem, &#sname>) {
let ptr = node.into_ptr().cast::<#sname>();
// SAFETY: ptr is always a valid pointer or NULL
let new = unsafe { ptr.as_mut() }
.expect(concat!("Cannot replace a ", stringify!(#sname), " with NULL"));
*self.mut_ref = new;
}
pub(crate) fn into_assignment(self) -> *mut *mut raw::Node {
std::ptr::from_mut(self.mut_ref).cast()
}
}
});
out_file.items.push(parse_quote! {
impl<'mem, 'mutref> std::ops::Deref for #smut<'mem, 'mutref> {
type Target = #sname;
fn deref(&self) -> &Self::Target {
self.mut_ref
}
}
});
out_file.items.push(parse_quote! {
impl<'mem, 'mutref> From<#smut<'mem, 'mutref>> for crate::NodeMut<'mem, 'mutref> {
fn from(node: #smut<'mem, 'mutref>) -> Self {
Self::#sname(node)
}
}
});
}
std::fs::write(path, prettyplease::unparse(&out_file))?;
Ok(node_structs)
}
fn generate_node_enum(
node_structs: &[NodeStruct],
path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let mut out_file = syn::File {
shebang: None,
attrs: Vec::new(),
items: Vec::new(),
};
let node_names = node_structs.iter().map(|s| &s.name).collect::<Vec<_>>();
let enum_variants = node_names
.iter()
.map::<syn::Variant, _>(|i| parse_quote!(#i(&'a nodes::#i) = nodes::#i::TAG));
out_file.items.push(parse_quote! {
#[allow(non_camel_case_types)]
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum Node<'a> {
/// A null pointer to a node
None = 0,
NodeList(&'a crate::list::NodeList) = raw::NodeTag::T_List,
#(#enum_variants,)*
/// A pointer to a node that wasn't part of a parse tree, or that
/// pg_raw_parse doesn't know how to generate code for.
Invalid(&'a raw::Node),
}
});
out_file.items.push(parse_quote! {
impl FromNodePtr for Node<'_> {
/// SAFETY: The caller is responsible for ensuring the provided
/// lifetime does not outlast the memory context this Node was
/// allocated in
unsafe fn from_ptr(tag: raw::NodeTag::Type, ptr: Option<NonNull<raw::Node>>) -> Self {
// SAFETY: PG will never return an invalid Node other than NULL
// and the caller is ensuring a valid lifetime
match (tag, ptr) {
(_, None) => Self::None,
#((nodes::#node_names::TAG, Some(ptr)) => {
debug_assert!(ptr.cast::<nodes::#node_names>().is_aligned());
// SAFETY: We're checking the tag
Self::#node_names(unsafe { ptr.cast().as_ref() })
})*
// SAFETY: We're checking the tag
(raw::NodeTag::T_List, Some(ptr)) => {
debug_assert!(ptr.cast::<list::NodeList>().is_aligned());
Self::NodeList(unsafe { ptr.cast().as_ref() })
}
(_, Some(p)) => Self::Invalid(unsafe { p.as_ref() }),
}
}
}
});
out_file.items.push(parse_quote! {
impl AsNodeRef for Node<'_> {
type AsRef<'b> = Node<'b>;
type List = crate::list::NodeList;
}
});
out_file.items.push(parse_quote! {
// SAFETY: We are returning the inner pointer from as_ptr
unsafe impl AsNodePtr for Node<'_> {
fn as_ptr(self) -> *mut raw::Node {
match self {
Self::None => std::ptr::null_mut(),
Self::Invalid(p) => (&raw const *p).cast_mut(),
Self::NodeList(p) => (&raw const *p).cast_mut().cast(),
#(Self::#node_names(p) => (&raw const *p).cast_mut().cast(),)*
}
}
}
});
out_file.items.push(parse_quote! {
impl<'mem> FromNodeMut<'mem> for Node<'_> {
type MutRef<'mutref> = NodeMut<'mem, 'mutref>;
unsafe fn from_ptr_mut<'mutref>(
tag: Option<raw::NodeTag::Type>,
ptr: &'mutref mut *mut raw::Node,
id: Id<'mem>,
) -> Self::MutRef<'mutref> {
// SAFETY: Caller is responsible for making this safe.
unsafe {
match tag {
None => NodeMut::None(ptr, id),
#(Some(nodes::#node_names::TAG) => {
NodeMut::#node_names(<&'mem nodes::#node_names>::from_ptr_mut(tag, ptr, id))
})*
Some(raw::NodeTag::T_List) => NodeMut::NodeList(<&'mem crate::list::NodeList>::from_ptr_mut(tag, ptr, id)),
Some(_) => NodeMut::Invalid(ptr, id),
}
}
}
}
});
let enum_variants = node_names.iter().map::<syn::Variant, _>(
|i| parse_quote!(#i(<&'mem nodes::#i as FromNodeMut<'mem>>::MutRef<'mutref>) = nodes::#i::TAG),
);
out_file.items.push(parse_quote! {
#[allow(non_camel_case_types)]
#[repr(u32)]
pub enum NodeMut<'mem, 'mutref> {
/// A NULL pointer to a node
None(&'mutref mut *mut raw::Node, Id<'mem>) = 0,
NodeList(<&'mem crate::list::NodeList as FromNodeMut<'mem>>::MutRef<'mutref>) = raw::NodeTag::T_List,
#(#enum_variants,)*
/// A pointer to a node that wasn't part of a parse tree, or that
/// pg_raw_parse doesn't know how to generate code for.
Invalid(&'mutref mut *mut raw::Node, Id<'mem>),
}
});
out_file.items.push(parse_quote! {
impl<'mem, 'mutref> NodeMut<'mem, 'mutref> {
pub fn as_ref(&self) -> Node<'_> {
match self {
Self::None(..) => Node::None,
Self::NodeList(list) => Node::NodeList(list),
#(Self::#node_names(n) => Node::#node_names(n),)*
// SAFETY: This was always constructed with a valid pointer
Self::Invalid(ptr, _) => Node::Invalid(unsafe { (*ptr).as_ref() }.unwrap())
}
}
/// Returns the lifetime brand for the memory context this points
/// to
pub(crate) fn id(&self) -> Id<'mem> {
match self {
Self::None(_, id) => *id,
Self::NodeList(list) => list.id,
#(Self::#node_names(n) => n.id,)*
Self::Invalid(_, id) => *id,
}
}
/// Get the raw pointer representation of this node
pub(crate) fn into_ptr(self) -> *mut raw::Node {
match self {
Self::None(..) => std::ptr::null_mut(),
Self::NodeList(list) => list.as_ptr(),
#(Self::#node_names(p) => p.as_ptr(),)*
Self::Invalid(p, _) => *p,
}
}
/// Assign a node in-place
///
/// # Safety
///
/// This NodeMut must not have been constructed from a node that
/// is expected to be of a specific type
pub(crate) unsafe fn replace(self, node: make::Unique<'mem, Node<'_>>) {
let ref_mut = match self {
Self::None(ref_mut, _) => ptr::from_mut(ref_mut).cast(),
Self::NodeList(list) => list.into_assignment(),
#(Self::#node_names(p) => p.into_assignment(),)*
Self::Invalid(ref_mut, _) => ptr::from_mut(ref_mut),
};
// SAFETY: Caller is responsible for making this safe
unsafe { *ref_mut = node.into_ptr() };
}
}
});
std::fs::write(path, prettyplease::unparse(&out_file))?;
Ok(())
}
/// Returns the function names needed in `raw`
fn generate_make_funcs(
bindings: &str,
node_structs: &[NodeStruct],
out_dir: &Path,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let file = syn::parse_file(bindings)?;
let makefuncs = file
.items
.into_iter()
.flat_map(|i| match i {
syn::Item::ForeignMod(f) => f.items,
_ => Vec::new(),
})
.filter_map(|i| match i {
syn::ForeignItem::Fn(f) => Some(f),
_ => None,
})
.filter_map(|f| {
let fname = f.sig.ident.to_string();
if fname.starts_with("make")
&& let Some(s) = node_structs.iter().find(|s| s.name == fname[4..])
{
Some((s, f))
} else {
None
}
})
.collect::<Vec<_>>();
let mem = parse_quote!('mem);
let items = makefuncs.iter().map(|(node, makefunc)| -> syn::ImplItem {
let node_name = &node.name;
let raw_func_name = &makefunc.sig.ident;
let func_name = snake_case(raw_func_name);
let arg_fields = makefunc
.sig
.inputs
.iter()
.filter_map(|arg| match arg {
syn::FnArg::Typed(pat_type) => Some(pat_type),
_ => None,