Parser.cpp 202 KB
Newer Older
con's avatar
con committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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"
27
#include "ObjectiveCTypeQualifiers.h"
28
#include "QtContextKeywords.h"
29
#include <string>
Roberto Raggi's avatar
Roberto Raggi committed
30
#include <cstdio> // for putchar
31
#if defined(_MSC_VER) && (_MSC_VER < 1800)
Tobias Hunger's avatar
Tobias Hunger committed
32
#    define va_copy(dst, src) ((dst) = (src))
33
34
#elif defined(__INTEL_COMPILER) && !defined(va_copy)
#    define va_copy __va_copy
Tobias Hunger's avatar
Tobias Hunger committed
35
36
#endif

Roberto Raggi's avatar
Roberto Raggi committed
37
#define CPLUSPLUS_NO_DEBUG_RULE
38
#define MAX_EXPRESSION_DEPTH 100
39
#define MAX_STATEMENT_DEPTH 100
Roberto Raggi's avatar
Roberto Raggi committed
40

41
42
using namespace CPlusPlus;

Roberto Raggi's avatar
Roberto Raggi committed
43
44
45
46
47
48
namespace {

class DebugRule {
    static int depth;

public:
49
    DebugRule(const char *name, const char *spell, unsigned idx, bool blocked)
Roberto Raggi's avatar
Roberto Raggi committed
50
    {
51
        for (int i = 0; i <= depth; ++i)
52
          fputc('-', stderr);
Roberto Raggi's avatar
Roberto Raggi committed
53
54

        ++depth;
55
        fprintf(stderr, " %s, ahead: '%s' (%u) - block-errors: %d\n", name, spell, idx, blocked);
Roberto Raggi's avatar
Roberto Raggi committed
56
    }
Roberto Raggi's avatar
Roberto Raggi committed
57
58
59
60
61
62
63

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

int DebugRule::depth = 0;

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
}

85
namespace Prec {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
};
103
} // namespace Precedece
104

105
106
inline int precedence(int tokenKind, bool templateArguments)
{
107
108
    // ### this will/might need some tuning for C++0x
    // (see: [temp.names]p3)
109
    if (templateArguments && (tokenKind == T_GREATER || tokenKind == T_GREATER_GREATER))
110
111
        return -1;

112
    if (lookAtAssignmentOperator(tokenKind))
113
        return Prec::Assignment;
114

115
    switch (tokenKind) {
116
117
118
119
120
121
122
    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;
123
    case T_EQUAL_EQUAL:
124
    case T_EXCLAIM_EQUAL:   return Prec::Equality;
125
126
127
    case T_GREATER:
    case T_LESS:
    case T_LESS_EQUAL:
128
    case T_GREATER_EQUAL:   return Prec::Relational;
129
    case T_LESS_LESS:
130
    case T_GREATER_GREATER: return Prec::ExclusiveOr;
131
    case T_PLUS:
132
    case T_MINUS:           return Prec::Additive;
133
134
    case T_STAR:
    case T_SLASH:
135
    case T_PERCENT:         return Prec::Multiplicative;
136
    case T_ARROW_STAR:
137
138
    case T_DOT_STAR:        return Prec::PointerToMember;
    default:                return Prec::Unknown;
139
140
141
    }
}

142
inline bool isBinaryOperator(int tokenKind)
143
144
145
146
147
148
149
{ return precedence(tokenKind, false) != Prec::Unknown; }

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

Roberto Raggi's avatar
Roberto Raggi committed
151
152
153
} // end of anonymous namespace

#ifndef CPLUSPLUS_NO_DEBUG_RULE
154
#  define DEBUG_THIS_RULE() DebugRule __debug_rule__(__func__, tok().spell(), cursor(), _translationUnit->blockErrors())
Roberto Raggi's avatar
Roberto Raggi committed
155
156
157
158
#else
#  define DEBUG_THIS_RULE() do {} while (0)
#endif

159
#define PARSE_EXPRESSION_WITH_OPERATOR_PRECEDENCE(node, minPrecedence) { \
160
161
162
163
    if (LA() == T_THROW) { \
        if (!parseThrowExpression(node)) \
            return false; \
    } else if (!parseCastExpression(node)) \
164
165
166
167
168
169
        return false; \
    \
    parseExpressionWithOperatorPrecedence(node, minPrecedence); \
    return true; \
}

con's avatar
con committed
170
171
Parser::Parser(TranslationUnit *unit)
    : _translationUnit(unit),
172
173
174
      _control(unit->control()),
      _pool(unit->memoryPool()),
      _languageFeatures(unit->languageFeatures()),
con's avatar
con committed
175
176
      _tokenIndex(1),
      _templateArguments(0),
Roberto Raggi's avatar
Roberto Raggi committed
177
      _inFunctionBody(false),
Roberto Raggi's avatar
Roberto Raggi committed
178
      _inObjCImplementationContext(false),
179
      _inExpressionStatement(false),
180
181
      _expressionDepth(0),
      _statementDepth(0)
con's avatar
con committed
182
183
184
185
186
187
188
189
190
191
192
193
{ }

Parser::~Parser()
{ }

bool Parser::switchTemplateArguments(bool templateArguments)
{
    bool previousTemplateArguments = _templateArguments;
    _templateArguments = templateArguments;
    return previousTemplateArguments;
}

194
195
196
197
198
bool Parser::maybeSplitGreaterGreaterToken(int n)
{
    return _translationUnit->maybeSplitGreaterGreaterToken(_tokenIndex + n - 1);
}

