Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Parse qualifiers correctly #347

Open
Tracked by #349
jyn514 opened this issue Mar 15, 2020 · 3 comments · May be fixed by #508
Open
Tracked by #349

Parse qualifiers correctly #347

jyn514 opened this issue Mar 15, 2020 · 3 comments · May be fixed by #508
Labels
bug Something isn't working parser Issue to do with parsing the abstract syntax tree

Comments

@jyn514
Copy link
Owner

jyn514 commented Mar 15, 2020

Basically the same as #49.

Expected behavior

int *const c is not the same as int const *c

The int *const c declares c as immutable, while the data it points to can be modified. int const *c declares the data c points to as const, while the variable c can still be modified. Currently we have this backwards.

Code

int f(const int *p) {
	const int *q;
	p = q;
}
<stdin>:3:4 error: invalid program: cannot assign to variable 'p' with `const` qualifier
	p = q;

C Standard

6.7.6.1p3:

3 EXAMPLE The following pair of declarations demonstrates the difference between a ''variable pointer to a constant value'' and a ''constant pointer to a variable value''.

         const int *ptr_to_constant;
         int *const constant_ptr;

The contents of any object pointed to by ptr_to_constant shall not be modified through that pointer, but ptr_to_constant itself may be changed to point to another object. Similarly, the contents of the int pointed to by constant_ptr may be modified, but constant_ptr itself shall always point to the same location.

@jyn514 jyn514 added bug Something isn't working parser Issue to do with parsing the abstract syntax tree labels Mar 15, 2020
@jyn514
Copy link
Owner Author

jyn514 commented Mar 26, 2020

Will be fixed by #151

Nope, not fixed:

int f(const int *p) {
	const int *q;
	p = q;
}
<stdin>:3:3 error: invalid program: cannot assign to variable 'p' with `const` qualifier
	p = q;
        ^^^^

@jyn514
Copy link
Owner Author

jyn514 commented Aug 16, 2020

Relevant code:

/// Parse a single type, given the specifiers and declarator.
fn parse_type(
&mut self,
specifiers: Vec<ast::DeclarationSpecifier>,
declarator: ast::DeclaratorType,
location: Location,
) -> ParsedType {
let mut specs = self.parse_specifiers(specifiers, location);
specs.ctype = self.parse_declarator(specs.ctype, declarator, location);
if !specs.ctype.is_function() && specs.qualifiers.func != FunctionQualifiers::default() {
self.err(
SemanticError::FuncQualifiersNotAllowed(specs.qualifiers.func),
location,
);
}
specs
}

I think I need to pass in &mut specs.qualifiers to parse_declarator.

@jyn514
Copy link
Owner Author

jyn514 commented Aug 16, 2020

More test cases:

  • int *const p, i; - the const should only affect p.
  • inline int (*f)(), int (*inline f)() - not allowed

@jyn514 jyn514 linked a pull request Aug 16, 2020 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working parser Issue to do with parsing the abstract syntax tree
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant