Merge from trunk
[official-gcc.git] / gcc / cp / lex.c
blob58bc847d5feeed09a149f634190893361a2c9679
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is the lexical analyzer for GNU C++. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "input.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "cp-tree.h"
32 #include "cpplib.h"
33 #include "flags.h"
34 #include "c-family/c-pragma.h"
35 #include "c-family/c-objc.h"
36 #include "tm_p.h"
37 #include "timevar.h"
39 static int interface_strcmp (const char *);
40 static void init_cp_pragma (void);
42 static tree parse_strconst_pragma (const char *, int);
43 static void handle_pragma_vtable (cpp_reader *);
44 static void handle_pragma_unit (cpp_reader *);
45 static void handle_pragma_interface (cpp_reader *);
46 static void handle_pragma_implementation (cpp_reader *);
47 static void handle_pragma_java_exceptions (cpp_reader *);
49 static void init_operators (void);
50 static void copy_lang_type (tree);
52 /* A constraint that can be tested at compile time. */
53 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
55 /* Functions and data structures for #pragma interface.
57 `#pragma implementation' means that the main file being compiled
58 is considered to implement (provide) the classes that appear in
59 its main body. I.e., if this is file "foo.cc", and class `bar'
60 is defined in "foo.cc", then we say that "foo.cc implements bar".
62 All main input files "implement" themselves automagically.
64 `#pragma interface' means that unless this file (of the form "foo.h"
65 is not presently being included by file "foo.cc", the
66 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
67 of the vtables nor any of the inline functions defined in foo.h
68 will ever be output.
70 There are cases when we want to link files such as "defs.h" and
71 "main.cc". In this case, we give "defs.h" a `#pragma interface',
72 and "main.cc" has `#pragma implementation "defs.h"'. */
74 struct impl_files
76 const char *filename;
77 struct impl_files *next;
80 static struct impl_files *impl_file_chain;
82 /* True if we saw "#pragma GCC java_exceptions". */
83 bool pragma_java_exceptions;
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 if (cxx_dialect < cxx11)
176 mask |= D_CXX0X;
177 if (flag_no_asm)
178 mask |= D_ASM | D_EXT;
179 if (flag_no_gnu_keywords)
180 mask |= D_EXT;
182 /* The Objective-C keywords are all context-dependent. */
183 mask |= D_OBJC;
185 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
186 for (i = 0; i < num_c_common_reswords; i++)
188 if (c_common_reswords[i].disable & D_CONLY)
189 continue;
190 id = get_identifier (c_common_reswords[i].word);
191 C_SET_RID_CODE (id, c_common_reswords[i].rid);
192 ridpointers [(int) c_common_reswords[i].rid] = id;
193 if (! (c_common_reswords[i].disable & mask))
194 C_IS_RESERVED_WORD (id) = 1;
198 static void
199 init_cp_pragma (void)
201 c_register_pragma (0, "vtable", handle_pragma_vtable);
202 c_register_pragma (0, "unit", handle_pragma_unit);
203 c_register_pragma (0, "interface", handle_pragma_interface);
204 c_register_pragma (0, "implementation", handle_pragma_implementation);
205 c_register_pragma ("GCC", "interface", handle_pragma_interface);
206 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
207 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
210 /* TRUE if a code represents a statement. */
212 bool statement_code_p[MAX_TREE_CODES];
214 /* Initialize the C++ front end. This function is very sensitive to
215 the exact order that things are done here. It would be nice if the
216 initialization done by this routine were moved to its subroutines,
217 and the ordering dependencies clarified and reduced. */
218 bool
219 cxx_init (void)
221 location_t saved_loc;
222 unsigned int i;
223 static const enum tree_code stmt_codes[] = {
224 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
225 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
226 IF_STMT, CLEANUP_STMT, FOR_STMT,
227 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
228 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
229 EXPR_STMT
232 flag_concepts = true;
234 memset (&statement_code_p, 0, sizeof (statement_code_p));
235 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
236 statement_code_p[stmt_codes[i]] = true;
238 saved_loc = input_location;
239 input_location = BUILTINS_LOCATION;
241 init_reswords ();
242 init_tree ();
243 init_cp_semantics ();
244 init_operators ();
245 init_method ();
246 init_error ();
248 current_function_decl = NULL;
250 class_type_node = ridpointers[(int) RID_CLASS];
252 cxx_init_decl_processing ();
254 if (c_common_init () == false)
256 input_location = saved_loc;
257 return false;
260 init_cp_pragma ();
262 init_repo ();
264 input_location = saved_loc;
265 return true;
268 /* Return nonzero if S is not considered part of an
269 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
271 static int
272 interface_strcmp (const char* s)
274 /* Set the interface/implementation bits for this scope. */
275 struct impl_files *ifiles;
276 const char *s1;
278 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
280 const char *t1 = ifiles->filename;
281 s1 = s;
283 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
284 continue;
286 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
287 s1++, t1++;
289 /* A match. */
290 if (*s1 == *t1)
291 return 0;
293 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
294 if (strchr (s1, '.') || strchr (t1, '.'))
295 continue;
297 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
298 continue;
300 /* A match. */
301 return 0;
304 /* No matches. */
305 return 1;
310 /* Parse a #pragma whose sole argument is a string constant.
311 If OPT is true, the argument is optional. */
312 static tree
313 parse_strconst_pragma (const char* name, int opt)
315 tree result, x;
316 enum cpp_ttype t;
318 t = pragma_lex (&result);
319 if (t == CPP_STRING)
321 if (pragma_lex (&x) != CPP_EOF)
322 warning (0, "junk at end of #pragma %s", name);
323 return result;
326 if (t == CPP_EOF && opt)
327 return NULL_TREE;
329 error ("invalid #pragma %s", name);
330 return error_mark_node;
333 static void
334 handle_pragma_vtable (cpp_reader* /*dfile*/)
336 parse_strconst_pragma ("vtable", 0);
337 sorry ("#pragma vtable no longer supported");
340 static void
341 handle_pragma_unit (cpp_reader* /*dfile*/)
343 /* Validate syntax, but don't do anything. */
344 parse_strconst_pragma ("unit", 0);
347 static void
348 handle_pragma_interface (cpp_reader* /*dfile*/)
350 tree fname = parse_strconst_pragma ("interface", 1);
351 struct c_fileinfo *finfo;
352 const char *filename;
354 if (fname == error_mark_node)
355 return;
356 else if (fname == 0)
357 filename = lbasename (LOCATION_FILE (input_location));
358 else
359 filename = TREE_STRING_POINTER (fname);
361 finfo = get_fileinfo (LOCATION_FILE (input_location));
363 if (impl_file_chain == 0)
365 /* If this is zero at this point, then we are
366 auto-implementing. */
367 if (main_input_filename == 0)
368 main_input_filename = LOCATION_FILE (input_location);
371 finfo->interface_only = interface_strcmp (filename);
372 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
373 a definition in another file. */
374 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
375 finfo->interface_unknown = 0;
378 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
379 We used to only allow this at toplevel, but that restriction was buggy
380 in older compilers and it seems reasonable to allow it in the headers
381 themselves, too. It only needs to precede the matching #p interface.
383 We don't touch finfo->interface_only or finfo->interface_unknown;
384 the user must specify a matching #p interface for this to have
385 any effect. */
387 static void
388 handle_pragma_implementation (cpp_reader* /*dfile*/)
390 tree fname = parse_strconst_pragma ("implementation", 1);
391 const char *filename;
392 struct impl_files *ifiles = impl_file_chain;
394 if (fname == error_mark_node)
395 return;
397 if (fname == 0)
399 if (main_input_filename)
400 filename = main_input_filename;
401 else
402 filename = LOCATION_FILE (input_location);
403 filename = lbasename (filename);
405 else
407 filename = TREE_STRING_POINTER (fname);
408 if (cpp_included_before (parse_in, filename, input_location))
409 warning (0, "#pragma implementation for %qs appears after "
410 "file is included", filename);
413 for (; ifiles; ifiles = ifiles->next)
415 if (! filename_cmp (ifiles->filename, filename))
416 break;
418 if (ifiles == 0)
420 ifiles = XNEW (struct impl_files);
421 ifiles->filename = xstrdup (filename);
422 ifiles->next = impl_file_chain;
423 impl_file_chain = ifiles;
427 /* Indicate that this file uses Java-personality exception handling. */
428 static void
429 handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
431 tree x;
432 if (pragma_lex (&x) != CPP_EOF)
433 warning (0, "junk at end of #pragma GCC java_exceptions");
435 choose_personality_routine (lang_java);
436 pragma_java_exceptions = true;
439 /* Issue an error message indicating that the lookup of NAME (an
440 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
442 tree
443 unqualified_name_lookup_error (tree name)
445 if (IDENTIFIER_OPNAME_P (name))
447 if (name != ansi_opname (ERROR_MARK))
448 error ("%qD not defined", name);
450 else
452 if (!objc_diagnose_private_ivar (name))
454 error ("%qD was not declared in this scope", name);
455 suggest_alternatives_for (location_of (name), name);
457 /* Prevent repeated error messages by creating a VAR_DECL with
458 this NAME in the innermost block scope. */
459 if (local_bindings_p ())
461 tree decl;
462 decl = build_decl (input_location,
463 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 (input_location, "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 (input_location, "(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 /* Wrapper around build_lang_decl_loc(). Should gradually move to
516 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
517 build_lang_decl(). */
519 tree
520 build_lang_decl (enum tree_code code, tree name, tree type)
522 return build_lang_decl_loc (input_location, code, name, type);
525 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
526 DECL_LANG_SPECIFIC info to the result. */
528 tree
529 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
531 tree t;
533 t = build_decl (loc, code, name, type);
534 retrofit_lang_decl (t);
536 return t;
539 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
540 and pushdecl (for functions generated by the back end). */
542 void
543 retrofit_lang_decl (tree t)
545 struct lang_decl *ld;
546 size_t size;
547 int sel;
549 if (TREE_CODE (t) == FUNCTION_DECL)
550 sel = 1, size = sizeof (struct lang_decl_fn);
551 else if (TREE_CODE (t) == NAMESPACE_DECL)
552 sel = 2, size = sizeof (struct lang_decl_ns);
553 else if (TREE_CODE (t) == PARM_DECL)
554 sel = 3, size = sizeof (struct lang_decl_parm);
555 else if (LANG_DECL_HAS_MIN (t))
556 sel = 0, size = sizeof (struct lang_decl_min);
557 else
558 gcc_unreachable ();
560 ld = ggc_alloc_cleared_lang_decl (size);
562 ld->u.base.selector = sel;
564 DECL_LANG_SPECIFIC (t) = ld;
565 if (current_lang_name == lang_name_cplusplus
566 || decl_linkage (t) == lk_none)
567 SET_DECL_LANGUAGE (t, lang_cplusplus);
568 else if (current_lang_name == lang_name_c)
569 SET_DECL_LANGUAGE (t, lang_c);
570 else if (current_lang_name == lang_name_java)
571 SET_DECL_LANGUAGE (t, lang_java);
572 else
573 gcc_unreachable ();
575 if (GATHER_STATISTICS)
577 tree_node_counts[(int)lang_decl] += 1;
578 tree_node_sizes[(int)lang_decl] += size;
582 void
583 cxx_dup_lang_specific_decl (tree node)
585 int size;
586 struct lang_decl *ld;
588 if (! DECL_LANG_SPECIFIC (node))
589 return;
591 if (TREE_CODE (node) == FUNCTION_DECL)
592 size = sizeof (struct lang_decl_fn);
593 else if (TREE_CODE (node) == NAMESPACE_DECL)
594 size = sizeof (struct lang_decl_ns);
595 else if (TREE_CODE (node) == PARM_DECL)
596 size = sizeof (struct lang_decl_parm);
597 else if (LANG_DECL_HAS_MIN (node))
598 size = sizeof (struct lang_decl_min);
599 else
600 gcc_unreachable ();
602 ld = ggc_alloc_lang_decl (size);
603 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
604 DECL_LANG_SPECIFIC (node) = ld;
606 if (GATHER_STATISTICS)
608 tree_node_counts[(int)lang_decl] += 1;
609 tree_node_sizes[(int)lang_decl] += size;
613 /* Copy DECL, including any language-specific parts. */
615 tree
616 copy_decl (tree decl)
618 tree copy;
620 copy = copy_node (decl);
621 cxx_dup_lang_specific_decl (copy);
622 return copy;
625 /* Replace the shared language-specific parts of NODE with a new copy. */
627 static void
628 copy_lang_type (tree node)
630 int size;
631 struct lang_type *lt;
633 if (! TYPE_LANG_SPECIFIC (node))
634 return;
636 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
637 size = sizeof (struct lang_type);
638 else
639 size = sizeof (struct lang_type_ptrmem);
640 lt = ggc_alloc_lang_type (size);
641 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
642 TYPE_LANG_SPECIFIC (node) = lt;
644 if (GATHER_STATISTICS)
646 tree_node_counts[(int)lang_type] += 1;
647 tree_node_sizes[(int)lang_type] += size;
651 /* Copy TYPE, including any language-specific parts. */
653 tree
654 copy_type (tree type)
656 tree copy;
658 copy = copy_node (type);
659 copy_lang_type (copy);
660 return copy;
663 tree
664 cxx_make_type (enum tree_code code)
666 tree t = make_node (code);
668 /* Create lang_type structure. */
669 if (RECORD_OR_UNION_CODE_P (code)
670 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
672 struct lang_type *pi
673 = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
675 TYPE_LANG_SPECIFIC (t) = pi;
676 pi->u.c.h.is_lang_type_class = 1;
678 if (GATHER_STATISTICS)
680 tree_node_counts[(int)lang_type] += 1;
681 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
685 /* Set up some flags that give proper default behavior. */
686 if (RECORD_OR_UNION_CODE_P (code))
688 struct c_fileinfo *finfo = \
689 get_fileinfo (LOCATION_FILE (input_location));
690 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
691 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
694 return t;
697 tree
698 make_class_type (enum tree_code code)
700 tree t = cxx_make_type (code);
701 SET_CLASS_TYPE_P (t, 1);
702 return t;
705 /* Returns true if we are currently in the main source file, or in a
706 template instantiation started from the main source file. */
708 bool
709 in_main_input_context (void)
711 struct tinst_level *tl = outermost_tinst_level();
713 if (tl)
714 return filename_cmp (main_input_filename,
715 LOCATION_FILE (tl->locus)) == 0;
716 else
717 return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;