Commit 5219955c by Iain Sandoe Committed by Iain Sandoe

objective-c - add instancetype.

The instancetype has been added as a typedef alias to id
in order to allow diagnosis of cases where a class is used
or returned where an instance is expected.

This adds the typedef, and tests that we can parse it.
It doesn't alter the diagnostics yet.

gcc/objc/

2019-05-18  Iain Sandoe  <iain@sandoe.co.uk>

	* objc/objc-act.h (OCTI_INSTANCE_TYPE, OCTI_INSTANCETYPE_NAME): New.
	(objc_global_trees): Add instance type and name.
	(INSTANCE_TYPEDEF_NAME): New.
	* objc/objc-act.c (synth_module_prologue): Build decls for
	objc_instancetype_type and objc_instancetype_name.

gcc/testsuite/

2019-05-18  Iain Sandoe  <iain@sandoe.co.uk>

	* objc.dg/instancetype-0.m: New.

From-SVN: r271370
parent c1416683
2019-05-18 Iain Sandoe <iain@sandoe.co.uk>
* objc/objc-act.h (OCTI_INSTANCE_TYPE, OCTI_INSTANCETYPE_NAME): New.
(objc_global_trees): Add instance type and name.
(INSTANCE_TYPEDEF_NAME): New.
* objc/objc-act.c (synth_module_prologue): Build decls for
objc_instancetype_type and objc_instancetype_name.
2019-05-16 Martin Sebor <msebor@redhat.com> 2019-05-16 Martin Sebor <msebor@redhat.com>
* objc-act.c (objc_begin_catch_clause): Quote keywords and options * objc-act.c (objc_begin_catch_clause): Quote keywords and options
......
...@@ -2950,12 +2950,14 @@ synth_module_prologue (void) ...@@ -2950,12 +2950,14 @@ synth_module_prologue (void)
objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id); objc_class_reference = xref_tag (RECORD_TYPE, objc_class_id);
objc_object_type = build_pointer_type (objc_object_reference); objc_object_type = build_pointer_type (objc_object_reference);
objc_instancetype_type = build_pointer_type (objc_object_reference);
objc_class_type = build_pointer_type (objc_class_reference); objc_class_type = build_pointer_type (objc_class_reference);
objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME); objc_object_name = get_identifier (OBJECT_TYPEDEF_NAME);
objc_instancetype_name = get_identifier (INSTANCE_TYPEDEF_NAME);
objc_class_name = get_identifier (CLASS_TYPEDEF_NAME); objc_class_name = get_identifier (CLASS_TYPEDEF_NAME);
/* Declare the 'id' and 'Class' typedefs. */ /* Declare the 'id', 'instancetype' and 'Class' typedefs. */
type = lang_hooks.decls.pushdecl (build_decl (input_location, type = lang_hooks.decls.pushdecl (build_decl (input_location,
TYPE_DECL, TYPE_DECL,
objc_object_name, objc_object_name,
...@@ -2964,6 +2966,12 @@ synth_module_prologue (void) ...@@ -2964,6 +2966,12 @@ synth_module_prologue (void)
type = lang_hooks.decls.pushdecl (build_decl (input_location, type = lang_hooks.decls.pushdecl (build_decl (input_location,
TYPE_DECL, TYPE_DECL,
objc_instancetype_name,
objc_instancetype_type));
TREE_NO_WARNING (type) = 1;
type = lang_hooks.decls.pushdecl (build_decl (input_location,
TYPE_DECL,
objc_class_name, objc_class_name,
objc_class_type)); objc_class_type));
TREE_NO_WARNING (type) = 1; TREE_NO_WARNING (type) = 1;
......
...@@ -313,6 +313,7 @@ enum objc_tree_index ...@@ -313,6 +313,7 @@ enum objc_tree_index
OCTI_SUPER_TYPE, OCTI_SUPER_TYPE,
OCTI_SEL_TYPE, OCTI_SEL_TYPE,
OCTI_ID_TYPE, OCTI_ID_TYPE,
OCTI_INSTANCE_TYPE,
OCTI_CLS_TYPE, OCTI_CLS_TYPE,
OCTI_NST_TYPE, OCTI_NST_TYPE,
OCTI_PROTO_TYPE, OCTI_PROTO_TYPE,
...@@ -368,6 +369,7 @@ enum objc_tree_index ...@@ -368,6 +369,7 @@ enum objc_tree_index
OCTI_OBJ_ID, OCTI_OBJ_ID,
OCTI_CLS_ID, OCTI_CLS_ID,
OCTI_ID_NAME, OCTI_ID_NAME,
OCTI_INSTANCETYPE_NAME,
OCTI_CLASS_NAME, OCTI_CLASS_NAME,
OCTI_CNST_STR_ID, OCTI_CNST_STR_ID,
OCTI_CNST_STR_TYPE, OCTI_CNST_STR_TYPE,
...@@ -443,6 +445,7 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX]; ...@@ -443,6 +445,7 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX];
#define objc_super_type objc_global_trees[OCTI_SUPER_TYPE] #define objc_super_type objc_global_trees[OCTI_SUPER_TYPE]
#define objc_selector_type objc_global_trees[OCTI_SEL_TYPE] #define objc_selector_type objc_global_trees[OCTI_SEL_TYPE]
#define objc_object_type objc_global_trees[OCTI_ID_TYPE] #define objc_object_type objc_global_trees[OCTI_ID_TYPE]
#define objc_instancetype_type objc_global_trees[OCTI_INSTANCE_TYPE]
#define objc_class_type objc_global_trees[OCTI_CLS_TYPE] #define objc_class_type objc_global_trees[OCTI_CLS_TYPE]
#define objc_instance_type objc_global_trees[OCTI_NST_TYPE] #define objc_instance_type objc_global_trees[OCTI_NST_TYPE]
#define objc_protocol_type objc_global_trees[OCTI_PROTO_TYPE] #define objc_protocol_type objc_global_trees[OCTI_PROTO_TYPE]
...@@ -570,7 +573,8 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX]; ...@@ -570,7 +573,8 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX];
#define objc_object_id objc_global_trees[OCTI_OBJ_ID] #define objc_object_id objc_global_trees[OCTI_OBJ_ID]
#define objc_class_id objc_global_trees[OCTI_CLS_ID] #define objc_class_id objc_global_trees[OCTI_CLS_ID]
#define objc_object_name objc_global_trees[OCTI_ID_NAME] #define objc_object_name objc_global_trees[OCTI_ID_NAME]
#define objc_instancetype_name objc_global_trees[OCTI_INSTANCETYPE_NAME]
#define objc_class_name objc_global_trees[OCTI_CLASS_NAME] #define objc_class_name objc_global_trees[OCTI_CLASS_NAME]
/* Constant string classes. */ /* Constant string classes. */
...@@ -608,6 +612,7 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX]; ...@@ -608,6 +612,7 @@ extern GTY(()) tree objc_global_trees[OCTI_MAX];
/* Reserved tag definitions. */ /* Reserved tag definitions. */
#define OBJECT_TYPEDEF_NAME "id" #define OBJECT_TYPEDEF_NAME "id"
#define INSTANCE_TYPEDEF_NAME "instancetype"
#define CLASS_TYPEDEF_NAME "Class" #define CLASS_TYPEDEF_NAME "Class"
#define TAG_OBJECT "objc_object" #define TAG_OBJECT "objc_object"
......
2019-05-18 Iain Sandoe <iain@sandoe.co.uk>
* objc.dg/instancetype-0.m: New.
2019-05-17 Martin Sebor <msebor@redhat.com> 2019-05-17 Martin Sebor <msebor@redhat.com>
* gcc.dg/gcc_diag-11.c: Remove accidentally committed test. * gcc.dg/gcc_diag-11.c: Remove accidentally committed test.
......
/* Contributed by Iain Sandoe <iain@sandoe.co.uk>, May 2019. */
/* { dg-do compile } */
/* Basic check of parsing instancetype. */
extern id class_createInstance (id, int);
extern id class_getSuperclass (id);
@interface MyObject
{
Class isa;
}
+ (instancetype)alloc;
- (instancetype)init;
+ (instancetype)initialize;
+ (instancetype)factoryMethodA;
+ (id)factoryMethodB;
+ (Class) class;
+ (Class) superclass;
@end
@implementation MyObject
+ (instancetype)alloc { return class_createInstance (self, 0); }
- (instancetype)init { return self; }
+ (instancetype)initialize { return self; }
+ (instancetype)factoryMethodA { return [[[self class] alloc] init]; }
+ (id)factoryMethodB { return [[[self class] alloc] init]; }
+ (Class) class { return self; }
+ (Class) superclass { return class_getSuperclass (self); }
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment