Debian Codegraph @

src/nouveau/compiler/nak/ir_proc.rs

raw
1 // Copyright © 2023 Collabora, Ltd.
2 // SPDX-License-Identifier: MIT
3
4 extern crate proc_macro;
5 extern crate proc_macro2;
6 #[macro_use]
7 extern crate quote;
8 extern crate syn;
9
10 use compiler_proc::as_slice::*;
11 use proc_macro::TokenStream;
12 use proc_macro2::TokenStream as TokenStream2;
13 use syn::*;
14
15 #[proc_macro_derive(SrcsAsSlice, attributes(src_type))]
16 pub fn derive_srcs_as_slice(input: TokenStream) -> TokenStream {
17 derive_as_slice(input, "Src", "src_type", "SrcType")
18 }
19
20 #[proc_macro_derive(DstsAsSlice, attributes(dst_type))]
21 pub fn derive_dsts_as_slice(input: TokenStream) -> TokenStream {
22 derive_as_slice(input, "Dst", "dst_type", "DstType")
23 }
24
25 #[proc_macro_derive(DisplayOp)]
26 pub fn enum_derive_display_op(input: TokenStream) -> TokenStream {
27 let DeriveInput { ident, data, .. } = parse_macro_input!(input);
28
29 if let Data::Enum(e) = data {
30 let mut fmt_dsts_cases = TokenStream2::new();
31 let mut fmt_op_cases = TokenStream2::new();
32 for v in e.variants {
33 let case = v.ident;
34 fmt_dsts_cases.extend(quote! {
35 #ident::#case(x) => x.fmt_dsts(f),
36 });
37 fmt_op_cases.extend(quote! {
38 #ident::#case(x) => x.fmt_op(f),
39 });
40 }
41 quote! {
42 impl DisplayOp for #ident {
43 fn fmt_dsts(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 #fmt_dsts_cases
46 }
47 }
48
49 fn fmt_op(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 #fmt_op_cases
52 }
53 }
54 }
55 }
56 .into()
57 } else {
58 panic!("Not an enum type");
59 }
60 }
61
62 fn into_box_inner_type<'a>(from_type: &'a syn::Type) -> Option<&'a syn::Type> {
63 let last = match from_type {
64 Type::Path(TypePath { path, .. }) => path.segments.last()?,
65 _ => return None,
66 };
67
68 if last.ident != "Box" {
69 return None;
70 }
71
72 let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
73 args,
74 ..
75 }) = &last.arguments
76 else {
77 panic!("Expected Box<T> (with angle brackets)");
78 };
79
80 for arg in args {
81 if let GenericArgument::Type(inner_type) = arg {
82 return Some(inner_type);
83 }
84 }
85 panic!("Expected Box to use a type argument");
86 }
87
88 #[proc_macro_derive(FromVariants)]
89 pub fn derive_from_variants(input: TokenStream) -> TokenStream {
90 let DeriveInput { ident, data, .. } = parse_macro_input!(input);
91 let enum_type = ident;
92
93 let mut impls = TokenStream2::new();
94
95 if let Data::Enum(e) = data {
96 for v in e.variants {
97 let var_ident = v.ident;
98 let from_type = match v.fields {
99 Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => unnamed,
100 _ => panic!("Expected Op(OpFoo)"),
101 };
102
103 assert!(from_type.len() == 1, "Expected Op(OpFoo)");
104 let from_type = &from_type.first().unwrap().ty;
105
106 let quote = quote! {
107 impl From<#from_type> for #enum_type {
108 fn from (op: #from_type) -> #enum_type {
109 #enum_type::#var_ident(op)
110 }
111 }
112 };
113
114 impls.extend(quote);
115
116 if let Some(inner_type) = into_box_inner_type(from_type) {
117 let quote = quote! {
118 impl From<#inner_type> for #enum_type {
119 fn from(value: #inner_type) -> Self {
120 From::from(Box::new(value))
121 }
122 }
123 };
124
125 impls.extend(quote);
126 }
127 }
128 }
129
130 impls.into()
131 }