]>
Commit | Line | Data |
---|---|---|
471086d6 | 1 | /* Separate lexical analyzer for GNU C++. |
107c7f39 | 2 | Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
eb180587 | 3 | 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
471086d6 | 4 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
6f0d25a6 | 6 | This file is part of GCC. |
471086d6 | 7 | |
6f0d25a6 | 8 | GCC is free software; you can redistribute it and/or modify |
471086d6 | 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 | ||
6f0d25a6 | 13 | GCC is distributed in the hope that it will be useful, |
471086d6 | 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 | |
6f0d25a6 | 19 | along with GCC; see the file COPYING. If not, write to |
c58d4270 | 20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
471086d6 | 22 | |
23 | ||
24 | /* This file is the lexical analyzer for GNU C++. */ | |
25 | ||
39c8ac16 | 26 | #include "config.h" |
b3ef7553 | 27 | #include "system.h" |
805e22b2 | 28 | #include "coretypes.h" |
29 | #include "tm.h" | |
471086d6 | 30 | #include "input.h" |
31 | #include "tree.h" | |
471086d6 | 32 | #include "cp-tree.h" |
518796ad | 33 | #include "cpplib.h" |
988fc1d1 | 34 | #include "lex.h" |
471086d6 | 35 | #include "flags.h" |
917aa082 | 36 | #include "c-pragma.h" |
2a4e40b0 | 37 | #include "toplev.h" |
0ba25c9a | 38 | #include "output.h" |
d8c9779c | 39 | #include "tm_p.h" |
74d2af64 | 40 | #include "timevar.h" |
471086d6 | 41 | |
42c35e28 | 42 | static int interface_strcmp (const char *); |
43 | static void init_cp_pragma (void); | |
518796ad | 44 | |
42c35e28 | 45 | static tree parse_strconst_pragma (const char *, int); |
46 | static void handle_pragma_vtable (cpp_reader *); | |
47 | static void handle_pragma_unit (cpp_reader *); | |
48 | static void handle_pragma_interface (cpp_reader *); | |
49 | static void handle_pragma_implementation (cpp_reader *); | |
50 | static void handle_pragma_java_exceptions (cpp_reader *); | |
518796ad | 51 | |
42c35e28 | 52 | static void init_operators (void); |
53 | static void copy_lang_type (tree); | |
471086d6 | 54 | |
518796ad | 55 | /* A constraint that can be tested at compile time. */ |
518796ad | 56 | #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1] |
46e5e818 | 57 | |
23f83a9a | 58 | /* Functions and data structures for #pragma interface. |
59 | ||
60 | `#pragma implementation' means that the main file being compiled | |
61 | is considered to implement (provide) the classes that appear in | |
62 | its main body. I.e., if this is file "foo.cc", and class `bar' | |
63 | is defined in "foo.cc", then we say that "foo.cc implements bar". | |
64 | ||
65 | All main input files "implement" themselves automagically. | |
66 | ||
67 | `#pragma interface' means that unless this file (of the form "foo.h" | |
68 | is not presently being included by file "foo.cc", the | |
69 | CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none | |
70 | of the vtables nor any of the inline functions defined in foo.h | |
71 | will ever be output. | |
72 | ||
73 | There are cases when we want to link files such as "defs.h" and | |
74 | "main.cc". In this case, we give "defs.h" a `#pragma interface', | |
75 | and "main.cc" has `#pragma implementation "defs.h"'. */ | |
76 | ||
77 | struct impl_files | |
78 | { | |
44acf429 | 79 | const char *filename; |
23f83a9a | 80 | struct impl_files *next; |
81 | }; | |
82 | ||
83 | static struct impl_files *impl_file_chain; | |
84 | ||
471086d6 | 85 | \f |
86 | /* Return something to represent absolute declarators containing a *. | |
87 | TARGET is the absolute declarator that the * contains. | |
f9670f72 | 88 | CV_QUALIFIERS is a list of modifiers such as const or volatile |
471086d6 | 89 | to apply to the pointer type, represented as identifiers. |
90 | ||
91 | We return an INDIRECT_REF whose "contents" are TARGET | |
92 | and whose type is the modifier list. */ | |
93 | ||
94 | tree | |
42c35e28 | 95 | make_pointer_declarator (tree cv_qualifiers, tree target) |
471086d6 | 96 | { |
97 | if (target && TREE_CODE (target) == IDENTIFIER_NODE | |
98 | && ANON_AGGRNAME_P (target)) | |
905d4035 | 99 | error ("type name expected before `*'"); |
64ffa1a5 | 100 | target = build_nt (INDIRECT_REF, target); |
f9670f72 | 101 | TREE_TYPE (target) = cv_qualifiers; |
471086d6 | 102 | return target; |
103 | } | |
104 | ||
105 | /* Return something to represent absolute declarators containing a &. | |
106 | TARGET is the absolute declarator that the & contains. | |
f9670f72 | 107 | CV_QUALIFIERS is a list of modifiers such as const or volatile |
471086d6 | 108 | to apply to the reference type, represented as identifiers. |
109 | ||
110 | We return an ADDR_EXPR whose "contents" are TARGET | |
111 | and whose type is the modifier list. */ | |
3c3beda6 | 112 | |
471086d6 | 113 | tree |
42c35e28 | 114 | make_reference_declarator (tree cv_qualifiers, tree target) |
471086d6 | 115 | { |
64ffa1a5 | 116 | target = build_nt (ADDR_EXPR, target); |
f9670f72 | 117 | TREE_TYPE (target) = cv_qualifiers; |
471086d6 | 118 | return target; |
119 | } | |
f9670f72 | 120 | |
121 | tree | |
42c35e28 | 122 | make_call_declarator (tree target, tree parms, tree cv_qualifiers, |
123 | tree exception_specification) | |
f9670f72 | 124 | { |
64ffa1a5 | 125 | target = build_nt (CALL_EXPR, target, |
126 | tree_cons (parms, cv_qualifiers, NULL_TREE), | |
127 | /* The third operand is really RTL. We | |
128 | shouldn't put anything there. */ | |
129 | NULL_TREE); | |
f27113ad | 130 | CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification; |
f9670f72 | 131 | return target; |
132 | } | |
133 | ||
134 | void | |
42c35e28 | 135 | set_quals_and_spec (tree call_declarator, tree cv_qualifiers, |
136 | tree exception_specification) | |
f9670f72 | 137 | { |
f27113ad | 138 | CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers; |
139 | CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification; | |
f9670f72 | 140 | } |
471086d6 | 141 | \f |
471086d6 | 142 | int interface_only; /* whether or not current file is only for |
143 | interface definitions. */ | |
144 | int interface_unknown; /* whether or not we know this class | |
145 | to behave according to #pragma interface. */ | |
146 | ||
471086d6 | 147 | \f |
ca58dfe1 | 148 | void |
42c35e28 | 149 | cxx_finish (void) |
471086d6 | 150 | { |
cdc9fa3e | 151 | c_common_finish (); |
471086d6 | 152 | } |
153 | ||
97cc4539 | 154 | /* A mapping from tree codes to operator name information. */ |
155 | operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE]; | |
156 | /* Similar, but for assignment operators. */ | |
157 | operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE]; | |
3c3beda6 | 158 | |
97cc4539 | 159 | /* Initialize data structures that keep track of operator names. */ |
160 | ||
c5144efa | 161 | #define DEF_OPERATOR(NAME, C, M, AR, AP) \ |
518796ad | 162 | CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256); |
163 | #include "operators.def" | |
164 | #undef DEF_OPERATOR | |
165 | ||
97cc4539 | 166 | static void |
42c35e28 | 167 | init_operators (void) |
97cc4539 | 168 | { |
169 | tree identifier; | |
170 | char buffer[256]; | |
171 | struct operator_name_info_t *oni; | |
3c3beda6 | 172 | |
c5144efa | 173 | #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ |
0169d787 | 174 | sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \ |
97cc4539 | 175 | identifier = get_identifier (buffer); \ |
176 | IDENTIFIER_OPNAME_P (identifier) = 1; \ | |
177 | \ | |
178 | oni = (ASSN_P \ | |
179 | ? &assignment_operator_name_info[(int) CODE] \ | |
180 | : &operator_name_info[(int) CODE]); \ | |
181 | oni->identifier = identifier; \ | |
182 | oni->name = NAME; \ | |
94302392 | 183 | oni->mangled_name = MANGLING; \ |
184 | oni->arity = ARITY; | |
97cc4539 | 185 | |
186 | #include "operators.def" | |
187 | #undef DEF_OPERATOR | |
188 | ||
3c3beda6 | 189 | operator_name_info[(int) ERROR_MARK].identifier |
97cc4539 | 190 | = get_identifier ("<invalid operator>"); |
191 | ||
192 | /* Handle some special cases. These operators are not defined in | |
193 | the language, but can be produced internally. We may need them | |
194 | for error-reporting. (Eventually, we should ensure that this | |
195 | does not happen. Error messages involving these operators will | |
196 | be confusing to users.) */ | |
3c3beda6 | 197 | |
198 | operator_name_info [(int) INIT_EXPR].name | |
97cc4539 | 199 | = operator_name_info [(int) MODIFY_EXPR].name; |
200 | operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)"; | |
201 | operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)"; | |
202 | operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)"; | |
203 | operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)"; | |
204 | operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)"; | |
205 | operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)"; | |
206 | operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)"; | |
207 | operator_name_info [(int) ABS_EXPR].name = "abs"; | |
97cc4539 | 208 | operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&"; |
209 | operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||"; | |
210 | operator_name_info [(int) IN_EXPR].name = "in"; | |
211 | operator_name_info [(int) RANGE_EXPR].name = "..."; | |
212 | operator_name_info [(int) CONVERT_EXPR].name = "+"; | |
213 | ||
3c3beda6 | 214 | assignment_operator_name_info [(int) EXACT_DIV_EXPR].name |
97cc4539 | 215 | = "(exact /=)"; |
3c3beda6 | 216 | assignment_operator_name_info [(int) CEIL_DIV_EXPR].name |
97cc4539 | 217 | = "(ceiling /=)"; |
3c3beda6 | 218 | assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name |
97cc4539 | 219 | = "(floor /=)"; |
3c3beda6 | 220 | assignment_operator_name_info [(int) ROUND_DIV_EXPR].name |
97cc4539 | 221 | = "(round /=)"; |
3c3beda6 | 222 | assignment_operator_name_info [(int) CEIL_MOD_EXPR].name |
97cc4539 | 223 | = "(ceiling %=)"; |
3c3beda6 | 224 | assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name |
97cc4539 | 225 | = "(floor %=)"; |
3c3beda6 | 226 | assignment_operator_name_info [(int) ROUND_MOD_EXPR].name |
97cc4539 | 227 | = "(round %=)"; |
228 | } | |
229 | ||
518796ad | 230 | /* The reserved keyword table. */ |
231 | struct resword | |
471086d6 | 232 | { |
e99c3a1d | 233 | const char *const word; |
07e4a80b | 234 | ENUM_BITFIELD(rid) const rid : 16; |
e99c3a1d | 235 | const unsigned int disable : 16; |
518796ad | 236 | }; |
471086d6 | 237 | |
518796ad | 238 | /* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is |
239 | _true_. */ | |
240 | #define D_EXT 0x01 /* GCC extension */ | |
241 | #define D_ASM 0x02 /* in C99, but has a switch to turn it off */ | |
518796ad | 242 | |
243 | CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT); | |
244 | ||
245 | static const struct resword reswords[] = | |
246 | { | |
3b493511 | 247 | { "_Complex", RID_COMPLEX, 0 }, |
65b7f83f | 248 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
249 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
518796ad | 250 | { "__alignof", RID_ALIGNOF, 0 }, |
251 | { "__alignof__", RID_ALIGNOF, 0 }, | |
252 | { "__asm", RID_ASM, 0 }, | |
253 | { "__asm__", RID_ASM, 0 }, | |
254 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
255 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
256 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
257 | { "__complex", RID_COMPLEX, 0 }, | |
258 | { "__complex__", RID_COMPLEX, 0 }, | |
259 | { "__const", RID_CONST, 0 }, | |
260 | { "__const__", RID_CONST, 0 }, | |
261 | { "__extension__", RID_EXTENSION, 0 }, | |
65b7f83f | 262 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, |
518796ad | 263 | { "__imag", RID_IMAGPART, 0 }, |
264 | { "__imag__", RID_IMAGPART, 0 }, | |
265 | { "__inline", RID_INLINE, 0 }, | |
266 | { "__inline__", RID_INLINE, 0 }, | |
267 | { "__label__", RID_LABEL, 0 }, | |
268 | { "__null", RID_NULL, 0 }, | |
30b87eb6 | 269 | { "__offsetof", RID_OFFSETOF, 0 }, |
270 | { "__offsetof__", RID_OFFSETOF, 0 }, | |
518796ad | 271 | { "__real", RID_REALPART, 0 }, |
272 | { "__real__", RID_REALPART, 0 }, | |
273 | { "__restrict", RID_RESTRICT, 0 }, | |
274 | { "__restrict__", RID_RESTRICT, 0 }, | |
275 | { "__signed", RID_SIGNED, 0 }, | |
276 | { "__signed__", RID_SIGNED, 0 }, | |
e396cd54 | 277 | { "__thread", RID_THREAD, 0 }, |
518796ad | 278 | { "__typeof", RID_TYPEOF, 0 }, |
279 | { "__typeof__", RID_TYPEOF, 0 }, | |
280 | { "__volatile", RID_VOLATILE, 0 }, | |
281 | { "__volatile__", RID_VOLATILE, 0 }, | |
518796ad | 282 | { "asm", RID_ASM, D_ASM }, |
518796ad | 283 | { "auto", RID_AUTO, 0 }, |
518796ad | 284 | { "bool", RID_BOOL, 0 }, |
285 | { "break", RID_BREAK, 0 }, | |
286 | { "case", RID_CASE, 0 }, | |
287 | { "catch", RID_CATCH, 0 }, | |
288 | { "char", RID_CHAR, 0 }, | |
289 | { "class", RID_CLASS, 0 }, | |
518796ad | 290 | { "const", RID_CONST, 0 }, |
291 | { "const_cast", RID_CONSTCAST, 0 }, | |
292 | { "continue", RID_CONTINUE, 0 }, | |
293 | { "default", RID_DEFAULT, 0 }, | |
294 | { "delete", RID_DELETE, 0 }, | |
295 | { "do", RID_DO, 0 }, | |
296 | { "double", RID_DOUBLE, 0 }, | |
297 | { "dynamic_cast", RID_DYNCAST, 0 }, | |
298 | { "else", RID_ELSE, 0 }, | |
299 | { "enum", RID_ENUM, 0 }, | |
300 | { "explicit", RID_EXPLICIT, 0 }, | |
301 | { "export", RID_EXPORT, 0 }, | |
302 | { "extern", RID_EXTERN, 0 }, | |
303 | { "false", RID_FALSE, 0 }, | |
304 | { "float", RID_FLOAT, 0 }, | |
305 | { "for", RID_FOR, 0 }, | |
306 | { "friend", RID_FRIEND, 0 }, | |
307 | { "goto", RID_GOTO, 0 }, | |
308 | { "if", RID_IF, 0 }, | |
309 | { "inline", RID_INLINE, 0 }, | |
310 | { "int", RID_INT, 0 }, | |
311 | { "long", RID_LONG, 0 }, | |
312 | { "mutable", RID_MUTABLE, 0 }, | |
313 | { "namespace", RID_NAMESPACE, 0 }, | |
314 | { "new", RID_NEW, 0 }, | |
518796ad | 315 | { "operator", RID_OPERATOR, 0 }, |
518796ad | 316 | { "private", RID_PRIVATE, 0 }, |
317 | { "protected", RID_PROTECTED, 0 }, | |
318 | { "public", RID_PUBLIC, 0 }, | |
319 | { "register", RID_REGISTER, 0 }, | |
320 | { "reinterpret_cast", RID_REINTCAST, 0 }, | |
321 | { "return", RID_RETURN, 0 }, | |
322 | { "short", RID_SHORT, 0 }, | |
323 | { "signed", RID_SIGNED, 0 }, | |
324 | { "sizeof", RID_SIZEOF, 0 }, | |
325 | { "static", RID_STATIC, 0 }, | |
326 | { "static_cast", RID_STATCAST, 0 }, | |
327 | { "struct", RID_STRUCT, 0 }, | |
328 | { "switch", RID_SWITCH, 0 }, | |
329 | { "template", RID_TEMPLATE, 0 }, | |
330 | { "this", RID_THIS, 0 }, | |
331 | { "throw", RID_THROW, 0 }, | |
332 | { "true", RID_TRUE, 0 }, | |
333 | { "try", RID_TRY, 0 }, | |
334 | { "typedef", RID_TYPEDEF, 0 }, | |
335 | { "typename", RID_TYPENAME, 0 }, | |
336 | { "typeid", RID_TYPEID, 0 }, | |
337 | { "typeof", RID_TYPEOF, D_ASM|D_EXT }, | |
338 | { "union", RID_UNION, 0 }, | |
339 | { "unsigned", RID_UNSIGNED, 0 }, | |
340 | { "using", RID_USING, 0 }, | |
341 | { "virtual", RID_VIRTUAL, 0 }, | |
342 | { "void", RID_VOID, 0 }, | |
343 | { "volatile", RID_VOLATILE, 0 }, | |
3c3beda6 | 344 | { "wchar_t", RID_WCHAR, 0 }, |
518796ad | 345 | { "while", RID_WHILE, 0 }, |
5d830682 | 346 | |
518796ad | 347 | }; |
518796ad | 348 | |
9ceb1c29 | 349 | void |
42c35e28 | 350 | init_reswords (void) |
518796ad | 351 | { |
352 | unsigned int i; | |
353 | tree id; | |
5610f3ad | 354 | int mask = ((flag_no_asm ? D_ASM : 0) |
518796ad | 355 | | (flag_no_gnu_keywords ? D_EXT : 0)); |
356 | ||
6edf18a6 | 357 | ridpointers = ggc_calloc ((int) RID_MAX, sizeof (tree)); |
3585dac7 | 358 | for (i = 0; i < ARRAY_SIZE (reswords); i++) |
17fd67d5 | 359 | { |
518796ad | 360 | id = get_identifier (reswords[i].word); |
361 | C_RID_CODE (id) = reswords[i].rid; | |
362 | ridpointers [(int) reswords[i].rid] = id; | |
363 | if (! (reswords[i].disable & mask)) | |
364 | C_IS_RESERVED_WORD (id) = 1; | |
17fd67d5 | 365 | } |
518796ad | 366 | } |
17fd67d5 | 367 | |
518796ad | 368 | static void |
42c35e28 | 369 | init_cp_pragma (void) |
518796ad | 370 | { |
eb180587 | 371 | c_register_pragma (0, "vtable", handle_pragma_vtable); |
372 | c_register_pragma (0, "unit", handle_pragma_unit); | |
373 | c_register_pragma (0, "interface", handle_pragma_interface); | |
374 | c_register_pragma (0, "implementation", handle_pragma_implementation); | |
375 | c_register_pragma ("GCC", "interface", handle_pragma_interface); | |
376 | c_register_pragma ("GCC", "implementation", handle_pragma_implementation); | |
377 | c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions); | |
518796ad | 378 | } |
bac62436 | 379 | \f |
9ceb1c29 | 380 | /* Initialize the C++ front end. This function is very sensitive to |
381 | the exact order that things are done here. It would be nice if the | |
382 | initialization done by this routine were moved to its subroutines, | |
383 | and the ordering dependencies clarified and reduced. */ | |
03bde601 | 384 | bool |
385 | cxx_init (void) | |
518796ad | 386 | { |
bac62436 | 387 | static const enum tree_code stmt_codes[] = { |
388 | c_common_stmt_codes, | |
389 | cp_stmt_codes | |
390 | }; | |
391 | ||
392 | INIT_STATEMENT_CODES (stmt_codes); | |
393 | ||
0c6109e0 | 394 | /* We cannot just assign to input_filename because it has already |
395 | been initialized and will be used later as an N_BINCL for stabs+ | |
396 | debugging. */ | |
397 | push_srcloc ("<internal>", 0); | |
518796ad | 398 | |
399 | init_reswords (); | |
23f83a9a | 400 | init_tree (); |
41fb2c18 | 401 | init_cp_semantics (); |
97cc4539 | 402 | init_operators (); |
442fd60a | 403 | init_method (); |
471086d6 | 404 | init_error (); |
cfa70726 | 405 | |
471086d6 | 406 | current_function_decl = NULL; |
407 | ||
471086d6 | 408 | class_type_node = build_int_2 (class_type, 0); |
409 | TREE_TYPE (class_type_node) = class_type_node; | |
410 | ridpointers[(int) RID_CLASS] = class_type_node; | |
411 | ||
412 | record_type_node = build_int_2 (record_type, 0); | |
413 | TREE_TYPE (record_type_node) = record_type_node; | |
518796ad | 414 | ridpointers[(int) RID_STRUCT] = record_type_node; |
471086d6 | 415 | |
416 | union_type_node = build_int_2 (union_type, 0); | |
417 | TREE_TYPE (union_type_node) = union_type_node; | |
418 | ridpointers[(int) RID_UNION] = union_type_node; | |
419 | ||
420 | enum_type_node = build_int_2 (enum_type, 0); | |
421 | TREE_TYPE (enum_type_node) = enum_type_node; | |
422 | ridpointers[(int) RID_ENUM] = enum_type_node; | |
423 | ||
9ceb1c29 | 424 | cxx_init_decl_processing (); |
425 | ||
435fb09b | 426 | /* Create the built-in __null node. */ |
954885ed | 427 | null_node = build_int_2 (0, 0); |
771d21fa | 428 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); |
2739960c | 429 | ridpointers[RID_NULL] = null_node; |
ec10e4ad | 430 | |
471086d6 | 431 | interface_unknown = 1; |
8c463cf0 | 432 | |
03bde601 | 433 | if (c_common_init () == false) |
0c6109e0 | 434 | { |
435 | pop_srcloc(); | |
436 | return false; | |
437 | } | |
9ceb1c29 | 438 | |
439 | init_cp_pragma (); | |
440 | ||
03bde601 | 441 | init_repo (main_input_filename); |
9ceb1c29 | 442 | |
0c6109e0 | 443 | pop_srcloc(); |
03bde601 | 444 | return true; |
471086d6 | 445 | } |
471086d6 | 446 | \f |
471086d6 | 447 | /* Helper function to load global variables with interface |
448 | information. */ | |
96624a9e | 449 | |
471086d6 | 450 | void |
42c35e28 | 451 | extract_interface_info (void) |
471086d6 | 452 | { |
518796ad | 453 | struct c_fileinfo *finfo = 0; |
471086d6 | 454 | |
455 | if (flag_alt_external_templates) | |
456 | { | |
04d89d04 | 457 | tree til = tinst_for_decl (); |
3c3beda6 | 458 | |
471086d6 | 459 | if (til) |
518796ad | 460 | finfo = get_fileinfo (TINST_FILE (til)); |
471086d6 | 461 | } |
518796ad | 462 | if (!finfo) |
463 | finfo = get_fileinfo (input_filename); | |
464 | ||
465 | interface_only = finfo->interface_only; | |
466 | interface_unknown = finfo->interface_unknown; | |
471086d6 | 467 | } |
468 | ||
ac9386a0 | 469 | /* Return nonzero if S is not considered part of an |
471086d6 | 470 | INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */ |
96624a9e | 471 | |
471086d6 | 472 | static int |
42c35e28 | 473 | interface_strcmp (const char* s) |
471086d6 | 474 | { |
475 | /* Set the interface/implementation bits for this scope. */ | |
476 | struct impl_files *ifiles; | |
e1721763 | 477 | const char *s1; |
471086d6 | 478 | |
471086d6 | 479 | for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next) |
480 | { | |
e1721763 | 481 | const char *t1 = ifiles->filename; |
471086d6 | 482 | s1 = s; |
483 | ||
484 | if (*s1 != *t1 || *s1 == 0) | |
485 | continue; | |
486 | ||
487 | while (*s1 == *t1 && *s1 != 0) | |
488 | s1++, t1++; | |
489 | ||
490 | /* A match. */ | |
491 | if (*s1 == *t1) | |
492 | return 0; | |
493 | ||
494 | /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */ | |
78dbff7c | 495 | if (strchr (s1, '.') || strchr (t1, '.')) |
471086d6 | 496 | continue; |
497 | ||
498 | if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.') | |
499 | continue; | |
500 | ||
501 | /* A match. */ | |
502 | return 0; | |
503 | } | |
504 | ||
505 | /* No matches. */ | |
506 | return 1; | |
507 | } | |
508 | ||
518796ad | 509 | \f |
510 | ||
511 | /* Parse a #pragma whose sole argument is a string constant. | |
512 | If OPT is true, the argument is optional. */ | |
513 | static tree | |
42c35e28 | 514 | parse_strconst_pragma (const char* name, int opt) |
518796ad | 515 | { |
516 | tree result, x; | |
517 | enum cpp_ttype t; | |
518 | ||
519 | t = c_lex (&x); | |
520 | if (t == CPP_STRING) | |
521 | { | |
522 | result = x; | |
523 | if (c_lex (&x) != CPP_EOF) | |
524 | warning ("junk at end of #pragma %s", name); | |
525 | return result; | |
526 | } | |
527 | ||
528 | if (t == CPP_EOF && opt) | |
529 | return 0; | |
530 | ||
531 | error ("invalid #pragma %s", name); | |
532 | return (tree)-1; | |
533 | } | |
3c3beda6 | 534 | |
518796ad | 535 | static void |
42c35e28 | 536 | handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED ) |
518796ad | 537 | { |
b53db2b0 | 538 | parse_strconst_pragma ("vtable", 0); |
539 | sorry ("#pragma vtable no longer supported"); | |
518796ad | 540 | } |
541 | ||
495d2af2 | 542 | static void |
42c35e28 | 543 | handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED ) |
495d2af2 | 544 | { |
518796ad | 545 | /* Validate syntax, but don't do anything. */ |
546 | parse_strconst_pragma ("unit", 0); | |
547 | } | |
548 | ||
549 | static void | |
42c35e28 | 550 | handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED ) |
518796ad | 551 | { |
552 | tree fname = parse_strconst_pragma ("interface", 1); | |
553 | struct c_fileinfo *finfo; | |
554 | const char *main_filename; | |
555 | ||
556 | if (fname == (tree)-1) | |
557 | return; | |
558 | else if (fname == 0) | |
40275504 | 559 | main_filename = lbasename (input_filename); |
518796ad | 560 | else |
561 | main_filename = TREE_STRING_POINTER (fname); | |
562 | ||
563 | finfo = get_fileinfo (input_filename); | |
495d2af2 | 564 | |
565 | if (impl_file_chain == 0) | |
566 | { | |
567 | /* If this is zero at this point, then we are | |
568 | auto-implementing. */ | |
569 | if (main_input_filename == 0) | |
570 | main_input_filename = input_filename; | |
495d2af2 | 571 | } |
572 | ||
573 | interface_only = interface_strcmp (main_filename); | |
574 | #ifdef MULTIPLE_SYMBOL_SPACES | |
575 | if (! interface_only) | |
518796ad | 576 | #endif |
495d2af2 | 577 | interface_unknown = 0; |
518796ad | 578 | |
579 | finfo->interface_only = interface_only; | |
580 | finfo->interface_unknown = interface_unknown; | |
495d2af2 | 581 | } |
582 | ||
e060fd61 | 583 | /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME. |
584 | We used to only allow this at toplevel, but that restriction was buggy | |
585 | in older compilers and it seems reasonable to allow it in the headers | |
586 | themselves, too. It only needs to precede the matching #p interface. | |
587 | ||
588 | We don't touch interface_only or interface_unknown; the user must specify | |
589 | a matching #p interface for this to have any effect. */ | |
590 | ||
495d2af2 | 591 | static void |
42c35e28 | 592 | handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED ) |
495d2af2 | 593 | { |
518796ad | 594 | tree fname = parse_strconst_pragma ("implementation", 1); |
595 | const char *main_filename; | |
e060fd61 | 596 | struct impl_files *ifiles = impl_file_chain; |
518796ad | 597 | |
598 | if (fname == (tree)-1) | |
599 | return; | |
600 | ||
601 | if (fname == 0) | |
602 | { | |
603 | if (main_input_filename) | |
604 | main_filename = main_input_filename; | |
605 | else | |
606 | main_filename = input_filename; | |
40275504 | 607 | main_filename = lbasename (main_filename); |
518796ad | 608 | } |
609 | else | |
610 | { | |
611 | main_filename = TREE_STRING_POINTER (fname); | |
9751c00e | 612 | if (cpp_included (parse_in, main_filename)) |
518796ad | 613 | warning ("#pragma implementation for %s appears after file is included", |
614 | main_filename); | |
518796ad | 615 | } |
616 | ||
e060fd61 | 617 | for (; ifiles; ifiles = ifiles->next) |
495d2af2 | 618 | { |
e060fd61 | 619 | if (! strcmp (ifiles->filename, main_filename)) |
620 | break; | |
495d2af2 | 621 | } |
e060fd61 | 622 | if (ifiles == 0) |
495d2af2 | 623 | { |
6edf18a6 | 624 | ifiles = xmalloc (sizeof (struct impl_files)); |
ec6cb94a | 625 | ifiles->filename = main_filename; |
e060fd61 | 626 | ifiles->next = impl_file_chain; |
627 | impl_file_chain = ifiles; | |
495d2af2 | 628 | } |
495d2af2 | 629 | } |
53137e6a | 630 | |
6df8a9dd | 631 | /* Indicate that this file uses Java-personality exception handling. */ |
632 | static void | |
42c35e28 | 633 | handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED ) |
6df8a9dd | 634 | { |
635 | tree x; | |
636 | if (c_lex (&x) != CPP_EOF) | |
637 | warning ("junk at end of #pragma GCC java_exceptions"); | |
638 | ||
639 | choose_personality_routine (lang_java); | |
640 | } | |
641 | ||
7d172fa8 | 642 | /* Issue an error message indicating that the lookup of NAME (an |
0886adbc | 643 | IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */ |
7d172fa8 | 644 | |
0886adbc | 645 | tree |
7d172fa8 | 646 | unqualified_name_lookup_error (tree name) |
647 | { | |
648 | if (IDENTIFIER_OPNAME_P (name)) | |
649 | { | |
650 | if (name != ansi_opname (ERROR_MARK)) | |
651 | error ("`%D' not defined", name); | |
652 | } | |
653 | else if (current_function_decl == 0) | |
654 | error ("`%D' was not declared in this scope", name); | |
655 | else | |
656 | { | |
657 | if (IDENTIFIER_NAMESPACE_VALUE (name) != error_mark_node | |
658 | || IDENTIFIER_ERROR_LOCUS (name) != current_function_decl) | |
659 | { | |
660 | static int undeclared_variable_notice; | |
661 | ||
662 | error ("`%D' undeclared (first use this function)", name); | |
663 | ||
664 | if (! undeclared_variable_notice) | |
665 | { | |
666 | error ("(Each undeclared identifier is reported only once for each function it appears in.)"); | |
667 | undeclared_variable_notice = 1; | |
668 | } | |
669 | } | |
670 | /* Prevent repeated error messages. */ | |
671 | SET_IDENTIFIER_NAMESPACE_VALUE (name, error_mark_node); | |
672 | SET_IDENTIFIER_ERROR_LOCUS (name, current_function_decl); | |
673 | } | |
c324ed63 | 674 | |
0886adbc | 675 | return error_mark_node; |
e857e9c7 | 676 | } |
677 | ||
0886adbc | 678 | /* Like unqualified_name_lookup_error, but NAME is an unqualified-id |
679 | used as a function. Returns an appropriate expression for | |
680 | NAME. */ | |
681 | ||
e857e9c7 | 682 | tree |
0886adbc | 683 | unqualified_fn_lookup_error (tree name) |
e857e9c7 | 684 | { |
3cc0b4b9 | 685 | if (processing_template_decl) |
e857e9c7 | 686 | { |
0886adbc | 687 | /* In a template, it is invalid to write "f()" or "f(3)" if no |
688 | declaration of "f" is available. Historically, G++ and most | |
8ddbdfab | 689 | other compilers accepted that usage since they deferred all name |
690 | lookup until instantiation time rather than doing unqualified | |
691 | name lookup at template definition time; explain to the user what | |
692 | is going wrong. | |
693 | ||
694 | Note that we have the exact wording of the following message in | |
695 | the manual (trouble.texi, node "Name lookup"), so they need to | |
696 | be kept in synch. */ | |
c08d51be | 697 | pedwarn ("there are no arguments to `%D' that depend on a template " |
698 | "parameter, so a declaration of `%D' must be available", | |
699 | name, name); | |
0886adbc | 700 | |
701 | if (!flag_permissive) | |
e857e9c7 | 702 | { |
0886adbc | 703 | static bool hint; |
704 | if (!hint) | |
705 | { | |
706 | error ("(if you use `-fpermissive', G++ will accept your code, " | |
707 | "but allowing the use of an undeclared name is " | |
708 | "deprecated)"); | |
709 | hint = true; | |
710 | } | |
e857e9c7 | 711 | } |
c08d51be | 712 | return name; |
e857e9c7 | 713 | } |
471086d6 | 714 | |
0886adbc | 715 | return unqualified_name_lookup_error (name); |
471086d6 | 716 | } |
717 | ||
471086d6 | 718 | tree |
42c35e28 | 719 | build_lang_decl (enum tree_code code, tree name, tree type) |
471086d6 | 720 | { |
c4802aa6 | 721 | tree t; |
722 | ||
c4802aa6 | 723 | t = build_decl (code, name, type); |
4ba5f733 | 724 | retrofit_lang_decl (t); |
c4802aa6 | 725 | |
4ba5f733 | 726 | return t; |
727 | } | |
728 | ||
729 | /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl | |
730 | and pushdecl (for functions generated by the backend). */ | |
731 | ||
732 | void | |
42c35e28 | 733 | retrofit_lang_decl (tree t) |
4ba5f733 | 734 | { |
70a658bd | 735 | struct lang_decl *ld; |
c4802aa6 | 736 | size_t size; |
471086d6 | 737 | |
c4802aa6 | 738 | if (CAN_HAVE_FULL_LANG_DECL_P (t)) |
739 | size = sizeof (struct lang_decl); | |
740 | else | |
741 | size = sizeof (struct lang_decl_flags); | |
c6138f2d | 742 | |
6edf18a6 | 743 | ld = ggc_alloc_cleared (size); |
471086d6 | 744 | |
1f3233d1 | 745 | ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0; |
746 | ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0; | |
747 | ld->decl_flags.u2sel = 0; | |
748 | if (ld->decl_flags.can_be_full) | |
749 | ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0; | |
750 | ||
70a658bd | 751 | DECL_LANG_SPECIFIC (t) = ld; |
471086d6 | 752 | if (current_lang_name == lang_name_cplusplus) |
4b1984f5 | 753 | SET_DECL_LANGUAGE (t, lang_cplusplus); |
471086d6 | 754 | else if (current_lang_name == lang_name_c) |
4b1984f5 | 755 | SET_DECL_LANGUAGE (t, lang_c); |
f468434d | 756 | else if (current_lang_name == lang_name_java) |
4b1984f5 | 757 | SET_DECL_LANGUAGE (t, lang_java); |
523ac844 | 758 | else abort (); |
471086d6 | 759 | |
471086d6 | 760 | #ifdef GATHER_STATISTICS |
761 | tree_node_counts[(int)lang_decl] += 1; | |
c4802aa6 | 762 | tree_node_sizes[(int)lang_decl] += size; |
471086d6 | 763 | #endif |
471086d6 | 764 | } |
765 | ||
471086d6 | 766 | void |
42c35e28 | 767 | cxx_dup_lang_specific_decl (tree node) |
471086d6 | 768 | { |
769 | int size; | |
a1b0969c | 770 | struct lang_decl *ld; |
471086d6 | 771 | |
e857e9c7 | 772 | if (! DECL_LANG_SPECIFIC (node)) |
773 | return; | |
774 | ||
c6138f2d | 775 | if (!CAN_HAVE_FULL_LANG_DECL_P (node)) |
471086d6 | 776 | size = sizeof (struct lang_decl_flags); |
777 | else | |
778 | size = sizeof (struct lang_decl); | |
6edf18a6 | 779 | ld = ggc_alloc (size); |
b1b63592 | 780 | memcpy (ld, DECL_LANG_SPECIFIC (node), size); |
a1b0969c | 781 | DECL_LANG_SPECIFIC (node) = ld; |
1d36b416 | 782 | |
783 | #ifdef GATHER_STATISTICS | |
784 | tree_node_counts[(int)lang_decl] += 1; | |
785 | tree_node_sizes[(int)lang_decl] += size; | |
786 | #endif | |
471086d6 | 787 | } |
788 | ||
c771ec7a | 789 | /* Copy DECL, including any language-specific parts. */ |
790 | ||
791 | tree | |
42c35e28 | 792 | copy_decl (tree decl) |
c771ec7a | 793 | { |
794 | tree copy; | |
795 | ||
796 | copy = copy_node (decl); | |
dbc42b78 | 797 | cxx_dup_lang_specific_decl (copy); |
c771ec7a | 798 | return copy; |
799 | } | |
800 | ||
1d36b416 | 801 | /* Replace the shared language-specific parts of NODE with a new copy. */ |
802 | ||
f6020ad0 | 803 | static void |
42c35e28 | 804 | copy_lang_type (tree node) |
1d36b416 | 805 | { |
806 | int size; | |
807 | struct lang_type *lt; | |
808 | ||
809 | if (! TYPE_LANG_SPECIFIC (node)) | |
810 | return; | |
811 | ||
1f3233d1 | 812 | if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class) |
813 | size = sizeof (struct lang_type); | |
814 | else | |
815 | size = sizeof (struct lang_type_ptrmem); | |
6edf18a6 | 816 | lt = ggc_alloc (size); |
1d36b416 | 817 | memcpy (lt, TYPE_LANG_SPECIFIC (node), size); |
818 | TYPE_LANG_SPECIFIC (node) = lt; | |
819 | ||
820 | #ifdef GATHER_STATISTICS | |
821 | tree_node_counts[(int)lang_type] += 1; | |
822 | tree_node_sizes[(int)lang_type] += size; | |
823 | #endif | |
824 | } | |
825 | ||
826 | /* Copy TYPE, including any language-specific parts. */ | |
827 | ||
828 | tree | |
42c35e28 | 829 | copy_type (tree type) |
1d36b416 | 830 | { |
831 | tree copy; | |
832 | ||
833 | copy = copy_node (type); | |
834 | copy_lang_type (copy); | |
835 | return copy; | |
836 | } | |
837 | ||
471086d6 | 838 | tree |
42c35e28 | 839 | cxx_make_type (enum tree_code code) |
471086d6 | 840 | { |
cd16867a | 841 | tree t = make_node (code); |
471086d6 | 842 | |
1d36b416 | 843 | /* Create lang_type structure. */ |
844 | if (IS_AGGR_TYPE_CODE (code) | |
845 | || code == BOUND_TEMPLATE_TEMPLATE_PARM) | |
e4e283ec | 846 | { |
dc125c61 | 847 | struct lang_type *pi; |
848 | ||
6edf18a6 | 849 | pi = ggc_alloc_cleared (sizeof (struct lang_type)); |
471086d6 | 850 | |
dc125c61 | 851 | TYPE_LANG_SPECIFIC (t) = pi; |
1f3233d1 | 852 | pi->u.c.h.is_lang_type_class = 1; |
1d36b416 | 853 | |
854 | #ifdef GATHER_STATISTICS | |
855 | tree_node_counts[(int)lang_type] += 1; | |
856 | tree_node_sizes[(int)lang_type] += sizeof (struct lang_type); | |
857 | #endif | |
858 | } | |
859 | ||
860 | /* Set up some flags that give proper default behavior. */ | |
861 | if (IS_AGGR_TYPE_CODE (code)) | |
862 | { | |
e4e283ec | 863 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown); |
864 | CLASSTYPE_INTERFACE_ONLY (t) = interface_only; | |
471086d6 | 865 | |
e4e283ec | 866 | /* Make sure this is laid out, for ease of use later. In the |
867 | presence of parse errors, the normal was of assuring this | |
868 | might not ever get executed, so we lay it out *immediately*. */ | |
869 | build_pointer_type (t); | |
e4e283ec | 870 | } |
871 | else | |
872 | /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But, | |
873 | TYPE_ALIAS_SET is initialized to -1 by default, so we must | |
874 | clear it here. */ | |
875 | TYPE_ALIAS_SET (t) = 0; | |
471086d6 | 876 | |
4a8caefa | 877 | /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs |
3ac23ebc | 878 | since they can be virtual base types, and we then need a |
879 | canonical binfo for them. Ideally, this would be done lazily for | |
880 | all types. */ | |
1d36b416 | 881 | if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM |
882 | || code == BOUND_TEMPLATE_TEMPLATE_PARM | |
883 | || code == TYPENAME_TYPE) | |
902de8ed | 884 | TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE); |
3ac23ebc | 885 | |
471086d6 | 886 | return t; |
887 | } | |
888 | ||
9972f65e | 889 | tree |
42c35e28 | 890 | make_aggr_type (enum tree_code code) |
9972f65e | 891 | { |
a1f71e15 | 892 | tree t = cxx_make_type (code); |
9972f65e | 893 | |
894 | if (IS_AGGR_TYPE_CODE (code)) | |
895 | SET_IS_AGGR_TYPE (t, 1); | |
896 | ||
897 | return t; | |
898 | } | |
899 | ||
3e04bd45 | 900 | /* Return the type-qualifier corresponding to the identifier given by |
901 | RID. */ | |
902 | ||
903 | int | |
42c35e28 | 904 | cp_type_qual_from_rid (tree rid) |
3e04bd45 | 905 | { |
906 | if (rid == ridpointers[(int) RID_CONST]) | |
907 | return TYPE_QUAL_CONST; | |
908 | else if (rid == ridpointers[(int) RID_VOLATILE]) | |
909 | return TYPE_QUAL_VOLATILE; | |
910 | else if (rid == ridpointers[(int) RID_RESTRICT]) | |
911 | return TYPE_QUAL_RESTRICT; | |
912 | ||
523ac844 | 913 | abort (); |
3e04bd45 | 914 | return TYPE_UNQUALIFIED; |
915 | } |