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