@@ -17,15 +17,21 @@ impl<'ctx, 'cst> Pass<'ctx, 'cst> for AstGen<'ctx> {
1717 let mut ast_gen = Self :: new ( ctx) ;
1818
1919 for item in & cst. items {
20- match item {
21- cst:: Item :: FunctionDeclaration ( function_declaration) => {
22- ast_gen. lower_item_function ( function_declaration) ;
20+ let annotations = item
21+ . annotations
22+ . iter ( )
23+ . map ( |annotation| ast_gen. lower_annotation ( annotation) )
24+ . collect ( ) ;
25+
26+ match & item. kind {
27+ cst:: ItemKind :: FunctionDeclaration ( function_declaration) => {
28+ ast_gen. lower_item_function ( function_declaration, annotations) ;
2329 }
24- cst:: Item :: TraitDeclaration ( trait_declaration) => {
25- ast_gen. lower_trait_declaration ( trait_declaration) ;
30+ cst:: ItemKind :: TraitDeclaration ( trait_declaration) => {
31+ ast_gen. lower_trait_declaration ( trait_declaration, annotations ) ;
2632 }
27- cst:: Item :: TraitImplementation ( trait_implementation) => {
28- ast_gen. lower_trait_implementation ( trait_implementation) ;
33+ cst:: ItemKind :: TraitImplementation ( trait_implementation) => {
34+ ast_gen. lower_trait_implementation ( trait_implementation, annotations ) ;
2935 }
3036 }
3137 }
@@ -42,16 +48,38 @@ impl<'ctx> AstGen<'ctx> {
4248 }
4349 }
4450
45- fn lower_function ( & mut self , function : & cst:: FunctionDeclaration ) -> FunctionId {
51+ fn lower_annotation ( & mut self , annotation : & cst:: Annotation ) -> AnnotationId {
52+ let annotation = Annotation {
53+ key : self . ctx . strings . intern ( & annotation. key . 0 ) ,
54+ value : match & annotation. value {
55+ cst:: AnnotationValue :: None => None ,
56+ cst:: AnnotationValue :: Value { value, .. } => {
57+ Some ( self . ctx . strings . intern ( & value. 0 ) )
58+ }
59+ } ,
60+ } ;
61+ self . ast . annotations . insert ( annotation)
62+ }
63+
64+ fn lower_function (
65+ & mut self ,
66+ function : & cst:: FunctionDeclaration ,
67+ annotations : Vec < AnnotationId > ,
68+ ) -> FunctionId {
4669 let function_declaration = FunctionDeclaration {
70+ annotations,
4771 signature : self . lower_function_signature ( & function. signature ) ,
4872 body : self . lower_block ( & function. body ) ,
4973 } ;
5074 self . ast . function_declarations . insert ( function_declaration)
5175 }
5276
53- fn lower_item_function ( & mut self , function : & cst:: FunctionDeclaration ) -> FunctionId {
54- let id = self . lower_function ( function) ;
77+ fn lower_item_function (
78+ & mut self ,
79+ function : & cst:: FunctionDeclaration ,
80+ annotations : Vec < AnnotationId > ,
81+ ) -> FunctionId {
82+ let id = self . lower_function ( function, annotations) ;
5583 self . ast . item_functions . push ( id) ;
5684 id
5785 }
@@ -286,7 +314,11 @@ impl<'ctx> AstGen<'ctx> {
286314 }
287315
288316 /// Lower a [`cst::TraitDeclaration`] into a [`Trait`], producing a unique [`TraitId`].
289- fn lower_trait_declaration ( & mut self , trait_declaration : & cst:: TraitDeclaration ) -> TraitId {
317+ fn lower_trait_declaration (
318+ & mut self ,
319+ trait_declaration : & cst:: TraitDeclaration ,
320+ annotations : Vec < AnnotationId > ,
321+ ) -> TraitId {
290322 let methods = trait_declaration
291323 . methods
292324 . iter ( )
@@ -298,23 +330,33 @@ impl<'ctx> AstGen<'ctx> {
298330 } )
299331 . collect ( ) ;
300332 self . ast . traits . insert ( Trait {
333+ annotations,
301334 name : self . ctx . strings . intern ( & trait_declaration. name . 0 ) ,
302335 methods,
303336 } )
304337 }
305338
306- fn lower_trait_implementation ( & mut self , trait_implementation : & cst:: TraitImplementation ) {
339+ fn lower_trait_implementation (
340+ & mut self ,
341+ trait_implementation : & cst:: TraitImplementation ,
342+ annotations : Vec < AnnotationId > ,
343+ ) {
307344 let methods = trait_implementation
308345 . methods
309346 . iter ( )
310347 . map ( |method| {
311- let method = self . lower_function ( method) ;
348+ let method = self . lower_function (
349+ method,
350+ // Currently, annotations cannot be attached to non-item functions.
351+ Vec :: new ( ) ,
352+ ) ;
312353
313354 ( self . ast [ method] . signature . name , method)
314355 } )
315356 . collect ( ) ;
316357 let target_ty = self . lower_type ( & trait_implementation. ty ) ;
317358 self . ast . trait_implementations . push ( TraitImplementation {
359+ annotations,
318360 trait_name : self . ctx . strings . intern ( & trait_implementation. name . 0 ) ,
319361 target_ty,
320362 methods,
@@ -389,7 +431,7 @@ mod test {
389431 ) ]
390432 fn trait_declaration ( #[ case] name : & str , mut ctx : Ctx , #[ case] source : & ' static str ) {
391433 let mut pass = AstGen :: new ( & mut ctx) ;
392- let trait_id = pass. lower_trait_declaration ( & parse ( source) ) ;
434+ let trait_id = pass. lower_trait_declaration ( & parse ( source) , Vec :: new ( ) ) ;
393435 assert_debug_snapshot ! ( name, pass. ast[ trait_id] , source) ;
394436 }
395437
@@ -405,7 +447,16 @@ mod test {
405447 ) ]
406448 fn trait_implementation ( #[ case] name : & str , mut ctx : Ctx , #[ case] source : & ' static str ) {
407449 let mut pass = AstGen :: new ( & mut ctx) ;
408- pass. lower_trait_implementation ( & parse ( source) ) ;
450+ pass. lower_trait_implementation ( & parse ( source) , Vec :: new ( ) ) ;
409451 assert_debug_snapshot ! ( name, pass. ast. trait_implementations[ 0 ] , source) ;
410452 }
453+
454+ #[ rstest]
455+ #[ case( "annotation_key" , "@some_annotation" ) ]
456+ #[ case( "annotation_key_value" , "@some_annotation(value)" ) ]
457+ fn lower_annotation ( #[ case] name : & str , mut ctx : Ctx , #[ case] source : & str ) {
458+ let mut pass = AstGen :: new ( & mut ctx) ;
459+ let annotation = pass. lower_annotation ( & parse ( source) ) ;
460+ assert_debug_snapshot ! ( name, pass. ast. annotations[ annotation] , source) ;
461+ }
411462}
0 commit comments