risingwave_expr_macro/
parse.rs

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
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

//! Parse the tokens of the macro.

use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{LitStr, Token};

use super::*;

impl Parse for FunctionAttr {
    /// Parse the attribute of the function macro.
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let mut parsed = Self::default();

        let sig = input.parse::<LitStr>()?;
        let sig_str = sig.value();
        let (name_args, ret) = match sig_str.split_once("->") {
            Some((name_args, ret)) => (name_args, ret),
            None => (sig_str.as_str(), "void"),
        };
        let (name, args) = name_args
            .split_once('(')
            .ok_or_else(|| Error::new_spanned(&sig, "expected '('"))?;
        let args = args.trim_start().trim_end_matches([')', ' ']);
        let (is_table_function, ret) = match ret.trim_start().strip_prefix("setof") {
            Some(s) => (true, s),
            None => (false, ret),
        };
        parsed.name = name.trim().to_string();
        parsed.args = if args.is_empty() {
            vec![]
        } else {
            args.split(',').map(|s| s.trim().to_string()).collect()
        };
        parsed.ret = ret.trim().to_string();
        parsed.is_table_function = is_table_function;

        if input.parse::<Token![,]>().is_err() {
            return Ok(parsed);
        }

        let metas = input.parse_terminated(syn::Meta::parse, Token![,])?;
        for meta in metas {
            let get_value = || {
                let kv = meta.require_name_value()?;
                let syn::Expr::Lit(lit) = &kv.value else {
                    return Err(Error::new(kv.value.span(), "expected literal"));
                };
                let syn::Lit::Str(lit) = &lit.lit else {
                    return Err(Error::new(kv.value.span(), "expected string literal"));
                };
                Ok(lit.value())
            };
            if meta.path().is_ident("batch_fn") {
                parsed.batch_fn = Some(get_value()?);
            } else if meta.path().is_ident("state") {
                parsed.state = Some(get_value()?);
            } else if meta.path().is_ident("init_state") {
                parsed.init_state = Some(get_value()?);
            } else if meta.path().is_ident("prebuild") {
                parsed.prebuild = Some(get_value()?);
            } else if meta.path().is_ident("type_infer") {
                parsed.type_infer = Some(get_value()?);
            } else if meta.path().is_ident("generic") {
                parsed.generic = Some(get_value()?);
            } else if meta.path().is_ident("volatile") {
                parsed.volatile = true;
            } else if meta.path().is_ident("deprecated") || meta.path().is_ident("internal") {
                parsed.deprecated = true;
            } else if meta.path().is_ident("rewritten") {
                parsed.rewritten = true;
            } else if meta.path().is_ident("append_only") {
                parsed.append_only = true;
            } else {
                return Err(Error::new(
                    meta.span(),
                    format!("invalid property: {:?}", meta.path()),
                ));
            }
        }
        Ok(parsed)
    }
}

impl Parse for UserFunctionAttr {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let itemfn: syn::ItemFn = input.parse()?;
        Ok(UserFunctionAttr::from(&itemfn.sig))
    }
}

impl From<&syn::Signature> for UserFunctionAttr {
    fn from(sig: &syn::Signature) -> Self {
        let (return_type_kind, iterator_item_kind, core_return_type) = match &sig.output {
            syn::ReturnType::Default => (ReturnTypeKind::T, None, "()".into()),
            syn::ReturnType::Type(_, ty) => {
                let (kind, inner) = check_type(ty);
                match strip_iterator(inner) {
                    Some(ty) => {
                        let (inner_kind, inner) = check_type(ty);
                        (kind, Some(inner_kind), inner.to_token_stream().to_string())
                    }
                    None => (kind, None, inner.to_token_stream().to_string()),
                }
            }
        };
        UserFunctionAttr {
            name: sig.ident.to_string(),
            async_: sig.asyncness.is_some(),
            write: sig.inputs.iter().any(arg_is_write),
            context: sig.inputs.iter().any(arg_is_context),
            retract: last_arg_is_retract(sig),
            args_option: sig.inputs.iter().map(arg_is_option).collect(),
            first_mut_ref_arg: first_mut_ref_arg(sig),
            return_type_kind,
            iterator_item_kind,
            core_return_type,
            generic: sig.generics.params.len(),
            return_type_span: sig.output.span(),
        }
    }
}

impl Parse for AggregateImpl {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let itemimpl: syn::ItemImpl = input.parse()?;
        let parse_function = |name: &str| {
            itemimpl.items.iter().find_map(|item| match item {
                syn::ImplItem::Fn(syn::ImplItemFn { sig, .. }) if sig.ident == name => {
                    Some(UserFunctionAttr::from(sig))
                }
                _ => None,
            })
        };
        let self_path = itemimpl.self_ty.to_token_stream().to_string();
        let struct_name = match self_path.split_once('<') {
            Some((path, _)) => path.trim().into(), // remove generic parameters
            None => self_path,
        };
        Ok(AggregateImpl {
            struct_name,
            accumulate: parse_function("accumulate").expect("expect accumulate function"),
            retract: parse_function("retract"),
            merge: parse_function("merge"),
            finalize: parse_function("finalize"),
            create_state: parse_function("create_state"),
            encode_state: parse_function("encode_state"),
            decode_state: parse_function("decode_state"),
        })
    }
}

