From 413212cda18ceadf5981bb1e8f01e6ed2c9b7988 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 3 Dec 2015 20:48:37 -0500
Subject: [PATCH 30/38] FIXME: add location param to
 cp_parser_non_integral_constant_expression (unneeded?)

---
 gcc/cp/parser.c | 230 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 135 insertions(+), 95 deletions(-)

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 5787f21..5addd1c 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -2605,7 +2605,7 @@ static void cp_parser_check_for_definition_in_return_type
 static void cp_parser_check_for_invalid_template_id
   (cp_parser *, tree, enum tag_types, location_t location);
 static bool cp_parser_non_integral_constant_expression
-  (cp_parser *, non_integral_constant);
+  (cp_parser *, non_integral_constant, location_t);
 static void cp_parser_diagnose_invalid_type_name
   (cp_parser *, tree, location_t);
 static bool cp_parser_parse_and_diagnose_invalid_type_name
@@ -3012,7 +3012,8 @@ cp_parser_check_for_invalid_template_id (cp_parser* parser,
 
 static bool
 cp_parser_non_integral_constant_expression (cp_parser  *parser,
-					    non_integral_constant thing)
+					    non_integral_constant thing,
+					    location_t loc)
 {
   parser->non_integral_constant_expression_p = true;
   if (parser->integral_constant_expression_p)
@@ -3023,60 +3024,73 @@ cp_parser_non_integral_constant_expression (cp_parser  *parser,
 	  switch (thing)
 	    {
   	      case NIC_FLOAT:
-		error ("floating-point literal "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "floating-point literal "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_CAST:
-		error ("a cast to a type other than an integral or "
-		       "enumeration type cannot appear in a "
-		       "constant-expression");
+		error_at (loc,
+			  "a cast to a type other than an integral or "
+			  "enumeration type cannot appear in a "
+			  "constant-expression");
 		return true;
 	      case NIC_TYPEID:
-		error ("%<typeid%> operator "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "%<typeid%> operator "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_NCC:
-		error ("non-constant compound literals "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "non-constant compound literals "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_FUNC_CALL:
-		error ("a function call "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "a function call "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_INC:
-		error ("an increment "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "an increment "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_DEC:
-		error ("an decrement "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "an decrement "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_ARRAY_REF:
-		error ("an array reference "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "an array reference "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_ADDR_LABEL:
-		error ("the address of a label "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "the address of a label "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_OVERLOADED:
-		error ("calls to overloaded operators "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "calls to overloaded operators "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_ASSIGNMENT:
-		error ("an assignment cannot appear in a constant-expression");
+		error_at (loc,
+			  "an assignment cannot appear in a constant-expression");
 		return true;
 	      case NIC_COMMA:
-		error ("a comma operator "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "a comma operator "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_CONSTRUCTOR:
-		error ("a call to a constructor "
+		error_at (loc, "a call to a constructor "
 		       "cannot appear in a constant-expression");
 		return true;
 	      case NIC_TRANSACTION:
-		error ("a transaction expression "
-		       "cannot appear in a constant-expression");
+		error_at (loc,
+			  "a transaction expression "
+			  "cannot appear in a constant-expression");
 		return true;
 	      case NIC_THIS:
 		msg = "this";
@@ -3121,7 +3135,7 @@ cp_parser_non_integral_constant_expression (cp_parser  *parser,
 		gcc_unreachable ();
 	    }
 	  if (msg)
-	    error ("%qs cannot appear in a constant-expression", msg);
+	    error_at (loc, "%qs cannot appear in a constant-expression", msg);
 	  return true;
 	}
     }
@@ -4843,7 +4857,8 @@ cp_parser_primary_expression (cp_parser *parser,
 	     checked at that point.  If we are not within a cast, then
 	     this code is invalid.  */
 	  if (!cast_p)
-	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
+	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT,
+							token->location);
 	}
       return cp_expr (token->u.value, token->location);
 
@@ -5022,7 +5037,8 @@ cp_parser_primary_expression (cp_parser *parser,
 	      return error_mark_node;
 	    }
 	  /* Pointers cannot appear in constant-expressions.  */
-	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
+	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS,
+							  token->location))
 	    return error_mark_node;
 	  return cp_expr (finish_this_expr (), token->location);
 
@@ -5061,7 +5077,8 @@ cp_parser_primary_expression (cp_parser *parser,
 		gcc_unreachable ();
 	      }
 
-	    if (cp_parser_non_integral_constant_expression (parser, name))
+	    if (cp_parser_non_integral_constant_expression (parser, name,
+							    token->location))
 	      return error_mark_node;
 
 	    /* Look up the name.  */
@@ -5094,11 +5111,6 @@ cp_parser_primary_expression (cp_parser *parser,
 	    location_t finish_loc
 	      = cp_lexer_peek_token (parser->lexer)->location;
 	    cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
-	    /* Using `va_arg' in a constant-expression is not
-	       allowed.  */
-	    if (cp_parser_non_integral_constant_expression (parser,
-							    NIC_VA_ARG))
-	      return error_mark_node;
 	    /* Construct a location of the form:
 		 __builtin_va_arg (v, int)
 		 ~~~~~~~~~~~~~~~~~~~~~^~~~
@@ -5106,6 +5118,12 @@ cp_parser_primary_expression (cp_parser *parser,
 	       "__builtin_va_arg" token to the close paren.  */
 	    location_t combined_loc
 	      = make_location (type_location, start_loc, finish_loc);
+	    /* Using `va_arg' in a constant-expression is not
+	       allowed.  */
+	    if (cp_parser_non_integral_constant_expression (parser,
+							    NIC_VA_ARG,
+							    combined_loc))
+	      return error_mark_node;
 	    return build_x_va_arg (combined_loc, expression, type);
 	  }
 
@@ -6445,10 +6463,18 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	parser->greater_than_is_operator_p
 	  = saved_greater_than_is_operator_p;
 
+	/* Construct a location e.g. :
+	     reinterpret_cast <int *> (expr)
+	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+	   ranging from the start of the "*_cast" token to the final closing
+	   paren, with the caret at the start.  */
+	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
+
 	/* Only type conversions to integral or enumeration types
 	   can be used in constant-expressions.  */
 	if (!cast_valid_in_integral_constant_expression_p (type)
-	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
+	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST,
+							   cp_cast_loc))
 	  return error_mark_node;
 
 	switch (keyword)
@@ -6474,12 +6500,6 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	    gcc_unreachable ();
 	  }
 
-	/* Construct a location e.g. :
-	     reinterpret_cast <int *> (expr)
-	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-	   ranging from the start of the "*_cast" token to the final closing
-	   paren, with the caret at the start.  */
-	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
 	postfix_expression.set_location (cp_cast_loc);
       }
       break;
@@ -6528,7 +6548,8 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	/* Restore the saved message.  */
 	parser->type_definition_forbidden_message = saved_message;
 	/* `typeid' may not appear in an integral constant expression.  */
-	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
+	if (cp_parser_non_integral_constant_expression
+	    (parser, NIC_TYPEID, postfix_expression.get_location ()))
 	  return error_mark_node;
       }
       break;
@@ -6715,7 +6736,8 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 		   compound literals are not part of ISO C++, the
 		   standard has nothing to say.)  */
 		if (cp_parser_non_integral_constant_expression (parser,
-								NIC_NCC))
+								NIC_NCC,
+								input_location))
 		  {
 		    postfix_expression = error_mark_node;
 		    break;
@@ -6826,8 +6848,9 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	    /* Function calls are not permitted in
 	       constant-expressions.  */
 	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
-		&& cp_parser_non_integral_constant_expression (parser,
-							       NIC_FUNC_CALL))
+		&& cp_parser_non_integral_constant_expression
+		     (parser, NIC_FUNC_CALL,
+		      postfix_expression.get_location ()))
 	      {
 		postfix_expression = error_mark_node;
 		release_tree_vector (args);
@@ -6991,7 +7014,8 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	    = finish_increment_expr (postfix_expression,
 				     POSTINCREMENT_EXPR);
 	  /* Increments may not appear in constant-expressions.  */
-	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
+	  if (cp_parser_non_integral_constant_expression
+		(parser, NIC_INC, postfix_expression.get_location ()))
 	    postfix_expression = error_mark_node;
 	  idk = CP_ID_KIND_NONE;
           is_member_access = false;
@@ -7006,7 +7030,8 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
 	    = finish_increment_expr (postfix_expression,
 				     POSTDECREMENT_EXPR);
 	  /* Decrements may not appear in constant-expressions.  */
-	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
+	  if (cp_parser_non_integral_constant_expression
+		(parser, NIC_DEC, postfix_expression.get_location ()))
 	    postfix_expression = error_mark_node;
 	  idk = CP_ID_KIND_NONE;
           is_member_access = false;
@@ -7196,7 +7221,8 @@ cp_parser_postfix_open_square_expression (cp_parser *parser,
   /* When not doing offsetof, array references are not permitted in
      constant-expressions.  */
   if (!for_offsetof
-      && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
+      && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF,
+						      loc)))
     postfix_expression = error_mark_node;
 
   return postfix_expression;
@@ -7396,7 +7422,8 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser,
      constant-expressions.  */
   if (!for_offsetof
       && (cp_parser_non_integral_constant_expression
-	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
+	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT,
+	   postfix_expression.get_location ())))
     postfix_expression = error_mark_node;
 
   return postfix_expression;
