From: Nicola Pero Date: Wed, 29 Sep 2010 07:34:37 +0000 (+0000) Subject: In gcc/: 2010-09-29 Nicola Pero X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1973201f322176cdcc551f7ab968c4e5cd66cd83;p=gcc.git In gcc/: 2010-09-29 Nicola Pero In gcc/: 2010-09-29 Nicola Pero * c-parser.c (c_lex_one_token): In Objective-C, when dealing with a CPP_NAME which is a reserved word, clearly separate cases for OBJC_IS_PQ_KEYWORD, OBJC_IS_AT_KEYWORD and OBJC_IS_CXX_KEYWORD. In gcc/c-family: 2010-09-29 Nicola Pero * c-common.h (OBJC_IS_CXX_KEYWORD): New macro. Updated comments. (objc_is_reserved_word): Removed. * c-common.c: Updated comments. * c-lex.c (c_lex_with_flags): Use OBJC_IS_CXX_KEYWORD instead of objc_is_reserved_word. * stub-objc.c (objc_is_reserved_word): Removed. In gcc/objc/: 2010-09-29 Nicola Pero * objc-act.c (objc_is_reserved_word): Removed. In gcc/testsuite/: 2010-09-29 Nicola Pero * objc.dg/keywords-1.m: New test. * objc.dg/keywords-2.m: New test. * objc.dg/keywords-3.m: New test. * obj-c++.dg/keywords-1.mm: New test. * obj-c++.dg/keywords-2.mm: New test. From-SVN: r164715 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c6ab4f1ac79..5e5fd8ad37c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2010-09-29 Nicola Pero + + * c-parser.c (c_lex_one_token): In Objective-C, when dealing with + a CPP_NAME which is a reserved word, clearly separate cases for + OBJC_IS_PQ_KEYWORD, OBJC_IS_AT_KEYWORD and OBJC_IS_CXX_KEYWORD. + 2010-09-29 Nicola Pero * c-parser.c (c_lex_one_token): In Objective-C, do not replace diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 7480f63f321..716957381e3 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,12 @@ +2010-09-29 Nicola Pero + + * c-common.h (OBJC_IS_CXX_KEYWORD): New macro. Updated comments. + (objc_is_reserved_word): Removed. + * c-common.c: Updated comments. + * c-lex.c (c_lex_with_flags): Use OBJC_IS_CXX_KEYWORD instead of + objc_is_reserved_word. + * stub-objc.c (objc_is_reserved_word): Removed. + 2010-09-28 Iain Sandoe * c-common.h (objc_add_method_declaration): Adjust prototype to diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 63e2d70fa9d..1573d488171 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -381,8 +381,13 @@ static int resort_field_decl_cmp (const void *, const void *); If -fno-asm is used, D_ASM is added to the mask. If -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. - In C with -Wc++-compat, we warn if D_CXXWARN is set. */ + In C with -Wc++-compat, we warn if D_CXXWARN is set. + Note the complication of the D_CXX_OBJC keywords. These are + reserved words such as 'class'. In C++, 'class' is a reserved + word. In Objective-C++ it is too. In Objective-C, it is a + reserved word too, but only if it follows an '@' sign. +*/ const struct c_common_resword c_common_reswords[] = { { "_Bool", RID_BOOL, D_CONLY }, diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 45ec999745e..a9884045905 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -76,7 +76,8 @@ enum rid /* C++ */ RID_FRIEND, RID_VIRTUAL, RID_EXPLICIT, RID_EXPORT, RID_MUTABLE, - /* ObjC */ + /* ObjC ("PQ" reserved words - they do not appear after a '@' and + are keywords only in specific contexts) */ RID_IN, RID_OUT, RID_INOUT, RID_BYCOPY, RID_BYREF, RID_ONEWAY, /* C (reserved and imaginary types not implemented, so any use is a @@ -105,7 +106,8 @@ enum rid /* Too many ways of getting the name of a function as a string */ RID_FUNCTION_NAME, RID_PRETTY_FUNCTION_NAME, RID_C99_FUNCTION_NAME, - /* C++ */ + /* C++ (some of these are keywords in Objective-C as well, but only + if they appear after a '@') */ RID_BOOL, RID_WCHAR, RID_CLASS, RID_PUBLIC, RID_PRIVATE, RID_PROTECTED, RID_TEMPLATE, RID_NULL, RID_CATCH, @@ -133,7 +135,8 @@ enum rid /* C++0x */ RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR, RID_STATIC_ASSERT, - /* Objective-C */ + /* Objective-C ("AT" reserved words - they are only keywords when + they follow '@') */ RID_AT_ENCODE, RID_AT_END, RID_AT_CLASS, RID_AT_ALIAS, RID_AT_DEFS, RID_AT_PRIVATE, RID_AT_PROTECTED, RID_AT_PUBLIC, @@ -188,6 +191,18 @@ enum rid ((unsigned int) (rid) >= (unsigned int) RID_FIRST_PQ && \ (unsigned int) (rid) <= (unsigned int) RID_LAST_PQ) +/* OBJC_IS_CXX_KEYWORD recognizes the 'CXX_OBJC' keywords (such as + 'class') which are shared in a subtle way between Objective-C and + C++. When the lexer is lexing in Objective-C/Objective-C++, if it + finds '@' followed by one of these identifiers (eg, '@class'), it + recognizes the whole as an Objective-C keyword. If the identifier + is found elsewhere, it follows the rules of the C/C++ language. + */ +#define OBJC_IS_CXX_KEYWORD(rid) \ + (rid == RID_CLASS \ + || rid == RID_PUBLIC || rid == RID_PROTECTED || rid == RID_PRIVATE \ + || rid == RID_TRY || rid == RID_THROW || rid == RID_CATCH) + /* The elements of `ridpointers' are identifier nodes for the reserved type names and storage classes. It is indexed by a RID_... value. */ extern GTY ((length ("(int) RID_MAX"))) tree *ridpointers; @@ -940,7 +955,6 @@ extern void c_parse_error (const char *, enum cpp_ttype, tree, unsigned char); extern tree objc_is_class_name (tree); extern tree objc_is_object_ptr (tree); extern void objc_check_decl (tree); -extern int objc_is_reserved_word (tree); extern tree objc_common_type (tree, tree); extern bool objc_compare_types (tree, tree, int, tree); extern bool objc_have_common_type (tree, tree, int, tree); diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c index 5af574db226..8c65b8bfd6e 100644 --- a/gcc/c-family/c-lex.c +++ b/gcc/c-family/c-lex.c @@ -366,7 +366,8 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags, case CPP_NAME: *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node)); - if (objc_is_reserved_word (*value)) + if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value)) + || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value))) { type = CPP_AT_NAME; break; diff --git a/gcc/c-family/stub-objc.c b/gcc/c-family/stub-objc.c index 6e205bfb178..71a34de53ff 100644 --- a/gcc/c-family/stub-objc.c +++ b/gcc/c-family/stub-objc.c @@ -56,12 +56,6 @@ objc_check_decl (tree ARG_UNUSED (decl)) { } -int -objc_is_reserved_word (tree ARG_UNUSED (ident)) -{ - return 0; -} - tree objc_common_type (tree ARG_UNUSED (type1), tree ARG_UNUSED (type2)) { diff --git a/gcc/c-parser.c b/gcc/c-parser.c index 2ad16586ca1..d21cd767958 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -179,7 +179,11 @@ typedef struct GTY(()) c_parser { BOOL_BITFIELD in_if_block : 1; /* True if we want to lex an untranslated string. */ BOOL_BITFIELD lex_untranslated_string : 1; + /* Objective-C specific parser/lexer information. */ + + /* True if we are in a context where the Objective-C "PQ" keywords + are considered keywords. */ BOOL_BITFIELD objc_pq_context : 1; /* The following flag is needed to contextualize Objective-C lexical analysis. In some cases (e.g., 'int NSObject;'), it is @@ -236,16 +240,37 @@ c_lex_one_token (c_parser *parser, c_token *token) token->keyword = rid_code; break; } - else if (c_dialect_objc ()) + else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code)) { - if (!objc_is_reserved_word (token->value) - && (!OBJC_IS_PQ_KEYWORD (rid_code) - || parser->objc_pq_context)) + /* We found an Objective-C "pq" keyword (in, out, + inout, bycopy, byref, oneway). They need special + care because the interpretation depends on the + context. + */ + if (parser->objc_pq_context) { token->type = CPP_KEYWORD; token->keyword = rid_code; break; } + /* Else, "pq" keywords outside of the "pq" context are + not keywords, and we fall through to the code for + normal tokens. + */ + } + else if (c_dialect_objc () + && (OBJC_IS_AT_KEYWORD (rid_code) + || OBJC_IS_CXX_KEYWORD (rid_code))) + { + /* We found one of the Objective-C "@" keywords (defs, + selector, synchronized, etc) or one of the + Objective-C "cxx" keywords (class, private, + protected, public, try, catch, throw) without a + preceding '@' sign. Do nothing and fall through to + the code for normal tokens (in C++ we would still + consider the CXX ones keywords, but not in C). + */ + ; } else { diff --git a/gcc/objc/ChangeLog b/gcc/objc/ChangeLog index 26be691cc37..94c7ffafba8 100644 --- a/gcc/objc/ChangeLog +++ b/gcc/objc/ChangeLog @@ -1,3 +1,7 @@ +2010-09-29 Nicola Pero + + * objc-act.c (objc_is_reserved_word): Removed. + 2010-09-28 Iain Sandoe * objc-act.c (objc_add_method_declaration): Handle and ignore diff --git a/gcc/objc/objc-act.c b/gcc/objc/objc-act.c index 02c60ab0bdd..87eb11b2cdd 100644 --- a/gcc/objc/objc-act.c +++ b/gcc/objc/objc-act.c @@ -829,20 +829,6 @@ objc_add_instance_variable (tree decl) decl); } -/* Return 1 if IDENT is an ObjC/ObjC++ reserved keyword in the context of - an '@'. */ - -int -objc_is_reserved_word (tree ident) -{ - unsigned char code = C_RID_CODE (ident); - - return (OBJC_IS_AT_KEYWORD (code) - || code == RID_CLASS || code == RID_PUBLIC - || code == RID_PROTECTED || code == RID_PRIVATE - || code == RID_TRY || code == RID_THROW || code == RID_CATCH); -} - /* Return true if TYPE is 'id'. */ static bool diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 18f9f96fbb4..213e35c69e7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2010-09-29 Nicola Pero + + * objc.dg/keywords-1.m: New test. + * objc.dg/keywords-2.m: New test. + * objc.dg/keywords-3.m: New test. + * obj-c++.dg/keywords-1.mm: New test. + * obj-c++.dg/keywords-2.mm: New test. + 2010-09-28 Jason Merrill * g++.dg/cpp0x/rv-lvalue-req.C: Adjust messages. diff --git a/gcc/testsuite/obj-c++.dg/keywords-1.mm b/gcc/testsuite/obj-c++.dg/keywords-1.mm new file mode 100644 index 00000000000..abb4537ae64 --- /dev/null +++ b/gcc/testsuite/obj-c++.dg/keywords-1.mm @@ -0,0 +1,27 @@ +/* Test that 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway' + are not keywords outside of a "protocol qualifier" context. +*/ +/* { dg-do compile } */ + +typedef int in; + +in out (in inout) +{ + int byref = inout * 2; + + return byref + inout; +} + +@class byref; + +@interface inout +@end + +@protocol oneway; + +int main (void) +{ + in bycopy = (in)(out (0)); + + return (in)bycopy; +} diff --git a/gcc/testsuite/obj-c++.dg/keywords-2.mm b/gcc/testsuite/obj-c++.dg/keywords-2.mm new file mode 100644 index 00000000000..c81cb4d0cac --- /dev/null +++ b/gcc/testsuite/obj-c++.dg/keywords-2.mm @@ -0,0 +1,24 @@ +/* Test that 'encode', 'end', 'compatibility_alias', 'defs', + 'protocol', 'selector', finally', 'synchronized', 'interface', + 'implementation' are not keywords if not after a '@'. +*/ +/* { dg-do compile } */ + +int encode (int end) +{ + int compatibility_alias = end * 2; + int defs = compatibility_alias * 2; + int protocol = defs * 2; + int selector = protocol * 2; + int finally = selector * 2; + int synchronized = finally * 2; + int interface = synchronized * 2; + int implementation = interface * 2; + + return implementation; +} + +int main (void) +{ + return encode (0); +} diff --git a/gcc/testsuite/objc.dg/keywords-1.m b/gcc/testsuite/objc.dg/keywords-1.m new file mode 100644 index 00000000000..abb4537ae64 --- /dev/null +++ b/gcc/testsuite/objc.dg/keywords-1.m @@ -0,0 +1,27 @@ +/* Test that 'in', 'out', 'inout', 'bycopy', 'byref', 'oneway' + are not keywords outside of a "protocol qualifier" context. +*/ +/* { dg-do compile } */ + +typedef int in; + +in out (in inout) +{ + int byref = inout * 2; + + return byref + inout; +} + +@class byref; + +@interface inout +@end + +@protocol oneway; + +int main (void) +{ + in bycopy = (in)(out (0)); + + return (in)bycopy; +} diff --git a/gcc/testsuite/objc.dg/keywords-2.m b/gcc/testsuite/objc.dg/keywords-2.m new file mode 100644 index 00000000000..c81cb4d0cac --- /dev/null +++ b/gcc/testsuite/objc.dg/keywords-2.m @@ -0,0 +1,24 @@ +/* Test that 'encode', 'end', 'compatibility_alias', 'defs', + 'protocol', 'selector', finally', 'synchronized', 'interface', + 'implementation' are not keywords if not after a '@'. +*/ +/* { dg-do compile } */ + +int encode (int end) +{ + int compatibility_alias = end * 2; + int defs = compatibility_alias * 2; + int protocol = defs * 2; + int selector = protocol * 2; + int finally = selector * 2; + int synchronized = finally * 2; + int interface = synchronized * 2; + int implementation = interface * 2; + + return implementation; +} + +int main (void) +{ + return encode (0); +} diff --git a/gcc/testsuite/objc.dg/keywords-3.m b/gcc/testsuite/objc.dg/keywords-3.m new file mode 100644 index 00000000000..28c2cf50e17 --- /dev/null +++ b/gcc/testsuite/objc.dg/keywords-3.m @@ -0,0 +1,20 @@ +/* Test that 'class', 'public', 'private', protected', 'try', 'catch', + 'throw' are not keywords in pure Objective-C if not after a '@'. +*/ +/* { dg-do compile } */ + +int class (int public) +{ + int private = public; + int protected = private * 2; + int try = protected * 2; + int catch = try * 2; + int throw = catch * 2; + + return throw; +} + +int main (void) +{ + return class (0); +}