Bind.cpp 99.8 KB
Newer Older
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
27
28
29
// 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 "Bind.h"
#include "AST.h"
#include "TranslationUnit.h"
#include "Control.h"
#include "Names.h"
#include "Symbols.h"
#include "CoreTypes.h"
#include "Literals.h"
#include "Scope.h"
30
31
32

#include "cppassert.h"

Roberto Raggi's avatar
Roberto Raggi committed
33
#include <vector>
34
#include <string>
35
#include <memory>
36
#include <sstream>
37
38
39

using namespace CPlusPlus;

40
41
const int Bind::kMaxDepth(100);

42
43
Bind::Bind(TranslationUnit *unit)
    : ASTVisitor(unit),
Roberto Raggi's avatar
Roberto Raggi committed
44
45
      _scope(0),
      _expression(0),
Roberto Raggi's avatar
Roberto Raggi committed
46
      _name(0),
Roberto Raggi's avatar
Roberto Raggi committed
47
48
      _declaratorId(0),
      _visibility(Symbol::Public),
49
      _objcVisibility(Symbol::Public),
Roberto Raggi's avatar
Roberto Raggi committed
50
      _methodKey(Function::NormalMethod),
51
52
      _skipFunctionBodies(false),
      _depth(0)
53
54
55
{
}

Roberto Raggi's avatar
Roberto Raggi committed
56
57
58
59
60
61
62
63
64
65
bool Bind::skipFunctionBodies() const
{
    return _skipFunctionBodies;
}

void Bind::setSkipFunctionBodies(bool skipFunctionBodies)
{
    _skipFunctionBodies = skipFunctionBodies;
}

Roberto Raggi's avatar
Roberto Raggi committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
unsigned Bind::location(DeclaratorAST *ast, unsigned defaultLocation) const
{
    if (! ast)
        return defaultLocation;

    else if (ast->core_declarator)
        return location(ast->core_declarator, defaultLocation);

    return ast->firstToken();
}

unsigned Bind::location(CoreDeclaratorAST *ast, unsigned defaultLocation) const
{
    if (! ast)
        return defaultLocation;

    else if (NestedDeclaratorAST *nested = ast->asNestedDeclarator())
        return location(nested->declarator, defaultLocation);

    else if (DeclaratorIdAST *id = ast->asDeclaratorId())
        return location(id->name, defaultLocation);

    return ast->firstToken();
}

unsigned Bind::location(NameAST *name, unsigned defaultLocation) const
{
    if (! name)
        return defaultLocation;

    else if (DestructorNameAST *dtor = name->asDestructorName())
97
        return location(dtor->unqualified_name, defaultLocation);
Roberto Raggi's avatar
Roberto Raggi committed
98
99
100
101
102
103
104
105
106
107
108
109

    else if (TemplateIdAST *templId = name->asTemplateId())
        return templId->identifier_token;

    else if (QualifiedNameAST *q = name->asQualifiedName()) {
        if (q->unqualified_name)
            return location(q->unqualified_name, defaultLocation);
    }

    return name->firstToken();
}

Roberto Raggi's avatar
Roberto Raggi committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
void Bind::setDeclSpecifiers(Symbol *symbol, const FullySpecifiedType &declSpecifiers)
{
    if (! symbol)
        return;

    int storage = Symbol::NoStorage;

    if (declSpecifiers.isFriend())
        storage = Symbol::Friend;
    else if (declSpecifiers.isAuto())
        storage = Symbol::Auto;
    else if (declSpecifiers.isRegister())
        storage = Symbol::Register;
    else if (declSpecifiers.isStatic())
        storage = Symbol::Static;
    else if (declSpecifiers.isExtern())
        storage = Symbol::Extern;
    else if (declSpecifiers.isMutable())
        storage = Symbol::Mutable;
    else if (declSpecifiers.isTypedef())
        storage = Symbol::Typedef;

    symbol->setStorage(storage);

    if (Function *funTy = symbol->asFunction()) {
135
        if (declSpecifiers.isVirtual())
Roberto Raggi's avatar
Roberto Raggi committed
136
137
138
139
140
141
142
143
144
145
            funTy->setVirtual(true);
    }

    if (declSpecifiers.isDeprecated())
        symbol->setDeprecated(true);

    if (declSpecifiers.isUnavailable())
        symbol->setUnavailable(true);
}

Roberto Raggi's avatar
Roberto Raggi committed
146
Scope *Bind::switchScope(Scope *scope)
147
{
Roberto Raggi's avatar
Roberto Raggi committed
148
149
150
    if (! scope)
        return _scope;

Roberto Raggi's avatar
Roberto Raggi committed
151
152
    std::swap(_scope, scope);
    return scope;
153
154
}

Roberto Raggi's avatar
Roberto Raggi committed
155
156
157
158
159
160
int Bind::switchVisibility(int visibility)
{
    std::swap(_visibility, visibility);
    return visibility;
}

161
162
163
164
165
166
int Bind::switchObjCVisibility(int visibility)
{
    std::swap(_objcVisibility, visibility);
    return visibility;
}

Roberto Raggi's avatar
Roberto Raggi committed
167
int Bind::switchMethodKey(int methodKey)
168
{
Roberto Raggi's avatar
Roberto Raggi committed
169
170
    std::swap(_methodKey, methodKey);
    return methodKey;
171
172
}

173
174
175
176
177
178
179
void Bind::operator()(TranslationUnitAST *ast, Namespace *globalNamespace)
{
    Scope *previousScope = switchScope(globalNamespace);
    translationUnit(ast);
    (void) switchScope(previousScope);
}

