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)
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++. */
28 #include "coretypes.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
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"'. */
79 struct impl_files
*next
;
82 static struct impl_files
*impl_file_chain
;
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"
104 init_operators (void)
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; \
116 ? &assignment_operator_name_info[(int) CODE] \
117 : &operator_name_info[(int) CODE]); \
118 oni->identifier = identifier; \
120 oni->mangled_name = MANGLING; \
123 #include "operators.def"
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
152 assignment_operator_name_info
[(int) CEIL_DIV_EXPR
].name
154 assignment_operator_name_info
[(int) FLOOR_DIV_EXPR
].name
156 assignment_operator_name_info
[(int) ROUND_DIV_EXPR
].name
158 assignment_operator_name_info
[(int) CEIL_MOD_EXPR
].name
160 assignment_operator_name_info
[(int) FLOOR_MOD_EXPR
].name
162 assignment_operator_name_info
[(int) ROUND_MOD_EXPR
].name
166 /* Initialize the reserved words. */
176 if (cxx_dialect
!= cxx0x
)
179 mask
|= D_ASM
| D_EXT
;
180 if (flag_no_gnu_keywords
)
183 /* The Objective-C keywords are all context-dependent. */
186 ridpointers
= GGC_CNEWVEC (tree
, (int) RID_MAX
);
187 for (i
= 0; i
< num_c_common_reswords
; i
++)
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;
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. */
220 location_t saved_loc
;
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 WHILE_STMT
, DO_STMT
, BREAK_STMT
,
227 CONTINUE_STMT
, SWITCH_STMT
, EXPR_STMT
230 memset (&statement_code_p
, 0, sizeof (statement_code_p
));
231 for (i
= 0; i
< ARRAY_SIZE (stmt_codes
); i
++)
232 statement_code_p
[stmt_codes
[i
]] = true;
234 saved_loc
= input_location
;
235 input_location
= BUILTINS_LOCATION
;
239 init_cp_semantics ();
244 current_function_decl
= NULL
;
246 class_type_node
= ridpointers
[(int) RID_CLASS
];
248 cxx_init_decl_processing ();
250 /* The fact that G++ uses COMDAT for many entities (inline
251 functions, template instantiations, virtual tables, etc.) mean
252 that it is fundamentally unreliable to try to make decisions
253 about whether or not to output a particular entity until the end
254 of the compilation. However, the inliner requires that functions
255 be provided to the back end if they are to be inlined.
256 Therefore, we always use unit-at-a-time mode; in that mode, we
257 can provide entities to the back end and it will decide what to
258 emit based on what is actually needed. */
259 flag_unit_at_a_time
= 1;
261 if (c_common_init () == false)
263 input_location
= saved_loc
;
271 input_location
= saved_loc
;
275 /* Return nonzero if S is not considered part of an
276 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
279 interface_strcmp (const char* s
)
281 /* Set the interface/implementation bits for this scope. */
282 struct impl_files
*ifiles
;
285 for (ifiles
= impl_file_chain
; ifiles
; ifiles
= ifiles
->next
)
287 const char *t1
= ifiles
->filename
;
290 if (*s1
!= *t1
|| *s1
== 0)
293 while (*s1
== *t1
&& *s1
!= 0)
300 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
301 if (strchr (s1
, '.') || strchr (t1
, '.'))
304 if (*s1
== '\0' || s1
[-1] != '.' || t1
[-1] != '.')
317 /* Parse a #pragma whose sole argument is a string constant.
318 If OPT is true, the argument is optional. */
320 parse_strconst_pragma (const char* name
, int opt
)
325 t
= pragma_lex (&result
);
328 if (pragma_lex (&x
) != CPP_EOF
)
329 warning (0, "junk at end of #pragma %s", name
);
333 if (t
== CPP_EOF
&& opt
)
336 error ("invalid #pragma %s", name
);
337 return error_mark_node
;
341 handle_pragma_vtable (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
343 parse_strconst_pragma ("vtable", 0);
344 sorry ("#pragma vtable no longer supported");
348 handle_pragma_unit (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
350 /* Validate syntax, but don't do anything. */
351 parse_strconst_pragma ("unit", 0);
355 handle_pragma_interface (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
357 tree fname
= parse_strconst_pragma ("interface", 1);
358 struct c_fileinfo
*finfo
;
359 const char *filename
;
361 if (fname
== error_mark_node
)
364 filename
= lbasename (input_filename
);
366 filename
= TREE_STRING_POINTER (fname
);
368 finfo
= get_fileinfo (input_filename
);
370 if (impl_file_chain
== 0)
372 /* If this is zero at this point, then we are
373 auto-implementing. */
374 if (main_input_filename
== 0)
375 main_input_filename
= input_filename
;
378 finfo
->interface_only
= interface_strcmp (filename
);
379 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
380 a definition in another file. */
381 if (!MULTIPLE_SYMBOL_SPACES
|| !finfo
->interface_only
)
382 finfo
->interface_unknown
= 0;
385 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
386 We used to only allow this at toplevel, but that restriction was buggy
387 in older compilers and it seems reasonable to allow it in the headers
388 themselves, too. It only needs to precede the matching #p interface.
390 We don't touch finfo->interface_only or finfo->interface_unknown;
391 the user must specify a matching #p interface for this to have
395 handle_pragma_implementation (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
397 tree fname
= parse_strconst_pragma ("implementation", 1);
398 const char *filename
;
399 struct impl_files
*ifiles
= impl_file_chain
;
401 if (fname
== error_mark_node
)
406 if (main_input_filename
)
407 filename
= main_input_filename
;
409 filename
= input_filename
;
410 filename
= lbasename (filename
);
414 filename
= TREE_STRING_POINTER (fname
);
415 if (cpp_included_before (parse_in
, filename
, input_location
))
416 warning (0, "#pragma implementation for %qs appears after "
417 "file is included", filename
);
420 for (; ifiles
; ifiles
= ifiles
->next
)
422 if (! strcmp (ifiles
->filename
, filename
))
427 ifiles
= XNEW (struct impl_files
);
428 ifiles
->filename
= xstrdup (filename
);
429 ifiles
->next
= impl_file_chain
;
430 impl_file_chain
= ifiles
;
434 /* Indicate that this file uses Java-personality exception handling. */
436 handle_pragma_java_exceptions (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
439 if (pragma_lex (&x
) != CPP_EOF
)
440 warning (0, "junk at end of #pragma GCC java_exceptions");
442 choose_personality_routine (lang_java
);
445 /* Issue an error message indicating that the lookup of NAME (an
446 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
449 unqualified_name_lookup_error (tree name
)
451 if (IDENTIFIER_OPNAME_P (name
))
453 if (name
!= ansi_opname (ERROR_MARK
))
454 error ("%qD not defined", name
);
458 error ("%qD was not declared in this scope", name
);
459 /* Prevent repeated error messages by creating a VAR_DECL with
460 this NAME in the innermost block scope. */
461 if (current_function_decl
)
464 decl
= build_decl (VAR_DECL
, name
, error_mark_node
);
465 DECL_CONTEXT (decl
) = current_function_decl
;
466 push_local_binding (name
, decl
, 0);
467 /* Mark the variable as used so that we do not get warnings
468 about it being unused later. */
469 TREE_USED (decl
) = 1;
473 return error_mark_node
;
476 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
477 used as a function. Returns an appropriate expression for
481 unqualified_fn_lookup_error (tree name
)
483 if (processing_template_decl
)
485 /* In a template, it is invalid to write "f()" or "f(3)" if no
486 declaration of "f" is available. Historically, G++ and most
487 other compilers accepted that usage since they deferred all name
488 lookup until instantiation time rather than doing unqualified
489 name lookup at template definition time; explain to the user what
492 Note that we have the exact wording of the following message in
493 the manual (trouble.texi, node "Name lookup"), so they need to
495 permerror ("there are no arguments to %qD that depend on a template "
496 "parameter, so a declaration of %qD must be available",
499 if (!flag_permissive
)
504 inform ("(if you use %<-fpermissive%>, G++ will accept your "
505 "code, but allowing the use of an undeclared name is "
513 return unqualified_name_lookup_error (name
);
517 build_lang_decl (enum tree_code code
, tree name
, tree type
)
521 t
= build_decl (code
, name
, type
);
522 retrofit_lang_decl (t
);
524 /* All nesting of C++ functions is lexical; there is never a "static
525 chain" in the sense of GNU C nested functions. */
526 if (code
== FUNCTION_DECL
)
527 DECL_NO_STATIC_CHAIN (t
) = 1;
532 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
533 and pushdecl (for functions generated by the back end). */
536 retrofit_lang_decl (tree t
)
538 struct lang_decl
*ld
;
541 if (CAN_HAVE_FULL_LANG_DECL_P (t
))
542 size
= sizeof (struct lang_decl
);
544 size
= sizeof (struct lang_decl_flags
);
546 ld
= GGC_CNEWVAR (struct lang_decl
, size
);
548 ld
->decl_flags
.can_be_full
= CAN_HAVE_FULL_LANG_DECL_P (t
) ? 1 : 0;
549 ld
->decl_flags
.u1sel
= TREE_CODE (t
) == NAMESPACE_DECL
? 1 : 0;
550 ld
->decl_flags
.u2sel
= 0;
551 if (ld
->decl_flags
.can_be_full
)
552 ld
->u
.f
.u3sel
= TREE_CODE (t
) == FUNCTION_DECL
? 1 : 0;
554 DECL_LANG_SPECIFIC (t
) = ld
;
555 if (current_lang_name
== lang_name_cplusplus
556 || decl_linkage (t
) == lk_none
)
557 SET_DECL_LANGUAGE (t
, lang_cplusplus
);
558 else if (current_lang_name
== lang_name_c
)
559 SET_DECL_LANGUAGE (t
, lang_c
);
560 else if (current_lang_name
== lang_name_java
)
561 SET_DECL_LANGUAGE (t
, lang_java
);
565 #ifdef GATHER_STATISTICS
566 tree_node_counts
[(int)lang_decl
] += 1;
567 tree_node_sizes
[(int)lang_decl
] += size
;
572 cxx_dup_lang_specific_decl (tree node
)
575 struct lang_decl
*ld
;
577 if (! DECL_LANG_SPECIFIC (node
))
580 if (!CAN_HAVE_FULL_LANG_DECL_P (node
))
581 size
= sizeof (struct lang_decl_flags
);
583 size
= sizeof (struct lang_decl
);
584 ld
= GGC_NEWVAR (struct lang_decl
, size
);
585 memcpy (ld
, DECL_LANG_SPECIFIC (node
), size
);
586 DECL_LANG_SPECIFIC (node
) = ld
;
588 #ifdef GATHER_STATISTICS
589 tree_node_counts
[(int)lang_decl
] += 1;
590 tree_node_sizes
[(int)lang_decl
] += size
;
594 /* Copy DECL, including any language-specific parts. */
597 copy_decl (tree decl
)
601 copy
= copy_node (decl
);
602 cxx_dup_lang_specific_decl (copy
);
606 /* Replace the shared language-specific parts of NODE with a new copy. */
609 copy_lang_type (tree node
)
612 struct lang_type
*lt
;
614 if (! TYPE_LANG_SPECIFIC (node
))
617 if (TYPE_LANG_SPECIFIC (node
)->u
.h
.is_lang_type_class
)
618 size
= sizeof (struct lang_type
);
620 size
= sizeof (struct lang_type_ptrmem
);
621 lt
= GGC_NEWVAR (struct lang_type
, size
);
622 memcpy (lt
, TYPE_LANG_SPECIFIC (node
), size
);
623 TYPE_LANG_SPECIFIC (node
) = lt
;
625 #ifdef GATHER_STATISTICS
626 tree_node_counts
[(int)lang_type
] += 1;
627 tree_node_sizes
[(int)lang_type
] += size
;
631 /* Copy TYPE, including any language-specific parts. */
634 copy_type (tree type
)
638 copy
= copy_node (type
);
639 copy_lang_type (copy
);
644 cxx_make_type (enum tree_code code
)
646 tree t
= make_node (code
);
648 /* Create lang_type structure. */
649 if (RECORD_OR_UNION_CODE_P (code
)
650 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
652 struct lang_type
*pi
= GGC_CNEW (struct lang_type
);
654 TYPE_LANG_SPECIFIC (t
) = pi
;
655 pi
->u
.c
.h
.is_lang_type_class
= 1;
657 #ifdef GATHER_STATISTICS
658 tree_node_counts
[(int)lang_type
] += 1;
659 tree_node_sizes
[(int)lang_type
] += sizeof (struct lang_type
);
663 /* Set up some flags that give proper default behavior. */
664 if (RECORD_OR_UNION_CODE_P (code
))
666 struct c_fileinfo
*finfo
= get_fileinfo (input_filename
);
667 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, finfo
->interface_unknown
);
668 CLASSTYPE_INTERFACE_ONLY (t
) = finfo
->interface_only
;
675 make_class_type (enum tree_code code
)
677 tree t
= cxx_make_type (code
);
678 SET_CLASS_TYPE_P (t
, 1);
682 /* Returns true if we are currently in the main source file, or in a
683 template instantiation started from the main source file. */
686 in_main_input_context (void)
688 struct tinst_level
*tl
= outermost_tinst_level();
691 return strcmp (main_input_filename
,
692 LOCATION_FILE (tl
->locus
)) == 0;
694 return strcmp (main_input_filename
, input_filename
) == 0;