Parser.cpp 162 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "Parser.h"
#include "Token.h"
#include "Lexer.h"
#include "Control.h"
#include "AST.h"
#include "Literals.h"
55
#include "ObjectiveCTypeQualifiers.h"
56
#include "QtContextKeywords.h"
Roberto Raggi's avatar
Roberto Raggi committed
57
#include <cstdio> // for putchar
58

Roberto Raggi's avatar
Roberto Raggi committed
59
#define CPLUSPLUS_NO_DEBUG_RULE
60
#define MAX_EXPRESSION_DEPTH 100
Roberto Raggi's avatar
Roberto Raggi committed
61

62
63
using namespace CPlusPlus;

Roberto Raggi's avatar
Roberto Raggi committed
64
65
66
67
68
69
70
71
72
namespace {

class DebugRule {
    const char *name;
    static int depth;

public:
    DebugRule(const char *name)
        : name(name)
Roberto Raggi's avatar
Roberto Raggi committed
73
74
75
76
77
78
79
    {
        for (int i = 0; i < depth; ++i)
            putchar(' ');

        ++depth;
        printf("%s\n", name);
    }
Roberto Raggi's avatar
Roberto Raggi committed
80
81
82
83
84
85
86

    ~DebugRule()
    { --depth; }
};

int DebugRule::depth = 0;

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
inline bool lookAtAssignmentOperator(int tokenKind)
{
    switch (tokenKind) {
    case T_EQUAL:
    case T_AMPER_EQUAL:
    case T_CARET_EQUAL:
    case T_SLASH_EQUAL:
    case T_GREATER_GREATER_EQUAL:
    case T_LESS_LESS_EQUAL:
    case T_MINUS_EQUAL:
    case T_PERCENT_EQUAL:
    case T_PIPE_EQUAL:
    case T_PLUS_EQUAL:
    case T_STAR_EQUAL:
    case T_TILDE_EQUAL:
        return true;
    default:
        return false;
    } // switch
}

108
namespace Prec {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
enum {
    Unknown         = 0,
    Comma           = 1,
    Assignment      = 2,
    Conditional     = 3,
    LogicalOr       = 4,
    LogicalAnd      = 5,
    InclusiveOr     = 6,
    ExclusiveOr     = 7,
    And             = 8,
    Equality        = 9,
    Relational      = 10,
    Shift           = 11,
    Additive        = 12,
    Multiplicative  = 13,
    PointerToMember = 14
};
126
} // end of namespace Precedece
127

128
129
inline int precedence(int tokenKind, bool templateArguments)
{
130
131
    // ### this will/might need some tuning for C++0x
    // (see: [temp.names]p3)
132
133
134
    if (templateArguments && tokenKind == T_GREATER)
        return -1;

135
    if (lookAtAssignmentOperator(tokenKind))
136
        return Prec::Assignment;
137

138
    switch (tokenKind) {
139
140
141
142
143
144
145
    case T_COMMA:           return Prec::Comma;
    case T_QUESTION:        return Prec::Conditional;
    case T_PIPE_PIPE:       return Prec::LogicalOr;
    case T_AMPER_AMPER:     return Prec::LogicalAnd;
    case T_PIPE:            return Prec::InclusiveOr;
    case T_CARET:           return Prec::ExclusiveOr;
    case T_AMPER:           return Prec::And;
146
    case T_EQUAL_EQUAL:
147
    case T_EXCLAIM_EQUAL:   return Prec::Equality;
148
149
150
    case T_GREATER:
    case T_LESS:
    case T_LESS_EQUAL:
151
    case T_GREATER_EQUAL:   return Prec::Relational;
152
    case T_LESS_LESS:
153
    case T_GREATER_GREATER: return Prec::ExclusiveOr;
154
    case T_PLUS:
155
    case T_MINUS:           return Prec::Additive;
156
157
    case T_STAR:
    case T_SLASH:
158
    case T_PERCENT:         return Prec::Multiplicative;
159
    case T_ARROW_STAR:
160
161
    case T_DOT_STAR:        return Prec::PointerToMember;
    default:                return Prec::Unknown;
162
163
164
    }
}

165
inline bool isBinaryOperator(int tokenKind)
166
167
168
169
170
171
172
{ return precedence(tokenKind, false) != Prec::Unknown; }

inline bool isRightAssociative(int tokenKind)
{
    const int prec = precedence(tokenKind, false);
    return prec == Prec::Conditional || prec == Prec::Assignment;
}
173

Roberto Raggi's avatar
Roberto Raggi committed
174
175
176
177
178
179
180
181
} // end of anonymous namespace

#ifndef CPLUSPLUS_NO_DEBUG_RULE
#  define DEBUG_THIS_RULE() DebugRule __debug_rule__(__func__)
#else
#  define DEBUG_THIS_RULE() do {} while (0)
#endif

182
#define PARSE_EXPRESSION_WITH_OPERATOR_PRECEDENCE(node, minPrecedence) { \
183
184
185
186
    if (LA() == T_THROW) { \
        if (!parseThrowExpression(node)) \
            return false; \
    } else if (!parseCastExpression(node)) \
187
188
189
190
191
192
        return false; \
    \
    parseExpressionWithOperatorPrecedence(node, minPrecedence); \
    return true; \
}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class Parser::Rewind
{
    Parser *_parser;
    MemoryPool::State _state;

public:
    inline Rewind(Parser *parser)
        : _parser(parser) {}

    inline void operator()(unsigned tokenIndex)
    { rewind(tokenIndex); }

    inline void mark()
    { _state = _parser->_pool->state(); }

    inline void rewind(unsigned tokenIndex)
    {
        _parser->rewind(tokenIndex);

        if (_state.isValid())
            _parser->_pool->rewind(_state);
    }
};

con's avatar
con committed
217
218
219
220
221
222
223
Parser::Parser(TranslationUnit *unit)
    : _translationUnit(unit),
      _control(_translationUnit->control()),
      _pool(_translationUnit->memoryPool()),
      _tokenIndex(1),
      _templateArguments(0),
      _qtMocRunEnabled(false),
Roberto Raggi's avatar
Roberto Raggi committed
224
225
      _objCEnabled(false),
      _inFunctionBody(false),
Roberto Raggi's avatar
Roberto Raggi committed
226
      _inObjCImplementationContext(false),
227
      _expressionDepth(0)
con's avatar
con committed
228
229
230
231
232
233
234
235
236
237
238
{ }

Parser::~Parser()
{ }

bool Parser::qtMocRunEnabled() const
{ return _qtMocRunEnabled; }

void Parser::setQtMocRunEnabled(bool onoff)
{ _qtMocRunEnabled = onoff; }

Roberto Raggi's avatar
Roberto Raggi committed
239
240
241
242
243
244
bool Parser::objCEnabled() const
{ return _objCEnabled; }

