diff mbox series

C++ PATCH to remove unnecessary LAMBDA_EXPR fields

Message ID CADzB+2=ymiE35BtJ_B8K3SOBW1N4BqRRr+6u8d=Otx38jzTQCA@mail.gmail.com
State New
Headers show
Series C++ PATCH to remove unnecessary LAMBDA_EXPR fields | expand

Commit Message

Jason Merrill Aug. 29, 2017, 8:15 p.m. UTC
The explicit return type for a lambda is only used during parsing, so
we can track it in a local variable.  Later on we can look at the
actual return type of the function.

Since tree_lambda_expr is typed, and the type of a lambda-expression
is the closure, we can use TREE_TYPE for the closure instead of an
additional field.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit f5fe57f1735a860988df4c3eb30c2340463852b7
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jul 6 17:37:15 2017 -0400

            Remove unnecessary LAMBDA_EXPR fields.
    
            * cp-tree.h (LAMBDA_EXPR_CLOSURE): Use TREE_TYPE.
            (LAMBDA_EXPR_RETURN_TYPE): Remove.
            (struct tree_lambda_expr): Remove closure and return_type fields.
            * lambda.c (build_lambda_expr): Don't set LAMBDA_EXPR_RETURN_TYPE.
            * pt.c (tsubst_copy_and_build): Likewise.
            * parser.c (cp_parser_lambda_declarator_opt): Track return type.
            (cp_parser_lambda_body): Adjust unspecified return type check.
            * ptree.c (cxx_print_lambda_node): Don't print closure or
            return type.
diff mbox series

Patch

