This is an archive of the discontinued LLVM Phabricator instance.

[libc] Adjust the `cpp:function` type to support lambdas
ClosedPublic

Authored by jhuber6 on Apr 21 2023, 4:00 PM.

Details

Summary

The current function type does not support generic lambdas because it
relies on the lambda being implicitly convertible to a function pointer.
This patch adds support for this by copying the existing lightweight
function_ref type.

Diff Detail

Event Timeline

jhuber6 created this revision.Apr 21 2023, 4:00 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 21 2023, 4:00 PM
jhuber6 requested review of this revision.Apr 21 2023, 4:00 PM
jhuber6 updated this revision to Diff 515954.Apr 21 2023, 4:24 PM

Add l-value forward.

jhuber6 updated this revision to Diff 515958.Apr 21 2023, 5:05 PM

Add second SFINAE check required for overloading.

tschuett added inline comments.Apr 21 2023, 11:29 PM
libc/src/__support/CPP/functional.h
22

I am a bit confused by the naming. ISO C++ is going to call it std::function_ref.

jhuber6 added inline comments.Apr 22 2023, 4:49 AM
libc/src/__support/CPP/functional.h
22

It used to be called cpp::function and I'm basically copying llvm::function_ref, so I just changed the name for consistency.

sivachandra added inline comments.Apr 23 2023, 10:55 PM
libc/src/__support/CPP/functional.h
46

As suggested on discord, can we just replace this with __is_convertible? Minimum clang version we support is 11 and gcc version 12.2: https://libc.llvm.org/compiler_support.html

jhuber6 added inline comments.Apr 24 2023, 4:29 AM
libc/src/__support/CPP/functional.h
46

Since I ended up writing it the more generic way I'm somewhat tempted to keep it, but I try to change it.

sivachandra added inline comments.Apr 24 2023, 11:04 AM
libc/src/__support/CPP/type_traits.h
171

Can you add commentary to explain what is going on here?

174

Ditto.

jhuber6 updated this revision to Diff 516475.Apr 24 2023, 11:14 AM

Add comments

sivachandra added inline comments.Apr 24 2023, 11:19 AM
libc/src/__support/CPP/type_traits.h
171

Sorry, I meant commentary for void(static_cast<T (*)()>(nullptr)) in line 175 and for void(declval<void (&)(T)>()(declval<F>())) on line 179.

jhuber6 updated this revision to Diff 516478.Apr 24 2023, 11:35 AM

The impolementation was too complicated to explain so I used one provided by "philnik" on Discord.

jhuber6 updated this revision to Diff 516489.Apr 24 2023, 12:11 PM

Clean up.

sivachandra accepted this revision.Apr 24 2023, 1:36 PM
sivachandra added inline comments.
libc/src/__support/CPP/type_traits.h
182

I think I can reason this out but I am not sure if it comprehensive. At the same time, we don't have to be comprehensive in the libc.

This revision is now accepted and ready to land.Apr 24 2023, 1:36 PM
This revision was automatically updated to reflect the committed changes.