180
181
182
183
184
185
186
void Bind::operator()(DeclarationAST *ast, Scope *scope)
{
    Scope *previousScope = switchScope(scope);
    declaration(ast);
    (void) switchScope(previousScope);
}

187
188
189
190
191
192
193
void Bind::operator()(StatementAST *ast, Scope *scope)
{
    Scope *previousScope = switchScope(scope);
    statement(ast);
    (void) switchScope(previousScope);
}

194
FullySpecifiedType Bind::operator()(ExpressionAST *ast, Scope *scope)
195
196
{
    Scope *previousScope = switchScope(scope);
197
    FullySpecifiedType ty = expression(ast);
198
    (void) switchScope(previousScope);
199
200
201
202
203
204
205
206
207
    return ty;
}

FullySpecifiedType Bind::operator()(NewTypeIdAST *ast, Scope *scope)
{
    Scope *previousScope = switchScope(scope);
    FullySpecifiedType ty = newTypeId(ast);
    (void) switchScope(previousScope);
    return ty;
208
209
}

210
211
212
213
214
215
216
217
void Bind::statement(StatementAST *ast)
{
    accept(ast);
}

Bind::ExpressionTy Bind::expression(ExpressionAST *ast)
{
    ExpressionTy value = ExpressionTy();
Roberto Raggi's avatar
Roberto Raggi committed
218
    std::swap(_expression, value);
219
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
220
    std::swap(_expression, value);
221
222
223
224
225
226
227
228
229
230
231
    return value;
}

void Bind::declaration(DeclarationAST *ast)
{
    accept(ast);
}

const Name *Bind::name(NameAST *ast)
{
    const Name *value = 0;
Roberto Raggi's avatar
Roberto Raggi committed
232
    std::swap(_name, value);
233
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
234
    std::swap(_name, value);
235
236
237
238
239
240
    return value;
}

FullySpecifiedType Bind::specifier(SpecifierAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType value = init;
Roberto Raggi's avatar
Roberto Raggi committed
241
    std::swap(_type, value);
242
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
243
    std::swap(_type, value);
244
245
246
247
248
249
    return value;
}

FullySpecifiedType Bind::ptrOperator(PtrOperatorAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType value = init;
Roberto Raggi's avatar
Roberto Raggi committed
250
    std::swap(_type, value);
251
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
252
    std::swap(_type, value);
253
254
255
256
257
258
    return value;
}

FullySpecifiedType Bind::coreDeclarator(CoreDeclaratorAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType value = init;
Roberto Raggi's avatar
Roberto Raggi committed
259
    std::swap(_type, value);
260
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
261
    std::swap(_type, value);
262
263
264
265
266
267
    return value;
}

FullySpecifiedType Bind::postfixDeclarator(PostfixDeclaratorAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType value = init;
Roberto Raggi's avatar
Roberto Raggi committed
268
    std::swap(_type, value);
269
    accept(ast);
Roberto Raggi's avatar
Roberto Raggi committed
270
    std::swap(_type, value);
271
272
273
    return value;
}

274
275
276
277
278
279
280
281
282
283
284
285
286
bool Bind::preVisit(AST *)
{
    ++_depth;
    if (_depth > kMaxDepth)
        return false;
    return true;
}

void Bind::postVisit(AST *)
{
    --_depth;
}

287
288
289
290
// AST
bool Bind::visit(ObjCSelectorArgumentAST *ast)
{
    (void) ast;
291
    CPP_CHECK(!"unreachable");
292
293
294
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
295
const Name *Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasArg)
296
{
Roberto Raggi's avatar
Roberto Raggi committed
297
298
    if (! (ast && ast->name_token))
        return 0;
299

Roberto Raggi's avatar
Roberto Raggi committed
300
301
302
    if (ast->colon_token)
        *hasArg = true;

303
    return identifier(ast->name_token);
304
305
}

306
bool Bind::visit(GnuAttributeAST *ast)
307
308
{
    (void) ast;
309
    CPP_CHECK(!"unreachable");
310
311
312
    return false;
}

313
void Bind::attribute(GnuAttributeAST *ast)
314
315
316
317
318
{
    if (! ast)
        return;

    // unsigned identifier_token = ast->identifier_token;
319
320
321
322
323
324
325
    if (const Identifier *id = identifier(ast->identifier_token)) {
        if (id == control()->deprecatedId())
            _type.setDeprecated(true);
        else if (id == control()->unavailableId())
            _type.setUnavailable(true);
    }

326
327
328
329
330
331
332
333
334
335
336
    // unsigned lparen_token = ast->lparen_token;
    // unsigned tag_token = ast->tag_token;
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
        ExpressionTy value = this->expression(it->value);
    }
    // unsigned rparen_token = ast->rparen_token;
}

bool Bind::visit(DeclaratorAST *ast)
{
    (void) ast;
337
    CPP_CHECK(!"unreachable");
338
339
340
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
341
FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType &init, DeclaratorIdAST **declaratorId)
342
343
344
345
346
347
{
    FullySpecifiedType type = init;

    if (! ast)
        return type;

Roberto Raggi's avatar
Roberto Raggi committed
348
    std::swap(_declaratorId, declaratorId);
349
    bool isAuto = false;
350
351
    const bool cxx11Enabled = translationUnit()->languageFeatures().cxx11Enabled;
    if (cxx11Enabled)
352
353
        isAuto = type.isAuto();

354
355
    for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
        type = this->specifier(it->value, type);
356
357
        if (type.isAuto())
            isAuto = true;
358
359
360
361
362
363
364
    }
    for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
        type = this->ptrOperator(it->value, type);
    }
    for (PostfixDeclaratorListAST *it = ast->postfix_declarator_list; it; it = it->next) {
        type = this->postfixDeclarator(it->value, type);
    }