@@ -7928,7 +7955,8 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
 	  /* Create an expression representing the address.  */
 	  expression = finish_label_address_expr (identifier, combined_loc);
 	  if (cp_parser_non_integral_constant_expression (parser,
-							  NIC_ADDR_LABEL))
+							  NIC_ADDR_LABEL,
+							  combined_loc))
 	    expression = error_mark_node;
 	  return expression;
 	}
@@ -8020,7 +8048,8 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
 
       if (non_constant_p != NIC_NONE
 	  && cp_parser_non_integral_constant_expression (parser,
-							 non_constant_p))
+							 non_constant_p,
+							 loc))
 	expression = error_mark_node;
 
       return expression;
@@ -8149,9 +8178,19 @@ cp_parser_new_expression (cp_parser* parser)
   else
     initializer = NULL;
 
+  /* Construct a location e.g.:
+     ptr = new int[100]
+           ^~~~~~~~~~~~
+     with caret == start at the start of the "new" token, and the end
+     at the end of the final token we consumed.  */
+  cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
+  location_t end_loc = get_finish (end_tok->location);
+  location_t combined_loc = make_location (start_loc, start_loc, end_loc);
+
   /* A new-expression may not appear in an integral constant
      expression.  */
-  if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
+  if (cp_parser_non_integral_constant_expression (parser, NIC_NEW,
+						  combined_loc))
     ret = error_mark_node;
   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
      of a new-type-id or type-id of a new-expression, the new-expression shall
