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