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