365
    type = this->coreDeclarator(ast->core_declarator, type);
366
367
    for (SpecifierListAST *it = ast->post_attribute_list; it; it = it->next) {
        type = this->specifier(it->value, type);
368
369
        if (type.isAuto())
            isAuto = true;
370
    }
371
372
    if (!type->isFunctionType()) {
        ExpressionTy initializer = this->expression(ast->initializer);
373
        if (cxx11Enabled && isAuto) {
374
375
376
            type = initializer;
            type.setAuto(true);
        }
377
378
    }

Roberto Raggi's avatar
Roberto Raggi committed
379
    std::swap(_declaratorId, declaratorId);
380
    return type;
381
382
383
384
385
}

bool Bind::visit(QtPropertyDeclarationItemAST *ast)
{
    (void) ast;
386
    CPP_CHECK(!"unreachable");
387
388
389
390
391
392
    return false;
}

bool Bind::visit(QtInterfaceNameAST *ast)
{
    (void) ast;
393
    CPP_CHECK(!"unreachable");
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    return false;
}

void Bind::qtInterfaceName(QtInterfaceNameAST *ast)
{
    if (! ast)
        return;

    /*const Name *interface_name =*/ this->name(ast->interface_name);
    for (NameListAST *it = ast->constraint_list; it; it = it->next) {
        /*const Name *value =*/ this->name(it->value);
    }
}

bool Bind::visit(BaseSpecifierAST *ast)
{
    (void) ast;
411
    CPP_CHECK(!"unreachable");
412
413
414
    return false;
}

415
void Bind::baseSpecifier(BaseSpecifierAST *ast, unsigned colon_token, Class *klass)
416
417
418
419
{
    if (! ast)
        return;

Roberto Raggi's avatar
Roberto Raggi committed
420
    unsigned sourceLocation = location(ast->name, ast->firstToken());
421
422
423
424
425
426
427
428
429
430
431
    if (! sourceLocation)
        sourceLocation = std::max(colon_token, klass->sourceLocation());

    const Name *baseClassName = this->name(ast->name);
    BaseClass *baseClass = control()->newBaseClass(sourceLocation, baseClassName);
    if (ast->virtual_token)
        baseClass->setVirtual(true);
    if (ast->access_specifier_token) {
        const int visibility = visibilityForAccessSpecifier(tokenKind(ast->access_specifier_token));
        baseClass->setVisibility(visibility); // ### well, not exactly.
    }
432
    klass->addBaseClass(baseClass);
433
    ast->symbol = baseClass;
434
435
436
437
438
}

