Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split pointer declarators from the rest #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions grammar.rustpeg
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,8 @@ declarator0 -> Declarator =
{
Declarator {
kind: kind,
derived: concat(pointer, derived),
pointer: pointer,
derived: derived,
extensions: attr.unwrap_or_default(),
}
}
Expand Down Expand Up @@ -825,14 +826,14 @@ function_declarator -> FunctionDeclarator =
}
}

pointer -> Node<DerivedDeclarator> = node<pointer0>
pointer -> Node<PointerDeclarator> = node<pointer0>

pointer0 -> DerivedDeclarator =
pointer0 -> PointerDeclarator =
t:$("*" / clang<"^">) _ q:list0<node<pointer_qualifier>> {
if t == "^" {
DerivedDeclarator::Block(q)
PointerDeclarator::Block(q)
} else {
DerivedDeclarator::Pointer(q)
PointerDeclarator::Pointer(q)
}
}

Expand Down Expand Up @@ -882,21 +883,24 @@ abstract_declarator0 -> Declarator =
p:list0<pointer> _ k:node<direct_abstract_declarator> _ d:list0<derived_abstract_declarator> {
Declarator {
kind: k,
derived: concat(p, d),
pointer: p,
derived: d,
extensions: Vec::new(),
}
} /
p:list0<pointer> k:#position _ d:list1<derived_abstract_declarator> {
Declarator {
kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)),
derived: concat(p, d),
pointer: p,
derived: d,
extensions: Vec::new(),
}
} /
p:list1<pointer> k:#position {
Declarator {
kind: Node::new(DeclaratorKind::Abstract, Span::span(k, k)),
derived: p,
pointer: p,
derived: Vec::new(),
extensions: Vec::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion reftests/declaration-1080.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Declaration
TypeSpecifier Char
Declarator
DeclaratorKind Abstract
DerivedDeclarator Pointer
PointerDeclarator Pointer
Ellipsis Some
Extension
Attribute "format"
Expand Down
4 changes: 2 additions & 2 deletions reftests/declaration-2321.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "_close"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
TypeQualifier Nullable
DerivedDeclarator
Expand All @@ -25,6 +25,6 @@ Declaration
TypeSpecifier Void
Declarator
DeclaratorKind Abstract
DerivedDeclarator Pointer
PointerDeclarator Pointer
Ellipsis None
===*/
2 changes: 1 addition & 1 deletion reftests/declaration-31-ty-attr3.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Declaration
Declarator
DeclaratorKind
Identifier "i"
DerivedDeclarator Pointer
PointerDeclarator Pointer
===*/
2 changes: 1 addition & 1 deletion reftests/declaration-659.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "foo"
DerivedDeclarator Pointer
PointerDeclarator Pointer
InitDeclarator
Declarator
DeclaratorKind
Expand Down
2 changes: 1 addition & 1 deletion reftests/declaration-714.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "foobar"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
TypeQualifier Const
===*/
8 changes: 4 additions & 4 deletions reftests/declaration-880.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "fparseln"
DerivedDeclarator Pointer
PointerDeclarator Pointer
DerivedDeclarator
FunctionDeclarator
ParameterDeclaration
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion reftests/declaration-983.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "__buf"
DerivedDeclarator Pointer
PointerDeclarator Pointer
ParameterDeclaration
DeclarationSpecifier
TypeSpecifier TypedefName
Expand Down
2 changes: 1 addition & 1 deletion reftests/declaration-block-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "p"
DerivedDeclarator Block
PointerDeclarator Block
PointerQualifier
TypeQualifier Const
DerivedDeclarator
Expand Down
12 changes: 6 additions & 6 deletions reftests/declaration-block-3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "bsearch_b"
DerivedDeclarator Pointer
PointerDeclarator Pointer
DerivedDeclarator
FunctionDeclarator
ParameterDeclaration
Expand All @@ -24,7 +24,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "__key"
DerivedDeclarator Pointer
PointerDeclarator Pointer
ParameterDeclaration
DeclarationSpecifier
TypeQualifier Const
Expand All @@ -33,7 +33,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "__base"
DerivedDeclarator Pointer
PointerDeclarator Pointer
ParameterDeclaration
DeclarationSpecifier
TypeSpecifier TypedefName
Expand All @@ -56,7 +56,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "__compar"
DerivedDeclarator Block
PointerDeclarator Block
PointerQualifier
TypeQualifier Nonnull
DerivedDeclarator
Expand All @@ -68,15 +68,15 @@ Declaration
TypeSpecifier Void
Declarator
DeclaratorKind Abstract
DerivedDeclarator Pointer
PointerDeclarator Pointer
ParameterDeclaration
DeclarationSpecifier
TypeQualifier Const
DeclarationSpecifier
TypeSpecifier Void
Declarator
DeclaratorKind Abstract
DerivedDeclarator Pointer
PointerDeclarator Pointer
Ellipsis None
Extension
Attribute "__noescape__"
Expand Down
2 changes: 1 addition & 1 deletion reftests/declaration-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Declaration
Declarator
DeclaratorKind
Identifier "block"
DerivedDeclarator Block
PointerDeclarator Block
===*/
8 changes: 4 additions & 4 deletions reftests/declaration-ptr-attr1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions reftests/declaration-ptr-attr2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions reftests/declaration-ptr-attr3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Declaration
Declarator
DeclaratorKind
Identifier "f"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
Extension
Attribute "aligned"
Expand All @@ -19,5 +19,5 @@ Declaration
IntegerBase Decimal
IntegerSuffix false false
IntegerSize Int
DerivedDeclarator Pointer
PointerDeclarator Pointer
===*/
6 changes: 3 additions & 3 deletions reftests/translation_unit-1183.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TranslationUnit
Declarator
DeclaratorKind
Identifier "realpath"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
Extension
Attribute "__nothrow__"
Expand All @@ -42,7 +42,7 @@ TranslationUnit
Declarator
DeclaratorKind
Identifier "__name"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
TypeQualifier Restrict
ParameterDeclaration
Expand All @@ -51,7 +51,7 @@ TranslationUnit
Declarator
DeclaratorKind
Identifier "__resolved"
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerQualifier
TypeQualifier Restrict
Ellipsis None
Expand Down
4 changes: 2 additions & 2 deletions reftests/translation_unit-2373.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TranslationUnit
Declarator
DeclaratorKind
Identifier "argv"
DerivedDeclarator Pointer
DerivedDeclarator Pointer
PointerDeclarator Pointer
PointerDeclarator Pointer
Statement Compound
===*/
22 changes: 15 additions & 7 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ pub struct Declarator {
/// What is being declared
pub kind: Node<DeclaratorKind>,
/// Contains pointer, array and function declarator elements
pub pointer: Vec<Node<PointerDeclarator>>,
pub derived: Vec<Node<DerivedDeclarator>>,
/// Vendor-specific extensions
pub extensions: Vec<Node<Extension>>,
Expand All @@ -851,23 +852,30 @@ pub enum DeclaratorKind {
Declarator(Box<Node<Declarator>>),
}

/// 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<Node<PointerQualifier>>),
/// `^ qualifiers …`
///
/// [Clang extension](https://clang.llvm.org/docs/BlockLanguageSpec.html)
Block(Vec<Node<PointerQualifier>>),
}

/// Modifies declarator type
///
/// (C11 6.7.6.2, 6.7.6.3)
#[derive(Debug, PartialEq, Clone)]
pub enum DerivedDeclarator {
/// `… []`
Array(Node<ArrayDeclarator>),
/// `… ( parameters )`
Function(Node<FunctionDeclarator>),
/// `… ( identifiers )`
KRFunction(Vec<Node<Identifier>>),
/// `^ qualifiers …`
///
/// [Clang extension](https://clang.llvm.org/docs/BlockLanguageSpec.html)
Block(Vec<Node<PointerQualifier>>),
}

/// Array part of a declarator
Expand Down
Loading
Loading