[cfe-dev] Proposed change to __builtin_overload

Douglas Gregor dgregor at apple.com
Tue Feb 10 14:57:24 PST 2009


On Feb 10, 2009, at 2:33 PM, Ken Ferry wrote:

> On Tue, Feb 10, 2009 at 1:44 PM, Chris Lattner <clattner at apple.com>  
> wrote:
>
> On Feb 9, 2009, at 8:33 AM, Douglas Gregor wrote:
> >>
> >
> > 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 :)
>
> I spoke with Doug more about this offline, and I agree.  We should add
> an __attribute__((overloadable)) or something like that.  Doug
> volunteered to tackle this when he gets to a good intermediate point
> on his template work.  I filed PR 3542 to track this.
>
> Thanks Doug!  When this is done, I'll do the horrible header hacking
> stuff to hook it up for tgmath.h to use.
>
> -Chris
>
> I'm curious if this would be useful for a case I've hit as a user of  
> compilers.
>
> In Cocoa and CoreFoundation, there are a bunch of types that  
> different as far as the compiler is concerned, but the user knows  
> that they are the same.  NSString* and CFStringRef are  
> interchangeable, for example.

Interesting.

> I'd love to be able to inform the compiler that certain types are  
> compatible, but short of that, I have an NSToCF macro that does sort  
> of a type-safe cast.  If passed an argument of type of NSString*,  
> for example, the result is type CFStringRef.  It looks like this:
>
> #define NSToCF(nsObject) ( \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSArray*), (CFArrayRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSAttributedString*), (CFAttributedStringRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSCalendar*), (CFCalendarRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSCharacterSet*), (CFCharacterSetRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSData*), (CFDataRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSDate*), (CFDateRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSDictionary*), (CFDictionaryRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSError*), (CFErrorRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSLocale*), (CFLocaleRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableArray*), (CFMutableArrayRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableAttributedString*),  
> (CFMutableAttributedStringRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableCharacterSet*), (CFMutableCharacterSetRef) 
> (nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableData*), (CFMutableDataRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableDictionary*), (CFMutableDictionaryRef) 
> (nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableSet*), (CFMutableSetRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSMutableString*), (CFMutableStringRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSNumber*), (CFNumberRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSInputStream*), (CFReadStreamRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSTimer*), (CFRunLoopTimerRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSSet*), (CFSetRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSString*), (CFStringRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSTimeZone*), (CFTimeZoneRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSURL*), (CFURLRef)(nsObject), \
> __builtin_choose_expr (__builtin_types_compatible_p (typeof  
> (nsObject), NSOutputStream*), (CFWriteStreamRef)(nsObject), \
> 1.0/*error on type not found*/ )))))))))))))))))))))))))
>
> Would the changes you're talking about make this less disgusting?


I believe so. The idea would be to create a bunch of overloaded  
functions like so:

   inline CFArrayRef NSToCF(NSArray *nsObject) __attribute__ 
((always_inline, overloaded)) { return (CFArrayRef)nsObject; }
   inline CFAttributedStringRef NSToCF(NSAttributedString *nsObject)  
__attribute__((always_inline, overloaded)) { return  
(CFAttributedStringRef)nsObject; }
   inline CFCalendarRef NSToCF(NSCalendar *nsObject) __attribute__ 
((always_inline, overloaded)) { return (CFCalendarRef)nsObject; }
   inline CFDataRef NSToCF(NSData *nsObject) __attribute__ 
((always_inline, overloaded)) { return (CFDataRef)nsObject; }
   // and so on

It's better than counting parentheses :)

	- Doug

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090210/f35c9939/attachment.html>


More information about the cfe-dev mailing list