* i386.h (TARGET_GENERIC32, TARGET_GENERIC64): Remove.
[official-gcc.git] / gcc / cp / lex.c
blobd6ed809d61d04d03d644e4cecf791085b22ad11b
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987-2013 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 "cp-tree.h"
31 #include "cpplib.h"
32 #include "flags.h"
33 #include "c-family/c-pragma.h"
34 #include "c-family/c-objc.h"
35 #include "tm_p.h"
36 #include "timevar.h"
38 static int interface_strcmp (const char *);
39 static void init_cp_pragma (void);
41 static tree parse_strconst_pragma (const char *, int);
42 static void handle_pragma_vtable (cpp_reader *);
43 static void handle_pragma_unit (cpp_reader *);
44 static void handle_pragma_interface (cpp_reader *);
45 static void handle_pragma_implementation (cpp_reader *);
46 static void handle_pragma_java_exceptions (cpp_reader *);
48 static void init_operators (void);
49 static void copy_lang_type (tree);
51 /* A constraint that can be tested at compile time. */
52 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
54 /* Functions and data structures for #pragma interface.
56 `#pragma implementation' means that the main file being compiled
57 is considered to implement (provide) the classes that appear in
58 its main body. I.e., if this is file "foo.cc", and class `bar'
59 is defined in "foo.cc", then we say that "foo.cc implements bar".
61 All main input files "implement" themselves automagically.
63 `#pragma interface' means that unless this file (of the form "foo.h"
64 is not presently being included by file "foo.cc", the
65 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
66 of the vtables nor any of the inline functions defined in foo.h
67 will ever be output.
69 There are cases when we want to link files such as "defs.h" and
70 "main.cc". In this case, we give "defs.h" a `#pragma interface',
71 and "main.cc" has `#pragma implementation "defs.h"'. */
73 struct impl_files
75 const char *filename;
76 struct impl_files *next;
79 static struct impl_files *impl_file_chain;
81 /* True if we saw "#pragma GCC java_exceptions". */
82 bool pragma_java_exceptions;
84 void
85 cxx_finish (void)
87 c_common_finish ();
90 /* A mapping from tree codes to operator name information. */
91 operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
92 /* Similar, but for assignment operators. */
93 operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
95 /* Initialize data structures that keep track of operator names. */
97 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
98 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
99 #include "operators.def"
100 #undef DEF_OPERATOR
102 static void
103 init_operators (void)
105 tree identifier;
106 char buffer[256];
107 struct operator_name_info_t *oni;
109 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
110 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
111 identifier = get_identifier (buffer); \
112 IDENTIFIER_OPNAME_P (identifier) = 1; \
114 oni = (ASSN_P \
115 ? &assignment_operator_name_info[(int) CODE] \
116 : &operator_name_info[(int) CODE]); \
117 oni->identifier = identifier; \
118 oni->name = NAME; \
119 oni->mangled_name = MANGLING; \
120 oni->arity = ARITY;
122 #include "operators.def"
123 #undef DEF_OPERATOR
125 operator_name_info[(int) ERROR_MARK].identifier
126 = get_identifier ("<invalid operator>");
128 /* Handle some special cases. These operators are not defined in
129 the language, but can be produced internally. We may need them
130 for error-reporting. (Eventually, we should ensure that this
131 does not happen. Error messages involving these operators will
132 be confusing to users.) */
134 operator_name_info [(int) INIT_EXPR].name
135 = operator_name_info [(int) MODIFY_EXPR].name;
136 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
137 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
138 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
139 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
140 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
141 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
142 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
143 operator_name_info [(int) ABS_EXPR].name = "abs";
144 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
145 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
146 operator_name_info [(int) RANGE_EXPR].name = "...";
147 operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
149 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
150 = "(exact /=)";
151 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
152 = "(ceiling /=)";
153 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
154 = "(floor /=)";
155 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
156 = "(round /=)";
157 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
158 = "(ceiling %=)";
159 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
160 = "(floor %=)";
161 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
162 = "(round %=)";
165 /* Initialize the reserved words. */
167 void
168 init_reswords (void)
170 unsigned int i;
171 tree id;
172 int mask = 0;
174 if (cxx_dialect < cxx11)
175 mask |= D_CXX0X;
176 if (flag_no_asm)
177 mask |= D_ASM | D_EXT;
178 if (flag_no_gnu_keywords)
179 mask |= D_EXT;
181 /* The Objective-C keywords are all context-dependent. */
182 mask |= D_OBJC;
184 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
185 for (i = 0; i < num_c_common_reswords; i++)
187 if (c_common_reswords[i].disable & D_CONLY)
188 continue;
189 id = get_identifier (c_common_reswords[i].word);
190 C_SET_RID_CODE (id, c_common_reswords[i].rid);
191 ridpointers [(int) c_common_reswords[i].rid] = id;
192 if (! (c_common_reswords[i].disable & mask))
193 C_IS_RESERVED_WORD (id) = 1;
197 static void
198 init_cp_pragma (void)
200 c_register_pragma (0, "vtable", handle_pragma_vtable);
201 c_register_pragma (0, "unit", handle_pragma_unit);
202 c_register_pragma (0, "interface", handle_pragma_interface);
203 c_register_pragma (0, "implementation", handle_pragma_implementation);
204 c_register_pragma ("GCC", "interface", handle_pragma_interface);
205 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
206 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
209 /* TRUE if a code represents a statement. */
211 bool statement_code_p[MAX_TREE_CODES];
213 /* Initialize the C++ front end. This function is very sensitive to
214 the exact order that things are done here. It would be nice if the
215 initialization done by this routine were moved to its subroutines,
216 and the ordering dependencies clarified and reduced. */
217 bool
218 cxx_init (void)
220 location_t saved_loc;
221 unsigned int i;
222 static const enum tree_code stmt_codes[] = {
223 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
224 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
225 IF_STMT, CLEANUP_STMT, FOR_STMT,
226 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
227 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
228 EXPR_STMT
231 memset (&statement_code_p, 0, sizeof (statement_code_p));
232 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
233 statement_code_p[stmt_codes[i]] = true;
235 saved_loc = input_location;
236 input_location = BUILTINS_LOCATION;
238 init_reswords ();
239 init_tree ();
240 init_cp_semantics ();
241 init_operators ();
242 init_method ();
243 init_error ();
245 current_function_decl = NULL;
247 class_type_node = ridpointers[(int) RID_CLASS];
249 cxx_init_decl_processing ();
251 if (c_common_init () == false)
253 input_location = saved_loc;
254 return false;
257 init_cp_pragma ();
259 init_repo ();
261 input_location = saved_loc;
262 return true;
265 /* Return nonzero if S is not considered part of an
266 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
268 static int
269 interface_strcmp (const char* s)
271 /* Set the interface/implementation bits for this scope. */
272 struct impl_files *ifiles;
273 const char *s1;
275 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
277 const char *t1 = ifiles->filename;
278 s1 = s;
280 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
281 continue;
283 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
284 s1++, t1++;
286 /* A match. */
287 if (*s1 == *t1)
288 return 0;
290 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
291 if (strchr (s1, '.') || strchr (t1, '.'))
292 continue;
294 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
295 continue;
297 /* A match. */
298 return 0;
301 /* No matches. */
302 return 1;
307 /* Parse a #pragma whose sole argument is a string constant.
308 If OPT is true, the argument is optional. */
309 static tree
310 parse_strconst_pragma (const char* name, int opt)
312 tree result, x;
313 enum cpp_ttype t;
315 t = pragma_lex (&result);
316 if (t == CPP_STRING)
318 if (pragma_lex (&x) != CPP_EOF)
319 warning (0, "junk at end of #pragma %s", name);
320 return result;
323 if (t == CPP_EOF && opt)
324 return NULL_TREE;
326 error ("invalid #pragma %s", name);
327 return error_mark_node;
330 static void
331 handle_pragma_vtable (cpp_reader* /*dfile*/)
333 parse_strconst_pragma ("vtable", 0);
334 sorry ("#pragma vtable no longer supported");
337 static void
338 handle_pragma_unit (cpp_reader* /*dfile*/)
340 /* Validate syntax, but don't do anything. */
341 parse_strconst_pragma ("unit", 0);
344 static void
345 handle_pragma_interface (cpp_reader* /*dfile*/)
347 tree fname = parse_strconst_pragma ("interface", 1);
348 struct c_fileinfo *finfo;
349 const char *filename;
351 if (fname == error_mark_node)
352 return;
353 else if (fname == 0)
354 filename = lbasename (input_filename);
355 else
356 filename = TREE_STRING_POINTER (fname);
358 finfo = get_fileinfo (input_filename);
360 if (impl_file_chain == 0)
362 /* If this is zero at this point, then we are
363 auto-implementing. */
364 if (main_input_filename == 0)
365 main_input_filename = input_filename;
368 finfo->interface_only = interface_strcmp (filename);
369 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
370 a definition in another file. */
371 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
372 finfo->interface_unknown = 0;
375 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
376 We used to only allow this at toplevel, but that restriction was buggy
377 in older compilers and it seems reasonable to allow it in the headers
378 themselves, too. It only needs to precede the matching #p interface.
380 We don't touch finfo->interface_only or finfo->interface_unknown;
381 the user must specify a matching #p interface for this to have
382 any effect. */
384 static void
385 handle_pragma_implementation (cpp_reader* /*dfile*/)
387 tree fname = parse_strconst_pragma ("implementation", 1);
388 const char *filename;
389 struct impl_files *ifiles = impl_file_chain;
391 if (fname == error_mark_node)
392 return;
394 if (fname == 0)
396 if (main_input_filename)
397 filename = main_input_filename;
398 else
399 filename = input_filename;
400 filename = lbasename (filename);
402 else
404 filename = TREE_STRING_POINTER (fname);
405 if (cpp_included_before (parse_in, filename, input_location))
406 warning (0, "#pragma implementation for %qs appears after "
407 "file is included", filename);
410 for (; ifiles; ifiles = ifiles->next)
412 if (! filename_cmp (ifiles->filename, filename))
413 break;
415 if (ifiles == 0)
417 ifiles = XNEW (struct impl_files);
418 ifiles->filename = xstrdup (filename);
419 ifiles->next = impl_file_chain;
420 impl_file_chain = ifiles;
424 /* Indicate that this file uses Java-personality exception handling. */
425 static void
426 handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
428 tree x;
429 if (pragma_lex (&x) != CPP_EOF)
430 warning (0, "junk at end of #pragma GCC java_exceptions");
432 choose_personality_routine (lang_java);
433 pragma_java_exceptions = true;
436 /* Issue an error message indicating that the lookup of NAME (an
437 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
439 tree
440 unqualified_name_lookup_error (tree name)
442 if (IDENTIFIER_OPNAME_P (name))
444 if (name != ansi_opname (ERROR_MARK))
445 error ("%qD not defined", name);
447 else
449 if (!objc_diagnose_private_ivar (name))
451 error ("%qD was not declared in this scope", name);
452 suggest_alternatives_for (location_of (name), name);
454 /* Prevent repeated error messages by creating a VAR_DECL with
455 this NAME in the innermost block scope. */
456 if (local_bindings_p ())
458 tree decl;
459 decl = build_decl (input_location,
460 VAR_DECL, name, error_mark_node);
461 DECL_CONTEXT (decl) = current_function_decl;
462 push_local_binding (name, decl, 0);
463 /* Mark the variable as used so that we do not get warnings
464 about it being unused later. */
465 TREE_USED (decl) = 1;
469 return error_mark_node;
472 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
473 used as a function. Returns an appropriate expression for
474 NAME. */
476 tree
477 unqualified_fn_lookup_error (tree name)
479 if (processing_template_decl)
481 /* In a template, it is invalid to write "f()" or "f(3)" if no
482 declaration of "f" is available. Historically, G++ and most
483 other compilers accepted that usage since they deferred all name
484 lookup until instantiation time rather than doing unqualified
485 name lookup at template definition time; explain to the user what
486 is going wrong.
488 Note that we have the exact wording of the following message in
489 the manual (trouble.texi, node "Name lookup"), so they need to
490 be kept in synch. */
491 permerror (input_location, "there are no arguments to %qD that depend on a template "
492 "parameter, so a declaration of %qD must be available",
493 name, name);
495 if (!flag_permissive)
497 static bool hint;
498 if (!hint)
500 inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
501 "code, but allowing the use of an undeclared name is "
502 "deprecated)");
503 hint = true;
506 return name;
509 return unqualified_name_lookup_error (name);
512 /* Wrapper around build_lang_decl_loc(). Should gradually move to
513 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
514 build_lang_decl(). */
516 tree
517 build_lang_decl (enum tree_code code, tree name, tree type)
519 return build_lang_decl_loc (input_location, code, name, type);
522 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
523 DECL_LANG_SPECIFIC info to the result. */
525 tree
526 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
528 tree t;
530 t = build_decl (loc, code, name, type);
531 retrofit_lang_decl (t);
533 return t;
536 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
537 and pushdecl (for functions generated by the back end). */
539 void
540 retrofit_lang_decl (tree t)
542 struct lang_decl *ld;
543 size_t size;
544 int sel;
546 if (TREE_CODE (t) == FUNCTION_DECL)
547 sel = 1, size = sizeof (struct lang_decl_fn);
548 else if (TREE_CODE (t) == NAMESPACE_DECL)
549 sel = 2, size = sizeof (struct lang_decl_ns);
550 else if (TREE_CODE (t) == PARM_DECL)
551 sel = 3, size = sizeof (struct lang_decl_parm);
552 else if (LANG_DECL_HAS_MIN (t))
553 sel = 0, size = sizeof (struct lang_decl_min);
554 else
555 gcc_unreachable ();
557 ld = ggc_alloc_cleared_lang_decl (size);
559 ld->u.base.selector = sel;
561 DECL_LANG_SPECIFIC (t) = ld;
562 if (current_lang_name == lang_name_cplusplus
563 || decl_linkage (t) == lk_none)
564 SET_DECL_LANGUAGE (t, lang_cplusplus);
565 else if (current_lang_name == lang_name_c)
566 SET_DECL_LANGUAGE (t, lang_c);
567 else if (current_lang_name == lang_name_java)
568 SET_DECL_LANGUAGE (t, lang_java);
569 else
570 gcc_unreachable ();
572 if (GATHER_STATISTICS)
574 tree_node_counts[(int)lang_decl] += 1;
575 tree_node_sizes[(int)lang_decl] += size;
579 void
580 cxx_dup_lang_specific_decl (tree node)
582 int size;
583 struct lang_decl *ld;
585 if (! DECL_LANG_SPECIFIC (node))
586 return;
588 if (TREE_CODE (node) == FUNCTION_DECL)
589 size = sizeof (struct lang_decl_fn);
590 else if (TREE_CODE (node) == NAMESPACE_DECL)
591 size = sizeof (struct lang_decl_ns);
592 else if (TREE_CODE (node) == PARM_DECL)
593 size = sizeof (struct lang_decl_parm);
594 else if (LANG_DECL_HAS_MIN (node))
595 size = sizeof (struct lang_decl_min);
596 else
597 gcc_unreachable ();
599 ld = ggc_alloc_lang_decl (size);
600 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
601 DECL_LANG_SPECIFIC (node) = ld;
603 if (GATHER_STATISTICS)
605 tree_node_counts[(int)lang_decl] += 1;
606 tree_node_sizes[(int)lang_decl] += size;
610 /* Copy DECL, including any language-specific parts. */
612 tree
613 copy_decl (tree decl)
615 tree copy;
617 copy = copy_node (decl);
618 cxx_dup_lang_specific_decl (copy);
619 return copy;
622 /* Replace the shared language-specific parts of NODE with a new copy. */
624 static void
625 copy_lang_type (tree node)
627 int size;
628 struct lang_type *lt;
630 if (! TYPE_LANG_SPECIFIC (node))
631 return;
633 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
634 size = sizeof (struct lang_type);
635 else
636 size = sizeof (struct lang_type_ptrmem);
637 lt = ggc_alloc_lang_type (size);
638 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
639 TYPE_LANG_SPECIFIC (node) = lt;
641 if (GATHER_STATISTICS)
643 tree_node_counts[(int)lang_type] += 1;
644 tree_node_sizes[(int)lang_type] += size;
648 /* Copy TYPE, including any language-specific parts. */
650 tree
651 copy_type (tree type)
653 tree copy;
655 copy = copy_node (type);
656 copy_lang_type (copy);
657 return copy;
660 tree
661 cxx_make_type (enum tree_code code)
663 tree t = make_node (code);
665 /* Create lang_type structure. */
666 if (RECORD_OR_UNION_CODE_P (code)
667 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
669 struct lang_type *pi
670 = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
672 TYPE_LANG_SPECIFIC (t) = pi;
673 pi->u.c.h.is_lang_type_class = 1;
675 if (GATHER_STATISTICS)
677 tree_node_counts[(int)lang_type] += 1;
678 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
682 /* Set up some flags that give proper default behavior. */
683 if (RECORD_OR_UNION_CODE_P (code))
685 struct c_fileinfo *finfo = get_fileinfo (input_filename);
686 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
687 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
690 return t;
693 tree
694 make_class_type (enum tree_code code)
696 tree t = cxx_make_type (code);
697 SET_CLASS_TYPE_P (t, 1);
698 return t;
701 /* Returns true if we are currently in the main source file, or in a
702 template instantiation started from the main source file. */
704 bool
705 in_main_input_context (void)
707 struct tinst_level *tl = outermost_tinst_level();
709 if (tl)
710 return filename_cmp (main_input_filename,
711 LOCATION_FILE (tl->locus)) == 0;
712 else
713 return filename_cmp (main_input_filename, input_filename) == 0;