bool Bind::visit(CtorInitializerAST *ast)
{
    (void) ast;
439
    CPP_CHECK(!"unreachable");
440
441
442
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
443
void Bind::ctorInitializer(CtorInitializerAST *ast, Function *fun)
444
445
446
447
448
449
{
    if (! ast)
        return;

    // unsigned colon_token = ast->colon_token;
    for (MemInitializerListAST *it = ast->member_initializer_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
450
        this->memInitializer(it->value, fun);
451
452
453
454
455
456
457
    }
    // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
}

bool Bind::visit(EnumeratorAST *ast)
{
    (void) ast;
458
    CPP_CHECK(!"unreachable");
459
460
461
    return false;
}

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
namespace {

bool isInteger(const StringLiteral *stringLiteral)
{
    const int size = stringLiteral->size();
    const char *chars = stringLiteral->chars();
    for (int i = 0; i < size; ++i) {
        if (!isdigit(chars[i]))
            return false;
    }
    return true;
}

bool stringLiteralToInt(const StringLiteral *stringLiteral, int *output)
{
    if (!output)
        return false;

    if (!isInteger(stringLiteral)) {
        *output = 0;
        return false;
    }

    std::stringstream ss(std::string(stringLiteral->chars(), stringLiteral->size()));
Nikolai Kosjar's avatar
Nikolai Kosjar committed
486
    const bool ok = !(ss >> *output).fail();
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
    if (!ok)
        *output = 0;

    return ok;
}

void calculateConstantValue(const Symbol *symbol, EnumeratorDeclaration *e, Control *control)
{
    if (symbol) {
        if (const Declaration *decl = symbol->asDeclaration()) {
            if (const EnumeratorDeclaration *previousEnumDecl = decl->asEnumeratorDeclarator()) {
                if (const StringLiteral *constantValue = previousEnumDecl->constantValue()) {
                    int constantValueAsInt = 0;
                    if (stringLiteralToInt(constantValue, &constantValueAsInt)) {
                        ++constantValueAsInt;
                        const std::string buffer = std::to_string(constantValueAsInt);
                        e->setConstantValue(control->stringLiteral(buffer.c_str(),
                                                                   unsigned(buffer.size())));
                    }
                }
            }
        }
    }
}

} // anonymous namespace

Roberto Raggi's avatar
Roberto Raggi committed
514
void Bind::enumerator(EnumeratorAST *ast, Enum *symbol)
515
{
Roberto Raggi's avatar
Roberto Raggi committed
516
517
    (void) symbol;

518
519
520
521
522
    if (! ast)
        return;

    // unsigned identifier_token = ast->identifier_token;
    // unsigned equal_token = ast->equal_token;
523
    /*ExpressionTy expression =*/ this->expression(ast->expression);
Roberto Raggi's avatar
Roberto Raggi committed
524
525

    if (ast->identifier_token) {
526
        const Name *name = identifier(ast->identifier_token);
527
        EnumeratorDeclaration *e = control()->newEnumeratorDeclaration(ast->identifier_token, name);
Roberto Raggi's avatar
Roberto Raggi committed
528
        e->setType(control()->integerType(IntegerType::Int)); // ### introduce IntegerType::Enumerator
529

530
        if (ExpressionAST *expr = ast->expression)
531
            e->setConstantValue(asStringLiteral(expr->firstToken(), expr->lastToken()));
532
533
534
535
        else if (!symbol->isEmpty())
            calculateConstantValue(*(symbol->lastMember()-1), e, control());
        else
            e->setConstantValue(control()->stringLiteral("0", 1));
536

Roberto Raggi's avatar
Roberto Raggi committed
537
538
        symbol->addMember(e);
    }
539
540
}

541
bool Bind::visit(DynamicExceptionSpecificationAST *ast)
542
543
{
    (void) ast;
544
    CPP_CHECK(!"unreachable");
545
546
547
548
549
550
551
552
553
554
    return false;
}

FullySpecifiedType Bind::exceptionSpecification(ExceptionSpecificationAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType type = init;

    if (! ast)
        return type;

555
556
557
558
559
560
561
562
563
    if (DynamicExceptionSpecificationAST *dyn = ast->asDynamicExceptionSpecification()) {
        // unsigned throw_token = ast->throw_token;
        // unsigned lparen_token = ast->lparen_token;
        // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
        for (ExpressionListAST *it = dyn->type_id_list; it; it = it->next) {
            /*ExpressionTy value =*/ this->expression(it->value);
        }
    } else if (NoExceptSpecificationAST *no = ast->asNoExceptSpecification()) {
        /*ExpressionTy value =*/ this->expression(no->expression);
564
565
566
567
568
569
570
571
    }
    // unsigned rparen_token = ast->rparen_token;
    return type;
}

bool Bind::visit(MemInitializerAST *ast)
{
    (void) ast;
572
    CPP_CHECK(!"unreachable");
573
574
575
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
576
void Bind::memInitializer(MemInitializerAST *ast, Function *fun)
577
578
579
580
581
{
    if (! ast)
        return;

    /*const Name *name =*/ this->name(ast->name);
Roberto Raggi's avatar
Roberto Raggi committed
582
583

    Scope *previousScope = switchScope(fun);
584
    this->expression(ast->expression);
Roberto Raggi's avatar
Roberto Raggi committed
585
    (void) switchScope(previousScope);
586
587
588
589
590
}

bool Bind::visit(NestedNameSpecifierAST *ast)
{
    (void) ast;
591
    CPP_CHECK(!"unreachable");
592
593
594
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
595
const Name *Bind::nestedNameSpecifier(NestedNameSpecifierAST *ast)
596
597
{
    if (! ast)
Roberto Raggi's avatar
Roberto Raggi committed
598
        return 0;
599

Roberto Raggi's avatar
Roberto Raggi committed
600
601
    const Name *class_or_namespace_name = this->name(ast->class_or_namespace_name);
    return class_or_namespace_name;
602
603
}

604
bool Bind::visit(ExpressionListParenAST *ast)
605
{
606
607
608
609
610
611
    // unsigned lparen_token = ast->lparen_token;
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
        /*ExpressionTy value =*/ this->expression(it->value);
    }
    // unsigned rparen_token = ast->rparen_token;

612
613
614
    return false;
}

615
void Bind::newPlacement(ExpressionListParenAST *ast)
616
617
618
619
620
621
622
623
624
625
626
627
628
629
{
    if (! ast)
        return;

    // unsigned lparen_token = ast->lparen_token;
    for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
        ExpressionTy value = this->expression(it->value);
    }
    // unsigned rparen_token = ast->rparen_token;
}

bool Bind::visit(NewArrayDeclaratorAST *ast)
{
    (void) ast;
630
    CPP_CHECK(!"unreachable");
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
    return false;
}

FullySpecifiedType Bind::newArrayDeclarator(NewArrayDeclaratorAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType type = init;

    if (! ast)
        return type;

    // unsigned lbracket_token = ast->lbracket_token;
    ExpressionTy expression = this->expression(ast->expression);
    // unsigned rbracket_token = ast->rbracket_token;
    return type;
}

bool Bind::visit(NewTypeIdAST *ast)
{
    (void) ast;
650
    CPP_CHECK(!"unreachable");
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    return false;
}

FullySpecifiedType Bind::newTypeId(NewTypeIdAST *ast)
{
    FullySpecifiedType type;

    if (! ast)
        return type;


    for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
    for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
        type = this->ptrOperator(it->value, type);
    }
    for (NewArrayDeclaratorListAST *it = ast->new_array_declarator_list; it; it = it->next) {
        type = this->newArrayDeclarator(it->value, type);
    }
    return type;
}

bool Bind::visit(OperatorAST *ast)
{
    (void) ast;
677
    CPP_CHECK(!"unreachable");
678
679
680
    return false;
}

681
OperatorNameId::Kind Bind::cppOperator(OperatorAST *ast)
682
{
Roberto Raggi's avatar
Roberto Raggi committed
683
684
    OperatorNameId::Kind kind = OperatorNameId::InvalidOp;

685
    if (! ast)
Roberto Raggi's avatar
Roberto Raggi committed
686
        return kind;
687
688
689
690

    // unsigned op_token = ast->op_token;
    // unsigned open_token = ast->open_token;
    // unsigned close_token = ast->close_token;
Roberto Raggi's avatar
Roberto Raggi committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

    switch (tokenKind(ast->op_token)) {
    case T_NEW:
        if (ast->open_token)
            kind = OperatorNameId::NewArrayOp;
        else
            kind = OperatorNameId::NewOp;
        break;

    case T_DELETE:
        if (ast->open_token)
            kind = OperatorNameId::DeleteArrayOp;
        else
            kind = OperatorNameId::DeleteOp;
        break;

    case T_PLUS:
        kind = OperatorNameId::PlusOp;
        break;

    case T_MINUS:
        kind = OperatorNameId::MinusOp;
        break;

    case T_STAR:
        kind = OperatorNameId::StarOp;
        break;

    case T_SLASH:
        kind = OperatorNameId::SlashOp;
        break;

    case T_PERCENT:
        kind = OperatorNameId::PercentOp;
        break;

    case T_CARET:
        kind = OperatorNameId::CaretOp;
        break;

    case T_AMPER:
        kind = OperatorNameId::AmpOp;
        break;

    case T_PIPE:
        kind = OperatorNameId::PipeOp;
        break;

    case T_TILDE:
        kind = OperatorNameId::TildeOp;
        break;

    case T_EXCLAIM:
        kind = OperatorNameId::ExclaimOp;
        break;

    case T_EQUAL:
        kind = OperatorNameId::EqualOp;
        break;

    case T_LESS:
        kind = OperatorNameId::LessOp;
        break;

    case T_GREATER:
        kind = OperatorNameId::GreaterOp;
        break;

    case T_PLUS_EQUAL:
        kind = OperatorNameId::PlusEqualOp;
        break;

    case T_MINUS_EQUAL:
        kind = OperatorNameId::MinusEqualOp;
        break;

    case T_STAR_EQUAL:
        kind = OperatorNameId::StarEqualOp;
        break;

    case T_SLASH_EQUAL:
        kind = OperatorNameId::SlashEqualOp;
        break;

    case T_PERCENT_EQUAL:
        kind = OperatorNameId::PercentEqualOp;
        break;

    case T_CARET_EQUAL:
        kind = OperatorNameId::CaretEqualOp;
        break;

    case T_AMPER_EQUAL:
        kind = OperatorNameId::AmpEqualOp;
        break;

    case T_PIPE_EQUAL:
        kind = OperatorNameId::PipeEqualOp;
        break;

    case T_LESS_LESS:
        kind = OperatorNameId::LessLessOp;
        break;

    case T_GREATER_GREATER:
        kind = OperatorNameId::GreaterGreaterOp;
        break;

    case T_LESS_LESS_EQUAL:
        kind = OperatorNameId::LessLessEqualOp;
        break;

    case T_GREATER_GREATER_EQUAL:
        kind = OperatorNameId::GreaterGreaterEqualOp;
        break;

    case T_EQUAL_EQUAL:
        kind = OperatorNameId::EqualEqualOp;
        break;

    case T_EXCLAIM_EQUAL:
        kind = OperatorNameId::ExclaimEqualOp;
        break;

    case T_LESS_EQUAL:
        kind = OperatorNameId::LessEqualOp;
        break;

    case T_GREATER_EQUAL:
        kind = OperatorNameId::GreaterEqualOp;
        break;

    case T_AMPER_AMPER:
        kind = OperatorNameId::AmpAmpOp;
        break;

    case T_PIPE_PIPE:
        kind = OperatorNameId::PipePipeOp;
        break;

    case T_PLUS_PLUS:
        kind = OperatorNameId::PlusPlusOp;
        break;

    case T_MINUS_MINUS:
        kind = OperatorNameId::MinusMinusOp;
        break;

    case T_COMMA:
        kind = OperatorNameId::CommaOp;
        break;

    case T_ARROW_STAR:
        kind = OperatorNameId::ArrowStarOp;
        break;

    case T_ARROW:
        kind = OperatorNameId::ArrowOp;
        break;

    case T_LPAREN:
        kind = OperatorNameId::FunctionCallOp;
        break;

    case T_LBRACKET:
        kind = OperatorNameId::ArrayAccessOp;
        break;

    default:
        kind = OperatorNameId::InvalidOp;
    } // switch

    return kind;
864
865
866
867
868
}

bool Bind::visit(ParameterDeclarationClauseAST *ast)
{
    (void) ast;
869
    CPP_CHECK(!"unreachable");
870
871
872
    return false;
}

873
void Bind::parameterDeclarationClause(ParameterDeclarationClauseAST *ast, unsigned lparen_token, Function *fun)
874
875
876
877
{
    if (! ast)
        return;

878
879
880
881
882
883
884
    if (! fun) {
        translationUnit()->warning(lparen_token, "undefined function");
        return;
    }

    Scope *previousScope = switchScope(fun);

Roberto Raggi's avatar
Roberto Raggi committed
885
    for (ParameterDeclarationListAST *it = ast->parameter_declaration_list; it; it = it->next) {
886
887
        this->declaration(it->value);
    }
888
889
890
891
892

    if (ast->dot_dot_dot_token)
        fun->setVariadic(true);

    (void) switchScope(previousScope);
893
894
895
896
897
}

bool Bind::visit(TranslationUnitAST *ast)
{
    (void) ast;
898
    CPP_CHECK(!"unreachable");
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
    return false;
}

void Bind::translationUnit(TranslationUnitAST *ast)
{
    if (! ast)
        return;

    for (DeclarationListAST *it = ast->declaration_list; it; it = it->next) {
        this->declaration(it->value);
    }
}

bool Bind::visit(ObjCProtocolRefsAST *ast)
{
    (void) ast;
915
    CPP_CHECK(!"unreachable");
916
917
918
    return false;
}

