1*e4b17023SJohn Marino /* Separate lexical analyzer for GNU C++.
2*e4b17023SJohn Marino Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*e4b17023SJohn Marino 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2010
4*e4b17023SJohn Marino Free Software Foundation, Inc.
5*e4b17023SJohn Marino Hacked by Michael Tiemann (tiemann@cygnus.com)
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino This file is part of GCC.
8*e4b17023SJohn Marino
9*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
10*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
11*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
12*e4b17023SJohn Marino any later version.
13*e4b17023SJohn Marino
14*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
15*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
16*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17*e4b17023SJohn Marino GNU General Public License for more details.
18*e4b17023SJohn Marino
19*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
20*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
21*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
22*e4b17023SJohn Marino
23*e4b17023SJohn Marino
24*e4b17023SJohn Marino /* This file is the lexical analyzer for GNU C++. */
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino #include "config.h"
27*e4b17023SJohn Marino #include "system.h"
28*e4b17023SJohn Marino #include "coretypes.h"
29*e4b17023SJohn Marino #include "tm.h"
30*e4b17023SJohn Marino #include "input.h"
31*e4b17023SJohn Marino #include "tree.h"
32*e4b17023SJohn Marino #include "cp-tree.h"
33*e4b17023SJohn Marino #include "cpplib.h"
34*e4b17023SJohn Marino #include "flags.h"
35*e4b17023SJohn Marino #include "c-family/c-pragma.h"
36*e4b17023SJohn Marino #include "c-family/c-objc.h"
37*e4b17023SJohn Marino #include "output.h"
38*e4b17023SJohn Marino #include "tm_p.h"
39*e4b17023SJohn Marino #include "timevar.h"
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino static int interface_strcmp (const char *);
42*e4b17023SJohn Marino static void init_cp_pragma (void);
43*e4b17023SJohn Marino
44*e4b17023SJohn Marino static tree parse_strconst_pragma (const char *, int);
45*e4b17023SJohn Marino static void handle_pragma_vtable (cpp_reader *);
46*e4b17023SJohn Marino static void handle_pragma_unit (cpp_reader *);
47*e4b17023SJohn Marino static void handle_pragma_interface (cpp_reader *);
48*e4b17023SJohn Marino static void handle_pragma_implementation (cpp_reader *);
49*e4b17023SJohn Marino static void handle_pragma_java_exceptions (cpp_reader *);
50*e4b17023SJohn Marino
51*e4b17023SJohn Marino static void init_operators (void);
52*e4b17023SJohn Marino static void copy_lang_type (tree);
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino /* A constraint that can be tested at compile time. */
55*e4b17023SJohn Marino #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
56*e4b17023SJohn Marino
57*e4b17023SJohn Marino /* Functions and data structures for #pragma interface.
58*e4b17023SJohn Marino
59*e4b17023SJohn Marino `#pragma implementation' means that the main file being compiled
60*e4b17023SJohn Marino is considered to implement (provide) the classes that appear in
61*e4b17023SJohn Marino its main body. I.e., if this is file "foo.cc", and class `bar'
62*e4b17023SJohn Marino is defined in "foo.cc", then we say that "foo.cc implements bar".
63*e4b17023SJohn Marino
64*e4b17023SJohn Marino All main input files "implement" themselves automagically.
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino `#pragma interface' means that unless this file (of the form "foo.h"
67*e4b17023SJohn Marino is not presently being included by file "foo.cc", the
68*e4b17023SJohn Marino CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
69*e4b17023SJohn Marino of the vtables nor any of the inline functions defined in foo.h
70*e4b17023SJohn Marino will ever be output.
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino There are cases when we want to link files such as "defs.h" and
73*e4b17023SJohn Marino "main.cc". In this case, we give "defs.h" a `#pragma interface',
74*e4b17023SJohn Marino and "main.cc" has `#pragma implementation "defs.h"'. */
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino struct impl_files
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino const char *filename;
79*e4b17023SJohn Marino struct impl_files *next;
80*e4b17023SJohn Marino };
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino static struct impl_files *impl_file_chain;
83*e4b17023SJohn Marino
84*e4b17023SJohn Marino /* True if we saw "#pragma GCC java_exceptions". */
85*e4b17023SJohn Marino bool pragma_java_exceptions;
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino void
cxx_finish(void)88*e4b17023SJohn Marino cxx_finish (void)
89*e4b17023SJohn Marino {
90*e4b17023SJohn Marino c_common_finish ();
91*e4b17023SJohn Marino }
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino /* A mapping from tree codes to operator name information. */
94*e4b17023SJohn Marino operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
95*e4b17023SJohn Marino /* Similar, but for assignment operators. */
96*e4b17023SJohn Marino operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino /* Initialize data structures that keep track of operator names. */
99*e4b17023SJohn Marino
100*e4b17023SJohn Marino #define DEF_OPERATOR(NAME, C, M, AR, AP) \
101*e4b17023SJohn Marino CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
102*e4b17023SJohn Marino #include "operators.def"
103*e4b17023SJohn Marino #undef DEF_OPERATOR
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino static void
init_operators(void)106*e4b17023SJohn Marino init_operators (void)
107*e4b17023SJohn Marino {
108*e4b17023SJohn Marino tree identifier;
109*e4b17023SJohn Marino char buffer[256];
110*e4b17023SJohn Marino struct operator_name_info_t *oni;
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
113*e4b17023SJohn Marino sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
114*e4b17023SJohn Marino identifier = get_identifier (buffer); \
115*e4b17023SJohn Marino IDENTIFIER_OPNAME_P (identifier) = 1; \
116*e4b17023SJohn Marino \
117*e4b17023SJohn Marino oni = (ASSN_P \
118*e4b17023SJohn Marino ? &assignment_operator_name_info[(int) CODE] \
119*e4b17023SJohn Marino : &operator_name_info[(int) CODE]); \
120*e4b17023SJohn Marino oni->identifier = identifier; \
121*e4b17023SJohn Marino oni->name = NAME; \
122*e4b17023SJohn Marino oni->mangled_name = MANGLING; \
123*e4b17023SJohn Marino oni->arity = ARITY;
124*e4b17023SJohn Marino
125*e4b17023SJohn Marino #include "operators.def"
126*e4b17023SJohn Marino #undef DEF_OPERATOR
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino operator_name_info[(int) ERROR_MARK].identifier
129*e4b17023SJohn Marino = get_identifier ("<invalid operator>");
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino /* Handle some special cases. These operators are not defined in
132*e4b17023SJohn Marino the language, but can be produced internally. We may need them
133*e4b17023SJohn Marino for error-reporting. (Eventually, we should ensure that this
134*e4b17023SJohn Marino does not happen. Error messages involving these operators will
135*e4b17023SJohn Marino be confusing to users.) */
136*e4b17023SJohn Marino
137*e4b17023SJohn Marino operator_name_info [(int) INIT_EXPR].name
138*e4b17023SJohn Marino = operator_name_info [(int) MODIFY_EXPR].name;
139*e4b17023SJohn Marino operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
140*e4b17023SJohn Marino operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
141*e4b17023SJohn Marino operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
142*e4b17023SJohn Marino operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
143*e4b17023SJohn Marino operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
144*e4b17023SJohn Marino operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
145*e4b17023SJohn Marino operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
146*e4b17023SJohn Marino operator_name_info [(int) ABS_EXPR].name = "abs";
147*e4b17023SJohn Marino operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
148*e4b17023SJohn Marino operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
149*e4b17023SJohn Marino operator_name_info [(int) RANGE_EXPR].name = "...";
150*e4b17023SJohn Marino operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
151*e4b17023SJohn Marino
152*e4b17023SJohn Marino assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
153*e4b17023SJohn Marino = "(exact /=)";
154*e4b17023SJohn Marino assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
155*e4b17023SJohn Marino = "(ceiling /=)";
156*e4b17023SJohn Marino assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
157*e4b17023SJohn Marino = "(floor /=)";
158*e4b17023SJohn Marino assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
159*e4b17023SJohn Marino = "(round /=)";
160*e4b17023SJohn Marino assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
161*e4b17023SJohn Marino = "(ceiling %=)";
162*e4b17023SJohn Marino assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
163*e4b17023SJohn Marino = "(floor %=)";
164*e4b17023SJohn Marino assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
165*e4b17023SJohn Marino = "(round %=)";
166*e4b17023SJohn Marino }
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino /* Initialize the reserved words. */
169*e4b17023SJohn Marino
170*e4b17023SJohn Marino void
init_reswords(void)171*e4b17023SJohn Marino init_reswords (void)
172*e4b17023SJohn Marino {
173*e4b17023SJohn Marino unsigned int i;
174*e4b17023SJohn Marino tree id;
175*e4b17023SJohn Marino int mask = 0;
176*e4b17023SJohn Marino
177*e4b17023SJohn Marino if (cxx_dialect != cxx0x)
178*e4b17023SJohn Marino mask |= D_CXX0X;
179*e4b17023SJohn Marino if (flag_no_asm)
180*e4b17023SJohn Marino mask |= D_ASM | D_EXT;
181*e4b17023SJohn Marino if (flag_no_gnu_keywords)
182*e4b17023SJohn Marino mask |= D_EXT;
183*e4b17023SJohn Marino
184*e4b17023SJohn Marino /* The Objective-C keywords are all context-dependent. */
185*e4b17023SJohn Marino mask |= D_OBJC;
186*e4b17023SJohn Marino
187*e4b17023SJohn Marino ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
188*e4b17023SJohn Marino for (i = 0; i < num_c_common_reswords; i++)
189*e4b17023SJohn Marino {
190*e4b17023SJohn Marino if (c_common_reswords[i].disable & D_CONLY)
191*e4b17023SJohn Marino continue;
192*e4b17023SJohn Marino id = get_identifier (c_common_reswords[i].word);
193*e4b17023SJohn Marino C_SET_RID_CODE (id, c_common_reswords[i].rid);
194*e4b17023SJohn Marino ridpointers [(int) c_common_reswords[i].rid] = id;
195*e4b17023SJohn Marino if (! (c_common_reswords[i].disable & mask))
196*e4b17023SJohn Marino C_IS_RESERVED_WORD (id) = 1;
197*e4b17023SJohn Marino }
198*e4b17023SJohn Marino }
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino static void
init_cp_pragma(void)201*e4b17023SJohn Marino init_cp_pragma (void)
202*e4b17023SJohn Marino {
203*e4b17023SJohn Marino c_register_pragma (0, "vtable", handle_pragma_vtable);
204*e4b17023SJohn Marino c_register_pragma (0, "unit", handle_pragma_unit);
205*e4b17023SJohn Marino c_register_pragma (0, "interface", handle_pragma_interface);
206*e4b17023SJohn Marino c_register_pragma (0, "implementation", handle_pragma_implementation);
207*e4b17023SJohn Marino c_register_pragma ("GCC", "interface", handle_pragma_interface);
208*e4b17023SJohn Marino c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
209*e4b17023SJohn Marino c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
210*e4b17023SJohn Marino }
211*e4b17023SJohn Marino
212*e4b17023SJohn Marino /* TRUE if a code represents a statement. */
213*e4b17023SJohn Marino
214*e4b17023SJohn Marino bool statement_code_p[MAX_TREE_CODES];
215*e4b17023SJohn Marino
216*e4b17023SJohn Marino /* Initialize the C++ front end. This function is very sensitive to
217*e4b17023SJohn Marino the exact order that things are done here. It would be nice if the
218*e4b17023SJohn Marino initialization done by this routine were moved to its subroutines,
219*e4b17023SJohn Marino and the ordering dependencies clarified and reduced. */
220*e4b17023SJohn Marino bool
cxx_init(void)221*e4b17023SJohn Marino cxx_init (void)
222*e4b17023SJohn Marino {
223*e4b17023SJohn Marino location_t saved_loc;
224*e4b17023SJohn Marino unsigned int i;
225*e4b17023SJohn Marino static const enum tree_code stmt_codes[] = {
226*e4b17023SJohn Marino CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
227*e4b17023SJohn Marino EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
228*e4b17023SJohn Marino IF_STMT, CLEANUP_STMT, FOR_STMT,
229*e4b17023SJohn Marino RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
230*e4b17023SJohn Marino BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
231*e4b17023SJohn Marino EXPR_STMT
232*e4b17023SJohn Marino };
233*e4b17023SJohn Marino
234*e4b17023SJohn Marino memset (&statement_code_p, 0, sizeof (statement_code_p));
235*e4b17023SJohn Marino for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
236*e4b17023SJohn Marino statement_code_p[stmt_codes[i]] = true;
237*e4b17023SJohn Marino
238*e4b17023SJohn Marino saved_loc = input_location;
239*e4b17023SJohn Marino input_location = BUILTINS_LOCATION;
240*e4b17023SJohn Marino
241*e4b17023SJohn Marino init_reswords ();
242*e4b17023SJohn Marino init_tree ();
243*e4b17023SJohn Marino init_cp_semantics ();
244*e4b17023SJohn Marino init_operators ();
245*e4b17023SJohn Marino init_method ();
246*e4b17023SJohn Marino init_error ();
247*e4b17023SJohn Marino
248*e4b17023SJohn Marino current_function_decl = NULL;
249*e4b17023SJohn Marino
250*e4b17023SJohn Marino class_type_node = ridpointers[(int) RID_CLASS];
251*e4b17023SJohn Marino
252*e4b17023SJohn Marino cxx_init_decl_processing ();
253*e4b17023SJohn Marino
254*e4b17023SJohn Marino if (c_common_init () == false)
255*e4b17023SJohn Marino {
256*e4b17023SJohn Marino input_location = saved_loc;
257*e4b17023SJohn Marino return false;
258*e4b17023SJohn Marino }
259*e4b17023SJohn Marino
260*e4b17023SJohn Marino init_cp_pragma ();
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino init_repo ();
263*e4b17023SJohn Marino
264*e4b17023SJohn Marino input_location = saved_loc;
265*e4b17023SJohn Marino return true;
266*e4b17023SJohn Marino }
267*e4b17023SJohn Marino
268*e4b17023SJohn Marino /* Return nonzero if S is not considered part of an
269*e4b17023SJohn Marino INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
270*e4b17023SJohn Marino
271*e4b17023SJohn Marino static int
interface_strcmp(const char * s)272*e4b17023SJohn Marino interface_strcmp (const char* s)
273*e4b17023SJohn Marino {
274*e4b17023SJohn Marino /* Set the interface/implementation bits for this scope. */
275*e4b17023SJohn Marino struct impl_files *ifiles;
276*e4b17023SJohn Marino const char *s1;
277*e4b17023SJohn Marino
278*e4b17023SJohn Marino for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
279*e4b17023SJohn Marino {
280*e4b17023SJohn Marino const char *t1 = ifiles->filename;
281*e4b17023SJohn Marino s1 = s;
282*e4b17023SJohn Marino
283*e4b17023SJohn Marino if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
284*e4b17023SJohn Marino continue;
285*e4b17023SJohn Marino
286*e4b17023SJohn Marino while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
287*e4b17023SJohn Marino s1++, t1++;
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino /* A match. */
290*e4b17023SJohn Marino if (*s1 == *t1)
291*e4b17023SJohn Marino return 0;
292*e4b17023SJohn Marino
293*e4b17023SJohn Marino /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
294*e4b17023SJohn Marino if (strchr (s1, '.') || strchr (t1, '.'))
295*e4b17023SJohn Marino continue;
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
298*e4b17023SJohn Marino continue;
299*e4b17023SJohn Marino
300*e4b17023SJohn Marino /* A match. */
301*e4b17023SJohn Marino return 0;
302*e4b17023SJohn Marino }
303*e4b17023SJohn Marino
304*e4b17023SJohn Marino /* No matches. */
305*e4b17023SJohn Marino return 1;
306*e4b17023SJohn Marino }
307*e4b17023SJohn Marino
308*e4b17023SJohn Marino
309*e4b17023SJohn Marino
310*e4b17023SJohn Marino /* Parse a #pragma whose sole argument is a string constant.
311*e4b17023SJohn Marino If OPT is true, the argument is optional. */
312*e4b17023SJohn Marino static tree
parse_strconst_pragma(const char * name,int opt)313*e4b17023SJohn Marino parse_strconst_pragma (const char* name, int opt)
314*e4b17023SJohn Marino {
315*e4b17023SJohn Marino tree result, x;
316*e4b17023SJohn Marino enum cpp_ttype t;
317*e4b17023SJohn Marino
318*e4b17023SJohn Marino t = pragma_lex (&result);
319*e4b17023SJohn Marino if (t == CPP_STRING)
320*e4b17023SJohn Marino {
321*e4b17023SJohn Marino if (pragma_lex (&x) != CPP_EOF)
322*e4b17023SJohn Marino warning (0, "junk at end of #pragma %s", name);
323*e4b17023SJohn Marino return result;
324*e4b17023SJohn Marino }
325*e4b17023SJohn Marino
326*e4b17023SJohn Marino if (t == CPP_EOF && opt)
327*e4b17023SJohn Marino return NULL_TREE;
328*e4b17023SJohn Marino
329*e4b17023SJohn Marino error ("invalid #pragma %s", name);
330*e4b17023SJohn Marino return error_mark_node;
331*e4b17023SJohn Marino }
332*e4b17023SJohn Marino
333*e4b17023SJohn Marino static void
handle_pragma_vtable(cpp_reader * dfile ATTRIBUTE_UNUSED)334*e4b17023SJohn Marino handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
335*e4b17023SJohn Marino {
336*e4b17023SJohn Marino parse_strconst_pragma ("vtable", 0);
337*e4b17023SJohn Marino sorry ("#pragma vtable no longer supported");
338*e4b17023SJohn Marino }
339*e4b17023SJohn Marino
340*e4b17023SJohn Marino static void
handle_pragma_unit(cpp_reader * dfile ATTRIBUTE_UNUSED)341*e4b17023SJohn Marino handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
342*e4b17023SJohn Marino {
343*e4b17023SJohn Marino /* Validate syntax, but don't do anything. */
344*e4b17023SJohn Marino parse_strconst_pragma ("unit", 0);
345*e4b17023SJohn Marino }
346*e4b17023SJohn Marino
347*e4b17023SJohn Marino static void
handle_pragma_interface(cpp_reader * dfile ATTRIBUTE_UNUSED)348*e4b17023SJohn Marino handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
349*e4b17023SJohn Marino {
350*e4b17023SJohn Marino tree fname = parse_strconst_pragma ("interface", 1);
351*e4b17023SJohn Marino struct c_fileinfo *finfo;
352*e4b17023SJohn Marino const char *filename;
353*e4b17023SJohn Marino
354*e4b17023SJohn Marino if (fname == error_mark_node)
355*e4b17023SJohn Marino return;
356*e4b17023SJohn Marino else if (fname == 0)
357*e4b17023SJohn Marino filename = lbasename (input_filename);
358*e4b17023SJohn Marino else
359*e4b17023SJohn Marino filename = TREE_STRING_POINTER (fname);
360*e4b17023SJohn Marino
361*e4b17023SJohn Marino finfo = get_fileinfo (input_filename);
362*e4b17023SJohn Marino
363*e4b17023SJohn Marino if (impl_file_chain == 0)
364*e4b17023SJohn Marino {
365*e4b17023SJohn Marino /* If this is zero at this point, then we are
366*e4b17023SJohn Marino auto-implementing. */
367*e4b17023SJohn Marino if (main_input_filename == 0)
368*e4b17023SJohn Marino main_input_filename = input_filename;
369*e4b17023SJohn Marino }
370*e4b17023SJohn Marino
371*e4b17023SJohn Marino finfo->interface_only = interface_strcmp (filename);
372*e4b17023SJohn Marino /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
373*e4b17023SJohn Marino a definition in another file. */
374*e4b17023SJohn Marino if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
375*e4b17023SJohn Marino finfo->interface_unknown = 0;
376*e4b17023SJohn Marino }
377*e4b17023SJohn Marino
378*e4b17023SJohn Marino /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
379*e4b17023SJohn Marino We used to only allow this at toplevel, but that restriction was buggy
380*e4b17023SJohn Marino in older compilers and it seems reasonable to allow it in the headers
381*e4b17023SJohn Marino themselves, too. It only needs to precede the matching #p interface.
382*e4b17023SJohn Marino
383*e4b17023SJohn Marino We don't touch finfo->interface_only or finfo->interface_unknown;
384*e4b17023SJohn Marino the user must specify a matching #p interface for this to have
385*e4b17023SJohn Marino any effect. */
386*e4b17023SJohn Marino
387*e4b17023SJohn Marino static void
handle_pragma_implementation(cpp_reader * dfile ATTRIBUTE_UNUSED)388*e4b17023SJohn Marino handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
389*e4b17023SJohn Marino {
390*e4b17023SJohn Marino tree fname = parse_strconst_pragma ("implementation", 1);
391*e4b17023SJohn Marino const char *filename;
392*e4b17023SJohn Marino struct impl_files *ifiles = impl_file_chain;
393*e4b17023SJohn Marino
394*e4b17023SJohn Marino if (fname == error_mark_node)
395*e4b17023SJohn Marino return;
396*e4b17023SJohn Marino
397*e4b17023SJohn Marino if (fname == 0)
398*e4b17023SJohn Marino {
399*e4b17023SJohn Marino if (main_input_filename)
400*e4b17023SJohn Marino filename = main_input_filename;
401*e4b17023SJohn Marino else
402*e4b17023SJohn Marino filename = input_filename;
403*e4b17023SJohn Marino filename = lbasename (filename);
404*e4b17023SJohn Marino }
405*e4b17023SJohn Marino else
406*e4b17023SJohn Marino {
407*e4b17023SJohn Marino filename = TREE_STRING_POINTER (fname);
408*e4b17023SJohn Marino if (cpp_included_before (parse_in, filename, input_location))
409*e4b17023SJohn Marino warning (0, "#pragma implementation for %qs appears after "
410*e4b17023SJohn Marino "file is included", filename);
411*e4b17023SJohn Marino }
412*e4b17023SJohn Marino
413*e4b17023SJohn Marino for (; ifiles; ifiles = ifiles->next)
414*e4b17023SJohn Marino {
415*e4b17023SJohn Marino if (! filename_cmp (ifiles->filename, filename))
416*e4b17023SJohn Marino break;
417*e4b17023SJohn Marino }
418*e4b17023SJohn Marino if (ifiles == 0)
419*e4b17023SJohn Marino {
420*e4b17023SJohn Marino ifiles = XNEW (struct impl_files);
421*e4b17023SJohn Marino ifiles->filename = xstrdup (filename);
422*e4b17023SJohn Marino ifiles->next = impl_file_chain;
423*e4b17023SJohn Marino impl_file_chain = ifiles;
424*e4b17023SJohn Marino }
425*e4b17023SJohn Marino }
426*e4b17023SJohn Marino
427*e4b17023SJohn Marino /* Indicate that this file uses Java-personality exception handling. */
428*e4b17023SJohn Marino static void
handle_pragma_java_exceptions(cpp_reader * dfile ATTRIBUTE_UNUSED)429*e4b17023SJohn Marino handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
430*e4b17023SJohn Marino {
431*e4b17023SJohn Marino tree x;
432*e4b17023SJohn Marino if (pragma_lex (&x) != CPP_EOF)
433*e4b17023SJohn Marino warning (0, "junk at end of #pragma GCC java_exceptions");
434*e4b17023SJohn Marino
435*e4b17023SJohn Marino choose_personality_routine (lang_java);
436*e4b17023SJohn Marino pragma_java_exceptions = true;
437*e4b17023SJohn Marino }
438*e4b17023SJohn Marino
439*e4b17023SJohn Marino /* Issue an error message indicating that the lookup of NAME (an
440*e4b17023SJohn Marino IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
441*e4b17023SJohn Marino
442*e4b17023SJohn Marino tree
unqualified_name_lookup_error(tree name)443*e4b17023SJohn Marino unqualified_name_lookup_error (tree name)
444*e4b17023SJohn Marino {
445*e4b17023SJohn Marino if (IDENTIFIER_OPNAME_P (name))
446*e4b17023SJohn Marino {
447*e4b17023SJohn Marino if (name != ansi_opname (ERROR_MARK))
448*e4b17023SJohn Marino error ("%qD not defined", name);
449*e4b17023SJohn Marino }
450*e4b17023SJohn Marino else
451*e4b17023SJohn Marino {
452*e4b17023SJohn Marino if (!objc_diagnose_private_ivar (name))
453*e4b17023SJohn Marino {
454*e4b17023SJohn Marino error ("%qD was not declared in this scope", name);
455*e4b17023SJohn Marino suggest_alternatives_for (location_of (name), name);
456*e4b17023SJohn Marino }
457*e4b17023SJohn Marino /* Prevent repeated error messages by creating a VAR_DECL with
458*e4b17023SJohn Marino this NAME in the innermost block scope. */
459*e4b17023SJohn Marino if (local_bindings_p ())
460*e4b17023SJohn Marino {
461*e4b17023SJohn Marino tree decl;
462*e4b17023SJohn Marino decl = build_decl (input_location,
463*e4b17023SJohn Marino VAR_DECL, name, error_mark_node);
464*e4b17023SJohn Marino DECL_CONTEXT (decl) = current_function_decl;
465*e4b17023SJohn Marino push_local_binding (name, decl, 0);
466*e4b17023SJohn Marino /* Mark the variable as used so that we do not get warnings
467*e4b17023SJohn Marino about it being unused later. */
468*e4b17023SJohn Marino TREE_USED (decl) = 1;
469*e4b17023SJohn Marino }
470*e4b17023SJohn Marino }
471*e4b17023SJohn Marino
472*e4b17023SJohn Marino return error_mark_node;
473*e4b17023SJohn Marino }
474*e4b17023SJohn Marino
475*e4b17023SJohn Marino /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
476*e4b17023SJohn Marino used as a function. Returns an appropriate expression for
477*e4b17023SJohn Marino NAME. */
478*e4b17023SJohn Marino
479*e4b17023SJohn Marino tree
unqualified_fn_lookup_error(tree name)480*e4b17023SJohn Marino unqualified_fn_lookup_error (tree name)
481*e4b17023SJohn Marino {
482*e4b17023SJohn Marino if (processing_template_decl)
483*e4b17023SJohn Marino {
484*e4b17023SJohn Marino /* In a template, it is invalid to write "f()" or "f(3)" if no
485*e4b17023SJohn Marino declaration of "f" is available. Historically, G++ and most
486*e4b17023SJohn Marino other compilers accepted that usage since they deferred all name
487*e4b17023SJohn Marino lookup until instantiation time rather than doing unqualified
488*e4b17023SJohn Marino name lookup at template definition time; explain to the user what
489*e4b17023SJohn Marino is going wrong.
490*e4b17023SJohn Marino
491*e4b17023SJohn Marino Note that we have the exact wording of the following message in
492*e4b17023SJohn Marino the manual (trouble.texi, node "Name lookup"), so they need to
493*e4b17023SJohn Marino be kept in synch. */
494*e4b17023SJohn Marino permerror (input_location, "there are no arguments to %qD that depend on a template "
495*e4b17023SJohn Marino "parameter, so a declaration of %qD must be available",
496*e4b17023SJohn Marino name, name);
497*e4b17023SJohn Marino
498*e4b17023SJohn Marino if (!flag_permissive)
499*e4b17023SJohn Marino {
500*e4b17023SJohn Marino static bool hint;
501*e4b17023SJohn Marino if (!hint)
502*e4b17023SJohn Marino {
503*e4b17023SJohn Marino inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
504*e4b17023SJohn Marino "code, but allowing the use of an undeclared name is "
505*e4b17023SJohn Marino "deprecated)");
506*e4b17023SJohn Marino hint = true;
507*e4b17023SJohn Marino }
508*e4b17023SJohn Marino }
509*e4b17023SJohn Marino return name;
510*e4b17023SJohn Marino }
511*e4b17023SJohn Marino
512*e4b17023SJohn Marino return unqualified_name_lookup_error (name);
513*e4b17023SJohn Marino }
514*e4b17023SJohn Marino
515*e4b17023SJohn Marino /* Wrapper around build_lang_decl_loc(). Should gradually move to
516*e4b17023SJohn Marino build_lang_decl_loc() and then rename build_lang_decl_loc() back to
517*e4b17023SJohn Marino build_lang_decl(). */
518*e4b17023SJohn Marino
519*e4b17023SJohn Marino tree
build_lang_decl(enum tree_code code,tree name,tree type)520*e4b17023SJohn Marino build_lang_decl (enum tree_code code, tree name, tree type)
521*e4b17023SJohn Marino {
522*e4b17023SJohn Marino return build_lang_decl_loc (input_location, code, name, type);
523*e4b17023SJohn Marino }
524*e4b17023SJohn Marino
525*e4b17023SJohn Marino /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
526*e4b17023SJohn Marino DECL_LANG_SPECIFIC info to the result. */
527*e4b17023SJohn Marino
528*e4b17023SJohn Marino tree
build_lang_decl_loc(location_t loc,enum tree_code code,tree name,tree type)529*e4b17023SJohn Marino build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
530*e4b17023SJohn Marino {
531*e4b17023SJohn Marino tree t;
532*e4b17023SJohn Marino
533*e4b17023SJohn Marino t = build_decl (loc, code, name, type);
534*e4b17023SJohn Marino retrofit_lang_decl (t);
535*e4b17023SJohn Marino
536*e4b17023SJohn Marino return t;
537*e4b17023SJohn Marino }
538*e4b17023SJohn Marino
539*e4b17023SJohn Marino /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
540*e4b17023SJohn Marino and pushdecl (for functions generated by the back end). */
541*e4b17023SJohn Marino
542*e4b17023SJohn Marino void
retrofit_lang_decl(tree t)543*e4b17023SJohn Marino retrofit_lang_decl (tree t)
544*e4b17023SJohn Marino {
545*e4b17023SJohn Marino struct lang_decl *ld;
546*e4b17023SJohn Marino size_t size;
547*e4b17023SJohn Marino int sel;
548*e4b17023SJohn Marino
549*e4b17023SJohn Marino if (TREE_CODE (t) == FUNCTION_DECL)
550*e4b17023SJohn Marino sel = 1, size = sizeof (struct lang_decl_fn);
551*e4b17023SJohn Marino else if (TREE_CODE (t) == NAMESPACE_DECL)
552*e4b17023SJohn Marino sel = 2, size = sizeof (struct lang_decl_ns);
553*e4b17023SJohn Marino else if (TREE_CODE (t) == PARM_DECL)
554*e4b17023SJohn Marino sel = 3, size = sizeof (struct lang_decl_parm);
555*e4b17023SJohn Marino else if (LANG_DECL_HAS_MIN (t))
556*e4b17023SJohn Marino sel = 0, size = sizeof (struct lang_decl_min);
557*e4b17023SJohn Marino else
558*e4b17023SJohn Marino gcc_unreachable ();
559*e4b17023SJohn Marino
560*e4b17023SJohn Marino ld = ggc_alloc_cleared_lang_decl (size);
561*e4b17023SJohn Marino
562*e4b17023SJohn Marino ld->u.base.selector = sel;
563*e4b17023SJohn Marino
564*e4b17023SJohn Marino DECL_LANG_SPECIFIC (t) = ld;
565*e4b17023SJohn Marino if (current_lang_name == lang_name_cplusplus
566*e4b17023SJohn Marino || decl_linkage (t) == lk_none)
567*e4b17023SJohn Marino SET_DECL_LANGUAGE (t, lang_cplusplus);
568*e4b17023SJohn Marino else if (current_lang_name == lang_name_c)
569*e4b17023SJohn Marino SET_DECL_LANGUAGE (t, lang_c);
570*e4b17023SJohn Marino else if (current_lang_name == lang_name_java)
571*e4b17023SJohn Marino SET_DECL_LANGUAGE (t, lang_java);
572*e4b17023SJohn Marino else
573*e4b17023SJohn Marino gcc_unreachable ();
574*e4b17023SJohn Marino
575*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
576*e4b17023SJohn Marino tree_node_counts[(int)lang_decl] += 1;
577*e4b17023SJohn Marino tree_node_sizes[(int)lang_decl] += size;
578*e4b17023SJohn Marino #endif
579*e4b17023SJohn Marino }
580*e4b17023SJohn Marino
581*e4b17023SJohn Marino void
cxx_dup_lang_specific_decl(tree node)582*e4b17023SJohn Marino cxx_dup_lang_specific_decl (tree node)
583*e4b17023SJohn Marino {
584*e4b17023SJohn Marino int size;
585*e4b17023SJohn Marino struct lang_decl *ld;
586*e4b17023SJohn Marino
587*e4b17023SJohn Marino if (! DECL_LANG_SPECIFIC (node))
588*e4b17023SJohn Marino return;
589*e4b17023SJohn Marino
590*e4b17023SJohn Marino if (TREE_CODE (node) == FUNCTION_DECL)
591*e4b17023SJohn Marino size = sizeof (struct lang_decl_fn);
592*e4b17023SJohn Marino else if (TREE_CODE (node) == NAMESPACE_DECL)
593*e4b17023SJohn Marino size = sizeof (struct lang_decl_ns);
594*e4b17023SJohn Marino else if (TREE_CODE (node) == PARM_DECL)
595*e4b17023SJohn Marino size = sizeof (struct lang_decl_parm);
596*e4b17023SJohn Marino else if (LANG_DECL_HAS_MIN (node))
597*e4b17023SJohn Marino size = sizeof (struct lang_decl_min);
598*e4b17023SJohn Marino else
599*e4b17023SJohn Marino gcc_unreachable ();
600*e4b17023SJohn Marino
601*e4b17023SJohn Marino ld = ggc_alloc_lang_decl (size);
602*e4b17023SJohn Marino memcpy (ld, DECL_LANG_SPECIFIC (node), size);
603*e4b17023SJohn Marino DECL_LANG_SPECIFIC (node) = ld;
604*e4b17023SJohn Marino
605*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
606*e4b17023SJohn Marino tree_node_counts[(int)lang_decl] += 1;
607*e4b17023SJohn Marino tree_node_sizes[(int)lang_decl] += size;
608*e4b17023SJohn Marino #endif
609*e4b17023SJohn Marino }
610*e4b17023SJohn Marino
611*e4b17023SJohn Marino /* Copy DECL, including any language-specific parts. */
612*e4b17023SJohn Marino
613*e4b17023SJohn Marino tree
copy_decl(tree decl)614*e4b17023SJohn Marino copy_decl (tree decl)
615*e4b17023SJohn Marino {
616*e4b17023SJohn Marino tree copy;
617*e4b17023SJohn Marino
618*e4b17023SJohn Marino copy = copy_node (decl);
619*e4b17023SJohn Marino cxx_dup_lang_specific_decl (copy);
620*e4b17023SJohn Marino return copy;
621*e4b17023SJohn Marino }
622*e4b17023SJohn Marino
623*e4b17023SJohn Marino /* Replace the shared language-specific parts of NODE with a new copy. */
624*e4b17023SJohn Marino
625*e4b17023SJohn Marino static void
copy_lang_type(tree node)626*e4b17023SJohn Marino copy_lang_type (tree node)
627*e4b17023SJohn Marino {
628*e4b17023SJohn Marino int size;
629*e4b17023SJohn Marino struct lang_type *lt;
630*e4b17023SJohn Marino
631*e4b17023SJohn Marino if (! TYPE_LANG_SPECIFIC (node))
632*e4b17023SJohn Marino return;
633*e4b17023SJohn Marino
634*e4b17023SJohn Marino if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
635*e4b17023SJohn Marino size = sizeof (struct lang_type);
636*e4b17023SJohn Marino else
637*e4b17023SJohn Marino size = sizeof (struct lang_type_ptrmem);
638*e4b17023SJohn Marino lt = ggc_alloc_lang_type (size);
639*e4b17023SJohn Marino memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
640*e4b17023SJohn Marino TYPE_LANG_SPECIFIC (node) = lt;
641*e4b17023SJohn Marino
642*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
643*e4b17023SJohn Marino tree_node_counts[(int)lang_type] += 1;
644*e4b17023SJohn Marino tree_node_sizes[(int)lang_type] += size;
645*e4b17023SJohn Marino #endif
646*e4b17023SJohn Marino }
647*e4b17023SJohn Marino
648*e4b17023SJohn Marino /* Copy TYPE, including any language-specific parts. */
649*e4b17023SJohn Marino
650*e4b17023SJohn Marino tree
copy_type(tree type)651*e4b17023SJohn Marino copy_type (tree type)
652*e4b17023SJohn Marino {
653*e4b17023SJohn Marino tree copy;
654*e4b17023SJohn Marino
655*e4b17023SJohn Marino copy = copy_node (type);
656*e4b17023SJohn Marino copy_lang_type (copy);
657*e4b17023SJohn Marino return copy;
658*e4b17023SJohn Marino }
659*e4b17023SJohn Marino
660*e4b17023SJohn Marino tree
cxx_make_type(enum tree_code code)661*e4b17023SJohn Marino cxx_make_type (enum tree_code code)
662*e4b17023SJohn Marino {
663*e4b17023SJohn Marino tree t = make_node (code);
664*e4b17023SJohn Marino
665*e4b17023SJohn Marino /* Create lang_type structure. */
666*e4b17023SJohn Marino if (RECORD_OR_UNION_CODE_P (code)
667*e4b17023SJohn Marino || code == BOUND_TEMPLATE_TEMPLATE_PARM)
668*e4b17023SJohn Marino {
669*e4b17023SJohn Marino struct lang_type *pi
670*e4b17023SJohn Marino = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
671*e4b17023SJohn Marino
672*e4b17023SJohn Marino TYPE_LANG_SPECIFIC (t) = pi;
673*e4b17023SJohn Marino pi->u.c.h.is_lang_type_class = 1;
674*e4b17023SJohn Marino
675*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
676*e4b17023SJohn Marino tree_node_counts[(int)lang_type] += 1;
677*e4b17023SJohn Marino tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
678*e4b17023SJohn Marino #endif
679*e4b17023SJohn Marino }
680*e4b17023SJohn Marino
681*e4b17023SJohn Marino /* Set up some flags that give proper default behavior. */
682*e4b17023SJohn Marino if (RECORD_OR_UNION_CODE_P (code))
683*e4b17023SJohn Marino {
684*e4b17023SJohn Marino struct c_fileinfo *finfo = get_fileinfo (input_filename);
685*e4b17023SJohn Marino SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
686*e4b17023SJohn Marino CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
687*e4b17023SJohn Marino }
688*e4b17023SJohn Marino
689*e4b17023SJohn Marino return t;
690*e4b17023SJohn Marino }
691*e4b17023SJohn Marino
692*e4b17023SJohn Marino tree
make_class_type(enum tree_code code)693*e4b17023SJohn Marino make_class_type (enum tree_code code)
694*e4b17023SJohn Marino {
695*e4b17023SJohn Marino tree t = cxx_make_type (code);
696*e4b17023SJohn Marino SET_CLASS_TYPE_P (t, 1);
697*e4b17023SJohn Marino return t;
698*e4b17023SJohn Marino }
699*e4b17023SJohn Marino
700*e4b17023SJohn Marino /* Returns true if we are currently in the main source file, or in a
701*e4b17023SJohn Marino template instantiation started from the main source file. */
702*e4b17023SJohn Marino
703*e4b17023SJohn Marino bool
in_main_input_context(void)704*e4b17023SJohn Marino in_main_input_context (void)
705*e4b17023SJohn Marino {
706*e4b17023SJohn Marino struct tinst_level *tl = outermost_tinst_level();
707*e4b17023SJohn Marino
708*e4b17023SJohn Marino if (tl)
709*e4b17023SJohn Marino return filename_cmp (main_input_filename,
710*e4b17023SJohn Marino LOCATION_FILE (tl->locus)) == 0;
711*e4b17023SJohn Marino else
712*e4b17023SJohn Marino return filename_cmp (main_input_filename, input_filename) == 0;
713*e4b17023SJohn Marino }
714