impl Parse for AggregateFnOrImpl {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        // consume attributes
        let _ = input.call(syn::Attribute::parse_outer)?;
        if input.peek(Token![impl]) {
            Ok(AggregateFnOrImpl::Impl(input.parse()?))
        } else {
            Ok(AggregateFnOrImpl::Fn(input.parse()?))
        }
    }
}

/// Check if the argument is `&mut impl Write`.
fn arg_is_write(arg: &syn::FnArg) -> bool {
    let syn::FnArg::Typed(arg) = arg else {
        return false;
    };
    let syn::Type::Reference(syn::TypeReference { elem, .. }) = arg.ty.as_ref() else {
        return false;
    };
    let syn::Type::ImplTrait(syn::TypeImplTrait { bounds, .. }) = elem.as_ref() else {
        return false;
    };
    let Some(syn::TypeParamBound::Trait(syn::TraitBound { path, .. })) = bounds.first() else {
        return false;
    };
    let Some(seg) = path.segments.last() else {
        return false;
    };
    seg.ident == "Write"
}

/// Check if the argument is `&Context`.
fn arg_is_context(arg: &syn::FnArg) -> bool {
    let syn::FnArg::Typed(arg) = arg else {
        return false;
    };
    let syn::Type::Reference(syn::TypeReference { elem, .. }) = arg.ty.as_ref() else {
        return false;
    };
    let syn::Type::Path(path) = elem.as_ref() else {
        return false;
    };
    let Some(seg) = path.path.segments.last() else {
        return false;
    };
    seg.ident == "Context"
}

/// Check if the last argument is `retract: bool`.
fn last_arg_is_retract(sig: &syn::Signature) -> bool {
    let Some(syn::FnArg::Typed(arg)) = sig.inputs.last() else {
        return false;
    };
    let syn::Pat::Ident(pat) = &*arg.pat else {
        return false;
    };
    pat.ident.to_string().contains("retract")
}

/// Check if the argument is `Option`.
fn arg_is_option(arg: &syn::FnArg) -> bool {
    let syn::FnArg::Typed(arg) = arg else {
        return false;
    };
    let syn::Type::Path(path) = arg.ty.as_ref() else {
        return false;
    };
    let Some(seg) = path.path.segments.last() else {
        return false;
    };
    seg.ident == "Option"
}

/// Returns `T` if the first argument (except `self`) is `&mut T`.
fn first_mut_ref_arg(sig: &syn::Signature) -> Option<String> {
    let arg = match sig.inputs.first()? {
        syn::FnArg::Typed(arg) => arg,
        syn::FnArg::Receiver(_) => match sig.inputs.iter().nth(1)? {
            syn::FnArg::Typed(arg) => arg,
            _ => return None,
        },
    };
    let syn::Type::Reference(syn::TypeReference {
        elem,
        mutability: Some(_),
        ..
    }) = arg.ty.as_ref()
    else {
        return None;
    };
    Some(elem.to_token_stream().to_string())
}

/// Check the return type.
fn check_type(ty: &syn::Type) -> (ReturnTypeKind, &syn::Type) {
    if let Some(inner) = strip_outer_type(ty, "Result") {
        if let Some(inner) = strip_outer_type(inner, "Option") {
            (ReturnTypeKind::ResultOption, inner)
        } else {
            (ReturnTypeKind::Result, inner)
        }
    } else if let Some(inner) = strip_outer_type(ty, "Option") {
        (ReturnTypeKind::Option, inner)
    } else if let Some(inner) = strip_outer_type(ty, "DatumRef") {
        (ReturnTypeKind::Option, inner)
    } else {
        (ReturnTypeKind::T, ty)
    }
}

/// Check if the type is `type_<T>` and return `T`.
fn strip_outer_type<'a>(ty: &'a syn::Type, type_: &str) -> Option<&'a syn::Type> {
    let syn::Type::Path(path) = ty else {
        return None;
    };
    let seg = path.path.segments.last()?;
    if seg.ident != type_ {
        return None;
    }
    let syn::PathArguments::AngleBracketed(args) = &seg.arguments else {
        return None;
    };
    let Some(syn::GenericArgument::Type(ty)) = args.args.first() else {
        return None;
    };
    Some(ty)
}

/// Check if the type is `impl Iterator<Item = T>` and return `T`.
fn strip_iterator(ty: &syn::Type) -> Option<&syn::Type> {
    let syn::Type::ImplTrait(impl_trait) = ty else {
        return None;
    };
    let syn::TypeParamBound::Trait(trait_bound) = impl_trait.bounds.first()? else {
        return None;
    };
    let segment = trait_bound.path.segments.last().unwrap();
    if segment.ident != "Iterator" {
        return None;
    }
    let syn::PathArguments::AngleBracketed(angle_bracketed) = &segment.arguments else {
        return None;
    };
    for arg in &angle_bracketed.args {
        if let syn::GenericArgument::AssocType(b) = arg
            && b.ident == "Item"
        {
            return Some(&b.ty);
        }
    }
    None
}