Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997, 1998
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "c_types.h"
63
#include "id_ops.h"
64
#include "type_ops.h"
65
#include "namespace.h"
66
#include "parse.h"
67
#include "predict.h"
68
#include "syntax.h"
69
 
70
 
71
/*
72
    PARSING C++ USING SID
73
 
74
    The parser for this compiler is generated using the sid tool.
75
    Basically sid can generate a parser for any grammar it can transform
76
    into an LL(1) grammar.  Unfortunately, C++ is not a LL(1) language,
77
    so that no such grammar for C++ can be written.  In fact, C++ is a
78
    LL(k) language since a potentially unlimited look-ahead is necessary
79
    to distinguish declarations from expressions.  However the points
80
    at which C++ is not LL(1) are relatively few, so we have used sid's
81
    predicate mechanism to enable the look-ahead in these places to be
82
    done by hand, leaving sid to deal with the rest.  This file contains
83
    the implementation of these predicates.  A number of the routines
84
    contain conditional compilation to handle the differences between
85
    the C++ and C grammars.  Note that if the grammars are changed it
86
    may also be necessary to modify these routines.
87
 
88
    The look-ahead itself is implemented using next_token to read and store
89
    the next token.  Provided crt_token is reset afterwards, this will be
90
    invisible to the main parser.
91
*/
92
 
93
 
94
/*
95
    TOKEN LOOK-UP TABLE
96
 
97
    This table gives a simple look-up for lexical tokens to predict which
98
    kind of construct is likely to start with this token.  The values
99
    themselves are built into symbols.h.
100
*/
101
 
102
#define TOK_NONE			0
103
#define TOK_DECLARATION			1
104
#define TOK_DECL_SPEC			2
105
#define TOK_EXTERN			3
106
#define TOK_EXP				4
107
#define TOK_NESTED_NAME			5
108
#define TOK_FULL_NAME			6
109
#define TOK_SIMPLE_TYPE			7
110
#define TOK_STATEMENT			8
111
#define TOK_TYPE			9
112
#define TOK_TYPE_KEY			10
113
#define TOK_TYPE_SPEC			11
114
 
115
#if LANGUAGE_CPP
116
#define TOK_ASM				TOK_DECLARATION
117
#else
118
#define TOK_ASM				TOK_STATEMENT
119
#endif
120
 
7 7u83 121
#define lookup_token(T)			((int)tokens[(T)])
2 7u83 122
 
7 7u83 123
static unsigned char tokens[] = {
124
#define LEX_TOKEN(A, B, C)		(C),
2 7u83 125
#include "symbols.h"
126
#undef LEX_TOKEN
7 7u83 127
	TOK_NONE
128
};
2 7u83 129
 
130
 
131
/*
132
    PARSER STATE FLAGS
133
 
134
    These flags are used to indicate various parser states.  The flag
135
    have_type_specifier is set during reading a sequence of declaration-
136
    specifiers or type-specifiers to indicate that a type-specifier (other
137
    than a cv-qualifier) has been read.  Similarly have_type_declaration
138
    is set to indicate an elaborated-type-specifier.  in_function_defn
139
    is used to count nested function definitions.
140
*/
141
 
7 7u83 142
int have_type_specifier = 0;
143
int have_type_declaration = TYPE_DECL_NONE;
144
int have_func_declarator = 0;
145
int in_function_defn = 0;
146
int in_class_defn = 0;
147
int in_declaration = 0;
148
int in_default_arg = 0;
149
int in_weak_param = 0;
150
int in_ptr_mem_selector = 0;
151
int in_token_decl = 0;
152
int in_template_decl = 0;
153
int really_in_function_defn = 0;
154
int really_in_class_defn = 0;
155
int is_function_next = 0;
156
int is_constructor_next = 0;
2 7u83 157
 
158
 
159
/*
160
    PARSER STATE COUNTERS
161
 
162
    These variables are used to keep count of various items of interest
163
    in the parser, such as the number of expressions which have side
164
    effects and the number of type definitions.
165
*/
166
 
7 7u83 167
int no_side_effects = 0;
168
int no_type_defns = 0;
169
int have_destructor = 0;
170
unsigned long no_declarations = 0;
171
unsigned long no_token_defns = 0;
2 7u83 172
 
173
 
174
/*
175
    FEATURE USE FLAGS
176
 
177
    These flags are set to indicate that certain features, which require
178
    the program to perform extra checks, have been used.
179
*/
180
 
7 7u83 181
int used_extern_volatile = 0;
182
int used_register = 0;
2 7u83 183
 
184
 
185
/*
186
    FORWARD DECLARATIONS
187
 
188
    The following look-ahead functions need to be declared in advance.
189
*/
190
 
191
#if LANGUAGE_CPP
7 7u83 192
static int predict_declarator(int, int, int);
2 7u83 193
#endif
194
 
195
 
196
/*
197
    SKIP OVER A BRACKETED SEQUENCE
198
 
199
    This routine is called after reading an open bracket to skip to the
200
    corresponding close bracket.  It returns the number of tokens skipped.
201
*/
202
 
203
#if LANGUAGE_CPP
204
 
7 7u83 205
static int
206
skip_brackets(void)
2 7u83 207
{
7 7u83 208
	int n = 0;
209
	int brackets = 1;
210
	for (;;) {
211
		int t = next_token();
212
		n++;
213
		switch (t) {
214
		case lex_open_Hround:
215
		case lex_open_Hsquare_H1:
216
		case lex_open_Hbrace_H1:
217
			/* Open bracket */
218
			brackets++;
219
			break;
220
		case lex_close_Hround:
221
		case lex_close_Hsquare_H1:
222
		case lex_close_Hbrace_H1:
223
			/* Close bracket */
224
			if (--brackets == 0) {
225
				return(n);
226
			}
227
			break;
228
		case lex_eof:
229
			/* Premature end of file */
230
			return(n);
231
		}
2 7u83 232
	}
7 7u83 233
	/* NOTREACHED */
2 7u83 234
}
235
 
236
#endif
237
 
238
 
239
/*
240
    SKIP OVER AN OPERATOR NAME
241
 
242
    This routine is called after 'operator' has been read to return the
243
    token immediately following.  It has to deal with both overloaded
244
    operator function names and conversion function names.  Note that
245
    class and enumeration definitions are allowed in a conversion function
246
    name by the syntax, but are weeded out later.  Skipping over them
247
    here would therefore seem to be a lot of effort for something which
248
    is going to prove illegal however it is interpreted.
249
*/
250
 
251
#if LANGUAGE_CPP
252
 
