* config/i386/i386.h (TARGET_SUPPORTS_WIDE_INT): New define.
[official-gcc.git] / gcc / cp / lex.c
blob0fced4f9fd6ab95d4d23f7ee151c05708a32f921
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 "input.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "vec.h"
32 #include "double-int.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "wide-int.h"
37 #include "inchash.h"
38 #include "tree.h"
39 #include "stringpool.h"
40 #include "cp-tree.h"
41 #include "cpplib.h"
42 #include "flags.h"
43 #include "c-family/c-pragma.h"
44 #include "c-family/c-objc.h"
45 #include "tm_p.h"
46 #include "timevar.h"
48 static int interface_strcmp (const char *);
49 static void init_cp_pragma (void);
51 static tree parse_strconst_pragma (const char *, int);
52 static void handle_pragma_vtable (cpp_reader *);
53 static void handle_pragma_unit (cpp_reader *);
54 static void handle_pragma_interface (cpp_reader *);
55 static void handle_pragma_implementation (cpp_reader *);
56 static void handle_pragma_java_exceptions (cpp_reader *);
58 static void init_operators (void);
59 static void copy_lang_type (tree);
61 /* A constraint that can be tested at compile time. */
62 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
64 /* Functions and data structures for #pragma interface.
66 `#pragma implementation' means that the main file being compiled
67 is considered to implement (provide) the classes that appear in
68 its main body. I.e., if this is file "foo.cc", and class `bar'
69 is defined in "foo.cc", then we say that "foo.cc implements bar".
71 All main input files "implement" themselves automagically.
73 `#pragma interface' means that unless this file (of the form "foo.h"
74 is not presently being included by file "foo.cc", the
75 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
76 of the vtables nor any of the inline functions defined in foo.h
77 will ever be output.
79 There are cases when we want to link files such as "defs.h" and
80 "main.cc". In this case, we give "defs.h" a `#pragma interface',
81 and "main.cc" has `#pragma implementation "defs.h"'. */
83 struct impl_files
85 const char *filename;
86 struct impl_files *next;
89 static struct impl_files *impl_file_chain;
91 /* True if we saw "#pragma GCC java_exceptions". */
92 bool pragma_java_exceptions;
94 void
95 cxx_finish (void)
97 c_common_finish ();
100 /* A mapping from tree codes to operator name information. */
101 operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
102 /* Similar, but for assignment operators. */
103 operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
105 /* Initialize data structures that keep track of operator names. */
107 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
108 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
109 #include "operators.def"
110 #undef DEF_OPERATOR
112 static void
113 init_operators (void)
115 tree identifier;
116 char buffer[256];
117 struct operator_name_info_t *oni;
119 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
120 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
121 identifier = get_identifier (buffer); \
122 IDENTIFIER_OPNAME_P (identifier) = 1; \
124 oni = (ASSN_P \
125 ? &assignment_operator_name_info[(int) CODE] \
126 : &operator_name_info[(int) CODE]); \
127 oni->identifier = identifier; \
128 oni->name = NAME; \
129 oni->mangled_name = MANGLING; \
130 oni->arity = ARITY;
132 #include "operators.def"
133 #undef DEF_OPERATOR
135 operator_name_info[(int) ERROR_MARK].identifier
136 = get_identifier ("<invalid operator>");
138 /* Handle some special cases. These operators are not defined in
139 the language, but can be produced internally. We may need them
140 for error-reporting. (Eventually, we should ensure that this
141 does not happen. Error messages involving these operators will
142 be confusing to users.) */
144 operator_name_info [(int) INIT_EXPR].name
145 = operator_name_info [(int) MODIFY_EXPR].name;
146 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
147 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
148 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
149 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
150 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
151 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
152 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
153 operator_name_info [(int) ABS_EXPR].name = "abs";
154 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
155 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
156 operator_name_info [(int) RANGE_EXPR].name = "...";
157 operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
159 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
160 = "(exact /=)";
161 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
162 = "(ceiling /=)";
163 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
164 = "(floor /=)";
165 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
166 = "(round /=)";
167 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
168 = "(ceiling %=)";
169 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
170 = "(floor %=)";
171 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
172 = "(round %=)";
175 /* Initialize the reserved words. */
177 void
178 init_reswords (void)
180 unsigned int i;
181 tree id;
182 int mask = 0;
184 if (cxx_dialect < cxx11)
185 mask |= D_CXX0X;
186 if (flag_no_asm)
187 mask |= D_ASM | D_EXT;
188 if (flag_no_gnu_keywords)
189 mask |= D_EXT;
191 /* The Objective-C keywords are all context-dependent. */
192 mask |= D_OBJC;
194 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
195 for (i = 0; i < num_c_common_reswords; i++)
197 if (c_common_reswords[i].disable & D_CONLY)
198 continue;
199 id = get_identifier (c_common_reswords[i].word);
200 C_SET_RID_CODE (id, c_common_reswords[i].rid);
201 ridpointers [(int) c_common_reswords[i].rid] = id;
202 if (! (c_common_reswords[i].disable & mask))
203 C_IS_RESERVED_WORD (id) = 1;
206 for (i = 0; i < NUM_INT_N_ENTS; i++)
208 char name[50];
209 sprintf (name, "__int%d", int_n_data[i].bitsize);
210 id = get_identifier (name);
211 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
212 C_IS_RESERVED_WORD (id) = 1;
216 static void
217 init_cp_pragma (void)
219 c_register_pragma (0, "vtable", handle_pragma_vtable);
220 c_register_pragma (0, "unit", handle_pragma_unit);
221 c_register_pragma (0, "interface", handle_pragma_interface);
222 c_register_pragma (0, "implementation", handle_pragma_implementation);
223 c_register_pragma ("GCC", "interface", handle_pragma_interface);
224 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
225 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
228 /* TRUE if a code represents a statement. */
230 bool statement_code_p[MAX_TREE_CODES];
232 /* Initialize the C++ front end. This function is very sensitive to
233 the exact order that things are done here. It would be nice if the
234 initialization done by this routine were moved to its subroutines,
235 and the ordering dependencies clarified and reduced. */
236 bool
237 cxx_init (void)
239 location_t saved_loc;
240 unsigned int i;
241 static const enum tree_code stmt_codes[] = {
242 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
243 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
244 IF_STMT, CLEANUP_STMT, FOR_STMT,
245 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
246 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
247 EXPR_STMT
250 memset (&statement_code_p, 0, sizeof (statement_code_p));
251 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
252 statement_code_p[stmt_codes[i]] = true;
254 saved_loc = input_location;
255 input_location = BUILTINS_LOCATION;
257 init_reswords ();
258 init_tree ();
259 init_cp_semantics ();
260 init_operators ();
261 init_method ();
262 init_error ();
264 current_function_decl = NULL;
266 class_type_node = ridpointers[(int) RID_CLASS];
268 cxx_init_decl_processing ();
270 if (c_common_init () == false)
272 input_location = saved_loc;
273 return false;
276 init_cp_pragma ();
278 init_repo ();
280 input_location = saved_loc;
281 return true;
284 /* Return nonzero if S is not considered part of an
285 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
287 static int
288 interface_strcmp (const char* s)
290 /* Set the interface/implementation bits for this scope. */
291 struct impl_files *ifiles;
292 const char *s1;
294 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
296 const char *t1 = ifiles->filename;
297 s1 = s;
299 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
300 continue;
302 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
303 s1++, t1++;
305 /* A match. */
306 if (*s1 == *t1)
307 return 0;
309 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
310 if (strchr (s1, '.') || strchr (t1, '.'))
311 continue;
313 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
314 continue;
316 /* A match. */
317 return 0;
320 /* No matches. */
321 return 1;
326 /* Parse a #pragma whose sole argument is a string constant.
327 If OPT is true, the argument is optional. */
328 static tree
329 parse_strconst_pragma (const char* name, int opt)
331 tree result, x;
332 enum cpp_ttype t;
334 t = pragma_lex (&result);
335 if (t == CPP_STRING)
337 if (pragma_lex (&x) != CPP_EOF)
338 warning (0, "junk at end of #pragma %s", name);
339 return result;
342 if (t == CPP_EOF && opt)
343 return NULL_TREE;
345 error ("invalid #pragma %s", name);
346 return error_mark_node;
349 static void
350 handle_pragma_vtable (cpp_reader* /*dfile*/)
352 parse_strconst_pragma ("vtable", 0);
353 sorry ("#pragma vtable no longer supported");
356 static void
357 handle_pragma_unit (cpp_reader* /*dfile*/)
359 /* Validate syntax, but don't do anything. */
360 parse_strconst_pragma ("unit", 0);
363 static void
364 handle_pragma_interface (cpp_reader* /*dfile*/)
366 tree fname = parse_strconst_pragma ("interface", 1);
367 struct c_fileinfo *finfo;
368 const char *filename;
370 if (fname == error_mark_node)
371 return;
372 else if (fname == 0)
373 filename = lbasename (LOCATION_FILE (input_location));
374 else
375 filename = TREE_STRING_POINTER (fname);
377 finfo = get_fileinfo (LOCATION_FILE (input_location));
379 if (impl_file_chain == 0)
381 /* If this is zero at this point, then we are
382 auto-implementing. */
383 if (main_input_filename == 0)
384 main_input_filename = LOCATION_FILE (input_location);
387 finfo->interface_only = interface_strcmp (filename);
388 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
389 a definition in another file. */
390 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
391 finfo->interface_unknown = 0;
394 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
395 We used to only allow this at toplevel, but that restriction was buggy
396 in older compilers and it seems reasonable to allow it in the headers
397 themselves, too. It only needs to precede the matching #p interface.
399 We don't touch finfo->interface_only or finfo->interface_unknown;
400 the user must specify a matching #p interface for this to have
401 any effect. */
403 static void
404 handle_pragma_implementation (cpp_reader* /*dfile*/)
406 tree fname = parse_strconst_pragma ("implementation", 1);
407 const char *filename;
408 struct impl_files *ifiles = impl_file_chain;
410 if (fname == error_mark_node)
411 return;
413 if (fname == 0)
415 if (main_input_filename)
416 filename = main_input_filename;
417 else
418 filename = LOCATION_FILE (input_location);
419 filename = lbasename (filename);
421 else
423 filename = TREE_STRING_POINTER (fname);
424 if (cpp_included_before (parse_in, filename, input_location))
425 warning (0, "#pragma implementation for %qs appears after "
426 "file is included", filename);
429 for (; ifiles; ifiles = ifiles->next)
431 if (! filename_cmp (ifiles->filename, filename))
432 break;
434 if (ifiles == 0)
436 ifiles = XNEW (struct impl_files);
437 ifiles->filename = xstrdup (filename);
438 ifiles->next = impl_file_chain;
439 impl_file_chain = ifiles;
443 /* Indicate that this file uses Java-personality exception handling. */
444 static void
445 handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
447 tree x;
448 if (pragma_lex (&x) != CPP_EOF)
449 warning (0, "junk at end of #pragma GCC java_exceptions");
451 choose_personality_routine (lang_java);
452 pragma_java_exceptions = true;
455 /* Issue an error message indicating that the lookup of NAME (an
456 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
458 tree
459 unqualified_name_lookup_error (tree name)
461 if (IDENTIFIER_OPNAME_P (name))
463 if (name != ansi_opname (ERROR_MARK))
464 error ("%qD not defined", name);
466 else
468 if (!objc_diagnose_private_ivar (name))
470 error ("%qD was not declared in this scope", name);
471 suggest_alternatives_for (location_of (name), name);
473 /* Prevent repeated error messages by creating a VAR_DECL with
474 this NAME in the innermost block scope. */
475 if (local_bindings_p ())
477 tree decl;
478 decl = build_decl (input_location,
479 VAR_DECL, name, error_mark_node);
480 DECL_CONTEXT (decl) = current_function_decl;
481 push_local_binding (name, decl, 0);
482 /* Mark the variable as used so that we do not get warnings
483 about it being unused later. */
484 TREE_USED (decl) = 1;
488 return error_mark_node;
491 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
492 used as a function. Returns an appropriate expression for
493 NAME. */
495 tree
496 unqualified_fn_lookup_error (tree name)
498 if (processing_template_decl)
500 /* In a template, it is invalid to write "f()" or "f(3)" if no
501 declaration of "f" is available. Historically, G++ and most
502 other compilers accepted that usage since they deferred all name
503 lookup until instantiation time rather than doing unqualified
504 name lookup at template definition time; explain to the user what
505 is going wrong.
507 Note that we have the exact wording of the following message in
508 the manual (trouble.texi, node "Name lookup"), so they need to
509 be kept in synch. */
510 permerror (input_location, "there are no arguments to %qD that depend on a template "
511 "parameter, so a declaration of %qD must be available",
512 name, name);
514 if (!flag_permissive)
516 static bool hint;
517 if (!hint)
519 inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
520 "code, but allowing the use of an undeclared name is "
521 "deprecated)");
522 hint = true;
525 return name;
528 return unqualified_name_lookup_error (name);
531 /* Wrapper around build_lang_decl_loc(). Should gradually move to
532 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
533 build_lang_decl(). */
535 tree
536 build_lang_decl (enum tree_code code, tree name, tree type)
538 return build_lang_decl_loc (input_location, code, name, type);
541 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
542 DECL_LANG_SPECIFIC info to the result. */
544 tree
545 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
547 tree t;
549 t = build_decl (loc, code, name, type);
550 retrofit_lang_decl (t);
552 return t;
555 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
556 and pushdecl (for functions generated by the back end). */
558 void
559 retrofit_lang_decl (tree t)
561 struct lang_decl *ld;
562 size_t size;
563 int sel;
565 if (TREE_CODE (t) == FUNCTION_DECL)
566 sel = 1, size = sizeof (struct lang_decl_fn);
567 else if (TREE_CODE (t) == NAMESPACE_DECL)
568 sel = 2, size = sizeof (struct lang_decl_ns);
569 else if (TREE_CODE (t) == PARM_DECL)
570 sel = 3, size = sizeof (struct lang_decl_parm);
571 else if (LANG_DECL_HAS_MIN (t))
572 sel = 0, size = sizeof (struct lang_decl_min);
573 else
574 gcc_unreachable ();
576 ld = (struct lang_decl *) ggc_internal_cleared_alloc (size);
578 ld->u.base.selector = sel;
580 DECL_LANG_SPECIFIC (t) = ld;
581 if (current_lang_name == lang_name_cplusplus
582 || decl_linkage (t) == lk_none)
583 SET_DECL_LANGUAGE (t, lang_cplusplus);
584 else if (current_lang_name == lang_name_c)
585 SET_DECL_LANGUAGE (t, lang_c);
586 else if (current_lang_name == lang_name_java)
587 SET_DECL_LANGUAGE (t, lang_java);
588 else
589 gcc_unreachable ();
591 if (GATHER_STATISTICS)
593 tree_node_counts[(int)lang_decl] += 1;
594 tree_node_sizes[(int)lang_decl] += size;
598 void
599 cxx_dup_lang_specific_decl (tree node)
601 int size;
602 struct lang_decl *ld;
604 if (! DECL_LANG_SPECIFIC (node))
605 return;
607 if (TREE_CODE (node) == FUNCTION_DECL)
608 size = sizeof (struct lang_decl_fn);
609 else if (TREE_CODE (node) == NAMESPACE_DECL)
610 size = sizeof (struct lang_decl_ns);
611 else if (TREE_CODE (node) == PARM_DECL)
612 size = sizeof (struct lang_decl_parm);
613 else if (LANG_DECL_HAS_MIN (node))
614 size = sizeof (struct lang_decl_min);
615 else
616 gcc_unreachable ();
618 ld = (struct lang_decl *) ggc_internal_alloc (size);
619 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
620 DECL_LANG_SPECIFIC (node) = ld;
622 if (GATHER_STATISTICS)
624 tree_node_counts[(int)lang_decl] += 1;
625 tree_node_sizes[(int)lang_decl] += size;
629 /* Copy DECL, including any language-specific parts. */
631 tree
632 copy_decl (tree decl)
634 tree copy;
636 copy = copy_node (decl);
637 cxx_dup_lang_specific_decl (copy);
638 return copy;
641 /* Replace the shared language-specific parts of NODE with a new copy. */
643 static void
644 copy_lang_type (tree node)
646 int size;
647 struct lang_type *lt;
649 if (! TYPE_LANG_SPECIFIC (node))
650 return;
652 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
653 size = sizeof (struct lang_type);
654 else
655 size = sizeof (struct lang_type_ptrmem);
656 lt = (struct lang_type *) ggc_internal_alloc (size);
657 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
658 TYPE_LANG_SPECIFIC (node) = lt;
660 if (GATHER_STATISTICS)
662 tree_node_counts[(int)lang_type] += 1;
663 tree_node_sizes[(int)lang_type] += size;
667 /* Copy TYPE, including any language-specific parts. */
669 tree
670 copy_type (tree type)
672 tree copy;
674 copy = copy_node (type);
675 copy_lang_type (copy);
676 return copy;
679 tree
680 cxx_make_type (enum tree_code code)
682 tree t = make_node (code);
684 /* Create lang_type structure. */
685 if (RECORD_OR_UNION_CODE_P (code)
686 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
688 struct lang_type *pi
689 = (struct lang_type *) ggc_internal_cleared_alloc
690 (sizeof (struct lang_type));
692 TYPE_LANG_SPECIFIC (t) = pi;
693 pi->u.c.h.is_lang_type_class = 1;
695 if (GATHER_STATISTICS)
697 tree_node_counts[(int)lang_type] += 1;
698 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
702 /* Set up some flags that give proper default behavior. */
703 if (RECORD_OR_UNION_CODE_P (code))
705 struct c_fileinfo *finfo = \
706 get_fileinfo (LOCATION_FILE (input_location));
707 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
708 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
711 return t;
714 tree
715 make_class_type (enum tree_code code)
717 tree t = cxx_make_type (code);
718 SET_CLASS_TYPE_P (t, 1);
719 return t;
722 /* Returns true if we are currently in the main source file, or in a
723 template instantiation started from the main source file. */
725 bool
726 in_main_input_context (void)
728 struct tinst_level *tl = outermost_tinst_level();
730 if (tl)
731 return filename_cmp (main_input_filename,
732 LOCATION_FILE (tl->locus)) == 0;
733 else
734 return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;