919
void Bind::objCProtocolRefs(ObjCProtocolRefsAST *ast, Symbol *objcClassOrProtocol)
920
921
922
923
924
{
    if (! ast)
        return;

    for (NameListAST *it = ast->identifier_list; it; it = it->next) {
925
926
927
928
929
930
        const Name *protocolName = this->name(it->value);
        ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(it->value->firstToken(), protocolName);
        if (ObjCClass *klass = objcClassOrProtocol->asObjCClass())
            klass->addProtocol(baseProtocol);
        else if (ObjCProtocol *proto = objcClassOrProtocol->asObjCProtocol())
            proto->addProtocol(baseProtocol);
931
932
933
934
935
936
    }
}

bool Bind::visit(ObjCMessageArgumentAST *ast)
{
    (void) ast;
937
    CPP_CHECK(!"unreachable");
938
939
940
941
942
943
944
945
946
947
948
949
950
951
    return false;
}

void Bind::objCMessageArgument(ObjCMessageArgumentAST *ast)
{
    if (! ast)
        return;

    ExpressionTy parameter_value_expression = this->expression(ast->parameter_value_expression);
}

bool Bind::visit(ObjCTypeNameAST *ast)
{
    (void) ast;
952
    CPP_CHECK(!"unreachable");
953
954
955
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
956
FullySpecifiedType Bind::objCTypeName(ObjCTypeNameAST *ast)
957
958
{
    if (! ast)
Roberto Raggi's avatar
Roberto Raggi committed
959
        return FullySpecifiedType();
960
961
962

    // unsigned type_qualifier_token = ast->type_qualifier_token;
    ExpressionTy type_id = this->expression(ast->type_id);
Roberto Raggi's avatar
Roberto Raggi committed
963
    return type_id;
964
965
966
967
968
}

bool Bind::visit(ObjCInstanceVariablesDeclarationAST *ast)
{
    (void) ast;
969
    CPP_CHECK(!"unreachable");
970
971
972
    return false;
}

973
void Bind::objCInstanceVariablesDeclaration(ObjCInstanceVariablesDeclarationAST *ast, ObjCClass *klass)
974
{
975
976
    (void) klass;

977
978
979
980
981
982
983
984
985
986
987
988
989
    if (! ast)
        return;

    // unsigned lbrace_token = ast->lbrace_token;
    for (DeclarationListAST *it = ast->instance_variable_list; it; it = it->next) {
        this->declaration(it->value);
    }
    // unsigned rbrace_token = ast->rbrace_token;
}

bool Bind::visit(ObjCPropertyAttributeAST *ast)
{
    (void) ast;
990
    CPP_CHECK(!"unreachable");
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
    return false;
}

void Bind::objCPropertyAttribute(ObjCPropertyAttributeAST *ast)
{
    if (! ast)
        return;

    // unsigned attribute_identifier_token = ast->attribute_identifier_token;
    // unsigned equals_token = ast->equals_token;
    /*const Name *method_selector =*/ this->name(ast->method_selector);
}

bool Bind::visit(ObjCMessageArgumentDeclarationAST *ast)
{
    (void) ast;
1007
    CPP_CHECK(!"unreachable");
1008
1009
1010
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1011
void Bind::objCMessageArgumentDeclaration(ObjCMessageArgumentDeclarationAST *ast, ObjCMethod *method)
1012
1013
1014
1015
{
    if (! ast)
        return;

Roberto Raggi's avatar
Roberto Raggi committed
1016
1017
    FullySpecifiedType type = this->objCTypeName(ast->type_name);

1018
1019
1020
    for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
Roberto Raggi's avatar
Roberto Raggi committed
1021
1022

    const Name *param_name = this->name(ast->param_name);
Roberto Raggi's avatar
Roberto Raggi committed
1023
    Argument *arg = control()->newArgument(location(ast->param_name, ast->firstToken()), param_name);
Roberto Raggi's avatar
Roberto Raggi committed
1024
1025
1026
    arg->setType(type);
    ast->argument = arg;
    method->addMember(arg);
1027
1028
1029
1030
1031
}

bool Bind::visit(ObjCMethodPrototypeAST *ast)
{
    (void) ast;
1032
    CPP_CHECK(!"unreachable");
1033
1034
1035
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
1036
ObjCMethod *Bind::objCMethodPrototype(ObjCMethodPrototypeAST *ast)
1037
1038
{
    if (! ast)
Roberto Raggi's avatar
Roberto Raggi committed
1039
        return 0;
1040
1041

    // unsigned method_type_token = ast->method_type_token;
Roberto Raggi's avatar
Roberto Raggi committed
1042
1043
1044
    FullySpecifiedType returnType = this->objCTypeName(ast->type_name);
    const Name *selector = this->name(ast->selector);

Roberto Raggi's avatar
Roberto Raggi committed
1045
    const unsigned sourceLocation = location(ast->selector, ast->firstToken());
Roberto Raggi's avatar
Roberto Raggi committed
1046
1047
1048
1049
1050
1051
1052
1053
1054
    ObjCMethod *method = control()->newObjCMethod(sourceLocation, selector);
    // ### set the offsets
    method->setReturnType(returnType);
    if (isObjCClassMethod(tokenKind(ast->method_type_token)))
        method->setStorage(Symbol::Static);
    method->setVisibility(_objcVisibility);
    ast->symbol = method;

    Scope *previousScope = switchScope(method);
1055
    for (ObjCMessageArgumentDeclarationListAST *it = ast->argument_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
1056
        this->objCMessageArgumentDeclaration(it->value, method);
1057
    }
Roberto Raggi's avatar
Roberto Raggi committed
1058
1059
1060
1061
1062
1063
    (void) switchScope(previousScope);

    if (ast->dot_dot_dot_token)
        method->setVariadic(true);

    FullySpecifiedType specifiers;
1064
    for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
Roberto Raggi's avatar
Roberto Raggi committed
1065
        specifiers = this->specifier(it->value, specifiers);
1066
    }
Roberto Raggi's avatar
Roberto Raggi committed
1067
    //setDeclSpecifiers(method, specifiers);
Roberto Raggi's avatar
Roberto Raggi committed
1068
1069

    return method;
1070
1071
1072
1073
1074
}

bool Bind::visit(ObjCSynthesizedPropertyAST *ast)
{
    (void) ast;
1075
    CPP_CHECK(!"unreachable");
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
    return false;
}

void Bind::objCSynthesizedProperty(ObjCSynthesizedPropertyAST *ast)
{
    if (! ast)
        return;

    // unsigned property_identifier_token = ast->property_identifier_token;
    // unsigned equals_token = ast->equals_token;
    // unsigned alias_identifier_token = ast->alias_identifier_token;
}

bool Bind::visit(LambdaIntroducerAST *ast)
{
    (void) ast;
1092
    CPP_CHECK(!"unreachable");
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
    return false;
}

void Bind::lambdaIntroducer(LambdaIntroducerAST *ast)
{
    if (! ast)
        return;

    // unsigned lbracket_token = ast->lbracket_token;
    this->lambdaCapture(ast->lambda_capture);
    // unsigned rbracket_token = ast->rbracket_token;
}

bool Bind::visit(LambdaCaptureAST *ast)
{
    (void) ast;
1109
    CPP_CHECK(!"unreachable");
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
    return false;
}

void Bind::lambdaCapture(LambdaCaptureAST *ast)
{
    if (! ast)
        return;

    // unsigned default_capture_token = ast->default_capture_token;
    for (CaptureListAST *it = ast->capture_list; it; it = it->next) {
        this->capture(it->value);
    }
}

bool Bind::visit(CaptureAST *ast)
{
    (void) ast;
1127
    CPP_CHECK(!"unreachable");
1128
1129
1130
1131
1132
1133
1134
1135
    return false;
}

void Bind::capture(CaptureAST *ast)
{
    if (! ast)
        return;

1136
    name(ast->identifier);
1137
1138
1139
1140
1141
}

bool Bind::visit(LambdaDeclaratorAST *ast)
{
    (void) ast;
1142
    CPP_CHECK(!"unreachable");
1143
1144
1145
    return false;
}

1146
Function *Bind::lambdaDeclarator(LambdaDeclaratorAST *ast)
1147
1148
{
    if (! ast)
1149
        return 0;
1150

1151
    Function *fun = control()->newFunction(0, 0);
1152
1153
    fun->setStartOffset(tokenAt(ast->firstToken()).utf16charsBegin());
    fun->setEndOffset(tokenAt(ast->lastToken() - 1).utf16charsEnd());
1154
1155

    FullySpecifiedType type;
1156
    if (ast->trailing_return_type)
1157
        type = this->trailingReturnType(ast->trailing_return_type, type);
1158
    ast->symbol = fun;
1159

1160
    // unsigned lparen_token = ast->lparen_token;
1161
    this->parameterDeclarationClause(ast->parameter_declaration_clause, ast->lparen_token, fun);
1162
1163
1164
1165
1166
    // unsigned rparen_token = ast->rparen_token;
    for (SpecifierListAST *it = ast->attributes; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
    // unsigned mutable_token = ast->mutable_token;
1167
1168
1169
1170
1171
    type = this->exceptionSpecification(ast->exception_specification, type);

    if (!type.isValid())
        type.setType(control()->voidType());
    fun->setReturnType(type);
1172
    return fun;
1173
1174
1175
1176
1177
}

bool Bind::visit(TrailingReturnTypeAST *ast)
{
    (void) ast;
1178
    CPP_CHECK(!"unreachable");
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
    return false;
}

FullySpecifiedType Bind::trailingReturnType(TrailingReturnTypeAST *ast, const FullySpecifiedType &init)
{
    FullySpecifiedType type = init;

    if (! ast)
        return type;

    // unsigned arrow_token = ast->arrow_token;
    for (SpecifierListAST *it = ast->attributes; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
Roberto Raggi's avatar
Roberto Raggi committed
1193
    for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1194
1195
        type = this->specifier(it->value, type);
    }
Roberto Raggi's avatar
Roberto Raggi committed
1196
1197
    DeclaratorIdAST *declaratorId = 0;
    type = this->declarator(ast->declarator, type, &declaratorId);
1198
1199
1200
    return type;
}

1201
const StringLiteral *Bind::asStringLiteral(unsigned firstToken, unsigned lastToken)
1202
1203
1204
1205
{
    std::string buffer;
    for (unsigned index = firstToken; index != lastToken; ++index) {
        const Token &tk = tokenAt(index);
1206
        if (index != firstToken && (tk.whitespace() || tk.newline()))
1207
1208
1209
            buffer += ' ';
        buffer += tk.spell();
    }
1210
    return control()->stringLiteral(buffer.c_str(), unsigned(buffer.size()));
1211
}
1212
1213
1214
1215

// StatementAST
bool Bind::visit(QtMemberDeclarationAST *ast)
{
1216
1217
1218
    const Name *name = 0;

    if (tokenKind(ast->q_token) == T_Q_D)
1219
        name = control()->identifier("d");
1220
    else
1221
        name = control()->identifier("q");
1222
1223
1224
1225
1226

    FullySpecifiedType declTy = this->expression(ast->type_id);

    if (tokenKind(ast->q_token) == T_Q_D) {
        if (NamedType *namedTy = declTy->asNamedType()) {
1227
            if (const Identifier *nameId = namedTy->name()->asNameId()) {
1228
1229
1230
1231
                std::string privateClass;
                privateClass += nameId->identifier()->chars();
                privateClass += "Private";

1232
                const Name *privName = control()->identifier(privateClass.c_str(), unsigned(privateClass.size()));
1233
1234
1235
1236
1237
1238
1239
1240
1241
                declTy.setType(control()->namedType(privName));
            }
        }
    }

    Declaration *symbol = control()->newDeclaration(/*generated*/ 0, name);
    symbol->setType(control()->pointerType(declTy));

    _scope->addMember(symbol);
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
    return false;
}

bool Bind::visit(CaseStatementAST *ast)
{
    ExpressionTy expression = this->expression(ast->expression);
    this->statement(ast->statement);
    return false;
}

bool Bind::visit(CompoundStatementAST *ast)
{
Roberto Raggi's avatar
Roberto Raggi committed
1254
    Block *block = control()->newBlock(ast->firstToken());
1255
    unsigned startScopeToken = ast->lbrace_token ? ast->lbrace_token : ast->firstToken();
1256
1257
    block->setStartOffset(tokenAt(startScopeToken).utf16charsEnd());
    block->setEndOffset(tokenAt(ast->lastToken() - 1).utf16charsEnd());
Roberto Raggi's avatar
Roberto Raggi committed
1258
1259
1260
    ast->symbol = block;
    _scope->addMember(block);
    Scope *previousScope = switchScope(block);
1261
1262
1263
    for (StatementListAST *it = ast->statement_list; it; it = it->next) {
        this->statement(it->value);
    }
Roberto Raggi's avatar
Roberto Raggi committed
1264
    (void) switchScope(previousScope);
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
    return false;
}

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

bool Bind::visit(DoStatementAST *ast)
{
    this->statement(ast->statement);
    ExpressionTy expression = this->expression(ast->expression);
    return false;
}

bool Bind::visit(ExpressionOrDeclarationStatementAST *ast)
{
    this->statement(ast->expression);
    this->statement(ast->declaration);
    return false;
}

bool Bind::visit(ExpressionStatementAST *ast)
{
    ExpressionTy expression = this->expression(ast->expression);
    // unsigned semicolon_token = ast->semicolon_token;
    return false;
}

bool Bind::visit(ForeachStatementAST *ast)
1296
1297
1298
{
    Block *block = control()->newBlock(ast->firstToken());
    const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1299
1300
    block->setStartOffset(tokenAt(startScopeToken).utf16charsEnd());
    block->setEndOffset(tokenAt(ast->lastToken()).utf16charsBegin());
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
    _scope->addMember(block);
    ast->symbol = block;

    Scope *previousScope = switchScope(block);

    FullySpecifiedType type;
    for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
    DeclaratorIdAST *declaratorId = 0;
    type = this->declarator(ast->declarator, type, &declaratorId);
    const StringLiteral *initializer = 0;
1313
    if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) {
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
        ExpressionTy exprType = this->expression(ast->expression);

        ArrayType* arrayType = 0;
        arrayType = exprType->asArrayType();

        if (arrayType != 0)
            type = arrayType->elementType();
        else if (ast->expression != 0) {
            unsigned startOfExpression = ast->expression->firstToken();
            unsigned endOfExpression = ast->expression->lastToken();
            const StringLiteral *sl = asStringLiteral(startOfExpression, endOfExpression);
            const std::string buff = std::string("*") + sl->chars() + ".begin()";
1326
            initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size()));
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
        }
    }

    if (declaratorId && declaratorId->name) {
        unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
        Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
        decl->setType(type);
        decl->setInitializer(initializer);
        block->addMember(decl);
    }

    /*ExpressionTy initializer =*/ this->expression(ast->initializer);
    /*ExpressionTy expression =*/ this->expression(ast->expression);
    this->statement(ast->statement);
    (void) switchScope(previousScope);
    return false;
}

