Patchwork [gimplefe] splits cpp_lshift and cpp_rshift tokens into discrete cpp_less and cpp_greater tokens respectively

login
register
mail settings
Submitter Sandeep Soni
Date March 9, 2012, 1:47 a.m.
Message ID <CANY-sXVu9OmZxx=PqTv4xsCasw93qtDYmz-7iUSbQqP3sPQ=TA@mail.gmail.com>
Download mbox | patch
Permalink /patch/145642/
State New
Headers show

Comments

Sandeep Soni - March 9, 2012, 1:47 a.m.
Hi,
The current patch splits cpp_lshift and cpp_rshift tokens into
cpp_less and cpp_greater tokens respectively.The necessary changes due
to this are made throughout the parser. The changelog is as follows.
Up for review.

2012-03-08  Sandeep Soni  <soni.sandeepb@gmail.com>

	* parser.c (gl_lex_token): For every CPP_LSHIFT and CPP_RSHIFT tokens,
	split the token into two discrete CPP_LESS and CPP_GREATER tokens
	respectively.
	(gl_split_token): New. The split function for the case above.
	(gl_token_is_of_type): New. Checks if the tokens type is the expected
	type.
	(gp_parse_var_decl): Changing consumption of CPP_LSHIFT and CPP_RSHIFT
	tokens into CPP_LESS and CPP_GREATER tokens.
	(gp_parse_record_type): Ditto.
	(gp_parse_union_type): Ditto.
	(gp_parse_cond_stmt): Ditto.
	(gp_parse_goto_stmt): Ditto.
	(gp_parse_label_stmt): Ditto.
	(gp_parse_switch_stmt): Ditto.
Diego Novillo - March 13, 2012, 4:12 p.m.
On 08/03/12 20:47 , Sandeep Soni wrote:

> +/* Splits the token TOKEN into two tokens FIRST_TOKEN and SECOND_TOKEN.
> +   Note that the split should work only if the type of the TOKEN is
> +   either CPP_RSHIFT or CPP_LSHIFT which gets splitted into two tokens

s/splitted/split/

>    while (gl_lex_token (lexer, &token))
> -    VEC_safe_push (gimple_token, gc, lexer->tokens, &token);
> +    {
> +      if (gl_token_is_of_type (&token,CPP_LSHIFT) ||
> +	  gl_token_is_of_type (&token,CPP_RSHIFT))

'||' goes at the start of the second line, aligned with the first 
character after the '('.


OK with those changes.


Diego.
Sandeep Soni - March 14, 2012, 6:11 p.m.
On Tue, Mar 13, 2012 at 9:42 PM, Diego Novillo <dnovillo@google.com> wrote:
> On 08/03/12 20:47 , Sandeep Soni wrote:
>
>> +/* Splits the token TOKEN into two tokens FIRST_TOKEN and SECOND_TOKEN.
>> +   Note that the split should work only if the type of the TOKEN is
>> +   either CPP_RSHIFT or CPP_LSHIFT which gets splitted into two tokens
>
>
> s/splitted/split/
>
>>   while (gl_lex_token (lexer, &token))
>> -    VEC_safe_push (gimple_token, gc, lexer->tokens, &token);
>> +    {
>> +      if (gl_token_is_of_type (&token,CPP_LSHIFT) ||
>> +         gl_token_is_of_type (&token,CPP_RSHIFT))
>
>
> '||' goes at the start of the second line, aligned with the first character
> after the '('.
>
>
> OK with those changes.
>
>
> Diego.

Done.

Patch

Index: gcc/gimple/parser.c
===================================================================
--- gcc/gimple/parser.c	(revision 184993)
+++ gcc/gimple/parser.c	(working copy)
@@ -494,7 +494,8 @@ 
 {
   gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
-  gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
 }
 
 /* Parse a gimple_cond tuple that is read from the reader PARSER. For now we only 
@@ -516,9 +517,11 @@ 
 static void
 gp_parse_goto_stmt (gimple_parser *parser)
 {
-  gl_consume_expected_token (parser->lexer, CPP_LSHIFT);
+  gl_consume_expected_token (parser->lexer, CPP_LESS);
+  gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
-  gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
 }
 
 /* Parse a gimple_label tuple that is read from the reader PARSER. For now we only 
@@ -527,9 +530,11 @@ 
 static void
 gp_parse_label_stmt (gimple_parser *parser)
 {
-  gl_consume_expected_token (parser->lexer, CPP_LSHIFT);
+  gl_consume_expected_token (parser->lexer, CPP_LESS);
+  gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
-  gl_consume_expected_token (parser->lexer, CPP_RSHIFT);  
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);  
 }
 
 /* Parse a gimple_switch tuple that is read from the reader PARSER. For now we only 
@@ -547,25 +552,23 @@ 
   gl_consume_expected_token (parser->lexer, CPP_COLON);
   gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
 
   while (!gl_at_eof (parser->lexer))
     {
       next_token = gl_consume_token (parser->lexer);
       
-      if (next_token->type == CPP_GREATER)
+      if (next_token->type == CPP_COMMA)
         {
-          gl_consume_expected_token (parser->lexer, CPP_COMMA);
           gl_consume_expected_token (parser->lexer, CPP_NAME);
           gl_consume_expected_token (parser->lexer, CPP_NUMBER);
           gl_consume_expected_token (parser->lexer, CPP_COLON);
           gl_consume_expected_token (parser->lexer, CPP_LESS);
-          gl_consume_expected_token (parser->lexer, CPP_NAME);  
+          gl_consume_expected_token (parser->lexer, CPP_NAME);
+          gl_consume_expected_token (parser->lexer, CPP_GREATER);
         }
-      else if (next_token->type == CPP_RSHIFT)
-        {
-          next_token = gl_consume_token (parser->lexer);
-          break;
-        }
+      else if (next_token->type == CPP_GREATER)
+        break;
       else
         error_at (next_token->location, 
 	          "Incorrect use of the gimple_switch statement");
@@ -715,14 +718,14 @@ 
 static void
 gp_parse_expect_field_decl (gimple_parser *parser)
 {
-  gl_consume_expected_token (parser->lexer, CPP_NAME);
   gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
   gl_consume_expected_token (parser->lexer, CPP_COMMA);
   gl_consume_expected_token (parser->lexer, CPP_NAME);
   gl_consume_expected_token (parser->lexer, CPP_LESS);
   gl_consume_expected_token (parser->lexer, CPP_NUMBER);
-  gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
+  gl_consume_expected_token (parser->lexer, CPP_GREATER);
 
 }
 
@@ -788,7 +791,9 @@ 
    };
 
    The tuple representation is done as :
-   UNION_TYPE <some_union,4,FIELD_DECL<first_var,INTEGER_TYPE<4>>,FIELD_DECL<second_var,<REAL_TYPE<4>>>
+   UNION_TYPE <some_union,4,
+      FIELD_DECL <first_var,INTEGER_TYPE<4>>,
+      FIELD_DECL<second_var,<REAL_TYPE<4>>>
 */
 
 /* Recognizer function for Union declarations. The union tuple is read
@@ -844,7 +849,10 @@ 
    };
 
    The tuple representation is done as :
-   ENUMERAL_TYPE<some_enum,3,<FIRST,1>,<SECOND,2>,<LAST,5>>
+   ENUMERAL_TYPE<some_enum,3,
+      <FIRST,1>,
+      <SECOND,2>,
+      <LAST,5>>
 */
 
 static void
@@ -860,13 +868,10 @@ 
   while (!gl_at_eof (parser->lexer))
     {
       next_token = gl_consume_token (parser->lexer);
-      if (next_token->type == CPP_RSHIFT)
+      if (next_token->type == CPP_GREATER)
 	break;
       else if (next_token->type == CPP_COMMA)
-	{
-	  next_token = gl_consume_token (parser->lexer);
-	  gp_parse_expect_const_decl (parser);
-	}
+	gp_parse_expect_const_decl (parser);
     }  
 }
 
