1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
32 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
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:
41 // The above copyright notice and this permission notice shall be included in
42 // all copies or substantial portions of the Software.
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
54 #include "TranslationUnit.h"
58 #include "CoreTypes.h"
66 using namespace CPlusPlus;
68 const int Bind::kMaxDepth(100);
70 Bind::Bind(TranslationUnit *unit)
76 _visibility(Symbol::Public),
77 _objcVisibility(Symbol::Public),
78 _methodKey(Function::NormalMethod),
79 _skipFunctionBodies(false),
84 bool Bind::skipFunctionBodies() const
86 return _skipFunctionBodies;
89 void Bind::setSkipFunctionBodies(bool skipFunctionBodies)
91 _skipFunctionBodies = skipFunctionBodies;
94 unsigned Bind::location(DeclaratorAST *ast, unsigned defaultLocation) const
97 return defaultLocation;
99 else if (ast->core_declarator)
100 return location(ast->core_declarator, defaultLocation);
102 return ast->firstToken();
105 unsigned Bind::location(CoreDeclaratorAST *ast, unsigned defaultLocation) const
108 return defaultLocation;
110 else if (NestedDeclaratorAST *nested = ast->asNestedDeclarator())
111 return location(nested->declarator, defaultLocation);
113 else if (DeclaratorIdAST *id = ast->asDeclaratorId())
114 return location(id->name, defaultLocation);
116 return ast->firstToken();
119 unsigned Bind::location(NameAST *name, unsigned defaultLocation) const
122 return defaultLocation;
124 else if (DestructorNameAST *dtor = name->asDestructorName())
125 return dtor->identifier_token;
127 else if (TemplateIdAST *templId = name->asTemplateId())
128 return templId->identifier_token;
130 else if (QualifiedNameAST *q = name->asQualifiedName()) {
131 if (q->unqualified_name)
132 return location(q->unqualified_name, defaultLocation);
135 return name->firstToken();
138 void Bind::setDeclSpecifiers(Symbol *symbol, const FullySpecifiedType &declSpecifiers)
143 int storage = Symbol::NoStorage;
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;
160 symbol->setStorage(storage);
162 if (Function *funTy = symbol->asFunction()) {
163 if (declSpecifiers.isVirtual()) {
164 funTy->setVirtual(true);
168 if (declSpecifiers.isDeprecated())
169 symbol->setDeprecated(true);
171 if (declSpecifiers.isUnavailable())
172 symbol->setUnavailable(true);
175 Scope *Bind::switchScope(Scope *scope)
180 std::swap(_scope, scope);
184 int Bind::switchVisibility(int visibility)
186 std::swap(_visibility, visibility);
190 int Bind::switchObjCVisibility(int visibility)
192 std::swap(_objcVisibility, visibility);
196 int Bind::switchMethodKey(int methodKey)
198 std::swap(_methodKey, methodKey);
202 void Bind::operator()(TranslationUnitAST *ast, Namespace *globalNamespace)
204 Scope *previousScope = switchScope(globalNamespace);
205 translationUnit(ast);
206 (void) switchScope(previousScope);
209 void Bind::operator()(DeclarationAST *ast, Scope *scope)
211 Scope *previousScope = switchScope(scope);
213 (void) switchScope(previousScope);
216 FullySpecifiedType Bind::operator()(ExpressionAST *ast, Scope *scope)
218 Scope *previousScope = switchScope(scope);
219 FullySpecifiedType ty = expression(ast);
220 (void) switchScope(previousScope);
224 FullySpecifiedType Bind::operator()(NewTypeIdAST *ast, Scope *scope)
226 Scope *previousScope = switchScope(scope);
227 FullySpecifiedType ty = newTypeId(ast);
228 (void) switchScope(previousScope);
232 void Bind::statement(StatementAST *ast)
237 Bind::ExpressionTy Bind::expression(ExpressionAST *ast)
239 ExpressionTy value = ExpressionTy();
240 std::swap(_expression, value);
242 std::swap(_expression, value);
246 void Bind::declaration(DeclarationAST *ast)
251 const Name *Bind::name(NameAST *ast)
253 const Name *value = 0;
254 std::swap(_name, value);
256 std::swap(_name, value);
260 FullySpecifiedType Bind::specifier(SpecifierAST *ast, const FullySpecifiedType &init)
262 FullySpecifiedType value = init;
263 std::swap(_type, value);
265 std::swap(_type, value);
269 FullySpecifiedType Bind::ptrOperator(PtrOperatorAST *ast, const FullySpecifiedType &init)
271 FullySpecifiedType value = init;
272 std::swap(_type, value);
274 std::swap(_type, value);
278 FullySpecifiedType Bind::coreDeclarator(CoreDeclaratorAST *ast, const FullySpecifiedType &init)
280 FullySpecifiedType value = init;
281 std::swap(_type, value);
283 std::swap(_type, value);
287 FullySpecifiedType Bind::postfixDeclarator(PostfixDeclaratorAST *ast, const FullySpecifiedType &init)
289 FullySpecifiedType value = init;
290 std::swap(_type, value);
292 std::swap(_type, value);
296 bool Bind::preVisit(AST *)
299 if (_depth > kMaxDepth)
304 void Bind::postVisit(AST *)
310 bool Bind::visit(ObjCSelectorArgumentAST *ast)
313 assert(!"unreachable");
317 const Name *Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasArg)
319 if (! (ast && ast->name_token))
322 if (ast->colon_token)
325 return identifier(ast->name_token);
328 bool Bind::visit(AttributeAST *ast)
331 assert(!"unreachable");
335 void Bind::attribute(AttributeAST *ast)
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);
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);
353 // unsigned rparen_token = ast->rparen_token;
356 bool Bind::visit(DeclaratorAST *ast)
359 assert(!"unreachable");
363 FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType &init, DeclaratorIdAST **declaratorId)
365 FullySpecifiedType type = init;
370 std::swap(_declaratorId, declaratorId);
371 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
372 type = this->specifier(it->value, type);
374 for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
375 type = this->ptrOperator(it->value, type);
377 for (PostfixDeclaratorListAST *it = ast->postfix_declarator_list; it; it = it->next) {
378 type = this->postfixDeclarator(it->value, type);
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);
384 // unsigned equals_token = ast->equals_token;
385 ExpressionTy initializer = this->expression(ast->initializer);
386 std::swap(_declaratorId, declaratorId);
390 bool Bind::visit(QtPropertyDeclarationItemAST *ast)
393 assert(!"unreachable");
397 bool Bind::visit(QtInterfaceNameAST *ast)
400 assert(!"unreachable");
404 void Bind::qtInterfaceName(QtInterfaceNameAST *ast)
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);
415 bool Bind::visit(BaseSpecifierAST *ast)
418 assert(!"unreachable");
422 void Bind::baseSpecifier(BaseSpecifierAST *ast, unsigned colon_token, Class *klass)
427 unsigned sourceLocation = location(ast->name, ast->firstToken());
428 if (! sourceLocation)
429 sourceLocation = std::max(colon_token, klass->sourceLocation());
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.
439 klass->addBaseClass(baseClass);
440 ast->symbol = baseClass;
443 bool Bind::visit(CtorInitializerAST *ast)
446 assert(!"unreachable");
450 void Bind::ctorInitializer(CtorInitializerAST *ast, Function *fun)
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);
459 // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
462 bool Bind::visit(EnumeratorAST *ast)
465 assert(!"unreachable");
469 void Bind::enumerator(EnumeratorAST *ast, Enum *symbol)
476 // unsigned identifier_token = ast->identifier_token;
477 // unsigned equal_token = ast->equal_token;
478 ExpressionTy expression = this->expression(ast->expression);
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);
488 bool Bind::visit(ExceptionSpecificationAST *ast)
491 assert(!"unreachable");
495 FullySpecifiedType Bind::exceptionSpecification(ExceptionSpecificationAST *ast, const FullySpecifiedType &init)
497 FullySpecifiedType type = init;
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);
508 // unsigned rparen_token = ast->rparen_token;
512 bool Bind::visit(MemInitializerAST *ast)
515 assert(!"unreachable");
519 void Bind::memInitializer(MemInitializerAST *ast, Function *fun)
524 /*const Name *name =*/ this->name(ast->name);
526 Scope *previousScope = switchScope(fun);
527 for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
528 /*ExpressionTy value =*/ this->expression(it->value);
530 (void) switchScope(previousScope);
533 bool Bind::visit(NestedNameSpecifierAST *ast)
536 assert(!"unreachable");
540 const Name *Bind::nestedNameSpecifier(NestedNameSpecifierAST *ast)
545 const Name *class_or_namespace_name = this->name(ast->class_or_namespace_name);
546 return class_or_namespace_name;
549 bool Bind::visit(NewPlacementAST *ast)
552 assert(!"unreachable");
556 void Bind::newPlacement(NewPlacementAST *ast)
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);
565 // unsigned rparen_token = ast->rparen_token;
568 bool Bind::visit(NewArrayDeclaratorAST *ast)
571 assert(!"unreachable");
575 FullySpecifiedType Bind::newArrayDeclarator(NewArrayDeclaratorAST *ast, const FullySpecifiedType &init)
577 FullySpecifiedType type = init;
582 // unsigned lbracket_token = ast->lbracket_token;
583 ExpressionTy expression = this->expression(ast->expression);
584 // unsigned rbracket_token = ast->rbracket_token;
588 bool Bind::visit(NewInitializerAST *ast)
591 assert(!"unreachable");
595 void Bind::newInitializer(NewInitializerAST *ast)
600 // unsigned lparen_token = ast->lparen_token;
601 ExpressionTy expression = this->expression(ast->expression);
602 // unsigned rparen_token = ast->rparen_token;
605 bool Bind::visit(NewTypeIdAST *ast)
608 assert(!"unreachable");
612 FullySpecifiedType Bind::newTypeId(NewTypeIdAST *ast)
614 FullySpecifiedType type;
620 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
621 type = this->specifier(it->value, type);
623 for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
624 type = this->ptrOperator(it->value, type);
626 for (NewArrayDeclaratorListAST *it = ast->new_array_declarator_list; it; it = it->next) {
627 type = this->newArrayDeclarator(it->value, type);
632 bool Bind::visit(OperatorAST *ast)
635 assert(!"unreachable");
639 OperatorNameId::Kind Bind::cppOperator(OperatorAST *ast)
641 OperatorNameId::Kind kind = OperatorNameId::InvalidOp;
646 // unsigned op_token = ast->op_token;
647 // unsigned open_token = ast->open_token;
648 // unsigned close_token = ast->close_token;
650 switch (tokenKind(ast->op_token)) {
653 kind = OperatorNameId::NewArrayOp;
655 kind = OperatorNameId::NewOp;
660 kind = OperatorNameId::DeleteArrayOp;
662 kind = OperatorNameId::DeleteOp;
666 kind = OperatorNameId::PlusOp;
670 kind = OperatorNameId::MinusOp;
674 kind = OperatorNameId::StarOp;
678 kind = OperatorNameId::SlashOp;
682 kind = OperatorNameId::PercentOp;
686 kind = OperatorNameId::CaretOp;
690 kind = OperatorNameId::AmpOp;
694 kind = OperatorNameId::PipeOp;
698 kind = OperatorNameId::TildeOp;
702 kind = OperatorNameId::ExclaimOp;
706 kind = OperatorNameId::EqualOp;
710 kind = OperatorNameId::LessOp;
714 kind = OperatorNameId::GreaterOp;
718 kind = OperatorNameId::PlusEqualOp;
722 kind = OperatorNameId::MinusEqualOp;
726 kind = OperatorNameId::StarEqualOp;
730 kind = OperatorNameId::SlashEqualOp;
733 case T_PERCENT_EQUAL:
734 kind = OperatorNameId::PercentEqualOp;
738 kind = OperatorNameId::CaretEqualOp;
742 kind = OperatorNameId::AmpEqualOp;
746 kind = OperatorNameId::PipeEqualOp;
750 kind = OperatorNameId::LessLessOp;
753 case T_GREATER_GREATER:
754 kind = OperatorNameId::GreaterGreaterOp;
757 case T_LESS_LESS_EQUAL:
758 kind = OperatorNameId::LessLessEqualOp;
761 case T_GREATER_GREATER_EQUAL:
762 kind = OperatorNameId::GreaterGreaterEqualOp;
766 kind = OperatorNameId::EqualEqualOp;
769 case T_EXCLAIM_EQUAL:
770 kind = OperatorNameId::ExclaimEqualOp;
774 kind = OperatorNameId::LessEqualOp;
777 case T_GREATER_EQUAL:
778 kind = OperatorNameId::GreaterEqualOp;
782 kind = OperatorNameId::AmpAmpOp;
786 kind = OperatorNameId::PipePipeOp;
790 kind = OperatorNameId::PlusPlusOp;
794 kind = OperatorNameId::MinusMinusOp;
798 kind = OperatorNameId::CommaOp;
802 kind = OperatorNameId::ArrowStarOp;
806 kind = OperatorNameId::ArrowOp;
810 kind = OperatorNameId::FunctionCallOp;
814 kind = OperatorNameId::ArrayAccessOp;
818 kind = OperatorNameId::InvalidOp;
824 bool Bind::visit(ParameterDeclarationClauseAST *ast)
827 assert(!"unreachable");
831 void Bind::parameterDeclarationClause(ParameterDeclarationClauseAST *ast, unsigned lparen_token, Function *fun)
837 translationUnit()->warning(lparen_token, "undefined function");
841 Scope *previousScope = switchScope(fun);
843 for (ParameterDeclarationListAST *it = ast->parameter_declaration_list; it; it = it->next) {
844 this->declaration(it->value);
847 if (ast->dot_dot_dot_token)
848 fun->setVariadic(true);
850 (void) switchScope(previousScope);
853 bool Bind::visit(TranslationUnitAST *ast)
856 assert(!"unreachable");
860 void Bind::translationUnit(TranslationUnitAST *ast)
865 for (DeclarationListAST *it = ast->declaration_list; it; it = it->next) {
866 this->declaration(it->value);
870 bool Bind::visit(ObjCProtocolRefsAST *ast)
873 assert(!"unreachable");
877 void Bind::objCProtocolRefs(ObjCProtocolRefsAST *ast, Symbol *objcClassOrProtocol)
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);
892 bool Bind::visit(ObjCMessageArgumentAST *ast)
895 assert(!"unreachable");
899 void Bind::objCMessageArgument(ObjCMessageArgumentAST *ast)
904 ExpressionTy parameter_value_expression = this->expression(ast->parameter_value_expression);
907 bool Bind::visit(ObjCTypeNameAST *ast)
910 assert(!"unreachable");
914 FullySpecifiedType Bind::objCTypeName(ObjCTypeNameAST *ast)
917 return FullySpecifiedType();
919 // unsigned type_qualifier_token = ast->type_qualifier_token;
920 ExpressionTy type_id = this->expression(ast->type_id);
924 bool Bind::visit(ObjCInstanceVariablesDeclarationAST *ast)
927 assert(!"unreachable");
931 void Bind::objCInstanceVariablesDeclaration(ObjCInstanceVariablesDeclarationAST *ast, ObjCClass *klass)
938 // unsigned lbrace_token = ast->lbrace_token;
939 for (DeclarationListAST *it = ast->instance_variable_list; it; it = it->next) {
940 this->declaration(it->value);
942 // unsigned rbrace_token = ast->rbrace_token;
945 bool Bind::visit(ObjCPropertyAttributeAST *ast)
948 assert(!"unreachable");
952 void Bind::objCPropertyAttribute(ObjCPropertyAttributeAST *ast)
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);
962 bool Bind::visit(ObjCMessageArgumentDeclarationAST *ast)
965 assert(!"unreachable");
969 void Bind::objCMessageArgumentDeclaration(ObjCMessageArgumentDeclarationAST *ast, ObjCMethod *method)
974 FullySpecifiedType type = this->objCTypeName(ast->type_name);
976 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
977 type = this->specifier(it->value, type);
980 const Name *param_name = this->name(ast->param_name);
981 Argument *arg = control()->newArgument(location(ast->param_name, ast->firstToken()), param_name);
984 method->addMember(arg);
987 bool Bind::visit(ObjCMethodPrototypeAST *ast)
990 assert(!"unreachable");
994 ObjCMethod *Bind::objCMethodPrototype(ObjCMethodPrototypeAST *ast)
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);
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;
1012 Scope *previousScope = switchScope(method);
1013 for (ObjCMessageArgumentDeclarationListAST *it = ast->argument_list; it; it = it->next) {
1014 this->objCMessageArgumentDeclaration(it->value, method);
1016 (void) switchScope(previousScope);
1018 if (ast->dot_dot_dot_token)
1019 method->setVariadic(true);
1021 FullySpecifiedType specifiers;
1022 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
1023 specifiers = this->specifier(it->value, specifiers);
1025 //setDeclSpecifiers(method, specifiers);
1030 bool Bind::visit(ObjCSynthesizedPropertyAST *ast)
1033 assert(!"unreachable");
1037 void Bind::objCSynthesizedProperty(ObjCSynthesizedPropertyAST *ast)
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;
1047 bool Bind::visit(LambdaIntroducerAST *ast)
1050 assert(!"unreachable");
1054 void Bind::lambdaIntroducer(LambdaIntroducerAST *ast)
1059 // unsigned lbracket_token = ast->lbracket_token;
1060 this->lambdaCapture(ast->lambda_capture);
1061 // unsigned rbracket_token = ast->rbracket_token;
1064 bool Bind::visit(LambdaCaptureAST *ast)
1067 assert(!"unreachable");
1071 void Bind::lambdaCapture(LambdaCaptureAST *ast)
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);
1082 bool Bind::visit(CaptureAST *ast)
1085 assert(!"unreachable");
1089 void Bind::capture(CaptureAST *ast)
1096 bool Bind::visit(LambdaDeclaratorAST *ast)
1099 assert(!"unreachable");
1103 void Bind::lambdaDeclarator(LambdaDeclaratorAST *ast)
1109 Function *fun = 0; // ### implement me
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);
1118 // unsigned mutable_token = ast->mutable_token;
1119 type = this->exceptionSpecification(ast->exception_specification, type);
1120 type = this->trailingReturnType(ast->trailing_return_type, type);
1123 bool Bind::visit(TrailingReturnTypeAST *ast)
1126 assert(!"unreachable");
1130 FullySpecifiedType Bind::trailingReturnType(TrailingReturnTypeAST *ast, const FullySpecifiedType &init)
1132 FullySpecifiedType type = init;
1137 // unsigned arrow_token = ast->arrow_token;
1138 for (SpecifierListAST *it = ast->attributes; it; it = it->next) {
1139 type = this->specifier(it->value, type);
1141 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1142 type = this->specifier(it->value, type);
1144 DeclaratorIdAST *declaratorId = 0;
1145 type = this->declarator(ast->declarator, type, &declaratorId);
1151 bool Bind::visit(QtMemberDeclarationAST *ast)
1153 const Name *name = 0;
1155 if (tokenKind(ast->q_token) == T_Q_D)
1156 name = control()->identifier("d");
1158 name = control()->identifier("q");
1160 FullySpecifiedType declTy = this->expression(ast->type_id);
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";
1169 const Name *privName = control()->identifier(privateClass.c_str(), privateClass.size());
1170 declTy.setType(control()->namedType(privName));
1175 Declaration *symbol = control()->newDeclaration(/*generated*/ 0, name);
1176 symbol->setType(control()->pointerType(declTy));
1178 _scope->addMember(symbol);
1182 bool Bind::visit(CaseStatementAST *ast)
1184 ExpressionTy expression = this->expression(ast->expression);
1185 this->statement(ast->statement);
1189 bool Bind::visit(CompoundStatementAST *ast)
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);
1201 (void) switchScope(previousScope);
1205 bool Bind::visit(DeclarationStatementAST *ast)
1207 this->declaration(ast->declaration);
1211 bool Bind::visit(DoStatementAST *ast)
1213 this->statement(ast->statement);
1214 ExpressionTy expression = this->expression(ast->expression);
1218 bool Bind::visit(ExpressionOrDeclarationStatementAST *ast)
1220 this->statement(ast->expression);
1221 this->statement(ast->declaration);
1225 bool Bind::visit(ExpressionStatementAST *ast)
1227 ExpressionTy expression = this->expression(ast->expression);
1228 // unsigned semicolon_token = ast->semicolon_token;
1232 bool Bind::visit(ForeachStatementAST *ast)
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;
1241 Scope *previousScope = switchScope(block);
1243 FullySpecifiedType type;
1244 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1245 type = this->specifier(it->value, type);
1247 DeclaratorIdAST *declaratorId = 0;
1248 type = this->declarator(ast->declarator, type, &declaratorId);
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);
1257 /*ExpressionTy initializer =*/ this->expression(ast->initializer);
1258 /*ExpressionTy expression =*/ this->expression(ast->expression);
1259 this->statement(ast->statement);
1260 (void) switchScope(previousScope);
1264 bool Bind::visit(ForStatementAST *ast)
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;
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);
1284 bool Bind::visit(IfStatementAST *ast)
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;
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);
1301 bool Bind::visit(LabeledStatementAST *ast)
1303 // unsigned label_token = ast->label_token;
1304 // unsigned colon_token = ast->colon_token;
1305 this->statement(ast->statement);
1309 bool Bind::visit(BreakStatementAST *ast)
1312 // unsigned break_token = ast->break_token;
1313 // unsigned semicolon_token = ast->semicolon_token;
1317 bool Bind::visit(ContinueStatementAST *ast)
1320 // unsigned continue_token = ast->continue_token;
1321 // unsigned semicolon_token = ast->semicolon_token;
1325 bool Bind::visit(GotoStatementAST *ast)
1328 // unsigned goto_token = ast->goto_token;
1329 // unsigned identifier_token = ast->identifier_token;
1330 // unsigned semicolon_token = ast->semicolon_token;
1334 bool Bind::visit(ReturnStatementAST *ast)
1336 ExpressionTy expression = this->expression(ast->expression);
1340 bool Bind::visit(SwitchStatementAST *ast)
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;
1349 Scope *previousScope = switchScope(block);
1350 /*ExpressionTy condition =*/ this->expression(ast->condition);
1351 this->statement(ast->statement);
1352 (void) switchScope(previousScope);
1356 bool Bind::visit(TryBlockStatementAST *ast)
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);
1366 bool Bind::visit(CatchClauseAST *ast)
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;
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);
1383 bool Bind::visit(WhileStatementAST *ast)
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;
1392 Scope *previousScope = switchScope(block);
1393 /*ExpressionTy condition =*/ this->expression(ast->condition);
1394 this->statement(ast->statement);
1395 (void) switchScope(previousScope);
1399 bool Bind::visit(ObjCFastEnumerationAST *ast)
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;
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);
1413 DeclaratorIdAST *declaratorId = 0;
1414 type = this->declarator(ast->declarator, type, &declaratorId);
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);
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);
1430 bool Bind::visit(ObjCSynchronizedStatementAST *ast)
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);
1442 bool Bind::visit(IdExpressionAST *ast)
1444 /*const Name *name =*/ this->name(ast->name);
1448 bool Bind::visit(CompoundExpressionAST *ast)
1450 // unsigned lparen_token = ast->lparen_token;
1451 this->statement(ast->statement);
1452 // unsigned rparen_token = ast->rparen_token;
1456 bool Bind::visit(CompoundLiteralAST *ast)
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);
1465 bool Bind::visit(QtMethodAST *ast)
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;
1476 bool Bind::visit(BinaryExpressionAST *ast)
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);
1484 bool Bind::visit(CastExpressionAST *ast)
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);
1493 bool Bind::visit(ConditionAST *ast)
1495 FullySpecifiedType type;
1496 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1497 type = this->specifier(it->value, type);
1499 DeclaratorIdAST *declaratorId = 0;
1500 type = this->declarator(ast->declarator, type, &declaratorId);
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);
1512 bool Bind::visit(ConditionalExpressionAST *ast)
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);
1522 bool Bind::visit(CppCastExpressionAST *ast)
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;
1534 bool Bind::visit(DeleteExpressionAST *ast)
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);
1544 bool Bind::visit(ArrayInitializerAST *ast)
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);
1550 // unsigned rbrace_token = ast->rbrace_token;
1554 bool Bind::visit(NewExpressionAST *ast)
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);
1567 bool Bind::visit(TypeidExpressionAST *ast)
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;
1576 bool Bind::visit(TypenameCallExpressionAST *ast)
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);
1584 // unsigned rparen_token = ast->rparen_token;
1588 bool Bind::visit(TypeConstructorCallAST *ast)
1590 FullySpecifiedType type;
1591 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1592 type = this->specifier(it->value, type);
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);
1598 // unsigned rparen_token = ast->rparen_token;
1602 bool Bind::visit(SizeofExpressionAST *ast)
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;
1612 bool Bind::visit(NumericLiteralAST *ast)
1615 // unsigned literal_token = ast->literal_token;
1619 bool Bind::visit(BoolLiteralAST *ast)
1622 // unsigned literal_token = ast->literal_token;
1626 bool Bind::visit(ThisExpressionAST *ast)
1629 // unsigned this_token = ast->this_token;
1633 bool Bind::visit(NestedExpressionAST *ast)
1635 // unsigned lparen_token = ast->lparen_token;
1636 ExpressionTy expression = this->expression(ast->expression);
1637 // unsigned rparen_token = ast->rparen_token;
1641 bool Bind::visit(StringLiteralAST *ast)
1643 // unsigned literal_token = ast->literal_token;
1644 ExpressionTy next = this->expression(ast->next);
1648 bool Bind::visit(ThrowExpressionAST *ast)
1650 // unsigned throw_token = ast->throw_token;
1651 ExpressionTy expression = this->expression(ast->expression);
1655 bool Bind::visit(TypeIdAST *ast)
1657 FullySpecifiedType type;
1658 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1659 type = this->specifier(it->value, type);
1661 DeclaratorIdAST *declaratorId = 0;
1662 type = this->declarator(ast->declarator, type, &declaratorId);
1667 bool Bind::visit(UnaryExpressionAST *ast)
1669 // unsigned unary_op_token = ast->unary_op_token;
1670 ExpressionTy expression = this->expression(ast->expression);
1674 bool Bind::visit(ObjCMessageExpressionAST *ast)
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);
1682 // unsigned rbracket_token = ast->rbracket_token;
1686 bool Bind::visit(ObjCProtocolExpressionAST *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;
1696 bool Bind::visit(ObjCEncodeExpressionAST *ast)
1698 // unsigned encode_token = ast->encode_token;
1699 FullySpecifiedType type = this->objCTypeName(ast->type_name);
1703 bool Bind::visit(ObjCSelectorExpressionAST *ast)
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;
1712 bool Bind::visit(LambdaExpressionAST *ast)
1714 this->lambdaIntroducer(ast->lambda_introducer);
1715 this->lambdaDeclarator(ast->lambda_declarator);
1716 this->statement(ast->statement);
1720 bool Bind::visit(BracedInitializerAST *ast)
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);
1726 // unsigned comma_token = ast->comma_token;
1727 // unsigned rbrace_token = ast->rbrace_token;
1731 static int methodKeyForInvokableToken(int kind)
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;
1740 return Function::NormalMethod;
1744 bool Bind::visit(SimpleDeclarationAST *ast)
1746 int methodKey = _methodKey;
1747 if (ast->qt_invokable_token)
1748 methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token));
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);
1756 List<Symbol *> **symbolTail = &ast->symbols;
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();
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;
1771 ForwardClassDeclaration *decl = control()->newForwardClassDeclaration(sourceLocation, name);
1772 setDeclSpecifiers(decl, type);
1773 _scope->addMember(decl);
1775 *symbolTail = new (translationUnit()->memoryPool()) List<Symbol *>(decl);
1776 symbolTail = &(*symbolTail)->next;
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);
1784 const Name *declName = 0;
1785 unsigned sourceLocation = location(it->value, ast->firstToken());
1786 if (declaratorId && declaratorId->name) {
1787 declName = declaratorId->name->name;
1790 Declaration *decl = control()->newDeclaration(sourceLocation, declName);
1791 decl->setType(declTy);
1792 setDeclSpecifiers(decl, type);
1794 if (Function *fun = decl->type()->asFunctionType()) {
1795 fun->setScope(_scope);
1797 setDeclSpecifiers(fun, type);
1798 if (declaratorId && declaratorId->name)
1799 fun->setName(declaratorId->name->name); // update the function name
1802 if (_scope->isClass()) {
1803 decl->setVisibility(_visibility);
1805 if (Function *funTy = decl->type()->asFunctionType()) {
1806 funTy->setMethodKey(methodKey);
1808 if (funTy->isVirtual() && it->value->equal_token)
1809 funTy->setPureVirtual(true);
1813 _scope->addMember(decl);
1815 *symbolTail = new (translationUnit()->memoryPool()) List<Symbol *>(decl);
1816 symbolTail = &(*symbolTail)->next;
1821 bool Bind::visit(EmptyDeclarationAST *ast)
1824 unsigned semicolon_token = ast->semicolon_token;
1825 if (_scope && (_scope->isClass() || _scope->isNamespace())) {
1826 const Token &tk = tokenAt(semicolon_token);
1828 if (! tk.generated())
1829 translationUnit()->warning(semicolon_token, "extra `;'");
1834 bool Bind::visit(AccessDeclarationAST *ast)
1836 const int accessSpecifier = tokenKind(ast->access_specifier_token);
1837 _visibility = visibilityForAccessSpecifier(accessSpecifier);
1839 if (ast->slots_token)
1840 _methodKey = Function::SlotMethod;
1841 else if (accessSpecifier == T_Q_SIGNALS)
1842 _methodKey = Function::SignalMethod;
1844 _methodKey = Function::NormalMethod;
1849 bool Bind::visit(QtObjectTagAST *ast)
1852 // unsigned q_object_token = ast->q_object_token;
1856 bool Bind::visit(QtPrivateSlotAST *ast)
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);
1868 DeclaratorIdAST *declaratorId = 0;
1869 type = this->declarator(ast->declarator, type, &declaratorId);
1870 // unsigned rparen_token = ast->rparen_token;
1874 static void qtPropertyAttribute(TranslationUnit *unit, ExpressionAST *expression,
1876 QtPropertyDeclaration::Flag flag,
1877 QtPropertyDeclaration::Flag function)
1881 *flags &= ~function & ~flag;
1882 if (BoolLiteralAST *boollit = expression->asBoolLiteral()) {
1883 const int kind = unit->tokenAt(boollit->literal_token).kind();
1891 bool Bind::visit(QtPropertyDeclarationAST *ast)
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);
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);
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)
1910 this->expression(it->value->expression);
1912 std::string name = spell(it->value->item_name_token);
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);
1940 propertyDeclaration->setFlags(flags);
1941 _scope->addMember(propertyDeclaration);
1942 // unsigned rparen_token = ast->rparen_token;
1946 bool Bind::visit(QtEnumDeclarationAST *ast)
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);
1954 QtEnum *qtEnum = control()->newQtEnum(it->value->firstToken(), value);
1955 _scope->addMember(qtEnum);
1958 // unsigned rparen_token = ast->rparen_token;
1962 bool Bind::visit(QtFlagsDeclarationAST *ast)
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);
1969 // unsigned rparen_token = ast->rparen_token;
1973 bool Bind::visit(QtInterfacesDeclarationAST *ast)
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);
1980 // unsigned rparen_token = ast->rparen_token;
1984 bool Bind::visit(AsmDefinitionAST *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;
1995 bool Bind::visit(ExceptionDeclarationAST *ast)
1997 FullySpecifiedType type;
1998 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
1999 type = this->specifier(it->value, type);
2001 DeclaratorIdAST *declaratorId = 0;
2002 type = this->declarator(ast->declarator, type, &declaratorId);
2003 // unsigned dot_dot_dot_token = ast->dot_dot_dot_token;
2007 bool Bind::visit(FunctionDefinitionAST *ast)
2009 int methodKey = _methodKey;
2010 if (ast->qt_invokable_token)
2011 methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token));
2013 FullySpecifiedType declSpecifiers;
2014 for (SpecifierListAST *it = ast->decl_specifier_list; it; it = it->next) {
2015 declSpecifiers = this->specifier(it->value, declSpecifiers);
2017 DeclaratorIdAST *declaratorId = 0;
2018 FullySpecifiedType type = this->declarator(ast->declarator, declSpecifiers.qualifiedType(), &declaratorId);
2020 Function *fun = type->asFunctionType();
2024 setDeclSpecifiers(fun, declSpecifiers);
2026 if (_scope->isClass()) {
2027 fun->setVisibility(_visibility);
2028 fun->setMethodKey(methodKey);
2031 if (declaratorId && declaratorId->name) {
2032 fun->setSourceLocation(location(declaratorId, ast->firstToken()), translationUnit());
2033 fun->setName(declaratorId->name->name);
2036 _scope->addMember(fun);
2038 translationUnit()->warning(ast->firstToken(), "expected a function declarator");
2040 this->ctorInitializer(ast->ctor_initializer, fun);
2042 if (fun && ! _skipFunctionBodies && ast->function_body) {
2043 Scope *previousScope = switchScope(fun);
2044 this->statement(ast->function_body);
2045 (void) switchScope(previousScope);
2047 if (CompoundStatementAST *c = ast->function_body->asCompoundStatement()) {
2049 fun->setEndOffset(c->symbol->endOffset());
2057 bool Bind::visit(LinkageBodyAST *ast)
2059 // unsigned lbrace_token = ast->lbrace_token;
2060 for (DeclarationListAST *it = ast->declaration_list; it; it = it->next) {
2061 this->declaration(it->value);
2063 // unsigned rbrace_token = ast->rbrace_token;
2067 bool Bind::visit(LinkageSpecificationAST *ast)
2069 // unsigned extern_token = ast->extern_token;
2070 // unsigned extern_type_token = ast->extern_type_token;
2071 this->declaration(ast->declaration);
2075 bool Bind::visit(NamespaceAST *ast)
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);
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);
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());
2095 _scope->addMember(ns);
2097 Scope *previousScope = switchScope(ns);
2098 this->declaration(ast->linkage_body);
2099 (void) switchScope(previousScope);
2103 bool Bind::visit(NamespaceAliasDefinitionAST *ast)
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);
2112 NamespaceAlias *namespaceAlias = control()->newNamespaceAlias(sourceLocation, name);
2113 namespaceAlias->setNamespaceName(this->name(ast->name));
2114 _scope->addMember(namespaceAlias);
2118 bool Bind::visit(ParameterDeclarationAST *ast)
2120 FullySpecifiedType type;
2121 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
2122 type = this->specifier(it->value, type);
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);
2129 unsigned sourceLocation = ast->firstToken();
2131 sourceLocation = declaratorId->firstToken();
2133 const Name *argName = 0;
2134 if (declaratorId && declaratorId->name)
2135 argName = declaratorId->name->name;
2137 Argument *arg = control()->newArgument(location(declaratorId, ast->firstToken()), argName);
2140 if (ast->expression) {
2141 unsigned startOfExpression = ast->expression->firstToken();
2142 unsigned endOfExpression = ast->expression->lastToken();
2144 for (unsigned index = startOfExpression; index != endOfExpression; ++index) {
2145 const Token &tk = tokenAt(index);
2146 if (tk.whitespace() || tk.newline())
2148 buffer += tk.spell();
2150 const StringLiteral *initializer = control()->stringLiteral(buffer.c_str(), buffer.size());
2151 arg->setInitializer(initializer);
2154 _scope->addMember(arg);
2160 bool Bind::visit(TemplateDeclarationAST *ast)
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);
2168 for (DeclarationListAST *it = ast->template_parameter_list; it; it = it->next) {
2169 this->declaration(it->value);
2171 // unsigned greater_token = ast->greater_token;
2172 this->declaration(ast->declaration);
2173 (void) switchScope(previousScope);
2175 if (Symbol *decl = templ->declaration()) {
2176 templ->setSourceLocation(decl->sourceLocation(), translationUnit());
2177 templ->setName(decl->name());
2180 _scope->addMember(templ);
2184 bool Bind::visit(TypenameTypeParameterAST *ast)
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);
2192 TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
2193 arg->setType(type_id);
2195 _scope->addMember(arg);
2199 bool Bind::visit(TemplateTypeParameterAST *ast)
2201 unsigned sourceLocation = location(ast->name, ast->firstToken());
2203 // unsigned template_token = ast->template_token;
2204 // unsigned less_token = ast->less_token;
2205 // ### process the template prototype
2207 for (DeclarationListAST *it = ast->template_parameter_list; it; it = it->next) {
2208 this->declaration(it->value);
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;
2215 const Name *name = this->name(ast->name);
2216 ExpressionTy type_id = this->expression(ast->type_id);
2218 // ### introduce TemplateTypeArgument
2219 TypenameArgument *arg = control()->newTypenameArgument(sourceLocation, name);
2220 arg->setType(type_id);
2222 _scope->addMember(arg);
2227 bool Bind::visit(UsingAST *ast)
2229 unsigned sourceLocation = location(ast->name, ast->firstToken());
2230 const Name *name = this->name(ast->name);
2232 UsingDeclaration *symbol = control()->newUsingDeclaration(sourceLocation, name);
2233 ast->symbol = symbol;
2234 _scope->addMember(symbol);
2238 bool Bind::visit(UsingDirectiveAST *ast)
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);
2248 bool Bind::visit(ObjCClassForwardDeclarationAST *ast)
2250 FullySpecifiedType declSpecifiers;
2251 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2252 declSpecifiers = this->specifier(it->value, declSpecifiers);
2255 List<ObjCForwardClassDeclaration *> **symbolTail = &ast->symbols;
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);
2261 const unsigned sourceLocation = location(it->value, ast->firstToken());
2262 ObjCForwardClassDeclaration *fwd = control()->newObjCForwardClassDeclaration(sourceLocation, name);
2263 setDeclSpecifiers(fwd, declSpecifiers);
2264 _scope->addMember(fwd);
2266 *symbolTail = new (translationUnit()->memoryPool()) List<ObjCForwardClassDeclaration *>(fwd);
2267 symbolTail = &(*symbolTail)->next;
2273 unsigned Bind::calculateScopeStart(ObjCClassDeclarationAST *ast) const
2275 if (ast->inst_vars_decl)
2276 if (unsigned pos = ast->inst_vars_decl->lbrace_token)
2277 return tokenAt(pos).end();
2279 if (ast->protocol_refs)
2280 if (unsigned pos = ast->protocol_refs->lastToken())
2281 return tokenAt(pos - 1).end();
2283 if (ast->superclass)
2284 if (unsigned pos = ast->superclass->lastToken())
2285 return tokenAt(pos - 1).end();
2287 if (ast->colon_token)
2288 return tokenAt(ast->colon_token).end();
2290 if (ast->rparen_token)
2291 return tokenAt(ast->rparen_token).end();
2293 if (ast->category_name)
2294 if (unsigned pos = ast->category_name->lastToken())
2295 return tokenAt(pos - 1).end();
2297 if (ast->lparen_token)
2298 return tokenAt(ast->lparen_token).end();
2300 if (ast->class_name)
2301 if (unsigned pos = ast->class_name->lastToken())
2302 return tokenAt(pos - 1).end();
2304 return tokenAt(ast->firstToken()).begin();
2307 bool Bind::visit(ObjCClassDeclarationAST *ast)
2309 FullySpecifiedType declSpecifiers;
2310 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2311 declSpecifiers = this->specifier(it->value, declSpecifiers);
2313 const Name *class_name = this->name(ast->class_name);
2314 const Name *category_name = this->name(ast->category_name);
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);
2321 klass->setStartOffset(calculateScopeStart(ast));
2322 klass->setEndOffset(tokenAt(ast->lastToken() - 1).begin());
2324 if (ast->interface_token)
2325 klass->setInterface(true);
2327 klass->setCategoryName(category_name);
2329 Scope *previousScope = switchScope(klass);
2331 if (const Name *superclass = this->name(ast->superclass)) {
2332 ObjCBaseClass *superKlass = control()->newObjCBaseClass(ast->superclass->firstToken(), superclass);
2333 klass->setBaseClass(superKlass);
2336 this->objCProtocolRefs(ast->protocol_refs, klass);
2338 const int previousObjCVisibility = switchObjCVisibility(Function::Protected);
2340 this->objCInstanceVariablesDeclaration(ast->inst_vars_decl, klass);
2342 (void) switchObjCVisibility(Function::Public);
2343 for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
2344 this->declaration(it->value);
2347 (void) switchObjCVisibility(previousObjCVisibility);
2348 (void) switchScope(previousScope);
2352 bool Bind::visit(ObjCProtocolForwardDeclarationAST *ast)
2354 FullySpecifiedType declSpecifiers;
2355 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2356 declSpecifiers = this->specifier(it->value, declSpecifiers);
2359 List<ObjCForwardProtocolDeclaration *> **symbolTail = &ast->symbols;
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);
2365 const unsigned sourceLocation = location(it->value, ast->firstToken());
2366 ObjCForwardProtocolDeclaration *fwd = control()->newObjCForwardProtocolDeclaration(sourceLocation, name);
2367 setDeclSpecifiers(fwd, declSpecifiers);
2368 _scope->addMember(fwd);
2370 *symbolTail = new (translationUnit()->memoryPool()) List<ObjCForwardProtocolDeclaration *>(fwd);
2371 symbolTail = &(*symbolTail)->next;
2377 unsigned Bind::calculateScopeStart(ObjCProtocolDeclarationAST *ast) const
2379 if (ast->protocol_refs)
2380 if (unsigned pos = ast->protocol_refs->lastToken())
2381 return tokenAt(pos - 1).end();
2383 if (unsigned pos = ast->name->lastToken())
2384 return tokenAt(pos - 1).end();
2386 return tokenAt(ast->firstToken()).begin();
2389 bool Bind::visit(ObjCProtocolDeclarationAST *ast)
2391 FullySpecifiedType type;
2392 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2393 type = this->specifier(it->value, type);
2395 // unsigned protocol_token = ast->protocol_token;
2396 const Name *name = this->name(ast->name);
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);
2405 Scope *previousScope = switchScope(protocol);
2406 const int previousObjCVisibility = switchObjCVisibility(Function::Public);
2408 this->objCProtocolRefs(ast->protocol_refs, protocol);
2409 for (DeclarationListAST *it = ast->member_declaration_list; it; it = it->next) {
2410 this->declaration(it->value);
2413 (void) switchObjCVisibility(previousObjCVisibility);
2414 (void) switchScope(previousScope);
2418 bool Bind::visit(ObjCVisibilityDeclarationAST *ast)
2420 _objcVisibility = visibilityForObjCAccessSpecifier(tokenKind(ast->visibility_token));
2424 bool Bind::visit(ObjCPropertyDeclarationAST *ast)
2427 FullySpecifiedType type;
2428 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2429 type = this->specifier(it->value, type);
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);
2436 // unsigned rparen_token = ast->rparen_token;
2437 this->declaration(ast->simple_declaration);
2438 // List<ObjCPropertyDeclaration *> *symbols = ast->symbols;
2442 bool Bind::visit(ObjCMethodDeclarationAST *ast)
2444 ObjCMethod *method = this->objCMethodPrototype(ast->method_prototype);
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);
2462 bool Bind::visit(ObjCSynthesizedPropertiesDeclarationAST *ast)
2464 // unsigned synthesized_token = ast->synthesized_token;
2465 for (ObjCSynthesizedPropertyListAST *it = ast->property_identifier_list; it; it = it->next) {
2466 this->objCSynthesizedProperty(it->value);
2468 // unsigned semicolon_token = ast->semicolon_token;
2472 bool Bind::visit(ObjCDynamicPropertiesDeclarationAST *ast)
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);
2478 // unsigned semicolon_token = ast->semicolon_token;
2484 bool Bind::visit(ObjCSelectorAST *ast) // ### review
2486 std::vector<const Name *> arguments;
2487 bool hasArgs = false;
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);
2494 if (! arguments.empty()) {
2495 _name = control()->selectorNameId(&arguments[0], arguments.size(), hasArgs);
2502 bool Bind::visit(QualifiedNameAST *ast)
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);
2509 _name = class_or_namespace_name;
2512 const Name *unqualified_name = this->name(ast->unqualified_name);
2513 if (_name || ast->global_scope_token)
2514 _name = control()->qualifiedNameId(_name, unqualified_name);
2516 _name = unqualified_name;
2522 bool Bind::visit(OperatorFunctionIdAST *ast)
2524 const OperatorNameId::Kind op = this->cppOperator(ast->op);
2525 ast->name = _name = control()->operatorNameId(op);
2529 bool Bind::visit(ConversionFunctionIdAST *ast)
2531 FullySpecifiedType type;
2532 for (SpecifierListAST *it = ast->type_specifier_list; it; it = it->next) {
2533 type = this->specifier(it->value, type);
2535 for (PtrOperatorListAST *it = ast->ptr_operator_list; it; it = it->next) {
2536 type = this->ptrOperator(it->value, type);
2538 ast->name = _name = control()->conversionNameId(type);
2542 bool Bind::visit(SimpleNameAST *ast)
2544 const Identifier *id = identifier(ast->identifier_token);
2550 bool Bind::visit(DestructorNameAST *ast)
2552 const Identifier *id = identifier(ast->identifier_token);
2553 _name = control()->destructorNameId(id);
2558 bool Bind::visit(TemplateIdAST *ast)
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);
2567 const Identifier *id = identifier(ast->identifier_token);
2568 if (templateArguments.empty())
2569 _name = control()->templateNameId(id);
2571 _name = control()->templateNameId(id, &templateArguments[0], templateArguments.size());
2579 bool Bind::visit(SimpleSpecifierAST *ast)
2581 switch (tokenKind(ast->specifier_token)) {
2583 if (_type.isConst())
2584 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2585 _type.setConst(true);
2589 if (_type.isVolatile())
2590 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2591 _type.setVolatile(true);
2595 if (_type.isFriend())
2596 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2597 _type.setFriend(true);
2602 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2603 _type.setAuto(true);
2607 if (_type.isRegister())
2608 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2609 _type.setRegister(true);
2613 if (_type.isStatic())
2614 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2615 _type.setStatic(true);
2619 if (_type.isExtern())
2620 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2621 _type.setExtern(true);
2625 if (_type.isMutable())
2626 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2627 _type.setMutable(true);
2631 if (_type.isTypedef())
2632 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2633 _type.setTypedef(true);
2637 if (_type.isInline())
2638 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2639 _type.setInline(true);
2643 if (_type.isVirtual())
2644 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2645 _type.setVirtual(true);
2649 if (_type.isExplicit())
2650 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2651 _type.setExplicit(true);
2655 if (_type.isSigned())
2656 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2657 _type.setSigned(true);
2661 if (_type.isUnsigned())
2662 translationUnit()->error(ast->specifier_token, "duplicate `%s'", spell(ast->specifier_token));
2663 _type.setUnsigned(true);
2668 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2669 _type.setType(control()->integerType(IntegerType::Char));
2674 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2675 _type.setType(control()->integerType(IntegerType::WideChar));
2680 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2681 _type.setType(control()->integerType(IntegerType::Bool));
2686 IntegerType *intType = control()->integerType(IntegerType::Int);
2687 if (_type.type() != intType)
2688 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2690 _type.setType(control()->integerType(IntegerType::Short));
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)
2701 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2703 _type.setType(control()->integerType(IntegerType::Int));
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));
2715 } else if (tp == doubleType) {
2716 _type.setType(control()->floatType(FloatType::LongDouble));
2718 } else if (tp != intType) {
2719 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2722 _type.setType(control()->integerType(IntegerType::Long));
2727 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2728 _type.setType(control()->floatType(FloatType::Float));
2733 IntegerType *longType = control()->integerType(IntegerType::Long);
2734 if (_type.type() == longType) {
2735 _type.setType(control()->floatType(FloatType::LongDouble));
2738 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2740 _type.setType(control()->floatType(FloatType::Double));
2745 translationUnit()->error(ast->specifier_token, "duplicate data type in declaration");
2746 _type.setType(control()->voidType());
2755 bool Bind::visit(AttributeSpecifierAST *ast)
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);
2763 // unsigned first_rparen_token = ast->first_rparen_token;
2764 // unsigned second_rparen_token = ast->second_rparen_token;
2768 bool Bind::visit(TypeofSpecifierAST *ast)
2770 ExpressionTy expression = this->expression(ast->expression);
2775 bool Bind::visit(ClassSpecifierAST *ast)
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
2781 for (SpecifierListAST *it = ast->attribute_list; it; it = it->next) {
2782 _type = this->specifier(it->value, _type);
2785 const Name *className = this->name(ast->name);
2788 sourceLocation = location(ast->name, sourceLocation);
2789 startScopeOffset = tokenAt(sourceLocation).end(); // at the end of the class name
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
2799 Class *klass = control()->newClass(sourceLocation, className);
2800 klass->setStartOffset(startScopeOffset);
2801 klass->setEndOffset(tokenAt(ast->lastToken() - 1).end());
2802 _scope->addMember(klass);
2804 if (_scope->isClass())
2805 klass->setVisibility(_visibility);
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);
2816 _type.setType(klass);
2818 Scope *previousScope = switchScope(klass);
2819 const int previousVisibility = switchVisibility(visibilityForClassKey(classKey));
2820 const int previousMethodKey = switchMethodKey(Function::NormalMethod);
2822 for (BaseSpecifierListAST *it = ast->base_clause_list; it; it = it->next) {
2823 this->baseSpecifier(it->value, ast->colon_token, klass);
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);
2830 (void) switchMethodKey(previousMethodKey);
2831 (void) switchVisibility(previousVisibility);
2832 (void) switchScope(previousScope);
2834 ast->symbol = klass;
2838 bool Bind::visit(NamedTypeSpecifierAST *ast)
2840 _type.setType(control()->namedType(this->name(ast->name)));
2844 bool Bind::visit(ElaboratedTypeSpecifierAST *ast)
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);
2850 _type.setType(control()->namedType(this->name(ast->name)));
2854 bool Bind::visit(EnumSpecifierAST *ast)
2856 unsigned sourceLocation = location(ast->name, ast->firstToken());
2857 const Name *enumName = this->name(ast->name);
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());
2863 _scope->addMember(e);
2865 if (_scope->isClass())
2866 e->setVisibility(_visibility);
2868 Scope *previousScope = switchScope(e);
2869 for (EnumeratorListAST *it = ast->enumerator_list; it; it = it->next) {
2870 this->enumerator(it->value, e);
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");
2880 (void) switchScope(previousScope);
2886 bool Bind::visit(PointerToMemberAST *ast)
2888 const Name *memberName = 0;
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);
2895 memberName = class_or_namespace_name;
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);
2906 bool Bind::visit(PointerAST *ast)
2908 if (_type->isReferenceType())
2909 translationUnit()->error(ast->firstToken(), "cannot declare pointer to a reference");
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);
2919 bool Bind::visit(ReferenceAST *ast)
2921 const bool rvalueRef = (tokenKind(ast->reference_token) == T_AMPER_AMPER);
2923 if (_type->isReferenceType())
2924 translationUnit()->error(ast->firstToken(), "cannot declare reference to a reference");
2926 FullySpecifiedType type(control()->referenceType(_type, rvalueRef));
2933 bool Bind::visit(CallAST *ast)
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);
2940 // unsigned rparen_token = ast->rparen_token;
2944 bool Bind::visit(ArrayAccessAST *ast)
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;
2953 bool Bind::visit(PostIncrDecrAST *ast)
2955 ExpressionTy base_expression = this->expression(ast->base_expression);
2956 // unsigned incr_decr_token = ast->incr_decr_token;
2960 bool Bind::visit(MemberAccessAST *ast)
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);
2970 // CoreDeclaratorAST
2971 bool Bind::visit(DeclaratorIdAST *ast)
2973 /*const Name *name =*/ this->name(ast->name);
2974 *_declaratorId = ast;
2978 bool Bind::visit(NestedDeclaratorAST *ast)
2980 _type = this->declarator(ast->declarator, _type, _declaratorId);
2985 // PostfixDeclaratorAST
2986 bool Bind::visit(FunctionDeclaratorAST *ast)
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);
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);
3001 // propagate the cv-qualifiers
3002 fun->setConst(type.isConst());
3003 fun->setVolatile(type.isVolatile());
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);
3016 bool Bind::visit(ArrayDeclaratorAST *ast)
3018 ExpressionTy expression = this->expression(ast->expression);
3019 FullySpecifiedType type(control()->arrayType(_type));
3024 int Bind::visibilityForAccessSpecifier(int tokenKind)
3026 switch (tokenKind) {
3028 return Symbol::Public;
3030 return Symbol::Protected;
3032 return Symbol::Private;
3034 return Symbol::Protected;
3036 return Symbol::Public;
3040 int Bind::visibilityForClassKey(int tokenKind)
3042 switch (tokenKind) {
3044 return Symbol::Private;
3047 return Symbol::Public;
3049 return Symbol::Public;
3053 int Bind::visibilityForObjCAccessSpecifier(int tokenKind)
3055 switch (tokenKind) {
3057 return Symbol::Public;
3058 case T_AT_PROTECTED:
3059 return Symbol::Protected;
3061 return Symbol::Private;
3063 return Symbol::Package;
3065 return Symbol::Protected;
3069 bool Bind::isObjCClassMethod(int tokenKind)
3071 switch (tokenKind) {