1/* Mainly the interface between cpplib and the C front ends.
2   Copyright (C) 1987-2022 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3.  If not see
18<http://www.gnu.org/licenses/>.  */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "target.h"
24#include "c-common.h"
25#include "timevar.h"
26#include "stringpool.h"
27#include "stor-layout.h"
28#include "c-pragma.h"
29#include "debug.h"
30#include "flags.h"
31#include "file-prefix-map.h" /* remap_macro_filename()  */
32#include "langhooks.h"
33#include "attribs.h"
34
35/* We may keep statistics about how long which files took to compile.  */
36static int header_time, body_time;
37static splay_tree file_info_tree;
38
39int pending_lang_change; /* If we need to switch languages - C++ only */
40int c_header_level;	 /* depth in C headers - C++ only */
41
42static tree interpret_integer (const cpp_token *, unsigned int,
43			       enum overflow_type *);
44static tree interpret_float (const cpp_token *, unsigned int, const char *,
45			     enum overflow_type *);
46static tree interpret_fixed (const cpp_token *, unsigned int);
47static enum integer_type_kind narrowest_unsigned_type
48	(const widest_int &, unsigned int);
49static enum integer_type_kind narrowest_signed_type
50	(const widest_int &, unsigned int);
51static enum cpp_ttype lex_string (const cpp_token *, tree *, bool, bool);
52static tree lex_charconst (const cpp_token *);
53static void update_header_times (const char *);
54static int dump_one_header (splay_tree_node, void *);
55static void cb_line_change (cpp_reader *, const cpp_token *, int);
56static void cb_ident (cpp_reader *, unsigned int, const cpp_string *);
57static void cb_def_pragma (cpp_reader *, unsigned int);
58static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *);
59static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *);
60
61void
62init_c_lex (void)
63{
64  struct c_fileinfo *toplevel;
65
66  /* The get_fileinfo data structure must be initialized before
67     cpp_read_main_file is called.  */
68  toplevel = get_fileinfo ("<top level>");
69  if (flag_detailed_statistics)
70    {
71      header_time = 0;
72      body_time = get_run_time ();
73      toplevel->time = body_time;
74    }
75
76  struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
77
78  cb->line_change = cb_line_change;
79  cb->ident = cb_ident;
80  cb->def_pragma = cb_def_pragma;
81  cb->valid_pch = c_common_valid_pch;
82  cb->read_pch = c_common_read_pch;
83  cb->has_attribute = c_common_has_attribute;
84  cb->has_builtin = c_common_has_builtin;
85  cb->get_source_date_epoch = cb_get_source_date_epoch;
86  cb->get_suggestion = cb_get_suggestion;
87  cb->remap_filename = remap_macro_filename;
88
89  /* Set the debug callbacks if we can use them.  */
90  if ((debug_info_level == DINFO_LEVEL_VERBOSE
91       && dwarf_debuginfo_p ())
92      || flag_dump_go_spec != NULL)
93    {
94      cb->define = cb_define;
95      cb->undef = cb_undef;
96    }
97}
98
99struct c_fileinfo *
100get_fileinfo (const char *name)
101{
102  splay_tree_node n;
103  struct c_fileinfo *fi;
104
105  if (!file_info_tree)
106    file_info_tree = splay_tree_new (splay_tree_compare_strings,
107				     0,
108				     splay_tree_delete_pointers);
109
110  n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
111  if (n)
112    return (struct c_fileinfo *) n->value;
113
114  fi = XNEW (struct c_fileinfo);
115  fi->time = 0;
116  fi->interface_only = 0;
117  fi->interface_unknown = 1;
118  splay_tree_insert (file_info_tree, (splay_tree_key) name,
119		     (splay_tree_value) fi);
120  return fi;
121}
122
123static void
124update_header_times (const char *name)
125{
126  /* Changing files again.  This means currently collected time
127     is charged against header time, and body time starts back at 0.  */
128  if (flag_detailed_statistics)
129    {
130      int this_time = get_run_time ();
131      struct c_fileinfo *file = get_fileinfo (name);
132      header_time += this_time - body_time;
133      file->time += this_time - body_time;
134      body_time = this_time;
135    }
136}
137
138static int
139dump_one_header (splay_tree_node n, void * ARG_UNUSED (dummy))
140{
141  print_time ((const char *) n->key,
142	      ((struct c_fileinfo *) n->value)->time);
143  return 0;
144}
145
146void
147dump_time_statistics (void)
148{
149  struct c_fileinfo *file = get_fileinfo (LOCATION_FILE (input_location));
150  int this_time = get_run_time ();
151  file->time += this_time - body_time;
152
153  fprintf (stderr, "\n******\n");
154  print_time ("header files (total)", header_time);
155  print_time ("main file (total)", this_time - body_time);
156  fprintf (stderr, "ratio = %g : 1\n",
157	   (double) header_time / (double) (this_time - body_time));
158  fprintf (stderr, "\n******\n");
159
160  splay_tree_foreach (file_info_tree, dump_one_header, 0);
161}
162
163static void
164cb_ident (cpp_reader * ARG_UNUSED (pfile),
165	  unsigned int ARG_UNUSED (line),
166	  const cpp_string * ARG_UNUSED (str))
167{
168  if (!flag_no_ident)
169    {
170      /* Convert escapes in the string.  */
171      cpp_string cstr = { 0, 0 };
172      if (cpp_interpret_string (pfile, str, 1, &cstr, CPP_STRING))
173	{
174	  targetm.asm_out.output_ident ((const char *) cstr.text);
175	  free (CONST_CAST (unsigned char *, cstr.text));
176	}
177    }
178}
179
180/* Called at the start of every non-empty line.  TOKEN is the first
181   lexed token on the line.  Used for diagnostic line numbers.  */
182static void
183cb_line_change (cpp_reader * ARG_UNUSED (pfile), const cpp_token *token,
184		int parsing_args)
185{
186  if (token->type != CPP_EOF && !parsing_args)
187    input_location = token->src_loc;
188}
189
190void
191fe_file_change (const line_map_ordinary *new_map)
192{
193  if (new_map == NULL)
194    return;
195
196  if (new_map->reason == LC_ENTER)
197    {
198      /* Don't stack the main buffer on the input stack;
199	 we already did in compile_file.  */
200      if (!MAIN_FILE_P (new_map))
201	{
202	  location_t included_at = linemap_included_from (new_map);
203	  int line = 0;
204	  if (included_at > BUILTINS_LOCATION)
205	    line = SOURCE_LINE (new_map - 1, included_at);
206
207	  input_location = new_map->start_location;
208	  (*debug_hooks->start_source_file) (line, LINEMAP_FILE (new_map));
209#ifdef SYSTEM_IMPLICIT_EXTERN_C
210	  if (c_header_level)
211	    ++c_header_level;
212	  else if (LINEMAP_SYSP (new_map) == 2)
213	    {
214	      c_header_level = 1;
215	      ++pending_lang_change;
216	    }
217#endif
218	}
219    }
220  else if (new_map->reason == LC_LEAVE)
221    {
222#ifdef SYSTEM_IMPLICIT_EXTERN_C
223      if (c_header_level && --c_header_level == 0)
224	{
225	  if (LINEMAP_SYSP (new_map) == 2)
226	    warning (0, "badly nested C headers from preprocessor");
227	  --pending_lang_change;
228	}
229#endif
230      input_location = new_map->start_location;
231
232      (*debug_hooks->end_source_file) (LINEMAP_LINE (new_map));
233    }
234
235  update_header_times (LINEMAP_FILE (new_map));
236  input_location = new_map->start_location;
237}
238
239static void
240cb_def_pragma (cpp_reader *pfile, location_t loc)
241{
242  /* Issue a warning message if we have been asked to do so.  Ignore
243     unknown pragmas in system headers unless an explicit
244     -Wunknown-pragmas has been given.  */
245  if (warn_unknown_pragmas > in_system_header_at (input_location))
246    {
247      const unsigned char *space, *name;
248      const cpp_token *s;
249      location_t fe_loc = loc;
250
251      space = name = (const unsigned char *) "";
252      s = cpp_get_token (pfile);
253      if (s->type != CPP_EOF)
254	{
255	  space = cpp_token_as_text (pfile, s);
256	  s = cpp_get_token (pfile);
257	  if (s->type == CPP_NAME)
258	    name = cpp_token_as_text (pfile, s);
259	}
260
261      warning_at (fe_loc, OPT_Wunknown_pragmas, "ignoring %<#pragma %s %s%>",
262		  space, name);
263    }
264}
265
266/* #define callback for DWARF and DWARF2 debug info.  */
267static void
268cb_define (cpp_reader *pfile, location_t loc, cpp_hashnode *node)
269{
270  const struct line_map *map = linemap_lookup (line_table, loc);
271  (*debug_hooks->define) (SOURCE_LINE (linemap_check_ordinary (map), loc),
272			  (const char *) cpp_macro_definition (pfile, node));
273}
274
275/* #undef callback for DWARF and DWARF2 debug info.  */
276static void
277cb_undef (cpp_reader *pfile, location_t loc, cpp_hashnode *node)
278{
279  if (lang_hooks.preprocess_undef)
280    lang_hooks.preprocess_undef (pfile, loc, node);
281
282  const struct line_map *map = linemap_lookup (line_table, loc);
283  (*debug_hooks->undef) (SOURCE_LINE (linemap_check_ordinary (map), loc),
284			 (const char *) NODE_NAME (node));
285}
286
287/* Wrapper around cpp_get_token to skip CPP_PADDING tokens
288   and not consume CPP_EOF.  */
289static const cpp_token *
290get_token_no_padding (cpp_reader *pfile)
291{
292  for (;;)
293    {
294      const cpp_token *ret = cpp_peek_token (pfile, 0);
295      if (ret->type == CPP_EOF)
296	return ret;
297      ret = cpp_get_token (pfile);
298      if (ret->type != CPP_PADDING)
299	return ret;
300    }
301}
302
303/* Callback for has_attribute.  */
304int
305c_common_has_attribute (cpp_reader *pfile, bool std_syntax)
306{
307  int result = 0;
308  tree attr_name = NULL_TREE;
309  const cpp_token *token;
310
311  token = get_token_no_padding (pfile);
312  if (token->type != CPP_OPEN_PAREN)
313    {
314      cpp_error (pfile, CPP_DL_ERROR,
315		 "missing '(' after \"__has_attribute\"");
316      return 0;
317    }
318  token = get_token_no_padding (pfile);
319  if (token->type == CPP_NAME)
320    {
321      attr_name = get_identifier ((const char *)
322				  cpp_token_as_text (pfile, token));
323      attr_name = canonicalize_attr_name (attr_name);
324      bool have_scope = false;
325      int idx = 0;
326      const cpp_token *nxt_token;
327      do
328	nxt_token = cpp_peek_token (pfile, idx++);
329      while (nxt_token->type == CPP_PADDING);
330      if (!c_dialect_cxx ()
331	  && nxt_token->type == CPP_COLON
332	  && (nxt_token->flags & COLON_SCOPE) != 0)
333	{
334	  const cpp_token *prev_token = nxt_token;
335	  do
336	    nxt_token = cpp_peek_token (pfile, idx++);
337	  while (nxt_token->type == CPP_PADDING);
338	  if (nxt_token->type == CPP_COLON)
339	    {
340	      /* __has_attribute (vendor::attr) in -std=c17 etc. modes.
341		 :: isn't CPP_SCOPE but 2 CPP_COLON tokens, where the
342		 first one should have COLON_SCOPE flag to distinguish
343		 it from : :.  */
344	      have_scope = true;
345	      get_token_no_padding (pfile); // Eat first colon.
346	    }
347	  else
348	    nxt_token = prev_token;
349	}
350      if (nxt_token->type == CPP_SCOPE || have_scope)
351	{
352	  get_token_no_padding (pfile); // Eat scope.
353	  nxt_token = get_token_no_padding (pfile);
354	  if (nxt_token->type == CPP_NAME)
355	    {
356	      tree attr_ns = attr_name;
357	      tree attr_id
358		= get_identifier ((const char *)
359				  cpp_token_as_text (pfile, nxt_token));
360	      attr_id = canonicalize_attr_name (attr_id);
361	      if (c_dialect_cxx ())
362		{
363		  /* OpenMP attributes need special handling.  */
364		  if ((flag_openmp || flag_openmp_simd)
365		      && is_attribute_p ("omp", attr_ns)
366		      && (is_attribute_p ("directive", attr_id)
367			  || is_attribute_p ("sequence", attr_id)))
368		    result = 1;
369		}
370	      if (result)
371		attr_name = NULL_TREE;
372	      else
373		attr_name = build_tree_list (attr_ns, attr_id);
374	    }
375	  else
376	    {
377	      cpp_error (pfile, CPP_DL_ERROR,
378			 "attribute identifier required after scope");
379	      attr_name = NULL_TREE;
380	    }
381	  if (have_scope)
382	    {
383	      /* The parser in this case won't be able to parse
384		 [[vendor::attr]], so ensure 0 is returned.  */
385	      result = 0;
386	      attr_name = NULL_TREE;
387	    }
388	  else
389	    have_scope = true;
390	}
391      else
392	{
393	  /* Some standard attributes need special handling.  */
394	  if (c_dialect_cxx ())
395	    {
396	      if (is_attribute_p ("noreturn", attr_name))
397		result = 200809;
398	      else if (is_attribute_p ("deprecated", attr_name))
399		result = 201309;
400	      else if (is_attribute_p ("maybe_unused", attr_name)
401		       || is_attribute_p ("fallthrough", attr_name))
402		result = 201603;
403	      else if (is_attribute_p ("no_unique_address", attr_name)
404		       || is_attribute_p ("likely", attr_name)
405		       || is_attribute_p ("unlikely", attr_name))
406		result = 201803;
407	      else if (is_attribute_p ("nodiscard", attr_name))
408		result = 201907;
409	    }
410	  else
411	    {
412	      if (is_attribute_p ("deprecated", attr_name)
413		  || is_attribute_p ("maybe_unused", attr_name)
414		  || is_attribute_p ("fallthrough", attr_name))
415		result = 201904;
416	      else if (is_attribute_p ("nodiscard", attr_name))
417		result = 202003;
418	    }
419	  if (result)
420	    attr_name = NULL_TREE;
421	}
422      if (attr_name && (have_scope || !std_syntax))
423	{
424	  init_attributes ();
425	  const struct attribute_spec *attr = lookup_attribute_spec (attr_name);
426	  if (attr)
427	    result = 1;
428	}
429    }
430  else
431    {
432      cpp_error (pfile, CPP_DL_ERROR,
433		 "macro \"__has_attribute\" requires an identifier");
434      return 0;
435    }
436
437  if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
438    cpp_error (pfile, CPP_DL_ERROR,
439	       "missing ')' after \"__has_attribute\"");
440
441  return result;
442}
443
444/* Callback for has_builtin.  */
445
446int
447c_common_has_builtin (cpp_reader *pfile)
448{
449  const cpp_token *token = get_token_no_padding (pfile);
450  if (token->type != CPP_OPEN_PAREN)
451    {
452      cpp_error (pfile, CPP_DL_ERROR,
453		 "missing '(' after \"__has_builtin\"");
454      return 0;
455    }
456
457  const char *name = "";
458  token = get_token_no_padding (pfile);
459  if (token->type == CPP_NAME)
460    {
461      name = (const char *) cpp_token_as_text (pfile, token);
462      token = get_token_no_padding (pfile);
463      if (token->type != CPP_CLOSE_PAREN)
464	{
465	  cpp_error (pfile, CPP_DL_ERROR,
466		     "expected ')' after \"%s\"", name);
467	  name = "";
468	}
469    }
470  else
471    {
472      cpp_error (pfile, CPP_DL_ERROR,
473		 "macro \"__has_builtin\" requires an identifier");
474      if (token->type == CPP_CLOSE_PAREN)
475	return 0;
476    }
477
478  /* Consume tokens up to the closing parenthesis, including any nested
479     pairs of parentheses, to avoid confusing redundant errors.  */
480  for (unsigned nparen = 1; ; token = get_token_no_padding (pfile))
481    {
482      if (token->type == CPP_OPEN_PAREN)
483	++nparen;
484      else if (token->type == CPP_CLOSE_PAREN)
485	--nparen;
486      else if (token->type == CPP_EOF)
487	break;
488      if (!nparen)
489	break;
490    }
491
492  return names_builtin_p (name);
493}
494
495
496/* Read a token and return its type.  Fill *VALUE with its value, if
497   applicable.  Fill *CPP_FLAGS with the token's flags, if it is
498   non-NULL.  */
499
500enum cpp_ttype
501c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
502		  int lex_flags)
503{
504  const cpp_token *tok;
505  enum cpp_ttype type;
506  unsigned char add_flags = 0;
507  enum overflow_type overflow = OT_NONE;
508
509  timevar_push (TV_CPP);
510 retry:
511  tok = cpp_get_token_with_location (parse_in, loc);
512  type = tok->type;
513
514 retry_after_at:
515  switch (type)
516    {
517    case CPP_PADDING:
518      goto retry;
519
520    case CPP_NAME:
521      *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
522      break;
523
524    case CPP_NUMBER:
525      {
526	const char *suffix = NULL;
527	unsigned int flags = cpp_classify_number (parse_in, tok, &suffix, *loc);
528
529	switch (flags & CPP_N_CATEGORY)
530	  {
531	  case CPP_N_INVALID:
532	    /* cpplib has issued an error.  */
533	    *value = error_mark_node;
534	    break;
535
536	  case CPP_N_INTEGER:
537	    /* C++ uses '0' to mark virtual functions as pure.
538	       Set PURE_ZERO to pass this information to the C++ parser.  */
539	    if (tok->val.str.len == 1 && *tok->val.str.text == '0')
540	      add_flags = PURE_ZERO;
541	    *value = interpret_integer (tok, flags, &overflow);
542	    break;
543
544	  case CPP_N_FLOATING:
545	    *value = interpret_float (tok, flags, suffix, &overflow);
546	    break;
547
548	  default:
549	    gcc_unreachable ();
550	  }
551
552	if (flags & CPP_N_USERDEF)
553	  {
554	    char *str;
555	    tree literal;
556	    tree suffix_id = get_identifier (suffix);
557	    int len = tok->val.str.len - strlen (suffix);
558	    /* If this is going to be used as a C string to pass to a
559	       raw literal operator, we need to add a trailing NUL.  */
560	    tree num_string = build_string (len + 1,
561					    (const char *) tok->val.str.text);
562	    TREE_TYPE (num_string) = char_array_type_node;
563	    num_string = fix_string_type (num_string);
564	    str = CONST_CAST (char *, TREE_STRING_POINTER (num_string));
565	    str[len] = '\0';
566	    literal = build_userdef_literal (suffix_id, *value, overflow,
567					     num_string);
568	    *value = literal;
569	  }
570      }
571      break;
572
573    case CPP_ATSIGN:
574      /* An @ may give the next token special significance in Objective-C.  */
575      if (c_dialect_objc ())
576	{
577	  location_t atloc = *loc;
578	  location_t newloc;
579
580	retry_at:
581	  tok = cpp_get_token_with_location (parse_in, &newloc);
582	  type = tok->type;
583	  switch (type)
584	    {
585	    case CPP_PADDING:
586	      goto retry_at;
587
588	    case CPP_STRING:
589	    case CPP_WSTRING:
590	    case CPP_STRING16:
591	    case CPP_STRING32:
592	    case CPP_UTF8STRING:
593	      type = lex_string (tok, value, true, true);
594	      break;
595
596	    case CPP_NAME:
597	      *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
598	      if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value))
599		  || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value)))
600		{
601		  type = CPP_AT_NAME;
602		  /* Note the complication: if we found an OBJC_CXX
603		     keyword, for example, 'class', we will be
604		     returning a token of type CPP_AT_NAME and rid
605		     code RID_CLASS (not RID_AT_CLASS).  The language
606		     parser needs to convert that to RID_AT_CLASS.
607		     However, we've now spliced the '@' together with the
608		     keyword that follows; Adjust the location so that we
609		     get a source range covering the composite.
610		  */
611	         *loc = make_location (atloc, atloc, newloc);
612		  break;
613		}
614	      /* FALLTHROUGH */
615
616	    default:
617	      /* ... or not.  */
618	      error_at (atloc, "stray %<@%> in program");
619	      *loc = newloc;
620	      goto retry_after_at;
621	    }
622	  break;
623	}
624
625      /* FALLTHROUGH */
626    case CPP_HASH:
627    case CPP_PASTE:
628      {
629	unsigned char name[8];
630
631	*cpp_spell_token (parse_in, tok, name, true) = 0;
632
633	error_at (*loc, "stray %qs in program", name);
634      }
635
636      goto retry;
637
638    case CPP_OTHER:
639      {
640	cppchar_t c = tok->val.str.text[0];
641
642	if (c == '"' || c == '\'')
643	  error_at (*loc, "missing terminating %c character", (int) c);
644	else if (ISGRAPH (c))
645	  error_at (*loc, "stray %qc in program", (int) c);
646	else
647	  {
648	    rich_location rich_loc (line_table, *loc);
649	    rich_loc.set_escape_on_output (true);
650	    error_at (&rich_loc, "stray %<\\%o%> in program", (int) c);
651	  }
652      }
653      goto retry;
654
655    case CPP_CHAR_USERDEF:
656    case CPP_WCHAR_USERDEF:
657    case CPP_CHAR16_USERDEF:
658    case CPP_CHAR32_USERDEF:
659    case CPP_UTF8CHAR_USERDEF:
660      {
661	tree literal;
662	cpp_token temp_tok = *tok;
663	const char *suffix = cpp_get_userdef_suffix (tok);
664	temp_tok.val.str.len -= strlen (suffix);
665	temp_tok.type = cpp_userdef_char_remove_type (type);
666	literal = build_userdef_literal (get_identifier (suffix),
667					 lex_charconst (&temp_tok),
668					 OT_NONE, NULL_TREE);
669	*value = literal;
670      }
671      break;
672
673    case CPP_CHAR:
674    case CPP_WCHAR:
675    case CPP_CHAR16:
676    case CPP_CHAR32:
677    case CPP_UTF8CHAR:
678      *value = lex_charconst (tok);
679      break;
680
681    case CPP_STRING_USERDEF:
682    case CPP_WSTRING_USERDEF:
683    case CPP_STRING16_USERDEF:
684    case CPP_STRING32_USERDEF:
685    case CPP_UTF8STRING_USERDEF:
686      {
687	tree literal, string;
688	const char *suffix = cpp_get_userdef_suffix (tok);
689	string = build_string (tok->val.str.len - strlen (suffix),
690			       (const char *) tok->val.str.text);
691	literal = build_userdef_literal (get_identifier (suffix),
692					 string, OT_NONE, NULL_TREE);
693	*value = literal;
694      }
695      break;
696
697    case CPP_STRING:
698    case CPP_WSTRING:
699    case CPP_STRING16:
700    case CPP_STRING32:
701    case CPP_UTF8STRING:
702      if ((lex_flags & C_LEX_STRING_NO_JOIN) == 0)
703	{
704	  type = lex_string (tok, value, false,
705			     (lex_flags & C_LEX_STRING_NO_TRANSLATE) == 0);
706	  break;
707	}
708      *value = build_string (tok->val.str.len, (const char *) tok->val.str.text);
709      break;
710
711    case CPP_PRAGMA:
712      *value = build_int_cst (integer_type_node, tok->val.pragma);
713      break;
714
715    case CPP_HEADER_NAME:
716      *value = build_string (tok->val.str.len, (const char *)tok->val.str.text);
717      break;
718
719      /* This token should not be visible outside cpplib.  */
720    case CPP_MACRO_ARG:
721      gcc_unreachable ();
722
723    /* CPP_COMMENT will appear when compiling with -C.  Ignore, except
724       when it is a FALLTHROUGH comment, in that case set
725       PREV_FALLTHROUGH flag on the next non-comment token.  */
726    case CPP_COMMENT:
727      if (tok->flags & PREV_FALLTHROUGH)
728	{
729	  do
730	    {
731	      tok = cpp_get_token_with_location (parse_in, loc);
732	      type = tok->type;
733	    }
734	  while (type == CPP_PADDING || type == CPP_COMMENT);
735	  add_flags |= PREV_FALLTHROUGH;
736	  goto retry_after_at;
737	}
738       goto retry;
739
740    default:
741      *value = NULL_TREE;
742      break;
743    }
744
745  if (cpp_flags)
746    *cpp_flags = tok->flags | add_flags;
747
748  timevar_pop (TV_CPP);
749
750  return type;
751}
752
753/* Returns the narrowest C-visible unsigned type, starting with the
754   minimum specified by FLAGS, that can fit HIGH:LOW, or itk_none if
755   there isn't one.  */
756
757static enum integer_type_kind
758narrowest_unsigned_type (const widest_int &val, unsigned int flags)
759{
760  int itk;
761
762  if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
763    itk = itk_unsigned_int;
764  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
765    itk = itk_unsigned_long;
766  else
767    itk = itk_unsigned_long_long;
768
769  for (; itk < itk_none; itk += 2 /* skip unsigned types */)
770    {
771      tree upper;
772
773      if (integer_types[itk] == NULL_TREE)
774	continue;
775      upper = TYPE_MAX_VALUE (integer_types[itk]);
776
777      if (wi::geu_p (wi::to_widest (upper), val))
778	return (enum integer_type_kind) itk;
779    }
780
781  return itk_none;
782}
783
784/* Ditto, but narrowest signed type.  */
785static enum integer_type_kind
786narrowest_signed_type (const widest_int &val, unsigned int flags)
787{
788  int itk;
789
790  if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
791    itk = itk_int;
792  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
793    itk = itk_long;
794  else
795    itk = itk_long_long;
796
797  for (; itk < itk_none; itk += 2 /* skip signed types */)
798    {
799      tree upper;
800
801      if (integer_types[itk] == NULL_TREE)
802	continue;
803      upper = TYPE_MAX_VALUE (integer_types[itk]);
804
805      if (wi::geu_p (wi::to_widest (upper), val))
806	return (enum integer_type_kind) itk;
807    }
808
809  return itk_none;
810}
811
812/* Interpret TOKEN, an integer with FLAGS as classified by cpplib.  */
813static tree
814interpret_integer (const cpp_token *token, unsigned int flags,
815		   enum overflow_type *overflow)
816{
817  tree value, type;
818  enum integer_type_kind itk;
819  cpp_num integer;
820  HOST_WIDE_INT ival[3];
821
822  *overflow = OT_NONE;
823
824  integer = cpp_interpret_integer (parse_in, token, flags);
825  if (integer.overflow)
826    *overflow = OT_OVERFLOW;
827
828  ival[0] = integer.low;
829  ival[1] = integer.high;
830  ival[2] = 0;
831  widest_int wval = widest_int::from_array (ival, 3);
832
833  /* The type of a constant with a U suffix is straightforward.  */
834  if (flags & CPP_N_UNSIGNED)
835    itk = narrowest_unsigned_type (wval, flags);
836  else
837    {
838      /* The type of a potentially-signed integer constant varies
839	 depending on the base it's in, the standard in use, and the
840	 length suffixes.  */
841      enum integer_type_kind itk_u
842	= narrowest_unsigned_type (wval, flags);
843      enum integer_type_kind itk_s
844	= narrowest_signed_type (wval, flags);
845
846      /* In both C89 and C99, octal and hex constants may be signed or
847	 unsigned, whichever fits tighter.  We do not warn about this
848	 choice differing from the traditional choice, as the constant
849	 is probably a bit pattern and either way will work.  */
850      if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL)
851	itk = MIN (itk_u, itk_s);
852      else
853	{
854	  /* In C99, decimal constants are always signed.
855	     In C89, decimal constants that don't fit in long have
856	     undefined behavior; we try to make them unsigned long.
857	     In GCC's extended C89, that last is true of decimal
858	     constants that don't fit in long long, too.  */
859
860	  itk = itk_s;
861	  if (itk_s > itk_u && itk_s > itk_long)
862	    {
863	      if (!flag_isoc99)
864		{
865		  if (itk_u < itk_unsigned_long)
866		    itk_u = itk_unsigned_long;
867		  itk = itk_u;
868		  warning (0, "this decimal constant is unsigned only in ISO C90");
869		}
870	      else
871		warning (OPT_Wtraditional,
872			 "this decimal constant would be unsigned in ISO C90");
873	    }
874	}
875    }
876
877  if (itk == itk_none)
878    /* cpplib has already issued a warning for overflow.  */
879    type = ((flags & CPP_N_UNSIGNED)
880	    ? widest_unsigned_literal_type_node
881	    : widest_integer_literal_type_node);
882  else if (flags & CPP_N_SIZE_T)
883    {
884      /* itk refers to fundamental types not aliased size types.  */
885      if (flags & CPP_N_UNSIGNED)
886	type = size_type_node;
887      else
888	type = signed_size_type_node;
889    }
890  else
891    {
892      type = integer_types[itk];
893      if (itk > itk_unsigned_long
894	  && (flags & CPP_N_WIDTH) != CPP_N_LARGE)
895	emit_diagnostic
896	  ((c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99)
897	   ? DK_PEDWARN : DK_WARNING,
898	   input_location, OPT_Wlong_long,
899	   (flags & CPP_N_UNSIGNED)
900	   ? "integer constant is too large for %<unsigned long%> type"
901	   : "integer constant is too large for %<long%> type");
902    }
903
904  value = wide_int_to_tree (type, wval);
905
906  /* Convert imaginary to a complex type.  */
907  if (flags & CPP_N_IMAGINARY)
908    value = build_complex (NULL_TREE, build_int_cst (type, 0), value);
909
910  return value;
911}
912
913/* Interpret TOKEN, a floating point number with FLAGS as classified
914   by cpplib.  For C++11 SUFFIX may contain a user-defined literal suffix.  */
915static tree
916interpret_float (const cpp_token *token, unsigned int flags,
917		 const char *suffix, enum overflow_type *overflow)
918{
919  tree type;
920  tree const_type;
921  tree value;
922  REAL_VALUE_TYPE real;
923  REAL_VALUE_TYPE real_trunc;
924  char *copy;
925  size_t copylen;
926
927  *overflow = OT_NONE;
928
929  /* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64
930     pragma has been used and is either double or _Decimal64.  Types
931     that are not allowed with decimal float default to double.  */
932  if (flags & CPP_N_DEFAULT)
933    {
934      flags ^= CPP_N_DEFAULT;
935      flags |= CPP_N_MEDIUM;
936
937      if (((flags & CPP_N_HEX) == 0) && ((flags & CPP_N_IMAGINARY) == 0))
938	{
939	  warning (OPT_Wunsuffixed_float_constants,
940		   "unsuffixed floating constant");
941	  if (float_const_decimal64_p ())
942	    flags |= CPP_N_DFLOAT;
943	}
944    }
945
946  /* Decode _Fract and _Accum.  */
947  if (flags & CPP_N_FRACT || flags & CPP_N_ACCUM)
948    return interpret_fixed (token, flags);
949
950  /* Decode type based on width and properties. */
951  if (flags & CPP_N_DFLOAT)
952    if (!targetm.decimal_float_supported_p ())
953      {
954	error ("decimal floating-point not supported for this target");
955	return error_mark_node;
956      }
957    else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
958      type = dfloat128_type_node;
959    else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
960      type = dfloat32_type_node;
961    else
962      type = dfloat64_type_node;
963  else
964    if (flags & CPP_N_WIDTH_MD)
965      {
966	char suffix;
967	machine_mode mode;
968
969	if ((flags & CPP_N_WIDTH_MD) == CPP_N_MD_W)
970	  suffix = 'w';
971	else
972	  suffix = 'q';
973
974	mode = targetm.c.mode_for_suffix (suffix);
975	if (mode == VOIDmode)
976	  {
977	    error ("unsupported non-standard suffix on floating constant");
978
979	    return error_mark_node;
980	  }
981	else
982	  pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant");
983
984	type = c_common_type_for_mode (mode, 0);
985	gcc_assert (type);
986      }
987    else if ((flags & (CPP_N_FLOATN | CPP_N_FLOATNX)) != 0)
988      {
989	unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
990	bool extended = (flags & CPP_N_FLOATNX) != 0;
991	type = NULL_TREE;
992	for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
993	  if (floatn_nx_types[i].n == (int) n
994	      && floatn_nx_types[i].extended == extended)
995	    {
996	      type = FLOATN_NX_TYPE_NODE (i);
997	      break;
998	    }
999	if (type == NULL_TREE)
1000	  {
1001	    error ("unsupported non-standard suffix on floating constant");
1002	    return error_mark_node;
1003	  }
1004	else
1005	  pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant");
1006      }
1007    else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1008      type = long_double_type_node;
1009    else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
1010	     || flag_single_precision_constant)
1011      type = float_type_node;
1012    else
1013      type = double_type_node;
1014
1015  const_type = excess_precision_type (type);
1016  if (!const_type)
1017    const_type = type;
1018
1019  /* Copy the constant to a nul-terminated buffer.  If the constant
1020     has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF
1021     can't handle them.  */
1022  copylen = token->val.str.len;
1023  if (flags & CPP_N_USERDEF)
1024    copylen -= strlen (suffix);
1025  else if (flags & CPP_N_DFLOAT)
1026    copylen -= 2;
1027  else
1028    {
1029      if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
1030	/* Must be an F or L or machine defined suffix.  */
1031	copylen--;
1032      if (flags & CPP_N_IMAGINARY)
1033	/* I or J suffix.  */
1034	copylen--;
1035      if (flags & CPP_N_FLOATNX)
1036	copylen--;
1037      if (flags & (CPP_N_FLOATN | CPP_N_FLOATNX))
1038	{
1039	  unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
1040	  while (n > 0)
1041	    {
1042	      copylen--;
1043	      n /= 10;
1044	    }
1045	}
1046    }
1047
1048  copy = (char *) alloca (copylen + 1);
1049  if (c_dialect_cxx () ? cxx_dialect > cxx11 : flag_isoc2x)
1050    {
1051      size_t maxlen = 0;
1052      for (size_t i = 0; i < copylen; ++i)
1053        if (token->val.str.text[i] != '\'')
1054          copy[maxlen++] = token->val.str.text[i];
1055      copy[maxlen] = '\0';
1056    }
1057  else
1058    {
1059      memcpy (copy, token->val.str.text, copylen);
1060      copy[copylen] = '\0';
1061    }
1062
1063  real_from_string3 (&real, copy, TYPE_MODE (const_type));
1064  if (const_type != type)
1065    /* Diagnosing if the result of converting the value with excess
1066       precision to the semantic type would overflow (with associated
1067       double rounding) is more appropriate than diagnosing if the
1068       result of converting the string directly to the semantic type
1069       would overflow.  */
1070    real_convert (&real_trunc, TYPE_MODE (type), &real);
1071
1072  /* Both C and C++ require a diagnostic for a floating constant
1073     outside the range of representable values of its type.  Since we
1074     have __builtin_inf* to produce an infinity, this is now a
1075     mandatory pedwarn if the target does not support infinities.  */
1076  if (REAL_VALUE_ISINF (real)
1077      || (const_type != type && REAL_VALUE_ISINF (real_trunc)))
1078    {
1079      *overflow = OT_OVERFLOW;
1080      if (!(flags & CPP_N_USERDEF))
1081	{
1082	  if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
1083	    pedwarn (input_location, 0,
1084		     "floating constant exceeds range of %qT", type);
1085	  else
1086	    warning (OPT_Woverflow,
1087		     "floating constant exceeds range of %qT", type);
1088	}
1089    }
1090  /* We also give a warning if the value underflows.  */
1091  else if (real_equal (&real, &dconst0)
1092	   || (const_type != type
1093	       && real_equal (&real_trunc, &dconst0)))
1094    {
1095      REAL_VALUE_TYPE realvoidmode;
1096      int oflow = real_from_string (&realvoidmode, copy);
1097      *overflow = (oflow == 0 ? OT_NONE
1098			      : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW));
1099      if (!(flags & CPP_N_USERDEF))
1100	{
1101	  if (oflow < 0 || !real_equal (&realvoidmode, &dconst0))
1102	    warning (OPT_Woverflow, "floating constant truncated to zero");
1103	}
1104    }
1105
1106  /* Create a node with determined type and value.  */
1107  value = build_real (const_type, real);
1108  if (flags & CPP_N_IMAGINARY)
1109    {
1110      value = build_complex (NULL_TREE,
1111			     fold_convert (const_type,
1112					   integer_zero_node), value);
1113      if (type != const_type)
1114	{
1115	  const_type = TREE_TYPE (value);
1116	  type = build_complex_type (type);
1117	}
1118    }
1119
1120  if (type != const_type)
1121    value = build1_loc (token->src_loc, EXCESS_PRECISION_EXPR, type, value);
1122
1123  return value;
1124}
1125
1126/* Interpret TOKEN, a fixed-point number with FLAGS as classified
1127   by cpplib.  */
1128
1129static tree
1130interpret_fixed (const cpp_token *token, unsigned int flags)
1131{
1132  tree type;
1133  tree value;
1134  FIXED_VALUE_TYPE fixed;
1135  char *copy;
1136  size_t copylen;
1137
1138  copylen = token->val.str.len;
1139
1140  if (flags & CPP_N_FRACT) /* _Fract.  */
1141    {
1142      if (flags & CPP_N_UNSIGNED) /* Unsigned _Fract.  */
1143	{
1144	  if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1145	    {
1146	      type = unsigned_long_long_fract_type_node;
1147	      copylen -= 4;
1148	    }
1149	  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1150	    {
1151	      type = unsigned_long_fract_type_node;
1152	      copylen -= 3;
1153	    }
1154	  else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1155	    {
1156	      type = unsigned_short_fract_type_node;
1157	      copylen -= 3;
1158	    }
1159          else
1160	    {
1161	      type = unsigned_fract_type_node;
1162	      copylen -= 2;
1163	    }
1164	}
1165      else /* Signed _Fract.  */
1166	{
1167	  if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1168	    {
1169	      type = long_long_fract_type_node;
1170	      copylen -= 3;
1171	    }
1172	  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1173	    {
1174	      type = long_fract_type_node;
1175	      copylen -= 2;
1176	    }
1177	  else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1178	    {
1179	      type = short_fract_type_node;
1180	      copylen -= 2;
1181	    }
1182          else
1183	    {
1184	      type = fract_type_node;
1185	      copylen --;
1186	    }
1187	  }
1188    }
1189  else /* _Accum.  */
1190    {
1191      if (flags & CPP_N_UNSIGNED) /* Unsigned _Accum.  */
1192	{
1193	  if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1194	    {
1195	      type = unsigned_long_long_accum_type_node;
1196	      copylen -= 4;
1197	    }
1198	  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1199	    {
1200	      type = unsigned_long_accum_type_node;
1201	      copylen -= 3;
1202	    }
1203	  else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1204	    {
1205	      type = unsigned_short_accum_type_node;
1206	      copylen -= 3;
1207	     }
1208	  else
1209	    {
1210	      type = unsigned_accum_type_node;
1211	      copylen -= 2;
1212	    }
1213	}
1214      else /* Signed _Accum.  */
1215        {
1216	  if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1217	    {
1218	      type = long_long_accum_type_node;
1219	      copylen -= 3;
1220	    }
1221	  else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1222	    {
1223	      type = long_accum_type_node;
1224	      copylen -= 2;
1225	    }
1226	  else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1227	    {
1228	      type = short_accum_type_node;
1229	      copylen -= 2;
1230	    }
1231	  else
1232	    {
1233	      type = accum_type_node;
1234	      copylen --;
1235	    }
1236	}
1237    }
1238
1239  copy = (char *) alloca (copylen + 1);
1240  memcpy (copy, token->val.str.text, copylen);
1241  copy[copylen] = '\0';
1242
1243  fixed_from_string (&fixed, copy, SCALAR_TYPE_MODE (type));
1244
1245  /* Create a node with determined type and value.  */
1246  value = build_fixed (type, fixed);
1247
1248  return value;
1249}
1250
1251/* Convert a series of STRING, WSTRING, STRING16, STRING32 and/or
1252   UTF8STRING tokens into a tree, performing string constant
1253   concatenation.  TOK is the first of these.  VALP is the location to
1254   write the string into.  OBJC_STRING indicates whether an '@' token
1255   preceded the incoming token (in that case, the strings can either
1256   be ObjC strings, preceded by a single '@', or normal strings, not
1257   preceded by '@'.  The result will be a CPP_OBJC_STRING).  Returns
1258   the CPP token type of the result (CPP_STRING, CPP_WSTRING,
1259   CPP_STRING32, CPP_STRING16, CPP_UTF8STRING, or CPP_OBJC_STRING).
1260
1261   This is unfortunately more work than it should be.  If any of the
1262   strings in the series has an L prefix, the result is a wide string
1263   (6.4.5p4).  Whether or not the result is a wide string affects the
1264   meaning of octal and hexadecimal escapes (6.4.4.4p6,9).  But escape
1265   sequences do not continue across the boundary between two strings in
1266   a series (6.4.5p7), so we must not lose the boundaries.  Therefore
1267   cpp_interpret_string takes a vector of cpp_string structures, which
1268   we must arrange to provide.  */
1269
1270static enum cpp_ttype
1271lex_string (const cpp_token *tok, tree *valp, bool objc_string, bool translate)
1272{
1273  tree value;
1274  size_t concats = 0;
1275  struct obstack str_ob;
1276  struct obstack loc_ob;
1277  cpp_string istr;
1278  enum cpp_ttype type = tok->type;
1279
1280  /* Try to avoid the overhead of creating and destroying an obstack
1281     for the common case of just one string.  */
1282  cpp_string str = tok->val.str;
1283  location_t init_loc = tok->src_loc;
1284  cpp_string *strs = &str;
1285  location_t *locs = NULL;
1286
1287  /* objc_at_sign_was_seen is only used when doing Objective-C string
1288     concatenation.  It is 'true' if we have seen an '@' before the
1289     current string, and 'false' if not.  We must see exactly one or
1290     zero '@' before each string.  */
1291  bool objc_at_sign_was_seen = false;
1292
1293 retry:
1294  tok = cpp_get_token (parse_in);
1295  switch (tok->type)
1296    {
1297    case CPP_PADDING:
1298      goto retry;
1299    case CPP_ATSIGN:
1300      if (objc_string)
1301	{
1302	  if (objc_at_sign_was_seen)
1303	    error ("repeated %<@%> before Objective-C string");
1304
1305	  objc_at_sign_was_seen = true;
1306	  goto retry;
1307	}
1308      /* FALLTHROUGH */
1309
1310    default:
1311      break;
1312
1313    case CPP_WSTRING:
1314    case CPP_STRING16:
1315    case CPP_STRING32:
1316    case CPP_UTF8STRING:
1317      if (type != tok->type)
1318	{
1319	  if (type == CPP_STRING)
1320	    type = tok->type;
1321	  else
1322	    error ("unsupported non-standard concatenation of string literals");
1323	}
1324      /* FALLTHROUGH */
1325
1326    case CPP_STRING:
1327      if (!concats)
1328	{
1329	  gcc_obstack_init (&str_ob);
1330	  gcc_obstack_init (&loc_ob);
1331	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
1332	  obstack_grow (&loc_ob, &init_loc, sizeof (location_t));
1333	}
1334
1335      concats++;
1336      obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string));
1337      obstack_grow (&loc_ob, &tok->src_loc, sizeof (location_t));
1338
1339      if (objc_string)
1340	objc_at_sign_was_seen = false;
1341      goto retry;
1342    }
1343
1344  /* It is an error if we saw a '@' with no following string.  */
1345  if (objc_at_sign_was_seen)
1346    error ("stray %<@%> in program");
1347
1348  /* We have read one more token than we want.  */
1349  _cpp_backup_tokens (parse_in, 1);
1350  if (concats)
1351    {
1352      strs = XOBFINISH (&str_ob, cpp_string *);
1353      locs = XOBFINISH (&loc_ob, location_t *);
1354    }
1355
1356  if (concats && !objc_string && !in_system_header_at (input_location))
1357    warning (OPT_Wtraditional,
1358	     "traditional C rejects string constant concatenation");
1359
1360  if ((translate
1361       ? cpp_interpret_string : cpp_interpret_string_notranslate)
1362      (parse_in, strs, concats + 1, &istr, type))
1363    {
1364      value = build_string (istr.len, (const char *) istr.text);
1365      free (CONST_CAST (unsigned char *, istr.text));
1366      if (concats)
1367	{
1368	  gcc_assert (locs);
1369	  gcc_assert (g_string_concat_db);
1370	  g_string_concat_db->record_string_concatenation (concats + 1, locs);
1371	}
1372    }
1373  else
1374    {
1375      /* Callers cannot generally handle error_mark_node in this context,
1376	 so return the empty string instead.  cpp_interpret_string has
1377	 issued an error.  */
1378      switch (type)
1379	{
1380	default:
1381	case CPP_STRING:
1382	case CPP_UTF8STRING:
1383	  value = build_string (1, "");
1384	  break;
1385	case CPP_STRING16:
1386	  value = build_string (TYPE_PRECISION (char16_type_node)
1387				/ TYPE_PRECISION (char_type_node),
1388				"\0");  /* char16_t is 16 bits */
1389	  break;
1390	case CPP_STRING32:
1391	  value = build_string (TYPE_PRECISION (char32_type_node)
1392				/ TYPE_PRECISION (char_type_node),
1393				"\0\0\0");  /* char32_t is 32 bits */
1394	  break;
1395	case CPP_WSTRING:
1396	  value = build_string (TYPE_PRECISION (wchar_type_node)
1397				/ TYPE_PRECISION (char_type_node),
1398				"\0\0\0");  /* widest supported wchar_t
1399					       is 32 bits */
1400	  break;
1401        }
1402    }
1403
1404  switch (type)
1405    {
1406    default:
1407    case CPP_STRING:
1408      TREE_TYPE (value) = char_array_type_node;
1409      break;
1410    case CPP_UTF8STRING:
1411      if (flag_char8_t)
1412        TREE_TYPE (value) = char8_array_type_node;
1413      else
1414        TREE_TYPE (value) = char_array_type_node;
1415      break;
1416    case CPP_STRING16:
1417      TREE_TYPE (value) = char16_array_type_node;
1418      break;
1419    case CPP_STRING32:
1420      TREE_TYPE (value) = char32_array_type_node;
1421      break;
1422    case CPP_WSTRING:
1423      TREE_TYPE (value) = wchar_array_type_node;
1424    }
1425  *valp = fix_string_type (value);
1426
1427  if (concats)
1428    {
1429      obstack_free (&str_ob, 0);
1430      obstack_free (&loc_ob, 0);
1431    }
1432
1433  return objc_string ? CPP_OBJC_STRING : type;
1434}
1435
1436/* Converts a (possibly wide) character constant token into a tree.  */
1437static tree
1438lex_charconst (const cpp_token *token)
1439{
1440  cppchar_t result;
1441  tree type, value;
1442  unsigned int chars_seen;
1443  int unsignedp = 0;
1444
1445  result = cpp_interpret_charconst (parse_in, token,
1446				    &chars_seen, &unsignedp);
1447
1448  if (token->type == CPP_WCHAR)
1449    type = wchar_type_node;
1450  else if (token->type == CPP_CHAR32)
1451    type = char32_type_node;
1452  else if (token->type == CPP_CHAR16)
1453    type = char16_type_node;
1454  else if (token->type == CPP_UTF8CHAR)
1455    {
1456      if (!c_dialect_cxx ())
1457	type = unsigned_char_type_node;
1458      else if (flag_char8_t)
1459        type = char8_type_node;
1460      else
1461        type = char_type_node;
1462    }
1463  /* In C, a character constant has type 'int'.
1464     In C++ 'char', but multi-char charconsts have type 'int'.  */
1465  else if (!c_dialect_cxx () || chars_seen > 1)
1466    type = integer_type_node;
1467  else
1468    type = char_type_node;
1469
1470  /* Cast to cppchar_signed_t to get correct sign-extension of RESULT
1471     before possibly widening to HOST_WIDE_INT for build_int_cst.  */
1472  if (unsignedp || (cppchar_signed_t) result >= 0)
1473    value = build_int_cst (type, result);
1474  else
1475    value = build_int_cst (type, (cppchar_signed_t) result);
1476
1477  return value;
1478}
1479
1480/* Helper function for c_parser_peek_conflict_marker
1481   and cp_lexer_peek_conflict_marker.
1482   Given a possible conflict marker token of kind TOK1_KIND
1483   consisting of a pair of characters, get the token kind for the
1484   standalone final character.  */
1485
1486enum cpp_ttype
1487conflict_marker_get_final_tok_kind (enum cpp_ttype tok1_kind)
1488{
1489  switch (tok1_kind)
1490    {
1491    default: gcc_unreachable ();
1492    case CPP_LSHIFT:
1493      /* "<<" and '<' */
1494      return CPP_LESS;
1495
1496    case CPP_EQ_EQ:
1497      /* "==" and '=' */
1498      return CPP_EQ;
1499
1500    case CPP_RSHIFT:
1501      /* ">>" and '>' */
1502      return CPP_GREATER;
1503    }
1504}
1505