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

Added basic implementation of operator overloading #1606

Merged
merged 8 commits into from
Sep 12, 2024
Merged

Conversation

oxisto
Copy link
Member

@oxisto oxisto commented Jun 29, 2024

This PR adds basic functionality for resolving operator overloading for languages that support it (via the trait HasOperatorOverloading - which is currently python and C++). The respective frontend looks for function declarations that match entries in overloadedOperatorNames and creates an OperatorDeclaration.

The SymbolResolver is then looking for specific expressions (that implement HasOverloadedOperation) to see whether the expression is really an overloaded call to an operator declaration. In this case, the expression is replaced with an OperatorCallExpression, pointing to the respective OperatorDeclaration. This happens only if a declaration was found, currently, no OperatorDeclaration is inferred. This might be something for the future.

Currently the following nodes support HasOverloadedOperation and should work:

  • BinaryOperator: Tested in C++ and Python
  • UnaryOperator: Tested in C++ and Python
  • MemberExpression: Only really possible in C++, also tested in C++. This is basically the main trick behind every smart pointer framework by overriding the -> operator.
  • CallExpression: Not tested

There are probably more expressions, such as array indices, etc. that currently do not implement HasOverloadedOperation yet.

This PR depends on the following PRs which need to be resolved first:

@oxisto oxisto changed the title operator resolution Added operator resolution for some operations Jun 29, 2024
@oxisto oxisto force-pushed the operator-resolution branch from eecbd7b to 3924bb5 Compare July 1, 2024 11:14
@oxisto oxisto force-pushed the operator-declaration branch 3 times, most recently from f86c2ac to 432105b Compare July 3, 2024 18:09
@oxisto oxisto force-pushed the operator-declaration branch 2 times, most recently from a1badb9 to 9cd5df4 Compare July 21, 2024 21:10
@oxisto oxisto force-pushed the operator-resolution branch 5 times, most recently from a0c3a4e to 4cc7f4a Compare July 21, 2024 22:04
@oxisto oxisto changed the base branch from operator-declaration to main July 21, 2024 22:05
@oxisto oxisto force-pushed the operator-resolution branch from 4cc7f4a to 5ffe807 Compare July 22, 2024 20:18
@oxisto oxisto changed the base branch from main to symbol-resolver-cleanup July 22, 2024 20:18
@oxisto oxisto force-pushed the operator-resolution branch from 5ffe807 to f055ceb Compare July 22, 2024 20:22
@oxisto oxisto force-pushed the symbol-resolver-cleanup branch from cdd2f29 to 16c584e Compare July 22, 2024 20:44
@oxisto oxisto force-pushed the operator-resolution branch from f055ceb to 494a5a9 Compare July 22, 2024 20:53
@oxisto oxisto force-pushed the symbol-resolver-cleanup branch 2 times, most recently from 1cde93a to cea51b3 Compare July 22, 2024 21:54
@oxisto oxisto force-pushed the operator-resolution branch from 494a5a9 to da8ba63 Compare July 22, 2024 22:28
@oxisto oxisto force-pushed the symbol-resolver-cleanup branch 2 times, most recently from c3db66d to 89ea939 Compare July 26, 2024 19:37
@oxisto oxisto force-pushed the operator-resolution branch 5 times, most recently from 750138e to 74d0c81 Compare July 28, 2024 18:49
@oxisto oxisto requested a review from maximiliankaul as a code owner August 24, 2024 17:07
@oxisto oxisto force-pushed the operator-resolution branch 4 times, most recently from 01c17f6 to 7241b17 Compare August 30, 2024 12:58
@oxisto oxisto added enhancement New feature or request cxx Related to C/C++ language parsing python labels Sep 2, 2024
@oxisto oxisto changed the title Added operator resolution for some operations Added basic implementation of operator overloading Sep 2, 2024
@oxisto oxisto requested a review from lshala September 2, 2024 16:15
@KuechA
Copy link
Contributor

KuechA commented Sep 5, 2024

Since the frontend has to implement this feature, it would make sense to add this to the documentation page https://github.com/Fraunhofer-AISEC/cpg/blob/main/docs/docs/CPG/impl/language.md

Copy link
Contributor

@maximiliankaul maximiliankaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the stuff I understand. These parts look good to merge after some minor improvements. However, I'm uncertain about too much of the internal pass / graph stuff to click on "approve"...

@oxisto oxisto force-pushed the operator-resolution branch 2 times, most recently from 40b46d3 to b4b8938 Compare September 9, 2024 13:43
Copy link
Collaborator

@konradweiss konradweiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some short question from my side and we should be ready to merge.

@konradweiss konradweiss self-requested a review September 12, 2024 10:25
oxisto and others added 8 commits September 12, 2024 13:04
This PR makes sure that Python now properly uses our `TypeResolver` by only setting the unqualified name for a type and let the type resolver handle the rest. Otherwise we will get incorrect results, e.g., when retrieving name of a type within a class.
@oxisto oxisto force-pushed the operator-resolution branch from b4b8938 to f08b313 Compare September 12, 2024 11:05
Copy link

sonarcloud bot commented Sep 12, 2024

@oxisto oxisto merged commit c630634 into main Sep 12, 2024
3 checks passed
@oxisto oxisto deleted the operator-resolution branch September 12, 2024 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cxx Related to C/C++ language parsing enhancement New feature or request python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants