[前][次][番号順一覧][スレッド一覧]

ruby-changes:60264

From: Nobuyoshi <ko1@a...>
Date: Mon, 2 Mar 2020 16:54:58 +0900 (JST)
Subject: [ruby-changes:60264] 20a2ab0825 (master): Packed stacked bit flags into one struct

https://git.ruby-lang.org/ruby.git/commit/?id=20a2ab0825

From 20a2ab0825b7e441e303002f0feeb1b643198ffc Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 2 Mar 2020 16:08:39 +0900
Subject: Packed stacked bit flags into one struct


diff --git a/parse.y b/parse.y
index f0f2f8b..99e11e7 100644
--- a/parse.y
+++ b/parse.y
@@ -26,6 +26,13 @@ https://github.com/ruby/ruby/blob/trunk/parse.y#L26
 #include <errno.h>
 #include <stdio.h>
 
+struct lex_flags {
+    unsigned int defined: 1;
+    unsigned int kwarg: 1;
+    unsigned int def: 1;
+    unsigned int class: 1;
+};
+
 #include "internal.h"
 #include "internal/compile.h"
 #include "internal/complex.h"
@@ -291,15 +298,13 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L298
 
     int max_numparam;
 
+    struct lex_flags in;
+
     unsigned int command_start:1;
     unsigned int eofp: 1;
     unsigned int ruby__end__seen: 1;
     unsigned int debug: 1;
     unsigned int has_shebang: 1;
-    unsigned int in_defined: 1;
-    unsigned int in_kwarg: 1;
-    unsigned int in_def: 1;
-    unsigned int in_class: 1;
     unsigned int token_seen: 1;
     unsigned int token_info_enabled: 1;
 # if WARN_PAST_SCOPE
@@ -1001,6 +1006,7 @@ static int looking_at_eol_p(struct parser_params *p); https://github.com/ruby/ruby/blob/trunk/parse.y#L1006
     st_table *tbl;
     const struct vtable *vars;
     struct rb_strterm_struct *strterm;
+    struct lex_flags flags;
 }
 
 %token <id>
@@ -1428,7 +1434,7 @@ stmt		: keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem https://github.com/ruby/ruby/blob/trunk/parse.y#L1434
 		    }
 		| keyword_END '{' compstmt '}'
 		    {
-			if (p->in_def) {
+			if (p->in.def) {
 			    rb_warn0("END in method; use at_exit");
 			}
 		    /*%%%*/
@@ -1576,14 +1582,14 @@ expr		: command_call https://github.com/ruby/ruby/blob/trunk/parse.y#L1582
 			value_expr($1);
 			SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
 			p->command_start = FALSE;
-			$<num>$ = p->in_kwarg;
-			p->in_kwarg = 1;
+			$<flags>$ = p->in;
+			p->in.kwarg = 1;
 		    }
 		    {$<tbl>$ = push_pvtbl(p);}
 		  p_expr
 		    {pop_pvtbl(p, $<tbl>4);}
 		    {
-			p->in_kwarg = !!$<num>3;
+			p->in.kwarg = $<flags>3.kwarg;
 		    /*%%%*/
 			$$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
 		    /*% %*/
@@ -2351,9 +2357,9 @@ arg		: lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L2357
 		    {
 			$$ = logop(p, idOROP, $1, $3, &@2, &@$);
 		    }
-		| keyword_defined opt_nl {p->in_defined = 1;} arg
+		| keyword_defined opt_nl {p->in.defined = 1;} arg
 		    {
-			p->in_defined = 0;
+			p->in.defined = 0;
 			$$ = new_defined(p, $4, &@$);
 		    }
 		| arg '?' arg opt_nl ':' arg
@@ -2754,9 +2760,9 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2760
 		    /*% %*/
 		    /*% ripper: yield0! %*/
 		    }
-		| keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
+		| keyword_defined opt_nl '(' {p->in.defined = 1;} expr rparen
 		    {
-			p->in_defined = 0;
+			p->in.defined = 0;
 			$$ = new_defined(p, $5, &@$);
 		    }
 		| keyword_not '(' expr rparen
@@ -2925,12 +2931,12 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2931
 		    }
 		| k_class cpath superclass
 		    {
-			if (p->in_def) {
+			if (p->in.def) {
 			    YYLTYPE loc = code_loc_gen(&@1, &@2);
 			    yyerror1(&loc, "class definition in method body");
 			}
-			$<num>1 = p->in_class;
-			p->in_class = 1;
+			$<flags>1 = p->in;
+			p->in.class = 1;
 			local_push(p, 0);
 		    }
 		  bodystmt
@@ -2944,13 +2950,13 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2950
 		    /*% %*/
 		    /*% ripper: class!($2, $3, $5) %*/
 			local_pop(p);
-			p->in_class = $<num>1 & 1;
+			p->in.class = $<flags>1.class;
 		    }
 		| k_class tLSHFT expr
 		    {
-			$<num>$ = (p->in_class << 1) | p->in_def;
-			p->in_def = 0;
-			p->in_class = 0;
+			$<flags>$ = p->in;
+			p->in.def = 0;
+			p->in.class = 0;
 			local_push(p, 0);
 		    }
 		  term
