Skip to content

Fix for operator recognition in cpp2 code sections. #476

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

Merged
merged 1 commit into from
Jun 14, 2023

Conversation

MaxSagebaum
Copy link
Contributor

For the cpp2 file:

operator*: (v1: double, v2: double) -> _ = v1 * v2;

The cppfront command cppfront opMul.cpp2 -p produces the error:

opMul.cpp2(1,1): error: pure-cpp2 switch disables Cpp1 syntax

I took this as an opportunity to get familiar with the code of cppfront. The switch in starts_with_operator had the comments for the operators but the cases statements where missing.

With the missing cases added, the test file works now with cppfront.

I also checked all operators against the list in https://en.cppreference.com/w/cpp/language/operators

  1. Additional missing operators: &=, |=
  2. Operators that are checked but not defined: &&=, ||=
  3. Other missing operators: ,, ->*, (), []

I added the operators in point 1. Should I remove the operators under point 2? Should I add the operators under point 3?

On a side note: Thanks for working on cpp2 and providing it to the public!

The operators &=, |=, *=, *, %=, &, ^=, ^, ~= and ~ where not properly recognized
in cpp2 code sections.
@JohelEGP
Copy link
Contributor

I also checked all operators against the list in https://en.cppreference.com/w/cpp/language/operators

1. Additional missing operators: `&=`, `|=`

2. Operators that are checked but not defined: `&&=`, `||=`

3. Other missing operators: `,`, `->*`, `()`, `[]`

See also #387 (comment).

@JohelEGP
Copy link
Contributor

operator*: (v1: double, v2: double) -> _ = v1 * v2;

That declaration works at type scope.

Perhaps the greater issue is that the parser that recognizes a Cpp2 declaration at the top-level
doesn't use the same logic for parsing identifiers as the parser within a Cpp2 declaration.

@MaxSagebaum
Copy link
Contributor Author

Thanks for pointing out #387 (comment) most of it would be fixed when I also add a patch for point 2 and 3.

operator*: (v1: double, v2: double) -> _ = v1 * v2;

That declaration works at type scope.

Perhaps the greater issue is that the parser that recognizes a Cpp2 declaration at the top-level doesn't use the same logic for parsing identifiers as the parser within a Cpp2 declaration.

You are right, that one would not write this operator. My original test had a custom class as arguments. Nevertheless, cppfront identifies it on a global level as a cpp1 line and will copy it to the generated source. The cpp1 compiler will then complain about this line.

@JohelEGP
Copy link
Contributor

By type scope, I meant in a type scope, not with UDT arguments.
Or more generally, within a Cpp2 declaration.
This should work, but doesn't: https://cpp2.godbolt.org/z/z9nEYo6ar.

t: type = { }
ns: namespace = {
operator*: (_: t) = { } // Recognized as Cpp2.
}
operator*: (_: t) = { } // Not recognized as Cpp2.
main: () = { }
build/_cppfront/main.cpp:19:1: error: 'operator*' does not name a type
   19 | operator*: (_: t) = { } // Not recognized as Cpp2.
      | ^~~~~~~~

@MaxSagebaum
Copy link
Contributor Author

Yes, my fix takes care of

operator*: (_: t) = { } // Not recognized as Cpp2.

@hsutter hsutter merged commit be2b41f into hsutter:main Jun 14, 2023
zaucy pushed a commit to zaucy/cppfront that referenced this pull request Dec 5, 2023
The operators &=, |=, *=, *, %=, &, ^=, ^, ~= and ~ where not properly recognized
in cpp2 code sections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants