1
/* This file is part of KDevelop
2
Copyright 2002-2005 Roberto Raggi <roberto@kdevelop.org>
3
Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License version 2 as published by the Free Software Foundation.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
23
#include "memorypool.h"
26
#define DECLARE_AST_NODE(k) \
27
enum { __node_kind = Kind_##k };
34
class AccessSpecifierAST;
35
class AsmDefinitionAST;
37
class BaseSpecifierAST;
38
class BinaryExpressionAST;
39
class CastExpressionAST;
40
class ClassMemberAccessAST;
41
class ClassSpecifierAST;
42
class CompoundStatementAST;
44
class ConditionalExpressionAST;
45
class CppCastExpressionAST;
46
class CtorInitializerAST;
48
class DeclarationStatementAST;
50
class DeleteExpressionAST;
53
class EnumSpecifierAST;
55
class ExceptionSpecificationAST;
57
class ExpressionOrDeclarationStatementAST;
58
class ExpressionStatementAST;
59
class ForStatementAST;
60
class FunctionCallAST;
61
class FunctionDefinitionAST;
63
class IncrDecrExpressionAST;
64
class InitDeclaratorAST;
66
class InitializerClauseAST;
67
class LabeledStatementAST;
69
class LinkageSpecificationAST;
70
class MemInitializerAST;
73
class NamespaceAliasDefinitionAST;
74
class NewDeclaratorAST;
75
class NewExpressionAST;
76
class NewInitializerAST;
79
class OperatorFunctionIdAST;
80
class ParameterDeclarationAST;
81
class ParameterDeclarationClauseAST;
82
class PostfixExpressionAST;
83
class PrimaryExpressionAST;
86
class ReturnStatementAST;
87
class SimpleDeclarationAST;
88
class SimpleTypeSpecifierAST;
89
class SizeofExpressionAST;
91
class StringLiteralAST;
92
class SubscriptExpressionAST;
93
class SwitchStatementAST;
94
class TemplateArgumentAST;
95
class TemplateDeclarationAST;
96
class TemplateParameterAST;
97
class ThrowExpressionAST;
98
class TranslationUnitAST;
99
class TryBlockStatementAST;
100
class CatchStatementAST;
102
class TypeIdentificationAST;
103
class TypeParameterAST;
104
class TypeSpecifierAST;
106
class UnaryExpressionAST;
107
class UnqualifiedNameAST;
109
class UsingDirectiveAST;
110
class WhileStatementAST;
111
class WinDeclSpecAST;
114
{///@warning When adding new nodes here, also modify the names[] array in dumptree.cpp
121
Kind_AccessSpecifier, // 1
122
Kind_AsmDefinition, // 2
123
Kind_BaseClause, // 3
124
Kind_BaseSpecifier, // 4
125
Kind_BinaryExpression, // 5
126
Kind_CastExpression, // 6
127
Kind_ClassMemberAccess, // 7
128
Kind_ClassSpecifier, // 8
129
Kind_CompoundStatement, // 9
130
Kind_Condition, // 10
131
Kind_ConditionalExpression, // 11
132
Kind_CppCastExpression, // 12
133
Kind_CtorInitializer, // 13
134
Kind_DeclarationStatement, // 14
135
Kind_Declarator, // 15
136
Kind_DeleteExpression, // 16
137
Kind_DoStatement, // 17
138
Kind_ElaboratedTypeSpecifier, // 18
139
Kind_EnumSpecifier, // 19
140
Kind_Enumerator, // 20
141
Kind_ExceptionSpecification, // 21
142
Kind_ExpressionOrDeclarationStatement, // 22
143
Kind_ExpressionStatement, // 23
144
Kind_ForStatement, // 24
145
Kind_FunctionCall, // 25
146
Kind_FunctionDefinition, // 26
147
Kind_IfStatement, // 27
148
Kind_IncrDecrExpression, // 28
149
Kind_InitDeclarator, // 29
150
Kind_Initializer, // 30
151
Kind_InitializerClause, // 31
152
Kind_LabeledStatement, // 32
153
Kind_LinkageBody, // 33
154
Kind_LinkageSpecification, // 34
155
Kind_MemInitializer, // 35
157
Kind_Namespace, // 37
158
Kind_NamespaceAliasDefinition, // 38
159
Kind_NewDeclarator, // 39
160
Kind_NewExpression, // 40
161
Kind_NewInitializer, // 41
162
Kind_NewTypeId, // 42
164
Kind_OperatorFunctionId, // 44
165
Kind_ParameterDeclaration, // 45
166
Kind_ParameterDeclarationClause, // 46
167
Kind_PostfixExpression, // 47
168
Kind_PrimaryExpression, // 48
169
Kind_PtrOperator, // 49
170
Kind_PtrToMember, // 50
171
Kind_ReturnStatement, // 51
172
Kind_SimpleDeclaration, // 52
173
Kind_SimpleTypeSpecifier, // 53
174
Kind_SizeofExpression, // 54
175
Kind_StringLiteral, // 55
176
Kind_SubscriptExpression, // 56
177
Kind_SwitchStatement, // 57
178
Kind_TemplateArgument, // 58
179
Kind_TemplateDeclaration, // 59
180
Kind_TemplateParameter, // 60
181
Kind_ThrowExpression, // 61
182
Kind_TranslationUnit, // 62
183
Kind_TryBlockStatement, // 63
184
Kind_CatchStatement, // 64
186
Kind_TypeIdentification, // 66
187
Kind_TypeParameter, // 67
189
Kind_UnaryExpression, // 69
190
Kind_UnqualifiedName, // 70
192
Kind_UsingDirective, // 72
193
Kind_WhileStatement, // 73
194
Kind_WinDeclSpec, // 74
196
Kind_JumpStatement, // 76
197
Kind_SignalSlotExpression, // 77
206
/** Context opened by this AST
207
*This is usually not filled for all AST's, only for those that open a new context
209
KDevelop::DUContext* ducontext;
216
const ListNode<uint> *comments; //A list of comment-tokens
219
class TypeSpecifierAST : public AST
223
const ListNode<uint> *cv; // const or volatile tokens
226
class StatementAST : public AST
232
class ExpressionAST : public AST
238
class DeclarationAST : public AST, public CommentAST
244
class AccessSpecifierAST : public DeclarationAST
248
DECLARE_AST_NODE(AccessSpecifier)
250
const ListNode<uint> *specs;
253
class AsmDefinitionAST : public DeclarationAST
257
DECLARE_AST_NODE(AsmDefinition)
259
const ListNode<uint> *cv;
262
class BaseClauseAST : public AST // ### kill me
266
DECLARE_AST_NODE(BaseClause)
268
const ListNode<BaseSpecifierAST*> *base_specifiers;
271
class BaseSpecifierAST : public AST
275
DECLARE_AST_NODE(BaseSpecifier)
278
uint access_specifier;
282
class BinaryExpressionAST : public ExpressionAST
286
DECLARE_AST_NODE(BinaryExpression)
288
uint op; //Index of the token that describes the operator
289
ExpressionAST *left_expression;
290
ExpressionAST *right_expression;
293
///An expression used to do more detailed processing of SIGNAL(..) and SLOT(..) specifications
294
class SignalSlotExpressionAST : public ExpressionAST {
297
DECLARE_AST_NODE(SignalSlotExpression)
298
//The unqualified name also contains the argument types in its template-arguments
299
UnqualifiedNameAST *name;
302
class CastExpressionAST : public ExpressionAST
306
DECLARE_AST_NODE(CastExpression)
309
ExpressionAST *expression;
312
class ClassMemberAccessAST : public ExpressionAST
316
DECLARE_AST_NODE(ClassMemberAccess)
318
uint op; //Index of the token that describes the operator
322
class ClassSpecifierAST : public TypeSpecifierAST
326
DECLARE_AST_NODE(ClassSpecifier)
328
WinDeclSpecAST *win_decl_specifiers;
331
BaseClauseAST *base_clause;
332
const ListNode<DeclarationAST*> *member_specs;
335
class CompoundStatementAST : public StatementAST
339
DECLARE_AST_NODE(CompoundStatement)
341
const ListNode<StatementAST*> *statements;
344
class ConditionAST : public AST
348
DECLARE_AST_NODE(Condition)
350
TypeSpecifierAST *type_specifier;
351
DeclaratorAST *declarator;
352
ExpressionAST *expression;
355
class ConditionalExpressionAST : public ExpressionAST
359
DECLARE_AST_NODE(ConditionalExpression)
361
ExpressionAST *condition;
362
ExpressionAST *left_expression;
363
ExpressionAST *right_expression;
367
* type_id is the type that should be casted to
368
* expression is the expression casted from
369
* sub_expressions is a list of post-fix expressions, see PostfixExpressionAST
371
class CppCastExpressionAST : public ExpressionAST
375
DECLARE_AST_NODE(CppCastExpression)
377
uint op; //Index of the token that describes the operator
379
ExpressionAST *expression;
380
const ListNode<ExpressionAST*> *sub_expressions;
383
class CtorInitializerAST : public AST
387
DECLARE_AST_NODE(CtorInitializer)
390
const ListNode<MemInitializerAST*> *member_initializers;
393
class DeclarationStatementAST : public StatementAST
397
DECLARE_AST_NODE(DeclarationStatement)
399
DeclarationAST *declaration;
402
class DeclaratorAST : public AST
406
DECLARE_AST_NODE(Declarator)
408
const ListNode<PtrOperatorAST*> *ptr_ops;
409
DeclaratorAST *sub_declarator;
411
ExpressionAST *bit_expression;
412
const ListNode<ExpressionAST*> *array_dimensions;
414
bool parameter_is_initializer; //Used by the declaration-builder to mark a parameter-declaration clause as a mis-parsed initializer
415
ParameterDeclarationClauseAST *parameter_declaration_clause;
416
const ListNode<uint> *fun_cv;
417
ExceptionSpecificationAST *exception_spec;
420
class DeleteExpressionAST : public ExpressionAST
424
DECLARE_AST_NODE(DeleteExpression)
430
ExpressionAST *expression;
433
class DoStatementAST : public StatementAST
437
DECLARE_AST_NODE(DoStatement)
439
StatementAST *statement;
440
ExpressionAST *expression;
443
class ElaboratedTypeSpecifierAST : public TypeSpecifierAST
447
DECLARE_AST_NODE(ElaboratedTypeSpecifier)
451
bool isDeclaration; //Whether this type-specifier is a forward declaration rather than use use
454
class EnumSpecifierAST : public TypeSpecifierAST
458
DECLARE_AST_NODE(EnumSpecifier)
461
const ListNode<EnumeratorAST*> *enumerators;
464
class EnumeratorAST : public AST, public CommentAST
468
DECLARE_AST_NODE(Enumerator)
471
ExpressionAST *expression;
474
class ExceptionSpecificationAST : public AST
478
DECLARE_AST_NODE(ExceptionSpecification)
481
const ListNode<TypeIdAST*> *type_ids;
484
class ExpressionOrDeclarationStatementAST : public StatementAST
488
DECLARE_AST_NODE(ExpressionOrDeclarationStatement)
490
StatementAST *expression;
491
StatementAST *declaration;
493
// This was not originally part of the AST - added by the context visitor
494
bool expressionChosen;
497
///An expression terminated by a semicolon or similar
498
class ExpressionStatementAST : public StatementAST
502
DECLARE_AST_NODE(ExpressionStatement)
504
ExpressionAST *expression;
507
class FunctionCallAST : public ExpressionAST
511
DECLARE_AST_NODE(FunctionCall)
513
ExpressionAST *arguments;
516
class FunctionDefinitionAST : public DeclarationAST
520
DECLARE_AST_NODE(FunctionDefinition)
522
const ListNode<uint> *storage_specifiers;
523
const ListNode<uint> *function_specifiers;
524
TypeSpecifierAST *type_specifier;
525
InitDeclaratorAST *init_declarator;
526
StatementAST *function_body;
527
WinDeclSpecAST *win_decl_specifiers;
528
CtorInitializerAST *constructor_initializers;
531
class ForStatementAST : public StatementAST
535
DECLARE_AST_NODE(ForStatement)
537
StatementAST *init_statement;
538
ConditionAST *condition;
539
ExpressionAST *expression;
540
StatementAST *statement;
543
class IfStatementAST : public StatementAST
547
DECLARE_AST_NODE(IfStatement)
549
ConditionAST *condition;
550
StatementAST *statement;
551
StatementAST *else_statement;
554
class IncrDecrExpressionAST : public ExpressionAST
558
DECLARE_AST_NODE(IncrDecrExpression)
560
uint op; //Index of the token that describes the operator
563
class InitDeclaratorAST : public AST
567
DECLARE_AST_NODE(InitDeclarator)
569
DeclaratorAST *declarator;
570
InitializerAST *initializer;
573
class InitializerAST : public AST
577
DECLARE_AST_NODE(Initializer)
579
InitializerClauseAST *initializer_clause;
580
ExpressionAST *expression;
583
class InitializerClauseAST : public AST
587
DECLARE_AST_NODE(InitializerClause)
589
// either 'expression' or 'initializer_list' or neither are used.
590
// neither are used when the clause represents the empty initializer "{}"
592
// assignment expression
593
ExpressionAST *expression;
594
const ListNode<InitializerClauseAST*> *initializer_list;
597
class LabeledStatementAST : public StatementAST
601
DECLARE_AST_NODE(LabeledStatement)
604
//The constant label expression
605
ExpressionAST *expression;
606
StatementAST* statement;
609
class LinkageBodyAST : public AST
613
DECLARE_AST_NODE(LinkageBody)
615
const ListNode<DeclarationAST*> *declarations;
618
class LinkageSpecificationAST : public DeclarationAST
622
DECLARE_AST_NODE(LinkageSpecification)
625
LinkageBodyAST *linkage_body;
626
DeclarationAST *declaration;
629
class MemInitializerAST : public AST
633
DECLARE_AST_NODE(MemInitializer)
635
NameAST *initializer_id;
636
ExpressionAST *expression;
639
class NameAST : public AST
643
DECLARE_AST_NODE(Name)
646
const ListNode<UnqualifiedNameAST*> *qualified_names;
647
UnqualifiedNameAST *unqualified_name;
650
class NamespaceAST : public DeclarationAST
654
DECLARE_AST_NODE(Namespace)
657
LinkageBodyAST *linkage_body;
660
class NamespaceAliasDefinitionAST : public DeclarationAST
664
DECLARE_AST_NODE(NamespaceAliasDefinition)
670
class NewDeclaratorAST : public AST
674
DECLARE_AST_NODE(NewDeclarator)
676
PtrOperatorAST *ptr_op;
677
NewDeclaratorAST *sub_declarator;
678
const ListNode<ExpressionAST*> *expressions;
681
class NewExpressionAST : public ExpressionAST
685
DECLARE_AST_NODE(NewExpression)
689
ExpressionAST *expression;
691
NewTypeIdAST *new_type_id;
692
NewInitializerAST *new_initializer;
695
class NewInitializerAST : public AST
699
DECLARE_AST_NODE(NewInitializer)
701
ExpressionAST *expression;
704
class NewTypeIdAST : public AST
708
DECLARE_AST_NODE(NewTypeId)
710
TypeSpecifierAST *type_specifier;
711
NewInitializerAST *new_initializer;
712
NewDeclaratorAST *new_declarator;
715
class OperatorAST : public AST
719
DECLARE_AST_NODE(Operator)
721
uint op; //Index of the token that describes the operator
726
class OperatorFunctionIdAST : public AST
730
DECLARE_AST_NODE(OperatorFunctionId)
733
TypeSpecifierAST *type_specifier;
734
const ListNode<PtrOperatorAST*> *ptr_ops;
737
class ParameterDeclarationAST : public AST
741
DECLARE_AST_NODE(ParameterDeclaration)
743
TypeSpecifierAST *type_specifier;
744
DeclaratorAST *declarator;
745
ExpressionAST *expression;
748
class ParameterDeclarationClauseAST : public AST
752
DECLARE_AST_NODE(ParameterDeclarationClause)
754
const ListNode<ParameterDeclarationAST*> *parameter_declarations;
759
* A post-fix expression is an expression that consists of one primary expression and multiple sub-expressions that are evaluated from
760
* left to right, each sub-expression based on the previous expression.
764
* "a->b" : "a" is the primary expression, "->b" is a sub-expression
765
* "a->b(5,3)" : "a" is the primary expression, "->b" is a sub-expression, and "(5,3)" is a sub-expression
767
class PostfixExpressionAST : public ExpressionAST
771
DECLARE_AST_NODE(PostfixExpression)
773
TypeSpecifierAST *type_specifier;
774
ExpressionAST *expression;
775
const ListNode<ExpressionAST*> *sub_expressions;
778
class PrimaryExpressionAST : public ExpressionAST
782
DECLARE_AST_NODE(PrimaryExpression)
784
StringLiteralAST *literal;
786
StatementAST *expression_statement;
787
ExpressionAST *sub_expression;
791
class PtrOperatorAST : public AST
795
DECLARE_AST_NODE(PtrOperator)
797
const ListNode<uint> *cv;
798
uint op; //Index of the token that describes the operator. Is zero when mem_ptr is non-zero.
799
PtrToMemberAST *mem_ptr;
802
class PtrToMemberAST : public AST
806
DECLARE_AST_NODE(PtrToMember)
809
class JumpStatementAST : public StatementAST
813
DECLARE_AST_NODE(JumpStatement)
815
// index of operator token which describes the jump, one of
816
// 'break', 'continue' or 'goto. Return statements are handled by
817
// ReturnStatementAST
819
// identifier for 'goto' statements
823
class ReturnStatementAST : public StatementAST
827
DECLARE_AST_NODE(ReturnStatement)
829
ExpressionAST *expression;
832
class SimpleDeclarationAST : public DeclarationAST
836
DECLARE_AST_NODE(SimpleDeclaration)
838
const ListNode<uint> *storage_specifiers;
839
const ListNode<uint> *function_specifiers;
840
TypeSpecifierAST *type_specifier;
841
const ListNode<InitDeclaratorAST*> *init_declarators;
842
WinDeclSpecAST *win_decl_specifiers;
845
class SimpleTypeSpecifierAST : public TypeSpecifierAST
849
DECLARE_AST_NODE(SimpleTypeSpecifier)
851
const ListNode<uint> *integrals;
854
ExpressionAST *expression;
858
class SizeofExpressionAST : public ExpressionAST
862
DECLARE_AST_NODE(SizeofExpression)
866
ExpressionAST *expression;
869
class StringLiteralAST : public AST
873
DECLARE_AST_NODE(StringLiteral)
875
const ListNode<uint> *literals;
879
class SubscriptExpressionAST : public ExpressionAST
883
DECLARE_AST_NODE(SubscriptExpression)
885
ExpressionAST *subscript;
888
class SwitchStatementAST : public StatementAST
892
DECLARE_AST_NODE(SwitchStatement)
894
ConditionAST *condition;
895
StatementAST *statement;
898
class TemplateArgumentAST : public AST
902
DECLARE_AST_NODE(TemplateArgument)
905
ExpressionAST *expression;
908
class TemplateDeclarationAST : public DeclarationAST
912
DECLARE_AST_NODE(TemplateDeclaration)
915
const ListNode<TemplateParameterAST*> *template_parameters;
916
DeclarationAST* declaration;
919
class TemplateParameterAST : public AST
923
DECLARE_AST_NODE(TemplateParameter)
925
TypeParameterAST *type_parameter; //This is used if this is a template-parameter like "class A"
926
ParameterDeclarationAST *parameter_declaration; //This is used if this is a template-parameter like "int a"
929
class ThrowExpressionAST : public ExpressionAST
933
DECLARE_AST_NODE(ThrowExpression)
936
ExpressionAST *expression;
939
class TranslationUnitAST : public AST, public CommentAST
945
DECLARE_AST_NODE(TranslationUnit)
947
const ListNode<DeclarationAST*> *declarations;
949
///true if either a '}' was missing at the end, or there was a '}' too much.
950
///This indicates a temporary state where the user is typing, and the document is completely invalid.
951
bool hadMissingCompoundTokens;
953
// Note: non AST related, saves parsing session...
954
ParseSession* session;
957
class TryBlockStatementAST : public StatementAST
961
DECLARE_AST_NODE(TryBlockStatement)
963
StatementAST* try_block;
964
const ListNode<CatchStatementAST*> *catch_blocks;
967
class CatchStatementAST : public StatementAST
971
DECLARE_AST_NODE(CatchStatement)
973
ConditionAST* condition;
974
StatementAST* statement;
977
class TypeIdAST : public AST
981
DECLARE_AST_NODE(TypeId)
983
TypeSpecifierAST *type_specifier;
984
DeclaratorAST *declarator;
988
class TypeIdentificationAST : public ExpressionAST
992
DECLARE_AST_NODE(TypeIdentification)
996
ExpressionAST *expression;
999
class TypeParameterAST : public AST
1003
DECLARE_AST_NODE(TypeParameter)
1008
const ListNode<TemplateParameterAST*> *template_parameters;
1009
NameAST *template_name;
1012
class TypedefAST : public DeclarationAST
1016
DECLARE_AST_NODE(Typedef)
1018
TypeSpecifierAST *type_specifier;
1019
const ListNode<InitDeclaratorAST*> *init_declarators;
1022
class UnaryExpressionAST : public ExpressionAST
1026
DECLARE_AST_NODE(UnaryExpression)
1028
uint op; //Index of the token that describes the operator
1029
ExpressionAST *expression;
1032
class UnqualifiedNameAST : public AST
1036
DECLARE_AST_NODE(UnqualifiedName)
1041
OperatorFunctionIdAST *operator_id;
1042
const ListNode<TemplateArgumentAST*> *template_arguments;
1045
class UsingAST : public DeclarationAST
1049
DECLARE_AST_NODE(Using)
1055
class UsingDirectiveAST : public DeclarationAST
1059
DECLARE_AST_NODE(UsingDirective)
1064
class WhileStatementAST : public StatementAST
1068
DECLARE_AST_NODE(WhileStatement)
1070
ConditionAST *condition;
1071
StatementAST *statement;
1074
class WinDeclSpecAST : public AST
1078
DECLARE_AST_NODE(WinDeclSpec)
1084
template <class _Tp>
1085
_Tp *CreateNode(pool *memory_pool)
1087
_Tp *node = reinterpret_cast<_Tp*>(memory_pool->allocate(sizeof(_Tp)));
1088
node->kind = _Tp::__node_kind;
1092
template <class _Tp>
1093
_Tp ast_cast(AST *item)
1095
if (item && static_cast<_Tp>(0)->__node_kind == item->kind)
1096
return static_cast<_Tp>(item);