7 7u83 253
static int
254
skip_operator(void)
2 7u83 255
{
7 7u83 256
	int t, c;
257
	int go = 1;
258
	int have_type = 0;
2 7u83 259
 
7 7u83 260
	/* Check for conversion function names */
261
	do {
262
		t = next_token();
263
		c = lookup_token(t);
264
		switch (c) {
265
		case TOK_SIMPLE_TYPE:
266
		case TOK_TYPE_SPEC:
267
		case TOK_TYPE:
268
			/* These are type-specifiers */
269
			have_type = 1;
270
			break;
271
		case TOK_NESTED_NAME:
272
		case TOK_FULL_NAME: {
273
			/* Look for nested type names */
274
			PPTOKEN *p = crt_token;
275
			NAMESPACE np = crt_lookup;
276
			int t2 = next_token();
277
			c = lookup_token(t2);
278
			if (c != TOK_TYPE) {
279
				crt_lookup = np;
280
				crt_token = p;
281
				return(t);
282
			}
283
			have_type = 1;
284
			break;
2 7u83 285
		}
7 7u83 286
		case TOK_TYPE_KEY:
287
			/* These are elaborated-type-specifiers */
288
			t = next_token();
289
			switch (t) {
290
			case lex_identifier:
291
			case lex_type_Hname:
292
			case lex_namespace_Hname:
293
			case lex_statement_Hname:
294
			case lex_template_Htype:
295
				/* Name present */
296
				t = next_token();
297
				break;
298
			case lex_full_Hname:
299
			case lex_nested_Hname:
300
			case lex_colon_Hcolon:
301
				/* Allow for nested names */
302
				t = next_token();
303
				switch (t) {
304
				case lex_identifier:
305
				case lex_type_Hname:
306
				case lex_namespace_Hname:
307
				case lex_statement_Hname:
308
				case lex_template_Htype:
309
					break;
310
				default:
311
					return(t);
312
				}
313
				break;
314
			default:
315
				/* Other characters */
316
				return(t);
2 7u83 317
			}
7 7u83 318
			have_type = 1;
319
			break;
320
		default:
2 7u83 321
			/* Other characters */
7 7u83 322
			go = 0;
323
			break;
2 7u83 324
		}
7 7u83 325
	} while (go);
326
 
327
	/* Step over any conversion function declarators */
328
	if (have_type) {
329
		go = 1;
330
		do {
331
			switch (t) {
332
			case lex_and_H1:
333
			case lex_full_Hname_Hstar:
334
			case lex_nested_Hname_Hstar:
335
			case lex_star:
336
			case lex_const:
337
			case lex_volatile:
338
				/* Pointer operators */
339
				t = next_token();
340
				break;
341
			default:
342
				/* Other characters */
343
				go = 0;
344
				break;
345
			}
346
		} while (go);
347
		return(t);
2 7u83 348
	}
349
 
7 7u83 350
	/* Check for overloaded operator function names */
351
	switch (t) {
352
	case lex_open_Hround:
353
		/* Check for 'operator ()' */
354
		t = next_token();
355
		if (t == lex_close_Hround) {
356
			t = next_token();
2 7u83 357
		}
7 7u83 358
		break;
359
	case lex_open_Hsquare_H1:
360
		/* Check for 'operator []' */
361
		t = next_token();
362
		if (t == lex_close_Hsquare_H1) {
363
			t = next_token();
2 7u83 364
		}
7 7u83 365
		break;
366
	case lex_question:
367
		/* Check for 'operator ?:' */
368
		t = next_token();
369
		if (t == lex_colon) {
370
			t = next_token();
2 7u83 371
		}
7 7u83 372
		break;
373
	case lex_new:
374
	case lex_delete:
375
		/* Check for 'operator new []' and 'operator delete []' */
376
		t = next_token();
377
		if (t == lex_open_Hsquare_H1) {
378
			PPTOKEN *p = crt_token;
379
			NAMESPACE np = crt_lookup;
380
			int t2 = next_token();
381
			if (t2 == lex_close_Hsquare_H1) {
382
				t = next_token();
383
			} else {
384
				crt_token = p;
385
				crt_lookup = np;
386
			}
387
		}
388
		break;
2 7u83 389
	}
7 7u83 390
	return(t);
2 7u83 391
}
392
 
393
#endif
394
 
395
 
396
/*
397
    LOOK-AHEAD FOR FUNCTION PARAMETERS
398
 
399
    This routine is used to distinguish lists of function parameters from
400
    lists of expressions (such as function arguments or function style
401
    initialisers.  It is called after an initial open bracket has been
402
    read.  It returns 1 if the following list definitely consists of
403
    function parameters, 0 if it definitely consists of expressions,
404
    and 2 if it could be either.
405
*/
406
 
407
#if LANGUAGE_CPP
408
 
7 7u83 409
static int
410
predict_func_params(int t, int empty, int depth)
2 7u83 411
{
7 7u83 412
	int c = lookup_token(t);
413
	switch (c) {
414
	case TOK_DECLARATION:
415
	case TOK_DECL_SPEC:
416
	case TOK_EXTERN:
417
	case TOK_TYPE_KEY:
418
	case TOK_TYPE_SPEC:
419
		/* These are all obviously parameter declarations */
420
		return(1);
421
	case TOK_SIMPLE_TYPE:
422
	case TOK_TYPE:
423
		/* These are simple-type-specifiers */
424
		t = next_token();
425
		if (t == lex_open_Hround) {
426
			t = next_token();
427
			return(predict_func_params(t, 1, 1));
428
		}
429
		return(1);
430
	case TOK_NESTED_NAME:
431
	case TOK_FULL_NAME:
432
		/* Look for nested type-names */
433
		t = next_token();
434
		c = lookup_token(t);
435
		if (c == TOK_TYPE) {
436
			t = next_token();
437
			if (t == lex_open_Hround) {
438
				t = next_token();
439
				return(predict_func_params(t, 1, 1));
440
			}
441
			return(1);
442
		}
443
		return(0);
2 7u83 444
	}
7 7u83 445
	if (t == lex_ellipsis) {
446
		return(1);
2 7u83 447
	}
7 7u83 448
	if (t == lex_ellipsis_Hexp) {
449
		return(1);
450
	}
451
	if (t == lex_close_Hround) {
452
		/* Empty pair of brackets */
453
		return(empty);
454
	}
455
	if (depth) {
456
		int d = predict_declarator(t, 2, 1);
457
		if (d == 0) {
458
			return(0);
2 7u83 459
		}
7 7u83 460
		if (d == 2) {
461
			/* Comma - check next parameter */
462
			t = next_token();
463
			return(predict_func_params(t, 1, 0));
464
		}
465
		if (d == 3) {
466
			return(2);
467
		}
468
		return(1);
2 7u83 469
	}
7 7u83 470
	return(0);
2 7u83 471
}
472
 
473
#endif
474
 
475
 
476
/*
477
    LOOK-AHEAD FOR DECLARATORS
478
 
479
    This routine is used to distinguish declarators from other constructs,
480
    including expressions.  The argument depth indicates the number of
481
    open brackets which have been read before the routine is entered.
482
    The argument loc is 0 to indicate normal declarators, 1 to indicate
483
    abstract declarators and 2 to indicate parameter declarators.  The
484
    return value is 0 if this is definitely not a declarator, and 1 if it
485
    definitely is.  The return values 2 and 3 are used to indicate possible
486
    declarators in the parameter case, with 2 indicating a following comma
487
    and 3 a close bracket.
488
*/
489
 
490
#if LANGUAGE_CPP
491
 
7 7u83 492
static int
493
predict_declarator(int t, int loc, int depth)
2 7u83 494
{
7 7u83 495
	int go = 1;
496
	int res = -1;
497
	int have_init = 0;
498
	NAMESPACE ns = NULL_nspace;
2 7u83 499
 
7 7u83 500
	/* Step over open brackets and pointer operations */
501
	while (go) {
502
		switch (t) {
503
		case lex_and_H1:
504
		case lex_star:
505
			/* Can be a pointer or reference or a unary operator */
506
			t = next_token();
507
			if (t == lex_const || t == lex_volatile) {
508
				/* This is definitely a pointer operation */
509
				return(1);
510
			}
511
			break;
512
		case lex_full_Hname_Hstar:
513
		case lex_nested_Hname_Hstar:
514
			/* Definitely a pointer to member */
515
			return(1);
516
		case lex_open_Hround:
517
			/* Nested declarator brackets */
518
			depth++;
519
			t = next_token();
520
			break;
521
		default:
522
			/* Other tokens */
523
			go = 0;
524
			break;
2 7u83 525
		}
526
	}
527
 
7 7u83 528
	/* Check for declarator-id */
529
	if (loc != 1) {
530
		switch (t) {
531
		case lex_nested_Hname:
532
		case lex_full_Hname:
533
		case lex_colon_Hcolon:
534
			/* Allow for qualified identifiers */
535
			ns = crt_lookup;
536
			t = next_token();
537
			break;
538
		}
539
		switch (t) {
540
		case lex_identifier:
541
		case lex_type_Hname:
542
		case lex_namespace_Hname:
543
		case lex_statement_Hname:
544
		case lex_destructor_Hname:
545
		case lex_template_Hid:
546
		case lex_template_Htype:
547
			/* Identifiers and destructors */
548
			t = next_token();
549
			break;
550
		case lex_operator:
551
			/* Operator function identifiers */
552
			t = skip_operator();
553
			break;
554
		default:
555
			/* Anything else isn't a declarator-id */
556
			if (loc == 0 || !IS_NULL_nspace(ns)) {
557
				return(0);
558
			}
559
			break;
560
		}
2 7u83 561
	}
7 7u83 562
 
563
	/* Check for declarator tail and initialiser */
564
	if (!IS_NULL_nspace(ns)) {
565
		IGNORE add_nested_nspace(ns);
2 7u83 566
	}
7 7u83 567
	for (;;) {
568
		switch (t) {
2 7u83 569
 
7 7u83 570
		case lex_open_Hround: {
571
			/* Function parameters, function call or initialiser */
572
			int d;
573
			PPTOKEN *p = crt_token;
574
			NAMESPACE np = crt_lookup;
575
			t = next_token();
576
			d = predict_func_params(t, 1, 0);
577
			if (d == 1) {
578
				/* Definitely function parameters */
579
				res = 1;
580
			} else if (d == 0) {
581
				/* Definitely expression list */
582
				if (depth > 0 || have_init) {
583
					/* Can't be an initialiser in these
584
					 * cases */
585
					res = 0;
586
				} else {
587
					crt_lookup = np;
588
					crt_token = p;
589
					IGNORE skip_brackets();
590
					have_init = 1;
591
				}
592
			}
593
			break;
2 7u83 594
		}
595
 
7 7u83 596
		case lex_open_Hsquare_H1: {
597
			/* Array dimension or index expression */
598
			int d = skip_brackets();
599
			if (d == 1) {
600
				/* Only array dimensions can be empty */
601
				res = 1;
602
			}
603
			break;
2 7u83 604
		}
605
 
7 7u83 606
		case lex_assign: {
607
			/* Initialiser or assignment expression */
608
			int brackets = 0;
609
			if (loc == 1 || depth > 0) {
610
				/* Can't have initialiser in these cases */
611
				res = 0;
612
				break;
2 7u83 613
			}
7 7u83 614
			t = next_token();
615
			if (t == lex_open_Hbrace_H1) {
616
				/* Can only have aggregates in initialisers */
617
				if (loc == 0) {
618
					res = 1;
619
					break;
620
				}
2 7u83 621
			}
7 7u83 622
			/* Scan to end of expression */
623
			go = 1;
624
			while (go) {
625
				switch (t) {
626
				case lex_open_Hround:
627
				case lex_open_Hsquare_H1:
628
				case lex_open_Hbrace_H1:
629
					/* Open bracket */
630
					brackets++;
631
					break;
632
				case lex_close_Hround:
633
				case lex_close_Hsquare_H1:
634
				case lex_close_Hbrace_H1:
635
					/* Close bracket */
636
					brackets--;
637
					if (brackets < 0 && loc == 2) {
638
						res = 3;
639
					}
640
					break;
641
				case lex_semicolon:
642
					/* End of declaration */
643
					if (brackets <= 0) {
644
						res = 3;
645
					}
646
					break;
647
				case lex_comma: {
648
					/* Comma */
649
					if (brackets <= 0) {
650
						/* Check rest of declaration */
651
						if (loc == 1) {
652
							res = 0;
653
						} else if (loc == 2) {
654
							res = 2;
655
						} else {
656
							t = next_token();
657
							res = predict_declarator(t, 0, 0);
658
						}
659
					}
660
					break;
661
				}
662
				case lex_ellipsis:
663
				case lex_ellipsis_Hexp:
664
					/* Ellipsis */
665
					if (loc == 2 && brackets <= 0) {
666
						res = 1;
667
					}
668
					break;
669
				case lex_eof:
670
					/* Premature end of file */
671
					res = 1;
672
					break;
673
				}
674
				if (res != -1) {
675
					break;
676
				}
677
				t = next_token();
2 7u83 678
			}
7 7u83 679
			break;
680
		}
681
 
682
		case lex_semicolon:
683
		case lex_close_Htemplate:
684
			/* End of declaration (don't worry about depth) */
685
			res = 3;
686
			break;
687
 
688
		case lex_comma:
689
			/* Comma */
690
			if (depth <= 0) {
2 7u83 691
				/* Check rest of declaration */
7 7u83 692
				if (loc == 1) {
693
					res = 1;
694
				} else if (loc == 2) {
695
					res = 2;
2 7u83 696
				} else {
7 7u83 697
					t = next_token();
698
					res = predict_declarator(t, 0, 0);
2 7u83 699
				}
7 7u83 700
			} else {
701
				res = 0;
2 7u83 702
			}
7 7u83 703
			break;
704
 
705
		case lex_ellipsis:
706
		case lex_ellipsis_Hexp:
707
			/* Ellipsis */
708
			if (depth <= 0 && loc == 2) {
709
				res = 1;
710
			} else {
711
				res = 0;
2 7u83 712
			}
7 7u83 713
			break;
714
 
715
		case lex_close_Hround: {
716
			/* Declarator close bracket */
717
			depth--;
718
			if (depth < 0) {
719
				if (loc == 1) {
720
					res = 1;
721
				} else if (loc == 2) {
722
					res = 3;
723
				}
2 7u83 724
			}
7 7u83 725
			break;
2 7u83 726
		}
727
 
7 7u83 728
		default:
729
			/* Nothing else can appear in a declaration */
730
			res = 0;
731
			break;
2 7u83 732
		}
7 7u83 733
		if (res != -1) {
734
			break;
2 7u83 735
		}
7 7u83 736
		t = next_token();
2 7u83 737
	}
7 7u83 738
	if (!IS_NULL_nspace(ns)) {
739
		IGNORE remove_nested_nspace(ns);
740
	}
741
	return(res);
2 7u83 742
}
743
 
744
#endif
745
 
746
 
747
/*
748
    LOOK-AHEAD FOR DECLARATION STATEMENTS
749
 
750
    A look-ahead is needed in declaration-statement to distinguish
751
    declarations from other statements including, in particular, expression
752
    statements.  The disambiguation rule is that anything which looks like
753
    a declaration is a declaration.  Note that the empty statement is
754
    classified as an expression-statement.  Actually the real answer to
755
    questions like is 'int ( a ) ;' a declaration or an expression is,
756
    are flying pigs kosher?
757
*/
758
 
7 7u83 759
int
760
predict_decl(void)
2 7u83 761
{
7 7u83 762
	int t = crt_lex_token;
763
	int c = lookup_token(t);
764
	switch (c) {
765
	case TOK_DECLARATION:
766
	case TOK_DECL_SPEC:
767
	case TOK_EXTERN:
768
	case TOK_TYPE_KEY:
769
	case TOK_TYPE_SPEC:
770
		/* These are all obviously declarations */
771
		return(1);
772
	case TOK_SIMPLE_TYPE:
773
	case TOK_TYPE: {
774
		/* These are simple-type-specifiers */
775
		int d = 1;
776
		PPTOKEN *p = crt_token;
777
		NAMESPACE np = crt_lookup;
778
		t = next_token();
2 7u83 779
#if LANGUAGE_CPP
7 7u83 780
		if (t == lex_open_Hround) {
781
			/* This is the tricky case, 'T ( ...' */
782
			t = next_token();
783
			d = predict_declarator(t, 0, 1);
784
		} else /* continued ... */
2 7u83 785
#endif
7 7u83 786
			if (t == lex_colon) {
787
				/* Check for labels */
788
				if (c == TOK_TYPE) {
789
					d = 0;
790
				}
791
			}
792
		crt_lookup = np;
793
		crt_token = p;
794
		return(d);
2 7u83 795
	}
796
#if LANGUAGE_CPP
7 7u83 797
	case TOK_NESTED_NAME:
798
	case TOK_FULL_NAME: {
799
		/* Look for nested type-names */
800
		int d = 0;
801
		PPTOKEN *p = crt_token;
802
		NAMESPACE np = crt_lookup;
803
		t = next_token();
804
		c = lookup_token(t);
805
		if (c == TOK_TYPE) {
806
			d = 1;
807
			t = next_token();
808
			if (t == lex_open_Hround) {
809
				/* This is the tricky case, 'N::T ( ...' */
810
				t = next_token();
811
				d = predict_declarator(t, 0, 1);
812
			}
2 7u83 813
		}
7 7u83 814
		crt_lookup = np;
815
		crt_token = p;
816
		return(d);
2 7u83 817
	}
818
#endif
7 7u83 819
	}
820
	/* Nothing else is a declaration */
821
	return(0);
2 7u83 822
}
823
 
824
 
825
/*
826
    LOOK-AHEAD FOR UNDECLARED TYPES
827
 
828
    This look-ahead is used to handle syntax errors following undeclared
829
    types.  The context is in a sequence of declaration specifiers in
830
    which no type specifier has been encountered.  t gives the token read.
831
*/
832
 
7 7u83 833
static int
834
predict_undecl_type(int t, int force)
2 7u83 835
{
7 7u83 836
	switch (t) {
837
	case lex_identifier:
838
	case lex_type_Hname:
839
	case lex_namespace_Hname:
840
	case lex_statement_Hname: {
841
		int d = force;
842
		PPTOKEN *p = crt_token;
843
		NAMESPACE np = crt_lookup;
844
		if (!d) {
845
			t = next_token();
846
			switch (t) {
2 7u83 847
#if LANGUAGE_CPP
7 7u83 848
			case lex_and_H1:
849
			case lex_full_Hname_Hstar:
850
			case lex_nested_Hname_Hstar:
2 7u83 851
#endif
7 7u83 852
			case lex_star:
853
				/* These are ptr-operators */
854
				d = 1;
855
				break;
856
			case lex_open_Hround:
857
				/* This is stretching the bounds of
858
				 * possibility */
859
				break;
860
			case lex_identifier:
861
			case lex_type_Hname:
862
			case lex_namespace_Hname:
863
			case lex_statement_Hname:
864
			case lex_template_Htype:
865
			case lex_template_Hid:
866
			case lex_full_Hname:
867
			case lex_nested_Hname:
868
				/* The identifier can't be a declarator */
869
				d = 1;
870
				break;
871
			default: {
872
				/* Check for further declaration specifiers */
873
				int c = lookup_token(t);
874
				switch (c) {
875
				case TOK_DECL_SPEC:
876
				case TOK_SIMPLE_TYPE:
877
				case TOK_TYPE_KEY:
878
				case TOK_TYPE_SPEC:
879
				case TOK_EXTERN:
880
				case TOK_TYPE: {
881
					d = 1;
882
					break;
883
				}
884
				}
885
				break;
2 7u83 886
			}
7 7u83 887
			}
2 7u83 888
		}
7 7u83 889
		if (d) {
890
			p->tok = lex_type_Hname;
891
		}
892
		crt_lookup = np;
893
		crt_token = p;
894
		return(d);
2 7u83 895
	}
7 7u83 896
	}
897
	return(0);
2 7u83 898
}
899
 
900
 
901
/*
902
    LOOK-AHEAD FOR DECLARATION SPECIFIERS
903
 
904
    A look-ahead is needed to distinguish decl-specifiers from following
905
    declarators or other constructs.  Note that linkage-specifications are
906
    spotted at this stage.  In addition type names are not always what
907
    they seem.  For example in:
908
 
909
		typedef int t ;
910
		typedef int t ;
911
 
912
    the second t is a type-name, but needs to be recognised as a declarator-id
913
    rather than a decl-specifier.  Similarly in:
914
 
915
		class c {
916
		    c () ;
917
		} ;
918
 
919
    the second c is a class-name, but is actually a constructor name rather
920
    than a decl-specifier.
921
*/
922
 