@@ -2965,17 +2971,17 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2971
 		    /*% %*/
 		    /*% ripper: sclass!($3, $6) %*/
 			local_pop(p);
-			p->in_def = $<num>4 & 1;
-			p->in_class = ($<num>4 >> 1) & 1;
+			p->in.def = $<flags>4.def;
+			p->in.class = $<flags>4.class;
 		    }
 		| k_module cpath
 		    {
-			if (p->in_def) {
+			if (p->in.def) {
 			    YYLTYPE loc = code_loc_gen(&@1, &@2);
 			    yyerror1(&loc, "module definition in method body");
 			}
-			$<num>1 = p->in_class;
-			p->in_class = 1;
+			$<flags>1 = p->in;
+			p->in.class = 1;
 			local_push(p, 0);
 		    }
 		  bodystmt
@@ -2989,7 +2995,7 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2995
 		    /*% %*/
 		    /*% ripper: module!($2, $4) %*/
 			local_pop(p);
-			p->in_class = $<num>1 & 1;
+			p->in.class = $<flags>1.class;
 		    }
 		| k_def fname
 		    {
@@ -2999,8 +3005,8 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L3005
 			p->cur_arg = 0;
 		    }
 		    {
-			$<num>$ = p->in_def;
-			p->in_def = 1;
+			$<flags>$ = p->in;
+			p->in.def = 1;
 		    }
 		  f_arglist
 		  bodystmt
@@ -3015,14 +3021,14 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L3021
 		    /*% %*/
 		    /*% ripper: def!($2, $5, $6) %*/
 			local_pop(p);
-			p->in_def = $<num>4 & 1;
+			p->in.def = $<flags>4.def;
 			p->cur_arg = $<id>3;
 		    }
 		| k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
 		    {
 			numparam_name(p, get_id($5));
-			$<num>4 = p->in_def;
-			p->in_def = 1;
+			$<flags>1 = p->in;
+			p->in.def = 1;
 			SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
 			local_push(p, 0);
 			$<id>$ = p->cur_arg;
@@ -3041,7 +3047,7 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L3047
 		    /*% %*/
 		    /*% ripper: defs!($2, $3, $5, $7, $8) %*/
 			local_pop(p);
-			p->in_def = $<num>4 & 1;
+			p->in.def = $<flags>1.def;
 			p->cur_arg = $<id>6;
 		    }
 		| keyword_break
@@ -3212,7 +3218,7 @@ k_end		: keyword_end https://github.com/ruby/ruby/blob/trunk/parse.y#L3218
 
 k_return	: keyword_return
 		    {
-			if (p->in_class && !p->in_def && !dyna_in_block(p))
+			if (p->in.class && !p->in.def && !dyna_in_block(p))
 			    yyerror1(&@1, "Invalid return in class/module body");
 		    }
 		;
