From 1e9c6621f33117475058271d79a0371749bab857 Mon Sep 17 00:00:00 2001 From: Vickenty Fesunov Date: Thu, 21 Dec 2023 22:25:58 +0100 Subject: [PATCH] Split pointer declarators from the rest Fixes #44 --- grammar.rustpeg | 20 +++++++++------- reftests/declaration-1080.c | 2 +- reftests/declaration-2321.c | 4 ++-- reftests/declaration-31-ty-attr3.c | 2 +- reftests/declaration-659.c | 2 +- reftests/declaration-714.c | 2 +- reftests/declaration-880.c | 8 +++---- reftests/declaration-983.c | 2 +- reftests/declaration-block-2.c | 2 +- reftests/declaration-block-3.c | 12 +++++----- reftests/declaration-block.c | 2 +- reftests/declaration-ptr-attr1.c | 8 +++---- reftests/declaration-ptr-attr2.c | 8 +++---- reftests/declaration-ptr-attr3.c | 4 ++-- reftests/translation_unit-1183.c | 6 ++--- reftests/translation_unit-2373.c | 4 ++-- src/ast.rs | 22 +++++++++++------ src/parser.rs | 16 ++++++------- src/print.rs | 13 ++++++++-- src/visit.rs | 38 +++++++++++++++++++++++------- 20 files changed, 109 insertions(+), 68 deletions(-) diff --git a/grammar.rustpeg b/grammar.rustpeg index 60db0a5..0c8df4c 100644 --- a/grammar.rustpeg +++ b/grammar.rustpeg @@ -771,7 +771,8 @@ declarator0 -> Declarator = { Declarator { kind: kind, - derived: concat(pointer, derived), + pointer: pointer, + derived: derived, extensions: attr.unwrap_or_default(), } } @@ -825,14 +826,14 @@ function_declarator -> FunctionDeclarator = } } -pointer -> Node = node +pointer -> Node = node -pointer0 -> DerivedDeclarator = +pointer0 -> PointerDeclarator = t:$("*" / clang<"^">) _ q:list0> { if t == "^" { - DerivedDeclarator::Block(q) + PointerDeclarator::Block(q) } else { - DerivedDeclarator::Pointer(q) + PointerDeclarator::Pointer(q) } } @@ -882,21 +883,24 @@ abstract_declarator0 -> Declarator = p:list0 _ k:node _ d:list0 { Declarator { kind: k, - derived: concat(p, d), + pointer: p, + derived: d, extensions: Vec::new(), } } / p:list0 k:#position _ d:list1 { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), - derived: concat(p, d), + pointer: p, + derived: d, extensions: Vec::new(), } } / p:list1 k:#position { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), - derived: p, + pointer: p, + derived: Vec::new(), extensions: Vec::new(), } } diff --git a/reftests/declaration-1080.c b/reftests/declaration-1080.c index d8666fc..9ab7579 100644 --- a/reftests/declaration-1080.c +++ b/reftests/declaration-1080.c @@ -33,7 +33,7 @@ Declaration TypeSpecifier Char Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer Ellipsis Some Extension Attribute "format" diff --git a/reftests/declaration-2321.c b/reftests/declaration-2321.c index 8576aba..b076a33 100644 --- a/reftests/declaration-2321.c +++ b/reftests/declaration-2321.c @@ -15,7 +15,7 @@ Declaration Declarator DeclaratorKind Identifier "_close" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier TypeQualifier Nullable DerivedDeclarator @@ -25,6 +25,6 @@ Declaration TypeSpecifier Void Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer Ellipsis None ===*/ diff --git a/reftests/declaration-31-ty-attr3.c b/reftests/declaration-31-ty-attr3.c index 4e85d85..7d6d962 100644 --- a/reftests/declaration-31-ty-attr3.c +++ b/reftests/declaration-31-ty-attr3.c @@ -29,5 +29,5 @@ Declaration Declarator DeclaratorKind Identifier "i" - DerivedDeclarator Pointer + PointerDeclarator Pointer ===*/ diff --git a/reftests/declaration-659.c b/reftests/declaration-659.c index 6c0c470..8106f82 100644 --- a/reftests/declaration-659.c +++ b/reftests/declaration-659.c @@ -13,7 +13,7 @@ Declaration Declarator DeclaratorKind Identifier "foo" - DerivedDeclarator Pointer + PointerDeclarator Pointer InitDeclarator Declarator DeclaratorKind diff --git a/reftests/declaration-714.c b/reftests/declaration-714.c index e8ec675..ce82c47 100644 --- a/reftests/declaration-714.c +++ b/reftests/declaration-714.c @@ -23,7 +23,7 @@ Declaration Declarator DeclaratorKind Identifier "foobar" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier TypeQualifier Const ===*/ diff --git a/reftests/declaration-880.c b/reftests/declaration-880.c index 9369694..00ff8b8 100644 --- a/reftests/declaration-880.c +++ b/reftests/declaration-880.c @@ -11,7 +11,7 @@ Declaration Declarator DeclaratorKind Identifier "fparseln" - DerivedDeclarator Pointer + PointerDeclarator Pointer DerivedDeclarator FunctionDeclarator ParameterDeclaration @@ -20,21 +20,21 @@ Declaration Identifier "FILE" Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeSpecifier TypedefName Identifier "size_t" Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeSpecifier TypedefName Identifier "size_t" Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeQualifier Const diff --git a/reftests/declaration-983.c b/reftests/declaration-983.c index a91dda3..7cfc5bc 100644 --- a/reftests/declaration-983.c +++ b/reftests/declaration-983.c @@ -28,7 +28,7 @@ Declaration Declarator DeclaratorKind Identifier "__buf" - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeSpecifier TypedefName diff --git a/reftests/declaration-block-2.c b/reftests/declaration-block-2.c index bb9eeb0..f435c14 100644 --- a/reftests/declaration-block-2.c +++ b/reftests/declaration-block-2.c @@ -12,7 +12,7 @@ Declaration Declarator DeclaratorKind Identifier "p" - DerivedDeclarator Block + PointerDeclarator Block PointerQualifier TypeQualifier Const DerivedDeclarator diff --git a/reftests/declaration-block-3.c b/reftests/declaration-block-3.c index 398e2a4..1044a5c 100644 --- a/reftests/declaration-block-3.c +++ b/reftests/declaration-block-3.c @@ -13,7 +13,7 @@ Declaration Declarator DeclaratorKind Identifier "bsearch_b" - DerivedDeclarator Pointer + PointerDeclarator Pointer DerivedDeclarator FunctionDeclarator ParameterDeclaration @@ -24,7 +24,7 @@ Declaration Declarator DeclaratorKind Identifier "__key" - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeQualifier Const @@ -33,7 +33,7 @@ Declaration Declarator DeclaratorKind Identifier "__base" - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeSpecifier TypedefName @@ -56,7 +56,7 @@ Declaration Declarator DeclaratorKind Identifier "__compar" - DerivedDeclarator Block + PointerDeclarator Block PointerQualifier TypeQualifier Nonnull DerivedDeclarator @@ -68,7 +68,7 @@ Declaration TypeSpecifier Void Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer ParameterDeclaration DeclarationSpecifier TypeQualifier Const @@ -76,7 +76,7 @@ Declaration TypeSpecifier Void Declarator DeclaratorKind Abstract - DerivedDeclarator Pointer + PointerDeclarator Pointer Ellipsis None Extension Attribute "__noescape__" diff --git a/reftests/declaration-block.c b/reftests/declaration-block.c index 65979bc..7ffcbce 100644 --- a/reftests/declaration-block.c +++ b/reftests/declaration-block.c @@ -10,5 +10,5 @@ Declaration Declarator DeclaratorKind Identifier "block" - DerivedDeclarator Block + PointerDeclarator Block ===*/ diff --git a/reftests/declaration-ptr-attr1.c b/reftests/declaration-ptr-attr1.c index 1307d0e..dcb0934 100644 --- a/reftests/declaration-ptr-attr1.c +++ b/reftests/declaration-ptr-attr1.c @@ -12,10 +12,10 @@ Declaration Declarator DeclaratorKind Identifier "f" - DerivedDeclarator Pointer - DerivedDeclarator Pointer - DerivedDeclarator Pointer - DerivedDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer DerivedDeclarator FunctionDeclarator ParameterDeclaration diff --git a/reftests/declaration-ptr-attr2.c b/reftests/declaration-ptr-attr2.c index b35b1e8..34cab4c 100644 --- a/reftests/declaration-ptr-attr2.c +++ b/reftests/declaration-ptr-attr2.c @@ -12,10 +12,10 @@ Declaration Declarator DeclaratorKind Identifier "f" - DerivedDeclarator Pointer - DerivedDeclarator Pointer - DerivedDeclarator Pointer - DerivedDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer Extension Attribute "noreturn" DerivedDeclarator diff --git a/reftests/declaration-ptr-attr3.c b/reftests/declaration-ptr-attr3.c index 57bbd2e..170b1a9 100644 --- a/reftests/declaration-ptr-attr3.c +++ b/reftests/declaration-ptr-attr3.c @@ -9,7 +9,7 @@ Declaration Declarator DeclaratorKind Identifier "f" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier Extension Attribute "aligned" @@ -19,5 +19,5 @@ Declaration IntegerBase Decimal IntegerSuffix false false IntegerSize Int - DerivedDeclarator Pointer + PointerDeclarator Pointer ===*/ diff --git a/reftests/translation_unit-1183.c b/reftests/translation_unit-1183.c index 08d2c91..fb1f19d 100644 --- a/reftests/translation_unit-1183.c +++ b/reftests/translation_unit-1183.c @@ -26,7 +26,7 @@ TranslationUnit Declarator DeclaratorKind Identifier "realpath" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier Extension Attribute "__nothrow__" @@ -42,7 +42,7 @@ TranslationUnit Declarator DeclaratorKind Identifier "__name" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier TypeQualifier Restrict ParameterDeclaration @@ -51,7 +51,7 @@ TranslationUnit Declarator DeclaratorKind Identifier "__resolved" - DerivedDeclarator Pointer + PointerDeclarator Pointer PointerQualifier TypeQualifier Restrict Ellipsis None diff --git a/reftests/translation_unit-2373.c b/reftests/translation_unit-2373.c index 70e0790..69d8b19 100644 --- a/reftests/translation_unit-2373.c +++ b/reftests/translation_unit-2373.c @@ -27,7 +27,7 @@ TranslationUnit Declarator DeclaratorKind Identifier "argv" - DerivedDeclarator Pointer - DerivedDeclarator Pointer + PointerDeclarator Pointer + PointerDeclarator Pointer Statement Compound ===*/ diff --git a/src/ast.rs b/src/ast.rs index 42b6163..250bc5a 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -826,6 +826,7 @@ pub struct Declarator { /// What is being declared pub kind: Node, /// Contains pointer, array and function declarator elements + pub pointer: Vec>, pub derived: Vec>, /// Vendor-specific extensions pub extensions: Vec>, @@ -851,23 +852,30 @@ pub enum DeclaratorKind { Declarator(Box>), } -/// Modifies declarator type +/// Pointer declarator /// -/// (C11 6.7.6) +/// (C11 6.7.6.1) #[derive(Debug, PartialEq, Clone)] -pub enum DerivedDeclarator { +pub enum PointerDeclarator { /// `* qualifiers …` Pointer(Vec>), + /// `^ qualifiers …` + /// + /// [Clang extension](https://clang.llvm.org/docs/BlockLanguageSpec.html) + Block(Vec>), +} + +/// Modifies declarator type +/// +/// (C11 6.7.6.2, 6.7.6.3) +#[derive(Debug, PartialEq, Clone)] +pub enum DerivedDeclarator { /// `… []` Array(Node), /// `… ( parameters )` Function(Node), /// `… ( identifiers )` KRFunction(Vec>), - /// `^ qualifiers …` - /// - /// [Clang extension](https://clang.llvm.org/docs/BlockLanguageSpec.html) - Block(Vec>), } /// Array part of a declarator diff --git a/src/parser.rs b/src/parser.rs index 513d204..7fba854 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9907,7 +9907,7 @@ fn __parse_declarator0<'input>(__input: &'input str, __state: &mut ParseState<'i } }; match __seq_res { - Matched(__pos, derived) => Matched(__pos, { Declarator { kind: kind, derived: concat(pointer, derived), extensions: attr.unwrap_or_default() } }), + Matched(__pos, derived) => Matched(__pos, { Declarator { kind: kind, pointer: pointer, derived: derived, extensions: attr.unwrap_or_default() } }), Failed => Failed, } } @@ -10673,7 +10673,7 @@ fn __parse_function_declarator<'input>(__input: &'input str, __state: &mut Parse } } -fn __parse_pointer<'input>(__input: &'input str, __state: &mut ParseState<'input>, __pos: usize, env: &mut Env) -> RuleResult> { +fn __parse_pointer<'input>(__input: &'input str, __state: &mut ParseState<'input>, __pos: usize, env: &mut Env) -> RuleResult> { #![allow(non_snake_case, unused)] { let __seq_res = Matched(__pos, __pos); @@ -10696,7 +10696,7 @@ fn __parse_pointer<'input>(__input: &'input str, __state: &mut ParseState<'input } } -fn __parse_pointer0<'input>(__input: &'input str, __state: &mut ParseState<'input>, __pos: usize, env: &mut Env) -> RuleResult { +fn __parse_pointer0<'input>(__input: &'input str, __state: &mut ParseState<'input>, __pos: usize, env: &mut Env) -> RuleResult { #![allow(non_snake_case, unused)] { let __seq_res = { @@ -10791,9 +10791,9 @@ fn __parse_pointer0<'input>(__input: &'input str, __state: &mut ParseState<'inpu match __seq_res { Matched(__pos, q) => Matched(__pos, { if t == "^" { - DerivedDeclarator::Block(q) + PointerDeclarator::Block(q) } else { - DerivedDeclarator::Pointer(q) + PointerDeclarator::Pointer(q) } }), Failed => Failed, @@ -11164,7 +11164,7 @@ fn __parse_abstract_declarator0<'input>(__input: &'input str, __state: &mut Pars } }; match __seq_res { - Matched(__pos, d) => Matched(__pos, { Declarator { kind: k, derived: concat(p, d), extensions: Vec::new() } }), + Matched(__pos, d) => Matched(__pos, { Declarator { kind: k, pointer: p, derived: d, extensions: Vec::new() } }), Failed => Failed, } } @@ -11263,7 +11263,7 @@ fn __parse_abstract_declarator0<'input>(__input: &'input str, __state: &mut Pars } }; match __seq_res { - Matched(__pos, d) => Matched(__pos, { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), derived: concat(p, d), extensions: Vec::new() } }), + Matched(__pos, d) => Matched(__pos, { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), pointer: p, derived: d, extensions: Vec::new() } }), Failed => Failed, } } @@ -11320,7 +11320,7 @@ fn __parse_abstract_declarator0<'input>(__input: &'input str, __state: &mut Pars Matched(__pos, p) => { let __seq_res = Matched(__pos, __pos); match __seq_res { - Matched(__pos, k) => Matched(__pos, { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), derived: p, extensions: Vec::new() } }), + Matched(__pos, k) => Matched(__pos, { Declarator { kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)), pointer: p, derived: Vec::new(), extensions: Vec::new() } }), Failed => Failed, } } diff --git a/src/print.rs b/src/print.rs index b199448..2d8e28c 100644 --- a/src/print.rs +++ b/src/print.rs @@ -399,6 +399,11 @@ impl<'ast, 'a> Visit<'ast> for Printer<'a> { print_declarator_kind(self, n); visit_declarator_kind(&mut self.block(), n, span); } + fn visit_pointer_declarator(&mut self, n: &'ast PointerDeclarator, span: &'ast Span) { + self.name("PointerDeclarator"); + print_pointer_declarator(self, n); + visit_pointer_declarator(&mut self.block(), n, span); + } fn visit_derived_declarator(&mut self, n: &'ast DerivedDeclarator, span: &'ast Span) { self.name("DerivedDeclarator"); print_derived_declarator(self, n); @@ -565,11 +570,15 @@ fn print_declarator_kind<'ast>(p: &mut Printer, n: &'ast DeclaratorKind) { _ => {} } } +fn print_pointer_declarator<'ast>(p: &mut Printer, n: &'ast PointerDeclarator) { + match *n { + PointerDeclarator::Pointer(_) => p.w.write_str(" Pointer").unwrap(), + PointerDeclarator::Block(_) => p.w.write_str(" Block").unwrap(), + } +} fn print_derived_declarator<'ast>(p: &mut Printer, n: &'ast DerivedDeclarator) { match *n { - DerivedDeclarator::Pointer(_) => p.w.write_str(" Pointer").unwrap(), DerivedDeclarator::KRFunction(_) => p.w.write_str(" KRFunction").unwrap(), - DerivedDeclarator::Block(_) => p.w.write_str(" Block").unwrap(), _ => {} } } diff --git a/src/visit.rs b/src/visit.rs index 59ee577..874b769 100644 --- a/src/visit.rs +++ b/src/visit.rs @@ -309,6 +309,14 @@ pub trait Visit<'ast> { visit_declarator_kind(self, declarator_kind, span) } + fn visit_pointer_declarator( + &mut self, + pointer_declarator: &'ast PointerDeclarator, + span: &'ast Span, + ) { + visit_pointer_declarator(self, pointer_declarator, span) + } + fn visit_derived_declarator( &mut self, derived_declarator: &'ast DerivedDeclarator, @@ -1122,6 +1130,9 @@ pub fn visit_declarator<'ast, V: Visit<'ast> + ?Sized>( _span: &'ast Span, ) { visitor.visit_declarator_kind(&declarator.kind.node, &declarator.kind.span); + for pointer in &declarator.pointer { + visitor.visit_pointer_declarator(&pointer.node, &pointer.span); + } for derived in &declarator.derived { visitor.visit_derived_declarator(&derived.node, &derived.span); } @@ -1142,17 +1153,31 @@ pub fn visit_declarator_kind<'ast, V: Visit<'ast> + ?Sized>( } } -pub fn visit_derived_declarator<'ast, V: Visit<'ast> + ?Sized>( +pub fn visit_pointer_declarator<'ast, V: Visit<'ast> + ?Sized>( visitor: &mut V, - derived_declarator: &'ast DerivedDeclarator, + pointer_declarator: &'ast PointerDeclarator, _span: &'ast Span, ) { - match *derived_declarator { - DerivedDeclarator::Pointer(ref p) => { + match *pointer_declarator { + PointerDeclarator::Pointer(ref p) => { for pointer in p { visitor.visit_pointer_qualifier(&pointer.node, &pointer.span); } } + PointerDeclarator::Block(ref qs) => { + for q in qs { + visitor.visit_pointer_qualifier(&q.node, &q.span); + } + } + } +} + +pub fn visit_derived_declarator<'ast, V: Visit<'ast> + ?Sized>( + visitor: &mut V, + derived_declarator: &'ast DerivedDeclarator, + _span: &'ast Span, +) { + match *derived_declarator { DerivedDeclarator::Array(ref a) => visitor.visit_array_declarator(&a.node, &a.span), DerivedDeclarator::Function(ref f) => visitor.visit_function_declarator(&f.node, &f.span), DerivedDeclarator::KRFunction(ref k) => { @@ -1160,11 +1185,6 @@ pub fn visit_derived_declarator<'ast, V: Visit<'ast> + ?Sized>( visitor.visit_identifier(&identifier.node, &identifier.span); } } - DerivedDeclarator::Block(ref qs) => { - for q in qs { - visitor.visit_pointer_qualifier(&q.node, &q.span); - } - } } }