PrettyPrinter.cpp 26.6 KB
Newer Older
1
/**************************************************************************
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
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
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
27
**
28
**************************************************************************/
29
30
31

#include "PrettyPrinter.h"
#include "AST.h"
32
#include "Token.h"
33

34
35
#include <iostream>
#include <string>
36
#include <sstream>
mae's avatar
mae committed
37
#include <cassert>
38

39
40
#include <QString>

41
42
43
44
CPLUSPLUS_USE_NAMESPACE

PrettyPrinter::PrettyPrinter(Control *control, std::ostream &out)
    : ASTVisitor(control),
45
46
47
      _out(out),
      _depth(0),
      _lastToken(0)
48
49
{ }

50
51
52
53
54
55
56
57
void PrettyPrinter::operator()(AST *ast, const QByteArray &contents)
{
    _contents = contents;
    accept(ast);

    if (_lastToken + 1 < tokenCount())
        outToken(_lastToken + 1);
}
58
59

void PrettyPrinter::indent()
60
{ ++_depth; }
61
62

void PrettyPrinter::deindent()
63
{ --_depth; }
64
65

void PrettyPrinter::newline()
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{
    _out << '\n' << std::string(_depth * 4, ' ');
}

void PrettyPrinter::outToken(unsigned token)
{
    if (!token)
        return;

    const Token &t = tokenAt(token);
    const unsigned start = _lastToken ? tokenAt(_lastToken).end() : 0;
    const unsigned end = t.begin();
    _lastToken = token;

80
81
    std::ostringstream oss;

82
83
    // Preserve non-AST text
    QByteArray ba(_contents.constData() + start, end - start);
84
    oss << ba.constData();
85
86
87

    // Print the token itself
    QByteArray tt(_contents.constData() + t.begin(), t.length);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    oss << tt.constData();

    QString stuff = QString::fromUtf8(oss.str().c_str());
    QString indent = QString(_depth * 4, QLatin1Char(' '));

    int from = 0;
    int index = 0;
    while ((index = stuff.indexOf(QLatin1Char('\n'), from)) != -1) {
        from = index + 1;
        int firstNonWhitespace = from;

        while (firstNonWhitespace < stuff.length()) {
            const QChar c = stuff.at(firstNonWhitespace);
            if (c.isSpace() && c != QLatin1Char('\n'))
                ++firstNonWhitespace;
            else
                break;
        }

        if (firstNonWhitespace != from)
            stuff.replace(from, firstNonWhitespace - from, indent);
    }

    _out << stuff.toUtf8().constData();
112
}
113
114
115
116

bool PrettyPrinter::visit(AccessDeclarationAST *ast)
{
    deindent();
117
118
119
    outToken(ast->access_specifier_token);
    outToken(ast->slots_token);
    outToken(ast->colon_token);
120
121
122
123
124
125
    indent();
    return false;
}

bool PrettyPrinter::visit(ArrayAccessAST *ast)
{
126
    outToken(ast->lbracket_token);
127
    accept(ast->expression);
128
    outToken(ast->rbracket_token);
129
130
131
132
133
    return false;
}

bool PrettyPrinter::visit(ArrayDeclaratorAST *ast)
{
134
    outToken(ast->lbracket_token);
135
    accept(ast->expression);
136
    outToken(ast->rbracket_token);
137
138
139
140
141
    return false;
}

bool PrettyPrinter::visit(ArrayInitializerAST *ast)
{
142
    outToken(ast->lbrace_token);
143
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
144
        outToken(it->comma_token);
145
146
        accept(it->expression);
    }
147
    outToken(ast->rbrace_token);
148
149
150
151
152
    return false;
}

bool PrettyPrinter::visit(AsmDefinitionAST *ast)
{
153
154
155
156
157
158
    outToken(ast->asm_token);
    outToken(ast->volatile_token);
    outToken(ast->lparen_token);
    /* ### implement me */
    outToken(ast->rparen_token);
    outToken(ast->semicolon_token);
159
160
161
162
163
    return false;
}

bool PrettyPrinter::visit(AttributeSpecifierAST *ast)
{
164
165
166
    outToken(ast->attribute_token);
    outToken(ast->first_lparen_token);
    outToken(ast->second_lparen_token);
167
    for (AttributeAST *it = ast->attributes; it; it = it->next) {
168
        outToken(it->comma_token);
169
170
        accept(it);
    }
171
172
    outToken(ast->first_rparen_token);
    outToken(ast->second_rparen_token);
173
174
175
176
177
    return false;
}

bool PrettyPrinter::visit(AttributeAST *ast)
{
178
    outToken(ast->identifier_token);
179
    if (ast->lparen_token) {
180
181
        outToken(ast->lparen_token);
        outToken(ast->tag_token);
182
183
        if (ast->expression_list) {
            for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
184
                outToken(ast->expression_list->comma_token);
185
186
187
                accept(it->expression);
            }
        }
188
        outToken(ast->rparen_token);
189
190
191
192
193
194
    }
    return false;
}

bool PrettyPrinter::visit(BaseSpecifierAST *ast)
{
195
196
    outToken(ast->token_virtual);
    outToken(ast->token_access_specifier);
197
198
199
200
201
202
203
    accept(ast->name);
    return false;
}

bool PrettyPrinter::visit(BinaryExpressionAST *ast)
{
    accept(ast->left_expression);
204
    outToken(ast->binary_op_token);
205
206
207
208
209
210
    accept(ast->right_expression);
    return false;
}

bool PrettyPrinter::visit(BoolLiteralAST *ast)
{
211
    outToken(ast->token);
212
213
214
    return false;
}

215
bool PrettyPrinter::visit(BreakStatementAST *ast)
216
{
217
218
    outToken(ast->break_token);
    outToken(ast->semicolon_token);
219
220
221
222
223
    return false;
}

bool PrettyPrinter::visit(CallAST *ast)
{
224
    outToken(ast->lparen_token);
225
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
226
        outToken(it->comma_token);
227
228
        accept(it->expression);
    }
229
    outToken(ast->rparen_token);
230
231
232
233
234
    return false;
}

bool PrettyPrinter::visit(CaseStatementAST *ast)
{
235
    outToken(ast->case_token);
236
    accept(ast->expression);
237
238
    outToken(ast->colon_token);
    if (! ast->statement)
239
240
241
242
243
244
245
246
247
248
249
        return false;

    if (ast->statement->asCompoundStatement()) {
        accept(ast->statement);
    } else if (ast->statement->asCaseStatement() || ast->statement->asLabeledStatement()) {
        accept(ast->statement);
    } else {
        indent();
        accept(ast->statement);
        deindent();
    }
250
251
252
253
254
    return false;
}

bool PrettyPrinter::visit(CastExpressionAST *ast)
{
255
    outToken(ast->lparen_token);
256
    accept(ast->type_id);
257
    outToken(ast->rparen_token);
258
259
260
261
262
263
    accept(ast->expression);
    return false;
}

bool PrettyPrinter::visit(CatchClauseAST *ast)
{
264
265
    outToken(ast->catch_token);
    outToken(ast->lparen_token);
266
    accept(ast->exception_declaration);
267
    outToken(ast->rparen_token);
268
269
270
271
272
273
    accept(ast->statement);
    return false;
}

bool PrettyPrinter::visit(ClassSpecifierAST *ast)
{
274
    outToken(ast->classkey_token);
275
276
277
278
279
    if (ast->attributes) {
        accept(ast->attributes);
    }
    accept(ast->name);
    if (ast->colon_token) {
280
        outToken(ast->colon_token);
281
        for (BaseSpecifierAST *it = ast->base_clause; it; it = it->next) {
282
            outToken(it->comma_token);
283
284
285
            accept(it);
        }
    }
286
    outToken(ast->lbrace_token);
287
288
289
290
291
292
293
294
295
    if (ast->member_specifiers) {
        indent();
        if (ast->member_specifiers) {
            for (DeclarationAST *it = ast->member_specifiers; it; it = it->next) {
                accept(it);
            }
        }
        deindent();
    }
296
    outToken(ast->rbrace_token);
297
298
299
300
301
    return false;
}

bool PrettyPrinter::visit(CompoundStatementAST *ast)
{
302
    outToken(ast->lbrace_token);
303
304
305
306
307
308
309
    if (ast->statements) {
        indent();
        for (StatementAST *it = ast->statements; it; it = it->next) {
            accept(it);
        }
        deindent();
    }
310
    outToken(ast->rbrace_token);
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    return false;
}

bool PrettyPrinter::visit(ConditionAST *ast)
{
    for (SpecifierAST *it = ast->type_specifier; it; it = it->next) {
        accept(it);
    }
    if (ast->declarator) {
        accept(ast->declarator);
    }
    return false;
}

bool PrettyPrinter::visit(ConditionalExpressionAST *ast)
{
    accept(ast->condition);
328
    outToken(ast->question_token);
329
    accept(ast->left_expression);
330
    outToken(ast->colon_token);
331
332
333
334
    accept(ast->right_expression);
    return false;
}

335
bool PrettyPrinter::visit(ContinueStatementAST *ast)
336
{
337
338
    outToken(ast->continue_token);
    outToken(ast->semicolon_token);
339
340
341
342
343
    return false;
}

bool PrettyPrinter::visit(ConversionFunctionIdAST *ast)
{
344
    outToken(ast->operator_token);
345
346
347
348
349
350
351
352
353
354
355
    for (SpecifierAST *it = ast->type_specifier; it; it = it->next) {
        accept(it);
    }
    for (PtrOperatorAST *it = ast->ptr_operators; it; it = it->next) {
        accept(it);
    }
    return false;
}

bool PrettyPrinter::visit(CppCastExpressionAST *ast)
{
356
357
    outToken(ast->cast_token);
    outToken(ast->less_token);
358
    accept(ast->type_id);
359
360
    outToken(ast->greater_token);
    outToken(ast->lparen_token);
361
    accept(ast->expression);
362
    outToken(ast->rparen_token);
363
364
365
366
367
    return false;
}

bool PrettyPrinter::visit(CtorInitializerAST *ast)
{
368
    outToken(ast->colon_token);
369
    for (MemInitializerAST *it = ast->member_initializers; it; it = it->next) {
370
        outToken(it->comma_token);
371
        accept(it->name);
372
        outToken(it->lparen_token);
373
        accept(it->expression);
374
        outToken(it->rparen_token);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    }
    return false;
}

bool PrettyPrinter::visit(DeclaratorAST *ast)
{
    for (PtrOperatorAST *it = ast->ptr_operators; it; it = it->next) {
        accept(it);
    }
    if (ast->core_declarator) {
        accept(ast->core_declarator);
    }
    for (PostfixDeclaratorAST *it = ast->postfix_declarators; it; it = it->next) {
        accept(it);
    }
    for (SpecifierAST *it = ast->attributes; it; it = it->next) {
        accept(it);
    }
    if (ast->initializer) {
394
        outToken(ast->equals_token);
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
        accept(ast->initializer);
    }
    return false;
}

bool PrettyPrinter::visit(DeclarationStatementAST *ast)
{
    accept(ast->declaration);
    return false;
}

bool PrettyPrinter::visit(DeclaratorIdAST *ast)
{
    accept(ast->name);
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
412
bool PrettyPrinter::visit(DeclaratorListAST *ast)
413
{
Roberto Raggi's avatar
Roberto Raggi committed
414
    for (DeclaratorListAST *it = ast; it; it = it->next) {
415
        outToken(ast->comma_token);
Roberto Raggi's avatar
Roberto Raggi committed
416
417
        accept(it->declarator);
    }
418
419
420
421
422
    return false;
}

bool PrettyPrinter::visit(DeleteExpressionAST *ast)
{
423
424
    outToken(ast->scope_token);
    outToken(ast->delete_token);
425
426
427
428
429
430
431
432
    if (ast->expression) {
        accept(ast->expression);
    }
    return false;
}

bool PrettyPrinter::visit(DestructorNameAST *ast)
{
433
434
    outToken(ast->tilde_token);
    outToken(ast->identifier_token);
435
436
437
438
439
    return false;
}

bool PrettyPrinter::visit(DoStatementAST *ast)
{
440
    outToken(ast->do_token);
441
442
443
    if (ast->statement) {
        accept(ast->statement);
    }
444
445
    outToken(ast->while_token);
    outToken(ast->lparen_token);
446
    accept(ast->expression);
447
448
    outToken(ast->rparen_token);
    outToken(ast->semicolon_token);
449
450
451
452
453
    return false;
}

bool PrettyPrinter::visit(ElaboratedTypeSpecifierAST *ast)
{
454
    outToken(ast->classkey_token);
455
456
457
458
459
460
    if (ast->name) {
        accept(ast->name);
    }
    return false;
}

461
bool PrettyPrinter::visit(EmptyDeclarationAST *ast)
462
{
463
    outToken(ast->semicolon_token);
464
465
466
467
468
    return false;
}

bool PrettyPrinter::visit(EnumSpecifierAST *ast)
{
469
    outToken(ast->enum_token);
470
471
472
    if (ast->name) {
        accept(ast->name);
    }
473
    outToken(ast->lbrace_token);
474
475
476
    if (ast->enumerators) {
        indent();
        for (EnumeratorAST *it = ast->enumerators; it; it = it->next) {
477
            outToken(it->comma_token);
478
479
480
            accept(it);
        }
        deindent();
481
    }
482
    outToken(ast->rbrace_token);
483
484
485
486
487
    return false;
}

bool PrettyPrinter::visit(EnumeratorAST *ast)
{
488
    outToken(ast->identifier_token);
489
    if (ast->equal_token) {
490
        outToken(ast->equal_token);
491
492
493
494
495
496
497
498
499
500
501
502
503
        accept(ast->expression);
    }
    return false;
}

bool PrettyPrinter::visit(ExceptionDeclarationAST *ast)
{
    for (SpecifierAST *it = ast->type_specifier; it; it = it->next) {
        accept(it);
    }
    if (ast->declarator) {
        accept(ast->declarator);
    }
504
    outToken(ast->dot_dot_dot_token);
505
506
507
508
509
    return false;
}

bool PrettyPrinter::visit(ExceptionSpecificationAST *ast)
{
510
511
    outToken(ast->throw_token);
    outToken(ast->lparen_token);
512
    if (ast->dot_dot_dot_token)
513
        outToken(ast->dot_dot_dot_token);
514
515
    else {
        for (ExpressionListAST *it = ast->type_ids; it; it = it->next) {
516
            outToken(it->comma_token);
517
518
519
            accept(it->expression);
        }
    }
520
    outToken(ast->rparen_token);
521
522
523
524
525
526
    return false;
}

bool PrettyPrinter::visit(ExpressionListAST *ast)
{
    for (ExpressionListAST *it = ast; it; it = it->next) {
527
        outToken(it->comma_token);
528
529
530
531
532
533
534
535
536
537
538
539
540
541
        accept(it->expression);
    }
    return false;
}

bool PrettyPrinter::visit(ExpressionOrDeclarationStatementAST *ast)
{
    accept(ast->declaration);
    return false;
}

bool PrettyPrinter::visit(ExpressionStatementAST *ast)
{
    accept(ast->expression);
542
    outToken(ast->semicolon_token);
543
544
545
546
547
    return false;
}

bool PrettyPrinter::visit(ForStatementAST *ast)
{
548
549
    outToken(ast->for_token);
    outToken(ast->lparen_token);
550
551
    accept(ast->initializer);
    accept(ast->condition);
552
    outToken(ast->semicolon_token);
553
    accept(ast->expression);
554
    outToken(ast->rparen_token);
555
556
557
558
559
560
    accept(ast->statement);
    return false;
}

bool PrettyPrinter::visit(FunctionDeclaratorAST *ast)
{
561
    outToken(ast->lparen_token);
562
    accept(ast->parameters);
563
    outToken(ast->rparen_token);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
    for (SpecifierAST *it = ast->cv_qualifier_seq; it; it = it->next) {
        accept(it);
    }
    if (ast->exception_specification) {
        accept(ast->exception_specification);
    }
    return false;
}

bool PrettyPrinter::visit(FunctionDefinitionAST *ast)
{
    for (SpecifierAST *it = ast->decl_specifier_seq; it; it = it->next) {
        accept(it);
    }
    if (ast->declarator) {
        accept(ast->declarator);
    }
    accept(ast->ctor_initializer);
    accept(ast->function_body);
    return false;
}

bool PrettyPrinter::visit(GotoStatementAST *ast)
{
588
589
590
    outToken(ast->goto_token);
    outToken(ast->identifier_token);
    outToken(ast->semicolon_token);
591
592
593
594
595
    return false;
}

bool PrettyPrinter::visit(IfStatementAST *ast)
{
596
597
    outToken(ast->if_token);
    outToken(ast->lparen_token);
598
    accept(ast->condition);
599
    outToken(ast->rparen_token);
600
    if (ast->statement->asCompoundStatement()) {
601
        accept(ast->statement);
602
    } else {
603
604
605
606
607
        indent();
        accept(ast->statement);
        deindent();
    }
    if (ast->else_token) {
608
        outToken(ast->else_token);
609
610
611
612
613
614
615
        accept(ast->else_statement);
    }
    return false;
}

bool PrettyPrinter::visit(LabeledStatementAST *ast)
{
616
617
    outToken(ast->label_token);
    outToken(ast->colon_token);
618
619
620
621
622
623
    accept(ast->statement);
    return false;
}

bool PrettyPrinter::visit(LinkageBodyAST *ast)
{
624
    outToken(ast->lbrace_token);
625
626
627
628
629
630
631
    if (ast->declarations) {
        indent();
        for (DeclarationAST *it = ast->declarations; it; it = it->next) {
            accept(it);
        }
        deindent();
    }
632
    outToken(ast->rbrace_token);
633
634
635
636
637
    return false;
}

bool PrettyPrinter::visit(LinkageSpecificationAST *ast)
{
638
    outToken(ast->extern_token);
639
    if (ast->extern_type) {
640
        outToken(ast->extern_type);
641
642
643
644
645
646
647
648
649
    }

    accept(ast->declaration);
    return false;
}

bool PrettyPrinter::visit(MemInitializerAST *ast)
{
    accept(ast->name);
650
    outToken(ast->lparen_token);
651
    accept(ast->expression);
652
    outToken(ast->rparen_token);
653
654
655
656
657
    return false;
}

bool PrettyPrinter::visit(MemberAccessAST *ast)
{
658
659
    outToken(ast->access_token);
    outToken(ast->template_token);
660
661
662
663
664
665
666
667
668
669
670
671
    accept(ast->member_name);
    return false;
}

bool PrettyPrinter::visit(NamedTypeSpecifierAST *ast)
{
    accept(ast->name);
    return false;
}

bool PrettyPrinter::visit(NamespaceAST *ast)
{
672
673
    outToken(ast->namespace_token);
    outToken(ast->identifier_token);
674
675
676
677
678
679
680
681
682
    for (SpecifierAST *it = ast->attributes; it; it = it->next) {
        accept(it);
    }
    accept(ast->linkage_body);
    return false;
}

bool PrettyPrinter::visit(NamespaceAliasDefinitionAST *ast)
{
683
684
685
    outToken(ast->namespace_token);
    outToken(ast->namespace_name);
    outToken(ast->equal_token);
686
    accept(ast->name);
687
    outToken(ast->semicolon_token);
688
689
690
691
692
    return false;
}

bool PrettyPrinter::visit(NestedDeclaratorAST *ast)
{
693
    outToken(ast->lparen_token);
694
    accept(ast->declarator);
695
    outToken(ast->rparen_token);
696
697
698
699
700
    return false;
}

bool PrettyPrinter::visit(NestedExpressionAST *ast)
{
701
    outToken(ast->lparen_token);
702
    accept(ast->expression);
703
    outToken(ast->rparen_token);
704
705
706
707
708
709
    return false;
}

bool PrettyPrinter::visit(NestedNameSpecifierAST *ast)
{
    accept(ast->class_or_namespace_name);
710
    outToken(ast->scope_token);
711
712
713
    return false;
}

714
bool PrettyPrinter::visit(NewArrayDeclaratorAST *ast)
715
{
716
    outToken(ast->lbracket_token);
717
    accept(ast->expression);
718
    outToken(ast->rbracket_token);
719
720
721
722
723
    return false;
}

bool PrettyPrinter::visit(NewExpressionAST *ast)
{
724
725
    outToken(ast->scope_token);
    outToken(ast->new_token);
726
727
    accept(ast->new_placement);
    if (ast->lparen_token) {
728
        outToken(ast->lparen_token);
729
        accept(ast->type_id);
730
        outToken(ast->rparen_token);
731
    } else {
732
733
734
735
736
737
        accept(ast->new_type_id);
    }
    accept(ast->new_initializer);
    return false;
}

738
739
bool PrettyPrinter::visit(NewPlacementAST *ast)
{
740
    outToken(ast->lparen_token);
741
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
742
        outToken(it->comma_token);
743
744
        accept(it->expression);
    }
745
    outToken(ast->rparen_token);
746
747
748
    return false;
}

749
750
bool PrettyPrinter::visit(NewInitializerAST *ast)
{
751
    outToken(ast->lparen_token);
752
    accept(ast->expression);
753
    outToken(ast->rparen_token);
754
755
756
757
758
759
    return false;
}

bool PrettyPrinter::visit(NewTypeIdAST *ast)
{
    for (SpecifierAST *it = ast->type_specifier; it; it = it->next) {
760
        accept(it);
761
    }
762
763
764
765
766
    for (PtrOperatorAST *it = ast->ptr_operators; it; it = it->next) {
        accept(it);
    }
    for (NewArrayDeclaratorAST *it = ast->new_array_declarators; it; it = it->next) {
        accept(it);
767
768
769
770
771
772
    }
    return false;
}

bool PrettyPrinter::visit(NumericLiteralAST *ast)
{
773
    outToken(ast->token);
774
775
776
777
778
    return false;
}

bool PrettyPrinter::visit(OperatorAST *ast)
{
779
    outToken(ast->op_token);
780
    if (ast->open_token) {
781
782
        outToken(ast->open_token);
        outToken(ast->close_token);
783
784
785
786
787
788
    }
    return false;
}

bool PrettyPrinter::visit(OperatorFunctionIdAST *ast)
{
789
    outToken(ast->operator_token);
790
791
792
793
794
795
796
797
798
799
800
801
    accept(ast->op);
    return false;
}

bool PrettyPrinter::visit(ParameterDeclarationAST *ast)
{
    for (SpecifierAST *it = ast->type_specifier; it; it = it->next) {
        accept(it);
    }
    if (ast->declarator) {
        accept(ast->declarator);
    }
802
    outToken(ast->equal_token);
803
804
805
806
807
808
809
    accept(ast->expression);
    return false;
}

bool PrettyPrinter::visit(ParameterDeclarationClauseAST *ast)
{
    for (DeclarationAST *it = ast->parameter_declarations; it; it = it->next) {
810
        // XXX handle the comma tokens correctly
811
812
813
814
815
816
817
        accept(it);
    }
    return false;
}

bool PrettyPrinter::visit(PointerAST *ast)
{
818
    outToken(ast->star_token);
819
820
821
822
823
824
825
826
    for (SpecifierAST *it = ast->cv_qualifier_seq; it; it = it->next) {
        accept(it);
    }
    return false;
}

bool PrettyPrinter::visit(PointerToMemberAST *ast)
{
827
    outToken(ast->global_scope_token);
828
829
830
    for (NestedNameSpecifierAST *it = ast->nested_name_specifier; it; it = it->next) {
        accept(it);
    }
831
    outToken(ast->star_token);
832
833
834
835
836
837
838
839
    for (SpecifierAST *it = ast->cv_qualifier_seq; it; it = it->next) {
        accept(it);
    }
    return false;
}

bool PrettyPrinter::visit(PostIncrDecrAST *ast)
{
840
    outToken(ast->incr_decr_token);
841
842
843
844
845
846
847
848
849
850
851
852
853
854
    return false;
}

bool PrettyPrinter::visit(PostfixExpressionAST *ast)
{
    accept(ast->base_expression);
    for (PostfixAST *it = ast->postfix_expressions; it; it = it->next) {
        accept(it);
    }
    return false;
}

bool PrettyPrinter::visit(QualifiedNameAST *ast)
{
855
    outToken(ast->global_scope_token);
856
857
858
859
860
861
862
    for (NestedNameSpecifierAST *it = ast->nested_name_specifier; it; it = it->next) {
        accept(it);
    }
    accept(ast->unqualified_name);
    return false;
}

863
bool PrettyPrinter::visit(ReferenceAST *ast)
864
{
865
    outToken(ast->amp_token);
866
867
868
869
870
    return false;
}

bool PrettyPrinter::visit(ReturnStatementAST *ast)
{
871
    outToken(ast->return_token);
872
873
874
    if (ast->expression) {
        accept(ast->expression);
    }
875
    outToken(ast->semicolon_token);
876
877
878
879
880
881
882
883
884
885
    return false;
}

bool PrettyPrinter::visit(SimpleDeclarationAST *ast)
{
    for (SpecifierAST *it = ast->decl_specifier_seq; it; it = it->next) {
        accept(it);
    }
    if (ast->declarators) {
        for (DeclaratorListAST *it = ast->declarators; it; it = it->next) {
886
            outToken(it->comma_token);
887
888
889
            accept(it->declarator);
        }
    }
890
    outToken(ast->semicolon_token);
891
892
893
894
895
    return false;
}

bool PrettyPrinter::visit(SimpleNameAST *ast)
{
896
    outToken(ast->identifier_token);
897
898
899
900
901
    return false;
}

bool PrettyPrinter::visit(SimpleSpecifierAST *ast)
{
902
    outToken(ast->specifier_token);
903
904
905
906
907
    return false;
}

bool PrettyPrinter::visit(SizeofExpressionAST *ast)
{
908
    outToken(ast->sizeof_token);
909
910
911
912
913
914
    accept(ast->expression);
    return false;
}

bool PrettyPrinter::visit(StringLiteralAST *ast)
{
915
    for (StringLiteralAST *it = ast; it; it = it->next) {
916
        outToken(it->token);
917
918
919
920
921
922
    }
    return false;
}

bool PrettyPrinter::visit(SwitchStatementAST *ast)
{
923
924
    outToken(ast->switch_token);
    outToken(ast->lparen_token);
925
    accept(ast->condition);
926
    outToken(ast->rparen_token);
927
928
929
930
931
932
933
    accept(ast->statement);
    return false;
}

bool PrettyPrinter::visit(TemplateArgumentListAST *ast)
{
    for (TemplateArgumentListAST *it = ast; it; it = it->next) {
934
        outToken(it->comma_token);
935
936
937
938
939
940
941
        accept(it->template_argument);
    }
    return false;
}

bool PrettyPrinter::visit(TemplateDeclarationAST *ast)
{
942
943
944
    outToken(ast->export_token);
    outToken(ast->template_token);
    outToken(ast->less_token);
945
946
    if (ast->template_parameters) {
        for (DeclarationAST *it = ast->template_parameters; it; it = it->next) {
947
            // XXX handle the comma tokens correctly
948
949
950
            accept(it);
        }
    }
951
    outToken(ast->greater_token);
952
953
954
955
956
957
    accept(ast->declaration);
    return false;
}

bool PrettyPrinter::visit(TemplateIdAST *ast)
{
958
959
    outToken(ast->identifier_token);
    outToken(ast->less_token);
960
961
    if (ast->template_arguments) {
        for (TemplateArgumentListAST *it = ast->template_arguments; it; it = it->next) {
962
            outToken(it->comma_token);
963
964
965
            accept(it->template_argument);
        }
    }
966
    outToken(ast->greater_token);
967
968
969
970
971
    return false;
}

bool PrettyPrinter::visit(TemplateTypeParameterAST *ast)
{
972
973
    outToken(ast->template_token);
    outToken(ast->less_token);
974
975
    if (ast->template_parameters) {
        for (DeclarationAST *it = ast->template_parameters; it; it = it->next) {
976
            // XXX handle the comma tokens correctly
977
978
979
            accept(it);
        }
    }
980
981
    outToken(ast->greater_token);
    outToken(ast->class_token);
982
983
    accept(ast->name);
    if (ast->equal_token) {
984
        outToken(ast->equal_token);
985
986
987
988
989
        accept(ast->type_id);
    }
    return false;
}

990
bool PrettyPrinter::visit(ThisExpressionAST *ast)
991
{
992
    outToken(ast->this_token);
993
994
995
996
997
    return false;
}

bool PrettyPrinter::visit(ThrowExpressionAST *ast)
{
998
    outToken(ast->throw_token);
999
1000
    accept(ast->expression);
    return false;