diff mbox

ObjC - standardize parsing of @class etc

Message ID 1285802267.006520968@192.168.2.227
State New
Headers show

Commit Message

Nicola Pero Sept. 29, 2010, 11:17 p.m. UTC
This small patch standardizes how tokens such as @class are parsed by the Objective-C 
and Objective-C++ frontends.

With this patch they are always reported as CPP_AT_NAME / RID_AT_CLASS, which is consistent 
with how @synchronized & similar are reported (CPP_AT_NAME / RID_AT_SYNCHRONIZED)
and prevents any confusions with C++ keywords (reported as RID_CLASS etc).

It also makes c_lex_one_token() in C and C++ much more similar - which may allow someone to
merge them at some point if so wished.

I also tidied up comments around etc.

No regressions - OK to apply ?

Thanks

Comments

Mike Stump Sept. 30, 2010, 2:35 a.m. UTC | #1
On Sep 29, 2010, at 4:17 PM, Nicola Pero wrote:
> This small patch standardizes how tokens such as @class are parsed by the Objective-C 
> and Objective-C++ frontends.

> No regressions - OK to apply ?

Ok.
diff mbox

Patch

Index: gcc/c-family/ChangeLog
===================================================================
--- gcc/c-family/ChangeLog      (revision 164736)
+++ gcc/c-family/ChangeLog      (working copy)
@@ -1,5 +1,10 @@ 
 2010-09-29  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * c-lex.c (c_lex_with_flags): Updated comments for CPP_AT_NAME
+       Objective-C/Objective-C++ keywords.
+
+2010-09-29  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        Merge from 'apple/trunk' branch on FSF servers. 
        
        2005-10-04  Fariborz Jahanian <fjahanian@apple.com>
Index: gcc/c-family/c-lex.c
===================================================================
--- gcc/c-family/c-lex.c        (revision 164736)
+++ gcc/c-family/c-lex.c        (working copy)
@@ -370,6 +370,12 @@  c_lex_with_flags (tree *value, location_t *loc, un
                  || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value)))
                {
                  type = CPP_AT_NAME;
+                 /* Note the complication: if we found an OBJC_CXX
+                    keyword, for example, 'class', we will be
+                    returning a token of type CPP_AT_NAME and rid
+                    code RID_CLASS (not RID_AT_CLASS).  The language
+                    parser needs to convert that to RID_AT_CLASS.
+                 */
                  break;
                }
              /* FALLTHROUGH */
Index: gcc/ChangeLog
===================================================================
--- gcc/ChangeLog       (revision 164736)
+++ gcc/ChangeLog       (working copy)
@@ -1,5 +1,20 @@ 
 2010-09-29  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * c-parser.c (c_lex_one_token): When finding a CPP_AT_NAME
+       Objective-C token, map RID_CLASS to RID_AT_CLASS and similar.
+       (c_parser_external_declaration): Use RID_AT_CLASS
+       instead of RID_CLASS.
+       (c_parser_objc_class_declaration): Same change.
+       (c_parser_objc_try_catch_statement): Use RID_AT_TRY instead of
+       RID_TRY and RID_AT_CATCH instead of RID_CATCH.
+       (c_parser_objc_class_instance_variables): Use RID_AT_PRIVATE
+       instead of RID_PRIVATE, RID_AT_PROTECTED instead of RID_PROTECTED
+       and RID_AT_PUBLIC instead of RID_PUBLIC.
+       (c_parser_statement_after_labels): Use RID_AT_TRY instead of
+       RID_TRY and RID_AT_CATCH instead of RID_CATCH.
+       
+2010-09-29  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        Merge from 'apple/trunk' branch on FSF servers.
        * c-parser.c: Applied change originally in c-parse.in.
        
Index: gcc/cp/ChangeLog
===================================================================
--- gcc/cp/ChangeLog    (revision 164736)
+++ gcc/cp/ChangeLog    (working copy)
@@ -1,3 +1,8 @@ 
+2010-09-29  Nicola Pero  <nicola.pero@meta-innovation.com>
+
+       * parser.c (cp_lexer_get_preprocessor_token): Tidied up comments
+       and indentation when finding an Objective-C++ CPP_AT_NAME token.
+
 2010-09-29  Richard Guenther  <rguenther@suse.de>
 
        * cp-tree.h (CP_DECL_CONTEXT): Check DECL_FILE_SCOPE_P.
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c     (revision 164736)
+++ gcc/cp/parser.c     (working copy)
@@ -565,21 +565,28 @@  cp_lexer_get_preprocessor_token (cp_lexer *lexer,
          token->keyword = RID_MAX;
        }
     }