7 7u83 923
int
924
predict_dspec(int force)
2 7u83 925
{
7 7u83 926
	int d = 0;
927
	int t = crt_lex_token;
928
	int c = lookup_token(t);
2 7u83 929
#if LANGUAGE_CPP
7 7u83 930
	is_constructor_next = 0;
2 7u83 931
#endif
7 7u83 932
	switch (c) {
933
	case TOK_DECL_SPEC:
934
	case TOK_SIMPLE_TYPE:
935
	case TOK_TYPE_KEY:
936
	case TOK_TYPE_SPEC:
937
		/* These are declaration-specifiers */
938
		d = 1;
939
		break;
940
	case TOK_EXTERN: {
2 7u83 941
#if LANGUAGE_CPP
7 7u83 942
		/* Explicitly check for linkage-specifications */
943
		PPTOKEN *p = crt_token;
944
		NAMESPACE np = crt_lookup;
945
		t = next_token();
946
		if (t != lex_string_Hexp && t != lex_wstring_Hexp)d = 1;
947
		crt_lookup = np;
948
		crt_token = p;
2 7u83 949
#else
7 7u83 950
		d = 1;
2 7u83 951
#endif
7 7u83 952
		break;
2 7u83 953
	}
7 7u83 954
	case TOK_TYPE:
955
		/* Only the first type name is counted */
956
		if (t == lex_type_Hname || t == lex_template_Htype) {
957
			if (!have_type_specifier) {
2 7u83 958
#if LANGUAGE_CPP
7 7u83 959
				PPTOKEN *p = crt_token;
960
				NAMESPACE np = crt_lookup;
961
				d = 1;
962
				t = next_token();
963
				if (t == lex_open_Hround) {
964
					/* Allow for constructors */
965
					t = next_token();
966
					if (predict_func_params(t, 1, 0)) {
967
						is_constructor_next = 1;
968
						d = 0;
969
					}
970
				}
971
				crt_lookup = np;
972
				crt_token = p;
2 7u83 973
#else
7 7u83 974
				d = 1;
2 7u83 975
#endif
7 7u83 976
			}
977
		} else {
978
			d = 1;
2 7u83 979
		}
7 7u83 980
		break;
2 7u83 981
#if LANGUAGE_CPP
7 7u83 982
	case TOK_NESTED_NAME:
983
	case TOK_FULL_NAME:
984
		/* Look for nested type names */
985
		if (!have_type_specifier) {
986
			PPTOKEN *p = crt_token;
987
			NAMESPACE np = crt_lookup;
988
			t = next_token();
989
			c = lookup_token(t);
990
			if (c == TOK_TYPE) {
991
				/* These are simple-type-specifiers */
992
				d = 1;
993
				t = next_token();
994
				if (t == lex_open_Hround) {
995
					/* Allow for constructors */
996
					IGNORE add_nested_nspace(np);
997
					t = next_token();
998
					if (predict_func_params(t, 1, 0)) {
999
						is_constructor_next = 1;
1000
						d = 0;
1001
					}
1002
					IGNORE remove_nested_nspace(np);
1003
				}
1004
			} else {
1005
				d = predict_undecl_type(t, force);
2 7u83 1006
			}
7 7u83 1007
			crt_lookup = np;
1008
			crt_token = p;
2 7u83 1009
		}
7 7u83 1010
		break;
2 7u83 1011
#endif
7 7u83 1012
	default:
1013
		/* Check for undefined types */
1014
		if (!have_type_specifier) {
1015
			d = predict_undecl_type(t, force);
1016
			if (d)crt_lex_token = lex_type_Hname;
1017
		}
1018
		break;
2 7u83 1019
	}
7 7u83 1020
	return(d);
2 7u83 1021
}
1022
 
1023
 
1024
/*
1025
    LOOK-AHEAD FOR TYPE SPECIFIERS
1026
 
1027
    A look-ahead is needed to distinguish type-specifiers from following
1028
    declarators or other constructs.
1029
*/
1030
 
7 7u83 1031
int
1032
predict_tspec(int force)
2 7u83 1033
{
7 7u83 1034
	int d = 0;
1035
	int t = crt_lex_token;
1036
	int c = lookup_token(t);
1037
	switch (c) {
1038
	case TOK_SIMPLE_TYPE:
1039
	case TOK_TYPE_KEY:
1040
	case TOK_TYPE_SPEC:
1041
	case TOK_TYPE:
1042
		/* These are type-specifiers */
1043
		d = 1;
1044
		break;
2 7u83 1045
#if LANGUAGE_CPP
7 7u83 1046
	case TOK_NESTED_NAME:
1047
	case TOK_FULL_NAME: {
1048
		/* Look for nested type names */
1049
		PPTOKEN *p = crt_token;
1050
		NAMESPACE np = crt_lookup;
1051
		t = next_token();
1052
		c = lookup_token(t);
1053
		if (c == TOK_TYPE) {
1054
			d = 1;
1055
		} else {
1056
			if (!have_type_specifier) {
1057
				d = predict_undecl_type(t, force);
1058
			}
2 7u83 1059
		}
7 7u83 1060
		crt_lookup = np;
1061
		crt_token = p;
1062
		break;
2 7u83 1063
	}
1064
#endif
7 7u83 1065
	default:
1066
		/* Check for undefined types */
1067
		if (!have_type_specifier) {
1068
			d = predict_undecl_type(t, force);
1069
			if (d)crt_lex_token = lex_type_Hname;
1070
		}
1071
		break;
2 7u83 1072
	}
7 7u83 1073
	return(d);
2 7u83 1074
}
1075
 
1076
 
1077
/*
1078
    LOOK-AHEAD FOR FUNCTION QUALIFIERS
1079
 
1080
    A further look-ahead is required when distinguishing type-ids from
1081
    expressions after 'T ()' has been read for some type.  If the next
1082
    token is '+' for example it is an expression, if it is 'const' it
1083
    is a declaration.  t gives the next token and e gives the default
1084
    value.
1085
*/
1086
 
1087
#if LANGUAGE_CPP
1088
 