diff --git a/gcc/cp/cp-tree.def b/gcc/cp/cp-tree.def
index a46f9c3..890723f 100644
--- a/gcc/cp/cp-tree.def
+++ b/gcc/cp/cp-tree.def
@@ -468,8 +468,7 @@  DEFTREECODE (TRAIT_EXPR, "trait_expr", tcc_exceptional, 0)
    LAMBDA_EXPR_THIS_CAPTURE goes straight to the capture of `this', if it exists.
    LAMBDA_EXPR_PENDING_PROXIES is a vector of capture proxies which need to
    be pushed once scope returns to the lambda.
-   LAMBDA_EXPR_MUTABLE_P signals whether this lambda was declared mutable.
-   LAMBDA_EXPR_RETURN_TYPE holds the return type, if it was specified.  */
+   LAMBDA_EXPR_MUTABLE_P signals whether this lambda was declared mutable.  */
 DEFTREECODE (LAMBDA_EXPR, "lambda_expr", tcc_exceptional, 0)
 
 /* The declared type of an expression.  This is a C++0x extension.
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index a58e7bd..ad97be4 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -1253,11 +1253,6 @@  enum cp_lambda_default_capture_mode_type {
 #define LAMBDA_EXPR_MUTABLE_P(NODE) \
   TREE_LANG_FLAG_1 (LAMBDA_EXPR_CHECK (NODE))
 
-/* The return type in the expression.
- * NULL_TREE indicates that none was specified.  */
-#define LAMBDA_EXPR_RETURN_TYPE(NODE) \
-  (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->return_type)
-
 /* The source location of the lambda.  */
 #define LAMBDA_EXPR_LOCATION(NODE) \
   (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->locus)
@@ -1276,20 +1271,17 @@  enum cp_lambda_default_capture_mode_type {
 #define LAMBDA_EXPR_PENDING_PROXIES(NODE) \
   (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->pending_proxies)
 
-/* The closure type of the lambda.  Note that the TREE_TYPE of a
-   LAMBDA_EXPR is always NULL_TREE, because we need to instantiate the
-   LAMBDA_EXPR in order to instantiate the type.  */
+/* The closure type of the lambda, which is also the type of the
+   LAMBDA_EXPR.  */
 #define LAMBDA_EXPR_CLOSURE(NODE) \
-  (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->closure)
+  (TREE_TYPE (LAMBDA_EXPR_CHECK (NODE)))
 
 struct GTY (()) tree_lambda_expr
 {
   struct tree_typed typed;
   tree capture_list;
   tree this_capture;
-  tree return_type;
   tree extra_scope;
-  tree closure;
   vec<tree, va_gc> *pending_proxies;
   location_t locus;
   enum cp_lambda_default_capture_mode_type default_capture_mode;
diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index 0e8934b..ff76178 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -42,7 +42,6 @@  build_lambda_expr (void)
   LAMBDA_EXPR_CAPTURE_LIST         (lambda) = NULL_TREE;
   LAMBDA_EXPR_THIS_CAPTURE         (lambda) = NULL_TREE;
   LAMBDA_EXPR_PENDING_PROXIES      (lambda) = NULL;
-  LAMBDA_EXPR_RETURN_TYPE          (lambda) = NULL_TREE;
   LAMBDA_EXPR_MUTABLE_P            (lambda) = false;
   return lambda;
 }
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 55f088d..07913d6 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -10419,6 +10419,7 @@  cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
   tree exception_spec = NULL_TREE;
   tree template_param_list = NULL_TREE;
   tree tx_qual = NULL_TREE;
+  tree return_type = NULL_TREE;
   cp_decl_specifier_seq lambda_specs;
   clear_decl_specs (&lambda_specs);
 
@@ -10493,8 +10494,7 @@  cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
         {
           cp_lexer_consume_token (parser->lexer);
-          LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
-	    = cp_parser_trailing_type_id (parser);
+          return_type = cp_parser_trailing_type_id (parser);
         }
 
       /* The function parameters must be in scope all the way until after the
@@ -10517,8 +10517,8 @@  cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
     void *p;
 
     clear_decl_specs (&return_type_specs);
-    if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
-      return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
+    if (return_type)
+      return_type_specs.type = return_type;
     else
       /* Maybe we will deduce the return type later.  */
       return_type_specs.type = make_auto ();
@@ -10558,7 +10558,7 @@  cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
 	DECL_ARTIFICIAL (fco) = 1;
 	/* Give the object parameter a different name.  */
 	DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
-	if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
+	if (return_type)
 	  TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
       }
     if (template_param_list)
@@ -10648,7 +10648,7 @@  cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
        nor a deducible form, errors should be reported for return statements
        in the body.  Since we used void as the placeholder return type, parsing
        the body as usual will give such desired behavior.  */
-    if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
+    if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
       {
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index aaae06d..e064a11 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -17979,9 +17979,6 @@  tsubst_copy_and_build (tree t,
 	   declaration of the op() for later calls to lambda_function.  */
 	complete_type (type);
 
-	if (tree fn = lambda_function (type))
-	  LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
-
 	LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
 
 	insert_pending_capture_proxies ();
diff --git a/gcc/cp/ptree.c b/gcc/cp/ptree.c
index 24efe27..50c717e 100644
--- a/gcc/cp/ptree.c
+++ b/gcc/cp/ptree.c
@@ -204,8 +204,6 @@  cxx_print_lambda_node (FILE *file, tree node, int indent)
   fprintf (file, "] ");
   print_node (file, "capture_list", LAMBDA_EXPR_CAPTURE_LIST (node), indent + 4);
   print_node (file, "this_capture", LAMBDA_EXPR_THIS_CAPTURE (node), indent + 4);
-  print_node (file, "return_type", LAMBDA_EXPR_RETURN_TYPE (node), indent + 4);
-  print_node (file, "closure", LAMBDA_EXPR_CLOSURE (node), indent + 4);
 }
 
 void
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 869ebad..06ad203 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -9304,12 +9304,6 @@  apply_deduced_return_type (tree fco, tree return_type)
   if (return_type == error_mark_node)
     return;
 
-  if (LAMBDA_FUNCTION_P (fco))
-    {
-      tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
-      LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
-    }
-
   if (DECL_CONV_FN_P (fco))
     DECL_NAME (fco) = make_conv_op_name (return_type);