bool Bind::visit(RangeBasedForStatementAST *ast)
1346
{
1347
    Block *block = control()->newBlock(ast->firstToken());
1348
    const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1349
1350
    block->setStartOffset(tokenAt(startScopeToken).utf16charsEnd());
    block->setEndOffset(tokenAt(ast->lastToken()).utf16charsBegin());
1351
1352
1353
1354
1355
    _scope->addMember(block);
    ast->symbol = block;

    Scope *previousScope = switchScope(block);

1356
1357
1358
1359
    FullySpecifiedType type;
    for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
        type = this->specifier(it->value, type);
    }
Roberto Raggi's avatar
Roberto Raggi committed
1360
1361
    DeclaratorIdAST *declaratorId = 0;
    type = this->declarator(ast->declarator, type, &declaratorId);
1362
    const StringLiteral *initializer = 0;
1363
    if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) {
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
        ExpressionTy exprType = this->expression(ast->expression);

        ArrayType* arrayType = 0;
        arrayType = exprType->asArrayType();

        if (arrayType != 0)
            type = arrayType->elementType();
        else if (ast->expression != 0) {
            unsigned startOfExpression = ast->expression->firstToken();
            unsigned endOfExpression = ast->expression->lastToken();
            const StringLiteral *sl = asStringLiteral(startOfExpression, endOfExpression);
            const std::string buff = std::string("*") + sl->chars() + ".begin()";
1376
            initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size()));
1377
1378
        }
    }
1379
1380

    if (declaratorId && declaratorId->name) {
Roberto Raggi's avatar
Roberto Raggi committed
1381
        unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
1382
1383
        Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
        decl->setType(type);