@@ -976,9 +981,11 @@ 
   switch (code)
     {
     case INTEGER_TYPE:
+    case REAL_TYPE:
       gl_consume_expected_token (parser->lexer, CPP_LESS);
       gl_consume_expected_token (parser->lexer, CPP_NUMBER);
-      gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
       break;
 
     case ARRAY_TYPE:
@@ -994,8 +1001,9 @@ 
       gl_consume_expected_token (parser->lexer, CPP_NAME);
       gl_consume_expected_token (parser->lexer, CPP_LESS);
       gl_consume_expected_token (parser->lexer, CPP_NUMBER);
-      gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
       gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
       break;
 
     case POINTER_TYPE:
@@ -1007,8 +1015,9 @@ 
       gl_consume_expected_token (parser->lexer, CPP_NAME);
       gl_consume_expected_token (parser->lexer, CPP_LESS);
       gl_consume_expected_token (parser->lexer, CPP_NUMBER);
-      gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
       gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
       break;
 
     case RECORD_TYPE:
@@ -1016,7 +1025,8 @@ 
     case ENUMERAL_TYPE:
       gl_consume_expected_token (parser->lexer, CPP_LESS);
       gl_consume_expected_token (parser->lexer, CPP_NAME);
-      gl_consume_expected_token (parser->lexer, CPP_RSHIFT);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
+      gl_consume_expected_token (parser->lexer, CPP_GREATER);
       break;
 
     default: 
@@ -1155,7 +1165,44 @@ 
   return itk_none;
 }
 
+/* Returns true if the type of the TOKEN is equal to EXPECTED.  */
 
+static bool
+gl_token_is_of_type (gimple_token *token, enum cpp_ttype expected)
+{
+  return (token->type == expected);
+}
+
+/* Splits the token TOKEN into two tokens FIRST_TOKEN and SECOND_TOKEN.
+   Note that the split should work only if the type of the TOKEN is
+   either CPP_RSHIFT or CPP_LSHIFT which gets splitted into two tokens
+   of the type CPP_GREATER or CPP_LESS respectively.  */
+
+static void
+gl_split_token (gimple_token *token, gimple_token *first_token,
+		    gimple_token *second_token)
+{
+  switch (token->type)
+    {
+    case CPP_RSHIFT:
+      first_token->type = CPP_GREATER;
+      second_token->type = CPP_GREATER;
+      break;
+
+    case CPP_LSHIFT:
+      first_token->type = CPP_LESS;
+      second_token->type = CPP_LESS;
+      break;
+
+    default:
+      gcc_unreachable();
+    }
+
+  first_token->location = second_token->location = token->location;
+  first_token->flags = second_token->flags = token->flags;
+  first_token->value = second_token->value = token->value;
+}
+
 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib.  */
 
 static tree
@@ -1449,11 +1496,22 @@ 
 gl_lex (gimple_lexer *lexer)
 {
   gimple_token token;
+  gimple_token first_token, second_token;
 
   timevar_push (TV_CPP);
 
   while (gl_lex_token (lexer, &token))
-    VEC_safe_push (gimple_token, gc, lexer->tokens, &token);
+    {
+      if (gl_token_is_of_type (&token,CPP_LSHIFT) ||
+	  gl_token_is_of_type (&token,CPP_RSHIFT))
+	{
+	  gl_split_token (&token, &first_token, &second_token);
+	  VEC_safe_push (gimple_token, gc, lexer->tokens, &first_token);
+	  VEC_safe_push (gimple_token, gc, lexer->tokens, &second_token);
+        }
+      else 
+	VEC_safe_push (gimple_token, gc, lexer->tokens, &token);
+    }
 
   timevar_pop (TV_CPP);
 }