/* Lexical conventions [gram.lex] */
-static cp_expr cp_parser_identifier
- (cp_parser *);
-static cp_expr cp_parser_string_literal
- (cp_parser *, bool, bool, bool);
-static cp_expr cp_parser_userdef_char_literal
- (cp_parser *);
-static tree cp_parser_userdef_string_literal
+static tree finish_userdef_string_literal
(tree);
-static cp_expr cp_parser_userdef_numeric_literal
- (cp_parser *);
/* Basic concepts [gram.basic] */
return error_mark_node;
}
-/* Parse a sequence of adjacent string constants. Returns a
+/* Worker for cp_parser_string_literal and cp_parser_userdef_string_literal.
+ Do not call this directly; use either of the above.
+
+ Parse a sequence of adjacent string constants. Return a
TREE_STRING representing the combined, nul-terminated string
constant. If TRANSLATE is true, translate the string to the
execution character set. If WIDE_OK is true, a wide string is
- invalid here.
+ valid here. If UDL_OK is true, a string literal with user-defined
+ suffix can be used in this context.
C++98 [lex.string] says that if a narrow string literal token is
adjacent to a wide string literal token, the behavior is undefined.
This code is largely lifted from lex_string() in c-lex.cc.
FUTURE: ObjC++ will need to handle @-strings here. */
+
static cp_expr
-cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
- bool lookup_udlit = true)
+cp_parser_string_literal_common (cp_parser *parser, bool translate,
+ bool wide_ok, bool udl_ok,
+ bool lookup_udlit)
{
tree value;
size_t count;
if (cpp_userdef_string_p (tok->type))
{
+ if (!udl_ok)
+ {
+ error_at (loc, "string literal with user-defined suffix "
+ "is invalid in this context");
+ return error_mark_node;
+ }
string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
curr_type = cpp_userdef_string_remove_type (tok->type);
curr_tok_is_userdef_p = true;
tok = cp_lexer_peek_token (parser->lexer);
if (cpp_userdef_string_p (tok->type))
{
+ if (!udl_ok)
+ {
+ error_at (loc, "string literal with user-defined suffix "
+ "is invalid in this context");
+ return error_mark_node;
+ }
string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
curr_type = cpp_userdef_string_remove_type (tok->type);
curr_tok_is_userdef_p = true;
tree literal = build_userdef_literal (suffix_id, value,
OT_NONE, NULL_TREE);
if (lookup_udlit)
- value = cp_parser_userdef_string_literal (literal);
+ value = finish_userdef_string_literal (literal);
else
value = literal;
}
return cp_expr (value, loc);
}
+/* Parse a sequence of adjacent string constants. Return a TREE_STRING
+ representing the combined, nul-terminated string constant. If
+ TRANSLATE is true, translate the string to the execution character set.
+ If WIDE_OK is true, a wide string is valid here.
+
+ This function issues an error if a user defined string literal is
+ encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
+
+static inline cp_expr
+cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
+{
+ return cp_parser_string_literal_common (parser, translate, wide_ok,
+ /*udl_ok=*/false,
+ /*lookup_udlit=*/false);
+}
+
+/* Parse a string literal or user defined string literal.
+
+ user-defined-string-literal :
+ string-literal ud-suffix
+
+ If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
+
+static inline cp_expr
+cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
+{
+ return cp_parser_string_literal_common (parser, /*translate=*/true,
+ /*wide_ok=*/true, /*udl_ok=*/true,
+ lookup_udlit);
+}
+
/* Look up a literal operator with the name and the exact arguments. */
static tree
as arguments. */
static tree
-cp_parser_userdef_string_literal (tree literal)
+finish_userdef_string_literal (tree literal)
{
tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
/* ??? Should wide strings be allowed when parser->translate_strings_p
is false (i.e. in attributes)? If not, we can kill the third
argument to cp_parser_string_literal. */
- return (cp_parser_string_literal (parser,
- parser->translate_strings_p,
- true)
- .maybe_add_location_wrapper ());
+ if (parser->translate_strings_p)
+ return (cp_parser_userdef_string_literal (parser,
+ /*lookup_udlit=*/true)
+ .maybe_add_location_wrapper ());
+ else
+ return (cp_parser_string_literal (parser,
+ /*translate=*/false,
+ /*wide_ok=*/true)
+ .maybe_add_location_wrapper ());
case CPP_OPEN_PAREN:
/* If we see `( { ' then we are looking at the beginning of
static void
cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
{
- tree linkage;
-
/* Look for the `extern' keyword. */
cp_token *extern_token
= cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
/* Look for the string-literal. */
cp_token *string_token = cp_lexer_peek_token (parser->lexer);
- linkage = cp_parser_string_literal (parser, false, false);
+ tree linkage = cp_parser_string_literal (parser, /*translate=*/false,
+ /*wide_ok=*/false);
/* Transform the literal into an identifier. If the literal is a
wide-character string, or contains embedded NULs, then we can't
cp_parser_require (parser, CPP_COMMA, RT_COMMA);
/* Parse the string-literal message. */
- message = cp_parser_string_literal (parser,
- /*translate=*/false,
- /*wide_ok=*/true);
+ message = cp_parser_string_literal (parser, /*translate=*/false,
+ /*wide_ok=*/true);
/* A `)' completes the static assertion. */
if (!parens.require_close (parser))
case CPP_STRING16_USERDEF:
case CPP_STRING32_USERDEF:
{
- cp_expr str;
tree string_tree;
int sz, len;
maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
/* Consume the string. */
- str = cp_parser_string_literal (parser, /*translate=*/true,
- /*wide_ok=*/true, /*lookup_udlit=*/false);
+ cp_expr str = cp_parser_userdef_string_literal (parser,
+ /*lookup_udlit=*/false);
if (str == error_mark_node)
return error_mark_node;
else if (TREE_CODE (str) == USERDEF_LITERAL)
static void
cp_parser_asm_definition (cp_parser* parser)
{
- tree string;
tree outputs = NULL_TREE;
tree inputs = NULL_TREE;
tree clobbers = NULL_TREE;
if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
return;
/* Look for the string. */
- string = cp_parser_string_literal (parser, false, false);
+ tree string = cp_parser_string_literal (parser, /*translate=*/false,
+ /*wide_ok=*/false);
if (string == error_mark_node)
{
cp_parser_skip_to_closing_parenthesis (parser, true, false,
static tree
cp_parser_asm_specification_opt (cp_parser* parser)
{
- cp_token *token;
- tree asm_specification;
-
/* Peek at the next token. */
- token = cp_lexer_peek_token (parser->lexer);
+ cp_token *token = cp_lexer_peek_token (parser->lexer);
/* If the next token isn't the `asm' keyword, then there's no
asm-specification. */
if (!cp_parser_is_keyword (token, RID_ASM))
parens.require_open (parser);
/* Look for the string-literal. */
- asm_specification = cp_parser_string_literal (parser, false, false);
+ tree asm_specification = cp_parser_string_literal (parser,
+ /*translate=*/false,
+ /*wide_ok=*/false);
/* Look for the `)'. */
parens.require_close (parser);
while (true)
{
- tree string_literal;
- tree expression;
tree name;
if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
else
name = NULL_TREE;
/* Look for the string-literal. */
- string_literal = cp_parser_string_literal (parser, false, false);
+ tree string_literal = cp_parser_string_literal (parser,
+ /*translate=*/false,
+ /*wide_ok=*/false);
/* Look for the `('. */
matching_parens parens;
parens.require_open (parser);
/* Parse the expression. */
- expression = cp_parser_expression (parser);
+ tree expression = cp_parser_expression (parser);
/* Look for the `)'. */
parens.require_close (parser);
while (true)
{
- tree string_literal;
-
/* Look for the string literal. */
- string_literal = cp_parser_string_literal (parser, false, false);
+ tree string_literal = cp_parser_string_literal (parser,
+ /*translate=*/false,
+ /*wide_ok=*/false);
/* Add it to the list. */
clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
/* If the next token is not a `,', then the list is
cp_lexer_consume_token (parser->lexer);
}
else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
- value = cp_parser_string_literal (parser, false, false);
+ value = cp_parser_string_literal (parser,
+ /*translate=*/false,
+ /*wide_ok=*/false);
else
{
cp_parser_error (parser, "expected identifier or "
if (ret == CPP_PRAGMA_EOL)
ret = CPP_EOF;
else if (ret == CPP_STRING)
- *value = cp_parser_string_literal (the_parser, false, false);
+ *value = cp_parser_string_literal (the_parser, /*translate=*/false,
+ /*wide_ok=*/false);
else
{
if (ret == CPP_KEYWORD)