void Parser::setObjCEnabled(bool onoff)
{ _objCEnabled = onoff; }

con's avatar
con committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
bool Parser::switchTemplateArguments(bool templateArguments)
{
    bool previousTemplateArguments = _templateArguments;
    _templateArguments = templateArguments;
    return previousTemplateArguments;
}

bool Parser::blockErrors(bool block)
{ return _translationUnit->blockErrors(block); }

bool Parser::skipUntil(int token)
{
    while (int tk = LA()) {
        if (tk == token)
            return true;

        consumeToken();
    }

    return false;
}

267
void Parser::skipUntilDeclaration()
con's avatar
con committed
268
{
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    for (; ; consumeToken()) {
        switch (LA()) {
        case T_EOF_SYMBOL:

        // names
        case T_IDENTIFIER:
        case T_COLON_COLON:
        case T_TILDE:
        case T_OPERATOR:

        // empty declaration
        case T_SEMICOLON:

        // member specification
        case T_USING:
        case T_TEMPLATE:
        case T_PUBLIC:
        case T_PROTECTED:
        case T_PRIVATE:
        case T_Q_SIGNALS:
        case T_Q_SLOTS:

        // declarations
        case T_ENUM:
        case T_NAMESPACE:
        case T_ASM:
        case T_EXPORT:
        case T_AT_CLASS:
        case T_AT_INTERFACE:
        case T_AT_PROTOCOL:
        case T_AT_IMPLEMENTATION:
        case T_AT_END:
            return;

        default:
            if (lookAtBuiltinTypeSpecifier() || lookAtClassKey() ||
                lookAtFunctionSpecifier() || lookAtStorageClassSpecifier())
                return;
        } // switch
308
    }
con's avatar
con committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
}

bool Parser::skipUntilStatement()
{
    while (int tk = LA()) {
        switch (tk) {
            case T_SEMICOLON:
            case T_LBRACE:
            case T_RBRACE:
            case T_CONST:
            case T_VOLATILE:
            case T_IDENTIFIER:
            case T_CASE:
            case T_DEFAULT:
            case T_IF:
            case T_SWITCH:
            case T_WHILE:
            case T_DO:
            case T_FOR:
            case T_BREAK:
            case T_CONTINUE:
            case T_RETURN:
            case T_GOTO:
            case T_TRY:
            case T_CATCH:
            case T_THROW:
            case T_CHAR:
            case T_WCHAR_T:
            case T_BOOL:
            case T_SHORT:
            case T_INT:
            case T_LONG:
            case T_SIGNED:
            case T_UNSIGNED:
            case T_FLOAT:
            case T_DOUBLE:
            case T_VOID:
            case T_CLASS:
            case T_STRUCT:
            case T_UNION:
            case T_ENUM:
            case T_COLON_COLON:
            case T_TEMPLATE:
            case T_USING:
                return true;

355
356
357
358
            case T_AT_SYNCHRONIZED:
                if (objCEnabled())
                    return true;

con's avatar
con committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
            default:
                consumeToken();
        }
    }

    return false;
}

bool Parser::skip(int l, int r)
{
    int count = 0;

    while (int tk = LA()) {
        if (tk == l)
            ++count;
        else if (tk == r)
            --count;
        else if (l != T_LBRACE && (tk == T_LBRACE ||
                                   tk == T_RBRACE ||
                                   tk == T_SEMICOLON))
            return false;

        if (count == 0)
            return true;

        consumeToken();
    }

    return false;
}

void Parser::match(int kind, unsigned *token)
{
    if (LA() == kind)
        *token = consumeToken();
    else {
        *token = 0;
        _translationUnit->error(_tokenIndex, "expected token `%s' got `%s'",
                                Token::name(kind), tok().spell());
    }
}

bool Parser::parseClassOrNamespaceName(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
403
    DEBUG_THIS_RULE();
con's avatar
con committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    if (LA() == T_IDENTIFIER) {
        unsigned identifier_token = cursor();

        if (LA(2) == T_LESS && parseTemplateId(node) && LA() == T_COLON_COLON)
            return true;

        rewind(identifier_token);

        if (LA(2) == T_COLON_COLON) {
            SimpleNameAST *ast = new (_pool) SimpleNameAST;
            ast->identifier_token = consumeToken();
            node = ast;
            return true;
        }
    } else if (LA() == T_TEMPLATE) {
        unsigned template_token = consumeToken();
        if (parseTemplateId(node))
            return true;
        rewind(template_token);
    }
    return false;
}