@@ -8170,15 +8209,6 @@ cp_parser_new_expression (cp_parser* parser)
     }
   else
     {
-      /* Construct a location e.g.:
-           ptr = new int[100]
-                 ^~~~~~~~~~~~
-         with caret == start at the start of the "new" token, and the end
-         at the end of the final token we consumed.  */
-      cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
-      location_t end_loc = get_finish (end_tok->location);
-      location_t combined_loc = make_location (start_loc, start_loc, end_loc);
-
       /* Create a representation of the new-expression.  */
       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
 		       tf_warning_or_error);
@@ -8462,7 +8492,8 @@ cp_parser_delete_expression (cp_parser* parser)
 
   /* A delete-expression may not appear in an integral constant
      expression.  */
-  if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
+  if (cp_parser_non_integral_constant_expression (parser, NIC_DEL,
+						  input_location))
     return error_mark_node;
 
   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
@@ -8670,6 +8701,15 @@ cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
 					    /*decltype_p=*/false,
 					    pidk);
 
+	  /* Make a location:
+	       (TYPE) EXPR
+	       ^~~~~~~~~~~
+	     with start==caret at the open paren, extending to the
+	     end of "expr".  */
+	  location_t cast_loc = make_location (open_paren_loc,
+					       open_paren_loc,
+					       expr.get_finish ());
+
 	  if (cp_parser_parse_definitely (parser))
 	    {
 	      /* Warn about old-style casts, if so requested.  */
@@ -8683,18 +8723,11 @@ cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
 		 can be used in constant-expressions.  */
 	      if (!cast_valid_in_integral_constant_expression_p (type)
 		  && cp_parser_non_integral_constant_expression (parser,
-								 NIC_CAST))
+								 NIC_CAST,
+								 cast_loc))
 		return error_mark_node;
 
 	      /* Perform the cast.  */