@@ -3802,8 +3808,8 @@ p_case_body	: keyword_in https://github.com/ruby/ruby/blob/trunk/parse.y#L3808
 		    {
 			SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
 			p->command_start = FALSE;
-			$<num>$ = p->in_kwarg;
-			p->in_kwarg = 1;
+			$<flags>1 = p->in;
+			p->in.kwarg = 1;
 		    }
 		    {$<tbl>$ = push_pvtbl(p);}
 		    {$<tbl>$ = push_pktbl(p);}
@@ -3811,7 +3817,7 @@ p_case_body	: keyword_in https://github.com/ruby/ruby/blob/trunk/parse.y#L3817
 		    {pop_pktbl(p, $<tbl>4);}
 		    {pop_pvtbl(p, $<tbl>3);}
 		    {
-			p->in_kwarg = !!$<num>2;
+			p->in.kwarg = $<flags>1.kwarg;
 		    }
 		  compstmt
 		  p_cases
@@ -3958,7 +3964,7 @@ p_expr_basic	: p_value https://github.com/ruby/ruby/blob/trunk/parse.y#L3964
 		| tLBRACE
 		    {
 			$<tbl>$ = push_pktbl(p);
-			p->in_kwarg = 0;
+			p->in.kwarg = 0;
 		    }
 		  p_kwargs rbrace
 		    {
@@ -4856,13 +4862,13 @@ f_arglist	: '(' f_args rparen https://github.com/ruby/ruby/blob/trunk/parse.y#L4862
 			p->command_start = TRUE;
 		    }
 		|   {
-			$<num>$ = p->in_kwarg;
-			p->in_kwarg = 1;
+			$<flags>$ = p->in;
+			p->in.kwarg = 1;
 			SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
 		    }
-		    f_args term
+		  f_args term
 		    {
-			p->in_kwarg = !!$<num>1;
+			p->in.kwarg = $<flags>1.kwarg;
 			$$ = $2;
 			SET_LEX_STATE(EXPR_BEG);
 			p->command_start = TRUE;
@@ -8881,7 +8887,7 @@ parser_yylex(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L8887
                 dispatch_scan_event(p, tIGNORED_NL);
             }
             fallthru = FALSE;
-	    if (!c && p->in_kwarg) {
+	    if (!c && p->in.kwarg) {
 		goto normal_newline;
 	    }
 	    goto retry;
@@ -10005,7 +10011,7 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L10011
 	    return node;
 	}
 # if WARN_PAST_SCOPE
-	if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
+	if (!p->in.defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
 	    rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
 	}
 # endif
@@ -10479,7 +10485,7 @@ assignable0(struct parser_params *p, ID id, const char **err) https://github.com/ruby/ruby/blob/trunk/parse.y#L10485
       case ID_GLOBAL: return NODE_GASGN;
       case ID_INSTANCE: return NODE_IASGN;
       case ID_CONST:
-	if (!p->in_def) return NODE_CDECL;
+	if (!p->in.def) return NODE_CDECL;
 	*err = "dynamic constant assignment";
 	return -1;
       case ID_CLASS: return NODE_CVASGN;
@@ -11691,7 +11697,7 @@ new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const https://github.com/ruby/ruby/blob/trunk/parse.y#L11697
 static NODE *
 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
 {
-    if (p->in_def) {
+    if (p->in.def) {
 	yyerror1(loc, "dynamic constant assignment");
     }
     return NEW_CDECL(0, 0, (path), loc);
@@ -11700,7 +11706,7 @@ const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L11706
 static VALUE
 const_decl(struct parser_params *p, VALUE path)
 {
-    if (p->in_def) {
+    if (p->in.def) {
 	path = dispatch1(assign_error, path);
 	ripper_error(p);
     }
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]