7 7u83 1089
static int
1090
predict_qual(int t, int e)
2 7u83 1091
{
7 7u83 1092
	switch (t) {
1093
	case lex_const:
1094
	case lex_volatile:
1095
	case lex_throw:
1096
		/* Function qualifiers */
1097
		e = 1;
1098
		break;
1099
	case lex_and_H1:
1100
	case lex_and_Heq_H1:
1101
	case lex_arrow:
1102
	case lex_arrow_Hstar:
1103
	case lex_assign:
1104
	case lex_div:
1105
	case lex_div_Heq:
1106
	case lex_dot:
1107
	case lex_dot_Hstar:
1108
	case lex_eq:
1109
	case lex_greater:
1110
	case lex_greater_Heq:
1111
	case lex_less:
1112
	case lex_less_Heq:
1113
	case lex_logical_Hand_H1:
1114
	case lex_logical_Hor_H1:
1115
	case lex_lshift:
1116
	case lex_lshift_Heq:
1117
	case lex_minus:
1118
	case lex_minus_Heq:
1119
	case lex_minus_Hminus:
1120
	case lex_not_Heq_H1:
1121
	case lex_or_H1:
1122
	case lex_or_Heq_H1:
1123
	case lex_plus:
1124
	case lex_plus_Heq:
1125
	case lex_plus_Hplus:
1126
	case lex_question:
1127
	case lex_rem:
1128
	case lex_rem_Heq:
1129
	case lex_rshift:
1130
	case lex_rshift_Heq:
1131
	case lex_star:
1132
	case lex_star_Heq:
1133
	case lex_xor_H1:
1134
	case lex_xor_Heq_H1:
1135
	case lex_abs:
1136
	case lex_max:
1137
	case lex_min:
1138
		/* Binary expression operators */
1139
		e = 0;
1140
		break;
1141
	case lex_comma:
1142
		/* Comma operators */
1143
		if (e == 1) {
1144
			e = 0;
1145
		}
1146
		break;
2 7u83 1147
	}
7 7u83 1148
	return(e);
2 7u83 1149
}
1150
 
1151
#endif
1152
 
1153
 
1154
/*
1155
    LOOK-AHEAD FOR TYPE IDENTIFIERS
1156
 
1157
    A look-ahead is needed to distinguish type-ids from expressions in,
1158
    for example, sizeof expressions.  A type-id consists of a sequence
1159
    of type-specifiers followed by an optional abstract-declarator,
1160
    whereas the only tricky expressions are those consisting of a
1161
    simple-type-specifier followed by a bracketed list of expressions.
1162
    e gives the value to be returned if the result could be either.
1163
*/
1164
 
7 7u83 1165
int
1166
predict_typeid(int e)
2 7u83 1167
{
7 7u83 1168
	int t = crt_lex_token;
1169
	int c = lookup_token(t);
1170
	switch (c) {
1171
	case TOK_TYPE_KEY:
1172
	case TOK_TYPE_SPEC:
1173
		/* These are type-specifiers */
1174
		return(1);
1175
	case TOK_SIMPLE_TYPE:
1176
	case TOK_TYPE: {
1177
		/* These are simple-type-specifiers */
1178
		int d = 1;
2 7u83 1179
#if LANGUAGE_CPP
7 7u83 1180
		PPTOKEN *p = crt_token;
1181
		NAMESPACE np = crt_lookup;
1182
		t = next_token();
1183
		if (t == lex_open_Hround) {
1184
			/* This is the tricky case 'T ( ...' */
1185
			PPTOKEN *q;
1186
			NAMESPACE nq;
1187
			t = next_token();
1188
			q = crt_token;
1189
			nq = crt_lookup;
1190
			d = predict_func_params(t, 2, 0);
1191
			if (d != 1) {
1192
				if (t == lex_close_Hround) {
1193
					t = next_token();
1194
					d = predict_qual(t, e);
1195
				} else {
1196
					crt_lookup = nq;
1197
					crt_token = q;
1198
					d = predict_declarator(t, 1, 1);
1199
				}
1200
			}
2 7u83 1201
		}
7 7u83 1202
		crt_lookup = np;
1203
		crt_token = p;
2 7u83 1204
#else
7 7u83 1205
		UNUSED(e);
2 7u83 1206
#endif
7 7u83 1207
		return(d);
2 7u83 1208
	}
1209
#if LANGUAGE_CPP
7 7u83 1210
	case TOK_NESTED_NAME:
1211
	case TOK_FULL_NAME: {
1212
		/* Look for nested type-names */
1213
		int d = 0;
1214
		PPTOKEN *p = crt_token;
1215
		NAMESPACE np = crt_lookup;
1216
		t = next_token();
1217
		c = lookup_token(t);
1218
		if (c == TOK_TYPE) {
1219
			d = 1;
1220
			t = next_token();
1221
			if (t == lex_open_Hround) {
1222
				/* This is the tricky case 'N::T ( ...' */
1223
				PPTOKEN *q;
1224
				NAMESPACE nq;
1225
				t = next_token();
1226
				q = crt_token;
1227
				nq = crt_lookup;
1228
				d = predict_func_params(t, 2, 0);
1229
				if (d != 1) {
1230
					if (t == lex_close_Hround) {
1231
						t = next_token();
1232
						d = predict_qual(t, e);
1233
					} else {
1234
						crt_lookup = nq;
1235
						crt_token = q;
1236
						d = predict_declarator(t, 1, 1);
1237
					}
1238
				}
2 7u83 1239
			}
1240
		}
7 7u83 1241
		crt_lookup = np;
1242
		crt_token = p;
1243
		return(d);
2 7u83 1244
	}
1245
#endif
7 7u83 1246
	}
1247
	return(0);
2 7u83 1248
}
1249
 
1250
 
1251
/*
1252
    LOOK-AHEAD FOR TEMPLATE TYPE NAMES
1253
 
1254
    A look-ahead is needed to distinguish template type names from other
1255
    types when parsing template arguments.
1256
*/
1257
 
7 7u83 1258
int
1259
predict_typename(void)
2 7u83 1260
{
7 7u83 1261
	int d = 0;
1262
	int t = crt_lex_token;
1263
	if (t == lex_type_Hname) {
1264
		/* Unqualified type names */
1265
		IDENTIFIER id = crt_token->pp_data.id.use;
1266
		DECL_SPEC ds = DEREF_dspec(id_storage(id));
1267
		if (ds & dspec_template) {
1268
			d = 1;
1269
		}
1270
	}
2 7u83 1271
#if LANGUAGE_CPP
7 7u83 1272
	if (t == lex_full_Hname || t == lex_nested_Hname ||
1273
	    t == lex_colon_Hcolon) {
1274
		PPTOKEN *p = crt_token;
1275
		NAMESPACE np = crt_lookup;
1276
		t = next_token();
1277
		if (t == lex_type_Hname) {
1278
			/* Qualified type names */
1279
			IDENTIFIER id = crt_token->pp_data.id.use;
1280
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
1281
			if (ds & dspec_template) {
1282
				d = 1;
1283
			}
1284
		}
1285
		crt_lookup = np;
1286
		crt_token = p;
2 7u83 1287
	}
1288
#endif
7 7u83 1289
	return(d);
2 7u83 1290
}
1291
 
1292
 
1293
/*
1294
    LOOK-AHEAD FOR INITIALISERS
1295
 
1296
    A look-ahead is needed to distinguish function-style initialisers in
1297
    init-declarator from function declarators.  This predicate is called
1298
    immediately after an open bracket.  Note that anything which could
1299
    possibly be a function declarator is.  If the result is an initialiser
1300
    beginning with '(' then this first token is replaced by a dummy to
1301
    prevent further calls to predict_init.
1302
*/
1303
 