-  /* Handle Objective-C++ keywords.  */
   else if (token->type == CPP_AT_NAME)
     {
+      /* This only happens in Objective-C++; it must be a keyword.  */
       token->type = CPP_KEYWORD;
       switch (C_RID_CODE (token->u.value))
        {
-       /* Map 'class' to '@class', 'private' to '@private', etc.  */
-       case RID_CLASS: token->keyword = RID_AT_CLASS; break;
-       case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
+         /* Replace 'class' with '@class', 'private' with '@private',
+            etc.  This prevents confusion with the C++ keyword
+            'class', and makes the tokens consistent with other
+            Objective-C 'AT' keywords.  For example '@class' is
+            reported as RID_AT_CLASS which is consistent with
+            '@synchronized', which is reported as
+            RID_AT_SYNCHRONIZED.
+         */
+       case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
+       case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
        case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
-       case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
-       case RID_THROW: token->keyword = RID_AT_THROW; break;
-       case RID_TRY: token->keyword = RID_AT_TRY; break;
-       case RID_CATCH: token->keyword = RID_AT_CATCH; break;
-       default: token->keyword = C_RID_CODE (token->u.value);
+       case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
+       case RID_THROW:     token->keyword = RID_AT_THROW; break;
+       case RID_TRY:       token->keyword = RID_AT_TRY; break;
+       case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
+       default:            token->keyword = C_RID_CODE (token->u.value);
        }
     }
   else if (token->type == CPP_PRAGMA)
Index: gcc/c-parser.c
===================================================================
--- gcc/c-parser.c      (revision 164736)
+++ gcc/c-parser.c      (working copy)
@@ -310,7 +310,25 @@  c_lex_one_token (c_parser *parser, c_token *token)
     case CPP_AT_NAME:
       /* This only happens in Objective-C; it must be a keyword.  */
       token->type = CPP_KEYWORD;
-      token->keyword = C_RID_CODE (token->value);
+      switch (C_RID_CODE (token->value))
+       {
+         /* Replace 'class' with '@class', 'private' with '@private',
+            etc.  This prevents confusion with the C++ keyword
+            'class', and makes the tokens consistent with other
+            Objective-C 'AT' keywords.  For example '@class' is
+            reported as RID_AT_CLASS which is consistent with
+            '@synchronized', which is reported as
+            RID_AT_SYNCHRONIZED.
+         */
+       case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
+       case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
+       case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
+       case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
+       case RID_THROW:     token->keyword = RID_AT_THROW; break;
+       case RID_TRY:       token->keyword = RID_AT_TRY; break;
+       case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
+       default:            token->keyword = C_RID_CODE (token->value);
+       }
       break;
     case CPP_COLON:
     case CPP_COMMA:
@@ -1106,7 +1124,7 @@  c_parser_external_declaration (c_parser *parser)
          gcc_assert (c_dialect_objc ());
          c_parser_objc_class_definition (parser, NULL_TREE);
          break;
-       case RID_CLASS:
+       case RID_AT_CLASS:
          gcc_assert (c_dialect_objc ());
          c_parser_objc_class_declaration (parser);
          break;
@@ -4081,7 +4099,7 @@  c_parser_statement_after_labels (c_parser *parser)
        case RID_ASM:
          stmt = c_parser_asm_statement (parser);
          break;
-       case RID_THROW:
+       case RID_AT_THROW:
          gcc_assert (c_dialect_objc ());
          c_parser_consume_token (parser);
          if (c_parser_next_token_is (parser, CPP_SEMICOLON))
@@ -4097,7 +4115,7 @@  c_parser_statement_after_labels (c_parser *parser)
              goto expect_semicolon;
            }
          break;
-       case RID_TRY:
+       case RID_AT_TRY:
          gcc_assert (c_dialect_objc ());
          c_parser_objc_try_catch_statement (parser);
          break;
@@ -6483,19 +6501,19 @@  c_parser_objc_class_instance_variables (c_parser *
          break;
        }
       /* Parse any objc-visibility-spec.  */
-      if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
+      if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
        {
          c_parser_consume_token (parser);
          objc_set_visibility (2);
          continue;
        }
-      else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
+      else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
        {
          c_parser_consume_token (parser);
          objc_set_visibility (0);
          continue;
        }
-      else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
+      else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
        {
          c_parser_consume_token (parser);
          objc_set_visibility (1);
@@ -6530,7 +6548,7 @@  static void
 c_parser_objc_class_declaration (c_parser *parser)
 {
   tree list = NULL_TREE;
-  gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
+  gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
   c_parser_consume_token (parser);
   /* Any identifiers, including those declared as type names, are OK
      here.  */
@@ -7052,12 +7070,12 @@  c_parser_objc_try_catch_statement (c_parser *parse
 {
   location_t loc;
   tree stmt;
-  gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
+  gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
   c_parser_consume_token (parser);
   loc = c_parser_peek_token (parser)->location;
   stmt = c_parser_compound_statement (parser);
   objc_begin_try_stmt (loc, stmt);
-  while (c_parser_next_token_is_keyword (parser, RID_CATCH))
+  while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
     {
       struct c_parm *parm;
       c_parser_consume_token (parser);