OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / cplusplus / Bind.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
33 //
34 // Permission is hereby granted, free of charge, to any person obtaining a copy
35 // of this software and associated documentation files (the "Software"), to deal
36 // in the Software without restriction, including without limitation the rights
37 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38 // copies of the Software, and to permit persons to whom the Software is
39 // furnished to do so, subject to the following conditions:
40 //
41 // The above copyright notice and this permission notice shall be included in
42 // all copies or substantial portions of the Software.
43 //
44 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
50 // THE SOFTWARE.
51
52 #include "Bind.h"
53 #include "AST.h"
54 #include "TranslationUnit.h"
55 #include "Control.h"
56 #include "Names.h"
57 #include "Symbols.h"
58 #include "CoreTypes.h"
59 #include "Literals.h"
60 #include "Scope.h"
61 #include <vector>
62 #include <string>
63 #include <memory>
64 #include <cassert>
65
66 using namespace CPlusPlus;
67
68 const int Bind::kMaxDepth(100);
69
70 Bind::Bind(TranslationUnit *unit)
71     : ASTVisitor(unit),
72       _scope(0),
73       _expression(0),
74       _name(0),
75       _declaratorId(0),
76       _visibility(Symbol::Public),
77       _objcVisibility(Symbol::Public),
78       _methodKey(Function::NormalMethod),
79       _skipFunctionBodies(false),
80       _depth(0)
81 {
82 }
83
84 bool Bind::skipFunctionBodies() const
85 {
86     return _skipFunctionBodies;
87 }
88
89 void Bind::setSkipFunctionBodies(bool skipFunctionBodies)
90 {
91     _skipFunctionBodies = skipFunctionBodies;
92 }
93
94 unsigned Bind::location(DeclaratorAST *ast, unsigned defaultLocation) const
95 {
96     if (! ast)
97         return defaultLocation;
98
99     else if (ast->core_declarator)
100         return location(ast->core_declarator, defaultLocation);
101
102     return ast->firstToken();
103 }
104
105 unsigned Bind::location(CoreDeclaratorAST *ast, unsigned defaultLocation) const
106 {
107     if (! ast)
108         return defaultLocation;
109
110     else if (NestedDeclaratorAST *nested = ast->asNestedDeclarator())
111         return location(nested->declarator, defaultLocation);
112
113     else if (DeclaratorIdAST *id = ast->asDeclaratorId())
114         return location(id->name, defaultLocation);
115
116     return ast->firstToken();
117 }
118
119 unsigned Bind::location(NameAST *name, unsigned defaultLocation) const
120 {
121     if (! name)
122         return defaultLocation;
123
124     else if (DestructorNameAST *dtor = name->asDestructorName())
125         return dtor->identifier_token;
126
127     else if (TemplateIdAST *templId = name->asTemplateId())
128         return templId->identifier_token;
129
130     else if (QualifiedNameAST *q = name->asQualifiedName()) {
131         if (q->unqualified_name)
132             return location(q->unqualified_name, defaultLocation);
133     }
134
135     return name->firstToken();
136 }
137
138 void Bind::setDeclSpecifiers(Symbol *symbol, const FullySpecifiedType &declSpecifiers)
139 {
140     if (! symbol)
141         return;
142
143     int storage = Symbol::NoStorage;
144
145     if (declSpecifiers.isFriend())
146         storage = Symbol::Friend;
147     else if (declSpecifiers.isAuto())
148         storage = Symbol::Auto;
149     else if (declSpecifiers.isRegister())
150         storage = Symbol::Register;
151     else if (declSpecifiers.isStatic())
152         storage = Symbol::Static;
153     else if (declSpecifiers.isExtern())
154         storage = Symbol::Extern;
155     else if (declSpecifiers.isMutable())
156         storage = Symbol::Mutable;
157     else if (declSpecifiers.isTypedef())
158         storage = Symbol::Typedef;
159
160     symbol->setStorage(storage);
161
162     if (Function *funTy = symbol->asFunction()) {
163         if (declSpecifiers.isVirtual()) {
164             funTy->setVirtual(true);
165         }
166     }
167
168     if (declSpecifiers.isDeprecated())
169         symbol->setDeprecated(true);
170
171     if (declSpecifiers.isUnavailable())
172         symbol->setUnavailable(true);
173 }
174
175 Scope *Bind::switchScope(Scope *scope)
176 {
177     if (! scope)
178         return _scope;
179
180     std::swap(_scope, scope);
181     return scope;
182 }
183
184 int Bind::switchVisibility(int visibility)
185 {
186     std::swap(_visibility, visibility);
187     return visibility;
188 }
189
190 int Bind::switchObjCVisibility(int visibility)
191 {
192     std::swap(_objcVisibility, visibility);
193     return visibility;
194 }
195
196 int Bind::switchMethodKey(int methodKey)
197 {
198     std::swap(_methodKey, methodKey);
199     return methodKey;
200 }
201
202 void Bind::operator()(TranslationUnitAST *ast, Namespace *globalNamespace)
203 {
204     Scope *previousScope = switchScope(globalNamespace);
205     translationUnit(ast);
206     (void) switchScope(previousScope);
207 }
208
209 void Bind::operator()(DeclarationAST *ast, Scope *scope)
210 {
211     Scope *previousScope = switchScope(scope);
212     declaration(ast);
213     (void) switchScope(previousScope);
214 }
215
216 FullySpecifiedType Bind::operator()(ExpressionAST *ast, Scope *scope)
217 {
218     Scope *previousScope = switchScope(scope);
219     FullySpecifiedType ty = expression(ast);
220     (void) switchScope(previousScope);
221     return ty;
222 }
223
224 FullySpecifiedType Bind::operator()(NewTypeIdAST *ast, Scope *scope)
225 {
226     Scope *previousScope = switchScope(scope);
227     FullySpecifiedType ty = newTypeId(ast);
228     (void) switchScope(previousScope);
229     return ty;
230 }
231
232 void Bind::statement(StatementAST *ast)
233 {
234     accept(ast);
235 }
236
237 Bind::ExpressionTy Bind::expression(ExpressionAST *ast)
238 {
239     ExpressionTy value = ExpressionTy();
240     std::swap(_expression, value);
241     accept(ast);
242     std::swap(_expression, value);
243     return value;
244 }
245
246 void Bind::declaration(DeclarationAST *ast)
247 {
248     accept(ast);
249 }
250
251 const Name *Bind::name(NameAST *ast)
252 {
253     const Name *value = 0;
254     std::swap(_name, value);
255     accept(ast);
256     std::swap(_name, value);
257     return value;
258 }
259
260 FullySpecifiedType Bind::specifier(SpecifierAST *ast, const FullySpecifiedType &init)
261 {
262     FullySpecifiedType value = init;
263     std::swap(_type, value);
264     accept(ast);
265     std::swap(_type, value);
266     return value;
267 }
268
269 FullySpecifiedType Bind::ptrOperator(PtrOperatorAST *ast, const FullySpecifiedType &init)
270 {
271     FullySpecifiedType value = init;
272     std::swap(_type, value);
273     accept(ast);
274     std::swap(_type, value);
275     return value;
276 }
277
278 FullySpecifiedType Bind::coreDeclarator(CoreDeclaratorAST *ast, const FullySpecifiedType &init)
279 {
280     FullySpecifiedType value = init;
281     std::swap(_type, value);
282     accept(ast);
283     std::swap(_type, value);
284     return value;
285 }
286
287 FullySpecifiedType Bind::postfixDeclarator(PostfixDeclaratorAST *ast, const FullySpecifiedType &init)
288 {
289     FullySpecifiedType value = init;
290     std::swap(_type, value);
291     accept(ast);
292     std::swap(_type, value);
293     return value;
294 }
295
296 bool Bind::preVisit(AST *)
297 {
298     ++_depth;
299     if (_depth > kMaxDepth)
300         return false;
301     return true;
302 }
303
304 void Bind::postVisit(AST *)
305 {
306     --_depth;
307 }
308
309 // AST
310 bool Bind::visit(ObjCSelectorArgumentAST *ast)
311 {
312     (void) ast;
313     assert(!"unreachable");
314     return false;
315 }
316
317 const Name *Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasArg)
318 {
319     if (! (ast && ast->name_token))
320         return 0;
321
322     if (ast->colon_token)
323         *hasArg = true;
324
325     return identifier(ast->name_token);
326 }
327
328 bool Bind::visit(AttributeAST *ast)
329 {
330     (void) ast;
331     assert(!"unreachable");
332     return false;
333 }
334
335 void Bind::attribute(AttributeAST *ast)
336 {
337     if (! ast)
338         return;
339
340     // unsigned identifier_token = ast->identifier_token;
341     if (const Identifier *id = identifier(ast->identifier_token)) {
342         if (id == control()->deprecatedId())
343             _type.setDeprecated(true);
344         else if (id == control()->unavailableId())
345             _type.setUnavailable(true);
346     }
347
348     // unsigned lparen_token = ast->lparen_token;
349     // unsigned tag_token = ast->tag_token;
350     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
351         ExpressionTy value = this->expression(it->value);
352     }
353     // unsigned rparen_token = ast->rparen_token;
354 }
355
356 bool Bind::visit(DeclaratorAST *ast)
357 {
358     (void) ast;
359     assert(!"unreachable");
360     return false;
361 }
362
363 FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType &init, DeclaratorIdAST **declaratorId)
364 {
365     FullySpecifiedType type = init;
366
367     if (! ast)
368         return type;
369
370     std::swap(_declaratorId, declaratorId);
371     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
372         type = this->specifier(it->value, type);
373     }
374     for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
375         type = this->ptrOperator(it->value, type);
376     }
377     for (PostfixDeclaratorListAST *it = ast->postfix_declarator_list; it; it = it->next) {
378         type = this->postfixDeclarator(it->value, type);
379     }
380     type = this->coreDeclarator(ast->core_declarator, type);
381     for (SpecifierListAST *it = ast->post_attribute_list; it; it = it->next) {
382         type = this->specifier(it->value, type);
383     }
384     // unsigned equals_token = ast->equals_token;
385     ExpressionTy initializer = this->expression(ast->initializer);
386     std::swap(_declaratorId, declaratorId);
387     return type;
388 }
389
390 bool Bind::visit(QtPropertyDeclarationItemAST *ast)
391 {
392     (void) ast;
393     assert(!"unreachable");
394     return false;
395 }
396
397 bool Bind::visit(QtInterfaceNameAST *ast)
398 {
399     (void) ast;
400     assert(!"unreachable");
401     return false;
402 }
403
404 void Bind::qtInterfaceName(QtInterfaceNameAST *ast)
405 {
406     if (! ast)
407         return;
408
409     /*const Name *interface_name =*/ this->name(ast->interface_name);
410     for (NameListAST *it = ast->constraint_list; it; it = it->next) {
411         /*const Name *value =*/ this->name(it->value);
412     }
413 }
414
415 bool Bind::visit(BaseSpecifierAST *ast)
416 {
417     (void) ast;
418     assert(!"unreachable");
419     return false;
420 }
421
422 void Bind::baseSpecifier(BaseSpecifierAST *ast, unsigned colon_token, Class *klass)
423 {
424     if (! ast)
425         return;
426
427     unsigned sourceLocation = location(ast->name, ast->firstToken());
428     if (! sourceLocation)
429         sourceLocation = std::max(colon_token, klass->sourceLocation());
430
431     const Name *baseClassName = this->name(ast->name);
432     BaseClass *baseClass = control()->newBaseClass(sourceLocation, baseClassName);
433     if (ast->virtual_token)
434         baseClass->setVirtual(true);
435     if (ast->access_specifier_token) {
436         const int visibility = visibilityForAccessSpecifier(tokenKind(ast->access_specifier_token));
437         baseClass->setVisibility(visibility); // ### well, not exactly.
438     }
439     klass->addBaseClass(baseClass);
440     ast->symbol = baseClass;
441 }
442
443 bool Bind::visit(CtorInitializerAST *ast)
444 {
445     (void) ast;
446     assert(!"unreachable");
447     return false;
448 }
449
450 void Bind::ctorInitializer(CtorInitializerAST *ast, Function *fun)
451 {
452     if (! ast)
453         return;
454
455     // unsigned colon_token = ast->colon_token;
456     for (MemInitializerListAST *it = ast->member_initializer_list; it; it = it->next) {
457         this->memInitializer(it->value, fun);
458     }
459     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
460 }
461
462 bool Bind::visit(EnumeratorAST *ast)
463 {
464     (void) ast;
465     assert(!"unreachable");
466     return false;
467 }
468
469 void Bind::enumerator(EnumeratorAST *ast, Enum *symbol)
470 {
471     (void) symbol;
472
473     if (! ast)
474         return;
475
476     // unsigned identifier_token = ast->identifier_token;
477     // unsigned equal_token = ast->equal_token;
478     ExpressionTy expression = this->expression(ast->expression);
479
480     if (ast->identifier_token) {
481         const Name *name = identifier(ast->identifier_token);
482         Declaration *e = control()->newDeclaration(ast->identifier_token, name);
483         e->setType(control()->integerType(IntegerType::Int)); // ### introduce IntegerType::Enumerator
484         symbol->addMember(e);
485     }
486 }
487
488 bool Bind::visit(ExceptionSpecificationAST *ast)
489 {
490     (void) ast;
491     assert(!"unreachable");
492     return false;
493 }
494
495 FullySpecifiedType Bind::exceptionSpecification(ExceptionSpecificationAST *ast, const FullySpecifiedType &init)
496 {
497     FullySpecifiedType type = init;
498
499     if (! ast)
500         return type;
501
502     // unsigned throw_token = ast->throw_token;
503     // unsigned lparen_token = ast->lparen_token;
504     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
505     for (ExpressionListAST *it = ast->type_id_list; it; it = it->next) {
506         ExpressionTy value = this->expression(it->value);
507     }
508     // unsigned rparen_token = ast->rparen_token;
509     return type;
510 }
511
512 bool Bind::visit(MemInitializerAST *ast)
513 {
514     (void) ast;
515     assert(!"unreachable");
516     return false;
517 }
518
519 void Bind::memInitializer(MemInitializerAST *ast, Function *fun)
520 {
521     if (! ast)
522         return;
523
524     /*const Name *name =*/ this->name(ast->name);
525
526     Scope *previousScope = switchScope(fun);
527     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
528         /*ExpressionTy value =*/ this->expression(it->value);
529     }
530     (void) switchScope(previousScope);
531 }
532
533 bool Bind::visit(NestedNameSpecifierAST *ast)
534 {
535     (void) ast;
536     assert(!"unreachable");
537     return false;
538 }
539
540 const Name *Bind::nestedNameSpecifier(NestedNameSpecifierAST *ast)
541 {
542     if (! ast)
543         return 0;
544
545     const Name *class_or_namespace_name = this->name(ast->class_or_namespace_name);
546     return class_or_namespace_name;
547 }
548
549 bool Bind::visit(NewPlacementAST *ast)
550 {
551     (void) ast;
552     assert(!"unreachable");
553     return false;
554 }
555
556 void Bind::newPlacement(NewPlacementAST *ast)
557 {
558     if (! ast)
559         return;
560
561     // unsigned lparen_token = ast->lparen_token;
562     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
563         ExpressionTy value = this->expression(it->value);
564     }
565     // unsigned rparen_token = ast->rparen_token;
566 }
567
568 bool Bind::visit(NewArrayDeclaratorAST *ast)
569 {
570     (void) ast;
571     assert(!"unreachable");
572     return false;
573 }
574
575 FullySpecifiedType Bind::newArrayDeclarator(NewArrayDeclaratorAST *ast, const FullySpecifiedType &init)
576 {
577     FullySpecifiedType type = init;
578
579     if (! ast)
580         return type;
581
582     // unsigned lbracket_token = ast->lbracket_token;
583     ExpressionTy expression = this->expression(ast->expression);
584     // unsigned rbracket_token = ast->rbracket_token;
585     return type;
586 }
587
588 bool Bind::visit(NewInitializerAST *ast)
589 {
590     (void) ast;
591     assert(!"unreachable");
592     return false;
593 }
594
595 void Bind::newInitializer(NewInitializerAST *ast)
596 {
597     if (! ast)
598         return;
599
600     // unsigned lparen_token = ast->lparen_token;
601     ExpressionTy expression = this->expression(ast->expression);
602     // unsigned rparen_token = ast->rparen_token;
603 }
604
605 bool Bind::visit(NewTypeIdAST *ast)
606 {
607     (void) ast;
608     assert(!"unreachable");
609     return false;
610 }
611
612 FullySpecifiedType Bind::newTypeId(NewTypeIdAST *ast)
613 {
614     FullySpecifiedType type;
615
616     if (! ast)
617         return type;
618
619
620     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
621         type = this->specifier(it->value, type);
622     }
623     for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
624         type = this->ptrOperator(it->value, type);
625     }
626     for (NewArrayDeclaratorListAST *it = ast->new_array_declarator_list; it; it = it->next) {
627         type = this->newArrayDeclarator(it->value, type);
628     }
629     return type;
630 }
631
632 bool Bind::visit(OperatorAST *ast)
633 {
634     (void) ast;
635     assert(!"unreachable");
636     return false;
637 }
638
639 OperatorNameId::Kind Bind::cppOperator(OperatorAST *ast)
640 {
641     OperatorNameId::Kind kind = OperatorNameId::InvalidOp;
642
643     if (! ast)
644         return kind;
645
646     // unsigned op_token = ast->op_token;
647     // unsigned open_token = ast->open_token;
648     // unsigned close_token = ast->close_token;
649
650     switch (tokenKind(ast->op_token)) {
651     case T_NEW:
652         if (ast->open_token)
653             kind = OperatorNameId::NewArrayOp;
654         else
655             kind = OperatorNameId::NewOp;
656         break;
657
658     case T_DELETE:
659         if (ast->open_token)
660             kind = OperatorNameId::DeleteArrayOp;
661         else
662             kind = OperatorNameId::DeleteOp;
663         break;
664
665     case T_PLUS:
666         kind = OperatorNameId::PlusOp;
667         break;
668
669     case T_MINUS:
670         kind = OperatorNameId::MinusOp;
671         break;
672
673     case T_STAR:
674         kind = OperatorNameId::StarOp;
675         break;
676
677     case T_SLASH:
678         kind = OperatorNameId::SlashOp;
679         break;
680
681     case T_PERCENT:
682         kind = OperatorNameId::PercentOp;
683         break;
684
685     case T_CARET:
686         kind = OperatorNameId::CaretOp;
687         break;
688
689     case T_AMPER:
690         kind = OperatorNameId::AmpOp;
691         break;
692
693     case T_PIPE:
694         kind = OperatorNameId::PipeOp;
695         break;
696
697     case T_TILDE:
698         kind = OperatorNameId::TildeOp;
699         break;
700
701     case T_EXCLAIM:
702         kind = OperatorNameId::ExclaimOp;
703         break;
704
705     case T_EQUAL:
706         kind = OperatorNameId::EqualOp;
707         break;
708
709     case T_LESS:
710         kind = OperatorNameId::LessOp;
711         break;
712
713     case T_GREATER:
714         kind = OperatorNameId::GreaterOp;
715         break;
716
717     case T_PLUS_EQUAL:
718         kind = OperatorNameId::PlusEqualOp;
719         break;
720
721     case T_MINUS_EQUAL:
722         kind = OperatorNameId::MinusEqualOp;
723         break;
724
725     case T_STAR_EQUAL:
726         kind = OperatorNameId::StarEqualOp;
727         break;
728
729     case T_SLASH_EQUAL:
730         kind = OperatorNameId::SlashEqualOp;
731         break;
732
733     case T_PERCENT_EQUAL:
734         kind = OperatorNameId::PercentEqualOp;
735         break;
736
737     case T_CARET_EQUAL:
738         kind = OperatorNameId::CaretEqualOp;
739         break;
740
741     case T_AMPER_EQUAL:
742         kind = OperatorNameId::AmpEqualOp;
743         break;
744
745     case T_PIPE_EQUAL:
746         kind = OperatorNameId::PipeEqualOp;
747         break;
748
749     case T_LESS_LESS:
750         kind = OperatorNameId::LessLessOp;
751         break;
752
753     case T_GREATER_GREATER:
754         kind = OperatorNameId::GreaterGreaterOp;
755         break;
756
757     case T_LESS_LESS_EQUAL:
758         kind = OperatorNameId::LessLessEqualOp;
759         break;
760
761     case T_GREATER_GREATER_EQUAL:
762         kind = OperatorNameId::GreaterGreaterEqualOp;
763         break;
764
765     case T_EQUAL_EQUAL:
766         kind = OperatorNameId::EqualEqualOp;
767         break;
768
769     case T_EXCLAIM_EQUAL:
770         kind = OperatorNameId::ExclaimEqualOp;
771         break;
772
773     case T_LESS_EQUAL:
774         kind = OperatorNameId::LessEqualOp;
775         break;
776
777     case T_GREATER_EQUAL:
778         kind = OperatorNameId::GreaterEqualOp;
779         break;
780
781     case T_AMPER_AMPER:
782         kind = OperatorNameId::AmpAmpOp;
783         break;
784
785     case T_PIPE_PIPE:
786         kind = OperatorNameId::PipePipeOp;
787         break;
788
789     case T_PLUS_PLUS:
790         kind = OperatorNameId::PlusPlusOp;
791         break;
792
793     case T_MINUS_MINUS:
794         kind = OperatorNameId::MinusMinusOp;
795         break;
796
797     case T_COMMA:
798         kind = OperatorNameId::CommaOp;
799         break;
800
801     case T_ARROW_STAR:
802         kind = OperatorNameId::ArrowStarOp;
803         break;
804
805     case T_ARROW:
806         kind = OperatorNameId::ArrowOp;
807         break;
808
809     case T_LPAREN:
810         kind = OperatorNameId::FunctionCallOp;
811         break;
812
813     case T_LBRACKET:
814         kind = OperatorNameId::ArrayAccessOp;
815         break;
816
817     default:
818         kind = OperatorNameId::InvalidOp;
819     } // switch
820
821     return kind;
822 }
823
824 bool Bind::visit(ParameterDeclarationClauseAST *ast)
825 {
826     (void) ast;
827     assert(!"unreachable");
828     return false;
829 }
830
831 void Bind::parameterDeclarationClause(ParameterDeclarationClauseAST *ast, unsigned lparen_token, Function *fun)
832 {
833     if (! ast)
834         return;
835
836     if (! fun) {
837         translationUnit()->warning(lparen_token, "undefined function");
838         return;
839     }
840
841     Scope *previousScope = switchScope(fun);
842
843     for (ParameterDeclarationListAST *it = ast->parameter_declaration_list; it; it = it->next) {
844         this->declaration(it->value);
845     }
846
847     if (ast->dot_dot_dot_token)
848         fun->setVariadic(true);
849
850     (void) switchScope(previousScope);
851 }
852
853 bool Bind::visit(TranslationUnitAST *ast)
854 {
855     (void) ast;
856     assert(!"unreachable");
857     return false;
858 }
859
860 void Bind::translationUnit(TranslationUnitAST *ast)
861 {
862     if (! ast)
863         return;
864
865     for (DeclarationListAST *it = ast->declaration_list; it; it = it->next) {
866         this->declaration(it->value);
867     }
868 }
869
870 bool Bind::visit(ObjCProtocolRefsAST *ast)
871 {
872     (void) ast;
873     assert(!"unreachable");
874     return false;
875 }
876
877 void Bind::objCProtocolRefs(ObjCProtocolRefsAST *ast, Symbol *objcClassOrProtocol)
878 {
879     if (! ast)
880         return;
881
882     for (NameListAST *it = ast->identifier_list; it; it = it->next) {
883         const Name *protocolName = this->name(it->value);
884         ObjCBaseProtocol *baseProtocol = control()->newObjCBaseProtocol(it->value->firstToken(), protocolName);
885         if (ObjCClass *klass = objcClassOrProtocol->asObjCClass())
886             klass->addProtocol(baseProtocol);
887         else if (ObjCProtocol *proto = objcClassOrProtocol->asObjCProtocol())
888             proto->addProtocol(baseProtocol);
889     }
890 }
891
892 bool Bind::visit(ObjCMessageArgumentAST *ast)
893 {
894     (void) ast;
895     assert(!"unreachable");
896     return false;
897 }
898
899 void Bind::objCMessageArgument(ObjCMessageArgumentAST *ast)
900 {
901     if (! ast)
902         return;
903
904     ExpressionTy parameter_value_expression = this->expression(ast->parameter_value_expression);
905 }
906
907 bool Bind::visit(ObjCTypeNameAST *ast)
908 {
909     (void) ast;
910     assert(!"unreachable");
911     return false;
912 }
913
914 FullySpecifiedType Bind::objCTypeName(ObjCTypeNameAST *ast)
915 {
916     if (! ast)
917         return FullySpecifiedType();
918
919     // unsigned type_qualifier_token = ast->type_qualifier_token;
920     ExpressionTy type_id = this->expression(ast->type_id);
921     return type_id;
922 }
923
924 bool Bind::visit(ObjCInstanceVariablesDeclarationAST *ast)
925 {
926     (void) ast;
927     assert(!"unreachable");
928     return false;
929 }
930
931 void Bind::objCInstanceVariablesDeclaration(ObjCInstanceVariablesDeclarationAST *ast, ObjCClass *klass)
932 {
933     (void) klass;
934
935     if (! ast)
936         return;
937
938     // unsigned lbrace_token = ast->lbrace_token;
939     for (DeclarationListAST *it = ast->instance_variable_list; it; it = it->next) {
940         this->declaration(it->value);
941     }
942     // unsigned rbrace_token = ast->rbrace_token;
943 }
944
945 bool Bind::visit(ObjCPropertyAttributeAST *ast)
946 {
947     (void) ast;
948     assert(!"unreachable");
949     return false;
950 }
951
952 void Bind::objCPropertyAttribute(ObjCPropertyAttributeAST *ast)
953 {
954     if (! ast)
955         return;
956
957     // unsigned attribute_identifier_token = ast->attribute_identifier_token;
958     // unsigned equals_token = ast->equals_token;
959     /*const Name *method_selector =*/ this->name(ast->method_selector);
960 }
961
962 bool Bind::visit(ObjCMessageArgumentDeclarationAST *ast)
963 {
964     (void) ast;
965     assert(!"unreachable");
966     return false;
967 }
968
969 void Bind::objCMessageArgumentDeclaration(ObjCMessageArgumentDeclarationAST *ast, ObjCMethod *method)
970 {
971     if (! ast)
972         return;
973
974     FullySpecifiedType type = this->objCTypeName(ast->type_name);
975
976     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
977         type = this->specifier(it->value, type);
978     }
979
980     const Name *param_name = this->name(ast->param_name);
981     Argument *arg = control()->newArgument(location(ast->param_name, ast->firstToken()), param_name);
982     arg->setType(type);
983     ast->argument = arg;
984     method->addMember(arg);
985 }
986
987 bool Bind::visit(ObjCMethodPrototypeAST *ast)
988 {
989     (void) ast;
990     assert(!"unreachable");
991     return false;
992 }
993
994 ObjCMethod *Bind::objCMethodPrototype(ObjCMethodPrototypeAST *ast)
995 {
996     if (! ast)
997         return 0;
998
999     // unsigned method_type_token = ast->method_type_token;
1000     FullySpecifiedType returnType = this->objCTypeName(ast->type_name);
1001     const Name *selector = this->name(ast->selector);
1002
1003     const unsigned sourceLocation = location(ast->selector, ast->firstToken());
1004     ObjCMethod *method = control()->newObjCMethod(sourceLocation, selector);
1005     // ### set the offsets
1006     method->setReturnType(returnType);
1007     if (isObjCClassMethod(tokenKind(ast->method_type_token)))
1008         method->setStorage(Symbol::Static);
1009     method->setVisibility(_objcVisibility);
1010     ast->symbol = method;
1011
1012     Scope *previousScope = switchScope(method);
1013     for (ObjCMessageArgumentDeclarationListAST *it = ast->argument_list; it; it = it->next) {
1014         this->objCMessageArgumentDeclaration(it->value, method);
1015     }
1016     (void) switchScope(previousScope);
1017
1018     if (ast->dot_dot_dot_token)
1019         method->setVariadic(true);
1020
1021     FullySpecifiedType specifiers;
1022     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
1023         specifiers = this->specifier(it->value, specifiers);
1024     }
1025     //setDeclSpecifiers(method, specifiers);
1026
1027     return method;
1028 }
1029
1030 bool Bind::visit(ObjCSynthesizedPropertyAST *ast)
1031 {
1032     (void) ast;
1033     assert(!"unreachable");
1034     return false;
1035 }
1036
1037 void Bind::objCSynthesizedProperty(ObjCSynthesizedPropertyAST *ast)
1038 {
1039     if (! ast)
1040         return;
1041
1042     // unsigned property_identifier_token = ast->property_identifier_token;
1043     // unsigned equals_token = ast->equals_token;
1044     // unsigned alias_identifier_token = ast->alias_identifier_token;
1045 }
1046
1047 bool Bind::visit(LambdaIntroducerAST *ast)
1048 {
1049     (void) ast;
1050     assert(!"unreachable");
1051     return false;
1052 }
1053
1054 void Bind::lambdaIntroducer(LambdaIntroducerAST *ast)
1055 {
1056     if (! ast)
1057         return;
1058
1059     // unsigned lbracket_token = ast->lbracket_token;
1060     this->lambdaCapture(ast->lambda_capture);
1061     // unsigned rbracket_token = ast->rbracket_token;
1062 }
1063
1064 bool Bind::visit(LambdaCaptureAST *ast)
1065 {
1066     (void) ast;
1067     assert(!"unreachable");
1068     return false;
1069 }
1070
1071 void Bind::lambdaCapture(LambdaCaptureAST *ast)
1072 {
1073     if (! ast)
1074         return;
1075
1076     // unsigned default_capture_token = ast->default_capture_token;
1077     for (CaptureListAST *it = ast->capture_list; it; it = it->next) {
1078         this->capture(it->value);
1079     }
1080 }
1081
1082 bool Bind::visit(CaptureAST *ast)
1083 {
1084     (void) ast;
1085     assert(!"unreachable");
1086     return false;
1087 }
1088
1089 void Bind::capture(CaptureAST *ast)
1090 {
1091     if (! ast)
1092         return;
1093
1094 }
1095
1096 bool Bind::visit(LambdaDeclaratorAST *ast)
1097 {
1098     (void) ast;
1099     assert(!"unreachable");
1100     return false;
1101 }
1102
1103 void Bind::lambdaDeclarator(LambdaDeclaratorAST *ast)
1104 {
1105     if (! ast)
1106         return;
1107
1108
1109     Function *fun = 0; // ### implement me
1110
1111     // unsigned lparen_token = ast->lparen_token;
1112     FullySpecifiedType type;
1113     this->parameterDeclarationClause(ast->parameter_declaration_clause, ast->lparen_token, fun);
1114     // unsigned rparen_token = ast->rparen_token;
1115     for (SpecifierListAST *it = ast->attributes; it; it = it->next) {
1116         type = this->specifier(it->value, type);
1117     }
1118     // unsigned mutable_token = ast->mutable_token;
1119     type = this->exceptionSpecification(ast->exception_specification, type);
1120     type = this->trailingReturnType(ast->trailing_return_type, type);
1121 }
1122
1123 bool Bind::visit(TrailingReturnTypeAST *ast)
1124 {
1125     (void) ast;
1126     assert(!"unreachable");
1127     return false;
1128 }
1129
1130 FullySpecifiedType Bind::trailingReturnType(TrailingReturnTypeAST *ast, const FullySpecifiedType &init)
1131 {
1132     FullySpecifiedType type = init;
1133
1134     if (! ast)
1135         return type;
1136
1137     // unsigned arrow_token = ast->arrow_token;
1138     for (SpecifierListAST *it = ast->attributes; it; it = it->next) {
1139         type = this->specifier(it->value, type);
1140     }
1141     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1142         type = this->specifier(it->value, type);
1143     }
1144     DeclaratorIdAST *declaratorId = 0;
1145     type = this->declarator(ast->declarator, type, &declaratorId);
1146     return type;
1147 }
1148
1149
1150 // StatementAST
1151 bool Bind::visit(QtMemberDeclarationAST *ast)
1152 {
1153     const Name *name = 0;
1154
1155     if (tokenKind(ast->q_token) == T_Q_D)
1156         name = control()->identifier("d");
1157     else
1158         name = control()->identifier("q");
1159
1160     FullySpecifiedType declTy = this->expression(ast->type_id);
1161
1162     if (tokenKind(ast->q_token) == T_Q_D) {
1163         if (NamedType *namedTy = declTy->asNamedType()) {
1164             if (const Identifier *nameId = namedTy->name()->asNameId()) {
1165                 std::string privateClass;
1166                 privateClass += nameId->identifier()->chars();
1167                 privateClass += "Private";
1168
1169                 const Name *privName = control()->identifier(privateClass.c_str(), privateClass.size());
1170                 declTy.setType(control()->namedType(privName));
1171             }
1172         }
1173     }
1174
1175     Declaration *symbol = control()->newDeclaration(/*generated*/ 0, name);
1176     symbol->setType(control()->pointerType(declTy));
1177
1178     _scope->addMember(symbol);
1179     return false;
1180 }
1181
1182 bool Bind::visit(CaseStatementAST *ast)
1183 {
1184     ExpressionTy expression = this->expression(ast->expression);
1185     this->statement(ast->statement);
1186     return false;
1187 }
1188
1189 bool Bind::visit(CompoundStatementAST *ast)
1190 {
1191     Block *block = control()->newBlock(ast->firstToken());
1192     unsigned startScopeToken = ast->lbrace_token ? ast->lbrace_token : ast->firstToken();
1193     block->setStartOffset(tokenAt(startScopeToken).end());
1194     block->setEndOffset(tokenAt(ast->lastToken() - 1).end());
1195     ast->symbol = block;
1196     _scope->addMember(block);
1197     Scope *previousScope = switchScope(block);
1198     for (StatementListAST *it = ast->statement_list; it; it = it->next) {
1199         this->statement(it->value);
1200     }
1201     (void) switchScope(previousScope);
1202     return false;
1203 }
1204
1205 bool Bind::visit(DeclarationStatementAST *ast)
1206 {
1207     this->declaration(ast->declaration);
1208     return false;
1209 }
1210
1211 bool Bind::visit(DoStatementAST *ast)
1212 {
1213     this->statement(ast->statement);
1214     ExpressionTy expression = this->expression(ast->expression);
1215     return false;
1216 }
1217
1218 bool Bind::visit(ExpressionOrDeclarationStatementAST *ast)
1219 {
1220     this->statement(ast->expression);
1221     this->statement(ast->declaration);
1222     return false;
1223 }
1224
1225 bool Bind::visit(ExpressionStatementAST *ast)
1226 {
1227     ExpressionTy expression = this->expression(ast->expression);
1228     // unsigned semicolon_token = ast->semicolon_token;
1229     return false;
1230 }
1231
1232 bool Bind::visit(ForeachStatementAST *ast)
1233 {
1234     Block *block = control()->newBlock(ast->firstToken());
1235     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1236     block->setStartOffset(tokenAt(startScopeToken).end());
1237     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1238     _scope->addMember(block);
1239     ast->symbol = block;
1240
1241     Scope *previousScope = switchScope(block);
1242
1243     FullySpecifiedType type;
1244     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1245         type = this->specifier(it->value, type);
1246     }
1247     DeclaratorIdAST *declaratorId = 0;
1248     type = this->declarator(ast->declarator, type, &declaratorId);
1249
1250     if (declaratorId && declaratorId->name) {
1251         unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
1252         Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
1253         decl->setType(type);
1254         block->addMember(decl);
1255     }
1256
1257     /*ExpressionTy initializer =*/ this->expression(ast->initializer);
1258     /*ExpressionTy expression =*/ this->expression(ast->expression);
1259     this->statement(ast->statement);
1260     (void) switchScope(previousScope);
1261     return false;
1262 }
1263
1264 bool Bind::visit(ForStatementAST *ast)
1265 {
1266     Block *block = control()->newBlock(ast->firstToken());
1267     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1268     block->setStartOffset(tokenAt(startScopeToken).end());
1269     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1270     _scope->addMember(block);
1271     ast->symbol = block;
1272
1273     Scope *previousScope = switchScope(block);
1274     this->statement(ast->initializer);
1275     /*ExpressionTy condition =*/ this->expression(ast->condition);
1276     // unsigned semicolon_token = ast->semicolon_token;
1277     /*ExpressionTy expression =*/ this->expression(ast->expression);
1278     // unsigned rparen_token = ast->rparen_token;
1279     this->statement(ast->statement);
1280     (void) switchScope(previousScope);
1281     return false;
1282 }
1283
1284 bool Bind::visit(IfStatementAST *ast)
1285 {
1286     Block *block = control()->newBlock(ast->firstToken());
1287     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1288     block->setStartOffset(tokenAt(startScopeToken).end());
1289     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1290     _scope->addMember(block);
1291     ast->symbol = block;
1292
1293     Scope *previousScope = switchScope(block);
1294     /*ExpressionTy condition =*/ this->expression(ast->condition);
1295     this->statement(ast->statement);
1296     this->statement(ast->else_statement);
1297     (void) switchScope(previousScope);
1298     return false;
1299 }
1300
1301 bool Bind::visit(LabeledStatementAST *ast)
1302 {
1303     // unsigned label_token = ast->label_token;
1304     // unsigned colon_token = ast->colon_token;
1305     this->statement(ast->statement);
1306     return false;
1307 }
1308
1309 bool Bind::visit(BreakStatementAST *ast)
1310 {
1311     (void) ast;
1312     // unsigned break_token = ast->break_token;
1313     // unsigned semicolon_token = ast->semicolon_token;
1314     return false;
1315 }
1316
1317 bool Bind::visit(ContinueStatementAST *ast)
1318 {
1319     (void) ast;
1320     // unsigned continue_token = ast->continue_token;
1321     // unsigned semicolon_token = ast->semicolon_token;
1322     return false;
1323 }
1324
1325 bool Bind::visit(GotoStatementAST *ast)
1326 {
1327     (void) ast;
1328     // unsigned goto_token = ast->goto_token;
1329     // unsigned identifier_token = ast->identifier_token;
1330     // unsigned semicolon_token = ast->semicolon_token;
1331     return false;
1332 }
1333
1334 bool Bind::visit(ReturnStatementAST *ast)
1335 {
1336     ExpressionTy expression = this->expression(ast->expression);
1337     return false;
1338 }
1339
1340 bool Bind::visit(SwitchStatementAST *ast)
1341 {
1342     Block *block = control()->newBlock(ast->firstToken());
1343     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1344     block->setStartOffset(tokenAt(startScopeToken).end());
1345     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1346     _scope->addMember(block);
1347     ast->symbol = block;
1348
1349     Scope *previousScope = switchScope(block);
1350     /*ExpressionTy condition =*/ this->expression(ast->condition);
1351     this->statement(ast->statement);
1352     (void) switchScope(previousScope);
1353     return false;
1354 }
1355
1356 bool Bind::visit(TryBlockStatementAST *ast)
1357 {
1358     // unsigned try_token = ast->try_token;
1359     this->statement(ast->statement);
1360     for (CatchClauseListAST *it = ast->catch_clause_list; it; it = it->next) {
1361         this->statement(it->value);
1362     }
1363     return false;
1364 }
1365
1366 bool Bind::visit(CatchClauseAST *ast)
1367 {
1368     Block *block = control()->newBlock(ast->firstToken());
1369     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1370     block->setStartOffset(tokenAt(startScopeToken).end());
1371     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1372     _scope->addMember(block);
1373     ast->symbol = block;
1374
1375     Scope *previousScope = switchScope(block);
1376     this->declaration(ast->exception_declaration);
1377     // unsigned rparen_token = ast->rparen_token;
1378     this->statement(ast->statement);
1379     (void) switchScope(previousScope);
1380     return false;
1381 }
1382
1383 bool Bind::visit(WhileStatementAST *ast)
1384 {
1385     Block *block = control()->newBlock(ast->firstToken());
1386     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1387     block->setStartOffset(tokenAt(startScopeToken).end());
1388     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1389     _scope->addMember(block);
1390     ast->symbol = block;
1391
1392     Scope *previousScope = switchScope(block);
1393     /*ExpressionTy condition =*/ this->expression(ast->condition);
1394     this->statement(ast->statement);
1395     (void) switchScope(previousScope);
1396     return false;
1397 }
1398
1399 bool Bind::visit(ObjCFastEnumerationAST *ast)
1400 {
1401     Block *block = control()->newBlock(ast->firstToken());
1402     const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
1403     block->setStartOffset(tokenAt(startScopeToken).end());
1404     block->setEndOffset(tokenAt(ast->lastToken()).begin());
1405     _scope->addMember(block);
1406     ast->symbol = block;
1407
1408     Scope *previousScope = switchScope(block);
1409     FullySpecifiedType type;
1410     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1411         type = this->specifier(it->value, type);
1412     }
1413     DeclaratorIdAST *declaratorId = 0;
1414     type = this->declarator(ast->declarator, type, &declaratorId);
1415
1416     if (declaratorId && declaratorId->name) {
1417         unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
1418         Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
1419         decl->setType(type);
1420         block->addMember(decl);
1421     }
1422
1423     /*ExpressionTy initializer =*/ this->expression(ast->initializer);
1424     /*ExpressionTy fast_enumeratable_expression =*/ this->expression(ast->fast_enumeratable_expression);
1425     this->statement(ast->statement);
1426     (void) switchScope(previousScope);
1427     return false;
1428 }
1429
1430 bool Bind::visit(ObjCSynchronizedStatementAST *ast)
1431 {
1432     // unsigned synchronized_token = ast->synchronized_token;
1433     // unsigned lparen_token = ast->lparen_token;
1434     ExpressionTy synchronized_object = this->expression(ast->synchronized_object);
1435     // unsigned rparen_token = ast->rparen_token;
1436     this->statement(ast->statement);
1437     return false;
1438 }
1439
1440
1441 // ExpressionAST
1442 bool Bind::visit(IdExpressionAST *ast)
1443 {
1444     /*const Name *name =*/ this->name(ast->name);
1445     return false;
1446 }
1447
1448 bool Bind::visit(CompoundExpressionAST *ast)
1449 {
1450     // unsigned lparen_token = ast->lparen_token;
1451     this->statement(ast->statement);
1452     // unsigned rparen_token = ast->rparen_token;
1453     return false;
1454 }
1455
1456 bool Bind::visit(CompoundLiteralAST *ast)
1457 {
1458     // unsigned lparen_token = ast->lparen_token;
1459     ExpressionTy type_id = this->expression(ast->type_id);
1460     // unsigned rparen_token = ast->rparen_token;
1461     ExpressionTy initializer = this->expression(ast->initializer);
1462     return false;
1463 }
1464
1465 bool Bind::visit(QtMethodAST *ast)
1466 {
1467     // unsigned method_token = ast->method_token;
1468     // unsigned lparen_token = ast->lparen_token;
1469     FullySpecifiedType type;
1470     DeclaratorIdAST *declaratorId = 0;
1471     type = this->declarator(ast->declarator, type, &declaratorId);
1472     // unsigned rparen_token = ast->rparen_token;
1473     return false;
1474 }
1475
1476 bool Bind::visit(BinaryExpressionAST *ast)
1477 {
1478     ExpressionTy left_expression = this->expression(ast->left_expression);
1479     // unsigned binary_op_token = ast->binary_op_token;
1480     ExpressionTy right_expression = this->expression(ast->right_expression);
1481     return false;
1482 }
1483
1484 bool Bind::visit(CastExpressionAST *ast)
1485 {
1486     // unsigned lparen_token = ast->lparen_token;
1487     ExpressionTy type_id = this->expression(ast->type_id);
1488     // unsigned rparen_token = ast->rparen_token;
1489     ExpressionTy expression = this->expression(ast->expression);
1490     return false;
1491 }
1492
1493 bool Bind::visit(ConditionAST *ast)
1494 {
1495     FullySpecifiedType type;
1496     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1497         type = this->specifier(it->value, type);
1498     }
1499     DeclaratorIdAST *declaratorId = 0;
1500     type = this->declarator(ast->declarator, type, &declaratorId);
1501
1502     if (declaratorId && declaratorId->name) {
1503         unsigned sourceLocation = location(declaratorId->name, ast->firstToken());
1504         Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
1505         decl->setType(type);
1506         _scope->addMember(decl);
1507     }
1508
1509     return false;
1510 }
1511
1512 bool Bind::visit(ConditionalExpressionAST *ast)
1513 {
1514     ExpressionTy condition = this->expression(ast->condition);
1515     // unsigned question_token = ast->question_token;
1516     ExpressionTy left_expression = this->expression(ast->left_expression);
1517     // unsigned colon_token = ast->colon_token;
1518     ExpressionTy right_expression = this->expression(ast->right_expression);
1519     return false;
1520 }
1521
1522 bool Bind::visit(CppCastExpressionAST *ast)
1523 {
1524     // unsigned cast_token = ast->cast_token;
1525     // unsigned less_token = ast->less_token;
1526     ExpressionTy type_id = this->expression(ast->type_id);
1527     // unsigned greater_token = ast->greater_token;
1528     // unsigned lparen_token = ast->lparen_token;
1529     ExpressionTy expression = this->expression(ast->expression);
1530     // unsigned rparen_token = ast->rparen_token;
1531     return false;
1532 }
1533
1534 bool Bind::visit(DeleteExpressionAST *ast)
1535 {
1536     // unsigned scope_token = ast->scope_token;
1537     // unsigned delete_token = ast->delete_token;
1538     // unsigned lbracket_token = ast->lbracket_token;
1539     // unsigned rbracket_token = ast->rbracket_token;
1540     ExpressionTy expression = this->expression(ast->expression);
1541     return false;
1542 }
1543
1544 bool Bind::visit(ArrayInitializerAST *ast)
1545 {
1546     // unsigned lbrace_token = ast->lbrace_token;
1547     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
1548         ExpressionTy value = this->expression(it->value);
1549     }
1550     // unsigned rbrace_token = ast->rbrace_token;
1551     return false;
1552 }
1553
1554 bool Bind::visit(NewExpressionAST *ast)
1555 {
1556     // unsigned scope_token = ast->scope_token;
1557     // unsigned new_token = ast->new_token;
1558     this->newPlacement(ast->new_placement);
1559     // unsigned lparen_token = ast->lparen_token;
1560     ExpressionTy type_id = this->expression(ast->type_id);
1561     // unsigned rparen_token = ast->rparen_token;
1562     this->newTypeId(ast->new_type_id);
1563     this->newInitializer(ast->new_initializer);
1564     return false;
1565 }
1566
1567 bool Bind::visit(TypeidExpressionAST *ast)
1568 {
1569     // unsigned typeid_token = ast->typeid_token;
1570     // unsigned lparen_token = ast->lparen_token;
1571     ExpressionTy expression = this->expression(ast->expression);
1572     // unsigned rparen_token = ast->rparen_token;
1573     return false;
1574 }
1575
1576 bool Bind::visit(TypenameCallExpressionAST *ast)
1577 {
1578     // unsigned typename_token = ast->typename_token;
1579     /*const Name *name =*/ this->name(ast->name);
1580     // unsigned lparen_token = ast->lparen_token;
1581     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
1582         ExpressionTy value = this->expression(it->value);
1583     }
1584     // unsigned rparen_token = ast->rparen_token;
1585     return false;
1586 }
1587
1588 bool Bind::visit(TypeConstructorCallAST *ast)
1589 {
1590     FullySpecifiedType type;
1591     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1592         type = this->specifier(it->value, type);
1593     }
1594     // unsigned lparen_token = ast->lparen_token;
1595     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
1596         ExpressionTy value = this->expression(it->value);
1597     }
1598     // unsigned rparen_token = ast->rparen_token;
1599     return false;
1600 }
1601
1602 bool Bind::visit(SizeofExpressionAST *ast)
1603 {
1604     // unsigned sizeof_token = ast->sizeof_token;
1605     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
1606     // unsigned lparen_token = ast->lparen_token;
1607     ExpressionTy expression = this->expression(ast->expression);
1608     // unsigned rparen_token = ast->rparen_token;
1609     return false;
1610 }
1611
1612 bool Bind::visit(NumericLiteralAST *ast)
1613 {
1614     (void) ast;
1615     // unsigned literal_token = ast->literal_token;
1616     return false;
1617 }
1618
1619 bool Bind::visit(BoolLiteralAST *ast)
1620 {
1621     (void) ast;
1622     // unsigned literal_token = ast->literal_token;
1623     return false;
1624 }
1625
1626 bool Bind::visit(ThisExpressionAST *ast)
1627 {
1628     (void) ast;
1629     // unsigned this_token = ast->this_token;
1630     return false;
1631 }
1632
1633 bool Bind::visit(NestedExpressionAST *ast)
1634 {
1635     // unsigned lparen_token = ast->lparen_token;
1636     ExpressionTy expression = this->expression(ast->expression);
1637     // unsigned rparen_token = ast->rparen_token;
1638     return false;
1639 }
1640
1641 bool Bind::visit(StringLiteralAST *ast)
1642 {
1643     // unsigned literal_token = ast->literal_token;
1644     ExpressionTy next = this->expression(ast->next);
1645     return false;
1646 }
1647
1648 bool Bind::visit(ThrowExpressionAST *ast)
1649 {
1650     // unsigned throw_token = ast->throw_token;
1651     ExpressionTy expression = this->expression(ast->expression);
1652     return false;
1653 }
1654
1655 bool Bind::visit(TypeIdAST *ast)
1656 {
1657     FullySpecifiedType type;
1658     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1659         type = this->specifier(it->value, type);
1660     }
1661     DeclaratorIdAST *declaratorId = 0;
1662     type = this->declarator(ast->declarator, type, &declaratorId);
1663     _expression = type;
1664     return false;
1665 }
1666
1667 bool Bind::visit(UnaryExpressionAST *ast)
1668 {
1669     // unsigned unary_op_token = ast->unary_op_token;
1670     ExpressionTy expression = this->expression(ast->expression);
1671     return false;
1672 }
1673
1674 bool Bind::visit(ObjCMessageExpressionAST *ast)
1675 {
1676     // unsigned lbracket_token = ast->lbracket_token;
1677     /*ExpressionTy receiver_expression =*/ this->expression(ast->receiver_expression);
1678     /*const Name *selector =*/ this->name(ast->selector);
1679     for (ObjCMessageArgumentListAST *it = ast->argument_list; it; it = it->next) {
1680         this->objCMessageArgument(it->value);
1681     }
1682     // unsigned rbracket_token = ast->rbracket_token;
1683     return false;
1684 }
1685
1686 bool Bind::visit(ObjCProtocolExpressionAST *ast)
1687 {
1688     (void) ast;
1689     // unsigned protocol_token = ast->protocol_token;
1690     // unsigned lparen_token = ast->lparen_token;
1691     // unsigned identifier_token = ast->identifier_token;
1692     // unsigned rparen_token = ast->rparen_token;
1693     return false;
1694 }
1695
1696 bool Bind::visit(ObjCEncodeExpressionAST *ast)
1697 {
1698     // unsigned encode_token = ast->encode_token;
1699     FullySpecifiedType type = this->objCTypeName(ast->type_name);
1700     return false;
1701 }
1702
1703 bool Bind::visit(ObjCSelectorExpressionAST *ast)
1704 {
1705     // unsigned selector_token = ast->selector_token;
1706     // unsigned lparen_token = ast->lparen_token;
1707     /*const Name *selector =*/ this->name(ast->selector);
1708     // unsigned rparen_token = ast->rparen_token;
1709     return false;
1710 }
1711
1712 bool Bind::visit(LambdaExpressionAST *ast)
1713 {
1714     this->lambdaIntroducer(ast->lambda_introducer);
1715     this->lambdaDeclarator(ast->lambda_declarator);
1716     this->statement(ast->statement);
1717     return false;
1718 }
1719
1720 bool Bind::visit(BracedInitializerAST *ast)
1721 {
1722     // unsigned lbrace_token = ast->lbrace_token;
1723     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
1724         ExpressionTy value = this->expression(it->value);
1725     }
1726     // unsigned comma_token = ast->comma_token;
1727     // unsigned rbrace_token = ast->rbrace_token;
1728     return false;
1729 }
1730
1731 static int methodKeyForInvokableToken(int kind)
1732 {
1733     if (kind == T_Q_SIGNAL)
1734         return Function::SignalMethod;
1735     else if (kind == T_Q_SLOT)
1736         return Function::SlotMethod;
1737     else if (kind == T_Q_INVOKABLE)
1738         return Function::InvokableMethod;
1739
1740     return Function::NormalMethod;
1741 }
1742
1743 // DeclarationAST
1744 bool Bind::visit(SimpleDeclarationAST *ast)
1745 {
1746     int methodKey = _methodKey;
1747     if (ast->qt_invokable_token)
1748         methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token));
1749
1750     // unsigned qt_invokable_token = ast->qt_invokable_token;
1751     FullySpecifiedType type;
1752     for (SpecifierListAST *it = ast->decl_specifier_list; it; it = it->next) {
1753         type = this->specifier(it->value, type);
1754     }
1755
1756     List<Symbol *> **symbolTail = &ast->symbols;
1757
1758     if (! ast->declarator_list) {
1759         ElaboratedTypeSpecifierAST *elabTypeSpec = 0;
1760         for (SpecifierListAST *it = ast->decl_specifier_list; ! elabTypeSpec && it; it = it->next)
1761             elabTypeSpec = it->value->asElaboratedTypeSpecifier();
1762
1763         if (elabTypeSpec && tokenKind(elabTypeSpec->classkey_token) != T_TYPENAME) {
1764             unsigned sourceLocation = elabTypeSpec->firstToken();
1765             const Name *name = 0;
1766             if (elabTypeSpec->name) {
1767                 sourceLocation = location(elabTypeSpec->name, sourceLocation);
1768                 name = elabTypeSpec->name->name;
1769             }
1770
1771             ForwardClassDeclaration *decl = control()->newForwardClassDeclaration(sourceLocation, name);
1772             setDeclSpecifiers(decl, type);
1773             _scope->addMember(decl);
1774
1775             *symbolTail = new (translationUnit()->memoryPool()) List<Symbol *>(decl);
1776             symbolTail = &(*symbolTail)->next;
1777         }
1778     }
1779
1780     for (DeclaratorListAST *it = ast->declarator_list; it; it = it->next) {
1781         DeclaratorIdAST *declaratorId = 0;
1782         FullySpecifiedType declTy = this->declarator(it->value, type.qualifiedType(), &declaratorId);
1783
1784         const Name *declName = 0;
1785         unsigned sourceLocation = location(it->value, ast->firstToken());
1786         if (declaratorId && declaratorId->name) {
1787             declName = declaratorId->name->name;
1788         }
1789
1790         Declaration *decl = control()->newDeclaration(sourceLocation, declName);
1791         decl->setType(declTy);
1792         setDeclSpecifiers(decl, type);
1793
1794         if (Function *fun = decl->type()->asFunctionType()) {
1795             fun->setScope(_scope);
1796
1797             setDeclSpecifiers(fun, type);
1798             if (declaratorId && declaratorId->name)
1799                 fun->setName(declaratorId->name->name); // update the function name
1800         }
1801
1802         if (_scope->isClass()) {
1803             decl->setVisibility(_visibility);
1804
1805             if (Function *funTy = decl->type()->asFunctionType()) {
1806                 funTy->setMethodKey(methodKey);
1807
1808                 if (funTy->isVirtual() && it->value->equal_token)
1809                     funTy->setPureVirtual(true);
1810             }
1811         }
1812
1813         _scope->addMember(decl);
1814
1815         *symbolTail = new (translationUnit()->memoryPool()) List<Symbol *>(decl);
1816         symbolTail = &(*symbolTail)->next;
1817     }
1818     return false;
1819 }
1820
1821 bool Bind::visit(EmptyDeclarationAST *ast)
1822 {
1823     (void) ast;
1824     unsigned semicolon_token = ast->semicolon_token;
1825     if (_scope && (_scope->isClass() || _scope->isNamespace())) {
1826         const Token &tk = tokenAt(semicolon_token);
1827
1828         if (! tk.generated())
1829             translationUnit()->warning(semicolon_token, "extra `;'");
1830     }
1831     return false;
1832 }
1833
1834 bool Bind::visit(AccessDeclarationAST *ast)
1835 {
1836     const int accessSpecifier = tokenKind(ast->access_specifier_token);
1837     _visibility = visibilityForAccessSpecifier(accessSpecifier);
1838
1839     if (ast->slots_token)
1840         _methodKey = Function::SlotMethod;
1841     else if (accessSpecifier == T_Q_SIGNALS)
1842         _methodKey = Function::SignalMethod;
1843     else
1844         _methodKey = Function::NormalMethod;
1845
1846     return false;
1847 }
1848
1849 bool Bind::visit(QtObjectTagAST *ast)
1850 {
1851     (void) ast;
1852     // unsigned q_object_token = ast->q_object_token;
1853     return false;
1854 }
1855
1856 bool Bind::visit(QtPrivateSlotAST *ast)
1857 {
1858     // unsigned q_private_slot_token = ast->q_private_slot_token;
1859     // unsigned lparen_token = ast->lparen_token;
1860     // unsigned dptr_token = ast->dptr_token;
1861     // unsigned dptr_lparen_token = ast->dptr_lparen_token;
1862     // unsigned dptr_rparen_token = ast->dptr_rparen_token;
1863     // unsigned comma_token = ast->comma_token;
1864     FullySpecifiedType type;
1865     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1866         type = this->specifier(it->value, type);
1867     }
1868     DeclaratorIdAST *declaratorId = 0;
1869     type = this->declarator(ast->declarator, type, &declaratorId);
1870     // unsigned rparen_token = ast->rparen_token;
1871     return false;
1872 }
1873
1874 static void qtPropertyAttribute(TranslationUnit *unit, ExpressionAST *expression,
1875                                 int *flags,
1876                                 QtPropertyDeclaration::Flag flag,
1877                                 QtPropertyDeclaration::Flag function)
1878 {
1879     if (!expression)
1880         return;
1881     *flags &= ~function & ~flag;
1882     if (BoolLiteralAST *boollit = expression->asBoolLiteral()) {
1883         const int kind = unit->tokenAt(boollit->literal_token).kind();
1884         if (kind == T_TRUE)
1885             *flags |= flag;
1886     } else {
1887         *flags |= function;
1888     }
1889 }
1890
1891 bool Bind::visit(QtPropertyDeclarationAST *ast)
1892 {
1893     // unsigned property_specifier_token = ast->property_specifier_token;
1894     // unsigned lparen_token = ast->lparen_token;
1895     ExpressionTy type_id = this->expression(ast->type_id);
1896     const Name *property_name = this->name(ast->property_name);
1897
1898     unsigned sourceLocation = ast->firstToken();
1899     if (ast->property_name)
1900         sourceLocation = ast->property_name->firstToken();
1901     QtPropertyDeclaration *propertyDeclaration = control()->newQtPropertyDeclaration(sourceLocation, property_name);
1902     propertyDeclaration->setType(type_id);
1903
1904     int flags = QtPropertyDeclaration::DesignableFlag
1905             | QtPropertyDeclaration::ScriptableFlag
1906             | QtPropertyDeclaration::StoredFlag;
1907     for (QtPropertyDeclarationItemListAST *it = ast->property_declaration_item_list; it; it = it->next) {
1908         if (!it->value || !it->value->item_name_token)
1909             continue;
1910         this->expression(it->value->expression);
1911
1912         std::string name = spell(it->value->item_name_token);
1913
1914         if (name == "CONSTANT") {
1915             flags |= QtPropertyDeclaration::ConstantFlag;
1916         } else if (name == "FINAL") {
1917             flags |= QtPropertyDeclaration::FinalFlag;
1918         } else if (name == "READ") {
1919             flags |= QtPropertyDeclaration::ReadFunction;
1920         } else if (name == "WRITE") {
1921             flags |= QtPropertyDeclaration::WriteFunction;
1922         } else if (name == "RESET") {
1923             flags |= QtPropertyDeclaration::ResetFunction;
1924         } else if (name == "NOTIFY") {
1925             flags |= QtPropertyDeclaration::NotifyFunction;
1926         } else if (name == "DESIGNABLE") {
1927             qtPropertyAttribute(translationUnit(), it->value->expression, &flags,
1928                                 QtPropertyDeclaration::DesignableFlag, QtPropertyDeclaration::DesignableFunction);
1929         } else if (name == "SCRIPTABLE") {
1930             qtPropertyAttribute(translationUnit(), it->value->expression, &flags,
1931                                 QtPropertyDeclaration::ScriptableFlag, QtPropertyDeclaration::ScriptableFunction);
1932         } else if (name == "STORED") {
1933             qtPropertyAttribute(translationUnit(), it->value->expression, &flags,
1934                                 QtPropertyDeclaration::StoredFlag, QtPropertyDeclaration::StoredFunction);
1935         } else if (name == "USER") {
1936             qtPropertyAttribute(translationUnit(), it->value->expression, &flags,
1937                                 QtPropertyDeclaration::UserFlag, QtPropertyDeclaration::UserFunction);
1938         }
1939     }
1940     propertyDeclaration->setFlags(flags);
1941     _scope->addMember(propertyDeclaration);
1942     // unsigned rparen_token = ast->rparen_token;
1943     return false;
1944 }
1945
1946 bool Bind::visit(QtEnumDeclarationAST *ast)
1947 {
1948     // unsigned enum_specifier_token = ast->enum_specifier_token;
1949     // unsigned lparen_token = ast->lparen_token;
1950     for (NameListAST *it = ast->enumerator_list; it; it = it->next) {
1951         const Name *value = this->name(it->value);
1952         if (!value)
1953             continue;
1954         QtEnum *qtEnum = control()->newQtEnum(it->value->firstToken(), value);
1955         _scope->addMember(qtEnum);
1956     }
1957
1958     // unsigned rparen_token = ast->rparen_token;
1959     return false;
1960 }
1961
1962 bool Bind::visit(QtFlagsDeclarationAST *ast)
1963 {
1964     // unsigned flags_specifier_token = ast->flags_specifier_token;
1965     // unsigned lparen_token = ast->lparen_token;
1966     for (NameListAST *it = ast->flag_enums_list; it; it = it->next) {
1967         /*const Name *value =*/ this->name(it->value);
1968     }
1969     // unsigned rparen_token = ast->rparen_token;
1970     return false;
1971 }
1972
1973 bool Bind::visit(QtInterfacesDeclarationAST *ast)
1974 {
1975     // unsigned interfaces_token = ast->interfaces_token;
1976     // unsigned lparen_token = ast->lparen_token;
1977     for (QtInterfaceNameListAST *it = ast->interface_name_list; it; it = it->next) {
1978         this->qtInterfaceName(it->value);
1979     }
1980     // unsigned rparen_token = ast->rparen_token;
1981     return false;
1982 }
1983
1984 bool Bind::visit(AsmDefinitionAST *ast)
1985 {
1986     (void) ast;
1987     // unsigned asm_token = ast->asm_token;
1988     // unsigned volatile_token = ast->volatile_token;
1989     // unsigned lparen_token = ast->lparen_token;
1990     // unsigned rparen_token = ast->rparen_token;
1991     // unsigned semicolon_token = ast->semicolon_token;
1992     return false;
1993 }
1994
1995 bool Bind::visit(ExceptionDeclarationAST *ast)
1996 {
1997     FullySpecifiedType type;
1998     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1999         type = this->specifier(it->value, type);
2000     }
2001     DeclaratorIdAST *declaratorId = 0;
2002     type = this->declarator(ast->declarator, type, &declaratorId);
2003     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
2004     return false;
2005 }
2006
2007 bool Bind::visit(FunctionDefinitionAST *ast)
2008 {
2009     int methodKey = _methodKey;
2010     if (ast->qt_invokable_token)
2011         methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token));
2012
2013     FullySpecifiedType declSpecifiers;
2014     for (SpecifierListAST *it = ast->decl_specifier_list; it; it = it->next) {
2015         declSpecifiers = this->specifier(it->value, declSpecifiers);
2016     }
2017     DeclaratorIdAST *declaratorId = 0;
2018     FullySpecifiedType type = this->declarator(ast->declarator, declSpecifiers.qualifiedType(), &declaratorId);
2019
2020     Function *fun = type->asFunctionType();
2021     ast->symbol = fun;
2022
2023     if (fun) {
2024         setDeclSpecifiers(fun, declSpecifiers);
2025
2026         if (_scope->isClass()) {
2027             fun->setVisibility(_visibility);
2028             fun->setMethodKey(methodKey);
2029         }
2030
2031         if (declaratorId && declaratorId->name) {
2032             fun->setSourceLocation(location(declaratorId, ast->firstToken()), translationUnit());
2033             fun->setName(declaratorId->name->name);
2034         }
2035
2036         _scope->addMember(fun);
2037     } else
2038         translationUnit()->warning(ast->firstToken(), "expected a function declarator");
2039
2040     this->ctorInitializer(ast->ctor_initializer, fun);
2041
2042     if (fun && ! _skipFunctionBodies && ast->function_body) {
2043         Scope *previousScope = switchScope(fun);
2044         this->statement(ast->function_body);
2045         (void) switchScope(previousScope);
2046
2047         if (CompoundStatementAST *c = ast->function_body->asCompoundStatement()) {
2048             if (c->symbol) {
2049                 fun->setEndOffset(c->symbol->endOffset());
2050             }
2051         }
2052     }
2053
2054     return false;
2055 }
2056
2057 bool Bind::visit(LinkageBodyAST *ast)
2058 {
2059     // unsigned lbrace_token = ast->lbrace_token;
2060     for (DeclarationListAST *it = ast->declaration_list; it; it = it->next) {
2061         this->declaration(it->value);
2062     }
2063     // unsigned rbrace_token = ast->rbrace_token;
2064     return false;
2065 }
2066
2067 bool Bind::visit(LinkageSpecificationAST *ast)
2068 {
2069     // unsigned extern_token = ast->extern_token;
2070     // unsigned extern_type_token = ast->extern_type_token;
2071     this->declaration(ast->declaration);
2072     return false;
2073 }
2074
2075 bool Bind::visit(NamespaceAST *ast)
2076 {
2077     // unsigned namespace_token = ast->namespace_token;
2078     // unsigned identifier_token = ast->identifier_token;
2079     FullySpecifiedType type;
2080     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2081         type = this->specifier(it->value, type);
2082     }
2083
2084     unsigned sourceLocation = ast->firstToken();
2085     const Name *namespaceName = 0;
2086     if (ast->identifier_token) {
2087         sourceLocation = ast->identifier_token;
2088         namespaceName = identifier(ast->identifier_token);
2089     }
2090
2091     Namespace *ns = control()->newNamespace(sourceLocation, namespaceName);
2092     ns->setStartOffset(tokenAt(sourceLocation).end()); // the scope starts after the namespace or the identifier token.
2093     ns->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2094     ast->symbol = ns;
2095     _scope->addMember(ns);
2096
2097     Scope *previousScope = switchScope(ns);
2098     this->declaration(ast->linkage_body);
2099     (void) switchScope(previousScope);
2100     return false;
2101 }
2102
2103 bool Bind::visit(NamespaceAliasDefinitionAST *ast)
2104 {
2105     unsigned sourceLocation = ast->firstToken();
2106     const Name *name = 0;
2107     if (ast->namespace_name_token) {
2108         sourceLocation = ast->namespace_name_token;
2109         name = identifier(ast->namespace_name_token);
2110     }
2111
2112     NamespaceAlias *namespaceAlias = control()->newNamespaceAlias(sourceLocation, name);
2113     namespaceAlias->setNamespaceName(this->name(ast->name));
2114     _scope->addMember(namespaceAlias);
2115     return false;
2116 }
2117
2118 bool Bind::visit(ParameterDeclarationAST *ast)
2119 {
2120     FullySpecifiedType type;
2121     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
2122         type = this->specifier(it->value, type);
2123     }
2124     DeclaratorIdAST *declaratorId = 0;
2125     type = this->declarator(ast->declarator, type, &declaratorId);
2126     // unsigned equal_token = ast->equal_token;
2127     ExpressionTy expression = this->expression(ast->expression);
2128
2129     unsigned sourceLocation = ast->firstToken();
2130     if (declaratorId)
2131         sourceLocation = declaratorId->firstToken();
2132
2133     const Name *argName = 0;
2134     if (declaratorId && declaratorId->name)
2135         argName = declaratorId->name->name;
2136
2137     Argument *arg = control()->newArgument(location(declaratorId, ast->firstToken()), argName);
2138     arg->setType(type);
2139
2140     if (ast->expression) {
2141         unsigned startOfExpression = ast->expression->firstToken();
2142         unsigned endOfExpression = ast->expression->lastToken();
2143         std::string buffer;
2144         for (unsigned index = startOfExpression; index != endOfExpression; ++index) {
2145             const Token &tk = tokenAt(index);
2146             if (tk.whitespace() || tk.newline())
2147                 buffer += ' ';
2148             buffer += tk.spell();
2149         }
2150         const StringLiteral *initializer = control()->stringLiteral(buffer.c_str(), buffer.size());
2151         arg->setInitializer(initializer);
2152     }
2153
2154     _scope->addMember(arg);
2155
2156     ast->symbol = arg;
2157     return false;
2158 }
2159
2160 bool Bind::visit(TemplateDeclarationAST *ast)
2161 {
2162     Template *templ = control()->newTemplate(ast->firstToken(), 0);
2163     templ->setStartOffset(tokenAt(ast->firstToken()).begin());
2164     templ->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2165     ast->symbol = templ;
2166     Scope *previousScope = switchScope(templ);
2167
2168     for (DeclarationListAST *it = ast->template_parameter_list; it; it = it->next) {
2169         this->declaration(it->value);
2170     }
2171     // unsigned greater_token = ast->greater_token;
2172     this->declaration(ast->declaration);
2173     (void) switchScope(previousScope);
2174
2175     if (Symbol *decl = templ->declaration()) {
2176         templ->setSourceLocation(decl->sourceLocation(), translationUnit());
2177         templ->setName(decl->name());
2178     }
2179
2180     _scope->addMember(templ);
2181     return false;
2182 }
2183
2184 bool Bind::visit(TypenameTypeParameterAST *ast)
2185 {
2186     unsigned sourceLocation = location(ast->name, ast->firstToken());
2187     // unsigned classkey_token = ast->classkey_token;
2188     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
2189     const Name *name = this->name(ast->name);
2190     ExpressionTy type_id = this->expression(ast->type_id);
2191
2192     TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
2193     arg->setType(type_id);
2194     ast->symbol = arg;
2195     _scope->addMember(arg);
2196     return false;
2197 }
2198
2199 bool Bind::visit(TemplateTypeParameterAST *ast)
2200 {
2201     unsigned sourceLocation = location(ast->name, ast->firstToken());
2202
2203     // unsigned template_token = ast->template_token;
2204     // unsigned less_token = ast->less_token;
2205     // ### process the template prototype
2206 #if 0
2207     for (DeclarationListAST *it = ast->template_parameter_list; it; it = it->next) {
2208         this->declaration(it->value);
2209     }
2210 #endif
2211     // unsigned greater_token = ast->greater_token;
2212     // unsigned class_token = ast->class_token;
2213     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
2214
2215     const Name *name = this->name(ast->name);
2216     ExpressionTy type_id = this->expression(ast->type_id);
2217
2218     // ### introduce TemplateTypeArgument
2219     TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
2220     arg->setType(type_id);
2221     ast->symbol = arg;
2222     _scope->addMember(arg);
2223
2224     return false;
2225 }
2226
2227 bool Bind::visit(UsingAST *ast)
2228 {
2229     unsigned sourceLocation = location(ast->name, ast->firstToken());
2230     const Name *name = this->name(ast->name);
2231
2232     UsingDeclaration *symbol = control()->newUsingDeclaration(sourceLocation, name);
2233     ast->symbol = symbol;
2234     _scope->addMember(symbol);
2235     return false;
2236 }
2237
2238 bool Bind::visit(UsingDirectiveAST *ast)
2239 {
2240     unsigned sourceLocation = location(ast->name, ast->firstToken());
2241     const Name *name = this->name(ast->name);
2242     UsingNamespaceDirective *symbol = control()->newUsingNamespaceDirective(sourceLocation, name);
2243     ast->symbol = symbol;
2244     _scope->addMember(symbol);
2245     return false;
2246 }
2247
2248 bool Bind::visit(ObjCClassForwardDeclarationAST *ast)
2249 {
2250     FullySpecifiedType declSpecifiers;
2251     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2252         declSpecifiers = this->specifier(it->value, declSpecifiers);
2253     }
2254
2255     List<ObjCForwardClassDeclaration *> **symbolTail = &ast->symbols;
2256
2257     // unsigned class_token = ast->class_token;
2258     for (NameListAST *it = ast->identifier_list; it; it = it->next) {
2259         const Name *name = this->name(it->value);
2260
2261         const unsigned sourceLocation = location(it->value, ast->firstToken());
2262         ObjCForwardClassDeclaration *fwd = control()->newObjCForwardClassDeclaration(sourceLocation, name);
2263         setDeclSpecifiers(fwd, declSpecifiers);
2264         _scope->addMember(fwd);
2265
2266         *symbolTail = new (translationUnit()->memoryPool()) List<ObjCForwardClassDeclaration *>(fwd);
2267         symbolTail = &(*symbolTail)->next;
2268     }
2269
2270     return false;
2271 }
2272
2273 unsigned Bind::calculateScopeStart(ObjCClassDeclarationAST *ast) const
2274 {
2275     if (ast->inst_vars_decl)
2276         if (unsigned pos = ast->inst_vars_decl->lbrace_token)
2277             return tokenAt(pos).end();
2278
2279     if (ast->protocol_refs)
2280         if (unsigned pos = ast->protocol_refs->lastToken())
2281             return tokenAt(pos - 1).end();
2282
2283     if (ast->superclass)
2284         if (unsigned pos = ast->superclass->lastToken())
2285             return tokenAt(pos - 1).end();
2286
2287     if (ast->colon_token)
2288         return tokenAt(ast->colon_token).end();
2289
2290     if (ast->rparen_token)
2291         return tokenAt(ast->rparen_token).end();
2292
2293     if (ast->category_name)
2294         if (unsigned pos = ast->category_name->lastToken())
2295             return tokenAt(pos - 1).end();
2296
2297     if (ast->lparen_token)
2298         return tokenAt(ast->lparen_token).end();
2299
2300     if (ast->class_name)
2301         if (unsigned pos = ast->class_name->lastToken())
2302             return tokenAt(pos - 1).end();
2303
2304     return tokenAt(ast->firstToken()).begin();
2305 }
2306
2307 bool Bind::visit(ObjCClassDeclarationAST *ast)
2308 {
2309     FullySpecifiedType declSpecifiers;
2310     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2311         declSpecifiers = this->specifier(it->value, declSpecifiers);
2312     }
2313     const Name *class_name = this->name(ast->class_name);
2314     const Name *category_name = this->name(ast->category_name);
2315
2316     const unsigned sourceLocation = location(ast->class_name, ast->firstToken());
2317     ObjCClass *klass = control()->newObjCClass(sourceLocation, class_name);
2318     ast->symbol = klass;
2319     _scope->addMember(klass);
2320
2321     klass->setStartOffset(calculateScopeStart(ast));
2322     klass->setEndOffset(tokenAt(ast->lastToken() - 1).begin());
2323
2324     if (ast->interface_token)
2325         klass->setInterface(true);
2326
2327     klass->setCategoryName(category_name);
2328
2329     Scope *previousScope = switchScope(klass);
2330
2331     if (const Name *superclass = this->name(ast->superclass)) {
2332         ObjCBaseClass *superKlass = control()->newObjCBaseClass(ast->superclass->firstToken(), superclass);
2333         klass->setBaseClass(superKlass);
2334     }
2335
2336     this->objCProtocolRefs(ast->protocol_refs, klass);
2337
2338     const int previousObjCVisibility = switchObjCVisibility(Function::Protected);
2339
2340     this->objCInstanceVariablesDeclaration(ast->inst_vars_decl, klass);
2341
2342     (void) switchObjCVisibility(Function::Public);
2343     for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
2344         this->declaration(it->value);
2345     }
2346
2347     (void) switchObjCVisibility(previousObjCVisibility);
2348     (void) switchScope(previousScope);
2349     return false;
2350 }
2351
2352 bool Bind::visit(ObjCProtocolForwardDeclarationAST *ast)
2353 {
2354     FullySpecifiedType declSpecifiers;
2355     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2356         declSpecifiers = this->specifier(it->value, declSpecifiers);
2357     }
2358
2359     List<ObjCForwardProtocolDeclaration *> **symbolTail = &ast->symbols;
2360
2361     // unsigned class_token = ast->class_token;
2362     for (NameListAST *it = ast->identifier_list; it; it = it->next) {
2363         const Name *name = this->name(it->value);
2364
2365         const unsigned sourceLocation = location(it->value, ast->firstToken());
2366         ObjCForwardProtocolDeclaration *fwd = control()->newObjCForwardProtocolDeclaration(sourceLocation, name);
2367         setDeclSpecifiers(fwd, declSpecifiers);
2368         _scope->addMember(fwd);
2369
2370         *symbolTail = new (translationUnit()->memoryPool()) List<ObjCForwardProtocolDeclaration *>(fwd);
2371         symbolTail = &(*symbolTail)->next;
2372     }
2373
2374     return false;
2375 }
2376
2377 unsigned Bind::calculateScopeStart(ObjCProtocolDeclarationAST *ast) const
2378 {
2379     if (ast->protocol_refs)
2380         if (unsigned pos = ast->protocol_refs->lastToken())
2381             return tokenAt(pos - 1).end();
2382     if (ast->name)
2383         if (unsigned pos = ast->name->lastToken())
2384             return tokenAt(pos - 1).end();
2385
2386     return tokenAt(ast->firstToken()).begin();
2387 }
2388
2389 bool Bind::visit(ObjCProtocolDeclarationAST *ast)
2390 {
2391     FullySpecifiedType type;
2392     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2393         type = this->specifier(it->value, type);
2394     }
2395     // unsigned protocol_token = ast->protocol_token;
2396     const Name *name = this->name(ast->name);
2397
2398     const unsigned sourceLocation = location(ast->name, ast->firstToken());
2399     ObjCProtocol *protocol = control()->newObjCProtocol(sourceLocation, name);
2400     protocol->setStartOffset(calculateScopeStart(ast));
2401     protocol->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2402     ast->symbol = protocol;
2403     _scope->addMember(protocol);
2404
2405     Scope *previousScope = switchScope(protocol);
2406     const int previousObjCVisibility = switchObjCVisibility(Function::Public);
2407
2408     this->objCProtocolRefs(ast->protocol_refs, protocol);
2409     for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
2410         this->declaration(it->value);
2411     }
2412
2413     (void) switchObjCVisibility(previousObjCVisibility);
2414     (void) switchScope(previousScope);
2415     return false;
2416 }
2417
2418 bool Bind::visit(ObjCVisibilityDeclarationAST *ast)
2419 {
2420     _objcVisibility = visibilityForObjCAccessSpecifier(tokenKind(ast->visibility_token));
2421     return false;
2422 }
2423
2424 bool Bind::visit(ObjCPropertyDeclarationAST *ast)
2425 {
2426     (void) ast;
2427     FullySpecifiedType type;
2428     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2429         type = this->specifier(it->value, type);
2430     }
2431     // unsigned property_token = ast->property_token;
2432     // unsigned lparen_token = ast->lparen_token;
2433     for (ObjCPropertyAttributeListAST *it = ast->property_attribute_list; it; it = it->next) {
2434         this->objCPropertyAttribute(it->value);
2435     }
2436     // unsigned rparen_token = ast->rparen_token;
2437     this->declaration(ast->simple_declaration);
2438     // List<ObjCPropertyDeclaration *> *symbols = ast->symbols;
2439     return false;
2440 }
2441
2442 bool Bind::visit(ObjCMethodDeclarationAST *ast)
2443 {
2444     ObjCMethod *method = this->objCMethodPrototype(ast->method_prototype);
2445
2446     if (! ast->function_body) {
2447         const Name *name = method->name();
2448         unsigned sourceLocation = ast->firstToken();
2449         Declaration *decl = control()->newDeclaration(sourceLocation, name);
2450         decl->setType(method);
2451         _scope->addMember(decl);
2452     } else if (! _skipFunctionBodies && ast->function_body) {
2453         Scope *previousScope = switchScope(method);
2454         this->statement(ast->function_body);
2455         (void) switchScope(previousScope);
2456         _scope->addMember(method);
2457     }
2458
2459     return false;
2460 }
2461
2462 bool Bind::visit(ObjCSynthesizedPropertiesDeclarationAST *ast)
2463 {
2464     // unsigned synthesized_token = ast->synthesized_token;
2465     for (ObjCSynthesizedPropertyListAST *it = ast->property_identifier_list; it; it = it->next) {
2466         this->objCSynthesizedProperty(it->value);
2467     }
2468     // unsigned semicolon_token = ast->semicolon_token;
2469     return false;
2470 }
2471
2472 bool Bind::visit(ObjCDynamicPropertiesDeclarationAST *ast)
2473 {
2474     // unsigned dynamic_token = ast->dynamic_token;
2475     for (NameListAST *it = ast->property_identifier_list; it; it = it->next) {
2476         /*const Name *value =*/ this->name(it->value);
2477     }
2478     // unsigned semicolon_token = ast->semicolon_token;
2479     return false;
2480 }
2481
2482
2483 // NameAST
2484 bool Bind::visit(ObjCSelectorAST *ast) // ### review
2485 {
2486     std::vector<const Name *> arguments;
2487     bool hasArgs = false;
2488
2489     for (ObjCSelectorArgumentListAST *it = ast->selector_argument_list; it; it = it->next) {
2490         if (const Name *selector_argument = this->objCSelectorArgument(it->value, &hasArgs))
2491             arguments.push_back(selector_argument);
2492     }
2493
2494     if (! arguments.empty()) {
2495         _name = control()->selectorNameId(&arguments[0], arguments.size(), hasArgs);
2496         ast->name = _name;
2497     }
2498
2499     return false;
2500 }
2501
2502 bool Bind::visit(QualifiedNameAST *ast)
2503 {
2504     for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) {
2505         const Name *class_or_namespace_name = this->nestedNameSpecifier(it->value);
2506         if (_name || ast->global_scope_token)
2507             _name = control()->qualifiedNameId(_name, class_or_namespace_name);
2508         else
2509             _name = class_or_namespace_name;
2510     }
2511
2512     const Name *unqualified_name = this->name(ast->unqualified_name);
2513     if (_name || ast->global_scope_token)
2514         _name = control()->qualifiedNameId(_name, unqualified_name);
2515     else
2516         _name = unqualified_name;
2517
2518     ast->name = _name;
2519     return false;
2520 }
2521
2522 bool Bind::visit(OperatorFunctionIdAST *ast)
2523 {
2524     const OperatorNameId::Kind op = this->cppOperator(ast->op);
2525     ast->name = _name = control()->operatorNameId(op);
2526     return false;
2527 }
2528
2529 bool Bind::visit(ConversionFunctionIdAST *ast)
2530 {
2531     FullySpecifiedType type;
2532     for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
2533         type = this->specifier(it->value, type);
2534     }
2535     for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
2536         type = this->ptrOperator(it->value, type);
2537     }
2538     ast->name = _name = control()->conversionNameId(type);
2539     return false;
2540 }
2541
2542 bool Bind::visit(SimpleNameAST *ast)
2543 {
2544     const Identifier *id = identifier(ast->identifier_token);
2545     _name = id;
2546     ast->name = _name;
2547     return false;
2548 }
2549
2550 bool Bind::visit(DestructorNameAST *ast)
2551 {
2552     const Identifier *id = identifier(ast->identifier_token);
2553     _name = control()->destructorNameId(id);
2554     ast->name = _name;
2555     return false;
2556 }
2557
2558 bool Bind::visit(TemplateIdAST *ast)
2559 {
2560     // collect the template parameters
2561     std::vector<FullySpecifiedType> templateArguments;
2562     for (ExpressionListAST *it = ast->template_argument_list; it; it = it->next) {
2563         ExpressionTy value = this->expression(it->value);
2564         templateArguments.push_back(value);
2565     }
2566
2567     const Identifier *id = identifier(ast->identifier_token);
2568     if (templateArguments.empty())
2569         _name = control()->templateNameId(id);
2570     else
2571         _name = control()->templateNameId(id, &templateArguments[0], templateArguments.size());
2572
2573     ast->name = _name;
2574     return false;
2575 }
2576
2577
2578 // SpecifierAST
2579 bool Bind::visit(SimpleSpecifierAST *ast)
2580 {
2581     switch (tokenKind(ast->specifier_token)) {
2582         case T_CONST:
2583             if (_type.isConst())
2584                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2585             _type.setConst(true);
2586             break;
2587
2588         case T_VOLATILE:
2589             if (_type.isVolatile())
2590                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2591             _type.setVolatile(true);
2592             break;
2593
2594         case T_FRIEND:
2595             if (_type.isFriend())
2596                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2597             _type.setFriend(true);
2598             break;
2599
2600         case T_AUTO:
2601             if (_type.isAuto())
2602                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2603             _type.setAuto(true);
2604             break;
2605
2606         case T_REGISTER:
2607             if (_type.isRegister())
2608                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2609             _type.setRegister(true);
2610             break;
2611
2612         case T_STATIC:
2613             if (_type.isStatic())
2614                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2615             _type.setStatic(true);
2616             break;
2617
2618         case T_EXTERN:
2619             if (_type.isExtern())
2620                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2621             _type.setExtern(true);
2622             break;
2623
2624         case T_MUTABLE:
2625             if (_type.isMutable())
2626                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2627             _type.setMutable(true);
2628             break;
2629
2630         case T_TYPEDEF:
2631             if (_type.isTypedef())
2632                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2633             _type.setTypedef(true);
2634             break;
2635
2636         case T_INLINE:
2637             if (_type.isInline())
2638                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2639             _type.setInline(true);
2640             break;
2641
2642         case T_VIRTUAL:
2643             if (_type.isVirtual())
2644                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2645             _type.setVirtual(true);
2646             break;
2647
2648         case T_EXPLICIT:
2649             if (_type.isExplicit())
2650                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2651             _type.setExplicit(true);
2652             break;
2653
2654         case T_SIGNED:
2655             if (_type.isSigned())
2656                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2657             _type.setSigned(true);
2658             break;
2659
2660         case T_UNSIGNED:
2661             if (_type.isUnsigned())
2662                 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2663             _type.setUnsigned(true);
2664             break;
2665
2666         case T_CHAR:
2667             if (_type)
2668                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2669             _type.setType(control()->integerType(IntegerType::Char));
2670             break;
2671
2672         case T_WCHAR_T:
2673             if (_type)
2674                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2675             _type.setType(control()->integerType(IntegerType::WideChar));
2676             break;
2677
2678         case T_BOOL:
2679             if (_type)
2680                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2681             _type.setType(control()->integerType(IntegerType::Bool));
2682             break;
2683
2684         case T_SHORT:
2685             if (_type) {
2686                 IntegerType *intType = control()->integerType(IntegerType::Int);
2687                 if (_type.type() != intType)
2688                     translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2689             }
2690             _type.setType(control()->integerType(IntegerType::Short));
2691             break;
2692
2693         case T_INT:
2694             if (_type) {
2695                 Type *tp = _type.type();
2696                 IntegerType *shortType = control()->integerType(IntegerType::Short);
2697                 IntegerType *longType = control()->integerType(IntegerType::Long);
2698                 IntegerType *longLongType = control()->integerType(IntegerType::LongLong);
2699                 if (tp == shortType || tp == longType || tp == longLongType)
2700                     break;
2701                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2702             }
2703             _type.setType(control()->integerType(IntegerType::Int));
2704             break;
2705
2706         case T_LONG:
2707             if (_type) {
2708                 Type *tp = _type.type();
2709                 IntegerType *intType = control()->integerType(IntegerType::Int);
2710                 IntegerType *longType = control()->integerType(IntegerType::Long);
2711                 FloatType *doubleType = control()->floatType(FloatType::Double);
2712                 if (tp == longType) {
2713                     _type.setType(control()->integerType(IntegerType::LongLong));
2714                     break;
2715                 } else if (tp == doubleType) {
2716                     _type.setType(control()->floatType(FloatType::LongDouble));
2717                     break;
2718                 } else if (tp != intType) {
2719                     translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2720                 }
2721             }
2722             _type.setType(control()->integerType(IntegerType::Long));
2723             break;
2724
2725         case T_FLOAT:
2726             if (_type)
2727                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2728             _type.setType(control()->floatType(FloatType::Float));
2729             break;
2730
2731         case T_DOUBLE:
2732             if (_type) {
2733                 IntegerType *longType = control()->integerType(IntegerType::Long);
2734                 if (_type.type() == longType) {
2735                     _type.setType(control()->floatType(FloatType::LongDouble));
2736                     break;
2737                 }
2738                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2739             }
2740             _type.setType(control()->floatType(FloatType::Double));
2741             break;
2742
2743         case T_VOID:
2744             if (_type)
2745                 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2746             _type.setType(control()->voidType());
2747             break;
2748
2749         default:
2750             break;
2751     } // switch
2752     return false;
2753 }
2754
2755 bool Bind::visit(AttributeSpecifierAST *ast)
2756 {
2757     // unsigned attribute_token = ast->attribute_token;
2758     // unsigned first_lparen_token = ast->first_lparen_token;
2759     // unsigned second_lparen_token = ast->second_lparen_token;
2760     for (AttributeListAST *it = ast->attribute_list; it; it = it->next) {
2761         this->attribute(it->value);
2762     }
2763     // unsigned first_rparen_token = ast->first_rparen_token;
2764     // unsigned second_rparen_token = ast->second_rparen_token;
2765     return false;
2766 }
2767
2768 bool Bind::visit(TypeofSpecifierAST *ast)
2769 {
2770     ExpressionTy expression = this->expression(ast->expression);
2771     _type = expression;
2772     return false;
2773 }
2774
2775 bool Bind::visit(ClassSpecifierAST *ast)
2776 {
2777     // unsigned classkey_token = ast->classkey_token;
2778     unsigned sourceLocation = ast->firstToken();
2779     unsigned startScopeOffset = tokenAt(sourceLocation).end(); // at the end of the class key
2780
2781     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2782         _type = this->specifier(it->value, _type);
2783     }
2784
2785     const Name *className = this->name(ast->name);
2786
2787     if (ast->name) {
2788         sourceLocation = location(ast->name, sourceLocation);
2789         startScopeOffset = tokenAt(sourceLocation).end(); // at the end of the class name
2790
2791         if (QualifiedNameAST *q = ast->name->asQualifiedName()) {
2792             if (q->unqualified_name) {
2793                 sourceLocation = q->unqualified_name->firstToken();
2794                 startScopeOffset = tokenAt(q->unqualified_name->lastToken() - 1).end(); // at the end of the unqualified name
2795             }
2796         }
2797     }
2798
2799     Class *klass = control()->newClass(sourceLocation, className);
2800     klass->setStartOffset(startScopeOffset);
2801     klass->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2802     _scope->addMember(klass);
2803
2804     if (_scope->isClass())
2805         klass->setVisibility(_visibility);
2806
2807     // set the class key
2808     unsigned classKey = tokenKind(ast->classkey_token);
2809     if (classKey == T_CLASS)
2810         klass->setClassKey(Class::ClassKey);
2811     else if (classKey == T_STRUCT)
2812         klass->setClassKey(Class::StructKey);
2813     else if (classKey == T_UNION)
2814         klass->setClassKey(Class::UnionKey);
2815
2816     _type.setType(klass);
2817
2818     Scope *previousScope = switchScope(klass);
2819     const int previousVisibility = switchVisibility(visibilityForClassKey(classKey));
2820     const int previousMethodKey = switchMethodKey(Function::NormalMethod);
2821
2822     for (BaseSpecifierListAST *it = ast->base_clause_list; it; it = it->next) {
2823         this->baseSpecifier(it->value, ast->colon_token, klass);
2824     }
2825     // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
2826     for (DeclarationListAST *it = ast->member_specifier_list; it; it = it->next) {
2827         this->declaration(it->value);
2828     }
2829
2830     (void) switchMethodKey(previousMethodKey);
2831     (void) switchVisibility(previousVisibility);
2832     (void) switchScope(previousScope);
2833
2834     ast->symbol = klass;
2835     return false;
2836 }
2837
2838 bool Bind::visit(NamedTypeSpecifierAST *ast)
2839 {
2840     _type.setType(control()->namedType(this->name(ast->name)));
2841     return false;
2842 }
2843
2844 bool Bind::visit(ElaboratedTypeSpecifierAST *ast)
2845 {
2846     // unsigned classkey_token = ast->classkey_token;
2847     for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2848         _type = this->specifier(it->value, _type);
2849     }
2850     _type.setType(control()->namedType(this->name(ast->name)));
2851     return false;
2852 }
2853
2854 bool Bind::visit(EnumSpecifierAST *ast)
2855 {
2856     unsigned sourceLocation = location(ast->name, ast->firstToken());
2857     const Name *enumName = this->name(ast->name);
2858
2859     Enum *e = control()->newEnum(sourceLocation, enumName);
2860     e->setStartOffset(tokenAt(sourceLocation).end()); // at the end of the enum or identifier token.
2861     e->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2862     ast->symbol = e;
2863     _scope->addMember(e);
2864
2865     if (_scope->isClass())
2866         e->setVisibility(_visibility);
2867
2868     Scope *previousScope = switchScope(e);
2869     for (EnumeratorListAST *it = ast->enumerator_list; it; it = it->next) {
2870         this->enumerator(it->value, e);
2871     }
2872
2873     if (ast->stray_comma_token /* && ! translationUnit()->cxx0xEnabled()*/) {
2874         const Token &tk = tokenAt(ast->stray_comma_token);
2875         if (! tk.generated())
2876             translationUnit()->warning(ast->stray_comma_token,
2877                                        "commas at the end of enumerator lists are a C++0x-specific feature");
2878     }
2879
2880     (void) switchScope(previousScope);
2881     return false;
2882 }
2883
2884
2885 // PtrOperatorAST
2886 bool Bind::visit(PointerToMemberAST *ast)
2887 {
2888     const Name *memberName = 0;
2889
2890     for (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list; it; it = it->next) {
2891         const Name *class_or_namespace_name = this->nestedNameSpecifier(it->value);
2892         if (memberName || ast->global_scope_token)
2893             memberName = control()->qualifiedNameId(memberName, class_or_namespace_name);
2894         else
2895             memberName = class_or_namespace_name;
2896     }
2897
2898     FullySpecifiedType type(control()->pointerToMemberType(memberName, _type));
2899     for (SpecifierListAST *it = ast->cv_qualifier_list; it; it = it->next) {
2900         type = this->specifier(it->value, type);
2901     }
2902     _type = type;
2903     return false;
2904 }
2905
2906 bool Bind::visit(PointerAST *ast)
2907 {
2908     if (_type->isReferenceType())
2909         translationUnit()->error(ast->firstToken(), "cannot declare pointer to a reference");
2910
2911     FullySpecifiedType type(control()->pointerType(_type));
2912     for (SpecifierListAST *it = ast->cv_qualifier_list; it; it = it->next) {
2913         type = this->specifier(it->value, type);
2914     }
2915     _type = type;
2916     return false;
2917 }
2918
2919 bool Bind::visit(ReferenceAST *ast)
2920 {
2921     const bool rvalueRef = (tokenKind(ast->reference_token) == T_AMPER_AMPER);
2922
2923     if (_type->isReferenceType())
2924         translationUnit()->error(ast->firstToken(), "cannot declare reference to a reference");
2925
2926     FullySpecifiedType type(control()->referenceType(_type, rvalueRef));
2927     _type = type;
2928     return false;
2929 }
2930
2931
2932 // PostfixAST
2933 bool Bind::visit(CallAST *ast)
2934 {
2935     /*ExpressionTy base_expression =*/ this->expression(ast->base_expression);
2936     // unsigned lparen_token = ast->lparen_token;
2937     for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
2938         /*ExpressionTy value =*/ this->expression(it->value);
2939     }
2940     // unsigned rparen_token = ast->rparen_token;
2941     return false;
2942 }
2943
2944 bool Bind::visit(ArrayAccessAST *ast)
2945 {
2946     /*ExpressionTy base_expression =*/ this->expression(ast->base_expression);
2947     // unsigned lbracket_token = ast->lbracket_token;
2948     /*ExpressionTy expression =*/ this->expression(ast->expression);
2949     // unsigned rbracket_token = ast->rbracket_token;
2950     return false;
2951 }
2952
2953 bool Bind::visit(PostIncrDecrAST *ast)
2954 {
2955     ExpressionTy base_expression = this->expression(ast->base_expression);
2956     // unsigned incr_decr_token = ast->incr_decr_token;
2957     return false;
2958 }
2959
2960 bool Bind::visit(MemberAccessAST *ast)
2961 {
2962     ExpressionTy base_expression = this->expression(ast->base_expression);
2963     // unsigned access_token = ast->access_token;
2964     // unsigned template_token = ast->template_token;
2965     /*const Name *member_name =*/ this->name(ast->member_name);
2966     return false;
2967 }
2968
2969
2970 // CoreDeclaratorAST
2971 bool Bind::visit(DeclaratorIdAST *ast)
2972 {
2973     /*const Name *name =*/ this->name(ast->name);
2974     *_declaratorId = ast;
2975     return false;
2976 }
2977
2978 bool Bind::visit(NestedDeclaratorAST *ast)
2979 {
2980     _type = this->declarator(ast->declarator, _type, _declaratorId);
2981     return false;
2982 }
2983
2984
2985 // PostfixDeclaratorAST
2986 bool Bind::visit(FunctionDeclaratorAST *ast)
2987 {
2988     Function *fun = control()->newFunction(0, 0);
2989     fun->setStartOffset(tokenAt(ast->firstToken()).begin());
2990     fun->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2991     fun->setReturnType(_type);
2992
2993     // unsigned lparen_token = ast->lparen_token;
2994     this->parameterDeclarationClause(ast->parameter_declaration_clause, ast->lparen_token, fun);
2995     // unsigned rparen_token = ast->rparen_token;
2996     FullySpecifiedType type(fun);
2997     for (SpecifierListAST *it = ast->cv_qualifier_list; it; it = it->next) {
2998         type = this->specifier(it->value, type);
2999     }
3000
3001     // propagate the cv-qualifiers
3002     fun->setConst(type.isConst());
3003     fun->setVolatile(type.isVolatile());
3004
3005     this->exceptionSpecification(ast->exception_specification, type);
3006     this->trailingReturnType(ast->trailing_return_type, type);
3007     if (ast->as_cpp_initializer != 0) {
3008         fun->setAmbiguous(true);
3009         /*ExpressionTy as_cpp_initializer =*/ this->expression(ast->as_cpp_initializer);
3010     }
3011     ast->symbol = fun;
3012     _type = type;
3013     return false;
3014 }
3015
3016 bool Bind::visit(ArrayDeclaratorAST *ast)
3017 {
3018     ExpressionTy expression = this->expression(ast->expression);
3019     FullySpecifiedType type(control()->arrayType(_type));
3020     _type = type;
3021     return false;
3022 }
3023
3024 int Bind::visibilityForAccessSpecifier(int tokenKind)
3025 {
3026     switch (tokenKind) {
3027     case T_PUBLIC:
3028         return Symbol::Public;
3029     case T_PROTECTED:
3030         return Symbol::Protected;
3031     case T_PRIVATE:
3032         return Symbol::Private;
3033     case T_Q_SIGNALS:
3034         return Symbol::Protected;
3035     default:
3036         return Symbol::Public;
3037     }
3038 }
3039
3040 int Bind::visibilityForClassKey(int tokenKind)
3041 {
3042     switch (tokenKind) {
3043     case T_CLASS:
3044         return Symbol::Private;
3045     case T_STRUCT:
3046     case T_UNION:
3047         return Symbol::Public;
3048     default:
3049         return Symbol::Public;
3050     }
3051 }
3052
3053 int Bind::visibilityForObjCAccessSpecifier(int tokenKind)
3054 {
3055     switch (tokenKind) {
3056     case T_AT_PUBLIC:
3057         return Symbol::Public;
3058     case T_AT_PROTECTED:
3059         return Symbol::Protected;
3060     case T_AT_PRIVATE:
3061         return Symbol::Private;
3062     case T_AT_PACKAGE:
3063         return Symbol::Package;
3064     default:
3065         return Symbol::Protected;
3066     }
3067 }
3068
3069 bool Bind::isObjCClassMethod(int tokenKind)
3070 {
3071     switch (tokenKind) {
3072     case T_PLUS:
3073         return true;
3074     case T_MINUS:
3075     default:
3076         return false;
3077     }
3078 }