1 /****************************************************************************
3 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
5 ** This file is part of Qt Jambi.
7 ** ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 ****************************************************************************/
25 /* This file is part of KDevelop
26 Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
28 This library is free software; you can redistribute it and/or
29 modify it under the terms of the GNU Library General Public
30 License version 2 as published by the Free Software Foundation.
32 This library is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 Library General Public License for more details.
37 You should have received a copy of the GNU Library General Public License
38 along with this library; see the file COPYING.LIB. If not, write to
39 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
40 Boston, MA 02110-1301, USA.
46 #include "smallobject.h"
49 #define DECLARE_AST_NODE(k) \
50 enum { __node_kind = Kind_##k };
52 struct AccessSpecifierAST
;
53 struct AsmDefinitionAST
;
55 struct BaseSpecifierAST
;
56 struct BinaryExpressionAST
;
57 struct CastExpressionAST
;
58 struct ClassMemberAccessAST
;
59 struct ClassSpecifierAST
;
60 struct CompoundStatementAST
;
62 struct ConditionalExpressionAST
;
63 struct CppCastExpressionAST
;
64 struct CtorInitializerAST
;
65 struct DeclarationAST
;
66 struct DeclarationStatementAST
;
68 struct DeleteExpressionAST
;
69 struct DoStatementAST
;
70 struct ElaboratedTypeSpecifierAST
;
71 struct EnumSpecifierAST
;
73 struct ExceptionSpecificationAST
;
75 struct ExpressionOrDeclarationStatementAST
;
76 struct ExpressionStatementAST
;
77 struct ForStatementAST
;
78 struct FunctionCallAST
;
79 struct FunctionDefinitionAST
;
80 struct IfStatementAST
;
81 struct IncrDecrExpressionAST
;
82 struct InitDeclaratorAST
;
83 struct InitializerAST
;
84 struct InitializerClauseAST
;
85 struct LabeledStatementAST
;
86 struct LinkageBodyAST
;
87 struct LinkageSpecificationAST
;
88 struct MemInitializerAST
;
91 struct NamespaceAliasDefinitionAST
;
92 struct NewDeclaratorAST
;
93 struct NewExpressionAST
;
94 struct NewInitializerAST
;
97 struct OperatorFunctionIdAST
;
98 struct ParameterDeclarationAST
;
99 struct ParameterDeclarationClauseAST
;
100 struct PostfixExpressionAST
;
101 struct PrimaryExpressionAST
;
102 struct PtrOperatorAST
;
103 struct PtrToMemberAST
;
104 struct ReturnStatementAST
;
105 struct SimpleDeclarationAST
;
106 struct SimpleTypeSpecifierAST
;
107 struct SizeofExpressionAST
;
109 struct StringLiteralAST
;
110 struct SubscriptExpressionAST
;
111 struct SwitchStatementAST
;
112 struct TemplateArgumentAST
;
113 struct TemplateDeclarationAST
;
114 struct TemplateParameterAST
;
115 struct ThrowExpressionAST
;
116 struct TranslationUnitAST
;
117 struct TryBlockStatementAST
;
119 struct TypeIdentificationAST
;
120 struct TypeParameterAST
;
121 struct TypeSpecifierAST
;
123 struct UnaryExpressionAST
;
124 struct UnqualifiedNameAST
;
126 struct UsingDirectiveAST
;
127 struct WhileStatementAST
;
128 struct WinDeclSpecAST
;
137 Kind_AccessSpecifier
,
141 Kind_BinaryExpression
,
143 Kind_ClassMemberAccess
,
145 Kind_CompoundStatement
,
147 Kind_ConditionalExpression
,
148 Kind_CppCastExpression
,
149 Kind_CtorInitializer
,
150 Kind_DeclarationStatement
,
152 Kind_DeleteExpression
,
154 Kind_ElaboratedTypeSpecifier
,
157 Kind_ExceptionSpecification
,
158 Kind_ExpressionOrDeclarationStatement
,
159 Kind_ExpressionStatement
,
162 Kind_FunctionDefinition
,
164 Kind_IncrDecrExpression
,
167 Kind_InitializerClause
,
168 Kind_LabeledStatement
,
170 Kind_LinkageSpecification
,
174 Kind_NamespaceAliasDefinition
,
180 Kind_OperatorFunctionId
,
181 Kind_ParameterDeclaration
,
182 Kind_ParameterDeclarationClause
,
183 Kind_PostfixExpression
,
184 Kind_PrimaryExpression
,
187 Kind_ReturnStatement
,
188 Kind_SimpleDeclaration
,
189 Kind_SimpleTypeSpecifier
,
190 Kind_SizeofExpression
,
192 Kind_SubscriptExpression
,
193 Kind_SwitchStatement
,
194 Kind_TemplateArgument
,
195 Kind_TemplateDeclaration
,
196 Kind_TemplateParameter
,
197 Kind_ThrowExpression
,
198 Kind_TranslationUnit
,
199 Kind_TryBlockStatement
,
201 Kind_TypeIdentification
,
204 Kind_UnaryExpression
,
205 Kind_UnqualifiedName
,
217 std::size_t start_token
;
218 std::size_t end_token
;
221 struct TypeSpecifierAST
: public AST
223 const ListNode
<std::size_t> *cv
;
226 struct StatementAST
: public AST
230 struct ExpressionAST
: public AST
234 struct DeclarationAST
: public AST
238 struct AccessSpecifierAST
: public DeclarationAST
240 DECLARE_AST_NODE(AccessSpecifier
)
242 const ListNode
<std::size_t> *specs
;
245 struct AsmDefinitionAST
: public DeclarationAST
247 DECLARE_AST_NODE(AsmDefinition
)
249 const ListNode
<std::size_t> *cv
;
252 struct BaseClauseAST
: public AST
// ### kill me
254 DECLARE_AST_NODE(BaseClause
)
256 const ListNode
<BaseSpecifierAST
*> *base_specifiers
;
259 struct BaseSpecifierAST
: public AST
261 DECLARE_AST_NODE(BaseSpecifier
)
264 std::size_t access_specifier
;
268 struct BinaryExpressionAST
: public ExpressionAST
270 DECLARE_AST_NODE(BinaryExpression
)
273 ExpressionAST
*left_expression
;
274 ExpressionAST
*right_expression
;
277 struct CastExpressionAST
: public ExpressionAST
279 DECLARE_AST_NODE(CastExpression
)
282 ExpressionAST
*expression
;
285 struct ClassMemberAccessAST
: public ExpressionAST
287 DECLARE_AST_NODE(ClassMemberAccess
)
293 struct ClassSpecifierAST
: public TypeSpecifierAST
295 DECLARE_AST_NODE(ClassSpecifier
)
297 WinDeclSpecAST
*win_decl_specifiers
;
298 std::size_t class_key
;
300 BaseClauseAST
*base_clause
;
301 const ListNode
<DeclarationAST
*> *member_specs
;
304 struct CompoundStatementAST
: public StatementAST
306 DECLARE_AST_NODE(CompoundStatement
)
308 const ListNode
<StatementAST
*> *statements
;
311 struct ConditionAST
: public AST
313 DECLARE_AST_NODE(Condition
)
315 TypeSpecifierAST
*type_specifier
;
316 DeclaratorAST
*declarator
;
317 ExpressionAST
*expression
;
320 struct ConditionalExpressionAST
: public ExpressionAST
322 DECLARE_AST_NODE(ConditionalExpression
)
324 ExpressionAST
*condition
;
325 ExpressionAST
*left_expression
;
326 ExpressionAST
*right_expression
;
329 struct CppCastExpressionAST
: public ExpressionAST
331 DECLARE_AST_NODE(CppCastExpression
)
335 ExpressionAST
*expression
;
336 const ListNode
<ExpressionAST
*> *sub_expressions
;
339 struct CtorInitializerAST
: public AST
341 DECLARE_AST_NODE(CtorInitializer
)
344 const ListNode
<MemInitializerAST
*> *member_initializers
;
347 struct DeclarationStatementAST
: public StatementAST
349 DECLARE_AST_NODE(DeclarationStatement
)
351 DeclarationAST
*declaration
;
354 struct DeclaratorAST
: public AST
356 DECLARE_AST_NODE(Declarator
)
358 const ListNode
<PtrOperatorAST
*> *ptr_ops
;
359 DeclaratorAST
*sub_declarator
;
361 ExpressionAST
*bit_expression
;
362 const ListNode
<ExpressionAST
*> *array_dimensions
;
363 ParameterDeclarationClauseAST
*parameter_declaration_clause
;
364 const ListNode
<std::size_t> *fun_cv
;
365 ExceptionSpecificationAST
*exception_spec
;
368 struct DeleteExpressionAST
: public ExpressionAST
370 DECLARE_AST_NODE(DeleteExpression
)
372 std::size_t scope_token
;
373 std::size_t delete_token
;
374 std::size_t lbracket_token
;
375 std::size_t rbracket_token
;
376 ExpressionAST
*expression
;
379 struct DoStatementAST
: public StatementAST
381 DECLARE_AST_NODE(DoStatement
)
383 StatementAST
*statement
;
384 ExpressionAST
*expression
;
387 struct ElaboratedTypeSpecifierAST
: public TypeSpecifierAST
389 DECLARE_AST_NODE(ElaboratedTypeSpecifier
)
395 struct EnumSpecifierAST
: public TypeSpecifierAST
397 DECLARE_AST_NODE(EnumSpecifier
)
400 const ListNode
<EnumeratorAST
*> *enumerators
;
403 struct EnumeratorAST
: public AST
405 DECLARE_AST_NODE(Enumerator
)
408 ExpressionAST
*expression
;
411 struct ExceptionSpecificationAST
: public AST
413 DECLARE_AST_NODE(ExceptionSpecification
)
415 std::size_t ellipsis
;
416 const ListNode
<TypeIdAST
*> *type_ids
;
419 struct ExpressionOrDeclarationStatementAST
: public StatementAST
421 DECLARE_AST_NODE(ExpressionOrDeclarationStatement
)
423 StatementAST
*expression
;
424 StatementAST
*declaration
;
427 struct ExpressionStatementAST
: public StatementAST
429 DECLARE_AST_NODE(ExpressionStatement
)
431 ExpressionAST
*expression
;
434 struct FunctionCallAST
: public ExpressionAST
436 DECLARE_AST_NODE(FunctionCall
)
438 ExpressionAST
*arguments
;
441 struct FunctionDefinitionAST
: public DeclarationAST
443 DECLARE_AST_NODE(FunctionDefinition
)
445 const ListNode
<std::size_t> *storage_specifiers
;
446 const ListNode
<std::size_t> *function_specifiers
;
447 TypeSpecifierAST
*type_specifier
;
448 InitDeclaratorAST
*init_declarator
;
449 StatementAST
*function_body
;
450 WinDeclSpecAST
*win_decl_specifiers
;
453 struct ForStatementAST
: public StatementAST
455 DECLARE_AST_NODE(ForStatement
)
457 StatementAST
*init_statement
;
458 ConditionAST
*condition
;
459 ExpressionAST
*expression
;
460 StatementAST
*statement
;
463 struct IfStatementAST
: public StatementAST
465 DECLARE_AST_NODE(IfStatement
)
467 ConditionAST
*condition
;
468 StatementAST
*statement
;
469 StatementAST
*else_statement
;
472 struct IncrDecrExpressionAST
: public ExpressionAST
474 DECLARE_AST_NODE(IncrDecrExpression
)
479 struct InitDeclaratorAST
: public AST
481 DECLARE_AST_NODE(InitDeclarator
)
483 DeclaratorAST
*declarator
;
484 InitializerAST
*initializer
;
487 struct InitializerAST
: public AST
489 DECLARE_AST_NODE(Initializer
)
491 InitializerClauseAST
*initializer_clause
;
492 ExpressionAST
*expression
;
495 struct InitializerClauseAST
: public AST
497 DECLARE_AST_NODE(InitializerClause
)
499 ExpressionAST
*expression
;
502 struct LabeledStatementAST
: public StatementAST
504 DECLARE_AST_NODE(LabeledStatement
)
507 struct LinkageBodyAST
: public AST
509 DECLARE_AST_NODE(LinkageBody
)
511 const ListNode
<DeclarationAST
*> *declarations
;
514 struct LinkageSpecificationAST
: public DeclarationAST
516 DECLARE_AST_NODE(LinkageSpecification
)
518 std::size_t extern_type
;
519 LinkageBodyAST
*linkage_body
;
520 DeclarationAST
*declaration
;
523 struct MemInitializerAST
: public AST
525 DECLARE_AST_NODE(MemInitializer
)
527 NameAST
*initializer_id
;
528 ExpressionAST
*expression
;
531 struct NameAST
: public AST
533 DECLARE_AST_NODE(Name
)
536 const ListNode
<UnqualifiedNameAST
*> *qualified_names
;
537 UnqualifiedNameAST
*unqualified_name
;
540 struct NamespaceAST
: public DeclarationAST
542 DECLARE_AST_NODE(Namespace
)
544 std::size_t namespace_name
;
545 LinkageBodyAST
*linkage_body
;
548 struct NamespaceAliasDefinitionAST
: public DeclarationAST
550 DECLARE_AST_NODE(NamespaceAliasDefinition
)
552 std::size_t namespace_name
;
556 struct NewDeclaratorAST
: public AST
558 DECLARE_AST_NODE(NewDeclarator
)
560 PtrOperatorAST
*ptr_op
;
561 NewDeclaratorAST
*sub_declarator
;
562 const ListNode
<ExpressionAST
*> *expressions
;
565 struct NewExpressionAST
: public ExpressionAST
567 DECLARE_AST_NODE(NewExpression
)
569 std::size_t scope_token
;
570 std::size_t new_token
;
571 ExpressionAST
*expression
;
573 NewTypeIdAST
*new_type_id
;
574 NewInitializerAST
*new_initializer
;
577 struct NewInitializerAST
: public AST
579 DECLARE_AST_NODE(NewInitializer
)
581 ExpressionAST
*expression
;
584 struct NewTypeIdAST
: public AST
586 DECLARE_AST_NODE(NewTypeId
)
588 TypeSpecifierAST
*type_specifier
;
589 NewInitializerAST
*new_initializer
;
590 NewDeclaratorAST
*new_declarator
;
593 struct OperatorAST
: public AST
595 DECLARE_AST_NODE(Operator
)
602 struct OperatorFunctionIdAST
: public AST
604 DECLARE_AST_NODE(OperatorFunctionId
)
607 TypeSpecifierAST
*type_specifier
;
608 const ListNode
<PtrOperatorAST
*> *ptr_ops
;
611 struct ParameterDeclarationAST
: public AST
613 DECLARE_AST_NODE(ParameterDeclaration
)
615 TypeSpecifierAST
*type_specifier
;
616 DeclaratorAST
*declarator
;
617 ExpressionAST
*expression
;
620 struct ParameterDeclarationClauseAST
: public AST
622 DECLARE_AST_NODE(ParameterDeclarationClause
)
624 const ListNode
<ParameterDeclarationAST
*> *parameter_declarations
;
625 std::size_t ellipsis
;
628 struct PostfixExpressionAST
: public ExpressionAST
630 DECLARE_AST_NODE(PostfixExpression
)
632 TypeSpecifierAST
*type_specifier
;
633 ExpressionAST
*expression
;
634 const ListNode
<ExpressionAST
*> *sub_expressions
;
637 struct PrimaryExpressionAST
: public ExpressionAST
639 DECLARE_AST_NODE(PrimaryExpression
)
641 StringLiteralAST
*literal
;
643 StatementAST
*expression_statement
;
644 ExpressionAST
*sub_expression
;
648 struct PtrOperatorAST
: public AST
650 DECLARE_AST_NODE(PtrOperator
)
652 const ListNode
<std::size_t> *cv
;
654 PtrToMemberAST
*mem_ptr
;
657 struct PtrToMemberAST
: public AST
659 DECLARE_AST_NODE(PtrToMember
)
662 struct ReturnStatementAST
: public StatementAST
664 DECLARE_AST_NODE(ReturnStatement
)
666 ExpressionAST
*expression
;
669 struct SimpleDeclarationAST
: public DeclarationAST
671 DECLARE_AST_NODE(SimpleDeclaration
)
673 const ListNode
<std::size_t> *storage_specifiers
;
674 const ListNode
<std::size_t> *function_specifiers
;
675 TypeSpecifierAST
*type_specifier
;
676 const ListNode
<InitDeclaratorAST
*> *init_declarators
;
677 WinDeclSpecAST
*win_decl_specifiers
;
680 struct SimpleTypeSpecifierAST
: public TypeSpecifierAST
682 DECLARE_AST_NODE(SimpleTypeSpecifier
)
684 const ListNode
<std::size_t> *integrals
;
687 ExpressionAST
*expression
;
691 struct SizeofExpressionAST
: public ExpressionAST
693 DECLARE_AST_NODE(SizeofExpression
)
695 std::size_t sizeof_token
;
697 ExpressionAST
*expression
;
700 struct StringLiteralAST
: public AST
702 DECLARE_AST_NODE(StringLiteral
)
704 const ListNode
<std::size_t> *literals
;
707 struct SubscriptExpressionAST
: public ExpressionAST
709 DECLARE_AST_NODE(SubscriptExpression
)
711 ExpressionAST
*subscript
;
714 struct SwitchStatementAST
: public StatementAST
716 DECLARE_AST_NODE(SwitchStatement
)
718 ConditionAST
*condition
;
719 StatementAST
*statement
;
722 struct TemplateArgumentAST
: public AST
724 DECLARE_AST_NODE(TemplateArgument
)
727 ExpressionAST
*expression
;
730 struct TemplateDeclarationAST
: public DeclarationAST
732 DECLARE_AST_NODE(TemplateDeclaration
)
734 std::size_t exported
;
735 const ListNode
<TemplateParameterAST
*> *template_parameters
;
736 DeclarationAST
* declaration
;
739 struct TemplateParameterAST
: public AST
741 DECLARE_AST_NODE(TemplateParameter
)
743 TypeParameterAST
*type_parameter
;
744 ParameterDeclarationAST
*parameter_declaration
;
747 struct ThrowExpressionAST
: public ExpressionAST
749 DECLARE_AST_NODE(ThrowExpression
)
751 std::size_t throw_token
;
752 ExpressionAST
*expression
;
755 struct TranslationUnitAST
: public AST
757 DECLARE_AST_NODE(TranslationUnit
)
759 const ListNode
<DeclarationAST
*> *declarations
;
762 struct TryBlockStatementAST
: public StatementAST
764 DECLARE_AST_NODE(TryBlockStatement
)
767 struct TypeIdAST
: public AST
769 DECLARE_AST_NODE(TypeId
)
771 TypeSpecifierAST
*type_specifier
;
772 DeclaratorAST
*declarator
;
775 struct TypeIdentificationAST
: public ExpressionAST
777 DECLARE_AST_NODE(TypeIdentification
)
779 std::size_t typename_token
;
781 ExpressionAST
*expression
;
784 struct TypeParameterAST
: public AST
786 DECLARE_AST_NODE(TypeParameter
)
791 const ListNode
<TemplateParameterAST
*> *template_parameters
;
792 NameAST
*template_name
;
795 struct TypedefAST
: public DeclarationAST
797 DECLARE_AST_NODE(Typedef
)
799 TypeSpecifierAST
*type_specifier
;
800 const ListNode
<InitDeclaratorAST
*> *init_declarators
;
803 struct UnaryExpressionAST
: public ExpressionAST
805 DECLARE_AST_NODE(UnaryExpression
)
808 ExpressionAST
*expression
;
811 struct UnqualifiedNameAST
: public AST
813 DECLARE_AST_NODE(UnqualifiedName
)
817 OperatorFunctionIdAST
*operator_id
;
818 const ListNode
<TemplateArgumentAST
*> *template_arguments
;
821 struct UsingAST
: public DeclarationAST
823 DECLARE_AST_NODE(Using
)
825 std::size_t type_name
;
829 struct UsingDirectiveAST
: public DeclarationAST
831 DECLARE_AST_NODE(UsingDirective
)
836 struct WhileStatementAST
: public StatementAST
838 DECLARE_AST_NODE(WhileStatement
)
840 ConditionAST
*condition
;
841 StatementAST
*statement
;
844 struct WinDeclSpecAST
: public AST
846 DECLARE_AST_NODE(WinDeclSpec
)
848 std::size_t specifier
;
849 std::size_t modifier
;
852 struct QPropertyAST
: public DeclarationAST
854 DECLARE_AST_NODE(QPropertyAST
)
858 _Tp
*CreateNode(pool
*memory_pool
)
860 _Tp
*node
= reinterpret_cast<_Tp
*>(memory_pool
->allocate(sizeof(_Tp
)));
861 node
->kind
= _Tp::__node_kind
;
866 _Tp
ast_cast(AST
*item
)
868 if (item
&& static_cast<_Tp
>(0)->__node_kind
== item
->kind
)
869 return static_cast<_Tp
>(item
);
876 // kate: space-indent on; indent-width 2; replace-tabs on;