Skip to content

Commit 27fc7cd

Browse files
author
Paolo Tranquilli
committed
Rust: use macro for some boilerplate code
1 parent aa129a7 commit 27fc7cd

File tree

3 files changed

+75
-131
lines changed

3 files changed

+75
-131
lines changed

rust/ast-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ fn write_extractor(grammar: &AstSrc) -> std::io::Result<String> {
456456
457457
use super::base::Translator;
458458
use super::mappings::TextValue;
459-
use crate::emit_detached;
459+
use crate::{{emit_detached,emit_canonical_origin}};
460460
use crate::generated;
461461
use crate::trap::{{Label, TrapId}};
462462
use ra_ap_syntax::ast::{{

rust/extractor/src/translate/base.rs

Lines changed: 73 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,67 @@ macro_rules! emit_detached {
3434
$self.extract_macro_call_expanded(&$node, $label);
3535
};
3636
(Function, $self:ident, $node:ident, $label:ident) => {
37-
$self.extract_canonical_origin_of_function(&$node, $label);
37+
emit_canonical_origin!(
38+
$self,
39+
$node,
40+
$label,
41+
to_fn_def,
42+
canonical_path_from_function
43+
);
3844
};
3945
(Trait, $self:ident, $node:ident, $label:ident) => {
40-
$self.extract_canonical_origin_of_trait(&$node, $label);
46+
emit_canonical_origin!(
47+
$self,
48+
$node,
49+
$label,
50+
to_trait_def,
51+
canonical_path_from_module_item
52+
);
4153
};
4254
(Struct, $self:ident, $node:ident, $label:ident) => {
43-
$self.extract_canonical_origin_of_struct(&$node, $label);
55+
emit_canonical_origin!(
56+
$self,
57+
$node,
58+
$label,
59+
to_struct_def,
60+
canonical_path_from_module_item
61+
);
4462
};
4563
(Enum, $self:ident, $node:ident, $label:ident) => {
46-
$self.extract_canonical_origin_of_enum(&$node, $label);
64+
emit_canonical_origin!(
65+
$self,
66+
$node,
67+
$label,
68+
to_enum_def,
69+
canonical_path_from_module_item
70+
);
4771
};
4872
(Union, $self:ident, $node:ident, $label:ident) => {
49-
$self.extract_canonical_origin_of_union(&$node, $label);
73+
emit_canonical_origin!(
74+
$self,
75+
$node,
76+
$label,
77+
to_union_def,
78+
canonical_path_from_module_item
79+
);
5080
};
5181
(Module, $self:ident, $node:ident, $label:ident) => {
52-
$self.extract_canonical_origin_of_module(&$node, $label);
82+
emit_canonical_origin!(
83+
$self,
84+
$node,
85+
$label,
86+
to_module_def,
87+
canonical_path_from_module
88+
);
5389
};
5490
(Variant, $self:ident, $node:ident, $label:ident) => {
55-
$self.extract_canonical_origin_of_enum_variant(&$node, $label);
91+
emit_canonical_origin!(
92+
$self,
93+
$node,
94+
$label,
95+
to_enum_variant_def,
96+
canonical_path_from_enum_variant
97+
);
5698
};
5799
// TODO canonical origin of other items
58100
(PathExpr, $self:ident, $node:ident, $label:ident) => {
@@ -76,6 +118,23 @@ macro_rules! emit_detached {
76118
($($_:tt)*) => {};
77119
}
78120

121+
#[macro_export]
122+
macro_rules! emit_canonical_origin {
123+
($self:ident, $node:ident, $label:ident, $to_def:ident, $canonical_path_method:ident) => {
124+
(|| {
125+
let sema = $self.semantics.as_ref()?;
126+
let def = sema.$to_def(&$node)?;
127+
let path = $self.$canonical_path_method(def)?;
128+
generated::Addressable::emit_canonical_path(
129+
$label.into(),
130+
path.into(),
131+
&mut $self.trap.writer,
132+
);
133+
Some(())
134+
})();
135+
};
136+
}
137+
79138
pub struct Translator<'a> {
80139
pub trap: TrapFile,
81140
path: &'a str,
@@ -488,7 +547,10 @@ impl<'a> Translator<'a> {
488547
})
489548
}
490549

491-
fn canonical_path_from_module(&mut self, item: Module) -> Option<Label<generated::Namespace>> {
550+
pub(crate) fn canonical_path_from_module(
551+
&mut self,
552+
item: Module,
553+
) -> Option<Label<generated::Namespace>> {
492554
cache_get_or_assign!(self.canonical_path_cache, item, {
493555
// if we have a Hir entity, it means we have semantics
494556
let sema = self.semantics.as_ref().unwrap();
@@ -511,7 +573,7 @@ impl<'a> Translator<'a> {
511573
})
512574
}
513575

514-
fn canonical_path_from_module_item(
576+
pub(crate) fn canonical_path_from_module_item(
515577
&mut self,
516578
item: impl ModuleItem,
517579
) -> Option<Label<generated::ModuleItemCanonicalPath>> {
@@ -560,7 +622,7 @@ impl<'a> Translator<'a> {
560622
})
561623
}
562624

563-
fn canonical_path_from_function(
625+
pub(crate) fn canonical_path_from_function(
564626
&mut self,
565627
item: Function,
566628
) -> Option<Label<generated::CanonicalPath>> {
@@ -639,7 +701,7 @@ impl<'a> Translator<'a> {
639701
}
640702
}
641703

642-
fn canonical_path_from_enum_variant(
704+
pub(crate) fn canonical_path_from_enum_variant(
643705
&mut self,
644706
item: Variant,
645707
) -> Option<Label<generated::CanonicalPath>> {
@@ -661,124 +723,6 @@ impl<'a> Translator<'a> {
661723
})
662724
}
663725

664-
pub(crate) fn extract_canonical_origin_of_module(
665-
&mut self,
666-
item: &ast::Module,
667-
label: Label<generated::Module>,
668-
) {
669-
(|| {
670-
let sema = self.semantics.as_ref()?;
671-
let def = sema.to_module_def(item)?;
672-
let path = self.canonical_path_from_module(def)?;
673-
generated::Addressable::emit_canonical_path(
674-
label.into(),
675-
path.into(),
676-
&mut self.trap.writer,
677-
);
678-
Some(())
679-
})();
680-
}
681-
682-
pub(crate) fn extract_canonical_origin_of_function(
683-
&mut self,
684-
item: &ast::Fn,
685-
label: Label<generated::Function>,
686-
) {
687-
(|| {
688-
let sema = self.semantics.as_ref()?;
689-
let def = sema.to_fn_def(item)?;
690-
let path = self.canonical_path_from_function(def)?;
691-
generated::Addressable::emit_canonical_path(label.into(), path, &mut self.trap.writer);
692-
Some(())
693-
})();
694-
}
695-
696-
pub(crate) fn extract_canonical_origin_of_enum(
697-
&mut self,
698-
item: &ast::Enum,
699-
label: Label<generated::Enum>,
700-
) {
701-
(|| {
702-
let sema = self.semantics.as_ref()?;
703-
let def = sema.to_enum_def(item)?;
704-
let path = self.canonical_path_from_module_item(def)?;
705-
generated::Addressable::emit_canonical_path(
706-
label.into(),
707-
path.into(),
708-
&mut self.trap.writer,
709-
);
710-
Some(())
711-
})();
712-
}
713-
714-
pub(crate) fn extract_canonical_origin_of_struct(
715-
&mut self,
716-
item: &ast::Struct,
717-
label: Label<generated::Struct>,
718-
) {
719-
(|| {
720-
let sema = self.semantics.as_ref()?;
721-
let def = sema.to_struct_def(item)?;
722-
let path = self.canonical_path_from_module_item(def)?;
723-
generated::Addressable::emit_canonical_path(
724-
label.into(),
725-
path.into(),
726-
&mut self.trap.writer,
727-
);
728-
Some(())
729-
})();
730-
}
731-
732-
pub(crate) fn extract_canonical_origin_of_union(
733-
&mut self,
734-
item: &ast::Union,
735-
label: Label<generated::Union>,
736-
) {
737-
(|| {
738-
let sema = self.semantics.as_ref()?;
739-
let def = sema.to_union_def(item)?;
740-
let path = self.canonical_path_from_module_item(def)?;
741-
generated::Addressable::emit_canonical_path(
742-
label.into(),
743-
path.into(),
744-
&mut self.trap.writer,
745-
);
746-
Some(())
747-
})();
748-
}
749-
750-
pub(crate) fn extract_canonical_origin_of_trait(
751-
&mut self,
752-
item: &ast::Trait,
753-
label: Label<generated::Trait>,
754-
) {
755-
(|| {
756-
let sema = self.semantics.as_ref()?;
757-
let def = sema.to_trait_def(item)?;
758-
let path = self.canonical_path_from_module_item(def)?;
759-
generated::Addressable::emit_canonical_path(
760-
label.into(),
761-
path.into(),
762-
&mut self.trap.writer,
763-
);
764-
Some(())
765-
})();
766-
}
767-
768-
pub(crate) fn extract_canonical_origin_of_enum_variant(
769-
&mut self,
770-
item: &ast::Variant,
771-
label: Label<generated::Variant>,
772-
) {
773-
(|| {
774-
let sema = self.semantics.as_ref()?;
775-
let def = sema.to_enum_variant_def(item)?;
776-
let path = self.canonical_path_from_enum_variant(def)?;
777-
generated::Addressable::emit_canonical_path(label.into(), path, &mut self.trap.writer);
778-
Some(())
779-
})();
780-
}
781-
782726
pub(crate) fn extract_path_canonical_destination(
783727
&mut self,
784728
item: &impl PathAst,

rust/extractor/src/translate/generated.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)