7 7u83 1304
int
1305
predict_init(void)
2 7u83 1306
{
1307
#if LANGUAGE_CPP
7 7u83 1308
	PPTOKEN *p = crt_token;
1309
	NAMESPACE np = crt_lookup;
1310
	int t = crt_lex_token;
1311
	int d = predict_func_params(t, 1, 0);
1312
	crt_lookup = np;
1313
	crt_token = p;
1314
	if (d == 0) {
1315
		/* Definitely doesn't look like a function declarator */
1316
		if (t == lex_open_Hround) {
1317
			t = lex_open_Hinit;
1318
			crt_lex_token = t;
1319
			p->tok = t;
1320
		}
1321
		return(1);
2 7u83 1322
	}
7 7u83 1323
	if (t == lex_ellipsis_Hexp) {
1324
		t = lex_ellipsis;
1325
		crt_lex_token = t;
1326
		p->tok = t;
1327
	}
2 7u83 1328
#endif
7 7u83 1329
	return(0);
2 7u83 1330
}
1331
 
1332
 
1333
/*
1334
    LOOK-AHEAD FOR DESTRUCTORS
1335
 
1336
    A look-ahead is required to distinguish a destructor-name from a
1337
    complement operator followed by a class-name.  The latter is only the
1338
    case if it is followed by a bracketed list of expressions.  The
1339
    really difficult case is when this list is empty.  The return value
1340
    is 2 following '::', 3 following '->' or '.', and 1 for other
1341
    destructors.
1342
*/
1343
 
7 7u83 1344
int
1345
predict_destr(NAMESPACE ns)
2 7u83 1346
{
1347
#if LANGUAGE_CPP
7 7u83 1348
	int d = 1;
1349
	int t = last_lex_token;
1350
	if (!IS_NULL_nspace(ns)) {
1351
		if (EQ_nspace(ns, global_namespace)) {
1352
			t = lex_colon_Hcolon;
1353
		} else {
1354
			t = lex_nested_Hname;
1355
		}
2 7u83 1356
	}
7 7u83 1357
	switch (t) {
1358
	case lex_nested_Hname:
1359
	case lex_full_Hname:
1360
		/* Always have destructor names after these */
1361
		if (cache_lookup) {
1362
			d = 2;
2 7u83 1363
		} else {
7 7u83 1364
			/* Can only happen after '.' or '->' */
1365
			d = 3;
2 7u83 1366
		}
7 7u83 1367
		break;
1368
	case lex_colon_Hcolon:
1369
		/* Never have destructor name after this */
1370
		d = 0;
1371
		break;
1372
	case lex_arrow:
1373
	case lex_dot:
1374
		/* Always have destructor names after these */
1375
		d = 3;
1376
		break;
1377
	default: {
1378
		PPTOKEN *p = crt_token;
1379
		NAMESPACE np = crt_lookup;
1380
		t = next_token();
1381
		if (t == lex_open_Hround) {
1382
			t = next_token();
1383
			if (t == lex_close_Hround) {
1384
				/* This is the commonest case */
1385
				if (in_function_defn)d = 0;
1386
			} else {
1387
				d = predict_func_params(t, 1, 0);
1388
				if (d)d = 1;
1389
			}
1390
		}
1391
		crt_lookup = np;
1392
		crt_token = p;
1393
		break;
2 7u83 1394
	}
7 7u83 1395
	}
1396
	return(d);
2 7u83 1397
#else
7 7u83 1398
	UNUSED(ns);
1399
	return(0);
2 7u83 1400
#endif
1401
}
1402
 
1403
 
1404
/*
1405
    LOOK-AHEAD FOR FUNCTION PARAMETERS
1406
 
1407
    A look-ahead is needed to distinguish a declarator-id representing a
1408
    function parameter from a type-specifier.  The only difficult case is
1409
    a type-name immediately after an open bracket.
1410
*/
1411
 
7 7u83 1412
int
1413
predict_param(void)
2 7u83 1414
{
7 7u83 1415
	int t = crt_lex_token;
1416
	switch (t) {
1417
	case lex_identifier:
1418
	case lex_namespace_Hname:
1419
	case lex_statement_Hname:
1420
	case lex_destructor_Hname:
1421
	case lex_template_Hid:
1422
	case lex_operator:
1423
		/* These are all unqualified-ids */
1424
		return(1);
1425
	case lex_type_Hname:
1426
	case lex_template_Htype:
1427
		/* Check for type names */
1428
		if (last_lex_token != lex_open_Hround) {
1429
			return(1);
1430
		}
1431
		break;
2 7u83 1432
#if LANGUAGE_CPP
7 7u83 1433
	case lex_full_Hname:
1434
	case lex_nested_Hname:
1435
	case lex_colon_Hcolon: {
1436
		/* Check token after a nested name */
1437
		int d;
1438
		PPTOKEN *p = crt_token;
1439
		NAMESPACE np = crt_lookup;
1440
		crt_lex_token = next_token();
1441
		d = predict_param();
1442
		crt_lex_token = t;
1443
		crt_lookup = np;
1444
		crt_token = p;
1445
		return(d);
2 7u83 1446
	}
1447
#endif
7 7u83 1448
	}
1449
	return(0);
2 7u83 1450
}
1451
 
1452
 
1453
/*
1454
    LOOK-AHEAD FOR CLASS SPECIFICATIONS
1455
 
1456
    A look-ahead of one token is needed in type-specifier to distinguish a
1457
    class-specifier or an enum-specifier from an elaborated-type-specifier.
1458
    This predicate is called after a class-key to check for a class-specifier
1459
    or an enum-specifier.  This is recognised by an optional qualified
1460
    identifier followed by an open brace (for the class body) or a colon
1461
    (for a base-clause, if col is true).
1462
*/
1463
 
7 7u83 1464
int
1465
predict_class(int col)
2 7u83 1466
{
7 7u83 1467
	int t = crt_lex_token;
2 7u83 1468
 
7 7u83 1469
	/* Examine the name */
1470
	switch (t) {
1471
	case lex_identifier:
1472
	case lex_type_Hname:
1473
	case lex_namespace_Hname:
1474
	case lex_statement_Hname:
1475
	case lex_template_Htype: {
1476
		/* Name present */
1477
		PPTOKEN *p = crt_token;
1478
		NAMESPACE np = crt_lookup;
1479
		t = next_token();
1480
		crt_lookup = np;
1481
		crt_token = p;
1482
		break;
2 7u83 1483
	}
1484
#if LANGUAGE_CPP
7 7u83 1485
	case lex_full_Hname:
1486
	case lex_nested_Hname:
1487
	case lex_colon_Hcolon: {
1488
		/* Allow for nested names */
1489
		PPTOKEN *p = crt_token;
1490
		NAMESPACE np = crt_lookup;
1491
		t = next_token();
1492
		switch (t) {
1493
		case lex_identifier:
1494
		case lex_type_Hname:
1495
		case lex_namespace_Hname:
1496
		case lex_statement_Hname:
1497
		case lex_template_Htype:
1498
			/* Nested name present */
1499
			t = next_token();
1500
			crt_lookup = np;
1501
			crt_token = p;
1502
			break;
1503
		default:
1504
			/* Invalid name */
1505
			crt_lookup = np;
1506
			crt_token = p;
1507
			return(0);
2 7u83 1508
		}
7 7u83 1509
		break;
2 7u83 1510
	}
1511
#endif
7 7u83 1512
	}
2 7u83 1513
 
7 7u83 1514
	/* Examine the token following the name */
1515
	if (t == lex_open_Hbrace_H1) {
1516
		return(1);
1517
	}
1518
	if (t == lex_colon && !in_token_decl) {
1519
		return(col);
1520
	}
1521
	return(0);
2 7u83 1522
}
1523
 
