diff options
author | Iain Sandoe <iain@sandoe.co.uk> | 2020-10-25 19:33:07 +0000 |
---|---|---|
committer | Iain Sandoe <iain@sandoe.co.uk> | 2020-11-13 10:40:54 +0000 |
commit | 5e28fca09c9c72bf5631efd0f0b06d52b0ebdb4d (patch) | |
tree | 668279298b8873e58c7ba3f20beb014d30666351 /gcc/objc/objc-act.c | |
parent | 64f191b152cb1df0e108a91880cb415e413bad56 (diff) | |
download | gcc-5e28fca09c9c72bf5631efd0f0b06d52b0ebdb4d.zip gcc-5e28fca09c9c72bf5631efd0f0b06d52b0ebdb4d.tar.gz gcc-5e28fca09c9c72bf5631efd0f0b06d52b0ebdb4d.tar.bz2 |
C-Family, Objective-C : Implement Objective-C nullability Part 1[PR90707].
This part of the implementation covers property nullability attributes
and includes the changes to common code. Follow-on changes will be needed
to cover Objective-C method definitions, but those are expected to be
local to the Objective-C front end.
The basis of the implementation is to translate the Objective-C-specific
keywords into an attribute (objc_nullability) which has the required
states to carry the attribute markup.
We introduce the keywords, and these are parsed and validated in the same
manner as other property attributes. The resulting value is attached to
the property as an objc_nullability attribute.
gcc/c-family/ChangeLog:
PR objc/90707
* c-common.c (c_common_reswords): null_unspecified, nullable,
nonnull, null_resettable: New keywords.
* c-common.h (enum rid): RID_NULL_UNSPECIFIED, RID_NULLABLE,
RID_NONNULL, RID_NULL_RESETTABLE: New.
(OBJC_IS_PATTR_KEYWORD): Include nullability keywords in the
ranges accepted for property attributes.
* c-attribs.c (handle_objc_nullability_attribute): New.
* c-objc.h (enum objc_property_attribute_group): Add
OBJC_PROPATTR_GROUP_NULLABLE.
(enum objc_property_attribute_kind):Add
OBJC_PROPERTY_ATTR_NULL_UNSPECIFIED, OBJC_PROPERTY_ATTR_NULLABLE,
OBJC_PROPERTY_ATTR_NONNULL, OBJC_PROPERTY_ATTR_NULL_RESETTABLE.
gcc/objc/ChangeLog:
PR objc/90707
* objc-act.c (objc_prop_attr_kind_for_rid): Handle nullability.
(objc_add_property_declaration): Handle nullability attributes.
Check that these are applicable to the property type.
* objc-act.h (enum objc_property_nullability): New.
gcc/testsuite/ChangeLog:
PR objc/90707
* obj-c++.dg/property/at-property-4.mm: Add basic nullability
tests.
* objc.dg/property/at-property-4.m: Likewise.
* obj-c++.dg/attributes/nullability-00.mm: New test.
* obj-c++.dg/property/nullability-00.mm: New test.
* objc.dg/attributes/nullability-00.m: New test.
* objc.dg/property/nullability-00.m: New test.
gcc/ChangeLog:
PR objc/90707
* doc/extend.texi: Document the objc_nullability attribute.
Diffstat (limited to 'gcc/objc/objc-act.c')
-rw-r--r-- | gcc/objc/objc-act.c | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/gcc/objc/objc-act.c b/gcc/objc/objc-act.c index e410386..2700bbe 100644 --- a/gcc/objc/objc-act.c +++ b/gcc/objc/objc-act.c @@ -825,6 +825,11 @@ objc_prop_attr_kind_for_rid (enum rid prop_rid) case RID_PROPATOMIC: return OBJC_PROPERTY_ATTR_ATOMIC; case RID_NONATOMIC: return OBJC_PROPERTY_ATTR_NONATOMIC; + case RID_NULL_UNSPECIFIED:return OBJC_PROPERTY_ATTR_NULL_UNSPECIFIED; + case RID_NULLABLE: return OBJC_PROPERTY_ATTR_NULLABLE; + case RID_NONNULL: return OBJC_PROPERTY_ATTR_NONNULL; + case RID_NULL_RESETTABLE: return OBJC_PROPERTY_ATTR_NULL_RESETTABLE; + case RID_CLASS: return OBJC_PROPERTY_ATTR_CLASS; } } @@ -995,6 +1000,27 @@ objc_add_property_declaration (location_t location, tree decl, property_nonatomic = attrs[OBJC_PROPATTR_GROUP_CLASS]->prop_kind == OBJC_PROPERTY_ATTR_CLASS; + /* Nullability specifications for the property. */ + enum objc_property_nullability property_nullability + = OBJC_PROPERTY_NULL_UNSET; + if (attrs[OBJC_PROPATTR_GROUP_NULLABLE]) + { + if (attrs[OBJC_PROPATTR_GROUP_NULLABLE]->prop_kind + == OBJC_PROPERTY_ATTR_NULL_UNSPECIFIED) + property_nullability = OBJC_PROPERTY_NULL_UNSPECIFIED; + else if (attrs[OBJC_PROPATTR_GROUP_NULLABLE]->prop_kind + == OBJC_PROPERTY_ATTR_NULLABLE) + property_nullability = OBJC_PROPERTY_NULLABLE; + else if (attrs[OBJC_PROPATTR_GROUP_NULLABLE]->prop_kind + == OBJC_PROPERTY_ATTR_NONNULL) + property_nullability = OBJC_PROPERTY_NONNULL; + else if (attrs[OBJC_PROPATTR_GROUP_NULLABLE]->prop_kind + == OBJC_PROPERTY_ATTR_NULL_RESETTABLE) + property_nullability = OBJC_PROPERTY_NULL_RESETTABLE; + else + gcc_unreachable (); + } + /* TODO: Check that the property type is an Objective-C object or a "POD". */ @@ -1272,7 +1298,8 @@ objc_add_property_declaration (location_t location, tree decl, tree property_decl = make_node (PROPERTY_DECL); /* Copy the basic information from the original decl. */ - TREE_TYPE (property_decl) = TREE_TYPE (decl); + tree p_type = TREE_TYPE (decl); + TREE_TYPE (property_decl) = p_type; DECL_SOURCE_LOCATION (property_decl) = DECL_SOURCE_LOCATION (decl); TREE_DEPRECATED (property_decl) = TREE_DEPRECATED (decl); @@ -1287,6 +1314,28 @@ objc_add_property_declaration (location_t location, tree decl, PROPERTY_IVAR_NAME (property_decl) = NULL_TREE; PROPERTY_DYNAMIC (property_decl) = 0; + /* FIXME: We seem to drop any existing DECL_ATTRIBUTES on the floor. */ + if (property_nullability != OBJC_PROPERTY_NULL_UNSET) + { + if (p_type && !POINTER_TYPE_P (p_type)) + error_at (decl_loc, "nullability specifier %qE cannot be applied to" + " non-pointer type %qT", + attrs[OBJC_PROPATTR_GROUP_NULLABLE]->name, p_type); + else if (p_type && POINTER_TYPE_P (p_type) && TREE_TYPE (p_type) + && POINTER_TYPE_P (TREE_TYPE (p_type))) + error_at (decl_loc, "nullability specifier %qE cannot be applied to" + " multi-level pointer type %qT", + attrs[OBJC_PROPATTR_GROUP_NULLABLE]->name, p_type); + else + { + tree attr_name = get_identifier ("objc_nullability"); + tree attr_value = build_int_cst (unsigned_type_node, + (unsigned)property_nullability); + tree nulla = build_tree_list (attr_name, attr_value); + DECL_ATTRIBUTES (property_decl) = nulla; + } + } + /* Remember the fact that the property was found in the @optional section in a @protocol, or not. */ if (objc_method_optional_flag) |