Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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