-	      /* Make a location:
-		   (TYPE) EXPR
-		   ^~~~~~~~~~~
-		 with start==caret at the open paren, extending to the
-		 end of "expr".  */
-	      location_t cast_loc = make_location (open_paren_loc,
-						   open_paren_loc,
-						   expr.get_finish ());
 	      expr = build_c_cast (cast_loc, type, expr);
 	      return expr;
 	    }
@@ -8978,7 +9011,8 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p,
 
       if (overload
 	  && cp_parser_non_integral_constant_expression (parser,
-							 NIC_OVERLOADED))
+							 NIC_OVERLOADED,
+							 combined_loc))
 	return error_mark_node;
     }
 
@@ -9120,21 +9154,23 @@ cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
 	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
 
-	      /* An assignment may not appear in a
-		 constant-expression.  */
-	      if (cp_parser_non_integral_constant_expression (parser,
-							      NIC_ASSIGNMENT))
-		return error_mark_node;
-	      /* Build the assignment expression.  Its default
-		 location:
+	      /* Build a location of the form:
 		   LHS = RHS
 		   ~~~~^~~~~
-		 is the location of the '=' token as the
+		 with the location of the '=' token as the
 		 caret, ranging from the start of the lhs to the
 		 end of the rhs.  */
 	      loc = make_location (loc,
 				   expr.get_start (),
 				   rhs.get_finish ());
+
+	      /* An assignment may not appear in a
+		 constant-expression.  */
+	      if (cp_parser_non_integral_constant_expression (parser,
+							      NIC_ASSIGNMENT,
+							      loc))
+		return error_mark_node;
+	      /* Build the assignment expression.  */
 	      expr = build_x_modify_expr (loc, expr,
 					  assignment_operator,
 					  rhs,
@@ -9301,7 +9337,7 @@ cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
       loc = cp_lexer_peek_token (parser->lexer)->location;
       cp_lexer_consume_token (parser->lexer);
       /* A comma operator cannot appear in a constant-expression.  */
-      if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
+      if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA, loc))
 	expression = error_mark_node;
     }
 
@@ -26139,15 +26175,6 @@ cp_parser_functional_cast (cp_parser* parser, tree type)
 
   cast = build_functional_cast (type, expression_list,
                                 tf_warning_or_error);
-  /* [expr.const]/1: In an integral constant expression "only type
-     conversions to integral or enumeration type can be used".  */
-  if (TREE_CODE (type) == TYPE_DECL)
-    type = TREE_TYPE (type);
-  if (cast != error_mark_node
-      && !cast_valid_in_integral_constant_expression_p (type)
-      && cp_parser_non_integral_constant_expression (parser,
-						     NIC_CONSTRUCTOR))
-    return error_mark_node;
 
   /* Create a location of the form:
        float(i)
@@ -26157,6 +26184,18 @@ cp_parser_functional_cast (cp_parser* parser, tree type)
   location_t finish_loc
     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
+
+  /* [expr.const]/1: In an integral constant expression "only type
+     conversions to integral or enumeration type can be used".  */
+  if (TREE_CODE (type) == TYPE_DECL)
+    type = TREE_TYPE (type);
+  if (cast != error_mark_node
+      && !cast_valid_in_integral_constant_expression_p (type)
+      && cp_parser_non_integral_constant_expression (parser,
+						     NIC_CONSTRUCTOR,
+						     combined_loc))
+    return error_mark_node;
+
   cast.set_location (combined_loc);
   return cast;
 }
@@ -37186,7 +37225,8 @@ cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
   expr = build_transaction_expr (token->location, expr, this_in, noex);
   parser->in_transaction = old_in;
 
-  if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
+  if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION,
+						  token->location))
     return error_mark_node;
 
   return (flag_tm ? expr : error_mark_node);
-- 
1.8.5.3

