Objective-C ARC. More code for Objective-C's

new APIs for literals. nfc. wip. rdar://17554063

llvm-svn: 215043
This commit is contained in:
Fariborz Jahanian 2014-08-06 23:40:31 +00:00
parent c959562092
commit 2a25dba153
8 changed files with 51 additions and 10 deletions

View File

@ -2007,6 +2007,8 @@ def err_undeclared_nsarray : Error<
def err_undeclared_nsdictionary : Error<
"NSDictionary must be available to use Objective-C dictionary "
"literals">;
def err_undeclared_alloc : Error<
"alloc must be available to use Objective-C literals">;
def err_undeclared_boxing_method : Error<
"declaration of %0 is missing in %1 class">;
def err_objc_literal_method_sig : Error<

View File

@ -685,8 +685,8 @@ public:
/// \brief The declaration of the initWithObjects:forKeys:count: method.
ObjCMethodDecl *InitDictionaryWithObjectsMethod;
/// \brief The declaration for + (id) alloc method.
ObjCMethodDecl *AllocObjectsMethod;
/// \brief The declaration for + (id) alloc method used in [NSDictionary alloc]
ObjCMethodDecl *DictAllocObjectsMethod;
/// \brief id<NSCopying> type.
QualType QIDNSCopying;

View File

@ -98,7 +98,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
InitArrayWithObjectsMethod(nullptr),
NSDictionaryDecl(nullptr), DictionaryWithObjectsMethod(nullptr),
InitDictionaryWithObjectsMethod(nullptr),
AllocObjectsMethod(nullptr),
DictAllocObjectsMethod(nullptr),
GlobalNewDeleteDeclared(false),
TUKind(TUKind),
NumSFINAEErrors(0),

View File

@ -746,6 +746,7 @@ ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) {
ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
ObjCDictionaryElement *Elements,
unsigned NumElements) {
bool Arc = getLangOpts().ObjCAutoRefCount;
// Look up the NSDictionary class, if we haven't done so already.
if (!NSDictionaryDecl) {
NamedDecl *IF = LookupSingleName(TUScope,
@ -765,9 +766,34 @@ ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
}
}
QualType IdT = Context.getObjCIdType();
if (Arc && !DictAllocObjectsMethod) {
// Find +[NSDictionary alloc] method.
IdentifierInfo *II = &Context.Idents.get("alloc");
Selector AllocSel = Context.Selectors.getSelector(0, &II);
DictAllocObjectsMethod = NSDictionaryDecl->lookupClassMethod(AllocSel);
if (!DictAllocObjectsMethod && getLangOpts().DebuggerObjCLiteral) {
DictAllocObjectsMethod = ObjCMethodDecl::Create(Context,
SourceLocation(), SourceLocation(), AllocSel,
IdT,
nullptr /*TypeSourceInfo */,
Context.getTranslationUnitDecl(),
false /*Instance*/, false/*isVariadic*/,
/*isPropertyAccessor=*/false,
/*isImplicitlyDeclared=*/true, /*isDefined=*/false,
ObjCMethodDecl::Required,
false);
SmallVector<ParmVarDecl *, 1> Params;
DictAllocObjectsMethod->setMethodParams(Context, Params, None);
}
if (!DictAllocObjectsMethod) {
Diag(SR.getBegin(), diag::err_undeclared_alloc);
return ExprError();
}
}
// Find the dictionaryWithObjects:forKeys:count: method, if we haven't done
// so already.
QualType IdT = Context.getObjCIdType();
if (!DictionaryWithObjectsMethod) {
Selector Sel = NSAPIObj->getNSDictionarySelector(
NSAPI::NSDict_dictionaryWithObjectsForKeysCount);
@ -925,7 +951,7 @@ ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
Context.getObjCInterfaceType(NSDictionaryDecl));
return MaybeBindToTemporary(ObjCDictionaryLiteral::Create(
Context, makeArrayRef(Elements, NumElements), HasPackExpansions, Ty,
DictionaryWithObjectsMethod, nullptr, SR));
DictionaryWithObjectsMethod, DictAllocObjectsMethod, SR));
}
ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,

View File

@ -3,6 +3,10 @@
typedef unsigned char BOOL;
@interface NSObject
+ (id)alloc;
@end
@interface NSNumber @end
@interface NSNumber (NSNumberCreation)
@ -21,14 +25,14 @@ typedef unsigned char BOOL;
+ (NSNumber *)numberWithBool:(BOOL)value;
@end
@interface NSArray
@interface NSArray : NSObject
@end
@interface NSArray (NSArrayCreation)
+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
@end
@interface NSDictionary
@interface NSDictionary : NSObject
+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
@end

View File

@ -3,6 +3,10 @@
typedef unsigned char BOOL;
@interface NSObject
+ (id)alloc;
@end
@interface NSNumber @end
@interface NSNumber (NSNumberCreation)
@ -21,14 +25,14 @@ typedef unsigned char BOOL;
+ (NSNumber *)numberWithBool:(BOOL)value;
@end
@interface NSArray
@interface NSArray : NSObject
@end
@interface NSArray (NSArrayCreation)
+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
@end
@interface NSDictionary
@interface NSDictionary : NSObject
+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
@end

View File

@ -5,7 +5,11 @@ typedef const void * CFTypeRef;
CFTypeRef CFBridgingRetain(id X);
id CFBridgingRelease(CFTypeRef);
@protocol NSCopying @end
@interface NSDictionary
@interface NSObject
+ (id)alloc;
@end
@interface NSDictionary : NSObject
+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
- (void)setObject:(id)object forKeyedSubscript:(id)key;
@end

View File

@ -16,6 +16,7 @@ typedef signed char BOOL;
@interface NSObject : BaseObject
- (BOOL)isEqual:(id)other;
+ (id)alloc;
@end
@interface NSNumber : NSObject