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

Backport '[clang] static operators should evaluate object argument (reland)' to release/18.x #80109

Merged
merged 2 commits into from
Feb 3, 2024

Commits on Jan 31, 2024

  1. Backport '[clang] static operators should evaluate object argument (r…

    …eland)' to release/18.x
    
    (cherry picked from commit ee01a2c)
    
    This re-applies 30155fc with a fix for clangd.
    
    clang don't evaluate the object argument of `static operator()` and
    `static operator[]` currently, for example:
    
    ```cpp
    
    struct Foo {
        static int operator()(int x, int y) {
            std::cout << "Foo::operator()" << std::endl;
            return x + y;
        }
        static int operator[](int x, int y) {
            std::cout << "Foo::operator[]" << std::endl;
            return x + y;
        }
    };
    Foo getFoo() {
        std::cout << "getFoo()" << std::endl;
        return {};
    }
    int main() {
        std::cout << getFoo()(1, 2) << std::endl;
        std::cout << getFoo()[1, 2] << std::endl;
    }
    ```
    
    `getFoo()` is expected to be called, but clang don't call it currently
    (17.0.6). This PR fixes this issue.
    
    Fixes llvm#67976, reland llvm#68485.
    
    - **clang/lib/Sema/SemaOverload.cpp**
    - **`Sema::CreateOverloadedArraySubscriptExpr` &
    `Sema::BuildCallToObjectOfClassType`**
    Previously clang generate `CallExpr` for static operators, ignoring the
    object argument. In this PR `CXXOperatorCallExpr` is generated for
    static operators instead, with the object argument as the first
    argument.
      - **`TryObjectArgumentInitialization`**
    `const` / `volatile` objects are allowed for static methods, so that we
    can call static operators on them.
    - **clang/lib/CodeGen/CGExpr.cpp**
      - **`CodeGenFunction::EmitCall`**
    CodeGen changes for `CXXOperatorCallExpr` with static operators: emit
    and ignore the object argument first, then emit the operator call.
    - **clang/lib/AST/ExprConstant.cpp**
      - **`‎ExprEvaluatorBase::handleCallExpr‎`**
    Evaluation of static operators in constexpr also need some small changes
    to work, so that the arguments won't be out of position.
    - **clang/lib/Sema/SemaChecking.cpp**
      - **`Sema::CheckFunctionCall`**
    Code for argument checking also need to be modify, or it will fail the
    test `clang/test/SemaCXX/overloaded-operator-decl.cpp`.
    - **clang-tools-extra/clangd/InlayHints.cpp**
      - **`InlayHintVisitor::VisitCallExpr`**
    Now that the `CXXOperatorCallExpr` for static operators also have object
    argument, we should also take care of this situation in clangd.
    
    - **Added:**
        - **clang/test/AST/ast-dump-static-operators.cpp**
          Verify the AST generated for static operators.
        - **clang/test/SemaCXX/cxx2b-static-operator.cpp**
    Static operators should be able to be called on const / volatile
    objects.
    - **Modified:**
        - **clang/test/CodeGenCXX/cxx2b-static-call-operator.cpp**
        - **clang/test/CodeGenCXX/cxx2b-static-subscript-operator.cpp**
          Matching the new CodeGen.
    
    - **clang/docs/ReleaseNotes.rst**
      Update release notes.
    
    ---------
    
    Co-authored-by: Shafik Yaghmour <[email protected]>
    Co-authored-by: cor3ntin <[email protected]>
    Co-authored-by: Aaron Ballman <[email protected]>
    4 people committed Jan 31, 2024
    Configuration menu
    Copy the full SHA
    3123a09 View commit details
    Browse the repository at this point in the history

Commits on Feb 2, 2024

  1. Configuration menu
    Copy the full SHA
    761e5bf View commit details
    Browse the repository at this point in the history