lex.c revision 90075
1/* Separate lexical analyzer for GNU C++. 2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001 Free Software Foundation, Inc. 4 Hacked by Michael Tiemann (tiemann@cygnus.com) 5 6This file is part of GNU CC. 7 8GNU CC is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 2, or (at your option) 11any later version. 12 13GNU CC is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with GNU CC; see the file COPYING. If not, write to 20the Free Software Foundation, 59 Temple Place - Suite 330, 21Boston, MA 02111-1307, USA. */ 22 23 24/* This file is the lexical analyzer for GNU C++. */ 25 26/* Cause the `yydebug' variable to be defined. */ 27#define YYDEBUG 1 28 29#include "config.h" 30#include "system.h" 31#include "input.h" 32#include "tree.h" 33#include "cp-tree.h" 34#include "cpplib.h" 35#include "c-lex.h" 36#include "lex.h" 37#include "parse.h" 38#include "flags.h" 39#include "c-pragma.h" 40#include "toplev.h" 41#include "output.h" 42#include "ggc.h" 43#include "tm_p.h" 44#include "timevar.h" 45#include "diagnostic.h" 46 47#ifdef MULTIBYTE_CHARS 48#include "mbchar.h" 49#include <locale.h> 50#endif 51 52extern void yyprint PARAMS ((FILE *, int, YYSTYPE)); 53 54static int interface_strcmp PARAMS ((const char *)); 55static int *init_cpp_parse PARAMS ((void)); 56static void init_cp_pragma PARAMS ((void)); 57 58static tree parse_strconst_pragma PARAMS ((const char *, int)); 59static void handle_pragma_vtable PARAMS ((cpp_reader *)); 60static void handle_pragma_unit PARAMS ((cpp_reader *)); 61static void handle_pragma_interface PARAMS ((cpp_reader *)); 62static void handle_pragma_implementation PARAMS ((cpp_reader *)); 63static void handle_pragma_java_exceptions PARAMS ((cpp_reader *)); 64 65#ifdef GATHER_STATISTICS 66#ifdef REDUCE_LENGTH 67static int reduce_cmp PARAMS ((int *, int *)); 68static int token_cmp PARAMS ((int *, int *)); 69#endif 70#endif 71static int is_global PARAMS ((tree)); 72static void init_operators PARAMS ((void)); 73static void copy_lang_type PARAMS ((tree)); 74 75/* A constraint that can be tested at compile time. */ 76#ifdef __STDC__ 77#define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1] 78#else 79#define CONSTRAINT(name, expr) extern int constraint_/**/name [(expr) ? 1 : -1] 80#endif 81 82#include "cpplib.h" 83 84extern int yychar; /* the lookahead symbol */ 85extern YYSTYPE yylval; /* the semantic value of the */ 86 /* lookahead symbol */ 87 88/* These flags are used by c-lex.c. In C++, they're always off and on, 89 respectively. */ 90int warn_traditional = 0; 91int flag_digraphs = 1; 92 93/* the declaration found for the last IDENTIFIER token read in. 94 yylex must look this up to detect typedefs, which get token type TYPENAME, 95 so it is left around in case the identifier is not a typedef but is 96 used in a context which makes it a reference to a variable. */ 97tree lastiddecl; 98 99/* Array for holding counts of the numbers of tokens seen. */ 100extern int *token_count; 101 102/* Functions and data structures for #pragma interface. 103 104 `#pragma implementation' means that the main file being compiled 105 is considered to implement (provide) the classes that appear in 106 its main body. I.e., if this is file "foo.cc", and class `bar' 107 is defined in "foo.cc", then we say that "foo.cc implements bar". 108 109 All main input files "implement" themselves automagically. 110 111 `#pragma interface' means that unless this file (of the form "foo.h" 112 is not presently being included by file "foo.cc", the 113 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none 114 of the vtables nor any of the inline functions defined in foo.h 115 will ever be output. 116 117 There are cases when we want to link files such as "defs.h" and 118 "main.cc". In this case, we give "defs.h" a `#pragma interface', 119 and "main.cc" has `#pragma implementation "defs.h"'. */ 120 121struct impl_files 122{ 123 const char *filename; 124 struct impl_files *next; 125}; 126 127static struct impl_files *impl_file_chain; 128 129 130/* Return something to represent absolute declarators containing a *. 131 TARGET is the absolute declarator that the * contains. 132 CV_QUALIFIERS is a list of modifiers such as const or volatile 133 to apply to the pointer type, represented as identifiers. 134 135 We return an INDIRECT_REF whose "contents" are TARGET 136 and whose type is the modifier list. */ 137 138tree 139make_pointer_declarator (cv_qualifiers, target) 140 tree cv_qualifiers, target; 141{ 142 if (target && TREE_CODE (target) == IDENTIFIER_NODE 143 && ANON_AGGRNAME_P (target)) 144 error ("type name expected before `*'"); 145 target = build_nt (INDIRECT_REF, target); 146 TREE_TYPE (target) = cv_qualifiers; 147 return target; 148} 149 150/* Return something to represent absolute declarators containing a &. 151 TARGET is the absolute declarator that the & contains. 152 CV_QUALIFIERS is a list of modifiers such as const or volatile 153 to apply to the reference type, represented as identifiers. 154 155 We return an ADDR_EXPR whose "contents" are TARGET 156 and whose type is the modifier list. */ 157 158tree 159make_reference_declarator (cv_qualifiers, target) 160 tree cv_qualifiers, target; 161{ 162 if (target) 163 { 164 if (TREE_CODE (target) == ADDR_EXPR) 165 { 166 error ("cannot declare references to references"); 167 return target; 168 } 169 if (TREE_CODE (target) == INDIRECT_REF) 170 { 171 error ("cannot declare pointers to references"); 172 return target; 173 } 174 if (TREE_CODE (target) == IDENTIFIER_NODE && ANON_AGGRNAME_P (target)) 175 error ("type name expected before `&'"); 176 } 177 target = build_nt (ADDR_EXPR, target); 178 TREE_TYPE (target) = cv_qualifiers; 179 return target; 180} 181 182tree 183make_call_declarator (target, parms, cv_qualifiers, exception_specification) 184 tree target, parms, cv_qualifiers, exception_specification; 185{ 186 target = build_nt (CALL_EXPR, target, 187 tree_cons (parms, cv_qualifiers, NULL_TREE), 188 /* The third operand is really RTL. We 189 shouldn't put anything there. */ 190 NULL_TREE); 191 CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification; 192 return target; 193} 194 195void 196set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification) 197 tree call_declarator, cv_qualifiers, exception_specification; 198{ 199 CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers; 200 CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification; 201} 202 203int interface_only; /* whether or not current file is only for 204 interface definitions. */ 205int interface_unknown; /* whether or not we know this class 206 to behave according to #pragma interface. */ 207 208/* Tree code classes. */ 209 210#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE, 211 212static const char cplus_tree_code_type[] = { 213 'x', 214#include "cp-tree.def" 215}; 216#undef DEFTREECODE 217 218/* Table indexed by tree code giving number of expression 219 operands beyond the fixed part of the node structure. 220 Not used for types or decls. */ 221 222#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH, 223 224static const int cplus_tree_code_length[] = { 225 0, 226#include "cp-tree.def" 227}; 228#undef DEFTREECODE 229 230/* Names of tree components. 231 Used for printing out the tree and error messages. */ 232#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME, 233 234static const char *const cplus_tree_code_name[] = { 235 "@@dummy", 236#include "cp-tree.def" 237}; 238#undef DEFTREECODE 239 240/* Post-switch processing. */ 241void 242cxx_post_options () 243{ 244 c_common_post_options (); 245} 246 247/* Initialization before switch parsing. */ 248void 249cxx_init_options () 250{ 251 c_common_init_options (clk_cplusplus); 252 253 /* Default exceptions on. */ 254 flag_exceptions = 1; 255 /* By default wrap lines at 80 characters. Is getenv ("COLUMNS") 256 preferable? */ 257 diagnostic_line_cutoff (global_dc) = 80; 258 /* By default, emit location information once for every 259 diagnostic message. */ 260 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE; 261} 262 263void 264cxx_finish () 265{ 266 if (flag_gnu_xref) 267 GNU_xref_end (errorcount + sorrycount); 268 c_common_finish (); 269} 270 271static int * 272init_cpp_parse () 273{ 274#ifdef GATHER_STATISTICS 275#ifdef REDUCE_LENGTH 276 reduce_count = (int *) xcalloc (sizeof (int), (REDUCE_LENGTH + 1)); 277 reduce_count += 1; 278 token_count = (int *) xcalloc (sizeof (int), (TOKEN_LENGTH + 1)); 279 token_count += 1; 280#endif 281#endif 282 return token_count; 283} 284 285/* A mapping from tree codes to operator name information. */ 286operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE]; 287/* Similar, but for assignment operators. */ 288operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE]; 289 290/* Initialize data structures that keep track of operator names. */ 291 292#define DEF_OPERATOR(NAME, C, M, AR, AP) \ 293 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256); 294#include "operators.def" 295#undef DEF_OPERATOR 296 297static void 298init_operators () 299{ 300 tree identifier; 301 char buffer[256]; 302 struct operator_name_info_t *oni; 303 304#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ 305 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \ 306 identifier = get_identifier (buffer); \ 307 IDENTIFIER_OPNAME_P (identifier) = 1; \ 308 \ 309 oni = (ASSN_P \ 310 ? &assignment_operator_name_info[(int) CODE] \ 311 : &operator_name_info[(int) CODE]); \ 312 oni->identifier = identifier; \ 313 oni->name = NAME; \ 314 oni->mangled_name = MANGLING; 315 316#include "operators.def" 317#undef DEF_OPERATOR 318 319 operator_name_info[(int) ERROR_MARK].identifier 320 = get_identifier ("<invalid operator>"); 321 322 /* Handle some special cases. These operators are not defined in 323 the language, but can be produced internally. We may need them 324 for error-reporting. (Eventually, we should ensure that this 325 does not happen. Error messages involving these operators will 326 be confusing to users.) */ 327 328 operator_name_info [(int) INIT_EXPR].name 329 = operator_name_info [(int) MODIFY_EXPR].name; 330 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)"; 331 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)"; 332 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)"; 333 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)"; 334 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)"; 335 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)"; 336 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)"; 337 operator_name_info [(int) ABS_EXPR].name = "abs"; 338 operator_name_info [(int) FFS_EXPR].name = "ffs"; 339 operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~"; 340 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&"; 341 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||"; 342 operator_name_info [(int) IN_EXPR].name = "in"; 343 operator_name_info [(int) RANGE_EXPR].name = "..."; 344 operator_name_info [(int) CONVERT_EXPR].name = "+"; 345 346 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name 347 = "(exact /=)"; 348 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name 349 = "(ceiling /=)"; 350 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name 351 = "(floor /=)"; 352 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name 353 = "(round /=)"; 354 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name 355 = "(ceiling %=)"; 356 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name 357 = "(floor %=)"; 358 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name 359 = "(round %=)"; 360} 361 362/* The reserved keyword table. */ 363struct resword 364{ 365 const char *const word; 366 const ENUM_BITFIELD(rid) rid : 16; 367 const unsigned int disable : 16; 368}; 369 370/* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is 371 _true_. */ 372#define D_EXT 0x01 /* GCC extension */ 373#define D_ASM 0x02 /* in C99, but has a switch to turn it off */ 374#define D_OPNAME 0x04 /* operator names */ 375 376CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT); 377 378static const struct resword reswords[] = 379{ 380 { "_Complex", RID_COMPLEX, 0 }, 381 { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, 382 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, 383 { "__alignof", RID_ALIGNOF, 0 }, 384 { "__alignof__", RID_ALIGNOF, 0 }, 385 { "__asm", RID_ASM, 0 }, 386 { "__asm__", RID_ASM, 0 }, 387 { "__attribute", RID_ATTRIBUTE, 0 }, 388 { "__attribute__", RID_ATTRIBUTE, 0 }, 389 { "__builtin_va_arg", RID_VA_ARG, 0 }, 390 { "__complex", RID_COMPLEX, 0 }, 391 { "__complex__", RID_COMPLEX, 0 }, 392 { "__const", RID_CONST, 0 }, 393 { "__const__", RID_CONST, 0 }, 394 { "__extension__", RID_EXTENSION, 0 }, 395 { "__func__", RID_C99_FUNCTION_NAME, 0 }, 396 { "__imag", RID_IMAGPART, 0 }, 397 { "__imag__", RID_IMAGPART, 0 }, 398 { "__inline", RID_INLINE, 0 }, 399 { "__inline__", RID_INLINE, 0 }, 400 { "__label__", RID_LABEL, 0 }, 401 { "__null", RID_NULL, 0 }, 402 { "__real", RID_REALPART, 0 }, 403 { "__real__", RID_REALPART, 0 }, 404 { "__restrict", RID_RESTRICT, 0 }, 405 { "__restrict__", RID_RESTRICT, 0 }, 406 { "__signed", RID_SIGNED, 0 }, 407 { "__signed__", RID_SIGNED, 0 }, 408 { "__typeof", RID_TYPEOF, 0 }, 409 { "__typeof__", RID_TYPEOF, 0 }, 410 { "__volatile", RID_VOLATILE, 0 }, 411 { "__volatile__", RID_VOLATILE, 0 }, 412 { "asm", RID_ASM, D_ASM }, 413 { "and", RID_AND, D_OPNAME }, 414 { "and_eq", RID_AND_EQ, D_OPNAME }, 415 { "auto", RID_AUTO, 0 }, 416 { "bitand", RID_BITAND, D_OPNAME }, 417 { "bitor", RID_BITOR, D_OPNAME }, 418 { "bool", RID_BOOL, 0 }, 419 { "break", RID_BREAK, 0 }, 420 { "case", RID_CASE, 0 }, 421 { "catch", RID_CATCH, 0 }, 422 { "char", RID_CHAR, 0 }, 423 { "class", RID_CLASS, 0 }, 424 { "compl", RID_COMPL, D_OPNAME }, 425 { "const", RID_CONST, 0 }, 426 { "const_cast", RID_CONSTCAST, 0 }, 427 { "continue", RID_CONTINUE, 0 }, 428 { "default", RID_DEFAULT, 0 }, 429 { "delete", RID_DELETE, 0 }, 430 { "do", RID_DO, 0 }, 431 { "double", RID_DOUBLE, 0 }, 432 { "dynamic_cast", RID_DYNCAST, 0 }, 433 { "else", RID_ELSE, 0 }, 434 { "enum", RID_ENUM, 0 }, 435 { "explicit", RID_EXPLICIT, 0 }, 436 { "export", RID_EXPORT, 0 }, 437 { "extern", RID_EXTERN, 0 }, 438 { "false", RID_FALSE, 0 }, 439 { "float", RID_FLOAT, 0 }, 440 { "for", RID_FOR, 0 }, 441 { "friend", RID_FRIEND, 0 }, 442 { "goto", RID_GOTO, 0 }, 443 { "if", RID_IF, 0 }, 444 { "inline", RID_INLINE, 0 }, 445 { "int", RID_INT, 0 }, 446 { "long", RID_LONG, 0 }, 447 { "mutable", RID_MUTABLE, 0 }, 448 { "namespace", RID_NAMESPACE, 0 }, 449 { "new", RID_NEW, 0 }, 450 { "not", RID_NOT, D_OPNAME }, 451 { "not_eq", RID_NOT_EQ, D_OPNAME }, 452 { "operator", RID_OPERATOR, 0 }, 453 { "or", RID_OR, D_OPNAME }, 454 { "or_eq", RID_OR_EQ, D_OPNAME }, 455 { "private", RID_PRIVATE, 0 }, 456 { "protected", RID_PROTECTED, 0 }, 457 { "public", RID_PUBLIC, 0 }, 458 { "register", RID_REGISTER, 0 }, 459 { "reinterpret_cast", RID_REINTCAST, 0 }, 460 { "return", RID_RETURN, 0 }, 461 { "short", RID_SHORT, 0 }, 462 { "signed", RID_SIGNED, 0 }, 463 { "sizeof", RID_SIZEOF, 0 }, 464 { "static", RID_STATIC, 0 }, 465 { "static_cast", RID_STATCAST, 0 }, 466 { "struct", RID_STRUCT, 0 }, 467 { "switch", RID_SWITCH, 0 }, 468 { "template", RID_TEMPLATE, 0 }, 469 { "this", RID_THIS, 0 }, 470 { "throw", RID_THROW, 0 }, 471 { "true", RID_TRUE, 0 }, 472 { "try", RID_TRY, 0 }, 473 { "typedef", RID_TYPEDEF, 0 }, 474 { "typename", RID_TYPENAME, 0 }, 475 { "typeid", RID_TYPEID, 0 }, 476 { "typeof", RID_TYPEOF, D_ASM|D_EXT }, 477 { "union", RID_UNION, 0 }, 478 { "unsigned", RID_UNSIGNED, 0 }, 479 { "using", RID_USING, 0 }, 480 { "virtual", RID_VIRTUAL, 0 }, 481 { "void", RID_VOID, 0 }, 482 { "volatile", RID_VOLATILE, 0 }, 483 { "wchar_t", RID_WCHAR, 0 }, 484 { "while", RID_WHILE, 0 }, 485 { "xor", RID_XOR, D_OPNAME }, 486 { "xor_eq", RID_XOR_EQ, D_OPNAME }, 487 488}; 489#define N_reswords (sizeof reswords / sizeof (struct resword)) 490 491/* Table mapping from RID_* constants to yacc token numbers. 492 Unfortunately we have to have entries for all the keywords in all 493 three languages. */ 494const short rid_to_yy[RID_MAX] = 495{ 496 /* RID_STATIC */ SCSPEC, 497 /* RID_UNSIGNED */ TYPESPEC, 498 /* RID_LONG */ TYPESPEC, 499 /* RID_CONST */ CV_QUALIFIER, 500 /* RID_EXTERN */ SCSPEC, 501 /* RID_REGISTER */ SCSPEC, 502 /* RID_TYPEDEF */ SCSPEC, 503 /* RID_SHORT */ TYPESPEC, 504 /* RID_INLINE */ SCSPEC, 505 /* RID_VOLATILE */ CV_QUALIFIER, 506 /* RID_SIGNED */ TYPESPEC, 507 /* RID_AUTO */ SCSPEC, 508 /* RID_RESTRICT */ CV_QUALIFIER, 509 510 /* C extensions. Bounded pointers are not yet in C++ */ 511 /* RID_BOUNDED */ 0, 512 /* RID_UNBOUNDED */ 0, 513 /* RID_COMPLEX */ TYPESPEC, 514 515 /* C++ */ 516 /* RID_FRIEND */ SCSPEC, 517 /* RID_VIRTUAL */ SCSPEC, 518 /* RID_EXPLICIT */ SCSPEC, 519 /* RID_EXPORT */ EXPORT, 520 /* RID_MUTABLE */ SCSPEC, 521 522 /* ObjC */ 523 /* RID_IN */ 0, 524 /* RID_OUT */ 0, 525 /* RID_INOUT */ 0, 526 /* RID_BYCOPY */ 0, 527 /* RID_BYREF */ 0, 528 /* RID_ONEWAY */ 0, 529 530 /* C */ 531 /* RID_INT */ TYPESPEC, 532 /* RID_CHAR */ TYPESPEC, 533 /* RID_FLOAT */ TYPESPEC, 534 /* RID_DOUBLE */ TYPESPEC, 535 /* RID_VOID */ TYPESPEC, 536 /* RID_ENUM */ ENUM, 537 /* RID_STRUCT */ AGGR, 538 /* RID_UNION */ AGGR, 539 /* RID_IF */ IF, 540 /* RID_ELSE */ ELSE, 541 /* RID_WHILE */ WHILE, 542 /* RID_DO */ DO, 543 /* RID_FOR */ FOR, 544 /* RID_SWITCH */ SWITCH, 545 /* RID_CASE */ CASE, 546 /* RID_DEFAULT */ DEFAULT, 547 /* RID_BREAK */ BREAK, 548 /* RID_CONTINUE */ CONTINUE, 549 /* RID_RETURN */ RETURN_KEYWORD, 550 /* RID_GOTO */ GOTO, 551 /* RID_SIZEOF */ SIZEOF, 552 553 /* C extensions */ 554 /* RID_ASM */ ASM_KEYWORD, 555 /* RID_TYPEOF */ TYPEOF, 556 /* RID_ALIGNOF */ ALIGNOF, 557 /* RID_ATTRIBUTE */ ATTRIBUTE, 558 /* RID_VA_ARG */ VA_ARG, 559 /* RID_EXTENSION */ EXTENSION, 560 /* RID_IMAGPART */ IMAGPART, 561 /* RID_REALPART */ REALPART, 562 /* RID_LABEL */ LABEL, 563 /* RID_PTRBASE */ 0, 564 /* RID_PTREXTENT */ 0, 565 /* RID_PTRVALUE */ 0, 566 /* RID_CHOOSE_EXPR */ 0, 567 /* RID_TYPES_COMPATIBLE_P */ 0, 568 569 /* RID_FUNCTION_NAME */ VAR_FUNC_NAME, 570 /* RID_PRETTY_FUNCTION_NAME */ VAR_FUNC_NAME, 571 /* RID_c99_FUNCTION_NAME */ VAR_FUNC_NAME, 572 573 /* C++ */ 574 /* RID_BOOL */ TYPESPEC, 575 /* RID_WCHAR */ TYPESPEC, 576 /* RID_CLASS */ AGGR, 577 /* RID_PUBLIC */ VISSPEC, 578 /* RID_PRIVATE */ VISSPEC, 579 /* RID_PROTECTED */ VISSPEC, 580 /* RID_TEMPLATE */ TEMPLATE, 581 /* RID_NULL */ CONSTANT, 582 /* RID_CATCH */ CATCH, 583 /* RID_DELETE */ DELETE, 584 /* RID_FALSE */ CXX_FALSE, 585 /* RID_NAMESPACE */ NAMESPACE, 586 /* RID_NEW */ NEW, 587 /* RID_OPERATOR */ OPERATOR, 588 /* RID_THIS */ THIS, 589 /* RID_THROW */ THROW, 590 /* RID_TRUE */ CXX_TRUE, 591 /* RID_TRY */ TRY, 592 /* RID_TYPENAME */ TYPENAME_KEYWORD, 593 /* RID_TYPEID */ TYPEID, 594 /* RID_USING */ USING, 595 596 /* casts */ 597 /* RID_CONSTCAST */ CONST_CAST, 598 /* RID_DYNCAST */ DYNAMIC_CAST, 599 /* RID_REINTCAST */ REINTERPRET_CAST, 600 /* RID_STATCAST */ STATIC_CAST, 601 602 /* alternate spellings */ 603 /* RID_AND */ ANDAND, 604 /* RID_AND_EQ */ ASSIGN, 605 /* RID_NOT */ '!', 606 /* RID_NOT_EQ */ EQCOMPARE, 607 /* RID_OR */ OROR, 608 /* RID_OR_EQ */ ASSIGN, 609 /* RID_XOR */ '^', 610 /* RID_XOR_EQ */ ASSIGN, 611 /* RID_BITAND */ '&', 612 /* RID_BITOR */ '|', 613 /* RID_COMPL */ '~', 614 615 /* Objective C */ 616 /* RID_ID */ 0, 617 /* RID_AT_ENCODE */ 0, 618 /* RID_AT_END */ 0, 619 /* RID_AT_CLASS */ 0, 620 /* RID_AT_ALIAS */ 0, 621 /* RID_AT_DEFS */ 0, 622 /* RID_AT_PRIVATE */ 0, 623 /* RID_AT_PROTECTED */ 0, 624 /* RID_AT_PUBLIC */ 0, 625 /* RID_AT_PROTOCOL */ 0, 626 /* RID_AT_SELECTOR */ 0, 627 /* RID_AT_INTERFACE */ 0, 628 /* RID_AT_IMPLEMENTATION */ 0 629}; 630 631void 632init_reswords () 633{ 634 unsigned int i; 635 tree id; 636 int mask = ((flag_operator_names ? 0 : D_OPNAME) 637 | (flag_no_asm ? D_ASM : 0) 638 | (flag_no_gnu_keywords ? D_EXT : 0)); 639 640 /* It is not necessary to register ridpointers as a GC root, because 641 all the trees it points to are permanently interned in the 642 get_identifier hash anyway. */ 643 ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree)); 644 for (i = 0; i < N_reswords; i++) 645 { 646 id = get_identifier (reswords[i].word); 647 C_RID_CODE (id) = reswords[i].rid; 648 ridpointers [(int) reswords[i].rid] = id; 649 if (! (reswords[i].disable & mask)) 650 C_IS_RESERVED_WORD (id) = 1; 651 } 652} 653 654static void 655init_cp_pragma () 656{ 657 cpp_register_pragma (parse_in, 0, "vtable", handle_pragma_vtable); 658 cpp_register_pragma (parse_in, 0, "unit", handle_pragma_unit); 659 660 cpp_register_pragma (parse_in, 0, "interface", handle_pragma_interface); 661 cpp_register_pragma (parse_in, 0, "implementation", 662 handle_pragma_implementation); 663 664 cpp_register_pragma (parse_in, "GCC", "interface", handle_pragma_interface); 665 cpp_register_pragma (parse_in, "GCC", "implementation", 666 handle_pragma_implementation); 667 cpp_register_pragma (parse_in, "GCC", "java_exceptions", 668 handle_pragma_java_exceptions); 669} 670 671/* Initialize the C++ front end. This function is very sensitive to 672 the exact order that things are done here. It would be nice if the 673 initialization done by this routine were moved to its subroutines, 674 and the ordering dependencies clarified and reduced. */ 675const char * 676cxx_init (filename) 677 const char *filename; 678{ 679 decl_printable_name = lang_printable_name; 680 input_filename = "<internal>"; 681 682 init_reswords (); 683 init_spew (); 684 init_tree (); 685 init_cplus_expand (); 686 init_cp_semantics (); 687 688 add_c_tree_codes (); 689 690 memcpy (tree_code_type + (int) LAST_C_TREE_CODE, 691 cplus_tree_code_type, 692 (int)LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE); 693 memcpy (tree_code_length + (int) LAST_C_TREE_CODE, 694 cplus_tree_code_length, 695 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (int)); 696 memcpy (tree_code_name + (int) LAST_C_TREE_CODE, 697 cplus_tree_code_name, 698 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (char *)); 699 700 init_operators (); 701 init_method (); 702 init_error (); 703 704 current_function_decl = NULL; 705 706 class_type_node = build_int_2 (class_type, 0); 707 TREE_TYPE (class_type_node) = class_type_node; 708 ridpointers[(int) RID_CLASS] = class_type_node; 709 710 record_type_node = build_int_2 (record_type, 0); 711 TREE_TYPE (record_type_node) = record_type_node; 712 ridpointers[(int) RID_STRUCT] = record_type_node; 713 714 union_type_node = build_int_2 (union_type, 0); 715 TREE_TYPE (union_type_node) = union_type_node; 716 ridpointers[(int) RID_UNION] = union_type_node; 717 718 enum_type_node = build_int_2 (enum_type, 0); 719 TREE_TYPE (enum_type_node) = enum_type_node; 720 ridpointers[(int) RID_ENUM] = enum_type_node; 721 722 cxx_init_decl_processing (); 723 724 /* Create the built-in __null node. */ 725 null_node = build_int_2 (0, 0); 726 TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0); 727 ridpointers[RID_NULL] = null_node; 728 729 token_count = init_cpp_parse (); 730 interface_unknown = 1; 731 732 filename = c_common_init (filename); 733 734 init_cp_pragma (); 735 736 if (flag_gnu_xref) 737 GNU_xref_begin (filename); 738 init_repo (filename); 739 740 return filename; 741} 742 743inline void 744yyprint (file, yychar, yylval) 745 FILE *file; 746 int yychar; 747 YYSTYPE yylval; 748{ 749 tree t; 750 switch (yychar) 751 { 752 case IDENTIFIER: 753 case TYPENAME: 754 case TYPESPEC: 755 case PTYPENAME: 756 case PFUNCNAME: 757 case IDENTIFIER_DEFN: 758 case TYPENAME_DEFN: 759 case PTYPENAME_DEFN: 760 case SCSPEC: 761 case PRE_PARSED_CLASS_DECL: 762 t = yylval.ttype; 763 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL) 764 { 765 fprintf (file, " `%s'", IDENTIFIER_POINTER (DECL_NAME (t))); 766 break; 767 } 768 my_friendly_assert (TREE_CODE (t) == IDENTIFIER_NODE, 224); 769 if (IDENTIFIER_POINTER (t)) 770 fprintf (file, " `%s'", IDENTIFIER_POINTER (t)); 771 break; 772 773 case AGGR: 774 if (yylval.ttype == class_type_node) 775 fprintf (file, " `class'"); 776 else if (yylval.ttype == record_type_node) 777 fprintf (file, " `struct'"); 778 else if (yylval.ttype == union_type_node) 779 fprintf (file, " `union'"); 780 else if (yylval.ttype == enum_type_node) 781 fprintf (file, " `enum'"); 782 else 783 abort (); 784 break; 785 786 case CONSTANT: 787 t = yylval.ttype; 788 if (TREE_CODE (t) == INTEGER_CST) 789 fprintf (file, 790#if HOST_BITS_PER_WIDE_INT == 64 791#if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT 792 " 0x%x%016x", 793#else 794#if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG 795 " 0x%lx%016lx", 796#else 797 " 0x%llx%016llx", 798#endif 799#endif 800#else 801#if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT 802 " 0x%lx%08lx", 803#else 804 " 0x%x%08x", 805#endif 806#endif 807 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t)); 808 break; 809 } 810} 811 812#if defined(GATHER_STATISTICS) && defined(REDUCE_LENGTH) 813static int *reduce_count; 814#endif 815 816int *token_count; 817 818#if 0 819#define REDUCE_LENGTH (sizeof (yyr2) / sizeof (yyr2[0])) 820#define TOKEN_LENGTH (256 + sizeof (yytname) / sizeof (yytname[0])) 821#endif 822 823#ifdef GATHER_STATISTICS 824#ifdef REDUCE_LENGTH 825void 826yyhook (yyn) 827 int yyn; 828{ 829 reduce_count[yyn] += 1; 830} 831 832static int 833reduce_cmp (p, q) 834 int *p, *q; 835{ 836 return reduce_count[*q] - reduce_count[*p]; 837} 838 839static int 840token_cmp (p, q) 841 int *p, *q; 842{ 843 return token_count[*q] - token_count[*p]; 844} 845#endif 846#endif 847 848void 849print_parse_statistics () 850{ 851#ifdef GATHER_STATISTICS 852#ifdef REDUCE_LENGTH 853#if YYDEBUG != 0 854 int i; 855 int maxlen = REDUCE_LENGTH; 856 unsigned *sorted; 857 858 if (reduce_count[-1] == 0) 859 return; 860 861 if (TOKEN_LENGTH > REDUCE_LENGTH) 862 maxlen = TOKEN_LENGTH; 863 sorted = (unsigned *) alloca (sizeof (int) * maxlen); 864 865 for (i = 0; i < TOKEN_LENGTH; i++) 866 sorted[i] = i; 867 qsort (sorted, TOKEN_LENGTH, sizeof (int), token_cmp); 868 for (i = 0; i < TOKEN_LENGTH; i++) 869 { 870 int idx = sorted[i]; 871 if (token_count[idx] == 0) 872 break; 873 if (token_count[idx] < token_count[-1]) 874 break; 875 fprintf (stderr, "token %d, `%s', count = %d\n", 876 idx, yytname[YYTRANSLATE (idx)], token_count[idx]); 877 } 878 fprintf (stderr, "\n"); 879 for (i = 0; i < REDUCE_LENGTH; i++) 880 sorted[i] = i; 881 qsort (sorted, REDUCE_LENGTH, sizeof (int), reduce_cmp); 882 for (i = 0; i < REDUCE_LENGTH; i++) 883 { 884 int idx = sorted[i]; 885 if (reduce_count[idx] == 0) 886 break; 887 if (reduce_count[idx] < reduce_count[-1]) 888 break; 889 fprintf (stderr, "rule %d, line %d, count = %d\n", 890 idx, yyrline[idx], reduce_count[idx]); 891 } 892 fprintf (stderr, "\n"); 893#endif 894#endif 895#endif 896} 897 898/* Sets the value of the 'yydebug' variable to VALUE. 899 This is a function so we don't have to have YYDEBUG defined 900 in order to build the compiler. */ 901 902void 903cxx_set_yydebug (value) 904 int value; 905{ 906#if YYDEBUG != 0 907 extern int yydebug; 908 yydebug = value; 909#else 910 warning ("YYDEBUG not defined"); 911#endif 912} 913 914/* Helper function to load global variables with interface 915 information. */ 916 917void 918extract_interface_info () 919{ 920 struct c_fileinfo *finfo = 0; 921 922 if (flag_alt_external_templates) 923 { 924 tree til = tinst_for_decl (); 925 926 if (til) 927 finfo = get_fileinfo (TINST_FILE (til)); 928 } 929 if (!finfo) 930 finfo = get_fileinfo (input_filename); 931 932 interface_only = finfo->interface_only; 933 interface_unknown = finfo->interface_unknown; 934 935 /* This happens to be a convenient place to put this. */ 936 if (flag_gnu_xref) GNU_xref_file (input_filename); 937} 938 939/* Return nonzero if S is not considered part of an 940 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */ 941 942static int 943interface_strcmp (s) 944 const char *s; 945{ 946 /* Set the interface/implementation bits for this scope. */ 947 struct impl_files *ifiles; 948 const char *s1; 949 950 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next) 951 { 952 const char *t1 = ifiles->filename; 953 s1 = s; 954 955 if (*s1 != *t1 || *s1 == 0) 956 continue; 957 958 while (*s1 == *t1 && *s1 != 0) 959 s1++, t1++; 960 961 /* A match. */ 962 if (*s1 == *t1) 963 return 0; 964 965 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */ 966 if (strchr (s1, '.') || strchr (t1, '.')) 967 continue; 968 969 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.') 970 continue; 971 972 /* A match. */ 973 return 0; 974 } 975 976 /* No matches. */ 977 return 1; 978} 979 980/* Heuristic to tell whether the user is missing a semicolon 981 after a struct or enum declaration. Emit an error message 982 if we know the user has blown it. */ 983 984void 985check_for_missing_semicolon (type) 986 tree type; 987{ 988 if (yychar < 0) 989 yychar = yylex (); 990 991 if ((yychar > 255 992 && yychar != SCSPEC 993 && yychar != IDENTIFIER 994 && yychar != TYPENAME 995 && yychar != CV_QUALIFIER 996 && yychar != SELFNAME) 997 || yychar == 0 /* EOF */) 998 { 999 if (TYPE_ANONYMOUS_P (type)) 1000 error ("semicolon missing after %s declaration", 1001 TREE_CODE (type) == ENUMERAL_TYPE ? "enum" : "struct"); 1002 else 1003 error ("semicolon missing after declaration of `%T'", type); 1004 shadow_tag (build_tree_list (0, type)); 1005 } 1006 /* Could probably also hack cases where class { ... } f (); appears. */ 1007 clear_anon_tags (); 1008} 1009 1010void 1011note_got_semicolon (type) 1012 tree type; 1013{ 1014 if (!TYPE_P (type)) 1015 abort (); 1016 if (CLASS_TYPE_P (type)) 1017 CLASSTYPE_GOT_SEMICOLON (type) = 1; 1018} 1019 1020void 1021note_list_got_semicolon (declspecs) 1022 tree declspecs; 1023{ 1024 tree link; 1025 1026 for (link = declspecs; link; link = TREE_CHAIN (link)) 1027 { 1028 tree type = TREE_VALUE (link); 1029 if (type && TYPE_P (type)) 1030 note_got_semicolon (type); 1031 } 1032 clear_anon_tags (); 1033} 1034 1035 1036/* Parse a #pragma whose sole argument is a string constant. 1037 If OPT is true, the argument is optional. */ 1038static tree 1039parse_strconst_pragma (name, opt) 1040 const char *name; 1041 int opt; 1042{ 1043 tree result, x; 1044 enum cpp_ttype t; 1045 1046 t = c_lex (&x); 1047 if (t == CPP_STRING) 1048 { 1049 result = x; 1050 if (c_lex (&x) != CPP_EOF) 1051 warning ("junk at end of #pragma %s", name); 1052 return result; 1053 } 1054 1055 if (t == CPP_EOF && opt) 1056 return 0; 1057 1058 error ("invalid #pragma %s", name); 1059 return (tree)-1; 1060} 1061 1062static void 1063handle_pragma_vtable (dfile) 1064 cpp_reader *dfile ATTRIBUTE_UNUSED; 1065{ 1066 parse_strconst_pragma ("vtable", 0); 1067 sorry ("#pragma vtable no longer supported"); 1068} 1069 1070static void 1071handle_pragma_unit (dfile) 1072 cpp_reader *dfile ATTRIBUTE_UNUSED; 1073{ 1074 /* Validate syntax, but don't do anything. */ 1075 parse_strconst_pragma ("unit", 0); 1076} 1077 1078static void 1079handle_pragma_interface (dfile) 1080 cpp_reader *dfile ATTRIBUTE_UNUSED; 1081{ 1082 tree fname = parse_strconst_pragma ("interface", 1); 1083 struct c_fileinfo *finfo; 1084 const char *main_filename; 1085 1086 if (fname == (tree)-1) 1087 return; 1088 else if (fname == 0) 1089 main_filename = lbasename (input_filename); 1090 else 1091 main_filename = TREE_STRING_POINTER (fname); 1092 1093 finfo = get_fileinfo (input_filename); 1094 1095 if (impl_file_chain == 0) 1096 { 1097 /* If this is zero at this point, then we are 1098 auto-implementing. */ 1099 if (main_input_filename == 0) 1100 main_input_filename = input_filename; 1101 } 1102 1103 interface_only = interface_strcmp (main_filename); 1104#ifdef MULTIPLE_SYMBOL_SPACES 1105 if (! interface_only) 1106#endif 1107 interface_unknown = 0; 1108 1109 finfo->interface_only = interface_only; 1110 finfo->interface_unknown = interface_unknown; 1111} 1112 1113/* Note that we have seen a #pragma implementation for the key MAIN_FILENAME. 1114 We used to only allow this at toplevel, but that restriction was buggy 1115 in older compilers and it seems reasonable to allow it in the headers 1116 themselves, too. It only needs to precede the matching #p interface. 1117 1118 We don't touch interface_only or interface_unknown; the user must specify 1119 a matching #p interface for this to have any effect. */ 1120 1121static void 1122handle_pragma_implementation (dfile) 1123 cpp_reader *dfile ATTRIBUTE_UNUSED; 1124{ 1125 tree fname = parse_strconst_pragma ("implementation", 1); 1126 const char *main_filename; 1127 struct impl_files *ifiles = impl_file_chain; 1128 1129 if (fname == (tree)-1) 1130 return; 1131 1132 if (fname == 0) 1133 { 1134 if (main_input_filename) 1135 main_filename = main_input_filename; 1136 else 1137 main_filename = input_filename; 1138 main_filename = lbasename (main_filename); 1139 } 1140 else 1141 { 1142 main_filename = TREE_STRING_POINTER (fname); 1143 if (cpp_included (parse_in, main_filename)) 1144 warning ("#pragma implementation for %s appears after file is included", 1145 main_filename); 1146 } 1147 1148 for (; ifiles; ifiles = ifiles->next) 1149 { 1150 if (! strcmp (ifiles->filename, main_filename)) 1151 break; 1152 } 1153 if (ifiles == 0) 1154 { 1155 ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files)); 1156 ifiles->filename = main_filename; 1157 ifiles->next = impl_file_chain; 1158 impl_file_chain = ifiles; 1159 } 1160} 1161 1162/* Indicate that this file uses Java-personality exception handling. */ 1163static void 1164handle_pragma_java_exceptions (dfile) 1165 cpp_reader *dfile ATTRIBUTE_UNUSED; 1166{ 1167 tree x; 1168 if (c_lex (&x) != CPP_EOF) 1169 warning ("junk at end of #pragma GCC java_exceptions"); 1170 1171 choose_personality_routine (lang_java); 1172} 1173 1174void 1175do_pending_lang_change () 1176{ 1177 for (; pending_lang_change > 0; --pending_lang_change) 1178 push_lang_context (lang_name_c); 1179 for (; pending_lang_change < 0; ++pending_lang_change) 1180 pop_lang_context (); 1181} 1182 1183/* Return true if d is in a global scope. */ 1184 1185static int 1186is_global (d) 1187 tree d; 1188{ 1189 while (1) 1190 switch (TREE_CODE (d)) 1191 { 1192 case ERROR_MARK: 1193 return 1; 1194 1195 case OVERLOAD: d = OVL_FUNCTION (d); continue; 1196 case TREE_LIST: d = TREE_VALUE (d); continue; 1197 default: 1198 my_friendly_assert (DECL_P (d), 980629); 1199 1200 return DECL_NAMESPACE_SCOPE_P (d); 1201 } 1202} 1203 1204tree 1205do_identifier (token, parsing, args) 1206 register tree token; 1207 int parsing; 1208 tree args; 1209{ 1210 register tree id; 1211 int lexing = (parsing == 1); 1212 1213 if (! lexing) 1214 id = lookup_name (token, 0); 1215 else 1216 id = lastiddecl; 1217 1218 if (lexing && id && TREE_DEPRECATED (id)) 1219 warn_deprecated_use (id); 1220 1221 /* Do Koenig lookup if appropriate (inside templates we build lookup 1222 expressions instead). 1223 1224 [basic.lookup.koenig]: If the ordinary unqualified lookup of the name 1225 finds the declaration of a class member function, the associated 1226 namespaces and classes are not considered. */ 1227 1228 if (args && !current_template_parms && (!id || is_global (id))) 1229 id = lookup_arg_dependent (token, id, args); 1230 1231 /* Remember that this name has been used in the class definition, as per 1232 [class.scope0] */ 1233 if (id && parsing) 1234 maybe_note_name_used_in_class (token, id); 1235 1236 if (id == error_mark_node) 1237 { 1238 /* lookup_name quietly returns error_mark_node if we're parsing, 1239 as we don't want to complain about an identifier that ends up 1240 being used as a declarator. So we call it again to get the error 1241 message. */ 1242 id = lookup_name (token, 0); 1243 return error_mark_node; 1244 } 1245 1246 if (!id || (TREE_CODE (id) == FUNCTION_DECL 1247 && DECL_ANTICIPATED (id))) 1248 { 1249 if (current_template_parms) 1250 return build_min_nt (LOOKUP_EXPR, token); 1251 else if (IDENTIFIER_OPNAME_P (token)) 1252 { 1253 if (token != ansi_opname (ERROR_MARK)) 1254 error ("`%D' not defined", token); 1255 id = error_mark_node; 1256 } 1257 else if (current_function_decl == 0) 1258 { 1259 error ("`%D' was not declared in this scope", token); 1260 id = error_mark_node; 1261 } 1262 else 1263 { 1264 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node 1265 || IDENTIFIER_ERROR_LOCUS (token) != current_function_decl) 1266 { 1267 static int undeclared_variable_notice; 1268 1269 error ("`%D' undeclared (first use this function)", token); 1270 1271 if (! undeclared_variable_notice) 1272 { 1273 error ("(Each undeclared identifier is reported only once for each function it appears in.)"); 1274 undeclared_variable_notice = 1; 1275 } 1276 } 1277 id = error_mark_node; 1278 /* Prevent repeated error messages. */ 1279 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node); 1280 SET_IDENTIFIER_ERROR_LOCUS (token, current_function_decl); 1281 } 1282 } 1283 1284 if (TREE_CODE (id) == VAR_DECL && DECL_DEAD_FOR_LOCAL (id)) 1285 { 1286 tree shadowed = DECL_SHADOWED_FOR_VAR (id); 1287 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL 1288 && DECL_DEAD_FOR_LOCAL (shadowed)) 1289 shadowed = DECL_SHADOWED_FOR_VAR (shadowed); 1290 if (!shadowed) 1291 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (id)); 1292 if (shadowed) 1293 { 1294 if (!DECL_ERROR_REPORTED (id)) 1295 { 1296 warning ("name lookup of `%s' changed", 1297 IDENTIFIER_POINTER (token)); 1298 cp_warning_at (" matches this `%D' under ISO standard rules", 1299 shadowed); 1300 cp_warning_at (" matches this `%D' under old rules", id); 1301 DECL_ERROR_REPORTED (id) = 1; 1302 } 1303 id = shadowed; 1304 } 1305 else if (!DECL_ERROR_REPORTED (id)) 1306 { 1307 DECL_ERROR_REPORTED (id) = 1; 1308 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (id))) 1309 { 1310 error ("name lookup of `%s' changed for new ISO `for' scoping", 1311 IDENTIFIER_POINTER (token)); 1312 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", id); 1313 id = error_mark_node; 1314 } 1315 else 1316 { 1317 pedwarn ("name lookup of `%s' changed for new ISO `for' scoping", 1318 IDENTIFIER_POINTER (token)); 1319 cp_pedwarn_at (" using obsolete binding at `%D'", id); 1320 } 1321 } 1322 } 1323 /* TREE_USED is set in `hack_identifier'. */ 1324 if (TREE_CODE (id) == CONST_DECL) 1325 { 1326 /* Check access. */ 1327 if (IDENTIFIER_CLASS_VALUE (token) == id) 1328 enforce_access (CP_DECL_CONTEXT(id), id); 1329 if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id)) 1330 id = DECL_INITIAL (id); 1331 } 1332 else 1333 id = hack_identifier (id, token); 1334 1335 /* We must look up dependent names when the template is 1336 instantiated, not while parsing it. For now, we don't 1337 distinguish between dependent and independent names. So, for 1338 example, we look up all overloaded functions at 1339 instantiation-time, even though in some cases we should just use 1340 the DECL we have here. We also use LOOKUP_EXPRs to find things 1341 like local variables, rather than creating TEMPLATE_DECLs for the 1342 local variables and then finding matching instantiations. */ 1343 if (current_template_parms 1344 && (is_overloaded_fn (id) 1345 || (TREE_CODE (id) == VAR_DECL 1346 && CP_DECL_CONTEXT (id) 1347 && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL) 1348 || TREE_CODE (id) == PARM_DECL 1349 || TREE_CODE (id) == RESULT_DECL 1350 || TREE_CODE (id) == USING_DECL)) 1351 id = build_min_nt (LOOKUP_EXPR, token); 1352 1353 return id; 1354} 1355 1356tree 1357do_scoped_id (token, parsing) 1358 tree token; 1359 int parsing; 1360{ 1361 tree id; 1362 /* during parsing, this is ::name. Otherwise, it is black magic. */ 1363 if (parsing) 1364 { 1365 id = make_node (CPLUS_BINDING); 1366 if (!qualified_lookup_using_namespace (token, global_namespace, id, 0)) 1367 id = NULL_TREE; 1368 else 1369 id = BINDING_VALUE (id); 1370 } 1371 else 1372 id = IDENTIFIER_GLOBAL_VALUE (token); 1373 if (parsing && yychar == YYEMPTY) 1374 yychar = yylex (); 1375 if (! id) 1376 { 1377 if (processing_template_decl) 1378 { 1379 id = build_min_nt (LOOKUP_EXPR, token); 1380 LOOKUP_EXPR_GLOBAL (id) = 1; 1381 return id; 1382 } 1383 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node) 1384 error ("`::%D' undeclared (first use here)", token); 1385 id = error_mark_node; 1386 /* Prevent repeated error messages. */ 1387 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node); 1388 } 1389 else 1390 { 1391 if (TREE_CODE (id) == ADDR_EXPR) 1392 mark_used (TREE_OPERAND (id, 0)); 1393 else if (TREE_CODE (id) != OVERLOAD) 1394 mark_used (id); 1395 } 1396 if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl) 1397 { 1398 /* XXX CHS - should we set TREE_USED of the constant? */ 1399 id = DECL_INITIAL (id); 1400 /* This is to prevent an enum whose value is 0 1401 from being considered a null pointer constant. */ 1402 id = build1 (NOP_EXPR, TREE_TYPE (id), id); 1403 TREE_CONSTANT (id) = 1; 1404 } 1405 1406 if (processing_template_decl) 1407 { 1408 if (is_overloaded_fn (id)) 1409 { 1410 id = build_min_nt (LOOKUP_EXPR, token); 1411 LOOKUP_EXPR_GLOBAL (id) = 1; 1412 return id; 1413 } 1414 /* else just use the decl */ 1415 } 1416 return convert_from_reference (id); 1417} 1418 1419tree 1420identifier_typedecl_value (node) 1421 tree node; 1422{ 1423 tree t, type; 1424 type = IDENTIFIER_TYPE_VALUE (node); 1425 if (type == NULL_TREE) 1426 return NULL_TREE; 1427 1428 if (IDENTIFIER_BINDING (node)) 1429 { 1430 t = IDENTIFIER_VALUE (node); 1431 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type) 1432 return t; 1433 } 1434 if (IDENTIFIER_NAMESPACE_VALUE (node)) 1435 { 1436 t = IDENTIFIER_NAMESPACE_VALUE (node); 1437 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type) 1438 return t; 1439 } 1440 1441 /* Will this one ever happen? */ 1442 if (TYPE_MAIN_DECL (type)) 1443 return TYPE_MAIN_DECL (type); 1444 1445 /* We used to do an internal error of 62 here, but instead we will 1446 handle the return of a null appropriately in the callers. */ 1447 return NULL_TREE; 1448} 1449 1450#ifdef GATHER_STATISTICS 1451/* The original for tree_node_kind is in the toplevel tree.c; changes there 1452 need to be brought into here, unless this were actually put into a header 1453 instead. */ 1454/* Statistics-gathering stuff. */ 1455typedef enum 1456{ 1457 d_kind, 1458 t_kind, 1459 b_kind, 1460 s_kind, 1461 r_kind, 1462 e_kind, 1463 c_kind, 1464 id_kind, 1465 op_id_kind, 1466 perm_list_kind, 1467 temp_list_kind, 1468 vec_kind, 1469 x_kind, 1470 lang_decl, 1471 lang_type, 1472 all_kinds 1473} tree_node_kind; 1474 1475extern int tree_node_counts[]; 1476extern int tree_node_sizes[]; 1477#endif 1478 1479tree 1480build_lang_decl (code, name, type) 1481 enum tree_code code; 1482 tree name; 1483 tree type; 1484{ 1485 tree t; 1486 1487 t = build_decl (code, name, type); 1488 retrofit_lang_decl (t); 1489 1490 return t; 1491} 1492 1493/* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl 1494 and pushdecl (for functions generated by the backend). */ 1495 1496void 1497retrofit_lang_decl (t) 1498 tree t; 1499{ 1500 struct lang_decl *ld; 1501 size_t size; 1502 1503 if (CAN_HAVE_FULL_LANG_DECL_P (t)) 1504 size = sizeof (struct lang_decl); 1505 else 1506 size = sizeof (struct lang_decl_flags); 1507 1508 ld = (struct lang_decl *) ggc_alloc_cleared (size); 1509 1510 DECL_LANG_SPECIFIC (t) = ld; 1511 if (current_lang_name == lang_name_cplusplus) 1512 SET_DECL_LANGUAGE (t, lang_cplusplus); 1513 else if (current_lang_name == lang_name_c) 1514 SET_DECL_LANGUAGE (t, lang_c); 1515 else if (current_lang_name == lang_name_java) 1516 SET_DECL_LANGUAGE (t, lang_java); 1517 else abort (); 1518 1519#ifdef GATHER_STATISTICS 1520 tree_node_counts[(int)lang_decl] += 1; 1521 tree_node_sizes[(int)lang_decl] += size; 1522#endif 1523} 1524 1525void 1526copy_lang_decl (node) 1527 tree node; 1528{ 1529 int size; 1530 struct lang_decl *ld; 1531 1532 if (! DECL_LANG_SPECIFIC (node)) 1533 return; 1534 1535 if (!CAN_HAVE_FULL_LANG_DECL_P (node)) 1536 size = sizeof (struct lang_decl_flags); 1537 else 1538 size = sizeof (struct lang_decl); 1539 ld = (struct lang_decl *) ggc_alloc (size); 1540 memcpy (ld, DECL_LANG_SPECIFIC (node), size); 1541 DECL_LANG_SPECIFIC (node) = ld; 1542 1543#ifdef GATHER_STATISTICS 1544 tree_node_counts[(int)lang_decl] += 1; 1545 tree_node_sizes[(int)lang_decl] += size; 1546#endif 1547} 1548 1549/* Copy DECL, including any language-specific parts. */ 1550 1551tree 1552copy_decl (decl) 1553 tree decl; 1554{ 1555 tree copy; 1556 1557 copy = copy_node (decl); 1558 copy_lang_decl (copy); 1559 return copy; 1560} 1561 1562/* Replace the shared language-specific parts of NODE with a new copy. */ 1563 1564static void 1565copy_lang_type (node) 1566 tree node; 1567{ 1568 int size; 1569 struct lang_type *lt; 1570 1571 if (! TYPE_LANG_SPECIFIC (node)) 1572 return; 1573 1574 size = sizeof (struct lang_type); 1575 lt = (struct lang_type *) ggc_alloc (size); 1576 memcpy (lt, TYPE_LANG_SPECIFIC (node), size); 1577 TYPE_LANG_SPECIFIC (node) = lt; 1578 1579#ifdef GATHER_STATISTICS 1580 tree_node_counts[(int)lang_type] += 1; 1581 tree_node_sizes[(int)lang_type] += size; 1582#endif 1583} 1584 1585/* Copy TYPE, including any language-specific parts. */ 1586 1587tree 1588copy_type (type) 1589 tree type; 1590{ 1591 tree copy; 1592 1593 copy = copy_node (type); 1594 copy_lang_type (copy); 1595 return copy; 1596} 1597 1598tree 1599cp_make_lang_type (code) 1600 enum tree_code code; 1601{ 1602 register tree t = make_node (code); 1603 1604 /* Create lang_type structure. */ 1605 if (IS_AGGR_TYPE_CODE (code) 1606 || code == BOUND_TEMPLATE_TEMPLATE_PARM) 1607 { 1608 struct lang_type *pi; 1609 1610 pi = ((struct lang_type *) 1611 ggc_alloc_cleared (sizeof (struct lang_type))); 1612 1613 TYPE_LANG_SPECIFIC (t) = pi; 1614 1615#ifdef GATHER_STATISTICS 1616 tree_node_counts[(int)lang_type] += 1; 1617 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type); 1618#endif 1619 } 1620 1621 /* Set up some flags that give proper default behavior. */ 1622 if (IS_AGGR_TYPE_CODE (code)) 1623 { 1624 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown); 1625 CLASSTYPE_INTERFACE_ONLY (t) = interface_only; 1626 1627 /* Make sure this is laid out, for ease of use later. In the 1628 presence of parse errors, the normal was of assuring this 1629 might not ever get executed, so we lay it out *immediately*. */ 1630 build_pointer_type (t); 1631 } 1632 else 1633 /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But, 1634 TYPE_ALIAS_SET is initialized to -1 by default, so we must 1635 clear it here. */ 1636 TYPE_ALIAS_SET (t) = 0; 1637 1638 /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs 1639 since they can be virtual base types, and we then need a 1640 canonical binfo for them. Ideally, this would be done lazily for 1641 all types. */ 1642 if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM 1643 || code == BOUND_TEMPLATE_TEMPLATE_PARM 1644 || code == TYPENAME_TYPE) 1645 TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE); 1646 1647 return t; 1648} 1649 1650tree 1651make_aggr_type (code) 1652 enum tree_code code; 1653{ 1654 tree t = cp_make_lang_type (code); 1655 1656 if (IS_AGGR_TYPE_CODE (code)) 1657 SET_IS_AGGR_TYPE (t, 1); 1658 1659 return t; 1660} 1661 1662void 1663compiler_error VPARAMS ((const char *msg, ...)) 1664{ 1665 char buf[1024]; 1666 1667 VA_OPEN (ap, msg); 1668 VA_FIXEDARG (ap, const char *, msg); 1669 1670 vsprintf (buf, msg, ap); 1671 VA_CLOSE (ap); 1672 1673 error_with_file_and_line (input_filename, lineno, "%s (compiler error)", buf); 1674} 1675 1676/* Return the type-qualifier corresponding to the identifier given by 1677 RID. */ 1678 1679int 1680cp_type_qual_from_rid (rid) 1681 tree rid; 1682{ 1683 if (rid == ridpointers[(int) RID_CONST]) 1684 return TYPE_QUAL_CONST; 1685 else if (rid == ridpointers[(int) RID_VOLATILE]) 1686 return TYPE_QUAL_VOLATILE; 1687 else if (rid == ridpointers[(int) RID_RESTRICT]) 1688 return TYPE_QUAL_RESTRICT; 1689 1690 abort (); 1691 return TYPE_UNQUALIFIED; 1692} 1693