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
;
84 /* True if we saw "#pragma GCC java_exceptions". */
85 bool pragma_java_exceptions
;
93 /* A mapping from tree codes to operator name information. */
94 operator_name_info_t operator_name_info
[(int) MAX_TREE_CODES
];
95 /* Similar, but for assignment operators. */
96 operator_name_info_t assignment_operator_name_info
[(int) MAX_TREE_CODES
];
98 /* Initialize data structures that keep track of operator names. */
100 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
101 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
102 #include "operators.def"
106 init_operators (void)
110 struct operator_name_info_t
*oni
;
112 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
113 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
114 identifier = get_identifier (buffer); \
115 IDENTIFIER_OPNAME_P (identifier) = 1; \
118 ? &assignment_operator_name_info[(int) CODE] \
119 : &operator_name_info[(int) CODE]); \
120 oni->identifier = identifier; \
122 oni->mangled_name = MANGLING; \
125 #include "operators.def"
128 operator_name_info
[(int) ERROR_MARK
].identifier
129 = get_identifier ("<invalid operator>");
131 /* Handle some special cases. These operators are not defined in
132 the language, but can be produced internally. We may need them
133 for error-reporting. (Eventually, we should ensure that this
134 does not happen. Error messages involving these operators will
135 be confusing to users.) */
137 operator_name_info
[(int) INIT_EXPR
].name
138 = operator_name_info
[(int) MODIFY_EXPR
].name
;
139 operator_name_info
[(int) EXACT_DIV_EXPR
].name
= "(ceiling /)";
140 operator_name_info
[(int) CEIL_DIV_EXPR
].name
= "(ceiling /)";
141 operator_name_info
[(int) FLOOR_DIV_EXPR
].name
= "(floor /)";
142 operator_name_info
[(int) ROUND_DIV_EXPR
].name
= "(round /)";
143 operator_name_info
[(int) CEIL_MOD_EXPR
].name
= "(ceiling %)";
144 operator_name_info
[(int) FLOOR_MOD_EXPR
].name
= "(floor %)";
145 operator_name_info
[(int) ROUND_MOD_EXPR
].name
= "(round %)";
146 operator_name_info
[(int) ABS_EXPR
].name
= "abs";
147 operator_name_info
[(int) TRUTH_AND_EXPR
].name
= "strict &&";
148 operator_name_info
[(int) TRUTH_OR_EXPR
].name
= "strict ||";
149 operator_name_info
[(int) RANGE_EXPR
].name
= "...";
150 operator_name_info
[(int) UNARY_PLUS_EXPR
].name
= "+";
152 assignment_operator_name_info
[(int) EXACT_DIV_EXPR
].name
154 assignment_operator_name_info
[(int) CEIL_DIV_EXPR
].name
156 assignment_operator_name_info
[(int) FLOOR_DIV_EXPR
].name
158 assignment_operator_name_info
[(int) ROUND_DIV_EXPR
].name
160 assignment_operator_name_info
[(int) CEIL_MOD_EXPR
].name
162 assignment_operator_name_info
[(int) FLOOR_MOD_EXPR
].name
164 assignment_operator_name_info
[(int) ROUND_MOD_EXPR
].name
168 /* Initialize the reserved words. */
177 if (cxx_dialect
!= cxx0x
)
180 mask
|= D_ASM
| D_EXT
;
181 if (flag_no_gnu_keywords
)
184 /* The Objective-C keywords are all context-dependent. */
187 ridpointers
= GGC_CNEWVEC (tree
, (int) RID_MAX
);
188 for (i
= 0; i
< num_c_common_reswords
; i
++)
190 if (c_common_reswords
[i
].disable
& D_CONLY
)
192 id
= get_identifier (c_common_reswords
[i
].word
);
193 C_SET_RID_CODE (id
, c_common_reswords
[i
].rid
);
194 ridpointers
[(int) c_common_reswords
[i
].rid
] = id
;
195 if (! (c_common_reswords
[i
].disable
& mask
))
196 C_IS_RESERVED_WORD (id
) = 1;
201 init_cp_pragma (void)
203 c_register_pragma (0, "vtable", handle_pragma_vtable
);
204 c_register_pragma (0, "unit", handle_pragma_unit
);
205 c_register_pragma (0, "interface", handle_pragma_interface
);
206 c_register_pragma (0, "implementation", handle_pragma_implementation
);
207 c_register_pragma ("GCC", "interface", handle_pragma_interface
);
208 c_register_pragma ("GCC", "implementation", handle_pragma_implementation
);
209 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions
);
212 /* TRUE if a code represents a statement. */
214 bool statement_code_p
[MAX_TREE_CODES
];
216 /* Initialize the C++ front end. This function is very sensitive to
217 the exact order that things are done here. It would be nice if the
218 initialization done by this routine were moved to its subroutines,
219 and the ordering dependencies clarified and reduced. */
223 location_t saved_loc
;
225 static const enum tree_code stmt_codes
[] = {
226 CTOR_INITIALIZER
, TRY_BLOCK
, HANDLER
,
227 EH_SPEC_BLOCK
, USING_STMT
, TAG_DEFN
,
228 IF_STMT
, CLEANUP_STMT
, FOR_STMT
,
229 WHILE_STMT
, DO_STMT
, BREAK_STMT
,
230 CONTINUE_STMT
, SWITCH_STMT
, EXPR_STMT
233 memset (&statement_code_p
, 0, sizeof (statement_code_p
));
234 for (i
= 0; i
< ARRAY_SIZE (stmt_codes
); i
++)
235 statement_code_p
[stmt_codes
[i
]] = true;
237 saved_loc
= input_location
;
238 input_location
= BUILTINS_LOCATION
;
242 init_cp_semantics ();
247 current_function_decl
= NULL
;
249 class_type_node
= ridpointers
[(int) RID_CLASS
];
251 cxx_init_decl_processing ();
253 if (c_common_init () == false)
255 input_location
= saved_loc
;
263 input_location
= saved_loc
;
267 /* Return nonzero if S is not considered part of an
268 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
271 interface_strcmp (const char* s
)
273 /* Set the interface/implementation bits for this scope. */
274 struct impl_files
*ifiles
;
277 for (ifiles
= impl_file_chain
; ifiles
; ifiles
= ifiles
->next
)
279 const char *t1
= ifiles
->filename
;
282 if (*s1
!= *t1
|| *s1
== 0)
285 while (*s1
== *t1
&& *s1
!= 0)
292 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
293 if (strchr (s1
, '.') || strchr (t1
, '.'))
296 if (*s1
== '\0' || s1
[-1] != '.' || t1
[-1] != '.')
309 /* Parse a #pragma whose sole argument is a string constant.
310 If OPT is true, the argument is optional. */
312 parse_strconst_pragma (const char* name
, int opt
)
317 t
= pragma_lex (&result
);
320 if (pragma_lex (&x
) != CPP_EOF
)
321 warning (0, "junk at end of #pragma %s", name
);
325 if (t
== CPP_EOF
&& opt
)
328 error ("invalid #pragma %s", name
);
329 return error_mark_node
;
333 handle_pragma_vtable (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
335 parse_strconst_pragma ("vtable", 0);
336 sorry ("#pragma vtable no longer supported");
340 handle_pragma_unit (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
342 /* Validate syntax, but don't do anything. */
343 parse_strconst_pragma ("unit", 0);
347 handle_pragma_interface (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
349 tree fname
= parse_strconst_pragma ("interface", 1);
350 struct c_fileinfo
*finfo
;
351 const char *filename
;
353 if (fname
== error_mark_node
)
356 filename
= lbasename (input_filename
);
358 filename
= TREE_STRING_POINTER (fname
);
360 finfo
= get_fileinfo (input_filename
);
362 if (impl_file_chain
== 0)
364 /* If this is zero at this point, then we are
365 auto-implementing. */
366 if (main_input_filename
== 0)
367 main_input_filename
= input_filename
;
370 finfo
->interface_only
= interface_strcmp (filename
);
371 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
372 a definition in another file. */
373 if (!MULTIPLE_SYMBOL_SPACES
|| !finfo
->interface_only
)
374 finfo
->interface_unknown
= 0;
377 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
378 We used to only allow this at toplevel, but that restriction was buggy
379 in older compilers and it seems reasonable to allow it in the headers
380 themselves, too. It only needs to precede the matching #p interface.
382 We don't touch finfo->interface_only or finfo->interface_unknown;
383 the user must specify a matching #p interface for this to have
387 handle_pragma_implementation (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
389 tree fname
= parse_strconst_pragma ("implementation", 1);
390 const char *filename
;
391 struct impl_files
*ifiles
= impl_file_chain
;
393 if (fname
== error_mark_node
)
398 if (main_input_filename
)
399 filename
= main_input_filename
;
401 filename
= input_filename
;
402 filename
= lbasename (filename
);
406 filename
= TREE_STRING_POINTER (fname
);
407 if (cpp_included_before (parse_in
, filename
, input_location
))
408 warning (0, "#pragma implementation for %qs appears after "
409 "file is included", filename
);
412 for (; ifiles
; ifiles
= ifiles
->next
)
414 if (! strcmp (ifiles
->filename
, filename
))
419 ifiles
= XNEW (struct impl_files
);
420 ifiles
->filename
= xstrdup (filename
);
421 ifiles
->next
= impl_file_chain
;
422 impl_file_chain
= ifiles
;
426 /* Indicate that this file uses Java-personality exception handling. */
428 handle_pragma_java_exceptions (cpp_reader
* dfile ATTRIBUTE_UNUSED
)
431 if (pragma_lex (&x
) != CPP_EOF
)
432 warning (0, "junk at end of #pragma GCC java_exceptions");
434 choose_personality_routine (lang_java
);
435 pragma_java_exceptions
= true;
438 /* Issue an error message indicating that the lookup of NAME (an
439 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
442 unqualified_name_lookup_error (tree name
)
444 if (IDENTIFIER_OPNAME_P (name
))
446 if (name
!= ansi_opname (ERROR_MARK
))
447 error ("%qD not defined", name
);
451 error ("%qD was not declared in this scope", name
);
452 /* Prevent repeated error messages by creating a VAR_DECL with
453 this NAME in the innermost block scope. */
454 if (current_function_decl
)
457 decl
= build_decl (input_location
,
458 VAR_DECL
, name
, error_mark_node
);
459 DECL_CONTEXT (decl
) = current_function_decl
;
460 push_local_binding (name
, decl
, 0);
461 /* Mark the variable as used so that we do not get warnings
462 about it being unused later. */
463 TREE_USED (decl
) = 1;
467 return error_mark_node
;
470 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
471 used as a function. Returns an appropriate expression for
475 unqualified_fn_lookup_error (tree name
)
477 if (processing_template_decl
)
479 /* In a template, it is invalid to write "f()" or "f(3)" if no
480 declaration of "f" is available. Historically, G++ and most
481 other compilers accepted that usage since they deferred all name
482 lookup until instantiation time rather than doing unqualified
483 name lookup at template definition time; explain to the user what
486 Note that we have the exact wording of the following message in
487 the manual (trouble.texi, node "Name lookup"), so they need to
489 permerror (input_location
, "there are no arguments to %qD that depend on a template "
490 "parameter, so a declaration of %qD must be available",
493 if (!flag_permissive
)
498 inform (input_location
, "(if you use %<-fpermissive%>, G++ will accept your "
499 "code, but allowing the use of an undeclared name is "
507 return unqualified_name_lookup_error (name
);
511 build_lang_decl (enum tree_code code
, tree name
, tree type
)
515 t
= build_decl (input_location
,
517 retrofit_lang_decl (t
);
519 /* All nesting of C++ functions is lexical; there is never a "static
520 chain" in the sense of GNU C nested functions. */
521 if (code
== FUNCTION_DECL
)
522 DECL_NO_STATIC_CHAIN (t
) = 1;
527 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
528 and pushdecl (for functions generated by the back end). */
531 retrofit_lang_decl (tree t
)
533 struct lang_decl
*ld
;
537 if (TREE_CODE (t
) == FUNCTION_DECL
)
538 sel
= 1, size
= sizeof (struct lang_decl_fn
);
539 else if (TREE_CODE (t
) == NAMESPACE_DECL
)
540 sel
= 2, size
= sizeof (struct lang_decl_ns
);
541 else if (TREE_CODE (t
) == PARM_DECL
)
542 sel
= 3, size
= sizeof (struct lang_decl_parm
);
543 else if (LANG_DECL_HAS_MIN (t
))
544 sel
= 0, size
= sizeof (struct lang_decl_min
);
548 ld
= GGC_CNEWVAR (struct lang_decl
, size
);
550 ld
->u
.base
.selector
= sel
;
552 DECL_LANG_SPECIFIC (t
) = ld
;
553 if (current_lang_name
== lang_name_cplusplus
554 || decl_linkage (t
) == lk_none
)
555 SET_DECL_LANGUAGE (t
, lang_cplusplus
);
556 else if (current_lang_name
== lang_name_c
)
557 SET_DECL_LANGUAGE (t
, lang_c
);
558 else if (current_lang_name
== lang_name_java
)
559 SET_DECL_LANGUAGE (t
, lang_java
);
563 #ifdef GATHER_STATISTICS
564 tree_node_counts
[(int)lang_decl
] += 1;
565 tree_node_sizes
[(int)lang_decl
] += size
;
570 cxx_dup_lang_specific_decl (tree node
)
573 struct lang_decl
*ld
;
575 if (! DECL_LANG_SPECIFIC (node
))
578 if (TREE_CODE (node
) == FUNCTION_DECL
)
579 size
= sizeof (struct lang_decl_fn
);
580 else if (TREE_CODE (node
) == NAMESPACE_DECL
)
581 size
= sizeof (struct lang_decl_ns
);
582 else if (TREE_CODE (node
) == PARM_DECL
)
583 size
= sizeof (struct lang_decl_parm
);
584 else if (LANG_DECL_HAS_MIN (node
))
585 size
= sizeof (struct lang_decl_min
);
589 ld
= GGC_NEWVAR (struct lang_decl
, size
);
590 memcpy (ld
, DECL_LANG_SPECIFIC (node
), size
);
591 DECL_LANG_SPECIFIC (node
) = ld
;
593 #ifdef GATHER_STATISTICS
594 tree_node_counts
[(int)lang_decl
] += 1;
595 tree_node_sizes
[(int)lang_decl
] += size
;
599 /* Copy DECL, including any language-specific parts. */
602 copy_decl (tree decl
)
606 copy
= copy_node (decl
);
607 cxx_dup_lang_specific_decl (copy
);
611 /* Replace the shared language-specific parts of NODE with a new copy. */
614 copy_lang_type (tree node
)
617 struct lang_type
*lt
;
619 if (! TYPE_LANG_SPECIFIC (node
))
622 if (TYPE_LANG_SPECIFIC (node
)->u
.h
.is_lang_type_class
)
623 size
= sizeof (struct lang_type
);
625 size
= sizeof (struct lang_type_ptrmem
);
626 lt
= GGC_NEWVAR (struct lang_type
, size
);
627 memcpy (lt
, TYPE_LANG_SPECIFIC (node
), size
);
628 TYPE_LANG_SPECIFIC (node
) = lt
;
630 #ifdef GATHER_STATISTICS
631 tree_node_counts
[(int)lang_type
] += 1;
632 tree_node_sizes
[(int)lang_type
] += size
;
636 /* Copy TYPE, including any language-specific parts. */
639 copy_type (tree type
)
643 copy
= copy_node (type
);
644 copy_lang_type (copy
);
649 cxx_make_type (enum tree_code code
)
651 tree t
= make_node (code
);
653 /* Create lang_type structure. */
654 if (RECORD_OR_UNION_CODE_P (code
)
655 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
657 struct lang_type
*pi
= GGC_CNEW (struct lang_type
);
659 TYPE_LANG_SPECIFIC (t
) = pi
;
660 pi
->u
.c
.h
.is_lang_type_class
= 1;
662 #ifdef GATHER_STATISTICS
663 tree_node_counts
[(int)lang_type
] += 1;
664 tree_node_sizes
[(int)lang_type
] += sizeof (struct lang_type
);
668 /* Set up some flags that give proper default behavior. */
669 if (RECORD_OR_UNION_CODE_P (code
))
671 struct c_fileinfo
*finfo
= get_fileinfo (input_filename
);
672 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, finfo
->interface_unknown
);
673 CLASSTYPE_INTERFACE_ONLY (t
) = finfo
->interface_only
;
680 make_class_type (enum tree_code code
)
682 tree t
= cxx_make_type (code
);
683 SET_CLASS_TYPE_P (t
, 1);
687 /* Returns true if we are currently in the main source file, or in a
688 template instantiation started from the main source file. */
691 in_main_input_context (void)
693 struct tinst_level
*tl
= outermost_tinst_level();
696 return strcmp (main_input_filename
,
697 LOCATION_FILE (tl
->locus
)) == 0;
699 return strcmp (main_input_filename
, input_filename
) == 0;