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