|
| 1 | +pub mod auto_spec; |
| 2 | + |
| 3 | +use proc_macro2::TokenStream; |
| 4 | +use verus_syn::{Attribute, ImplItem, Item, Meta, Path}; |
| 5 | + |
| 6 | +fn item_attrs(item: &Item) -> Option<&Vec<Attribute>> { |
| 7 | + match item { |
| 8 | + Item::Const(i) => Some(&i.attrs), |
| 9 | + Item::Enum(i) => Some(&i.attrs), |
| 10 | + Item::ExternCrate(i) => Some(&i.attrs), |
| 11 | + Item::Fn(i) => Some(&i.attrs), |
| 12 | + Item::ForeignMod(i) => Some(&i.attrs), |
| 13 | + Item::Impl(i) => Some(&i.attrs), |
| 14 | + Item::Macro(i) => Some(&i.attrs), |
| 15 | + Item::Mod(i) => Some(&i.attrs), |
| 16 | + Item::Static(i) => Some(&i.attrs), |
| 17 | + Item::Struct(i) => Some(&i.attrs), |
| 18 | + Item::Trait(i) => Some(&i.attrs), |
| 19 | + Item::TraitAlias(i) => Some(&i.attrs), |
| 20 | + Item::Type(i) => Some(&i.attrs), |
| 21 | + Item::Union(i) => Some(&i.attrs), |
| 22 | + Item::Use(i) => Some(&i.attrs), |
| 23 | + Item::Global(i) => Some(&i.attrs), |
| 24 | + Item::BroadcastUse(i) => Some(&i.attrs), |
| 25 | + Item::BroadcastGroup(i) => Some(&i.attrs), |
| 26 | + Item::AssumeSpecification(i) => Some(&i.attrs), |
| 27 | + Item::Verbatim(_) => None, |
| 28 | + _ => { |
| 29 | + panic!("Item is non_exhaustive, preventing us from catching this panic statically") |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn impl_item_attrs(item: &ImplItem) -> Option<&Vec<Attribute>> { |
| 35 | + match item { |
| 36 | + ImplItem::Const(i) => Some(&i.attrs), |
| 37 | + ImplItem::Fn(i) => Some(&i.attrs), |
| 38 | + ImplItem::Type(i) => Some(&i.attrs), |
| 39 | + ImplItem::Macro(i) => Some(&i.attrs), |
| 40 | + ImplItem::BroadcastGroup(i) => Some(&i.attrs), |
| 41 | + ImplItem::Verbatim(_) => None, |
| 42 | + _ => { |
| 43 | + panic!("ImplItem is non_exhaustive, preventing us from catching this panic statically") |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn traverse_path(path: &Path) -> Option<String> { |
| 49 | + if path.leading_colon.is_some() { |
| 50 | + return None; |
| 51 | + } |
| 52 | + let segments: Vec<String> = path.segments.iter().map(|s| s.ident.to_string()).collect(); |
| 53 | + let segments: Vec<&str> = segments.iter().map(|s| s.as_str()).collect(); |
| 54 | + match &segments[..] { |
| 55 | + [s] => Some(s.to_string()), |
| 56 | + ["contrib", s] => Some(s.to_string()), |
| 57 | + ["vstd", "contrib", s] => Some(s.to_string()), |
| 58 | + _ => None, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn collect_attrs(attrs: Option<&Vec<Attribute>>) -> Vec<(String, Option<TokenStream>)> { |
| 63 | + let Some(attrs) = attrs else { |
| 64 | + return vec![]; |
| 65 | + }; |
| 66 | + let mut attr_infos: Vec<(String, Option<TokenStream>)> = Vec::new(); |
| 67 | + for attr in attrs { |
| 68 | + let Some(name) = traverse_path(attr.path()) else { |
| 69 | + continue; |
| 70 | + }; |
| 71 | + let tokens = match &attr.meta { |
| 72 | + Meta::Path(_) => None, |
| 73 | + Meta::List(list) => Some(list.tokens.clone()), |
| 74 | + Meta::NameValue(_) => None, |
| 75 | + }; |
| 76 | + attr_infos.push((name, tokens)); |
| 77 | + } |
| 78 | + attr_infos |
| 79 | +} |
| 80 | + |
| 81 | +// It's often more useful to apply a macro to a verus_syn::Item before it's transformed |
| 82 | +// by "verus!" into a verbose syn encoding. |
| 83 | +// For example, we may want to look for "proof { ... }" blocks or change the mode of a Fn, |
| 84 | +// and it's better to do these directly in verus_syn syntax than to try to reverse engineer |
| 85 | +// the syn encoding of these features. |
| 86 | +// Therefore, we give contrib macros a chance to preprocess the verus_syn code here. |
| 87 | + |
| 88 | +// Unfortunately, name resolution hasn't run on item's attributes yet, |
| 89 | +// so we don't have a good way to identify which macro is which. |
| 90 | +// As a hack, we look for any of: |
| 91 | +// - #[vstd::contrib::m(args)] |
| 92 | +// - #[contrib::m(args)] |
| 93 | +// - #[m(args)] |
| 94 | +// where m is on the list of contrib macros and (args) is optional. |
| 95 | + |
| 96 | +pub(crate) fn contrib_preprocess_item(item: &mut Item, new_items: &mut Vec<Item>) { |
| 97 | + for (name, tokens) in collect_attrs(item_attrs(item)) { |
| 98 | + match name.as_str() { |
| 99 | + // Add contrib macros needing preprocessing here: |
| 100 | + "auto_spec" => auto_spec::auto_spec_item(item, tokens, new_items), |
| 101 | + _ => {} |
| 102 | + }; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +pub(crate) fn contrib_preprocess_impl_item(item: &mut ImplItem, new_items: &mut Vec<ImplItem>) { |
| 107 | + for (name, tokens) in collect_attrs(impl_item_attrs(item)) { |
| 108 | + match name.as_str() { |
| 109 | + // Add contrib macros needing preprocessing here: |
| 110 | + "auto_spec" => auto_spec::auto_spec_impl_item(item, tokens, new_items), |
| 111 | + _ => {} |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +pub(crate) fn contrib_preprocess_items(items: &mut Vec<Item>) { |
| 117 | + let mut i = 0; |
| 118 | + while i < items.len() { |
| 119 | + let mut new_items: Vec<Item> = Vec::new(); |
| 120 | + contrib_preprocess_item(&mut items[i], &mut new_items); |
| 121 | + // Add new items and preprocess the new items as well: |
| 122 | + items.extend(new_items); |
| 123 | + i += 1; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +pub(crate) fn contrib_preprocess_impl_items(items: &mut Vec<ImplItem>) { |
| 128 | + let mut i = 0; |
| 129 | + while i < items.len() { |
| 130 | + let mut new_items: Vec<ImplItem> = Vec::new(); |
| 131 | + contrib_preprocess_impl_item(&mut items[i], &mut new_items); |
| 132 | + // Add new items and preprocess the new items as well: |
| 133 | + items.extend(new_items); |
| 134 | + i += 1; |
| 135 | + } |
| 136 | +} |
0 commit comments