1524
 
1525
/*
1526
    LOOK-AHEAD FOR FUNCTION DEFINITIONS
1527
 
1528
    A predicate is needed to distinguish function definitions from function
1529
    declarations. A function definition is recognised by means of a '{'
1530
    (starting a function-body), a ':' (starting a ctor-initialiser), or
1531
    a 'try' (starting a function-try-block).  It has already been checked
1532
    whether the type of the current declarator is a function.
1533
*/
1534
 
7 7u83 1535
int
1536
predict_func_defn(void)
2 7u83 1537
{
7 7u83 1538
	int t = crt_lex_token;
1539
	if (t == lex_open_Hbrace_H1) {
1540
		return(1);
1541
	}
2 7u83 1542
#if LANGUAGE_CPP
7 7u83 1543
	if (t == lex_try) {
1544
		return(1);
1545
	}
1546
	if (t == lex_colon && !in_token_decl) {
1547
		return(1);
1548
	}
2 7u83 1549
#endif
7 7u83 1550
	return(0);
2 7u83 1551
}
1552
 
1553
 
1554
/*
1555
    LOOK-AHEAD FOR OBJECT INITIALISERS
1556
 
1557
    A predicate is needed to determine whether an object declaration
1558
    is followed by an initialising expression.  Note this is not the
1559
    same as the object being defined.
1560
*/
1561
 
7 7u83 1562
int
1563
predict_obj_defn(void)
2 7u83 1564
{
7 7u83 1565
	int t = crt_lex_token;
1566
	if (t == lex_assign) {
1567
		return(1);
1568
	}
2 7u83 1569
#if LANGUAGE_CPP
7 7u83 1570
	if (t == lex_open_Hround) {
1571
		return(1);
1572
	}
2 7u83 1573
#endif
7 7u83 1574
	return(0);
2 7u83 1575
}
1576
 
1577
 
1578
/*
1579
    LOOK-AHEAD FOR POINTER OPERATORS
1580
 
1581
    A look-ahead is needed following a conversion-function-id for possible
1582
    ptr-operators.  This is because, for instance 'operator int *' needs
1583
    to be resolved as the conversion function for 'int *' rather than as
1584
    the conversion function for 'int' times something.  The argument ref
1585
    indicates whether '&' is a valid pointer operator.
1586
*/
1587
 
7 7u83 1588
int
1589
predict_ptr(int ref)
2 7u83 1590
{
7 7u83 1591
	int t = crt_lex_token;
1592
	if (t == lex_star) {
1593
		return(1);
1594
	}
2 7u83 1595
#if LANGUAGE_CPP
7 7u83 1596
	if (t == lex_full_Hname_Hstar || t == lex_nested_Hname_Hstar) {
1597
		return(1);
1598
	}
1599
	if (t == lex_and_H1) {
1600
		return(ref);
1601
	}
2 7u83 1602
#else
7 7u83 1603
	UNUSED(ref);
2 7u83 1604
#endif
7 7u83 1605
	return(0);
2 7u83 1606
}
1607
 
1608
 
1609
/*
1610
    LOOK-AHEAD FOR OPERATOR NAMES
1611
 
1612
    A look-ahead is needed in the token syntax for overloaded operator
1613
    and conversion function names.
1614
*/
1615
 
7 7u83 1616
int
1617
predict_operator(void)
2 7u83 1618
{
1619
#if LANGUAGE_CPP
7 7u83 1620
	int t = crt_lex_token;
1621
	if (t == lex_operator) {
1622
		return(1);
1623
	}
2 7u83 1624
#endif
7 7u83 1625
	return(0);
2 7u83 1626
}
1627
 
1628
 
1629
/*
1630
    LOOK-AHEAD FOR ARRAY OPERATORS
1631
 
1632
    A look-ahead of one token is needed following 'operator new' and
1633
    'operator delete'.  A following '[' might be the start of an array index,
1634
    or it may introduce 'operator new []' or 'operator delete []'.  This
1635
    routine returns true in the latter case.
1636
*/
1637
 
7 7u83 1638
int
1639
predict_array(void)
2 7u83 1640
{
7 7u83 1641
	PPTOKEN *p;
1642
	NAMESPACE np;
1643
	int t = crt_lex_token;
1644
	if (t != lex_open_Hsquare_H1) {
1645
		return(0);
1646
	}
1647
	p = crt_token;
1648
	np = crt_lookup;
1649
	t = next_token();
1650
	crt_lookup = np;
1651
	crt_token = p;
1652
	if (t != lex_close_Hsquare_H1) {
1653
		return(0);
1654
	}
1655
	return(1);
2 7u83 1656
}
1657
 
1658
 
1659
/*
1660
    LOOK-AHEAD FOR TEMPLATE PARAMETERS
1661
 
1662
    A look-ahead is required to distinguish type template parameters from
1663
    non-type template parameters.
1664
*/
1665
 
7 7u83 1666
int
1667
predict_template(void)
2 7u83 1668
{
7 7u83 1669
	int d = 0;
1670
	int t = crt_lex_token;
1671
	if (t == lex_class || t == lex_typename) {
1672
		int have_id = 0;
1673
		PPTOKEN *p = crt_token;
1674
		int s = next_token();
1675
		switch (s) {
1676
		case lex_identifier:
1677
		case lex_type_Hname:
1678
		case lex_namespace_Hname:
1679
		case lex_statement_Hname:
1680
			s = next_token();
1681
			have_id = 1;
1682
			break;
1683
		}
1684
		if (s == lex_comma || s == lex_close_Htemplate) {
1685
			d = 1;
1686
		} else if (s == lex_assign) {
1687
			if (have_id) {
1688
				s = next_token();
1689
				crt_lex_token = s;
1690
				d = predict_typeid(2);
1691
			} else {
1692
				d = 1;
1693
			}
1694
		}
1695
		crt_lex_token = t;
1696
		crt_token = p;
1697
	} else if (t == lex_template) {
1698
		d = 1;
2 7u83 1699
	}
7 7u83 1700
	return(d);
2 7u83 1701
}