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