c-lex.c revision 90075
118334Speter/* Lexical analyzer for C and Objective C. 290075Sobrien Copyright (C) 1987, 1988, 1989, 1992, 1994, 1995, 1996, 1997 390075Sobrien 1998, 1999, 2000 Free Software Foundation, Inc. 418334Speter 590075SobrienThis file is part of GCC. 618334Speter 790075SobrienGCC is free software; you can redistribute it and/or modify it under 890075Sobrienthe terms of the GNU General Public License as published by the Free 990075SobrienSoftware Foundation; either version 2, or (at your option) any later 1090075Sobrienversion. 1118334Speter 1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY 1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or 1490075SobrienFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1590075Sobrienfor more details. 1618334Speter 1718334SpeterYou should have received a copy of the GNU General Public License 1890075Sobrienalong with GCC; see the file COPYING. If not, write to the Free 1990075SobrienSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 2090075Sobrien02111-1307, USA. */ 2118334Speter 2250397Sobrien#include "config.h" 2350397Sobrien#include "system.h" 2418334Speter 2518334Speter#include "rtl.h" 2618334Speter#include "tree.h" 2790075Sobrien#include "expr.h" 2818334Speter#include "input.h" 2950397Sobrien#include "output.h" 3018334Speter#include "c-lex.h" 3118334Speter#include "c-tree.h" 3218334Speter#include "flags.h" 3390075Sobrien#include "timevar.h" 3490075Sobrien#include "cpplib.h" 3518334Speter#include "c-pragma.h" 3650397Sobrien#include "toplev.h" 3752284Sobrien#include "intl.h" 3890075Sobrien#include "tm_p.h" 3990075Sobrien#include "splay-tree.h" 4090075Sobrien#include "debug.h" 4118334Speter 4250397Sobrien/* MULTIBYTE_CHARS support only works for native compilers. 4350397Sobrien ??? Ideally what we want is to model widechar support after 4450397Sobrien the current floating point support. */ 4550397Sobrien#ifdef CROSS_COMPILE 4650397Sobrien#undef MULTIBYTE_CHARS 4750397Sobrien#endif 4818334Speter 4918334Speter#ifdef MULTIBYTE_CHARS 5052284Sobrien#include "mbchar.h" 5118334Speter#include <locale.h> 5252284Sobrien#endif /* MULTIBYTE_CHARS */ 5390075Sobrien#ifndef GET_ENVIRONMENT 5490075Sobrien#define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ((ENV_VALUE) = getenv (ENV_NAME)) 5518334Speter#endif 5618334Speter 5790075Sobrien/* The current line map. */ 5890075Sobrienstatic const struct line_map *map; 5950397Sobrien 6090075Sobrien/* The line used to refresh the lineno global variable after each token. */ 6190075Sobrienstatic unsigned int src_lineno; 6218334Speter 6390075Sobrien/* We may keep statistics about how long which files took to compile. */ 6490075Sobrienstatic int header_time, body_time; 6590075Sobrienstatic splay_tree file_info_tree; 6690075Sobrien 6718334Speter/* Cause the `yydebug' variable to be defined. */ 6818334Speter#define YYDEBUG 1 6918334Speter 7018334Speter/* File used for outputting assembler code. */ 7118334Speterextern FILE *asm_out_file; 7218334Speter 7390075Sobrien#undef WCHAR_TYPE_SIZE 7490075Sobrien#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node) 7518334Speter 7618334Speter/* Number of bytes in a wide character. */ 7718334Speter#define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT) 7818334Speter 7990075Sobrienint indent_level; /* Number of { minus number of }. */ 8090075Sobrienint pending_lang_change; /* If we need to switch languages - C++ only */ 8190075Sobrienint c_header_level; /* depth in C headers - C++ only */ 8218334Speter 8390075Sobrien/* Nonzero tells yylex to ignore \ in string constants. */ 8490075Sobrienstatic int ignore_escape_flag; 8550397Sobrien 8690075Sobrienstatic void parse_float PARAMS ((PTR)); 8790075Sobrienstatic tree lex_number PARAMS ((const char *, unsigned int)); 8890075Sobrienstatic tree lex_string PARAMS ((const char *, unsigned int, int)); 8990075Sobrienstatic tree lex_charconst PARAMS ((const cpp_token *)); 9090075Sobrienstatic void update_header_times PARAMS ((const char *)); 9190075Sobrienstatic int dump_one_header PARAMS ((splay_tree_node, void *)); 9290075Sobrienstatic void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int)); 9390075Sobrienstatic void cb_ident PARAMS ((cpp_reader *, unsigned int, 9490075Sobrien const cpp_string *)); 9590075Sobrienstatic void cb_file_change PARAMS ((cpp_reader *, const struct line_map *)); 9690075Sobrienstatic void cb_def_pragma PARAMS ((cpp_reader *, unsigned int)); 9790075Sobrienstatic void cb_define PARAMS ((cpp_reader *, unsigned int, 9890075Sobrien cpp_hashnode *)); 9990075Sobrienstatic void cb_undef PARAMS ((cpp_reader *, unsigned int, 10090075Sobrien cpp_hashnode *)); 10118334Speter 10290075Sobrienconst char * 10390075Sobrieninit_c_lex (filename) 10490075Sobrien const char *filename; 10518334Speter{ 10690075Sobrien struct cpp_callbacks *cb; 10790075Sobrien struct c_fileinfo *toplevel; 10818334Speter 10990075Sobrien /* Set up filename timing. Must happen before cpp_read_main_file. */ 11090075Sobrien file_info_tree = splay_tree_new ((splay_tree_compare_fn)strcmp, 11190075Sobrien 0, 11290075Sobrien (splay_tree_delete_value_fn)free); 11390075Sobrien toplevel = get_fileinfo ("<top level>"); 11490075Sobrien if (flag_detailed_statistics) 11550397Sobrien { 11690075Sobrien header_time = 0; 11790075Sobrien body_time = get_run_time (); 11890075Sobrien toplevel->time = body_time; 11950397Sobrien } 12090075Sobrien 12118334Speter#ifdef MULTIBYTE_CHARS 12218334Speter /* Change to the native locale for multibyte conversions. */ 12318334Speter setlocale (LC_CTYPE, ""); 12490075Sobrien GET_ENVIRONMENT (literal_codeset, "LANG"); 12518334Speter#endif 12618334Speter 12790075Sobrien cb = cpp_get_callbacks (parse_in); 12818334Speter 12990075Sobrien cb->line_change = cb_line_change; 13090075Sobrien cb->ident = cb_ident; 13190075Sobrien cb->file_change = cb_file_change; 13290075Sobrien cb->def_pragma = cb_def_pragma; 13318334Speter 13490075Sobrien /* Set the debug callbacks if we can use them. */ 13590075Sobrien if (debug_info_level == DINFO_LEVEL_VERBOSE 13690075Sobrien && (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG 13790075Sobrien || write_symbols == VMS_AND_DWARF2_DEBUG)) 13818334Speter { 13990075Sobrien cb->define = cb_define; 14090075Sobrien cb->undef = cb_undef; 14118334Speter } 14252284Sobrien 14390075Sobrien /* Start it at 0. */ 14490075Sobrien lineno = 0; 14590075Sobrien 14690075Sobrien if (filename == NULL || !strcmp (filename, "-")) 14790075Sobrien filename = ""; 14890075Sobrien 14990075Sobrien return cpp_read_main_file (parse_in, filename, ident_hash); 15018334Speter} 15118334Speter 15290075Sobrien/* A thin wrapper around the real parser that initializes the 15390075Sobrien integrated preprocessor after debug output has been initialized. 15490075Sobrien Also, make sure the start_source_file debug hook gets called for 15590075Sobrien the primary source file. */ 15690075Sobrien 15790075Sobrienint 15890075Sobrienyyparse() 15918334Speter{ 16090075Sobrien (*debug_hooks->start_source_file) (lineno, input_filename); 16190075Sobrien cpp_finish_options (parse_in); 16290075Sobrien 16390075Sobrien return yyparse_1(); 16418334Speter} 16518334Speter 16690075Sobrienstruct c_fileinfo * 16790075Sobrienget_fileinfo (name) 16890075Sobrien const char *name; 16918334Speter{ 17090075Sobrien splay_tree_node n; 17190075Sobrien struct c_fileinfo *fi; 17218334Speter 17390075Sobrien n = splay_tree_lookup (file_info_tree, (splay_tree_key) name); 17490075Sobrien if (n) 17590075Sobrien return (struct c_fileinfo *) n->value; 17690075Sobrien 17790075Sobrien fi = (struct c_fileinfo *) xmalloc (sizeof (struct c_fileinfo)); 17890075Sobrien fi->time = 0; 17990075Sobrien fi->interface_only = 0; 18090075Sobrien fi->interface_unknown = 1; 18190075Sobrien splay_tree_insert (file_info_tree, (splay_tree_key) name, 18290075Sobrien (splay_tree_value) fi); 18390075Sobrien return fi; 18418334Speter} 18590075Sobrien 18690075Sobrienstatic void 18790075Sobrienupdate_header_times (name) 18890075Sobrien const char *name; 18950397Sobrien{ 19090075Sobrien /* Changing files again. This means currently collected time 19190075Sobrien is charged against header time, and body time starts back at 0. */ 19290075Sobrien if (flag_detailed_statistics) 19350397Sobrien { 19490075Sobrien int this_time = get_run_time (); 19590075Sobrien struct c_fileinfo *file = get_fileinfo (name); 19690075Sobrien header_time += this_time - body_time; 19790075Sobrien file->time += this_time - body_time; 19890075Sobrien body_time = this_time; 19950397Sobrien } 20050397Sobrien} 20150397Sobrien 20218334Speterstatic int 20390075Sobriendump_one_header (n, dummy) 20490075Sobrien splay_tree_node n; 20590075Sobrien void *dummy ATTRIBUTE_UNUSED; 20618334Speter{ 20790075Sobrien print_time ((const char *) n->key, 20890075Sobrien ((struct c_fileinfo *) n->value)->time); 20990075Sobrien return 0; 21018334Speter} 21118334Speter 21218334Spetervoid 21390075Sobriendump_time_statistics () 21418334Speter{ 21590075Sobrien struct c_fileinfo *file = get_fileinfo (input_filename); 21690075Sobrien int this_time = get_run_time (); 21790075Sobrien file->time += this_time - body_time; 21818334Speter 21990075Sobrien fprintf (stderr, "\n******\n"); 22090075Sobrien print_time ("header files (total)", header_time); 22190075Sobrien print_time ("main file (total)", this_time - body_time); 22290075Sobrien fprintf (stderr, "ratio = %g : 1\n", 22390075Sobrien (double)header_time / (double)(this_time - body_time)); 22490075Sobrien fprintf (stderr, "\n******\n"); 22518334Speter 22690075Sobrien splay_tree_foreach (file_info_tree, dump_one_header, 0); 22718334Speter} 22818334Speter 22990075Sobrien/* Not yet handled: #pragma, #define, #undef. 23090075Sobrien No need to deal with linemarkers under normal conditions. */ 23190075Sobrien 23290075Sobrienstatic void 23390075Sobriencb_ident (pfile, line, str) 23490075Sobrien cpp_reader *pfile ATTRIBUTE_UNUSED; 23590075Sobrien unsigned int line ATTRIBUTE_UNUSED; 23690075Sobrien const cpp_string *str ATTRIBUTE_UNUSED; 23750397Sobrien{ 23890075Sobrien#ifdef ASM_OUTPUT_IDENT 23990075Sobrien if (! flag_no_ident) 24050397Sobrien { 24190075Sobrien /* Convert escapes in the string. */ 24290075Sobrien tree value = lex_string ((const char *)str->text, str->len, 0); 24390075Sobrien ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (value)); 24450397Sobrien } 24590075Sobrien#endif 24650397Sobrien} 24750397Sobrien 24890075Sobrien/* Called at the start of every non-empty line. TOKEN is the first 24990075Sobrien lexed token on the line. Used for diagnostic line numbers. */ 25090075Sobrienstatic void 25190075Sobriencb_line_change (pfile, token, parsing_args) 25290075Sobrien cpp_reader *pfile ATTRIBUTE_UNUSED; 25390075Sobrien const cpp_token *token; 25490075Sobrien int parsing_args ATTRIBUTE_UNUSED; 25518334Speter{ 25690075Sobrien src_lineno = SOURCE_LINE (map, token->line); 25718334Speter} 25852284Sobrien 25952284Sobrienstatic void 26090075Sobriencb_file_change (pfile, new_map) 26190075Sobrien cpp_reader *pfile ATTRIBUTE_UNUSED; 26290075Sobrien const struct line_map *new_map; 26352284Sobrien{ 26490075Sobrien unsigned int to_line = SOURCE_LINE (new_map, new_map->to_line); 26552284Sobrien 26690075Sobrien if (new_map->reason == LC_ENTER) 26718334Speter { 26890075Sobrien /* Don't stack the main buffer on the input stack; 26990075Sobrien we already did in compile_file. */ 27090075Sobrien if (map == NULL) 27190075Sobrien main_input_filename = new_map->to_file; 27290075Sobrien else 27318334Speter { 27490075Sobrien lineno = SOURCE_LINE (new_map - 1, new_map->from_line - 1); 27590075Sobrien push_srcloc (new_map->to_file, 1); 27690075Sobrien input_file_stack->indent_level = indent_level; 27790075Sobrien (*debug_hooks->start_source_file) (lineno, new_map->to_file); 27890075Sobrien#ifndef NO_IMPLICIT_EXTERN_C 27990075Sobrien if (c_header_level) 28090075Sobrien ++c_header_level; 28190075Sobrien else if (new_map->sysp == 2) 28218334Speter { 28390075Sobrien c_header_level = 1; 28490075Sobrien ++pending_lang_change; 28518334Speter } 28690075Sobrien#endif 28718334Speter } 28890075Sobrien } 28990075Sobrien else if (new_map->reason == LC_LEAVE) 29090075Sobrien { 29190075Sobrien#ifndef NO_IMPLICIT_EXTERN_C 29290075Sobrien if (c_header_level && --c_header_level == 0) 29318334Speter { 29490075Sobrien if (new_map->sysp == 2) 29590075Sobrien warning ("badly nested C headers from preprocessor"); 29690075Sobrien --pending_lang_change; 29718334Speter } 29890075Sobrien#endif 29990075Sobrien#if 0 30090075Sobrien if (indent_level != input_file_stack->indent_level) 30118334Speter { 30290075Sobrien warning_with_file_and_line 30390075Sobrien (input_filename, lineno, 30490075Sobrien "this file contains more '%c's than '%c's", 30590075Sobrien indent_level > input_file_stack->indent_level ? '{' : '}', 30690075Sobrien indent_level > input_file_stack->indent_level ? '}' : '{'); 30718334Speter } 30890075Sobrien#endif 30990075Sobrien pop_srcloc (); 31090075Sobrien 31190075Sobrien (*debug_hooks->end_source_file) (to_line); 31290075Sobrien } 31318334Speter 31490075Sobrien update_header_times (new_map->to_file); 31590075Sobrien in_system_header = new_map->sysp != 0; 31690075Sobrien input_filename = new_map->to_file; 31790075Sobrien lineno = to_line; 31890075Sobrien map = new_map; 31918334Speter 32090075Sobrien /* Hook for C++. */ 32190075Sobrien extract_interface_info (); 32290075Sobrien} 32318334Speter 32490075Sobrienstatic void 32590075Sobriencb_def_pragma (pfile, line) 32690075Sobrien cpp_reader *pfile; 32790075Sobrien unsigned int line; 32890075Sobrien{ 32990075Sobrien /* Issue a warning message if we have been asked to do so. Ignore 33090075Sobrien unknown pragmas in system headers unless an explicit 33190075Sobrien -Wunknown-pragmas has been given. */ 33290075Sobrien if (warn_unknown_pragmas > in_system_header) 33390075Sobrien { 33490075Sobrien const unsigned char *space, *name = 0; 33590075Sobrien const cpp_token *s; 33618334Speter 33790075Sobrien s = cpp_get_token (pfile); 33890075Sobrien space = cpp_token_as_text (pfile, s); 33990075Sobrien s = cpp_get_token (pfile); 34090075Sobrien if (s->type == CPP_NAME) 34190075Sobrien name = cpp_token_as_text (pfile, s); 34218334Speter 34390075Sobrien lineno = SOURCE_LINE (map, line); 34490075Sobrien if (name) 34590075Sobrien warning ("ignoring #pragma %s %s", space, name); 34690075Sobrien else 34790075Sobrien warning ("ignoring #pragma %s", space); 34818334Speter } 34990075Sobrien} 35018334Speter 35190075Sobrien/* #define callback for DWARF and DWARF2 debug info. */ 35290075Sobrienstatic void 35390075Sobriencb_define (pfile, line, node) 35490075Sobrien cpp_reader *pfile; 35590075Sobrien unsigned int line; 35690075Sobrien cpp_hashnode *node; 35790075Sobrien{ 35890075Sobrien (*debug_hooks->define) (SOURCE_LINE (map, line), 35990075Sobrien (const char *) cpp_macro_definition (pfile, node)); 36090075Sobrien} 36118334Speter 36290075Sobrien/* #undef callback for DWARF and DWARF2 debug info. */ 36390075Sobrienstatic void 36490075Sobriencb_undef (pfile, line, node) 36590075Sobrien cpp_reader *pfile ATTRIBUTE_UNUSED; 36690075Sobrien unsigned int line; 36790075Sobrien cpp_hashnode *node; 36890075Sobrien{ 36990075Sobrien (*debug_hooks->undef) (SOURCE_LINE (map, line), 37090075Sobrien (const char *) NODE_NAME (node)); 37190075Sobrien} 37218334Speter 37390075Sobrien#if 0 /* not yet */ 37490075Sobrien/* Returns nonzero if C is a universal-character-name. Give an error if it 37590075Sobrien is not one which may appear in an identifier, as per [extendid]. 37618334Speter 37790075Sobrien Note that extended character support in identifiers has not yet been 37890075Sobrien implemented. It is my personal opinion that this is not a desirable 37990075Sobrien feature. Portable code cannot count on support for more than the basic 38090075Sobrien identifier character set. */ 38118334Speter 38290075Sobrienstatic inline int 38390075Sobrienis_extended_char (c) 38490075Sobrien int c; 38590075Sobrien{ 38690075Sobrien#ifdef TARGET_EBCDIC 38790075Sobrien return 0; 38890075Sobrien#else 38990075Sobrien /* ASCII. */ 39090075Sobrien if (c < 0x7f) 39190075Sobrien return 0; 39218334Speter 39390075Sobrien /* None of the valid chars are outside the Basic Multilingual Plane (the 39490075Sobrien low 16 bits). */ 39590075Sobrien if (c > 0xffff) 39618334Speter { 39790075Sobrien error ("universal-character-name '\\U%08x' not valid in identifier", c); 39890075Sobrien return 1; 39990075Sobrien } 40090075Sobrien 40190075Sobrien /* Latin */ 40290075Sobrien if ((c >= 0x00c0 && c <= 0x00d6) 40390075Sobrien || (c >= 0x00d8 && c <= 0x00f6) 40490075Sobrien || (c >= 0x00f8 && c <= 0x01f5) 40590075Sobrien || (c >= 0x01fa && c <= 0x0217) 40690075Sobrien || (c >= 0x0250 && c <= 0x02a8) 40790075Sobrien || (c >= 0x1e00 && c <= 0x1e9a) 40890075Sobrien || (c >= 0x1ea0 && c <= 0x1ef9)) 40990075Sobrien return 1; 41018334Speter 41190075Sobrien /* Greek */ 41290075Sobrien if ((c == 0x0384) 41390075Sobrien || (c >= 0x0388 && c <= 0x038a) 41490075Sobrien || (c == 0x038c) 41590075Sobrien || (c >= 0x038e && c <= 0x03a1) 41690075Sobrien || (c >= 0x03a3 && c <= 0x03ce) 41790075Sobrien || (c >= 0x03d0 && c <= 0x03d6) 41890075Sobrien || (c == 0x03da) 41990075Sobrien || (c == 0x03dc) 42090075Sobrien || (c == 0x03de) 42190075Sobrien || (c == 0x03e0) 42290075Sobrien || (c >= 0x03e2 && c <= 0x03f3) 42390075Sobrien || (c >= 0x1f00 && c <= 0x1f15) 42490075Sobrien || (c >= 0x1f18 && c <= 0x1f1d) 42590075Sobrien || (c >= 0x1f20 && c <= 0x1f45) 42690075Sobrien || (c >= 0x1f48 && c <= 0x1f4d) 42790075Sobrien || (c >= 0x1f50 && c <= 0x1f57) 42890075Sobrien || (c == 0x1f59) 42990075Sobrien || (c == 0x1f5b) 43090075Sobrien || (c == 0x1f5d) 43190075Sobrien || (c >= 0x1f5f && c <= 0x1f7d) 43290075Sobrien || (c >= 0x1f80 && c <= 0x1fb4) 43390075Sobrien || (c >= 0x1fb6 && c <= 0x1fbc) 43490075Sobrien || (c >= 0x1fc2 && c <= 0x1fc4) 43590075Sobrien || (c >= 0x1fc6 && c <= 0x1fcc) 43690075Sobrien || (c >= 0x1fd0 && c <= 0x1fd3) 43790075Sobrien || (c >= 0x1fd6 && c <= 0x1fdb) 43890075Sobrien || (c >= 0x1fe0 && c <= 0x1fec) 43990075Sobrien || (c >= 0x1ff2 && c <= 0x1ff4) 44090075Sobrien || (c >= 0x1ff6 && c <= 0x1ffc)) 44190075Sobrien return 1; 44218334Speter 44390075Sobrien /* Cyrillic */ 44490075Sobrien if ((c >= 0x0401 && c <= 0x040d) 44590075Sobrien || (c >= 0x040f && c <= 0x044f) 44690075Sobrien || (c >= 0x0451 && c <= 0x045c) 44790075Sobrien || (c >= 0x045e && c <= 0x0481) 44890075Sobrien || (c >= 0x0490 && c <= 0x04c4) 44990075Sobrien || (c >= 0x04c7 && c <= 0x04c8) 45090075Sobrien || (c >= 0x04cb && c <= 0x04cc) 45190075Sobrien || (c >= 0x04d0 && c <= 0x04eb) 45290075Sobrien || (c >= 0x04ee && c <= 0x04f5) 45390075Sobrien || (c >= 0x04f8 && c <= 0x04f9)) 45490075Sobrien return 1; 45518334Speter 45690075Sobrien /* Armenian */ 45790075Sobrien if ((c >= 0x0531 && c <= 0x0556) 45890075Sobrien || (c >= 0x0561 && c <= 0x0587)) 45990075Sobrien return 1; 46018334Speter 46190075Sobrien /* Hebrew */ 46290075Sobrien if ((c >= 0x05d0 && c <= 0x05ea) 46390075Sobrien || (c >= 0x05f0 && c <= 0x05f4)) 46490075Sobrien return 1; 46518334Speter 46690075Sobrien /* Arabic */ 46790075Sobrien if ((c >= 0x0621 && c <= 0x063a) 46890075Sobrien || (c >= 0x0640 && c <= 0x0652) 46990075Sobrien || (c >= 0x0670 && c <= 0x06b7) 47090075Sobrien || (c >= 0x06ba && c <= 0x06be) 47190075Sobrien || (c >= 0x06c0 && c <= 0x06ce) 47290075Sobrien || (c >= 0x06e5 && c <= 0x06e7)) 47390075Sobrien return 1; 47418334Speter 47590075Sobrien /* Devanagari */ 47690075Sobrien if ((c >= 0x0905 && c <= 0x0939) 47790075Sobrien || (c >= 0x0958 && c <= 0x0962)) 47890075Sobrien return 1; 47918334Speter 48090075Sobrien /* Bengali */ 48190075Sobrien if ((c >= 0x0985 && c <= 0x098c) 48290075Sobrien || (c >= 0x098f && c <= 0x0990) 48390075Sobrien || (c >= 0x0993 && c <= 0x09a8) 48490075Sobrien || (c >= 0x09aa && c <= 0x09b0) 48590075Sobrien || (c == 0x09b2) 48690075Sobrien || (c >= 0x09b6 && c <= 0x09b9) 48790075Sobrien || (c >= 0x09dc && c <= 0x09dd) 48890075Sobrien || (c >= 0x09df && c <= 0x09e1) 48990075Sobrien || (c >= 0x09f0 && c <= 0x09f1)) 49090075Sobrien return 1; 49118334Speter 49290075Sobrien /* Gurmukhi */ 49390075Sobrien if ((c >= 0x0a05 && c <= 0x0a0a) 49490075Sobrien || (c >= 0x0a0f && c <= 0x0a10) 49590075Sobrien || (c >= 0x0a13 && c <= 0x0a28) 49690075Sobrien || (c >= 0x0a2a && c <= 0x0a30) 49790075Sobrien || (c >= 0x0a32 && c <= 0x0a33) 49890075Sobrien || (c >= 0x0a35 && c <= 0x0a36) 49990075Sobrien || (c >= 0x0a38 && c <= 0x0a39) 50090075Sobrien || (c >= 0x0a59 && c <= 0x0a5c) 50190075Sobrien || (c == 0x0a5e)) 50290075Sobrien return 1; 50318334Speter 50490075Sobrien /* Gujarati */ 50590075Sobrien if ((c >= 0x0a85 && c <= 0x0a8b) 50690075Sobrien || (c == 0x0a8d) 50790075Sobrien || (c >= 0x0a8f && c <= 0x0a91) 50890075Sobrien || (c >= 0x0a93 && c <= 0x0aa8) 50990075Sobrien || (c >= 0x0aaa && c <= 0x0ab0) 51090075Sobrien || (c >= 0x0ab2 && c <= 0x0ab3) 51190075Sobrien || (c >= 0x0ab5 && c <= 0x0ab9) 51290075Sobrien || (c == 0x0ae0)) 51390075Sobrien return 1; 51418334Speter 51590075Sobrien /* Oriya */ 51690075Sobrien if ((c >= 0x0b05 && c <= 0x0b0c) 51790075Sobrien || (c >= 0x0b0f && c <= 0x0b10) 51890075Sobrien || (c >= 0x0b13 && c <= 0x0b28) 51990075Sobrien || (c >= 0x0b2a && c <= 0x0b30) 52090075Sobrien || (c >= 0x0b32 && c <= 0x0b33) 52190075Sobrien || (c >= 0x0b36 && c <= 0x0b39) 52290075Sobrien || (c >= 0x0b5c && c <= 0x0b5d) 52390075Sobrien || (c >= 0x0b5f && c <= 0x0b61)) 52490075Sobrien return 1; 52518334Speter 52690075Sobrien /* Tamil */ 52790075Sobrien if ((c >= 0x0b85 && c <= 0x0b8a) 52890075Sobrien || (c >= 0x0b8e && c <= 0x0b90) 52990075Sobrien || (c >= 0x0b92 && c <= 0x0b95) 53090075Sobrien || (c >= 0x0b99 && c <= 0x0b9a) 53190075Sobrien || (c == 0x0b9c) 53290075Sobrien || (c >= 0x0b9e && c <= 0x0b9f) 53390075Sobrien || (c >= 0x0ba3 && c <= 0x0ba4) 53490075Sobrien || (c >= 0x0ba8 && c <= 0x0baa) 53590075Sobrien || (c >= 0x0bae && c <= 0x0bb5) 53690075Sobrien || (c >= 0x0bb7 && c <= 0x0bb9)) 53790075Sobrien return 1; 53818334Speter 53990075Sobrien /* Telugu */ 54090075Sobrien if ((c >= 0x0c05 && c <= 0x0c0c) 54190075Sobrien || (c >= 0x0c0e && c <= 0x0c10) 54290075Sobrien || (c >= 0x0c12 && c <= 0x0c28) 54390075Sobrien || (c >= 0x0c2a && c <= 0x0c33) 54490075Sobrien || (c >= 0x0c35 && c <= 0x0c39) 54590075Sobrien || (c >= 0x0c60 && c <= 0x0c61)) 54690075Sobrien return 1; 54718334Speter 54890075Sobrien /* Kannada */ 54990075Sobrien if ((c >= 0x0c85 && c <= 0x0c8c) 55090075Sobrien || (c >= 0x0c8e && c <= 0x0c90) 55190075Sobrien || (c >= 0x0c92 && c <= 0x0ca8) 55290075Sobrien || (c >= 0x0caa && c <= 0x0cb3) 55390075Sobrien || (c >= 0x0cb5 && c <= 0x0cb9) 55490075Sobrien || (c >= 0x0ce0 && c <= 0x0ce1)) 55590075Sobrien return 1; 55618334Speter 55790075Sobrien /* Malayalam */ 55890075Sobrien if ((c >= 0x0d05 && c <= 0x0d0c) 55990075Sobrien || (c >= 0x0d0e && c <= 0x0d10) 56090075Sobrien || (c >= 0x0d12 && c <= 0x0d28) 56190075Sobrien || (c >= 0x0d2a && c <= 0x0d39) 56290075Sobrien || (c >= 0x0d60 && c <= 0x0d61)) 56390075Sobrien return 1; 56418334Speter 56590075Sobrien /* Thai */ 56690075Sobrien if ((c >= 0x0e01 && c <= 0x0e30) 56790075Sobrien || (c >= 0x0e32 && c <= 0x0e33) 56890075Sobrien || (c >= 0x0e40 && c <= 0x0e46) 56990075Sobrien || (c >= 0x0e4f && c <= 0x0e5b)) 57090075Sobrien return 1; 57118334Speter 57290075Sobrien /* Lao */ 57390075Sobrien if ((c >= 0x0e81 && c <= 0x0e82) 57490075Sobrien || (c == 0x0e84) 57590075Sobrien || (c == 0x0e87) 57690075Sobrien || (c == 0x0e88) 57790075Sobrien || (c == 0x0e8a) 57890075Sobrien || (c == 0x0e0d) 57990075Sobrien || (c >= 0x0e94 && c <= 0x0e97) 58090075Sobrien || (c >= 0x0e99 && c <= 0x0e9f) 58190075Sobrien || (c >= 0x0ea1 && c <= 0x0ea3) 58290075Sobrien || (c == 0x0ea5) 58390075Sobrien || (c == 0x0ea7) 58490075Sobrien || (c == 0x0eaa) 58590075Sobrien || (c == 0x0eab) 58690075Sobrien || (c >= 0x0ead && c <= 0x0eb0) 58790075Sobrien || (c == 0x0eb2) 58890075Sobrien || (c == 0x0eb3) 58990075Sobrien || (c == 0x0ebd) 59090075Sobrien || (c >= 0x0ec0 && c <= 0x0ec4) 59190075Sobrien || (c == 0x0ec6)) 59290075Sobrien return 1; 59318334Speter 59490075Sobrien /* Georgian */ 59590075Sobrien if ((c >= 0x10a0 && c <= 0x10c5) 59690075Sobrien || (c >= 0x10d0 && c <= 0x10f6)) 59790075Sobrien return 1; 59818334Speter 59990075Sobrien /* Hiragana */ 60090075Sobrien if ((c >= 0x3041 && c <= 0x3094) 60190075Sobrien || (c >= 0x309b && c <= 0x309e)) 60290075Sobrien return 1; 60318334Speter 60490075Sobrien /* Katakana */ 60590075Sobrien if ((c >= 0x30a1 && c <= 0x30fe)) 60690075Sobrien return 1; 60718334Speter 60890075Sobrien /* Bopmofo */ 60990075Sobrien if ((c >= 0x3105 && c <= 0x312c)) 61090075Sobrien return 1; 61118334Speter 61290075Sobrien /* Hangul */ 61390075Sobrien if ((c >= 0x1100 && c <= 0x1159) 61490075Sobrien || (c >= 0x1161 && c <= 0x11a2) 61590075Sobrien || (c >= 0x11a8 && c <= 0x11f9)) 61690075Sobrien return 1; 61718334Speter 61890075Sobrien /* CJK Unified Ideographs */ 61990075Sobrien if ((c >= 0xf900 && c <= 0xfa2d) 62090075Sobrien || (c >= 0xfb1f && c <= 0xfb36) 62190075Sobrien || (c >= 0xfb38 && c <= 0xfb3c) 62290075Sobrien || (c == 0xfb3e) 62390075Sobrien || (c >= 0xfb40 && c <= 0xfb41) 62490075Sobrien || (c >= 0xfb42 && c <= 0xfb44) 62590075Sobrien || (c >= 0xfb46 && c <= 0xfbb1) 62690075Sobrien || (c >= 0xfbd3 && c <= 0xfd3f) 62790075Sobrien || (c >= 0xfd50 && c <= 0xfd8f) 62890075Sobrien || (c >= 0xfd92 && c <= 0xfdc7) 62990075Sobrien || (c >= 0xfdf0 && c <= 0xfdfb) 63090075Sobrien || (c >= 0xfe70 && c <= 0xfe72) 63190075Sobrien || (c == 0xfe74) 63290075Sobrien || (c >= 0xfe76 && c <= 0xfefc) 63390075Sobrien || (c >= 0xff21 && c <= 0xff3a) 63490075Sobrien || (c >= 0xff41 && c <= 0xff5a) 63590075Sobrien || (c >= 0xff66 && c <= 0xffbe) 63690075Sobrien || (c >= 0xffc2 && c <= 0xffc7) 63790075Sobrien || (c >= 0xffca && c <= 0xffcf) 63890075Sobrien || (c >= 0xffd2 && c <= 0xffd7) 63990075Sobrien || (c >= 0xffda && c <= 0xffdc) 64090075Sobrien || (c >= 0x4e00 && c <= 0x9fa5)) 64190075Sobrien return 1; 64290075Sobrien 64390075Sobrien error ("universal-character-name '\\u%04x' not valid in identifier", c); 64490075Sobrien return 1; 64550397Sobrien#endif 64618334Speter} 64718334Speter 64890075Sobrien/* Add the UTF-8 representation of C to the token_buffer. */ 64918334Speter 65090075Sobrienstatic void 65190075Sobrienutf8_extend_token (c) 65290075Sobrien int c; 65318334Speter{ 65490075Sobrien int shift, mask; 65550397Sobrien 65690075Sobrien if (c <= 0x0000007f) 65718334Speter { 65890075Sobrien extend_token (c); 65990075Sobrien return; 66018334Speter } 66190075Sobrien else if (c <= 0x000007ff) 66290075Sobrien shift = 6, mask = 0xc0; 66390075Sobrien else if (c <= 0x0000ffff) 66490075Sobrien shift = 12, mask = 0xe0; 66590075Sobrien else if (c <= 0x001fffff) 66690075Sobrien shift = 18, mask = 0xf0; 66790075Sobrien else if (c <= 0x03ffffff) 66890075Sobrien shift = 24, mask = 0xf8; 66990075Sobrien else 67090075Sobrien shift = 30, mask = 0xfc; 67118334Speter 67290075Sobrien extend_token (mask | (c >> shift)); 67390075Sobrien do 67418334Speter { 67590075Sobrien shift -= 6; 67690075Sobrien extend_token ((unsigned char) (0x80 | (c >> shift))); 67718334Speter } 67890075Sobrien while (shift); 67918334Speter} 68090075Sobrien#endif 68118334Speter 68218334Speter#if 0 68318334Speterstruct try_type 68418334Speter{ 68590075Sobrien tree *const node_var; 68690075Sobrien const char unsigned_flag; 68790075Sobrien const char long_flag; 68890075Sobrien const char long_long_flag; 68918334Speter}; 69018334Speter 69152284Sobrienstruct try_type type_sequence[] = 69218334Speter{ 69318334Speter { &integer_type_node, 0, 0, 0}, 69418334Speter { &unsigned_type_node, 1, 0, 0}, 69518334Speter { &long_integer_type_node, 0, 1, 0}, 69618334Speter { &long_unsigned_type_node, 1, 1, 0}, 69718334Speter { &long_long_integer_type_node, 0, 1, 1}, 69818334Speter { &long_long_unsigned_type_node, 1, 1, 1} 69918334Speter}; 70018334Speter#endif /* 0 */ 70118334Speter 70252284Sobrienstruct pf_args 70352284Sobrien{ 70452284Sobrien /* Input */ 70590075Sobrien const char *str; 70690075Sobrien int fflag; 70790075Sobrien int lflag; 70852284Sobrien int base; 70990075Sobrien /* Output */ 71052284Sobrien int conversion_errno; 71152284Sobrien REAL_VALUE_TYPE value; 71290075Sobrien tree type; 71352284Sobrien}; 71452284Sobrien 71552284Sobrienstatic void 71652284Sobrienparse_float (data) 71752284Sobrien PTR data; 71852284Sobrien{ 71952284Sobrien struct pf_args * args = (struct pf_args *) data; 72090075Sobrien const char *typename; 72152284Sobrien 72290075Sobrien args->conversion_errno = 0; 72390075Sobrien args->type = double_type_node; 72490075Sobrien typename = "double"; 72552284Sobrien 72652284Sobrien /* The second argument, machine_mode, of REAL_VALUE_ATOF 72752284Sobrien tells the desired precision of the binary result 72852284Sobrien of decimal-to-binary conversion. */ 72952284Sobrien 73090075Sobrien if (args->fflag) 73152284Sobrien { 73290075Sobrien if (args->lflag) 73390075Sobrien error ("both 'f' and 'l' suffixes on floating constant"); 73452284Sobrien 73552284Sobrien args->type = float_type_node; 73690075Sobrien typename = "float"; 73752284Sobrien } 73890075Sobrien else if (args->lflag) 73952284Sobrien { 74052284Sobrien args->type = long_double_type_node; 74190075Sobrien typename = "long double"; 74252284Sobrien } 74390075Sobrien else if (flag_single_precision_constant) 74452284Sobrien { 74590075Sobrien args->type = float_type_node; 74690075Sobrien typename = "float"; 74752284Sobrien } 74890075Sobrien 74990075Sobrien errno = 0; 75090075Sobrien if (args->base == 16) 75190075Sobrien args->value = REAL_VALUE_HTOF (args->str, TYPE_MODE (args->type)); 75290075Sobrien else 75390075Sobrien args->value = REAL_VALUE_ATOF (args->str, TYPE_MODE (args->type)); 75490075Sobrien 75590075Sobrien args->conversion_errno = errno; 75690075Sobrien /* A diagnostic is required here by some ISO C testsuites. 75790075Sobrien This is not pedwarn, because some people don't want 75890075Sobrien an error for this. */ 75990075Sobrien if (REAL_VALUE_ISINF (args->value) && pedantic) 76090075Sobrien warning ("floating point number exceeds range of '%s'", typename); 76152284Sobrien} 76252284Sobrien 76318334Speterint 76490075Sobrienc_lex (value) 76590075Sobrien tree *value; 76618334Speter{ 76790075Sobrien const cpp_token *tok; 76818334Speter 76990075Sobrien retry: 77090075Sobrien timevar_push (TV_CPP); 77190075Sobrien do 77290075Sobrien tok = cpp_get_token (parse_in); 77390075Sobrien while (tok->type == CPP_PADDING); 77490075Sobrien timevar_pop (TV_CPP); 77518334Speter 77690075Sobrien /* The C++ front end does horrible things with the current line 77790075Sobrien number. To ensure an accurate line number, we must reset it 77890075Sobrien every time we return a token. */ 77990075Sobrien lineno = src_lineno; 78018334Speter 78190075Sobrien *value = NULL_TREE; 78290075Sobrien switch (tok->type) 78318334Speter { 78490075Sobrien case CPP_OPEN_BRACE: indent_level++; break; 78590075Sobrien case CPP_CLOSE_BRACE: indent_level--; break; 78618334Speter 78790075Sobrien /* Issue this error here, where we can get at tok->val.c. */ 78890075Sobrien case CPP_OTHER: 78990075Sobrien if (ISGRAPH (tok->val.c)) 79090075Sobrien error ("stray '%c' in program", tok->val.c); 79118334Speter else 79290075Sobrien error ("stray '\\%o' in program", tok->val.c); 79390075Sobrien goto retry; 79490075Sobrien 79590075Sobrien case CPP_NAME: 79690075Sobrien *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node)); 79790075Sobrien break; 79818334Speter 79990075Sobrien case CPP_NUMBER: 80090075Sobrien *value = lex_number ((const char *)tok->val.str.text, tok->val.str.len); 80190075Sobrien break; 80218334Speter 80390075Sobrien case CPP_CHAR: 80490075Sobrien case CPP_WCHAR: 80590075Sobrien *value = lex_charconst (tok); 80690075Sobrien break; 80718334Speter 80890075Sobrien case CPP_STRING: 80990075Sobrien case CPP_WSTRING: 81090075Sobrien *value = lex_string ((const char *)tok->val.str.text, 81190075Sobrien tok->val.str.len, tok->type == CPP_WSTRING); 81290075Sobrien break; 81318334Speter 81490075Sobrien /* These tokens should not be visible outside cpplib. */ 81590075Sobrien case CPP_HEADER_NAME: 81690075Sobrien case CPP_COMMENT: 81790075Sobrien case CPP_MACRO_ARG: 81890075Sobrien abort (); 81918334Speter 82090075Sobrien default: break; 82190075Sobrien } 82218334Speter 82390075Sobrien return tok->type; 82490075Sobrien} 82518334Speter 82690075Sobrien#define ERROR(msgid) do { error(msgid); goto syntax_error; } while(0) 82718334Speter 82890075Sobrienstatic tree 82990075Sobrienlex_number (str, len) 83090075Sobrien const char *str; 83190075Sobrien unsigned int len; 83290075Sobrien{ 83390075Sobrien int base = 10; 83490075Sobrien int count = 0; 83590075Sobrien int largest_digit = 0; 83690075Sobrien int numdigits = 0; 83790075Sobrien int overflow = 0; 83890075Sobrien int c; 83990075Sobrien tree value; 84090075Sobrien const char *p; 84190075Sobrien enum anon1 { NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON } floatflag = NOT_FLOAT; 84290075Sobrien 84390075Sobrien /* We actually store only HOST_BITS_PER_CHAR bits in each part. 84490075Sobrien The code below which fills the parts array assumes that a host 84590075Sobrien int is at least twice as wide as a host char, and that 84690075Sobrien HOST_BITS_PER_WIDE_INT is an even multiple of HOST_BITS_PER_CHAR. 84790075Sobrien Two HOST_WIDE_INTs is the largest int literal we can store. 84890075Sobrien In order to detect overflow below, the number of parts (TOTAL_PARTS) 84990075Sobrien must be exactly the number of parts needed to hold the bits 85090075Sobrien of two HOST_WIDE_INTs. */ 85190075Sobrien#define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2) 85290075Sobrien unsigned int parts[TOTAL_PARTS]; 85390075Sobrien 85490075Sobrien /* Optimize for most frequent case. */ 85590075Sobrien if (len == 1) 85690075Sobrien { 85790075Sobrien if (*str == '0') 85890075Sobrien return integer_zero_node; 85990075Sobrien else if (*str == '1') 86090075Sobrien return integer_one_node; 86190075Sobrien else 86290075Sobrien return build_int_2 (*str - '0', 0); 86390075Sobrien } 86418334Speter 86590075Sobrien for (count = 0; count < TOTAL_PARTS; count++) 86690075Sobrien parts[count] = 0; 86718334Speter 86890075Sobrien /* len is known to be >1 at this point. */ 86990075Sobrien p = str; 87018334Speter 87190075Sobrien if (len > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 87290075Sobrien { 87390075Sobrien base = 16; 87490075Sobrien p = str + 2; 87590075Sobrien } 87690075Sobrien /* The ISDIGIT check is so we are not confused by a suffix on 0. */ 87790075Sobrien else if (str[0] == '0' && ISDIGIT (str[1])) 87890075Sobrien { 87990075Sobrien base = 8; 88090075Sobrien p = str + 1; 88190075Sobrien } 88218334Speter 88390075Sobrien do 88490075Sobrien { 88590075Sobrien c = *p++; 88618334Speter 88790075Sobrien if (c == '.') 88818334Speter { 88990075Sobrien if (floatflag == AFTER_POINT) 89090075Sobrien ERROR ("too many decimal points in floating constant"); 89190075Sobrien else if (floatflag == AFTER_EXPON) 89290075Sobrien ERROR ("decimal point in exponent - impossible!"); 89390075Sobrien else 89490075Sobrien floatflag = AFTER_POINT; 89518334Speter 89690075Sobrien if (base == 8) 89790075Sobrien base = 10; 89890075Sobrien } 89990075Sobrien else if (c == '_') 90090075Sobrien /* Possible future extension: silently ignore _ in numbers, 90190075Sobrien permitting cosmetic grouping - e.g. 0x8000_0000 == 0x80000000 90290075Sobrien but somewhat easier to read. Ada has this? */ 90390075Sobrien ERROR ("underscore in number"); 90490075Sobrien else 90590075Sobrien { 90690075Sobrien int n; 90790075Sobrien /* It is not a decimal point. 90890075Sobrien It should be a digit (perhaps a hex digit). */ 90918334Speter 91090075Sobrien if (ISDIGIT (c) 91190075Sobrien || (base == 16 && ISXDIGIT (c))) 91218334Speter { 91390075Sobrien n = hex_value (c); 91418334Speter } 91590075Sobrien else if (base <= 10 && (c == 'e' || c == 'E')) 91690075Sobrien { 91790075Sobrien base = 10; 91890075Sobrien floatflag = AFTER_EXPON; 91990075Sobrien break; 92090075Sobrien } 92190075Sobrien else if (base == 16 && (c == 'p' || c == 'P')) 92290075Sobrien { 92390075Sobrien floatflag = AFTER_EXPON; 92490075Sobrien break; /* start of exponent */ 92590075Sobrien } 92690075Sobrien else 92790075Sobrien { 92890075Sobrien p--; 92990075Sobrien break; /* start of suffix */ 93090075Sobrien } 93118334Speter 93290075Sobrien if (n >= largest_digit) 93390075Sobrien largest_digit = n; 93490075Sobrien numdigits++; 93590075Sobrien 93690075Sobrien for (count = 0; count < TOTAL_PARTS; count++) 93790075Sobrien { 93890075Sobrien parts[count] *= base; 93990075Sobrien if (count) 94018334Speter { 94190075Sobrien parts[count] 94290075Sobrien += (parts[count-1] >> HOST_BITS_PER_CHAR); 94390075Sobrien parts[count-1] 94490075Sobrien &= (1 << HOST_BITS_PER_CHAR) - 1; 94518334Speter } 94690075Sobrien else 94790075Sobrien parts[0] += n; 94818334Speter } 94990075Sobrien 95090075Sobrien /* If the highest-order part overflows (gets larger than 95190075Sobrien a host char will hold) then the whole number has 95290075Sobrien overflowed. Record this and truncate the highest-order 95390075Sobrien part. */ 95490075Sobrien if (parts[TOTAL_PARTS - 1] >> HOST_BITS_PER_CHAR) 95590075Sobrien { 95690075Sobrien overflow = 1; 95790075Sobrien parts[TOTAL_PARTS - 1] &= (1 << HOST_BITS_PER_CHAR) - 1; 95890075Sobrien } 95918334Speter } 96090075Sobrien } 96190075Sobrien while (p < str + len); 96218334Speter 96390075Sobrien /* This can happen on input like `int i = 0x;' */ 96490075Sobrien if (numdigits == 0) 96590075Sobrien ERROR ("numeric constant with no digits"); 96618334Speter 96790075Sobrien if (largest_digit >= base) 96890075Sobrien ERROR ("numeric constant contains digits beyond the radix"); 96950397Sobrien 97090075Sobrien if (floatflag != NOT_FLOAT) 97190075Sobrien { 97290075Sobrien tree type; 97390075Sobrien int imag, fflag, lflag, conversion_errno; 97490075Sobrien REAL_VALUE_TYPE real; 97590075Sobrien struct pf_args args; 97690075Sobrien char *copy; 97718334Speter 97890075Sobrien if (base == 16 && pedantic && !flag_isoc99) 97990075Sobrien pedwarn ("floating constant may not be in radix 16"); 98018334Speter 98190075Sobrien if (base == 16 && floatflag != AFTER_EXPON) 98290075Sobrien ERROR ("hexadecimal floating constant has no exponent"); 98318334Speter 98490075Sobrien /* Read explicit exponent if any, and put it in tokenbuf. */ 98590075Sobrien if ((base == 10 && ((c == 'e') || (c == 'E'))) 98690075Sobrien || (base == 16 && (c == 'p' || c == 'P'))) 98790075Sobrien { 98890075Sobrien if (p < str + len) 98990075Sobrien c = *p++; 99090075Sobrien if (p < str + len && (c == '+' || c == '-')) 99190075Sobrien c = *p++; 99290075Sobrien /* Exponent is decimal, even if string is a hex float. */ 99390075Sobrien if (! ISDIGIT (c)) 99490075Sobrien ERROR ("floating constant exponent has no digits"); 99590075Sobrien while (p < str + len && ISDIGIT (c)) 99690075Sobrien c = *p++; 99790075Sobrien if (! ISDIGIT (c)) 99890075Sobrien p--; 99990075Sobrien } 100018334Speter 100190075Sobrien /* Copy the float constant now; we don't want any suffixes in the 100290075Sobrien string passed to parse_float. */ 100390075Sobrien copy = alloca (p - str + 1); 100490075Sobrien memcpy (copy, str, p - str); 100590075Sobrien copy[p - str] = '\0'; 100618334Speter 100790075Sobrien /* Now parse suffixes. */ 100890075Sobrien fflag = lflag = imag = 0; 100990075Sobrien while (p < str + len) 101090075Sobrien switch (*p++) 101118334Speter { 101290075Sobrien case 'f': case 'F': 101390075Sobrien if (fflag) 101490075Sobrien ERROR ("more than one 'f' suffix on floating constant"); 101590075Sobrien else if (warn_traditional && !in_system_header 101690075Sobrien && ! cpp_sys_macro_p (parse_in)) 101790075Sobrien warning ("traditional C rejects the 'f' suffix"); 101818334Speter 101990075Sobrien fflag = 1; 102090075Sobrien break; 102118334Speter 102290075Sobrien case 'l': case 'L': 102390075Sobrien if (lflag) 102490075Sobrien ERROR ("more than one 'l' suffix on floating constant"); 102590075Sobrien else if (warn_traditional && !in_system_header 102690075Sobrien && ! cpp_sys_macro_p (parse_in)) 102790075Sobrien warning ("traditional C rejects the 'l' suffix"); 102818334Speter 102990075Sobrien lflag = 1; 103090075Sobrien break; 103118334Speter 103290075Sobrien case 'i': case 'I': 103390075Sobrien case 'j': case 'J': 103490075Sobrien if (imag) 103590075Sobrien ERROR ("more than one 'i' or 'j' suffix on floating constant"); 103690075Sobrien else if (pedantic) 103790075Sobrien pedwarn ("ISO C forbids imaginary numeric constants"); 103890075Sobrien imag = 1; 103990075Sobrien break; 104018334Speter 104190075Sobrien default: 104290075Sobrien ERROR ("invalid suffix on floating constant"); 104318334Speter } 104418334Speter 104590075Sobrien /* Setup input for parse_float() */ 104690075Sobrien args.str = copy; 104790075Sobrien args.fflag = fflag; 104890075Sobrien args.lflag = lflag; 104990075Sobrien args.base = base; 105018334Speter 105190075Sobrien /* Convert string to a double, checking for overflow. */ 105290075Sobrien if (do_float_handler (parse_float, (PTR) &args)) 105390075Sobrien { 105490075Sobrien /* Receive output from parse_float() */ 105590075Sobrien real = args.value; 105690075Sobrien } 105790075Sobrien else 105890075Sobrien /* We got an exception from parse_float() */ 105990075Sobrien ERROR ("floating constant out of range"); 106018334Speter 106190075Sobrien /* Receive output from parse_float() */ 106290075Sobrien conversion_errno = args.conversion_errno; 106390075Sobrien type = args.type; 106452284Sobrien 106518334Speter#ifdef ERANGE 106690075Sobrien /* ERANGE is also reported for underflow, 106790075Sobrien so test the value to distinguish overflow from that. */ 106890075Sobrien if (conversion_errno == ERANGE && !flag_traditional && pedantic 106990075Sobrien && (REAL_VALUES_LESS (dconst1, real) 107090075Sobrien || REAL_VALUES_LESS (real, dconstm1))) 107190075Sobrien warning ("floating point number exceeds range of 'double'"); 107218334Speter#endif 107318334Speter 107490075Sobrien /* Create a node with determined type and value. */ 107590075Sobrien if (imag) 107690075Sobrien value = build_complex (NULL_TREE, convert (type, integer_zero_node), 107790075Sobrien build_real (type, real)); 107890075Sobrien else 107990075Sobrien value = build_real (type, real); 108090075Sobrien } 108190075Sobrien else 108290075Sobrien { 108390075Sobrien tree trad_type, ansi_type, type; 108490075Sobrien HOST_WIDE_INT high, low; 108590075Sobrien int spec_unsigned = 0; 108690075Sobrien int spec_long = 0; 108790075Sobrien int spec_long_long = 0; 108890075Sobrien int spec_imag = 0; 108990075Sobrien int suffix_lu = 0; 109090075Sobrien int warn = 0, i; 109118334Speter 109290075Sobrien trad_type = ansi_type = type = NULL_TREE; 109390075Sobrien while (p < str + len) 109490075Sobrien { 109590075Sobrien c = *p++; 109690075Sobrien switch (c) 109790075Sobrien { 109890075Sobrien case 'u': case 'U': 109990075Sobrien if (spec_unsigned) 110090075Sobrien error ("two 'u' suffixes on integer constant"); 110190075Sobrien else if (warn_traditional && !in_system_header 110290075Sobrien && ! cpp_sys_macro_p (parse_in)) 110390075Sobrien warning ("traditional C rejects the 'u' suffix"); 110418334Speter 110590075Sobrien spec_unsigned = 1; 110690075Sobrien if (spec_long) 110790075Sobrien suffix_lu = 1; 110890075Sobrien break; 110918334Speter 111090075Sobrien case 'l': case 'L': 111190075Sobrien if (spec_long) 111290075Sobrien { 111390075Sobrien if (spec_long_long) 111490075Sobrien error ("three 'l' suffixes on integer constant"); 111590075Sobrien else if (suffix_lu) 111690075Sobrien error ("'lul' is not a valid integer suffix"); 111790075Sobrien else if (c != spec_long) 111890075Sobrien error ("'Ll' and 'lL' are not valid integer suffixes"); 111990075Sobrien else if (pedantic && ! flag_isoc99 112090075Sobrien && ! in_system_header && warn_long_long) 112190075Sobrien pedwarn ("ISO C89 forbids long long integer constants"); 112290075Sobrien spec_long_long = 1; 112390075Sobrien } 112490075Sobrien spec_long = c; 112590075Sobrien break; 112618334Speter 112790075Sobrien case 'i': case 'I': case 'j': case 'J': 112890075Sobrien if (spec_imag) 112990075Sobrien error ("more than one 'i' or 'j' suffix on integer constant"); 113090075Sobrien else if (pedantic) 113190075Sobrien pedwarn ("ISO C forbids imaginary numeric constants"); 113290075Sobrien spec_imag = 1; 113390075Sobrien break; 113418334Speter 113590075Sobrien default: 113690075Sobrien ERROR ("invalid suffix on integer constant"); 113790075Sobrien } 113890075Sobrien } 113918334Speter 114090075Sobrien /* If the literal overflowed, pedwarn about it now. */ 114190075Sobrien if (overflow) 114290075Sobrien { 114390075Sobrien warn = 1; 114490075Sobrien pedwarn ("integer constant is too large for this configuration of the compiler - truncated to %d bits", HOST_BITS_PER_WIDE_INT * 2); 114590075Sobrien } 114618334Speter 114790075Sobrien /* This is simplified by the fact that our constant 114890075Sobrien is always positive. */ 114952284Sobrien 115090075Sobrien high = low = 0; 115118334Speter 115290075Sobrien for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++) 115390075Sobrien { 115490075Sobrien high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT 115590075Sobrien / HOST_BITS_PER_CHAR)] 115690075Sobrien << (i * HOST_BITS_PER_CHAR)); 115790075Sobrien low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR); 115890075Sobrien } 115918334Speter 116090075Sobrien value = build_int_2 (low, high); 116190075Sobrien TREE_TYPE (value) = long_long_unsigned_type_node; 116218334Speter 116390075Sobrien /* If warn_traditional, calculate both the ISO type and the 116490075Sobrien traditional type, then see if they disagree. 116590075Sobrien Otherwise, calculate only the type for the dialect in use. */ 116690075Sobrien if (warn_traditional || flag_traditional) 116790075Sobrien { 116890075Sobrien /* Calculate the traditional type. */ 116990075Sobrien /* Traditionally, any constant is signed; but if unsigned is 117090075Sobrien specified explicitly, obey that. Use the smallest size 117190075Sobrien with the right number of bits, except for one special 117290075Sobrien case with decimal constants. */ 117390075Sobrien if (! spec_long && base != 10 117490075Sobrien && int_fits_type_p (value, unsigned_type_node)) 117590075Sobrien trad_type = spec_unsigned ? unsigned_type_node : integer_type_node; 117690075Sobrien /* A decimal constant must be long if it does not fit in 117790075Sobrien type int. I think this is independent of whether the 117890075Sobrien constant is signed. */ 117990075Sobrien else if (! spec_long && base == 10 118090075Sobrien && int_fits_type_p (value, integer_type_node)) 118190075Sobrien trad_type = spec_unsigned ? unsigned_type_node : integer_type_node; 118290075Sobrien else if (! spec_long_long) 118390075Sobrien trad_type = (spec_unsigned 118490075Sobrien ? long_unsigned_type_node 118590075Sobrien : long_integer_type_node); 118690075Sobrien else if (int_fits_type_p (value, 118790075Sobrien spec_unsigned 118890075Sobrien ? long_long_unsigned_type_node 118990075Sobrien : long_long_integer_type_node)) 119090075Sobrien trad_type = (spec_unsigned 119190075Sobrien ? long_long_unsigned_type_node 119290075Sobrien : long_long_integer_type_node); 119390075Sobrien else 119490075Sobrien trad_type = (spec_unsigned 119590075Sobrien ? widest_unsigned_literal_type_node 119690075Sobrien : widest_integer_literal_type_node); 119790075Sobrien } 119890075Sobrien if (warn_traditional || ! flag_traditional) 119990075Sobrien { 120090075Sobrien /* Calculate the ISO type. */ 120190075Sobrien if (! spec_long && ! spec_unsigned 120290075Sobrien && int_fits_type_p (value, integer_type_node)) 120390075Sobrien ansi_type = integer_type_node; 120490075Sobrien else if (! spec_long && (base != 10 || spec_unsigned) 120590075Sobrien && int_fits_type_p (value, unsigned_type_node)) 120690075Sobrien ansi_type = unsigned_type_node; 120790075Sobrien else if (! spec_unsigned && !spec_long_long 120890075Sobrien && int_fits_type_p (value, long_integer_type_node)) 120990075Sobrien ansi_type = long_integer_type_node; 121090075Sobrien else if (! spec_long_long 121190075Sobrien && int_fits_type_p (value, long_unsigned_type_node)) 121290075Sobrien ansi_type = long_unsigned_type_node; 121390075Sobrien else if (! spec_unsigned 121490075Sobrien && int_fits_type_p (value, long_long_integer_type_node)) 121590075Sobrien ansi_type = long_long_integer_type_node; 121690075Sobrien else if (int_fits_type_p (value, long_long_unsigned_type_node)) 121790075Sobrien ansi_type = long_long_unsigned_type_node; 121890075Sobrien else if (! spec_unsigned 121990075Sobrien && int_fits_type_p (value, widest_integer_literal_type_node)) 122090075Sobrien ansi_type = widest_integer_literal_type_node; 122190075Sobrien else 122290075Sobrien ansi_type = widest_unsigned_literal_type_node; 122390075Sobrien } 122418334Speter 122590075Sobrien type = flag_traditional ? trad_type : ansi_type; 122618334Speter 122790075Sobrien /* We assume that constants specified in a non-decimal 122890075Sobrien base are bit patterns, and that the programmer really 122990075Sobrien meant what they wrote. */ 123090075Sobrien if (warn_traditional && !in_system_header 123190075Sobrien && base == 10 && trad_type != ansi_type) 123290075Sobrien { 123390075Sobrien if (TYPE_PRECISION (trad_type) != TYPE_PRECISION (ansi_type)) 123490075Sobrien warning ("width of integer constant changes with -traditional"); 123590075Sobrien else if (TREE_UNSIGNED (trad_type) != TREE_UNSIGNED (ansi_type)) 123690075Sobrien warning ("integer constant is unsigned in ISO C, signed with -traditional"); 123790075Sobrien else 123890075Sobrien warning ("width of integer constant may change on other systems with -traditional"); 123990075Sobrien } 124018334Speter 124190075Sobrien if (pedantic && !flag_traditional && (flag_isoc99 || !spec_long_long) 124290075Sobrien && !warn 124390075Sobrien && ((flag_isoc99 124490075Sobrien ? TYPE_PRECISION (long_long_integer_type_node) 124590075Sobrien : TYPE_PRECISION (long_integer_type_node)) < TYPE_PRECISION (type))) 124690075Sobrien { 124790075Sobrien warn = 1; 124890075Sobrien pedwarn ("integer constant larger than the maximum value of %s", 124990075Sobrien (flag_isoc99 125090075Sobrien ? (TREE_UNSIGNED (type) 125190075Sobrien ? _("an unsigned long long int") 125290075Sobrien : _("a long long int")) 125390075Sobrien : _("an unsigned long int"))); 125490075Sobrien } 125552284Sobrien 125690075Sobrien if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type)) 125790075Sobrien warning ("decimal constant is so large that it is unsigned"); 125852284Sobrien 125990075Sobrien if (spec_imag) 126090075Sobrien { 126190075Sobrien if (TYPE_PRECISION (type) 126290075Sobrien <= TYPE_PRECISION (integer_type_node)) 126390075Sobrien value = build_complex (NULL_TREE, integer_zero_node, 126490075Sobrien convert (integer_type_node, value)); 126590075Sobrien else 126690075Sobrien ERROR ("complex integer constant is too wide for 'complex int'"); 126790075Sobrien } 126890075Sobrien else if (flag_traditional && !int_fits_type_p (value, type)) 126990075Sobrien /* The traditional constant 0x80000000 is signed 127090075Sobrien but doesn't fit in the range of int. 127190075Sobrien This will change it to -0x80000000, which does fit. */ 127290075Sobrien { 127390075Sobrien TREE_TYPE (value) = unsigned_type (type); 127490075Sobrien value = convert (type, value); 127590075Sobrien TREE_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (value) = 0; 127690075Sobrien } 127790075Sobrien else 127890075Sobrien TREE_TYPE (value) = type; 127952284Sobrien 128090075Sobrien /* If it's still an integer (not a complex), and it doesn't 128190075Sobrien fit in the type we choose for it, then pedwarn. */ 128218334Speter 128390075Sobrien if (! warn 128490075Sobrien && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE 128590075Sobrien && ! int_fits_type_p (value, TREE_TYPE (value))) 128690075Sobrien pedwarn ("integer constant is larger than the maximum value for its type"); 128790075Sobrien } 128850397Sobrien 128990075Sobrien if (p < str + len) 129090075Sobrien error ("missing white space after number '%.*s'", (int) (p - str), str); 129150397Sobrien 129290075Sobrien return value; 129318334Speter 129490075Sobrien syntax_error: 129590075Sobrien return integer_zero_node; 129690075Sobrien} 129718334Speter 129890075Sobrienstatic tree 129990075Sobrienlex_string (str, len, wide) 130090075Sobrien const char *str; 130190075Sobrien unsigned int len; 130290075Sobrien int wide; 130390075Sobrien{ 130490075Sobrien tree value; 130590075Sobrien char *buf = alloca ((len + 1) * (wide ? WCHAR_BYTES : 1)); 130690075Sobrien char *q = buf; 130790075Sobrien const char *p = str, *limit = str + len; 130890075Sobrien unsigned int c; 130990075Sobrien unsigned width = wide ? WCHAR_TYPE_SIZE 131090075Sobrien : TYPE_PRECISION (char_type_node); 131152284Sobrien 131252284Sobrien#ifdef MULTIBYTE_CHARS 131390075Sobrien /* Reset multibyte conversion state. */ 131490075Sobrien (void) local_mbtowc (NULL, NULL, 0); 131518334Speter#endif 131618334Speter 131790075Sobrien while (p < limit) 131890075Sobrien { 131990075Sobrien#ifdef MULTIBYTE_CHARS 132090075Sobrien wchar_t wc; 132190075Sobrien int char_len; 132218334Speter 132390075Sobrien char_len = local_mbtowc (&wc, p, limit - p); 132490075Sobrien if (char_len == -1) 132590075Sobrien { 132690075Sobrien warning ("ignoring invalid multibyte character"); 132790075Sobrien char_len = 1; 132890075Sobrien c = *p++; 132990075Sobrien } 133090075Sobrien else 133190075Sobrien { 133290075Sobrien p += char_len; 133390075Sobrien c = wc; 133490075Sobrien } 133590075Sobrien#else 133690075Sobrien c = *p++; 133752284Sobrien#endif 133818334Speter 133990075Sobrien if (c == '\\' && !ignore_escape_flag) 134090075Sobrien { 134190075Sobrien unsigned int mask; 134218334Speter 134390075Sobrien if (width < HOST_BITS_PER_INT) 134490075Sobrien mask = ((unsigned int) 1 << width) - 1; 134590075Sobrien else 134690075Sobrien mask = ~0; 134790075Sobrien c = cpp_parse_escape (parse_in, (const unsigned char **) &p, 134890075Sobrien (const unsigned char *) limit, 134990075Sobrien mask, flag_traditional); 135090075Sobrien } 135190075Sobrien 135290075Sobrien /* Add this single character into the buffer either as a wchar_t 135390075Sobrien or as a single byte. */ 135490075Sobrien if (wide) 135590075Sobrien { 135690075Sobrien unsigned charwidth = TYPE_PRECISION (char_type_node); 135790075Sobrien unsigned bytemask = (1 << charwidth) - 1; 135890075Sobrien int byte; 135918334Speter 136090075Sobrien for (byte = 0; byte < WCHAR_BYTES; ++byte) 136118334Speter { 136290075Sobrien int n; 136390075Sobrien if (byte >= (int) sizeof (c)) 136490075Sobrien n = 0; 136590075Sobrien else 136690075Sobrien n = (c >> (byte * charwidth)) & bytemask; 136790075Sobrien if (BYTES_BIG_ENDIAN) 136890075Sobrien q[WCHAR_BYTES - byte - 1] = n; 136990075Sobrien else 137090075Sobrien q[byte] = n; 137118334Speter } 137290075Sobrien q += WCHAR_BYTES; 137390075Sobrien } 137490075Sobrien else 137590075Sobrien { 137690075Sobrien *q++ = c; 137790075Sobrien } 137890075Sobrien } 137918334Speter 138090075Sobrien /* Terminate the string value, either with a single byte zero 138190075Sobrien or with a wide zero. */ 138218334Speter 138390075Sobrien if (wide) 138490075Sobrien { 138590075Sobrien memset (q, 0, WCHAR_BYTES); 138690075Sobrien q += WCHAR_BYTES; 138718334Speter } 138890075Sobrien else 138990075Sobrien { 139090075Sobrien *q++ = '\0'; 139190075Sobrien } 139218334Speter 139390075Sobrien value = build_string (q - buf, buf); 139418334Speter 139590075Sobrien if (wide) 139690075Sobrien TREE_TYPE (value) = wchar_array_type_node; 139790075Sobrien else 139890075Sobrien TREE_TYPE (value) = char_array_type_node; 139918334Speter return value; 140018334Speter} 140118334Speter 140290075Sobrien/* Converts a (possibly wide) character constant token into a tree. */ 140390075Sobrienstatic tree 140490075Sobrienlex_charconst (token) 140590075Sobrien const cpp_token *token; 140618334Speter{ 140790075Sobrien HOST_WIDE_INT result; 140890075Sobrien tree value; 140990075Sobrien unsigned int chars_seen; 141090075Sobrien 141190075Sobrien result = cpp_interpret_charconst (parse_in, token, warn_multichar, 141290075Sobrien flag_traditional, &chars_seen); 141390075Sobrien if (token->type == CPP_WCHAR) 141490075Sobrien { 141590075Sobrien value = build_int_2 (result, 0); 141690075Sobrien TREE_TYPE (value) = wchar_type_node; 141790075Sobrien } 141890075Sobrien else 141990075Sobrien { 142090075Sobrien if (result < 0) 142190075Sobrien value = build_int_2 (result, -1); 142290075Sobrien else 142390075Sobrien value = build_int_2 (result, 0); 142490075Sobrien 142590075Sobrien /* In C, a character constant has type 'int'. 142690075Sobrien In C++ 'char', but multi-char charconsts have type 'int'. */ 142790075Sobrien if (c_language == clk_cplusplus && chars_seen <= 1) 142890075Sobrien TREE_TYPE (value) = char_type_node; 142990075Sobrien else 143090075Sobrien TREE_TYPE (value) = integer_type_node; 143190075Sobrien } 143290075Sobrien 143390075Sobrien return value; 143418334Speter} 1435