* gfortran.dg/debug/pr46756.f: Remove XFAIL for AIX.
[official-gcc.git] / gcc / cp / lex.c
blob3dc11553c5f01802c8cfaa3c3c5133a9bbb39e36
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987-2015 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 "tree.h"
29 #include "cp-tree.h"
30 #include "timevar.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "alias.h"
34 #include "flags.h"
35 #include "c-family/c-pragma.h"
36 #include "c-family/c-objc.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_CXX11;
176 if (!flag_concepts)
177 mask |= D_CXX_CONCEPTS;
178 if (!flag_tm)
179 mask |= D_TRANSMEM;
180 if (flag_no_asm)
181 mask |= D_ASM | D_EXT;
182 if (flag_no_gnu_keywords)
183 mask |= D_EXT;
185 /* The Objective-C keywords are all context-dependent. */
186 mask |= D_OBJC;
188 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
189 for (i = 0; i < num_c_common_reswords; i++)
191 if (c_common_reswords[i].disable & D_CONLY)
192 continue;
193 id = get_identifier (c_common_reswords[i].word);
194 C_SET_RID_CODE (id, c_common_reswords[i].rid);
195 ridpointers [(int) c_common_reswords[i].rid] = id;
196 if (! (c_common_reswords[i].disable & mask))
197 C_IS_RESERVED_WORD (id) = 1;
200 for (i = 0; i < NUM_INT_N_ENTS; i++)
202 char name[50];
203 sprintf (name, "__int%d", int_n_data[i].bitsize);
204 id = get_identifier (name);
205 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
206 C_IS_RESERVED_WORD (id) = 1;
210 static void
211 init_cp_pragma (void)
213 c_register_pragma (0, "vtable", handle_pragma_vtable);
214 c_register_pragma (0, "unit", handle_pragma_unit);
215 c_register_pragma (0, "interface", handle_pragma_interface);
216 c_register_pragma (0, "implementation", handle_pragma_implementation);
217 c_register_pragma ("GCC", "interface", handle_pragma_interface);
218 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
219 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
222 /* TRUE if a code represents a statement. */
224 bool statement_code_p[MAX_TREE_CODES];
226 /* Initialize the C++ front end. This function is very sensitive to
227 the exact order that things are done here. It would be nice if the
228 initialization done by this routine were moved to its subroutines,
229 and the ordering dependencies clarified and reduced. */
230 bool
231 cxx_init (void)
233 location_t saved_loc;
234 unsigned int i;
235 static const enum tree_code stmt_codes[] = {
236 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
237 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
238 IF_STMT, CLEANUP_STMT, FOR_STMT,
239 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
240 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
241 EXPR_STMT
244 memset (&statement_code_p, 0, sizeof (statement_code_p));
245 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
246 statement_code_p[stmt_codes[i]] = true;
248 saved_loc = input_location;
249 input_location = BUILTINS_LOCATION;
251 init_reswords ();
252 init_tree ();
253 init_cp_semantics ();
254 init_operators ();
255 init_method ();
257 current_function_decl = NULL;
259 class_type_node = ridpointers[(int) RID_CLASS];
261 cxx_init_decl_processing ();
263 if (c_common_init () == false)
265 input_location = saved_loc;
266 return false;
269 init_cp_pragma ();
271 init_repo ();
273 input_location = saved_loc;
274 return true;
277 /* Return nonzero if S is not considered part of an
278 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
280 static int
281 interface_strcmp (const char* s)
283 /* Set the interface/implementation bits for this scope. */
284 struct impl_files *ifiles;
285 const char *s1;
287 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
289 const char *t1 = ifiles->filename;
290 s1 = s;
292 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
293 continue;
295 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
296 s1++, t1++;
298 /* A match. */
299 if (*s1 == *t1)
300 return 0;
302 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
303 if (strchr (s1, '.') || strchr (t1, '.'))
304 continue;
306 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
307 continue;
309 /* A match. */
310 return 0;
313 /* No matches. */
314 return 1;
319 /* Parse a #pragma whose sole argument is a string constant.
320 If OPT is true, the argument is optional. */
321 static tree
322 parse_strconst_pragma (const char* name, int opt)
324 tree result, x;
325 enum cpp_ttype t;
327 t = pragma_lex (&result);
328 if (t == CPP_STRING)
330 if (pragma_lex (&x) != CPP_EOF)
331 warning (0, "junk at end of #pragma %s", name);
332 return result;
335 if (t == CPP_EOF && opt)
336 return NULL_TREE;
338 error ("invalid #pragma %s", name);
339 return error_mark_node;
342 static void
343 handle_pragma_vtable (cpp_reader* /*dfile*/)
345 parse_strconst_pragma ("vtable", 0);
346 sorry ("#pragma vtable no longer supported");
349 static void
350 handle_pragma_unit (cpp_reader* /*dfile*/)
352 /* Validate syntax, but don't do anything. */
353 parse_strconst_pragma ("unit", 0);
356 static void
357 handle_pragma_interface (cpp_reader* /*dfile*/)
359 tree fname = parse_strconst_pragma ("interface", 1);
360 struct c_fileinfo *finfo;
361 const char *filename;
363 if (fname == error_mark_node)
364 return;
365 else if (fname == 0)
366 filename = lbasename (LOCATION_FILE (input_location));
367 else
368 filename = TREE_STRING_POINTER (fname);
370 finfo = get_fileinfo (LOCATION_FILE (input_location));
372 if (impl_file_chain == 0)
374 /* If this is zero at this point, then we are
375 auto-implementing. */
376 if (main_input_filename == 0)
377 main_input_filename = LOCATION_FILE (input_location);
380 finfo->interface_only = interface_strcmp (filename);
381 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
382 a definition in another file. */
383 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
384 finfo->interface_unknown = 0;
387 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
388 We used to only allow this at toplevel, but that restriction was buggy
389 in older compilers and it seems reasonable to allow it in the headers
390 themselves, too. It only needs to precede the matching #p interface.
392 We don't touch finfo->interface_only or finfo->interface_unknown;
393 the user must specify a matching #p interface for this to have
394 any effect. */
396 static void
397 handle_pragma_implementation (cpp_reader* /*dfile*/)
399 tree fname = parse_strconst_pragma ("implementation", 1);
400 const char *filename;
401 struct impl_files *ifiles = impl_file_chain;
403 if (fname == error_mark_node)
404 return;
406 if (fname == 0)
408 if (main_input_filename)
409 filename = main_input_filename;
410 else
411 filename = LOCATION_FILE (input_location);
412 filename = lbasename (filename);
414 else
416 filename = TREE_STRING_POINTER (fname);
417 if (cpp_included_before (parse_in, filename, input_location))
418 warning (0, "#pragma implementation for %qs appears after "
419 "file is included", filename);
422 for (; ifiles; ifiles = ifiles->next)
424 if (! filename_cmp (ifiles->filename, filename))
425 break;
427 if (ifiles == 0)
429 ifiles = XNEW (struct impl_files);
430 ifiles->filename = xstrdup (filename);
431 ifiles->next = impl_file_chain;
432 impl_file_chain = ifiles;
436 /* Indicate that this file uses Java-personality exception handling. */
437 static void
438 handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
440 tree x;
441 if (pragma_lex (&x) != CPP_EOF)
442 warning (0, "junk at end of #pragma GCC java_exceptions");
444 choose_personality_routine (lang_java);
445 pragma_java_exceptions = true;
448 /* Issue an error message indicating that the lookup of NAME (an
449 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
451 tree
452 unqualified_name_lookup_error (tree name)
454 if (IDENTIFIER_OPNAME_P (name))
456 if (name != ansi_opname (ERROR_MARK))
457 error ("%qD not defined", name);
459 else
461 if (!objc_diagnose_private_ivar (name))
463 error ("%qD was not declared in this scope", name);
464 suggest_alternatives_for (location_of (name), name);
466 /* Prevent repeated error messages by creating a VAR_DECL with
467 this NAME in the innermost block scope. */
468 if (local_bindings_p ())
470 tree decl;
471 decl = build_decl (input_location,
472 VAR_DECL, name, error_mark_node);
473 DECL_CONTEXT (decl) = current_function_decl;
474 push_local_binding (name, decl, 0);
475 /* Mark the variable as used so that we do not get warnings
476 about it being unused later. */
477 TREE_USED (decl) = 1;
481 return error_mark_node;
484 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
485 used as a function. Returns an appropriate expression for
486 NAME. */
488 tree
489 unqualified_fn_lookup_error (tree name)
491 if (processing_template_decl)
493 /* In a template, it is invalid to write "f()" or "f(3)" if no
494 declaration of "f" is available. Historically, G++ and most
495 other compilers accepted that usage since they deferred all name
496 lookup until instantiation time rather than doing unqualified
497 name lookup at template definition time; explain to the user what
498 is going wrong.
500 Note that we have the exact wording of the following message in
501 the manual (trouble.texi, node "Name lookup"), so they need to
502 be kept in synch. */
503 permerror (input_location, "there are no arguments to %qD that depend on a template "
504 "parameter, so a declaration of %qD must be available",
505 name, name);
507 if (!flag_permissive)
509 static bool hint;
510 if (!hint)
512 inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
513 "code, but allowing the use of an undeclared name is "
514 "deprecated)");
515 hint = true;
518 return name;
521 return unqualified_name_lookup_error (name);
524 /* Wrapper around build_lang_decl_loc(). Should gradually move to
525 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
526 build_lang_decl(). */
528 tree
529 build_lang_decl (enum tree_code code, tree name, tree type)
531 return build_lang_decl_loc (input_location, code, name, type);
534 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
535 DECL_LANG_SPECIFIC info to the result. */
537 tree
538 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
540 tree t;
542 t = build_decl (loc, code, name, type);
543 retrofit_lang_decl (t);
545 return t;
548 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
549 and pushdecl (for functions generated by the back end). */
551 void
552 retrofit_lang_decl (tree t)
554 struct lang_decl *ld;
555 size_t size;
556 int sel;
558 if (DECL_LANG_SPECIFIC (t))
559 return;
561 if (TREE_CODE (t) == FUNCTION_DECL)
562 sel = 1, size = sizeof (struct lang_decl_fn);
563 else if (TREE_CODE (t) == NAMESPACE_DECL)
564 sel = 2, size = sizeof (struct lang_decl_ns);
565 else if (TREE_CODE (t) == PARM_DECL)
566 sel = 3, size = sizeof (struct lang_decl_parm);
567 else if (LANG_DECL_HAS_MIN (t))
568 sel = 0, size = sizeof (struct lang_decl_min);
569 else
570 gcc_unreachable ();
572 ld = (struct lang_decl *) ggc_internal_cleared_alloc (size);
574 ld->u.base.selector = sel;
576 DECL_LANG_SPECIFIC (t) = ld;
577 if (current_lang_name == lang_name_cplusplus
578 || decl_linkage (t) == lk_none)
579 SET_DECL_LANGUAGE (t, lang_cplusplus);
580 else if (current_lang_name == lang_name_c)
581 SET_DECL_LANGUAGE (t, lang_c);
582 else if (current_lang_name == lang_name_java)
583 SET_DECL_LANGUAGE (t, lang_java);
584 else
585 gcc_unreachable ();
587 if (GATHER_STATISTICS)
589 tree_node_counts[(int)lang_decl] += 1;
590 tree_node_sizes[(int)lang_decl] += size;
594 void
595 cxx_dup_lang_specific_decl (tree node)
597 int size;
598 struct lang_decl *ld;
600 if (! DECL_LANG_SPECIFIC (node))
601 return;
603 if (TREE_CODE (node) == FUNCTION_DECL)
604 size = sizeof (struct lang_decl_fn);
605 else if (TREE_CODE (node) == NAMESPACE_DECL)
606 size = sizeof (struct lang_decl_ns);
607 else if (TREE_CODE (node) == PARM_DECL)
608 size = sizeof (struct lang_decl_parm);
609 else if (LANG_DECL_HAS_MIN (node))
610 size = sizeof (struct lang_decl_min);
611 else
612 gcc_unreachable ();
614 ld = (struct lang_decl *) ggc_internal_alloc (size);
615 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
616 DECL_LANG_SPECIFIC (node) = ld;
618 if (GATHER_STATISTICS)
620 tree_node_counts[(int)lang_decl] += 1;
621 tree_node_sizes[(int)lang_decl] += size;
625 /* Copy DECL, including any language-specific parts. */
627 tree
628 copy_decl (tree decl)
630 tree copy;
632 copy = copy_node (decl);
633 cxx_dup_lang_specific_decl (copy);
634 return copy;
637 /* Replace the shared language-specific parts of NODE with a new copy. */
639 static void
640 copy_lang_type (tree node)
642 int size;
643 struct lang_type *lt;
645 if (! TYPE_LANG_SPECIFIC (node))
646 return;
648 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
649 size = sizeof (struct lang_type);
650 else
651 size = sizeof (struct lang_type_ptrmem);
652 lt = (struct lang_type *) ggc_internal_alloc (size);
653 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
654 TYPE_LANG_SPECIFIC (node) = lt;
656 if (GATHER_STATISTICS)
658 tree_node_counts[(int)lang_type] += 1;
659 tree_node_sizes[(int)lang_type] += size;
663 /* Copy TYPE, including any language-specific parts. */
665 tree
666 copy_type (tree type)
668 tree copy;
670 copy = copy_node (type);
671 copy_lang_type (copy);
672 return copy;
675 tree
676 cxx_make_type (enum tree_code code)
678 tree t = make_node (code);
680 /* Create lang_type structure. */
681 if (RECORD_OR_UNION_CODE_P (code)
682 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
684 struct lang_type *pi
685 = (struct lang_type *) ggc_internal_cleared_alloc
686 (sizeof (struct lang_type));
688 TYPE_LANG_SPECIFIC (t) = pi;
689 pi->u.c.h.is_lang_type_class = 1;
691 if (GATHER_STATISTICS)
693 tree_node_counts[(int)lang_type] += 1;
694 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
698 /* Set up some flags that give proper default behavior. */
699 if (RECORD_OR_UNION_CODE_P (code))
701 struct c_fileinfo *finfo = \
702 get_fileinfo (LOCATION_FILE (input_location));
703 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
704 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
707 return t;
710 tree
711 make_class_type (enum tree_code code)
713 tree t = cxx_make_type (code);
714 SET_CLASS_TYPE_P (t, 1);
715 return t;
718 /* Returns true if we are currently in the main source file, or in a
719 template instantiation started from the main source file. */
721 bool
722 in_main_input_context (void)
724 struct tinst_level *tl = outermost_tinst_level();
726 if (tl)
727 return filename_cmp (main_input_filename,
728 LOCATION_FILE (tl->locus)) == 0;
729 else
730 return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;