con's avatar
con committed
199
200
201
202
203
204
205
206
207
208
209
210
bool Parser::skipUntil(int token)
{
    while (int tk = LA()) {
        if (tk == token)
            return true;

        consumeToken();
    }

    return false;
}

211
void Parser::skipUntilDeclaration()
con's avatar
con committed
212
{
213
214
215
216
    for (; ; consumeToken()) {
        switch (LA()) {
        case T_EOF_SYMBOL:

217
218
219
        // end of a block
        case T_RBRACE:

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        // 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:
237
        case T_Q_PROPERTY:
238
        case T_Q_PRIVATE_PROPERTY:
239
240
        case T_Q_ENUMS:
        case T_Q_FLAGS:
241
        case T_Q_INTERFACES:
242
243
244
245
246

        // Qt function specifiers
        case T_Q_SIGNAL:
        case T_Q_SLOT:
        case T_Q_INVOKABLE:
247
248
249
250

        // declarations
        case T_ENUM:
        case T_NAMESPACE:
251
252
        case T_INLINE:
        case T_STATIC_ASSERT:
253
254
        case T_ASM:
        case T_EXPORT:
255
256
257
258
259
        case T_AT_CLASS:
        case T_AT_INTERFACE:
        case T_AT_PROTOCOL:
        case T_AT_IMPLEMENTATION:
        case T_AT_END:
260
261
262
263
264
265
266
            return;

        default:
            if (lookAtBuiltinTypeSpecifier() || lookAtClassKey() ||
                lookAtFunctionSpecifier() || lookAtStorageClassSpecifier())
                return;
        } // switch
267
    }
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
}

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:
295
296
            case T_CHAR16_T:
            case T_CHAR32_T:
con's avatar
con committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
            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;

316
            case T_AT_TRY:
317
            case T_AT_SYNCHRONIZED:
318
            case T_AT_THROW:
319
                if (_languageFeatures.objCEnabled)
320
                    return true;
321

con's avatar
con committed
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
            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;
}

353
354
355
356
357
358
359
360
361
362
363
364
int Parser::find(int token, int stopAt)
{
    for (int i = 1; ; ++i) {
        const int tk = LA(i);
        if (!tk || tk == stopAt)
            return 0;
        if (tk == token)
            return i;
    }
    return 0;
}

con's avatar
con committed
365
366
367
368
369
370
void Parser::match(int kind, unsigned *token)
{
    if (LA() == kind)
        *token = consumeToken();
    else {
        *token = 0;
371
        error(_tokenIndex, "expected token `%s' got `%s'",
Orgad Shaneh's avatar
Orgad Shaneh committed
372
              Token::name(kind), tok().spell());
con's avatar
con committed
373
374
375
376
377
    }
}

bool Parser::parseClassOrNamespaceName(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
378
    DEBUG_THIS_RULE();
379
    if (LA() == T_IDENTIFIER && (LA(2) == T_COLON_COLON || LA(2) == T_LESS)) {
con's avatar
con committed
380
381
        unsigned identifier_token = cursor();

382
383
384
385
386
387
388
389
        if (LA(2) == T_LESS) {
            bool blocked = blockErrors(true);
            if (parseTemplateId(node) && LA() == T_COLON_COLON) {
                blockErrors(blocked);
                return true;
            }
            blockErrors(blocked);
        }
con's avatar
con committed
390
391
392
393
394
395
396
397
398
399
400

        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();
401
        if (parseTemplateId(node, template_token) && LA() == T_COLON_COLON)
con's avatar
con committed
402
403
404
405
406
407
            return true;
        rewind(template_token);
    }
    return false;
}

408
bool Parser::parseTemplateId(NameAST *&node, unsigned template_token)
con's avatar
con committed
409
{
Roberto Raggi's avatar
Roberto Raggi committed
410
    DEBUG_THIS_RULE();
411
412
413

    const unsigned start = cursor();

con's avatar
con committed
414
415
    if (LA() == T_IDENTIFIER && LA(2) == T_LESS) {
        TemplateIdAST *ast = new (_pool) TemplateIdAST;
416
        ast->template_token = template_token;
con's avatar
con committed
417
418
        ast->identifier_token = consumeToken();
        ast->less_token = consumeToken();
419
        if (maybeSplitGreaterGreaterToken() || LA() == T_GREATER || parseTemplateArgumentList(
Roberto Raggi's avatar
Roberto Raggi committed
420
                ast->template_argument_list)) {
421
            if (maybeSplitGreaterGreaterToken() || LA() == T_GREATER) {
con's avatar
con committed
422
423
424
425
426
427
                ast->greater_token = consumeToken();
                node = ast;
                return true;
            }
        }
    }
428
429
430

    rewind(start);

con's avatar
con committed
431
432
433
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
434
bool Parser::parseNestedNameSpecifier(NestedNameSpecifierListAST *&node,
con's avatar
con committed
435
436
                                      bool /*acceptTemplateId*/)
{
Roberto Raggi's avatar
Roberto Raggi committed
437
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
438
    NestedNameSpecifierListAST **nested_name_specifier = &node;
con's avatar
con committed
439
    NameAST *class_or_namespace_name = 0;
Roberto Raggi's avatar
Roberto Raggi committed
440
    if (parseClassOrNamespaceName(class_or_namespace_name) && LA() == T_COLON_COLON) {
con's avatar
con committed
441
442
        unsigned scope_token = consumeToken();

Roberto Raggi's avatar
Roberto Raggi committed
443
444
445
446
447
        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
448
449
        nested_name_specifier = &(*nested_name_specifier)->next;

Roberto Raggi's avatar
Roberto Raggi committed
450
        while (parseClassOrNamespaceName(class_or_namespace_name) && LA() == T_COLON_COLON) {
con's avatar
con committed
451
            scope_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
452
453
454
455
456
457

            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
458
459
460
461
462
463
464
465
466
467
468
469
            nested_name_specifier = &(*nested_name_specifier)->next;
        }

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

    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
470
bool Parser::parseNestedNameSpecifierOpt(NestedNameSpecifierListAST *&name, bool acceptTemplateId)
con's avatar
con committed
471
{
Roberto Raggi's avatar
Roberto Raggi committed
472
    DEBUG_THIS_RULE();
con's avatar
con committed
473
474
475
476
477
478
479
480
    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
481
    DEBUG_THIS_RULE();
482
    unsigned global_scope_token = 0;
483
484
485
486
487
488
489
490
491
492
493
494

    switch (LA()) {
    case T_COLON_COLON:
    case T_IDENTIFIER:
    case T_TILDE: // destructor-name-id
    case T_OPERATOR: // operator-name-id
    case T_TEMPLATE: // template introduced template-id
      break;
    default:
      return false;
    }

con's avatar
con committed
495
496
497
    if (LA() == T_COLON_COLON)
        global_scope_token = consumeToken();

Roberto Raggi's avatar
Roberto Raggi committed
498
    NestedNameSpecifierListAST *nested_name_specifier = 0;
con's avatar
con committed
499
500
501
502
503
504
505
506
507
508
509
510
511
    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
512
        ast->nested_name_specifier_list = nested_name_specifier;
con's avatar
con committed
513
514
515
516
517
518
519
520
521
522
        ast->unqualified_name = unqualified_name;
        node = ast;
        return true;
    }

    return false;
}

bool Parser::parseTranslationUnit(TranslationUnitAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
523
    DEBUG_THIS_RULE();
con's avatar
con committed
524
    TranslationUnitAST *ast = new (_pool) TranslationUnitAST;
Roberto Raggi's avatar
Roberto Raggi committed
525
    DeclarationListAST **decl = &ast->declaration_list;
con's avatar
con committed
526
527
528
529

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

Roberto Raggi's avatar
Roberto Raggi committed
530
531
532
533
        DeclarationAST *declaration = 0;

        if (parseDeclaration(declaration)) {
            *decl = new (_pool) DeclarationListAST;
534
            (*decl)->value = declaration;
Roberto Raggi's avatar
Roberto Raggi committed
535
            decl = &(*decl)->next;
con's avatar
con committed
536
        } else {
537
            error(start_declaration, "expected a declaration");
con's avatar
con committed
538
539
540
            rewind(start_declaration + 1);
            skipUntilDeclaration();
        }
Roberto Raggi's avatar
Roberto Raggi committed
541

542
543

        if (TopLevelDeclarationProcessor *processor = _control->topLevelDeclarationProcessor()) {
544
            if (!processor->processDeclaration(declaration))
545
546
547
                break;
        }

Roberto Raggi's avatar
Roberto Raggi committed
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
    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);

583

con's avatar
con committed
584
585
586
587
    case T_TEMPLATE:
    case T_EXPORT:
        return parseTemplateDeclaration(node);

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

592
593
    case T_AT_INTERFACE:
        return parseObjCInterface(node);
594

595
596
    case T_AT_PROTOCOL:
        return parseObjCProtocol(node);
597

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

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    case T_Q_DECLARE_INTERFACE:
    {
        consumeToken();
        unsigned lparen_token = 0;
        match(T_LPAREN, &lparen_token);
        NameAST *name = 0;
        parseName(name);
        unsigned comma_token = 0;
        match(T_COMMA, &comma_token);
        unsigned string_literal = 0;
        match(T_STRING_LITERAL, &string_literal);
        unsigned rparen_token = 0;
        match(T_RPAREN, &rparen_token);
    }   return true;

616
617
    case T_AT_END:
        // TODO: should this be done here, or higher-up?
618
        error(cursor(), "skip stray token `%s'", tok().spell());
619
620
        consumeToken();
        break;
621

622
    case T_INLINE:
623
        if (_languageFeatures.cxx11Enabled && LA(2) == T_NAMESPACE)
624
            return parseNamespace(node);
625
626
627
        return parseSimpleDeclaration(node);

    case T_STATIC_ASSERT:
628
        if (_languageFeatures.cxx11Enabled)
629
630
            return parseStaticAssertDeclaration(node);
        return parseSimpleDeclaration(node);
631

Roberto Raggi's avatar
Roberto Raggi committed
632
    default: {
633
        if (_languageFeatures.objCEnabled && LA() == T___ATTRIBUTE__) {
Roberto Raggi's avatar
Roberto Raggi committed
634
            const unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
635
            SpecifierListAST *attributes = 0, **attr = &attributes;
Roberto Raggi's avatar
Roberto Raggi committed
636
637
            while (parseAttributeSpecifier(*attr))
                attr = &(*attr)->next;
638
639
640
641
642
643
            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);
Roberto Raggi's avatar
Roberto Raggi committed
644
645
            rewind(start);
        }
646

con's avatar
con committed
647
648
649
650
651
652
        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
653
654
    }   break; // default

con's avatar
con committed
655
    } // end switch
Roberto Raggi's avatar
Roberto Raggi committed
656
657

    return false;
con's avatar
con committed
658
659
660
661
}

bool Parser::parseLinkageSpecification(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
662
    DEBUG_THIS_RULE();
con's avatar
con committed
663
664
665
    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
666
        ast->extern_type_token = consumeToken();
con's avatar
con committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681

        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
682
    DEBUG_THIS_RULE();
con's avatar
con committed
683
684
685
    if (LA() == T_LBRACE) {
        LinkageBodyAST *ast = new (_pool) LinkageBodyAST;
        ast->lbrace_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
686
        DeclarationListAST **declaration_ptr = &ast->declaration_list;
con's avatar
con committed
687
688
689
690
691
692

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

            unsigned start_declaration = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
693
694
695
            DeclarationAST *declaration = 0;
            if (parseDeclaration(declaration)) {
                *declaration_ptr = new (_pool) DeclarationListAST;
696
                (*declaration_ptr)->value = declaration;
Roberto Raggi's avatar
Roberto Raggi committed
697
                declaration_ptr = &(*declaration_ptr)->next;
con's avatar
con committed
698
            } else {
699
                error(start_declaration, "expected a declaration");
con's avatar
con committed
700
701
702
                rewind(start_declaration + 1);
                skipUntilDeclaration();
            }
Roberto Raggi's avatar
Roberto Raggi committed
703
704

            _templateArgumentList.clear();
con's avatar
con committed
705
706
707
708
709
710
711
712
        }
        match(T_RBRACE, &ast->rbrace_token);
        node = ast;
        return true;
    }
    return false;
}

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
bool Parser::parseStaticAssertDeclaration(DeclarationAST *&node)
{
    DEBUG_THIS_RULE();
    if (LA() != T_STATIC_ASSERT)
        return false;

    StaticAssertDeclarationAST *ast = new (_pool) StaticAssertDeclarationAST;
    ast->static_assert_token = consumeToken();
    match(T_LPAREN, &ast->lparen_token);
    parseConstantExpression(ast->expression);
    match(T_COMMA, &ast->comma_token);
    parseStringLiteral(ast->string_literal);
    match(T_RPAREN, &ast->rparen_token);
    match(T_SEMICOLON, &ast->semicolon_token);

    node = ast;
    return true;
}

con's avatar
con committed
732
733
734
// ### rename parseNamespaceAliarOrDeclaration?
bool Parser::parseNamespace(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
735
    DEBUG_THIS_RULE();
736
    if (LA() != T_NAMESPACE && !(_languageFeatures.cxx11Enabled && LA() == T_INLINE && LA(2) == T_NAMESPACE))
con's avatar
con committed
737
738
        return false;

739
    unsigned inline_token = 0;
740
    if (_languageFeatures.cxx11Enabled && LA() == T_INLINE)
741
742
        inline_token = consumeToken();

con's avatar
con committed
743
744
745
    unsigned namespace_token = consumeToken();

    if (LA() == T_IDENTIFIER && LA(2) == T_EQUAL) {
746
747
        if (inline_token)
            warning(inline_token, "namespace alias cannot be inline");
con's avatar
con committed
748
749
750
        NamespaceAliasDefinitionAST *ast =
                new (_pool) NamespaceAliasDefinitionAST;
        ast->namespace_token = namespace_token;
Roberto Raggi's avatar
cleanup    
Roberto Raggi committed
751
        ast->namespace_name_token = consumeToken();
con's avatar
con committed
752
753
754
755
756
757
758
759
        ast->equal_token = consumeToken();
        parseName(ast->name);
        match(T_SEMICOLON, &ast->semicolon_token);
        node = ast;
        return true;
    }

    NamespaceAST *ast = new (_pool) NamespaceAST;
760
    ast->inline_token = inline_token;
con's avatar
con committed
761
762
763
    ast->namespace_token = namespace_token;
    if (LA() == T_IDENTIFIER)
        ast->identifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
764
    SpecifierListAST **attr_ptr = &ast->attribute_list;
con's avatar
con committed
765
766
767
768
    while (LA() == T___ATTRIBUTE__) {
        parseAttributeSpecifier(*attr_ptr);
        attr_ptr = &(*attr_ptr)->next;
    }
769
    if (LA() == T_LBRACE) {
con's avatar
con committed
770
        parseLinkageBody(ast->linkage_body);
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
    } else { // attempt to do error recovery
        unsigned pos = cursor();
        for (;LA() != T_EOF_SYMBOL; consumeToken()) {
            switch (LA()) {
            case T_IDENTIFIER:
            case T_POUND:
            case T_POUND_POUND:
            case T___ATTRIBUTE__:
            case T_LPAREN:
            case T_RPAREN:
            case T_DEFAULT:
            case T_PUBLIC:
            case T_PRIVATE:
            case T_PROTECTED:
                continue;
            }
            if (tok().isLiteral())
                continue;
            break;
        }
        if (LA() == T_LBRACE && parseLinkageBody(ast->linkage_body))
            warning(pos, "expected '{' before '%s'", _translationUnit->tokenAt(pos).spell());
        else
            rewind(pos);
    }
con's avatar
con committed
796
797
798
799
800
801
    node = ast;
    return true;
}

bool Parser::parseUsing(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
802
    DEBUG_THIS_RULE();
con's avatar
con committed
803
804
805
806
807
808
    if (LA() != T_USING)
        return false;

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

809
    if (_languageFeatures.cxx11Enabled && LA(2) == T_IDENTIFIER && parseAliasDeclaration(node))
810
811
        return true;

con's avatar
con committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
    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
826
    DEBUG_THIS_RULE();
con's avatar
con committed
827
828
829
830
831
    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))
832
833
            warning(cursor(), "expected `namespace name' before `%s'",
                    tok().spell());
con's avatar
con committed
834
835
836
837
838
839
840
        match(T_SEMICOLON, &ast->semicolon_token);
        node = ast;
        return true;
    }
    return false;
}

841
842
843
844
845
846
847
848
849
850
851
852
// alias-declaration = 'using' identifier attribute-specifier-seq(opt) '=' type-id ';'
bool Parser::parseAliasDeclaration(DeclarationAST *&node)
{
    DEBUG_THIS_RULE();
    if (LA() != T_USING || LA(2) != T_IDENTIFIER)
        return false;

    if (!find(T_EQUAL, T_SEMICOLON))
        return false;

    AliasDeclarationAST *alias = new (_pool) AliasDeclarationAST;
    alias->using_token = consumeToken();
853
854
855
    SimpleNameAST *name = new (_pool) SimpleNameAST;
    name->identifier_token = consumeToken();
    alias->name = name;
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873

    // ### attributes!
    while (LA() != T_EQUAL)
        consumeToken();

    alias->equal_token = consumeToken();

    ExpressionAST *expr = 0;
    parseTypeId(expr);
    if (expr)
        alias->typeId = expr->asTypeId();

    match(T_SEMICOLON, &alias->semicolon_token);

    node = alias;
    return true;
}

con's avatar
con committed
874
875
bool Parser::parseConversionFunctionId(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
876
    DEBUG_THIS_RULE();
con's avatar
con committed
877
878
879
    if (LA() != T_OPERATOR)
        return false;
    unsigned operator_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
880
    SpecifierListAST *type_specifier = 0;
881
    if (! parseTypeSpecifier(type_specifier))
con's avatar
con committed
882
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
883
    PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
con's avatar
con committed
884
885
886
887
888
    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
889
890
    ast->type_specifier_list = type_specifier;
    ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
891
892
893
894
895
896
    node = ast;
    return true;
}

bool Parser::parseOperatorFunctionId(NameAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
897
    DEBUG_THIS_RULE();
con's avatar
con committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
    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;
}

913
914
Parser::TemplateArgumentListEntry *Parser::templateArgumentListEntry(unsigned tokenIndex)
{
Roberto Raggi's avatar
Roberto Raggi committed
915
916
917
    std::map<unsigned, TemplateArgumentListEntry>::iterator it =_templateArgumentList.find(tokenIndex);
    if (it != _templateArgumentList.end())
        return &it->second;
918
919
920
921

    return 0;
}

Roberto Raggi's avatar
Roberto Raggi committed
922
bool Parser::parseTemplateArgumentList(ExpressionListAST *&node)
con's avatar
con committed
923
{
924
925
    DEBUG_THIS_RULE();

926
927
    if (TemplateArgumentListEntry *entry = templateArgumentListEntry(cursor())) {
        rewind(entry->cursor);
Roberto Raggi's avatar
Roberto Raggi committed
928
929
        node = entry->ast;
        return entry->ast != 0;
930
931
932
933
    }

    unsigned start = cursor();

Roberto Raggi's avatar
Roberto Raggi committed
934
    ExpressionListAST **template_argument_ptr = &node;
con's avatar
con committed
935
    ExpressionAST *template_argument = 0;
936
    const bool cxx11Enabled = _languageFeatures.cxx11Enabled;
con's avatar
con committed
937
    if (parseTemplateArgument(template_argument)) {
Roberto Raggi's avatar
Roberto Raggi committed
938
        *template_argument_ptr = new (_pool) ExpressionListAST;
939
        (*template_argument_ptr)->value = template_argument;
con's avatar
con committed
940
        template_argument_ptr = &(*template_argument_ptr)->next;
941

942
        if (cxx11Enabled && LA() == T_DOT_DOT_DOT)
943
944
            consumeToken(); // ### store this token in the AST

con's avatar
con committed
945
        while (LA() == T_COMMA) {
946
            consumeToken(); // consume T_COMMA
con's avatar
con committed
947
948

            if (parseTemplateArgument(template_argument)) {
Roberto Raggi's avatar
Roberto Raggi committed
949
                *template_argument_ptr = new (_pool) ExpressionListAST;
950
                (*template_argument_ptr)->value = template_argument;
con's avatar
con committed
951
                template_argument_ptr = &(*template_argument_ptr)->next;
952

953
                if (cxx11Enabled && LA() == T_DOT_DOT_DOT)
954
                    consumeToken(); // ### store this token in the AST
con's avatar
con committed
955
956
            }
        }
957

958
959
        if (_pool != _translationUnit->memoryPool()) {
            MemoryPool *pool = _translationUnit->memoryPool();
Roberto Raggi's avatar
Roberto Raggi committed
960
961
            ExpressionListAST *template_argument_list = node;
            for (ExpressionListAST *iter = template_argument_list, **ast_iter = &node;
962
                 iter; iter = iter->next, ast_iter = &(*ast_iter)->next)
Roberto Raggi's avatar
Roberto Raggi committed
963
                *ast_iter = new (pool) ExpressionListAST((iter->value) ? iter->value->clone(pool) : 0);
964
965
        }

966
        _templateArgumentList.insert(std::make_pair(start, TemplateArgumentListEntry(start, cursor(), node)));
con's avatar
con committed
967
968
        return true;
    }
969

970
    _templateArgumentList.insert(std::make_pair(start, TemplateArgumentListEntry(start, cursor(), 0)));
con's avatar
con committed
971
972
973
974
975
    return false;
}

bool Parser::parseAsmDefinition(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
976
    DEBUG_THIS_RULE();
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
    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();
1008
        parseAsmOperandList();
1009

1010
1011
1012
1013
        if (LA() == T_COLON) {
          consumeToken();
          parseAsmClobberList();
        }
1014
1015
1016
1017
1018
1019
1020
1021
1022
    }
    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
1023
    DEBUG_THIS_RULE();
1024
1025
1026
    if (LA() != T_STRING_LITERAL)
        return true;

1027
1028
1029
1030
    if (parseAsmOperand()) {
        while (LA() == T_COMMA) {
            consumeToken();
            parseAsmOperand();
con's avatar
con committed
1031
1032
1033
        }
        return true;
    }
1034

con's avatar
con committed
1035
1036
1037
    return false;
}

1038
1039
bool Parser::parseAsmOperand()
{
Roberto Raggi's avatar
Roberto Raggi committed
1040
    DEBUG_THIS_RULE();
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
    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
1061
    DEBUG_THIS_RULE();
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    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
1075
1076
bool Parser::parseTemplateDeclaration(DeclarationAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1077
    DEBUG_THIS_RULE();
con's avatar
con committed
1078
1079
1080
1081
1082
1083
    if (! (LA(1) == T_TEMPLATE || ((LA(1) == T_EXPORT || LA(1) == T_EXTERN)
            && LA(2) == T_TEMPLATE)))
        return false;

    TemplateDeclarationAST *ast = new (_pool) TemplateDeclarationAST;

1084
    if (LA() == T_EXPORT || LA() == T_EXTERN)
con's avatar
con committed
1085
1086
1087
1088
1089
1090
        ast->export_token = consumeToken();

    ast->template_token = consumeToken();

    if (LA() == T_LESS) {
        ast->less_token = consumeToken();
1091
        if (maybeSplitGreaterGreaterToken() || LA() == T_GREATER || parseTemplateParameterList(ast->template_parameter_list))
con's avatar
con committed
1092
1093
1094
            match(T_GREATER, &ast->greater_token);
    }

1095
    while (LA()) {
1096
1097
1098
1099
1100
1101
        unsigned start_declaration = cursor();

        ast->declaration = 0;
        if (parseDeclaration(ast->declaration))
            break;

1102
        error(start_declaration, "expected a declaration");
1103
1104
        rewind(start_declaration + 1);
        skipUntilDeclaration();
1105
    }
1106

con's avatar
con committed
1107
1108
1109
1110
1111
1112
    node = ast;
    return true;
}

bool Parser::parseOperator(OperatorAST *&node) // ### FIXME
{
Roberto Raggi's avatar
Roberto Raggi committed
1113
    DEBUG_THIS_RULE();
con's avatar
con committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
    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
1183
bool Parser::parseCvQualifiers(SpecifierListAST *&node)
con's avatar
con committed
1184
{
Roberto Raggi's avatar
Roberto Raggi committed
1185
    DEBUG_THIS_RULE();
Roberto Raggi's avatar
Roberto Raggi committed
1186

con's avatar
con committed
1187
    unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
1188
1189

    SpecifierListAST **ast = &node;
con's avatar
con committed
1190
1191
1192
1193
1194
1195
1196
    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
1197
            *ast = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1198
            ast = &(*ast)->next;
1199
        } else if (LA() == T___ATTRIBUTE__) {
1200
1201
            parseAttributeSpecifier(*ast);
            ast = &(*ast)->next;
con's avatar
con committed
1202
1203
1204
1205
1206
1207
1208
1209
        } else {
            break;
        }
    }

    return start != cursor();
}

1210
1211
1212
1213
bool Parser::parseRefQualifier(unsigned &ref_qualifier)
{
    DEBUG_THIS_RULE();

1214
    if (!_languageFeatures.cxx11Enabled)
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
        return false;

    if (LA() == T_AMPER || LA() == T_AMPER_AMPER) {
        ref_qualifier = consumeToken();
        return true;
    }

    return false;
}

1225
1226
1227
1228
1229
1230
1231
/**
 * \brief Handles override and final from C++ 2011, they are pseudo keywords and has special meaning only in function declaration
 */
bool Parser::parseOverrideFinalQualifiers(SpecifierListAST *&node)
{
    DEBUG_THIS_RULE();

1232
    if (!_languageFeatures.cxx11Enabled)
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
        return false;

    unsigned start = cursor();

    SpecifierListAST **ast = &node;
    while (*ast)
        ast = &(*ast)->next;

    while (LA() == T_IDENTIFIER) {
        const Identifier &id = *(_translationUnit->tokenAt(cursor()).identifier);

        if (id.equalTo(_control->cpp11Override())
                || id.equalTo(_control->cpp11Final())) {
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
            *ast = new (_pool) SpecifierListAST(spec);
            ast = &(*ast)->next;
        }
        else {
            break;
        }
    }

    return (start != cursor());
}

Roberto Raggi's avatar
Roberto Raggi committed
1259
bool Parser::parsePtrOperator(PtrOperatorListAST *&node)
con's avatar
con committed
1260
{
Roberto Raggi's avatar
Roberto Raggi committed
1261
    DEBUG_THIS_RULE();
1262
    if (LA() == T_AMPER || (_languageFeatures.cxx11Enabled && LA() == T_AMPER_AMPER)) {
con's avatar
con committed
1263
        ReferenceAST *ast = new (_pool) ReferenceAST;
1264
        ast->reference_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1265
        node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1266
1267
1268
1269
        return true;
    } else if (LA() == T_STAR) {
        PointerAST *ast = new (_pool) PointerAST;
        ast->star_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1270
        parseCvQualifiers(ast->cv_qualifier_list);
Roberto Raggi's avatar
Roberto Raggi committed
1271
        node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1272
1273
1274
1275
1276
1277
1278
1279
        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
1280
1281
        NestedNameSpecifierListAST *nested_name_specifiers = 0;
        bool has_nested_name_specifier = parseNestedNameSpecifier(nested_name_specifiers, true);
con's avatar
con committed
1282
1283
1284
        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
1285
            ast->nested_name_specifier_list = nested_name_specifiers;
con's avatar
con committed
1286
            ast->star_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1287
            parseCvQualifiers(ast->cv_qualifier_list);
Roberto Raggi's avatar
Roberto Raggi committed
1288
            node = new (_pool) PtrOperatorListAST(ast);
con's avatar
con committed
1289
1290
1291
1292
1293
1294
1295
1296
1297
            return true;
        }
        rewind(scope_or_identifier_token);
    }
    return false;
}

bool Parser::parseTemplateArgument(ExpressionAST *&node)
{
Roberto Raggi's avatar
Roberto Raggi committed
1298
    DEBUG_THIS_RULE();
con's avatar
con committed
1299
    unsigned start = cursor();
1300
1301
1302
    if (parseTypeId(node)) {
        int index = 1;

1303
        if (_languageFeatures.cxx11Enabled && LA() == T_DOT_DOT_DOT)
1304
1305
            index = 2;

1306
        if (LA(index) == T_COMMA || maybeSplitGreaterGreaterToken(index) || LA(index) == T_GREATER)
1307
1308
            return true;
    }
con's avatar
con committed
1309
1310
1311

    rewind(start);
    bool previousTemplateArguments = switchTemplateArguments(true);
1312
    bool parsed = parseConstantExpression(node);
con's avatar
con committed
1313
1314
1315
1316
    (void) switchTemplateArguments(previousTemplateArguments);
    return parsed;
}

1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
/** Parses a sequence of
 *
 * storage-class-specifier (unless noStorageSpecifier is true)
 * trailing-type-specifier, which contains
 *     cv-qualifier
 *     simple-type-specifier
 *     typename-specifier
 *     elaborated-type-specifier
 *
 * If onlySimpleTypeSpecifiers is true, it only eats simple-type-specifiers
 * and cv-qualifiers.
 */
Roberto Raggi's avatar
Roberto Raggi committed
1329
bool Parser::parseDeclSpecifierSeq(SpecifierListAST *&decl_specifier_seq,
1330
1331
                                   bool noStorageSpecifiers,
                                   bool onlySimpleTypeSpecifiers)
con's avatar
con committed
1332
{
Roberto Raggi's avatar
Roberto Raggi committed
1333
    DEBUG_THIS_RULE();
con's avatar
con committed
1334
1335
    bool has_type_specifier = false;
    NameAST *named_type_specifier = 0;
Roberto Raggi's avatar
Roberto Raggi committed
1336
    SpecifierListAST **decl_specifier_seq_ptr = &decl_specifier_seq;
con's avatar
con committed
1337
    for (;;) {
1338
1339
        if (! noStorageSpecifiers && ! onlySimpleTypeSpecifiers && lookAtStorageClassSpecifier()) {
            // storage-class-specifier
con's avatar
con committed
1340
1341
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1342
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1343
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
1344
1345
        } else if (lookAtCVQualifier()) {
            // cv-qualifier
con's avatar
con committed
1346
1347
            SimpleSpecifierAST *spec = new (_pool) SimpleSpecifierAST;
            spec->specifier_token = consumeToken();
Roberto Raggi's avatar
Roberto Raggi committed
1348
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1349
1350
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
        } else if (! named_type_specifier && lookAtBuiltinTypeSpecifier()) {
1351
            // parts of simple-type-specifier
con's avatar
con committed
1352
1353
1354
1355
1356
            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)) {
1357
            // parts of simple-type-specifier
con's avatar
con committed
1358
1359
1360
1361
            if (! parseName(named_type_specifier))
                return false;
            NamedTypeSpecifierAST *spec = new (_pool) NamedTypeSpecifierAST;
            spec->name = named_type_specifier;
Roberto Raggi's avatar
Roberto Raggi committed
1362
            *decl_specifier_seq_ptr = new (_pool) SpecifierListAST(spec);
con's avatar
con committed
1363
1364
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
            has_type_specifier = true;
1365
1366
1367
        } else if (! onlySimpleTypeSpecifiers && ! has_type_specifier &&
                   (LA() == T_TYPENAME || LA() == T_ENUM || lookAtClassKey())) {
            // typename-specifier, elaborated-type-specifier
con's avatar
con committed
1368
1369
            unsigned startOfElaboratedTypeSpecifier = cursor();
            if (! parseElaboratedTypeSpecifier(*decl_specifier_seq_ptr)) {
Orgad Shaneh's avatar
Orgad Shaneh committed
1370
                error(startOfElaboratedTypeSpecifier, "expected an elaborated type specifier");
con's avatar
con committed
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
                break;
            }
            decl_specifier_seq_ptr = &(*decl_specifier_seq_ptr)->next;
            has_type_specifier = true;
        } else
            break;
    }

    return decl_specifier_seq != 0;
}

