1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987-2017 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)
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++. */
26 #include "coretypes.h"
28 #include "stringpool.h"
29 #include "c-family/c-pragma.h"
30 #include "c-family/c-objc.h"
32 static int interface_strcmp (const char *);
33 static void init_cp_pragma (void);
35 static tree
parse_strconst_pragma (const char *, int);
36 static void handle_pragma_vtable (cpp_reader
*);
37 static void handle_pragma_unit (cpp_reader
*);
38 static void handle_pragma_interface (cpp_reader
*);
39 static void handle_pragma_implementation (cpp_reader
*);
41 static void init_operators (void);
42 static void copy_lang_type (tree
);
44 /* A constraint that can be tested at compile time. */
45 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
47 /* Functions and data structures for #pragma interface.
49 `#pragma implementation' means that the main file being compiled
50 is considered to implement (provide) the classes that appear in
51 its main body. I.e., if this is file "foo.cc", and class `bar'
52 is defined in "foo.cc", then we say that "foo.cc implements bar".
54 All main input files "implement" themselves automagically.
56 `#pragma interface' means that unless this file (of the form "foo.h"
57 is not presently being included by file "foo.cc", the
58 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
59 of the vtables nor any of the inline functions defined in foo.h
62 There are cases when we want to link files such as "defs.h" and
63 "main.cc". In this case, we give "defs.h" a `#pragma interface',
64 and "main.cc" has `#pragma implementation "defs.h"'. */
69 struct impl_files
*next
;
72 static struct impl_files
*impl_file_chain
;
80 /* A mapping from tree codes to operator name information. */
81 operator_name_info_t operator_name_info
[(int) MAX_TREE_CODES
];
82 /* Similar, but for assignment operators. */
83 operator_name_info_t assignment_operator_name_info
[(int) MAX_TREE_CODES
];
85 /* Initialize data structures that keep track of operator names. */
87 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
88 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
89 #include "operators.def"
97 struct operator_name_info_t
*oni
;
99 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
100 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
101 identifier = get_identifier (buffer); \
102 IDENTIFIER_OPNAME_P (identifier) = 1; \
105 ? &assignment_operator_name_info[(int) CODE] \
106 : &operator_name_info[(int) CODE]); \
107 oni->identifier = identifier; \
109 oni->mangled_name = MANGLING; \
112 #include "operators.def"
115 operator_name_info
[(int) ERROR_MARK
].identifier
116 = get_identifier ("<invalid operator>");
118 /* Handle some special cases. These operators are not defined in
119 the language, but can be produced internally. We may need them
120 for error-reporting. (Eventually, we should ensure that this
121 does not happen. Error messages involving these operators will
122 be confusing to users.) */
124 operator_name_info
[(int) INIT_EXPR
].name
125 = operator_name_info
[(int) MODIFY_EXPR
].name
;
126 operator_name_info
[(int) EXACT_DIV_EXPR
].name
= "(ceiling /)";
127 operator_name_info
[(int) CEIL_DIV_EXPR
].name
= "(ceiling /)";
128 operator_name_info
[(int) FLOOR_DIV_EXPR
].name
= "(floor /)";
129 operator_name_info
[(int) ROUND_DIV_EXPR
].name
= "(round /)";
130 operator_name_info
[(int) CEIL_MOD_EXPR
].name
= "(ceiling %)";
131 operator_name_info
[(int) FLOOR_MOD_EXPR
].name
= "(floor %)";
132 operator_name_info
[(int) ROUND_MOD_EXPR
].name
= "(round %)";
133 operator_name_info
[(int) ABS_EXPR
].name
= "abs";
134 operator_name_info
[(int) TRUTH_AND_EXPR
].name
= "strict &&";
135 operator_name_info
[(int) TRUTH_OR_EXPR
].name
= "strict ||";
136 operator_name_info
[(int) RANGE_EXPR
].name
= "...";
137 operator_name_info
[(int) UNARY_PLUS_EXPR
].name
= "+";
139 assignment_operator_name_info
[(int) EXACT_DIV_EXPR
].name
141 assignment_operator_name_info
[(int) CEIL_DIV_EXPR
].name
143 assignment_operator_name_info
[(int) FLOOR_DIV_EXPR
].name
145 assignment_operator_name_info
[(int) ROUND_DIV_EXPR
].name
147 assignment_operator_name_info
[(int) CEIL_MOD_EXPR
].name
149 assignment_operator_name_info
[(int) FLOOR_MOD_EXPR
].name
151 assignment_operator_name_info
[(int) ROUND_MOD_EXPR
].name
155 /* Initialize the reserved words. */
164 if (cxx_dialect
< cxx11
)
167 mask
|= D_CXX_CONCEPTS
;
171 mask
|= D_ASM
| D_EXT
;
172 if (flag_no_gnu_keywords
)
175 /* The Objective-C keywords are all context-dependent. */
178 ridpointers
= ggc_cleared_vec_alloc
<tree
> ((int) RID_MAX
);
179 for (i
= 0; i
< num_c_common_reswords
; i
++)
181 if (c_common_reswords
[i
].disable
& D_CONLY
)
183 id
= get_identifier (c_common_reswords
[i
].word
);
184 C_SET_RID_CODE (id
, c_common_reswords
[i
].rid
);
185 ridpointers
[(int) c_common_reswords
[i
].rid
] = id
;
186 if (! (c_common_reswords
[i
].disable
& mask
))
187 C_IS_RESERVED_WORD (id
) = 1;
190 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
193 sprintf (name
, "__int%d", int_n_data
[i
].bitsize
);
194 id
= get_identifier (name
);
195 C_SET_RID_CODE (id
, RID_FIRST_INT_N
+ i
);
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
);
211 /* TRUE if a code represents a statement. */
213 bool statement_code_p
[MAX_TREE_CODES
];
215 /* Initialize the C++ front end. This function is very sensitive to
216 the exact order that things are done here. It would be nice if the
217 initialization done by this routine were moved to its subroutines,
218 and the ordering dependencies clarified and reduced. */
222 location_t saved_loc
;
224 static const enum tree_code stmt_codes
[] = {
225 CTOR_INITIALIZER
, TRY_BLOCK
, HANDLER
,
226 EH_SPEC_BLOCK
, USING_STMT
, TAG_DEFN
,
227 IF_STMT
, CLEANUP_STMT
, FOR_STMT
,
228 RANGE_FOR_STMT
, WHILE_STMT
, DO_STMT
,
229 BREAK_STMT
, CONTINUE_STMT
, SWITCH_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 ();
246 current_function_decl
= NULL
;
248 class_type_node
= ridpointers
[(int) RID_CLASS
];
250 cxx_init_decl_processing ();
252 if (c_common_init () == false)
254 input_location
= saved_loc
;
262 input_location
= saved_loc
;
266 /* Return nonzero if S is not considered part of an
267 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
270 interface_strcmp (const char* s
)
272 /* Set the interface/implementation bits for this scope. */
273 struct impl_files
*ifiles
;
276 for (ifiles
= impl_file_chain
; ifiles
; ifiles
= ifiles
->next
)
278 const char *t1
= ifiles
->filename
;
281 if (*s1
== 0 || filename_ncmp (s1
, t1
, 1) != 0)
284 while (*s1
!= 0 && filename_ncmp (s1
, t1
, 1) == 0)
291 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
292 if (strchr (s1
, '.') || strchr (t1
, '.'))
295 if (*s1
== '\0' || s1
[-1] != '.' || t1
[-1] != '.')
308 /* Parse a #pragma whose sole argument is a string constant.
309 If OPT is true, the argument is optional. */
311 parse_strconst_pragma (const char* name
, int opt
)
316 t
= pragma_lex (&result
);
319 if (pragma_lex (&x
) != CPP_EOF
)
320 warning (0, "junk at end of #pragma %s", name
);
324 if (t
== CPP_EOF
&& opt
)
327 error ("invalid #pragma %s", name
);
328 return error_mark_node
;
332 handle_pragma_vtable (cpp_reader
* /*dfile*/)
334 parse_strconst_pragma ("vtable", 0);
335 sorry ("#pragma vtable no longer supported");
339 handle_pragma_unit (cpp_reader
* /*dfile*/)
341 /* Validate syntax, but don't do anything. */
342 parse_strconst_pragma ("unit", 0);
346 handle_pragma_interface (cpp_reader
* /*dfile*/)
348 tree fname
= parse_strconst_pragma ("interface", 1);
349 struct c_fileinfo
*finfo
;
350 const char *filename
;
352 if (fname
== error_mark_node
)
355 filename
= lbasename (LOCATION_FILE (input_location
));
357 filename
= TREE_STRING_POINTER (fname
);
359 finfo
= get_fileinfo (LOCATION_FILE (input_location
));
361 if (impl_file_chain
== 0)
363 /* If this is zero at this point, then we are
364 auto-implementing. */
365 if (main_input_filename
== 0)
366 main_input_filename
= LOCATION_FILE (input_location
);
369 finfo
->interface_only
= interface_strcmp (filename
);
370 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
371 a definition in another file. */
372 if (!MULTIPLE_SYMBOL_SPACES
|| !finfo
->interface_only
)
373 finfo
->interface_unknown
= 0;
376 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
377 We used to only allow this at toplevel, but that restriction was buggy
378 in older compilers and it seems reasonable to allow it in the headers
379 themselves, too. It only needs to precede the matching #p interface.
381 We don't touch finfo->interface_only or finfo->interface_unknown;
382 the user must specify a matching #p interface for this to have
386 handle_pragma_implementation (cpp_reader
* /*dfile*/)
388 tree fname
= parse_strconst_pragma ("implementation", 1);
389 const char *filename
;
390 struct impl_files
*ifiles
= impl_file_chain
;
392 if (fname
== error_mark_node
)
397 if (main_input_filename
)
398 filename
= main_input_filename
;
400 filename
= LOCATION_FILE (input_location
);
401 filename
= lbasename (filename
);
405 filename
= TREE_STRING_POINTER (fname
);
406 if (cpp_included_before (parse_in
, filename
, input_location
))
407 warning (0, "#pragma implementation for %qs appears after "
408 "file is included", filename
);
411 for (; ifiles
; ifiles
= ifiles
->next
)
413 if (! filename_cmp (ifiles
->filename
, filename
))
418 ifiles
= XNEW (struct impl_files
);
419 ifiles
->filename
= xstrdup (filename
);
420 ifiles
->next
= impl_file_chain
;
421 impl_file_chain
= ifiles
;
425 /* Issue an error message indicating that the lookup of NAME (an
426 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
429 unqualified_name_lookup_error (tree name
, location_t loc
)
431 if (loc
== UNKNOWN_LOCATION
)
432 loc
= EXPR_LOC_OR_LOC (name
, input_location
);
434 if (IDENTIFIER_OPNAME_P (name
))
436 if (name
!= cp_operator_id (ERROR_MARK
))
437 error_at (loc
, "%qD not defined", name
);
441 if (!objc_diagnose_private_ivar (name
))
443 error_at (loc
, "%qD was not declared in this scope", name
);
444 suggest_alternatives_for (loc
, name
, true);
446 /* Prevent repeated error messages by creating a VAR_DECL with
447 this NAME in the innermost block scope. */
448 if (local_bindings_p ())
451 decl
= build_decl (loc
, VAR_DECL
, name
, error_mark_node
);
452 DECL_CONTEXT (decl
) = current_function_decl
;
453 push_local_binding (name
, decl
, 0);
454 /* Mark the variable as used so that we do not get warnings
455 about it being unused later. */
456 TREE_USED (decl
) = 1;
460 return error_mark_node
;
463 /* Like unqualified_name_lookup_error, but NAME_EXPR is an unqualified-id
464 NAME, encapsulated with its location in a CP_EXPR, used as a function.
465 Returns an appropriate expression for NAME. */
468 unqualified_fn_lookup_error (cp_expr name_expr
)
470 tree name
= name_expr
.get_value ();
471 location_t loc
= name_expr
.get_location ();
472 if (loc
== UNKNOWN_LOCATION
)
473 loc
= input_location
;
475 if (processing_template_decl
)
477 /* In a template, it is invalid to write "f()" or "f(3)" if no
478 declaration of "f" is available. Historically, G++ and most
479 other compilers accepted that usage since they deferred all name
480 lookup until instantiation time rather than doing unqualified
481 name lookup at template definition time; explain to the user what
484 Note that we have the exact wording of the following message in
485 the manual (trouble.texi, node "Name lookup"), so they need to
487 permerror (loc
, "there are no arguments to %qD that depend on a template "
488 "parameter, so a declaration of %qD must be available",
491 if (!flag_permissive
)
496 inform (loc
, "(if you use %<-fpermissive%>, G++ will accept your "
497 "code, but allowing the use of an undeclared name is "
505 return unqualified_name_lookup_error (name
, loc
);
508 /* Wrapper around build_lang_decl_loc(). Should gradually move to
509 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
510 build_lang_decl(). */
513 build_lang_decl (enum tree_code code
, tree name
, tree type
)
515 return build_lang_decl_loc (input_location
, code
, name
, type
);
518 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
519 DECL_LANG_SPECIFIC info to the result. */
522 build_lang_decl_loc (location_t loc
, enum tree_code code
, tree name
, tree type
)
526 t
= build_decl (loc
, code
, name
, type
);
527 retrofit_lang_decl (t
);
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
;
542 if (DECL_LANG_SPECIFIC (t
))
545 if (TREE_CODE (t
) == FUNCTION_DECL
)
546 sel
= 1, size
= sizeof (struct lang_decl_fn
);
547 else if (TREE_CODE (t
) == NAMESPACE_DECL
)
548 sel
= 2, size
= sizeof (struct lang_decl_ns
);
549 else if (TREE_CODE (t
) == PARM_DECL
)
550 sel
= 3, size
= sizeof (struct lang_decl_parm
);
551 else if (LANG_DECL_HAS_MIN (t
))
552 sel
= 0, size
= sizeof (struct lang_decl_min
);
556 ld
= (struct lang_decl
*) ggc_internal_cleared_alloc (size
);
558 ld
->u
.base
.selector
= sel
;
560 DECL_LANG_SPECIFIC (t
) = ld
;
561 if (current_lang_name
== lang_name_cplusplus
562 || decl_linkage (t
) == lk_none
)
563 SET_DECL_LANGUAGE (t
, lang_cplusplus
);
564 else if (current_lang_name
== lang_name_c
)
565 SET_DECL_LANGUAGE (t
, lang_c
);
569 if (GATHER_STATISTICS
)
571 tree_node_counts
[(int)lang_decl
] += 1;
572 tree_node_sizes
[(int)lang_decl
] += size
;
577 cxx_dup_lang_specific_decl (tree node
)
580 struct lang_decl
*ld
;
582 if (! DECL_LANG_SPECIFIC (node
))
585 if (TREE_CODE (node
) == FUNCTION_DECL
)
586 size
= sizeof (struct lang_decl_fn
);
587 else if (TREE_CODE (node
) == NAMESPACE_DECL
)
588 size
= sizeof (struct lang_decl_ns
);
589 else if (TREE_CODE (node
) == PARM_DECL
)
590 size
= sizeof (struct lang_decl_parm
);
591 else if (LANG_DECL_HAS_MIN (node
))
592 size
= sizeof (struct lang_decl_min
);
596 ld
= (struct lang_decl
*) ggc_internal_alloc (size
);
597 memcpy (ld
, DECL_LANG_SPECIFIC (node
), size
);
598 DECL_LANG_SPECIFIC (node
) = ld
;
600 if (GATHER_STATISTICS
)
602 tree_node_counts
[(int)lang_decl
] += 1;
603 tree_node_sizes
[(int)lang_decl
] += size
;
607 /* Copy DECL, including any language-specific parts. */
610 copy_decl (tree decl
)
614 copy
= copy_node (decl
);
615 cxx_dup_lang_specific_decl (copy
);
619 /* Replace the shared language-specific parts of NODE with a new copy. */
622 copy_lang_type (tree node
)
625 struct lang_type
*lt
;
627 if (! TYPE_LANG_SPECIFIC (node
))
630 if (TYPE_LANG_SPECIFIC (node
)->u
.h
.is_lang_type_class
)
631 size
= sizeof (struct lang_type
);
633 size
= sizeof (struct lang_type_ptrmem
);
634 lt
= (struct lang_type
*) ggc_internal_alloc (size
);
635 memcpy (lt
, TYPE_LANG_SPECIFIC (node
), size
);
636 TYPE_LANG_SPECIFIC (node
) = lt
;
638 if (GATHER_STATISTICS
)
640 tree_node_counts
[(int)lang_type
] += 1;
641 tree_node_sizes
[(int)lang_type
] += size
;
645 /* Copy TYPE, including any language-specific parts. */
648 copy_type (tree type
)
652 copy
= copy_node (type
);
653 copy_lang_type (copy
);
658 cxx_make_type (enum tree_code code
)
660 tree t
= make_node (code
);
662 /* Create lang_type structure. */
663 if (RECORD_OR_UNION_CODE_P (code
)
664 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
667 = (struct lang_type
*) ggc_internal_cleared_alloc
668 (sizeof (struct lang_type
));
670 TYPE_LANG_SPECIFIC (t
) = pi
;
671 pi
->u
.c
.h
.is_lang_type_class
= 1;
673 if (GATHER_STATISTICS
)
675 tree_node_counts
[(int)lang_type
] += 1;
676 tree_node_sizes
[(int)lang_type
] += sizeof (struct lang_type
);
680 /* Set up some flags that give proper default behavior. */
681 if (RECORD_OR_UNION_CODE_P (code
))
683 struct c_fileinfo
*finfo
= \
684 get_fileinfo (LOCATION_FILE (input_location
));
685 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, finfo
->interface_unknown
);
686 CLASSTYPE_INTERFACE_ONLY (t
) = finfo
->interface_only
;
693 make_class_type (enum tree_code code
)
695 tree t
= cxx_make_type (code
);
696 SET_CLASS_TYPE_P (t
, 1);
700 /* Returns true if we are currently in the main source file, or in a
701 template instantiation started from the main source file. */
704 in_main_input_context (void)
706 struct tinst_level
*tl
= outermost_tinst_level();
709 return filename_cmp (main_input_filename
,
710 LOCATION_FILE (tl
->locus
)) == 0;
712 return filename_cmp (main_input_filename
, LOCATION_FILE (input_location
)) == 0;