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-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 changed the base branch from main to symbol-resolver-cleanup July 22, 2024 20:18
@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 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 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.
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