bool Parser::parseTemplateId(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
429
    DEBUG_THIS_RULE();
430
431
432

    const unsigned start = cursor();

con's avatar
con committed
433
434
435
436
437
    if (LA() == T_IDENTIFIER && LA(2) == T_LESS) {
        TemplateIdAST *ast = new (_pool) TemplateIdAST;
        ast->identifier_token = consumeToken();
        ast->less_token = consumeToken();
        if (LA() == T_GREATER || parseTemplateArgumentList(
Roberto Raggi's avatar
Roberto Raggi committed
438
                ast->template_argument_list)) {
con's avatar
con committed
439
440
441
442
443
444
445
            if (LA() == T_GREATER) {
                ast->greater_token = consumeToken();
                node = ast;
                return true;
            }
        }
    }
446
447
448

    rewind(start);

con's avatar
con committed
449
450
451
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
452
bool Parser::parseNestedNameSpecifier(NestedNameSpecifierListAST *&node,
con's avatar
con committed
453
454
                                      bool /*acceptTemplateId*/)
{
Roberto Raggi's avatar
Roberto Raggi committed
455
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
456
    NestedNameSpecifierListAST **nested_name_specifier = &node;
con's avatar
con committed
457
    NameAST *class_or_namespace_name = 0;
Roberto Raggi's avatar
Roberto Raggi committed
458
    if (parseClassOrNamespaceName(class_or_namespace_name) && LA() == T_COLON_COLON) {
con's avatar
con committed
459
460
        unsigned scope_token = consumeToken();

Roberto Raggi's avatar
Roberto Raggi committed
461
462
463
464
465
        NestedNameSpecifierAST *name = new (_pool) NestedNameSpecifierAST;
        name->class_or_namespace_name = class_or_namespace_name;
        name->scope_token = scope_token;

        *nested_name_specifier = new (_pool) NestedNameSpecifierListAST(name);
con's avatar
con committed
466
467
        nested_name_specifier = &(*nested_name_specifier)->next;

Roberto Raggi's avatar
Roberto Raggi committed
468
        while (parseClassOrNamespaceName(class_or_namespace_name) && LA() == T_COLON_COLON) {
con's avatar
con committed
469
            scope_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
470
471
472
473
474
475

            name = new (_pool) NestedNameSpecifierAST;
            name->class_or_namespace_name = class_or_namespace_name;
            name->scope_token = scope_token;

            *nested_name_specifier = new (_pool) NestedNameSpecifierListAST(name);
con's avatar
con committed
476
477
478
479
480
481
482
483
484
485
486
487
            nested_name_specifier = &(*nested_name_specifier)->next;
        }

        // ### ugly hack
        rewind(scope_token);
        consumeToken();
        return true;
    }

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
488
bool Parser::parseNestedNameSpecifierOpt(NestedNameSpecifierListAST *&name, bool acceptTemplateId)
con's avatar
con committed
489
{
Roberto Raggi's avatar
Roberto Raggi committed
490
    DEBUG_THIS_RULE();
con's avatar
con committed
491
492
493
494
495
496
497
498
    unsigned start = cursor();
    if (! parseNestedNameSpecifier(name, acceptTemplateId))
        rewind(start);
    return true;
}

bool Parser::parseName(NameAST *&node, bool acceptTemplateId)
{
Roberto Raggi's avatar
Roberto Raggi committed
499
    DEBUG_THIS_RULE();
con's avatar
con committed
500
501
502
503
    unsigned global_scope_token = 0;
    if (LA() == T_COLON_COLON)
        global_scope_token = consumeToken();

Roberto Raggi's avatar
Roberto Raggi committed
504
    NestedNameSpecifierListAST *nested_name_specifier = 0;
con's avatar
con committed
505
506
507
508
509
510
511
512
513
514
515
516
517
    parseNestedNameSpecifierOpt(nested_name_specifier,
                                /*acceptTemplateId=*/ true);

    NameAST *unqualified_name = 0;
    if (parseUnqualifiedName(unqualified_name,
                             /*acceptTemplateId=*/ acceptTemplateId || nested_name_specifier != 0)) {
        if (! global_scope_token && ! nested_name_specifier) {
            node = unqualified_name;
            return true;
        }

        QualifiedNameAST *ast = new (_pool) QualifiedNameAST;
        ast->global_scope_token = global_scope_token;
Roberto Raggi's avatar
Roberto Raggi committed
518
        ast->nested_name_specifier_list = nested_name_specifier;
con's avatar
con committed
519
520
521
522
523
524
525
526
527
528
        ast->unqualified_name = unqualified_name;
        node = ast;
        return true;
    }

    return false;
}

bool Parser::parseTranslationUnit(TranslationUnitAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
529
    DEBUG_THIS_RULE();
con's avatar
con committed
530
    TranslationUnitAST *ast = new (_pool) TranslationUnitAST;
Roberto Raggi's avatar
Roberto Raggi committed
531
    DeclarationListAST **decl = &ast->declaration_list;
con's avatar
con committed
532
533
534
535

    while (LA()) {
        unsigned start_declaration = cursor();

Roberto Raggi's avatar
Roberto Raggi committed
536
537
538
539
        DeclarationAST *declaration = 0;

        if (parseDeclaration(declaration)) {
            *decl = new (_pool) DeclarationListAST;
540
            (*decl)->value = declaration;
Roberto Raggi's avatar
Roberto Raggi committed
541
            decl = &(*decl)->next;
con's avatar
con committed
542
        } else {
543
            _translationUnit->error(start_declaration, "expected a declaration");
con's avatar
con committed
544
545
546
            rewind(start_declaration + 1);
            skipUntilDeclaration();
        }
Roberto Raggi's avatar
Roberto Raggi committed
547
548

        _templateArgumentList.clear();
con's avatar
con committed
549
550
551
552
553
554
555
556
    }

    node = ast;
    return true;
}

bool Parser::parseEmptyDeclaration(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
557
    DEBUG_THIS_RULE();
con's avatar
con committed
558
559
560
561
562
563
564
565
566
567
568
    if (LA() == T_SEMICOLON) {
        EmptyDeclarationAST *ast = new (_pool) EmptyDeclarationAST;
        ast->semicolon_token = consumeToken();
        node = ast;
        return true;
    }
    return false;
}

bool Parser::parseDeclaration(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
569
    DEBUG_THIS_RULE();
con's avatar
con committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    switch (LA()) {
    case T_SEMICOLON:
        return parseEmptyDeclaration(node);

    case T_NAMESPACE:
        return parseNamespace(node);

    case T_USING:
        return parseUsing(node);

    case T_ASM:
        return parseAsmDefinition(node);

    case T_TEMPLATE:
    case T_EXPORT:
        return parseTemplateDeclaration(node);

Roberto Raggi's avatar
Roberto Raggi committed
587
    // ObjcC++
588
    case T_AT_CLASS:
Erik Verbruggen's avatar
Erik Verbruggen committed
589
        return parseObjCClassForwardDeclaration(node);
590
591

    case T_AT_INTERFACE:
Roberto Raggi's avatar
Roberto Raggi committed
592
        return parseObjCInterface(node);
593
594

    case T_AT_PROTOCOL:
Roberto Raggi's avatar
Roberto Raggi committed
595
        return parseObjCProtocol(node);
596

597
598
599
    case T_AT_IMPLEMENTATION:
        return parseObjCImplementation(node);

Roberto Raggi's avatar
Roberto Raggi committed
600
    case T_AT_END:
Erik Verbruggen's avatar
Erik Verbruggen committed
601
602
603
604
        // TODO: should this be done here, or higher-up?
        _translationUnit->error(cursor(), "skip stray token `%s'", tok().spell());
        consumeToken();
        break;
605

Roberto Raggi's avatar
Roberto Raggi committed
606
607
608
    default: {
        if (_objCEnabled && LA() == T___ATTRIBUTE__) {
            const unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
609
            SpecifierListAST *attributes = 0, **attr = &attributes;
Roberto Raggi's avatar
Roberto Raggi committed
610
611
612
613
614
615
616
617
618
619
            while (parseAttributeSpecifier(*attr))
                attr = &(*attr)->next;
            if (LA() == T_AT_INTERFACE)
                return parseObjCInterface(node, attributes);
            else if (LA() == T_AT_PROTOCOL)
                return parseObjCProtocol(node, attributes);
            else if (LA() == T_AT_PROPERTY)
                return parseObjCPropertyDeclaration(node, attributes);
            rewind(start);
        }
620

con's avatar
con committed
621
622
623
624
625
626
        if (LA() == T_EXTERN && LA(2) == T_TEMPLATE)
            return parseTemplateDeclaration(node);
        else if (LA() == T_EXTERN && LA(2) == T_STRING_LITERAL)
            return parseLinkageSpecification(node);
        else
            return parseSimpleDeclaration(node);
Roberto Raggi's avatar
Roberto Raggi committed
627
628
    }   break; // default

con's avatar
con committed
629
    } // end switch
Roberto Raggi's avatar
Roberto Raggi committed
630
631

    return false;
con's avatar
con committed
632
633
634
635
}

bool Parser::parseLinkageSpecification(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
636
    DEBUG_THIS_RULE();
con's avatar
con committed
637
638
639
    if (LA() == T_EXTERN && LA(2) == T_STRING_LITERAL) {
        LinkageSpecificationAST *ast = new (_pool) LinkageSpecificationAST;
        ast->extern_token = consumeToken();
Roberto Raggi's avatar
cleanup    
Roberto Raggi committed
640
        ast->extern_type_token = consumeToken();
con's avatar
con committed
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655

        if (LA() == T_LBRACE)
            parseLinkageBody(ast->declaration);
        else
            parseDeclaration(ast->declaration);

        node = ast;
        return true;
    }

    return false;
}

bool Parser::parseLinkageBody(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
656
    DEBUG_THIS_RULE();
con's avatar
con committed
657
658
659
    if (LA() == T_LBRACE) {
        LinkageBodyAST *ast = new (_pool) LinkageBodyAST;
        ast->lbrace_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
660
        DeclarationListAST **declaration_ptr = &ast->declaration_list;
con's avatar
con committed
661
662
663
664
665
666

        while (int tk = LA()) {
            if (tk == T_RBRACE)
                break;

            unsigned start_declaration = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
667
668
669
            DeclarationAST *declaration = 0;
            if (parseDeclaration(declaration)) {
                *declaration_ptr = new (_pool) DeclarationListAST;
670
                (*declaration_ptr)->value = declaration;
Roberto Raggi's avatar
Roberto Raggi committed
671
                declaration_ptr = &(*declaration_ptr)->next;
con's avatar
con committed
672
            } else {
673
                _translationUnit->error(start_declaration, "expected a declaration");
con's avatar
con committed
674
675
676
                rewind(start_declaration + 1);
                skipUntilDeclaration();
            }
Roberto Raggi's avatar
Roberto Raggi committed
677
678

            _templateArgumentList.clear();
con's avatar
con committed
679
680
681
682
683
684
685
686
687
688
689
        }
        match(T_RBRACE, &ast->rbrace_token);
        node = ast;
        return true;
    }
    return false;
}

// ### rename parseNamespaceAliarOrDeclaration?
bool Parser::parseNamespace(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
690
    DEBUG_THIS_RULE();
con's avatar
con committed
691
692
693
694
695
696
697
698
699
    if (LA() != T_NAMESPACE)
        return false;

    unsigned namespace_token = consumeToken();

    if (LA() == T_IDENTIFIER && LA(2) == T_EQUAL) {
        NamespaceAliasDefinitionAST *ast =
                new (_pool) NamespaceAliasDefinitionAST;
        ast->namespace_token = namespace_token;
Roberto Raggi's avatar
cleanup    
Roberto Raggi committed
700
        ast->namespace_name_token = consumeToken();
con's avatar
con committed
701
702
703
704
705
706
707
708
709
710
711
        ast->equal_token = consumeToken();
        parseName(ast->name);
        match(T_SEMICOLON, &ast->semicolon_token);
        node = ast;
        return true;
    }

    NamespaceAST *ast = new (_pool) NamespaceAST;
    ast->namespace_token = namespace_token;
    if (LA() == T_IDENTIFIER)
        ast->identifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
712
    SpecifierListAST **attr_ptr = &ast->attribute_list;
con's avatar
con committed
713
714
715
716
717
718
719
720
721
722
723
724
    while (LA() == T___ATTRIBUTE__) {
        parseAttributeSpecifier(*attr_ptr);
        attr_ptr = &(*attr_ptr)->next;
    }
    if (LA() == T_LBRACE)
        parseLinkageBody(ast->linkage_body);
    node = ast;
    return true;
}

bool Parser::parseUsing(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
725
    DEBUG_THIS_RULE();
con's avatar
con committed
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
    if (LA() != T_USING)
        return false;

    if (LA(2) == T_NAMESPACE)
        return parseUsingDirective(node);

    UsingAST *ast = new (_pool) UsingAST;
    ast->using_token = consumeToken();

    if (LA() == T_TYPENAME)
        ast->typename_token = consumeToken();

    parseName(ast->name);
    match(T_SEMICOLON, &ast->semicolon_token);
    node = ast;
    return true;
}

bool Parser::parseUsingDirective(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
746
    DEBUG_THIS_RULE();
con's avatar
con committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    if (LA() == T_USING && LA(2) == T_NAMESPACE) {
        UsingDirectiveAST *ast = new (_pool) UsingDirectiveAST;
        ast->using_token = consumeToken();
        ast->namespace_token = consumeToken();
        if (! parseName(ast->name))
            _translationUnit->warning(cursor(), "expected `namespace name' before `%s'",
                                      tok().spell());
        match(T_SEMICOLON, &ast->semicolon_token);
        node = ast;
        return true;
    }
    return false;
}

bool Parser::parseConversionFunctionId(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
763
    DEBUG_THIS_RULE();
con's avatar
con committed
764
765
766
    if (LA() != T_OPERATOR)
        return false;
    unsigned operator_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
767
    SpecifierListAST *type_specifier = 0;
con's avatar
con committed
768
769
770
    if (! parseTypeSpecifier(type_specifier)) {
        return false;
    }
Roberto Raggi's avatar
Roberto Raggi committed
771
    PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
con's avatar
con committed
772
773
774
775
776
    while (parsePtrOperator(*ptr_operators_tail))
        ptr_operators_tail = &(*ptr_operators_tail)->next;

    ConversionFunctionIdAST *ast = new (_pool) ConversionFunctionIdAST;
    ast->operator_token = operator_token;
Roberto Raggi's avatar
Roberto Raggi committed
777
778
    ast->type_specifier_list = type_specifier;
    ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
779
780
781
782
783
784
    node = ast;
    return true;
}

bool Parser::parseOperatorFunctionId(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
785
    DEBUG_THIS_RULE();
con's avatar
con committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    if (LA() != T_OPERATOR)
        return false;
    unsigned operator_token = consumeToken();

    OperatorAST *op = 0;
    if (! parseOperator(op))
        return false;

    OperatorFunctionIdAST *ast = new (_pool) OperatorFunctionIdAST;
    ast->operator_token = operator_token;
    ast->op = op;
    node = ast;
    return true;
}

801
802
Parser::TemplateArgumentListEntry *Parser::templateArgumentListEntry(unsigned tokenIndex)
{
Roberto Raggi's avatar
Roberto Raggi committed
803
804
805
    std::map<unsigned, TemplateArgumentListEntry>::iterator it =_templateArgumentList.find(tokenIndex);
    if (it != _templateArgumentList.end())
        return &it->second;
806
807
808
809

    return 0;
}

con's avatar
con committed
810
811
bool Parser::parseTemplateArgumentList(TemplateArgumentListAST *&node)
{
812
813
    if (TemplateArgumentListEntry *entry = templateArgumentListEntry(cursor())) {
        rewind(entry->cursor);
Roberto Raggi's avatar
Roberto Raggi committed
814
815
        node = entry->ast;
        return entry->ast != 0;
816
817
818
819
    }

    unsigned start = cursor();

Roberto Raggi's avatar
Roberto Raggi committed
820
    DEBUG_THIS_RULE();
con's avatar
con committed
821
822
823
824
    TemplateArgumentListAST **template_argument_ptr = &node;
    ExpressionAST *template_argument = 0;
    if (parseTemplateArgument(template_argument)) {
        *template_argument_ptr = new (_pool) TemplateArgumentListAST;
825
        (*template_argument_ptr)->value = template_argument;
con's avatar
con committed
826
827
        template_argument_ptr = &(*template_argument_ptr)->next;
        while (LA() == T_COMMA) {
828
            consumeToken(); // consume T_COMMA
con's avatar
con committed
829
830
831

            if (parseTemplateArgument(template_argument)) {
                *template_argument_ptr = new (_pool) TemplateArgumentListAST;
832
                (*template_argument_ptr)->value = template_argument;
con's avatar
con committed
833
834
835
                template_argument_ptr = &(*template_argument_ptr)->next;
            }
        }
836

Roberto Raggi's avatar
Roberto Raggi committed
837
        _templateArgumentList.insert(std::make_pair(cursor(), TemplateArgumentListEntry(start, cursor(), node)));
con's avatar
con committed
838
839
        return true;
    }
840

Roberto Raggi's avatar
Roberto Raggi committed
841
    _templateArgumentList.insert(std::make_pair(cursor(), TemplateArgumentListEntry(start, cursor(), 0)));
842

con's avatar
con committed
843
844
845
846
847
    return false;
}

bool Parser::parseAsmDefinition(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
848
    DEBUG_THIS_RULE();
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
    if (LA() != T_ASM)
        return false;

    AsmDefinitionAST *ast = new (_pool) AsmDefinitionAST;
    ast->asm_token = consumeToken();

    if (LA() == T_VOLATILE)
        ast->volatile_token = consumeToken();

    match(T_LPAREN, &ast->lparen_token);
    unsigned string_literal_token = 0;
    match(T_STRING_LITERAL, &string_literal_token);
    while (LA() == T_STRING_LITERAL) {
        consumeToken();
    }
    if (LA() == T_COLON) {
        consumeToken(); // skip T_COLON
        parseAsmOperandList();
        if (LA() == T_COLON) {
            consumeToken();
            parseAsmOperandList();
            if (LA() == T_COLON) {
                consumeToken();
                parseAsmClobberList();
            }
        } else if (LA() == T_COLON_COLON) {
            consumeToken();
            parseAsmClobberList();
        }
    } else if (LA() == T_COLON_COLON) {
        consumeToken();
        parseAsmClobberList();
    }
    match(T_RPAREN, &ast->rparen_token);
    match(T_SEMICOLON, &ast->semicolon_token);
    node = ast;
    return true;
}

bool Parser::parseAsmOperandList()
{
Roberto Raggi's avatar
Roberto Raggi committed
890
    DEBUG_THIS_RULE();
891
892
893
    if (LA() != T_STRING_LITERAL)
        return true;

894
895
896
897
    if (parseAsmOperand()) {
        while (LA() == T_COMMA) {
            consumeToken();
            parseAsmOperand();
con's avatar
con committed
898
899
900
        }
        return true;
    }
901

con's avatar
con committed
902
903
904
    return false;
}

905
906
bool Parser::parseAsmOperand()
{
Roberto Raggi's avatar
Roberto Raggi committed
907
    DEBUG_THIS_RULE();
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
    unsigned string_literal_token = 0;
    match(T_STRING_LITERAL, &string_literal_token);

    if (LA() == T_LBRACKET) {
        /*unsigned lbracket_token = */ consumeToken();
        match(T_STRING_LITERAL, &string_literal_token);
        unsigned rbracket_token = 0;
        match(T_RBRACKET, &rbracket_token);
    }

    unsigned lparen_token = 0, rparen_token = 0;
    match(T_LPAREN, &lparen_token);
    ExpressionAST *expression = 0;
    parseExpression(expression);
    match(T_RPAREN, &rparen_token);
    return true;
}

bool Parser::parseAsmClobberList()
{
Roberto Raggi's avatar
Roberto Raggi committed
928
    DEBUG_THIS_RULE();
929
930
931
932
933
934
935
936
937
938
939
940
941
    if (LA() != T_STRING_LITERAL)
        return false;

    unsigned string_literal_token = consumeToken();

    while (LA() == T_COMMA) {
        consumeToken();
        match(T_STRING_LITERAL, &string_literal_token);
    }

    return true;
}

con's avatar
con committed
942
943
bool Parser::parseTemplateDeclaration(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
944
    DEBUG_THIS_RULE();
con's avatar
con committed
945
946
947
948
949
950
951
952
953
954
955
956
957
    if (! (LA(1) == T_TEMPLATE || ((LA(1) == T_EXPORT || LA(1) == T_EXTERN)
            && LA(2) == T_TEMPLATE)))
        return false;

    TemplateDeclarationAST *ast = new (_pool) TemplateDeclarationAST;

    if (LA() == T_EXPORT || LA() == T_EXPORT)
        ast->export_token = consumeToken();

    ast->template_token = consumeToken();

    if (LA() == T_LESS) {
        ast->less_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
958
        if (LA() == T_GREATER || parseTemplateParameterList(ast->template_parameter_list))
con's avatar
con committed
959
960
961
962
963
964
965
966
967
968
            match(T_GREATER, &ast->greater_token);
    }

    parseDeclaration(ast->declaration);
    node = ast;
    return true;
}

bool Parser::parseOperator(OperatorAST *&node) // ### FIXME
{
Roberto Raggi's avatar
Roberto Raggi committed
969
    DEBUG_THIS_RULE();
con's avatar
con committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
    OperatorAST *ast = new (_pool) OperatorAST;

    switch (LA()) {
    case T_NEW:
    case T_DELETE: {
        ast->op_token = consumeToken();
        if (LA() == T_LBRACKET) {
            ast->open_token = consumeToken();
            match(T_RBRACKET, &ast->close_token);
        }
    } break;

    case T_PLUS:
    case T_MINUS:
    case T_STAR:
    case T_SLASH:
    case T_PERCENT:
    case T_CARET:
    case T_AMPER:
    case T_PIPE:
    case T_TILDE:
    case T_EXCLAIM:
    case T_LESS:
    case T_GREATER:
    case T_COMMA:
    case T_AMPER_EQUAL:
    case T_CARET_EQUAL:
    case T_SLASH_EQUAL:
    case T_EQUAL:
    case T_EQUAL_EQUAL:
    case T_EXCLAIM_EQUAL:
    case T_GREATER_EQUAL:
    case T_GREATER_GREATER_EQUAL:
    case T_LESS_EQUAL:
    case T_LESS_LESS_EQUAL:
    case T_MINUS_EQUAL:
    case T_PERCENT_EQUAL:
    case T_PIPE_EQUAL:
    case T_PLUS_EQUAL:
    case T_STAR_EQUAL:
    case T_TILDE_EQUAL:
    case T_LESS_LESS:
    case T_GREATER_GREATER:
    case T_AMPER_AMPER:
    case T_PIPE_PIPE:
    case T_PLUS_PLUS:
    case T_MINUS_MINUS:
    case T_ARROW_STAR:
    case T_DOT_STAR:
    case T_ARROW:
        ast->op_token = consumeToken();
        break;

    default:
        if (LA() == T_LPAREN && LA(2) == T_RPAREN) {
            ast->op_token = ast->open_token = consumeToken();
            ast->close_token = consumeToken();
        } else if (LA() == T_LBRACKET && LA(2) == T_RBRACKET) {
            ast->op_token = ast->open_token = consumeToken();
            ast->close_token = consumeToken();
        } else {
            return false;
        }
    }

    node = ast;
    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
1039
bool Parser::parseCvQualifiers(SpecifierListAST *&node)
con's avatar
con committed
1040
{
Roberto Raggi's avatar
Roberto Raggi committed
1041
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
1042

con's avatar
con committed
1043
    unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
1044
1045

    SpecifierListAST **ast = &node;
con's avatar
con committed
1046
1047
1048
1049
1050
1051
1052
    while (*ast)
        ast = &(*ast)->next;

    while (int tk = LA()) {
        if (tk == T_CONST || tk == T_VOLATILE) {
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1053
            *ast = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1054
            ast = &(*ast)->next;
1055
1056
1057
        } else if(LA() == T___ATTRIBUTE__) {
            parseAttributeSpecifier(*ast);
            ast = &(*ast)->next;
con's avatar
con committed
1058
1059
1060
1061
1062
1063
1064
1065
        } else {
            break;
        }
    }

    return start != cursor();
}

Roberto Raggi's avatar
Roberto Raggi committed
1066
bool Parser::parsePtrOperator(PtrOperatorListAST *&node)
con's avatar
con committed
1067
{
Roberto Raggi's avatar
Roberto Raggi committed
1068
    DEBUG_THIS_RULE();
con's avatar
con committed
1069
1070
1071
    if (LA() == T_AMPER) {
        ReferenceAST *ast = new (_pool) ReferenceAST;
        ast->amp_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1072
        node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1073
1074
1075
1076
        return true;
    } else if (LA() == T_STAR) {
        PointerAST *ast = new (_pool) PointerAST;
        ast->star_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1077
        parseCvQualifiers(ast->cv_qualifier_list);
Roberto Raggi's avatar
Roberto Raggi committed
1078
        node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1079
1080
1081
1082
1083
1084
1085
1086
        return true;
    } else if (LA() == T_COLON_COLON || LA() == T_IDENTIFIER) {
        unsigned scope_or_identifier_token = cursor();

        unsigned global_scope_token = 0;
        if (LA() == T_COLON_COLON)
            global_scope_token = consumeToken();

Roberto Raggi's avatar
Roberto Raggi committed
1087
1088
        NestedNameSpecifierListAST *nested_name_specifiers = 0;
        bool has_nested_name_specifier = parseNestedNameSpecifier(nested_name_specifiers, true);
con's avatar
con committed
1089
1090
1091
        if (has_nested_name_specifier && LA() == T_STAR) {
            PointerToMemberAST *ast = new (_pool) PointerToMemberAST;
            ast->global_scope_token = global_scope_token;
Roberto Raggi's avatar
Roberto Raggi committed
1092
            ast->nested_name_specifier_list = nested_name_specifiers;
con's avatar
con committed
1093
            ast->star_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1094
            parseCvQualifiers(ast->cv_qualifier_list);
Roberto Raggi's avatar
Roberto Raggi committed
1095
            node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
            return true;
        }
        rewind(scope_or_identifier_token);
    }
    return false;
}

bool Parser::parseTemplateArgument(ExpressionAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1105
    DEBUG_THIS_RULE();
con's avatar
con committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
    unsigned start = cursor();
    if (parseTypeId(node) && (LA() == T_COMMA || LA() == T_GREATER))
        return true;

    rewind(start);
    bool previousTemplateArguments = switchTemplateArguments(true);
    bool parsed = parseLogicalOrExpression(node);
    (void) switchTemplateArguments(previousTemplateArguments);
    return parsed;
}

Roberto Raggi's avatar
Roberto Raggi committed
1117
bool Parser::parseDeclSpecifierSeq(SpecifierListAST *&decl_specifier_seq,
con's avatar
con committed
1118
1119
1120
                                   bool onlyTypeSpecifiers,
                                   bool simplified)
{
Roberto Raggi's avatar
Roberto Raggi committed
1121
    DEBUG_THIS_RULE();
con's avatar
con committed
1122
1123
    bool has_type_specifier = false;
    NameAST *named_type_specifier = 0;
Roberto Raggi's avatar
Roberto Raggi committed
1124
    SpecifierListAST **decl_specifier_seq_ptr = &decl_specifier_seq;
con's avatar
con committed
1125
1126
1127
1128
    for (;;) {
        if (lookAtCVQualifier()) {
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1129
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1130
1131
1132
1133
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
        } else if (! onlyTypeSpecifiers && lookAtStorageClassSpecifier()) {
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1134
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
        } else if (! named_type_specifier && lookAtBuiltinTypeSpecifier()) {
            parseBuiltinTypeSpecifier(*decl_specifier_seq_ptr);
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
            has_type_specifier = true;
        } else if (! has_type_specifier && (LA() == T_COLON_COLON ||
                                            LA() == T_IDENTIFIER)) {
            if (! parseName(named_type_specifier))
                return false;
            NamedTypeSpecifierAST *spec = new (_pool) NamedTypeSpecifierAST;
            spec->name = named_type_specifier;
Roberto Raggi's avatar
Roberto Raggi committed
1146
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
            has_type_specifier = true;
        } else if (! simplified && ! has_type_specifier && (LA() == T_TYPENAME ||
                                                            LA() == T_ENUM     ||
                                                            lookAtClassKey())) {
            unsigned startOfElaboratedTypeSpecifier = cursor();
            if (! parseElaboratedTypeSpecifier(*decl_specifier_seq_ptr)) {
                _translationUnit->error(startOfElaboratedTypeSpecifier,
                                        "expected an elaborated type specifier");
                break;
            }
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
            has_type_specifier = true;
        } else
            break;
    }

    return decl_specifier_seq != 0;
}

bool Parser::parseDeclaratorOrAbstractDeclarator(DeclaratorAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1169
    DEBUG_THIS_RULE();
con's avatar
con committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
    unsigned start = cursor();
    bool blocked = blockErrors(true);
    if (parseDeclarator(node)) {
        blockErrors(blocked);
        return true;
    }
    blockErrors(blocked);
    rewind(start);
    return parseAbstractDeclarator(node);
}

bool Parser::parseCoreDeclarator(DeclaratorAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1183
    DEBUG_THIS_RULE();
1184
    unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
1185
1186
    SpecifierListAST *attributes = 0;
    SpecifierListAST **attribute_ptr = &attributes;
1187
1188
1189
1190
1191
    while (LA() == T___ATTRIBUTE__) {
        parseAttributeSpecifier(*attribute_ptr);
        attribute_ptr = &(*attribute_ptr)->next;
    }

Roberto Raggi's avatar
Roberto Raggi committed
1192
    PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
con's avatar
con committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
    while (parsePtrOperator(*ptr_operators_tail))
        ptr_operators_tail = &(*ptr_operators_tail)->next;

    if (LA() == T_COLON_COLON || LA() == T_IDENTIFIER || LA() == T_TILDE
            || LA() == T_OPERATOR) {
        NameAST *name = 0;
        if (parseName(name)) {
            DeclaratorIdAST *declarator_id = new (_pool) DeclaratorIdAST;
            declarator_id->name = name;
            DeclaratorAST *ast = new (_pool) DeclaratorAST;
Roberto Raggi's avatar
Roberto Raggi committed
1203
1204
            ast->attribute_list = attributes;
            ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1205
1206
1207
1208
1209
            ast->core_declarator = declarator_id;
            node = ast;
            return true;
        }
    } else if (LA() == T_LPAREN) {
1210
1211
1212
        if (attributes)
            _translationUnit->warning(attributes->firstToken(), "unexpected attribtues");

con's avatar
con committed
1213
1214
1215
1216
1217
1218
1219
1220
        unsigned lparen_token = consumeToken();
        DeclaratorAST *declarator = 0;
        if (parseDeclarator(declarator) && LA() == T_RPAREN) {
            NestedDeclaratorAST *nested_declarator = new (_pool) NestedDeclaratorAST;
            nested_declarator->lparen_token = lparen_token;
            nested_declarator->declarator = declarator;
            nested_declarator->rparen_token = consumeToken();
            DeclaratorAST *ast = new (_pool) DeclaratorAST;
Roberto Raggi's avatar
Roberto Raggi committed
1221
            ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1222
1223
1224
1225
1226
            ast->core_declarator = nested_declarator;
            node = ast;
            return true;
        }
    }
1227
    rewind(start);
con's avatar
con committed
1228
1229
1230
    return false;
}

1231
bool Parser::parseDeclarator(DeclaratorAST *&node, bool stopAtCppInitializer)
con's avatar
con committed
1232
{
Roberto Raggi's avatar
Roberto Raggi committed
1233
    DEBUG_THIS_RULE();
con's avatar
con committed
1234
1235
1236
    if (! parseCoreDeclarator(node))
        return false;

Roberto Raggi's avatar
Roberto Raggi committed
1237
    PostfixDeclaratorListAST **postfix_ptr = &node->postfix_declarator_list;
con's avatar
con committed
1238
1239
1240
1241
1242

    for (;;) {
        unsigned startOfPostDeclarator = cursor();

        if (LA() == T_LPAREN) {
1243
1244
1245
1246
1247
            if (stopAtCppInitializer) {
                unsigned lparen_token = cursor();
                ExpressionAST *initializer = 0;

                bool blocked = blockErrors(true);
1248
                if (parseInitializer(initializer, &node->equals_token)) {
1249
1250
1251
                    if (NestedExpressionAST *expr = initializer->asNestedExpression()) {
                        if (expr->expression && expr->rparen_token && (LA() == T_COMMA || LA() == T_SEMICOLON)) {
                            rewind(lparen_token);
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264

                            // check for ambiguous declarators.

                            consumeToken();
                            ParameterDeclarationClauseAST *parameter_declaration_clause = 0;
                            if (parseParameterDeclarationClause(parameter_declaration_clause) && LA() == T_RPAREN) {
                                unsigned rparen_token = consumeToken();

                                FunctionDeclaratorAST *ast = new (_pool) FunctionDeclaratorAST;
                                ast->lparen_token = lparen_token;
                                ast->parameters = parameter_declaration_clause;
                                ast->as_cpp_initializer = initializer;
                                ast->rparen_token = rparen_token;
Roberto Raggi's avatar
Roberto Raggi committed
1265
                                *postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
1266
1267
1268
1269
1270
1271
1272
                                postfix_ptr = &(*postfix_ptr)->next;

                                blockErrors(blocked);
                                return true;
                            }


1273
                            blockErrors(blocked);
1274
                            rewind(lparen_token);
1275
1276
1277
1278
1279
1280
1281
1282
1283
                            return true;
                        }
                    }
                }

                blockErrors(blocked);
                rewind(lparen_token);
            }

con's avatar
con committed
1284
1285
1286
1287
1288
1289
1290
1291
1292
            FunctionDeclaratorAST *ast = new (_pool) FunctionDeclaratorAST;
            ast->lparen_token = consumeToken();
            parseParameterDeclarationClause(ast->parameters);
            if (LA() != T_RPAREN) {
                rewind(startOfPostDeclarator);
                break;
            }

            ast->rparen_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1293
            parseCvQualifiers(ast->cv_qualifier_list);
con's avatar
con committed
1294
            parseExceptionSpecification(ast->exception_specification);
Roberto Raggi's avatar
Roberto Raggi committed
1295
            *postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
con's avatar
con committed
1296
1297
1298
1299
1300
1301
1302
            postfix_ptr = &(*postfix_ptr)->next;
        } else if (LA() == T_LBRACKET) {
            ArrayDeclaratorAST *ast = new (_pool) ArrayDeclaratorAST;
            ast->lbracket_token = consumeToken();
            if (LA() == T_RBRACKET || parseConstantExpression(ast->expression)) {
                match(T_RBRACKET, &ast->rbracket_token);
            }
Roberto Raggi's avatar
Roberto Raggi committed
1303
            *postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
con's avatar
con committed
1304
1305
1306
1307
1308
            postfix_ptr = &(*postfix_ptr)->next;
        } else
            break;
    }

1309
1310
1311
1312
1313
1314
1315
1316
    if (LA() == T___ASM__ && LA(2) == T_LPAREN) { // ### store the asm specifier in the AST
        consumeToken(); // skip __asm__
        consumeToken(); // skip T_LPAREN

        if (skipUntil(T_RPAREN))
            consumeToken(); // skip T_RPAREN
    }

Roberto Raggi's avatar
Roberto Raggi committed
1317
    SpecifierListAST **spec_ptr = &node->post_attribute_list;
con's avatar
con committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
    while (LA() == T___ATTRIBUTE__) {
        parseAttributeSpecifier(*spec_ptr);
        spec_ptr = &(*spec_ptr)->next;
    }

    return true;
}

bool Parser::parseAbstractCoreDeclarator(DeclaratorAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1328
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
1329
1330

    PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
con's avatar
con committed
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
    while (parsePtrOperator(*ptr_operators_tail))
        ptr_operators_tail = &(*ptr_operators_tail)->next;

    unsigned after_ptr_operators = cursor();

    if (LA() == T_LPAREN) {
        unsigned lparen_token = consumeToken();
        DeclaratorAST *declarator = 0;
        if (parseAbstractDeclarator(declarator) && LA() == T_RPAREN) {
            NestedDeclaratorAST *nested_declarator = new (_pool) NestedDeclaratorAST;
            nested_declarator->lparen_token = lparen_token;
            nested_declarator->declarator = declarator;
            nested_declarator->rparen_token = consumeToken();
            DeclaratorAST *ast = new (_pool) DeclaratorAST;
Roberto Raggi's avatar
Roberto Raggi committed
1345
            ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1346
1347
1348
1349
1350
1351
1352
1353
1354
            ast->core_declarator = nested_declarator;
            node = ast;
            return true;
        }
    }

    rewind(after_ptr_operators);
    if (ptr_operators) {
        DeclaratorAST *ast = new (_pool) DeclaratorAST;
Roberto Raggi's avatar
Roberto Raggi committed
1355
        ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1356
1357
1358
1359
1360
1361
1362
1363
        node = ast;
    }

    return true;
}

bool Parser::parseAbstractDeclarator(DeclaratorAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1364
    DEBUG_THIS_RULE();
con's avatar
con committed
1365
1366
1367
    if (! parseAbstractCoreDeclarator(node))
        return false;

Roberto Raggi's avatar
Roberto Raggi committed
1368
    PostfixDeclaratorListAST *postfix_declarators = 0,
con's avatar
con committed
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
        **postfix_ptr = &postfix_declarators;

    for (;;) {
        if (LA() == T_LPAREN) {
            FunctionDeclaratorAST *ast = new (_pool) FunctionDeclaratorAST;
            ast->lparen_token = consumeToken();
            if (LA() == T_RPAREN || parseParameterDeclarationClause(ast->parameters)) {
                if (LA() == T_RPAREN)
                    ast->rparen_token = consumeToken();
            }
Roberto Raggi's avatar
Roberto Raggi committed
1379
            parseCvQualifiers(ast->cv_qualifier_list);
con's avatar
con committed
1380
            parseExceptionSpecification(ast->exception_specification);
Roberto Raggi's avatar
Roberto Raggi committed
1381
            *postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
con's avatar
con committed
1382
1383
1384
1385
1386
1387
1388
1389
            postfix_ptr = &(*postfix_ptr)->next;
        } else if (LA() == T_LBRACKET) {
            ArrayDeclaratorAST *ast = new (_pool) ArrayDeclaratorAST;
            ast->lbracket_token = consumeToken();
            if (LA() == T_RBRACKET || parseConstantExpression(ast->expression)) {
                if (LA() == T_RBRACKET)
                    ast->rbracket_token = consumeToken();
            }
Roberto Raggi's avatar
Roberto Raggi committed
1390
            *postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
con's avatar
con committed
1391
1392
1393
1394
1395
1396
1397
1398
1399
            postfix_ptr = &(*postfix_ptr)->next;
        } else
            break;
    }

    if (postfix_declarators) {
        if (! node)
            node = new (_pool) DeclaratorAST;

Roberto Raggi's avatar
Roberto Raggi committed
1400
        node->postfix_declarator_list = postfix_declarators;
con's avatar
con committed
1401
1402
1403
1404
1405
    }

    return true;
}

Roberto Raggi's avatar
Roberto Raggi committed
1406
bool Parser::parseEnumSpecifier(SpecifierListAST *&node)
con's avatar
con committed
1407
{
Roberto Raggi's avatar
Roberto Raggi committed
1408
    DEBUG_THIS_RULE();
con's avatar
con committed
1409
1410
1411
1412
1413
1414
1415
1416
1417
    if (LA() == T_ENUM) {
        unsigned enum_token = consumeToken();
        NameAST *name = 0;
        parseName(name);
        if (LA() == T_LBRACE) {
            EnumSpecifierAST *ast = new (_pool) EnumSpecifierAST;
            ast->enum_token = enum_token;
            ast->name = name;
            ast->lbrace_token = consumeToken();
1418
            unsigned comma_token = 0;
Roberto Raggi's avatar
Roberto Raggi committed
1419
            EnumeratorListAST **enumerator_ptr = &ast->enumerator_list;
con's avatar
con committed
1420
1421
1422
1423
1424
1425
1426
1427
1428
            while (int tk = LA()) {
                if (tk == T_RBRACE)
                    break;

                if (LA() != T_IDENTIFIER) {
                    _translationUnit->error(cursor(), "expected identifier before '%s'", tok().spell());
                    skipUntil(T_IDENTIFIER);
                }

1429
                if (parseEnumerator(*enumerator_ptr)) {
con's avatar
con committed
1430
                    enumerator_ptr = &(*enumerator_ptr)->next;
1431
                }
con's avatar
con committed
1432

1433
                if (LA() != T_RBRACE)
con's avatar
con committed
1434
1435
1436
                    match(T_COMMA, &comma_token);
            }
            match(T_RBRACE, &ast->rbrace_token);
Roberto Raggi's avatar
Roberto Raggi committed
1437
            node = new (_pool) SpecifierListAST(ast);
con's avatar
con committed
1438
1439
1440
1441
1442
1443
            return true;
        }
    }
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1444
bool Parser::parseTemplateParameterList(DeclarationListAST *&node)
con's avatar
con committed
1445
{
Roberto Raggi's avatar
Roberto Raggi committed
1446
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
1447
1448
1449
1450
    DeclarationListAST **template_parameter_ptr = &node;
    DeclarationAST *declaration = 0;
    if (parseTemplateParameter(declaration)) {
        *template_parameter_ptr = new (_pool) DeclarationListAST;
1451
        (*template_parameter_ptr)->value = declaration;
con's avatar
con committed
1452
        template_parameter_ptr = &(*template_parameter_ptr)->next;
Roberto Raggi's avatar
Roberto Raggi committed
1453

con's avatar
con committed
1454
        while (LA() == T_COMMA) {
1455
            consumeToken(); // XXX Store this token somewhere
con's avatar
con committed
1456

Roberto Raggi's avatar
Roberto Raggi committed
1457
1458
1459
            declaration = 0;
            if (parseTemplateParameter(declaration)) {
                *template_parameter_ptr = new (_pool) DeclarationListAST;