[cfe-dev] Proposed change to __builtin_overload

Douglas Gregor dgregor at apple.com
Mon Feb 9 08:33:19 PST 2009


On Feb 9, 2009, at 7:50 AM, Howard Hinnant wrote:

> On Feb 9, 2009, at 10:21 AM, Douglas Gregor wrote:
>
>> On Feb 9, 2009, at 6:56 AM, Howard Hinnant wrote:
>>> The C++ <cmath> has to implement this same logic, but gets to use C 
>>> ++
>>> to do it. :-)  I'm not suggesting that clang's <tgmath.h> be
>>> implemented in C++.  Only that seeing a C++ implementation may spark
>>> an idea for a more flexible __builtin_overload.
>>
>> This is a valid question, though, and one I would have asked if I  
>> thought the answer might be "yes" :)
>>
>> Should we use C++ function overloading for <tgmath.h>? We could  
>> expose the ability to declare overloaded functions in C (say,  
>> through some function attribute __overloaded__) and relax a few of  
>> our if (getLangOptions().CPlusPlus) conditions to let C take the  
>> function-overloading path.
>
> Fwiw the CodeWarrior compiler has/had the ability to switch into C++- 
> mode (and back to C) in the middle of a translation unit.  The  
> CodeWarrior <tgmath.h> used this capability and was implemented in C+ 
> +, using only C++ overloading (plus __typeof__), not templates, as I  
> recall.  I.e. it was very close to Doug's suggestion above.  It  
> worked well.  I don't know if it had any advantages or disadvantages  
> compared to the __builtin_overload approach.  It was used because we  
> already had the compiler tools (switching pragma) to do it, and did  
> not already have a __builtin_overload intrinsic, and so it was a  
> quick and easy path that worked.

It would take a bit of work to support such a pragma in Clang, because  
we currently have quite a few places where we make a lot of  
assumptions about language-specific data structures based on the  
current language we're parsing (e.g., we assume that all class types  
are handled by CXXRecordDecl in C++ mode). However, it seems  
reasonable that we would work toward making Clang #pragma c++'able.

In the shorter term, I think some kind of __overload__ attribute would  
be relatively easy to implement. Most of the pain would be in making  
sure that the C++ implicit-conversion rules handle all of the funky  
C99 types :)

	- Doug



More information about the cfe-dev mailing list