./:
[official-gcc.git] / gcc / cp / lex.c
blob890640e912a7455e50b8ab7d9885400521a264de
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file is the lexical analyzer for GNU C++. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "input.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "cpplib.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "tm_p.h"
39 #include "timevar.h"
41 static int interface_strcmp (const char *);
42 static void init_cp_pragma (void);
44 static tree parse_strconst_pragma (const char *, int);
45 static void handle_pragma_vtable (cpp_reader *);
46 static void handle_pragma_unit (cpp_reader *);
47 static void handle_pragma_interface (cpp_reader *);
48 static void handle_pragma_implementation (cpp_reader *);
49 static void handle_pragma_java_exceptions (cpp_reader *);
51 static void init_operators (void);
52 static void copy_lang_type (tree);
54 /* A constraint that can be tested at compile time. */
55 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
57 /* Functions and data structures for #pragma interface.
59 `#pragma implementation' means that the main file being compiled
60 is considered to implement (provide) the classes that appear in
61 its main body. I.e., if this is file "foo.cc", and class `bar'
62 is defined in "foo.cc", then we say that "foo.cc implements bar".
64 All main input files "implement" themselves automagically.
66 `#pragma interface' means that unless this file (of the form "foo.h"
67 is not presently being included by file "foo.cc", the
68 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
69 of the vtables nor any of the inline functions defined in foo.h
70 will ever be output.
72 There are cases when we want to link files such as "defs.h" and
73 "main.cc". In this case, we give "defs.h" a `#pragma interface',
74 and "main.cc" has `#pragma implementation "defs.h"'. */
76 struct impl_files
78 const char *filename;
79 struct impl_files *next;
82 static struct impl_files *impl_file_chain;
85 void
86 cxx_finish (void)
88 c_common_finish ();
91 /* A mapping from tree codes to operator name information. */
92 operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
93 /* Similar, but for assignment operators. */
94 operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
96 /* Initialize data structures that keep track of operator names. */
98 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
99 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
100 #include "operators.def"
101 #undef DEF_OPERATOR
103 static void
104 init_operators (void)
106 tree identifier;
107 char buffer[256];
108 struct operator_name_info_t *oni;
110 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
111 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
112 identifier = get_identifier (buffer); \
113 IDENTIFIER_OPNAME_P (identifier) = 1; \
115 oni = (ASSN_P \
116 ? &assignment_operator_name_info[(int) CODE] \
117 : &operator_name_info[(int) CODE]); \
118 oni->identifier = identifier; \
119 oni->name = NAME; \
120 oni->mangled_name = MANGLING; \
121 oni->arity = ARITY;
123 #include "operators.def"
124 #undef DEF_OPERATOR
126 operator_name_info[(int) ERROR_MARK].identifier
127 = get_identifier ("<invalid operator>");
129 /* Handle some special cases. These operators are not defined in
130 the language, but can be produced internally. We may need them
131 for error-reporting. (Eventually, we should ensure that this
132 does not happen. Error messages involving these operators will
133 be confusing to users.) */
135 operator_name_info [(int) INIT_EXPR].name
136 = operator_name_info [(int) MODIFY_EXPR].name;
137 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
138 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
139 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
140 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
141 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
142 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
143 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
144 operator_name_info [(int) ABS_EXPR].name = "abs";
145 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
146 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
147 operator_name_info [(int) RANGE_EXPR].name = "...";
148 operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
150 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
151 = "(exact /=)";
152 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
153 = "(ceiling /=)";
154 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
155 = "(floor /=)";
156 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
157 = "(round /=)";
158 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
159 = "(ceiling %=)";
160 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
161 = "(floor %=)";
162 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
163 = "(round %=)";
166 /* Initialize the reserved words. */
168 void
169 init_reswords (void)
171 unsigned int i;
172 tree id;
173 int mask = 0;
175 mask |= D_CONLY;
176 if (cxx_dialect != cxx0x)
177 mask |= D_CXX0X;
178 if (flag_no_asm)
179 mask |= D_ASM | D_EXT;
180 if (flag_no_gnu_keywords)
181 mask |= D_EXT;
182 if (!c_dialect_objc())
183 mask |= D_OBJC;
185 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
186 for (i = 0; i < num_c_common_reswords; i++)
188 id = get_identifier (c_common_reswords[i].word);
189 C_SET_RID_CODE (id, c_common_reswords[i].rid);
190 ridpointers [(int) c_common_reswords[i].rid] = id;
191 if (! (c_common_reswords[i].disable & mask))
192 C_IS_RESERVED_WORD (id) = 1;
196 static void
197 init_cp_pragma (void)
199 c_register_pragma (0, "vtable", handle_pragma_vtable);
200 c_register_pragma (0, "unit", handle_pragma_unit);
201 c_register_pragma (0, "interface", handle_pragma_interface);
202 c_register_pragma (0, "implementation", handle_pragma_implementation);
203 c_register_pragma ("GCC", "interface", handle_pragma_interface);
204 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
205 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
208 /* TRUE if a code represents a statement. */
210 bool statement_code_p[MAX_TREE_CODES];
212 /* Initialize the C++ front end. This function is very sensitive to
213 the exact order that things are done here. It would be nice if the
214 initialization done by this routine were moved to its subroutines,
215 and the ordering dependencies clarified and reduced. */
216 bool
217 cxx_init (void)
219 location_t saved_loc;
220 unsigned int i;
221 static const enum tree_code stmt_codes[] = {
222 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
223 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
224 IF_STMT, CLEANUP_STMT, FOR_STMT,
225 WHILE_STMT, DO_STMT, BREAK_STMT,
226 CONTINUE_STMT, SWITCH_STMT, EXPR_STMT
229 memset (&statement_code_p, 0, sizeof (statement_code_p));
230 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
231 statement_code_p[stmt_codes[i]] = true;
233 saved_loc = input_location;
234 input_location = BUILTINS_LOCATION;
236 init_reswords ();
237 init_tree ();
238 init_cp_semantics ();
239 init_operators ();
240 init_method ();
241 init_error ();
243 current_function_decl = NULL;
245 class_type_node = ridpointers[(int) RID_CLASS];
247 cxx_init_decl_processing ();
249 /* The fact that G++ uses COMDAT for many entities (inline
250 functions, template instantiations, virtual tables, etc.) mean
251 that it is fundamentally unreliable to try to make decisions
252 about whether or not to output a particular entity until the end
253 of the compilation. However, the inliner requires that functions
254 be provided to the back end if they are to be inlined.
255 Therefore, we always use unit-at-a-time mode; in that mode, we
256 can provide entities to the back end and it will decide what to
257 emit based on what is actually needed. */
258 flag_unit_at_a_time = 1;
260 if (c_common_init () == false)
262 input_location = saved_loc;
263 return false;
266 init_cp_pragma ();
268 init_repo ();
270 input_location = saved_loc;
271 return true;
274 /* Return nonzero if S is not considered part of an
275 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
277 static int
278 interface_strcmp (const char* s)
280 /* Set the interface/implementation bits for this scope. */
281 struct impl_files *ifiles;
282 const char *s1;
284 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
286 const char *t1 = ifiles->filename;
287 s1 = s;
289 if (*s1 != *t1 || *s1 == 0)
290 continue;
292 while (*s1 == *t1 && *s1 != 0)
293 s1++, t1++;
295 /* A match. */
296 if (*s1 == *t1)
297 return 0;
299 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
300 if (strchr (s1, '.') || strchr (t1, '.'))
301 continue;
303 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
304 continue;
306 /* A match. */
307 return 0;
310 /* No matches. */
311 return 1;
316 /* Parse a #pragma whose sole argument is a string constant.
317 If OPT is true, the argument is optional. */
318 static tree
319 parse_strconst_pragma (const char* name, int opt)
321 tree result, x;
322 enum cpp_ttype t;
324 t = pragma_lex (&result);
325 if (t == CPP_STRING)
327 if (pragma_lex (&x) != CPP_EOF)
328 warning (0, "junk at end of #pragma %s", name);
329 return result;
332 if (t == CPP_EOF && opt)
333 return NULL_TREE;
335 error ("invalid #pragma %s", name);
336 return error_mark_node;
339 static void
340 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
342 parse_strconst_pragma ("vtable", 0);
343 sorry ("#pragma vtable no longer supported");
346 static void
347 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
349 /* Validate syntax, but don't do anything. */
350 parse_strconst_pragma ("unit", 0);
353 static void
354 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
356 tree fname = parse_strconst_pragma ("interface", 1);
357 struct c_fileinfo *finfo;
358 const char *filename;
360 if (fname == error_mark_node)
361 return;
362 else if (fname == 0)
363 filename = lbasename (input_filename);
364 else
365 filename = TREE_STRING_POINTER (fname);
367 finfo = get_fileinfo (input_filename);
369 if (impl_file_chain == 0)
371 /* If this is zero at this point, then we are
372 auto-implementing. */
373 if (main_input_filename == 0)
374 main_input_filename = input_filename;
377 finfo->interface_only = interface_strcmp (filename);
378 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
379 a definition in another file. */
380 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
381 finfo->interface_unknown = 0;
384 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
385 We used to only allow this at toplevel, but that restriction was buggy
386 in older compilers and it seems reasonable to allow it in the headers
387 themselves, too. It only needs to precede the matching #p interface.
389 We don't touch finfo->interface_only or finfo->interface_unknown;
390 the user must specify a matching #p interface for this to have
391 any effect. */
393 static void
394 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
396 tree fname = parse_strconst_pragma ("implementation", 1);
397 const char *filename;
398 struct impl_files *ifiles = impl_file_chain;
400 if (fname == error_mark_node)
401 return;
403 if (fname == 0)
405 if (main_input_filename)
406 filename = main_input_filename;
407 else
408 filename = input_filename;
409 filename = lbasename (filename);
411 else
413 filename = TREE_STRING_POINTER (fname);
414 if (cpp_included_before (parse_in, filename, input_location))
415 warning (0, "#pragma implementation for %qs appears after "
416 "file is included", filename);
419 for (; ifiles; ifiles = ifiles->next)
421 if (! strcmp (ifiles->filename, filename))
422 break;
424 if (ifiles == 0)
426 ifiles = XNEW (struct impl_files);
427 ifiles->filename = xstrdup (filename);
428 ifiles->next = impl_file_chain;
429 impl_file_chain = ifiles;
433 /* Indicate that this file uses Java-personality exception handling. */
434 static void
435 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
437 tree x;
438 if (pragma_lex (&x) != CPP_EOF)
439 warning (0, "junk at end of #pragma GCC java_exceptions");
441 choose_personality_routine (lang_java);
444 /* Issue an error message indicating that the lookup of NAME (an
445 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
447 tree
448 unqualified_name_lookup_error (tree name)
450 if (IDENTIFIER_OPNAME_P (name))
452 if (name != ansi_opname (ERROR_MARK))
453 error ("%qD not defined", name);
455 else
457 error ("%qD was not declared in this scope", name);
458 /* Prevent repeated error messages by creating a VAR_DECL with
459 this NAME in the innermost block scope. */
460 if (current_function_decl)
462 tree decl;
463 decl = build_decl (VAR_DECL, name, error_mark_node);
464 DECL_CONTEXT (decl) = current_function_decl;
465 push_local_binding (name, decl, 0);
466 /* Mark the variable as used so that we do not get warnings
467 about it being unused later. */
468 TREE_USED (decl) = 1;
472 return error_mark_node;
475 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
476 used as a function. Returns an appropriate expression for
477 NAME. */
479 tree
480 unqualified_fn_lookup_error (tree name)
482 if (processing_template_decl)
484 /* In a template, it is invalid to write "f()" or "f(3)" if no
485 declaration of "f" is available. Historically, G++ and most
486 other compilers accepted that usage since they deferred all name
487 lookup until instantiation time rather than doing unqualified
488 name lookup at template definition time; explain to the user what
489 is going wrong.
491 Note that we have the exact wording of the following message in
492 the manual (trouble.texi, node "Name lookup"), so they need to
493 be kept in synch. */
494 permerror ("there are no arguments to %qD that depend on a template "
495 "parameter, so a declaration of %qD must be available",
496 name, name);
498 if (!flag_permissive)
500 static bool hint;
501 if (!hint)
503 inform ("(if you use %<-fpermissive%>, G++ will accept your "
504 "code, but allowing the use of an undeclared name is "
505 "deprecated)");
506 hint = true;
509 return name;
512 return unqualified_name_lookup_error (name);
515 tree
516 build_lang_decl (enum tree_code code, tree name, tree type)
518 tree t;
520 t = build_decl (code, name, type);
521 retrofit_lang_decl (t);
523 /* All nesting of C++ functions is lexical; there is never a "static
524 chain" in the sense of GNU C nested functions. */
525 if (code == FUNCTION_DECL)
526 DECL_NO_STATIC_CHAIN (t) = 1;
528 return t;
531 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
532 and pushdecl (for functions generated by the back end). */
534 void
535 retrofit_lang_decl (tree t)
537 struct lang_decl *ld;
538 size_t size;
540 if (CAN_HAVE_FULL_LANG_DECL_P (t))
541 size = sizeof (struct lang_decl);
542 else
543 size = sizeof (struct lang_decl_flags);
545 ld = GGC_CNEWVAR (struct lang_decl, size);
547 ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
548 ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
549 ld->decl_flags.u2sel = 0;
550 if (ld->decl_flags.can_be_full)
551 ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
553 DECL_LANG_SPECIFIC (t) = ld;
554 if (current_lang_name == lang_name_cplusplus
555 || decl_linkage (t) == lk_none)
556 SET_DECL_LANGUAGE (t, lang_cplusplus);
557 else if (current_lang_name == lang_name_c)
558 SET_DECL_LANGUAGE (t, lang_c);
559 else if (current_lang_name == lang_name_java)
560 SET_DECL_LANGUAGE (t, lang_java);
561 else
562 gcc_unreachable ();
564 #ifdef GATHER_STATISTICS
565 tree_node_counts[(int)lang_decl] += 1;
566 tree_node_sizes[(int)lang_decl] += size;
567 #endif
570 void
571 cxx_dup_lang_specific_decl (tree node)
573 int size;
574 struct lang_decl *ld;
576 if (! DECL_LANG_SPECIFIC (node))
577 return;
579 if (!CAN_HAVE_FULL_LANG_DECL_P (node))
580 size = sizeof (struct lang_decl_flags);
581 else
582 size = sizeof (struct lang_decl);
583 ld = GGC_NEWVAR (struct lang_decl, size);
584 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
585 DECL_LANG_SPECIFIC (node) = ld;
587 #ifdef GATHER_STATISTICS
588 tree_node_counts[(int)lang_decl] += 1;
589 tree_node_sizes[(int)lang_decl] += size;
590 #endif
593 /* Copy DECL, including any language-specific parts. */
595 tree
596 copy_decl (tree decl)
598 tree copy;
600 copy = copy_node (decl);
601 cxx_dup_lang_specific_decl (copy);
602 return copy;
605 /* Replace the shared language-specific parts of NODE with a new copy. */
607 static void
608 copy_lang_type (tree node)
610 int size;
611 struct lang_type *lt;
613 if (! TYPE_LANG_SPECIFIC (node))
614 return;
616 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
617 size = sizeof (struct lang_type);
618 else
619 size = sizeof (struct lang_type_ptrmem);
620 lt = GGC_NEWVAR (struct lang_type, size);
621 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
622 TYPE_LANG_SPECIFIC (node) = lt;
624 #ifdef GATHER_STATISTICS
625 tree_node_counts[(int)lang_type] += 1;
626 tree_node_sizes[(int)lang_type] += size;
627 #endif
630 /* Copy TYPE, including any language-specific parts. */
632 tree
633 copy_type (tree type)
635 tree copy;
637 copy = copy_node (type);
638 copy_lang_type (copy);
639 return copy;
642 tree
643 cxx_make_type (enum tree_code code)
645 tree t = make_node (code);
647 /* Create lang_type structure. */
648 if (RECORD_OR_UNION_CODE_P (code)
649 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
651 struct lang_type *pi = GGC_CNEW (struct lang_type);
653 TYPE_LANG_SPECIFIC (t) = pi;
654 pi->u.c.h.is_lang_type_class = 1;
656 #ifdef GATHER_STATISTICS
657 tree_node_counts[(int)lang_type] += 1;
658 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
659 #endif
662 /* Set up some flags that give proper default behavior. */
663 if (RECORD_OR_UNION_CODE_P (code))
665 struct c_fileinfo *finfo = get_fileinfo (input_filename);
666 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
667 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
670 return t;
673 tree
674 make_class_type (enum tree_code code)
676 tree t = cxx_make_type (code);
677 SET_CLASS_TYPE_P (t, 1);
678 return t;
681 /* Returns true if we are currently in the main source file, or in a
682 template instantiation started from the main source file. */
684 bool
685 in_main_input_context (void)
687 struct tinst_level *tl = outermost_tinst_level();
689 if (tl)
690 return strcmp (main_input_filename,
691 LOCATION_FILE (tl->locus)) == 0;
692 else
693 return strcmp (main_input_filename, input_filename) == 0;