Add clang support for new Objective-C literal syntax for NSDictionary, NSArray,
NSNumber, and boolean literals. This includes both Sema and Codegen support.
Included is also support for new Objective-C container subscripting.
My apologies for the large patch. It was very difficult to break apart.
The patch introduces changes to the driver as well to cause clang to link
in additional runtime support when needed to support the new language features.
Docs are forthcoming to document the implementation and behavior of these features.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152137 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/CodeGenObjCXX/Inputs/literal-support.h b/test/CodeGenObjCXX/Inputs/literal-support.h
new file mode 100644
index 0000000..5680a20
--- /dev/null
+++ b/test/CodeGenObjCXX/Inputs/literal-support.h
@@ -0,0 +1,35 @@
+#ifndef OBJC_LITERAL_SUPPORT_H
+#define OBJC_LITERAL_SUPPORT_H
+
+typedef unsigned char BOOL;
+
+@interface NSNumber @end
+
+@interface NSNumber (NSNumberCreation)
++ (NSNumber *)numberWithChar:(char)value;
++ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
++ (NSNumber *)numberWithShort:(short)value;
++ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
++ (NSNumber *)numberWithInt:(int)value;
++ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
++ (NSNumber *)numberWithLong:(long)value;
++ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
++ (NSNumber *)numberWithLongLong:(long long)value;
++ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
++ (NSNumber *)numberWithFloat:(float)value;
++ (NSNumber *)numberWithDouble:(double)value;
++ (NSNumber *)numberWithBool:(BOOL)value;
+@end
+
+@interface NSArray
+@end
+
+@interface NSArray (NSArrayCreation)
++ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
+@end
+
+@interface NSDictionary
++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
+@end
+
+#endif // OBJC_LITERAL_SUPPORT_H
diff --git a/test/CodeGenObjCXX/literals.mm b/test/CodeGenObjCXX/literals.mm
new file mode 100644
index 0000000..b8946fa
--- /dev/null
+++ b/test/CodeGenObjCXX/literals.mm
@@ -0,0 +1,111 @@
+// RUN: %clang_cc1 -I %S/Inputs -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -fexceptions -fobjc-exceptions -fcxx-exceptions -fobjc-arc-exceptions -O2 -disable-llvm-optzns -o - %s | FileCheck %s
+
+#include "literal-support.h"
+
+struct X {
+ X();
+ ~X();
+ operator id() const;
+};
+
+struct Y {
+ Y();
+ ~Y();
+ operator id() const;
+};
+
+// CHECK: define void @_Z10test_arrayv
+void test_array() {
+ // CHECK: [[OBJECTS:%[a-zA-Z0-9.]+]] = alloca [2 x i8*]
+
+ // Initializing first element
+ // CHECK: [[ELEMENT0:%[a-zA-Z0-9.]+]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 0
+ // CHECK-NEXT: call void @_ZN1XC1Ev
+ // CHECK-NEXT: [[OBJECT0:%[a-zA-Z0-9.]+]] = invoke i8* @_ZNK1XcvP11objc_objectEv
+ // CHECK: [[RET0:%[a-zA-Z0-9.]+]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[OBJECT0]])
+ // CHECK: store i8* [[RET0]], i8** [[ELEMENT0]]
+
+ // Initializing the second element
+ // CHECK: [[ELEMENT1:%[a-zA-Z0-9.]+]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1
+ // CHECK-NEXT: invoke void @_ZN1YC1Ev
+ // CHECK: [[OBJECT1:%[a-zA-Z0-9.]+]] = invoke i8* @_ZNK1YcvP11objc_objectEv
+ // CHECK: [[RET1:%[a-zA-Z0-9.]+]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[OBJECT1]])
+ // CHECK: store i8* [[RET1]], i8** [[ELEMENT1]]
+
+ // Build the array
+ // CHECK: {{invoke.*@objc_msgSend}}
+ // CHECK: call i8* @objc_retainAutoreleasedReturnValue
+ id arr = @[ X(), Y() ];
+
+ // Destroy temporaries
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NOT: ret void
+ // CHECK: invoke void @_ZN1YD1Ev
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NEXT: call void @_ZN1XD1Ev
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NEXT: ret void
+
+ // Check cleanups
+ // CHECK: call void @objc_release
+ // CHECK-NOT: call void @objc_release
+ // CHECK: invoke void @_ZN1YD1Ev
+ // CHECK: call void @objc_release
+ // CHECK-NOT: call void @objc_release
+ // CHECK: invoke void @_ZN1XD1Ev
+ // CHECK-NOT: call void @objc_release
+ // CHECK: unreachable
+}
+
+// CHECK: define weak_odr void @_Z24test_array_instantiationIiEvv
+template<typename T>
+void test_array_instantiation() {
+ // CHECK: [[OBJECTS:%[a-zA-Z0-9.]+]] = alloca [2 x i8*]
+
+ // Initializing first element
+ // CHECK: [[ELEMENT0:%[a-zA-Z0-9.]+]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 0
+ // CHECK-NEXT: call void @_ZN1XC1Ev
+ // CHECK-NEXT: [[OBJECT0:%[a-zA-Z0-9.]+]] = invoke i8* @_ZNK1XcvP11objc_objectEv
+ // CHECK: [[RET0:%[a-zA-Z0-9.]+]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[OBJECT0]])
+ // CHECK: store i8* [[RET0]], i8** [[ELEMENT0]]
+
+ // Initializing the second element
+ // CHECK: [[ELEMENT1:%[a-zA-Z0-9.]+]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1
+ // CHECK-NEXT: invoke void @_ZN1YC1Ev
+ // CHECK: [[OBJECT1:%[a-zA-Z0-9.]+]] = invoke i8* @_ZNK1YcvP11objc_objectEv
+ // CHECK: [[RET1:%[a-zA-Z0-9.]+]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[OBJECT1]])
+ // CHECK: store i8* [[RET1]], i8** [[ELEMENT1]]
+
+ // Build the array
+ // CHECK: {{invoke.*@objc_msgSend}}
+ // CHECK: call i8* @objc_retainAutoreleasedReturnValue
+ id arr = @[ X(), Y() ];
+
+ // Destroy temporaries
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NOT: ret void
+ // CHECK: invoke void @_ZN1YD1Ev
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NEXT: call void @_ZN1XD1Ev
+ // CHECK-NOT: ret void
+ // CHECK: call void @objc_release
+ // CHECK-NEXT: ret void
+
+ // Check cleanups
+ // CHECK: call void @objc_release
+ // CHECK-NOT: call void @objc_release
+ // CHECK: invoke void @_ZN1YD1Ev
+ // CHECK: call void @objc_release
+ // CHECK-NOT: call void @objc_release
+ // CHECK: invoke void @_ZN1XD1Ev
+ // CHECK-NOT: call void @objc_release
+ // CHECK: unreachable
+}
+
+template void test_array_instantiation<int>();
+
diff --git a/test/CodeGenObjCXX/objc-container-subscripting-1.mm b/test/CodeGenObjCXX/objc-container-subscripting-1.mm
new file mode 100644
index 0000000..c0dd0f8
--- /dev/null
+++ b/test/CodeGenObjCXX/objc-container-subscripting-1.mm
@@ -0,0 +1,50 @@
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin -o - %s | FileCheck %s
+
+typedef unsigned int size_t;
+@protocol P @end
+@protocol NSCopying @end
+
+@interface NSMutableArray
+- (id)objectAtIndexedSubscript:(size_t)index;
+- (void)setObject:(id)object atIndexedSubscript:(size_t)index;
+@end
+
+struct S {
+ operator unsigned int ();
+ operator id ();
+};
+
+@interface NSMutableDictionary
+- (id)objectForKeyedSubscript:(id<NSCopying>)key;
+- (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key;
+@end
+
+int main() {
+ NSMutableArray<P> * array;
+ S s;
+ id oldObject = array[(int)s];
+
+ NSMutableDictionary<P> *dict;
+ dict[(id)s] = oldObject;
+ oldObject = dict[(id)s];
+
+}
+
+template <class T> void test2(NSMutableArray *a) {
+ a[10] = 0;
+}
+template void test2<int>(NSMutableArray*);
+// CHECK: define weak_odr void @_Z5test2IiEvP14NSMutableArray
+// CHECK: @objc_msgSend
+// CHECK: ret void
+
+
+template <class T> void test3(NSMutableArray *a) {
+ a[sizeof(T)] = 0;
+}
+
+template void test3<int>(NSMutableArray*);
+// CHECK: define weak_odr void @_Z5test3IiEvP14NSMutableArray
+// CHECK: @objc_msgSend
+// CHECK: ret void
+
diff --git a/test/CodeGenObjCXX/objc-container-subscripting.mm b/test/CodeGenObjCXX/objc-container-subscripting.mm
new file mode 100644
index 0000000..dfe48e9
--- /dev/null
+++ b/test/CodeGenObjCXX/objc-container-subscripting.mm
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin -o - %s | FileCheck %s
+
+typedef unsigned int size_t;
+@protocol P @end
+
+@interface NSMutableArray
+- (id)objectAtIndexedSubscript:(size_t)index;
+- (void)setObject:(id)object atIndexedSubscript:(size_t)index;
+@end
+
+struct S {
+ operator unsigned int ();
+ operator id ();
+};
+
+@interface NSMutableDictionary
+- (id)objectForKeyedSubscript:(id)key;
+- (void)setObject:(id)object forKeyedSubscript:(id)key;
+@end
+
+int main() {
+ NSMutableArray<P> * array;
+ S s;
+ id oldObject = array[(int)s];
+
+ NSMutableDictionary<P> *dict;
+ dict[(id)s] = oldObject;
+ oldObject = dict[(id)s];
+
+}
+
+template <class T> void test2(NSMutableArray *a) {
+ a[10] = 0;
+}
+template void test2<int>(NSMutableArray*);
+// CHECK: define weak_odr void @_Z5test2IiEvP14NSMutableArray
+// CHECK: @objc_msgSend
+// CHECK: ret void
+
+
+template <class T> void test3(NSMutableArray *a) {
+ a[sizeof(T)] = 0;
+}
+
+template void test3<int>(NSMutableArray*);
+// CHECK: define weak_odr void @_Z5test3IiEvP14NSMutableArray
+// CHECK: @objc_msgSend
+// CHECK: ret void
+
+// CHECK: define void @_Z11static_dataP14NSMutableArray
+void static_data(NSMutableArray *array) {
+ // CHECK: call i32 @__cxa_guard_acquire
+ // CHECK: {{call i8*.*@objc_msgSend }}
+ // CHECK: call void @__cxa_guard_release
+ static id x = array[4];
+ // CHECK: ret void
+}