1382
bool Parser::parseDeclaratorOrAbstractDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list)
con's avatar
con committed
1383
{
Roberto Raggi's avatar
Roberto Raggi committed
1384
    DEBUG_THIS_RULE();
con's avatar
con committed
1385
1386
    unsigned start = cursor();
    bool blocked = blockErrors(true);
1387
    if (parseDeclarator(node, decl_specifier_list)) {
con's avatar
con committed
1388
1389
1390
1391
1392
        blockErrors(blocked);
        return true;
    }
    blockErrors(blocked);
    rewind(start);
1393
    return parseAbstractDeclarator(node, decl_specifier_list);
con's avatar
con committed
1394
1395
}

1396
bool Parser::parseCoreDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list, ClassSpecifierAST *)
con's avatar
con committed
1397
{
Roberto Raggi's avatar
Roberto Raggi committed
1398
    DEBUG_THIS_RULE();
1399
    unsigned start = cursor();
Roberto Raggi's avatar
Roberto Raggi committed
1400
1401
    SpecifierListAST *attributes = 0;
    SpecifierListAST **attribute_ptr = &attributes;
1402
1403
1404
1405
1406
    while (LA() == T___ATTRIBUTE__) {
        parseAttributeSpecifier(*attribute_ptr);
        attribute_ptr = &(*attribute_ptr)->next;
    }

Roberto Raggi's avatar
Roberto Raggi committed
1407
    PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
con's avatar
con committed
1408
1409
1410
    while (parsePtrOperator(*ptr_operators_tail))
        ptr_operators_tail = &(*ptr_operators_tail)->next;

1411
    if (LA() == T_COLON_COLON || LA() == T_IDENTIFIER || LA() == T_TILDE || LA() == T_OPERATOR
1412
        || (_languageFeatures.cxx11Enabled && LA() == T_DOT_DOT_DOT && (LA(2) == T_COLON_COLON || LA(2) == T_IDENTIFIER))) {
1413
1414
1415
1416
1417
1418

        unsigned dot_dot_dot_token = 0;

        if (LA() == T_DOT_DOT_DOT)
            dot_dot_dot_token = consumeToken();

con's avatar
con committed
1419
1420
1421
        NameAST *name = 0;
        if (parseName(name)) {
            DeclaratorIdAST *declarator_id = new (_pool) DeclaratorIdAST;
1422
            declarator_id->dot_dot_dot_token = dot_dot_dot_token;
con's avatar
con committed
1423
1424
            declarator_id->name = name;
            DeclaratorAST *ast = new (_pool) DeclaratorAST;
Roberto Raggi's avatar
Roberto Raggi committed
1425
1426
            ast->attribute_list = attributes;
            ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1427
1428
1429
1430
            ast->core_declarator = declarator_id;
            node = ast;
            return true;
        }
1431
    } else if (decl_specifier_list && LA() == T_LPAREN) {
1432
        if (attributes)
1433
            warning(attributes->firstToken(), "unexpected attribtues");
1434

con's avatar
con committed
1435
1436
        unsigned lparen_token = consumeToken();
        DeclaratorAST *declarator = 0;
1437
        if (parseDeclarator(declarator, decl_specifier_list) && LA() == T_RPAREN) {
con's avatar
con committed
1438
1439
1440
1441
1442
            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
1443
            ast->ptr_operator_list = ptr_operators;
con's avatar
con committed
1444
1445
1446
1447
1448
            ast->core_declarator = nested_declarator;
            node = ast;
            return true;
        }
    }
1449
    rewind(start);
con's avatar
con committed
1450
1451
1452
    return false;
}

1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
static bool maybeCppInitializer(DeclaratorAST *declarator)
{
    if (declarator->ptr_operator_list)
        return false;
    CoreDeclaratorAST *core_declarator = declarator->core_declarator;
    if (! core_declarator)
        return false;
    DeclaratorIdAST *declarator_id = core_declarator->asDeclaratorId();
    if (! declarator_id)
        return false;
    else if (! declarator_id->name)
        return false;
    else if (! declarator_id->name->asSimpleName())
        return false;

    return true;
}

1471
bool Parser::parseDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list, ClassSpecifierAST *declaringClass)
con's avatar
con committed
1472
{
Roberto Raggi's avatar
Roberto Raggi committed
1473
    DEBUG_THIS_RULE();
1474
    if (! parseCoreDeclarator(node, decl_specifier_list, declaringClass))
con's avatar
con committed
1475
1476
        return false;

Roberto Raggi's avatar
Roberto Raggi committed
1477
    PostfixDeclaratorListAST **postfix_ptr = &node->postfix_declarator_list;
con's avatar
con committed
1478
1479
1480
1481
1482

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

        if (LA() == T_LPAREN) {
1483
            if (! declaringClass && LA(2) != T_RPAREN && maybeCppInitializer(node)) {
1484
1485
1486
1487
                unsigned lparen_token = cursor();
                ExpressionAST *initializer = 0;

                bool blocked = blockErrors(true);
1488
                if (parseInitializer(initializer, &node->equal_token)) {
1489
1490
                    // maybe the initializer also parses as a FunctionDeclarator?
                    ExpressionListParenAST *expr = 0;
Roberto Raggi's avatar
Roberto Raggi committed
1491
                    if (initializer)
1492
                        expr = initializer->asExpressionListParen();
Roberto Raggi's avatar
Roberto Raggi committed
1493
                    if (expr) {
1494
                        if (expr->expression_list && expr->rparen_token && (LA() == T_COMMA || LA() == T_SEMICOLON)) {
1495
                            rewind(lparen_token);
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505

                            // 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;
Roberto Raggi's avatar
Roberto Raggi committed
1506
                                ast->parameter_declaration_clause = parameter_declaration_clause;