1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
6 ** This file is part of the Qt Script Generator project on Trolltech Labs.
8 ** This file may be used under the terms of the GNU General Public
9 ** License version 2.0 as published by the Free Software Foundation
10 ** and appearing in the file LICENSE.GPL included in the packaging of
11 ** this file. Please review the following information to ensure GNU
12 ** General Public Licensing requirements will be met:
13 ** http://www.trolltech.com/products/qt/opensource.html
15 ** If you are unsure which license is appropriate for your use, please
16 ** review the following information:
17 ** http://www.trolltech.com/products/qt/licensing.html or contact the
18 ** 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 ****************************************************************************/
29 #include "smallobject.h"
34 #define DECLARE_AST_NODE(k) \
35 enum { __node_kind = Kind_##k };
39 struct AccessSpecifierAST
;
40 struct AsmDefinitionAST
;
42 struct BaseSpecifierAST
;
43 struct BinaryExpressionAST
;
44 struct CastExpressionAST
;
45 struct ClassMemberAccessAST
;
46 struct ClassSpecifierAST
;
47 struct CompoundStatementAST
;
49 struct ConditionalExpressionAST
;
50 struct CppCastExpressionAST
;
51 struct CtorInitializerAST
;
52 struct DeclarationAST
;
53 struct DeclarationStatementAST
;
55 struct DeleteExpressionAST
;
56 struct DoStatementAST
;
57 struct ElaboratedTypeSpecifierAST
;
58 struct EnumSpecifierAST
;
60 struct ExceptionSpecificationAST
;
62 struct ExpressionOrDeclarationStatementAST
;
63 struct ExpressionStatementAST
;
64 struct ForStatementAST
;
65 struct FunctionCallAST
;
66 struct FunctionDefinitionAST
;
67 struct IfStatementAST
;
68 struct IncrDecrExpressionAST
;
69 struct InitDeclaratorAST
;
70 struct InitializerAST
;
71 struct InitializerClauseAST
;
72 struct LabeledStatementAST
;
73 struct LinkageBodyAST
;
74 struct LinkageSpecificationAST
;
75 struct MemInitializerAST
;
78 struct NamespaceAliasDefinitionAST
;
79 struct NewDeclaratorAST
;
80 struct NewExpressionAST
;
81 struct NewInitializerAST
;
84 struct OperatorFunctionIdAST
;
85 struct ParameterDeclarationAST
;
86 struct ParameterDeclarationClauseAST
;
87 struct PostfixExpressionAST
;
88 struct PrimaryExpressionAST
;
89 struct PtrOperatorAST
;
90 struct PtrToMemberAST
;
91 struct ReturnStatementAST
;
92 struct SimpleDeclarationAST
;
93 struct SimpleTypeSpecifierAST
;
94 struct SizeofExpressionAST
;
96 struct StringLiteralAST
;
97 struct SubscriptExpressionAST
;
98 struct SwitchStatementAST
;
99 struct TemplateArgumentAST
;
100 struct TemplateDeclarationAST
;
101 struct TemplateParameterAST
;
102 struct ThrowExpressionAST
;
103 struct TranslationUnitAST
;
104 struct TryBlockStatementAST
;
106 struct TypeIdentificationAST
;
107 struct TypeParameterAST
;
108 struct TypeSpecifierAST
;
110 struct UnaryExpressionAST
;
111 struct UnqualifiedNameAST
;
113 struct UsingDirectiveAST
;
114 struct WhileStatementAST
;
115 struct WinDeclSpecAST
;
125 Kind_AccessSpecifier
,
129 Kind_BinaryExpression
,
131 Kind_ClassMemberAccess
,
133 Kind_CompoundStatement
,
135 Kind_ConditionalExpression
,
136 Kind_CppCastExpression
,
137 Kind_CtorInitializer
,
138 Kind_DeclarationStatement
,
140 Kind_DeleteExpression
,
142 Kind_ElaboratedTypeSpecifier
,
145 Kind_ExceptionSpecification
,
146 Kind_ExpressionOrDeclarationStatement
,
147 Kind_ExpressionStatement
,
150 Kind_FunctionDefinition
,
152 Kind_IncrDecrExpression
,
155 Kind_InitializerClause
,
156 Kind_LabeledStatement
,
158 Kind_LinkageSpecification
,
162 Kind_NamespaceAliasDefinition
,
168 Kind_OperatorFunctionId
,
169 Kind_ParameterDeclaration
,
170 Kind_ParameterDeclarationClause
,
171 Kind_PostfixExpression
,
172 Kind_PrimaryExpression
,
175 Kind_ReturnStatement
,
176 Kind_SimpleDeclaration
,
177 Kind_SimpleTypeSpecifier
,
178 Kind_SizeofExpression
,
180 Kind_SubscriptExpression
,
181 Kind_SwitchStatement
,
182 Kind_TemplateArgument
,
183 Kind_TemplateDeclaration
,
184 Kind_TemplateParameter
,
185 Kind_ThrowExpression
,
186 Kind_TranslationUnit
,
187 Kind_TryBlockStatement
,
189 Kind_TypeIdentification
,
192 Kind_UnaryExpression
,
193 Kind_UnqualifiedName
,
199 Kind_ForwardDeclarationSpecifier
,
205 QString
toString(TokenStream
*stream
) const;
209 std::size_t start_token
;
210 std::size_t end_token
;
213 struct TypeSpecifierAST
: public AST
215 const ListNode
<std::size_t> *cv
;
218 struct StatementAST
: public AST
222 struct ExpressionAST
: public AST
226 struct DeclarationAST
: public AST
230 struct AccessSpecifierAST
: public DeclarationAST
232 DECLARE_AST_NODE(AccessSpecifier
)
234 const ListNode
<std::size_t> *specs
;
237 struct AsmDefinitionAST
: public DeclarationAST
239 DECLARE_AST_NODE(AsmDefinition
)
241 const ListNode
<std::size_t> *cv
;
244 struct BaseClauseAST
: public AST
// ### kill me
246 DECLARE_AST_NODE(BaseClause
)
248 const ListNode
<BaseSpecifierAST
*> *base_specifiers
;
251 struct BaseSpecifierAST
: public AST
253 DECLARE_AST_NODE(BaseSpecifier
)
256 std::size_t access_specifier
;
260 struct BinaryExpressionAST
: public ExpressionAST
262 DECLARE_AST_NODE(BinaryExpression
)
265 ExpressionAST
*left_expression
;
266 ExpressionAST
*right_expression
;
269 struct CastExpressionAST
: public ExpressionAST
271 DECLARE_AST_NODE(CastExpression
)
274 ExpressionAST
*expression
;
277 struct ClassMemberAccessAST
: public ExpressionAST
279 DECLARE_AST_NODE(ClassMemberAccess
)
285 struct ClassSpecifierAST
: public TypeSpecifierAST
287 DECLARE_AST_NODE(ClassSpecifier
)
289 WinDeclSpecAST
*win_decl_specifiers
;
290 std::size_t class_key
;
292 BaseClauseAST
*base_clause
;
293 const ListNode
<DeclarationAST
*> *member_specs
;
296 struct ForwardDeclarationSpecifierAST
: public TypeSpecifierAST
298 DECLARE_AST_NODE(ForwardDeclarationSpecifier
)
300 std::size_t class_key
;
302 BaseClauseAST
*base_clause
;
305 struct CompoundStatementAST
: public StatementAST
307 DECLARE_AST_NODE(CompoundStatement
)
309 const ListNode
<StatementAST
*> *statements
;
312 struct ConditionAST
: public AST
314 DECLARE_AST_NODE(Condition
)
316 TypeSpecifierAST
*type_specifier
;
317 DeclaratorAST
*declarator
;
318 ExpressionAST
*expression
;
321 struct ConditionalExpressionAST
: public ExpressionAST
323 DECLARE_AST_NODE(ConditionalExpression
)
325 ExpressionAST
*condition
;
326 ExpressionAST
*left_expression
;
327 ExpressionAST
*right_expression
;
330 struct CppCastExpressionAST
: public ExpressionAST
332 DECLARE_AST_NODE(CppCastExpression
)
336 ExpressionAST
*expression
;
337 const ListNode
<ExpressionAST
*> *sub_expressions
;
340 struct CtorInitializerAST
: public AST
342 DECLARE_AST_NODE(CtorInitializer
)
345 const ListNode
<MemInitializerAST
*> *member_initializers
;
348 struct DeclarationStatementAST
: public StatementAST
350 DECLARE_AST_NODE(DeclarationStatement
)
352 DeclarationAST
*declaration
;
355 struct DeclaratorAST
: public AST
357 DECLARE_AST_NODE(Declarator
)
359 const ListNode
<PtrOperatorAST
*> *ptr_ops
;
360 DeclaratorAST
*sub_declarator
;
362 ExpressionAST
*bit_expression
;
363 const ListNode
<ExpressionAST
*> *array_dimensions
;
364 ParameterDeclarationClauseAST
*parameter_declaration_clause
;
365 const ListNode
<std::size_t> *fun_cv
;
366 ExceptionSpecificationAST
*exception_spec
;
369 struct DeleteExpressionAST
: public ExpressionAST
371 DECLARE_AST_NODE(DeleteExpression
)
373 std::size_t scope_token
;
374 std::size_t delete_token
;
375 std::size_t lbracket_token
;
376 std::size_t rbracket_token
;
377 ExpressionAST
*expression
;
380 struct DoStatementAST
: public StatementAST
382 DECLARE_AST_NODE(DoStatement
)
384 StatementAST
*statement
;
385 ExpressionAST
*expression
;
388 struct ElaboratedTypeSpecifierAST
: public TypeSpecifierAST
390 DECLARE_AST_NODE(ElaboratedTypeSpecifier
)
396 struct EnumSpecifierAST
: public TypeSpecifierAST
398 DECLARE_AST_NODE(EnumSpecifier
)
401 const ListNode
<EnumeratorAST
*> *enumerators
;
404 struct EnumeratorAST
: public AST
406 DECLARE_AST_NODE(Enumerator
)
409 ExpressionAST
*expression
;
412 struct ExceptionSpecificationAST
: public AST
414 DECLARE_AST_NODE(ExceptionSpecification
)
416 std::size_t ellipsis
;
417 const ListNode
<TypeIdAST
*> *type_ids
;
420 struct ExpressionOrDeclarationStatementAST
: public StatementAST
422 DECLARE_AST_NODE(ExpressionOrDeclarationStatement
)
424 StatementAST
*expression
;
425 StatementAST
*declaration
;
428 struct ExpressionStatementAST
: public StatementAST
430 DECLARE_AST_NODE(ExpressionStatement
)
432 ExpressionAST
*expression
;
435 struct FunctionCallAST
: public ExpressionAST
437 DECLARE_AST_NODE(FunctionCall
)
439 ExpressionAST
*arguments
;
442 struct FunctionDefinitionAST
: public DeclarationAST
444 DECLARE_AST_NODE(FunctionDefinition
)
446 const ListNode
<std::size_t> *storage_specifiers
;
447 const ListNode
<std::size_t> *function_specifiers
;
448 TypeSpecifierAST
*type_specifier
;
449 InitDeclaratorAST
*init_declarator
;
450 StatementAST
*function_body
;
451 WinDeclSpecAST
*win_decl_specifiers
;
454 struct ForStatementAST
: public StatementAST
456 DECLARE_AST_NODE(ForStatement
)
458 StatementAST
*init_statement
;
459 ConditionAST
*condition
;
460 ExpressionAST
*expression
;
461 StatementAST
*statement
;
464 struct IfStatementAST
: public StatementAST
466 DECLARE_AST_NODE(IfStatement
)
468 ConditionAST
*condition
;
469 StatementAST
*statement
;
470 StatementAST
*else_statement
;
473 struct IncrDecrExpressionAST
: public ExpressionAST
475 DECLARE_AST_NODE(IncrDecrExpression
)
480 struct InitDeclaratorAST
: public AST
482 DECLARE_AST_NODE(InitDeclarator
)
484 DeclaratorAST
*declarator
;
485 InitializerAST
*initializer
;
488 struct InitializerAST
: public AST
490 DECLARE_AST_NODE(Initializer
)
492 InitializerClauseAST
*initializer_clause
;
493 ExpressionAST
*expression
;
496 struct InitializerClauseAST
: public AST
498 DECLARE_AST_NODE(InitializerClause
)
500 ExpressionAST
*expression
;
503 struct LabeledStatementAST
: public StatementAST
505 DECLARE_AST_NODE(LabeledStatement
)
508 struct LinkageBodyAST
: public AST
510 DECLARE_AST_NODE(LinkageBody
)
512 const ListNode
<DeclarationAST
*> *declarations
;
515 struct LinkageSpecificationAST
: public DeclarationAST
517 DECLARE_AST_NODE(LinkageSpecification
)
519 std::size_t extern_type
;
520 LinkageBodyAST
*linkage_body
;
521 DeclarationAST
*declaration
;
524 struct MemInitializerAST
: public AST
526 DECLARE_AST_NODE(MemInitializer
)
528 NameAST
*initializer_id
;
529 ExpressionAST
*expression
;
532 struct NameAST
: public AST
534 DECLARE_AST_NODE(Name
)
537 const ListNode
<UnqualifiedNameAST
*> *qualified_names
;
538 UnqualifiedNameAST
*unqualified_name
;
541 struct NamespaceAST
: public DeclarationAST
543 DECLARE_AST_NODE(Namespace
)
545 std::size_t namespace_name
;
546 LinkageBodyAST
*linkage_body
;
549 struct NamespaceAliasDefinitionAST
: public DeclarationAST
551 DECLARE_AST_NODE(NamespaceAliasDefinition
)
553 std::size_t namespace_name
;
557 struct NewDeclaratorAST
: public AST
559 DECLARE_AST_NODE(NewDeclarator
)
561 PtrOperatorAST
*ptr_op
;
562 NewDeclaratorAST
*sub_declarator
;
563 const ListNode
<ExpressionAST
*> *expressions
;
566 struct NewExpressionAST
: public ExpressionAST
568 DECLARE_AST_NODE(NewExpression
)
570 std::size_t scope_token
;
571 std::size_t new_token
;
572 ExpressionAST
*expression
;
574 NewTypeIdAST
*new_type_id
;
575 NewInitializerAST
*new_initializer
;
578 struct NewInitializerAST
: public AST
580 DECLARE_AST_NODE(NewInitializer
)
582 ExpressionAST
*expression
;
585 struct NewTypeIdAST
: public AST
587 DECLARE_AST_NODE(NewTypeId
)
589 TypeSpecifierAST
*type_specifier
;
590 NewInitializerAST
*new_initializer
;
591 NewDeclaratorAST
*new_declarator
;
594 struct OperatorAST
: public AST
596 DECLARE_AST_NODE(Operator
)
603 struct OperatorFunctionIdAST
: public AST
605 DECLARE_AST_NODE(OperatorFunctionId
)
608 TypeSpecifierAST
*type_specifier
;
609 const ListNode
<PtrOperatorAST
*> *ptr_ops
;
612 struct ParameterDeclarationAST
: public AST
614 DECLARE_AST_NODE(ParameterDeclaration
)
616 TypeSpecifierAST
*type_specifier
;
617 DeclaratorAST
*declarator
;
618 ExpressionAST
*expression
;
621 struct ParameterDeclarationClauseAST
: public AST
623 DECLARE_AST_NODE(ParameterDeclarationClause
)
625 const ListNode
<ParameterDeclarationAST
*> *parameter_declarations
;
626 std::size_t ellipsis
;
629 struct PostfixExpressionAST
: public ExpressionAST
631 DECLARE_AST_NODE(PostfixExpression
)
633 TypeSpecifierAST
*type_specifier
;
634 ExpressionAST
*expression
;
635 const ListNode
<ExpressionAST
*> *sub_expressions
;
638 struct PrimaryExpressionAST
: public ExpressionAST
640 DECLARE_AST_NODE(PrimaryExpression
)
642 StringLiteralAST
*literal
;
644 StatementAST
*expression_statement
;
645 ExpressionAST
*sub_expression
;
649 struct PtrOperatorAST
: public AST
651 DECLARE_AST_NODE(PtrOperator
)
653 const ListNode
<std::size_t> *cv
;
655 PtrToMemberAST
*mem_ptr
;
658 struct PtrToMemberAST
: public AST
660 DECLARE_AST_NODE(PtrToMember
)
663 struct ReturnStatementAST
: public StatementAST
665 DECLARE_AST_NODE(ReturnStatement
)
667 ExpressionAST
*expression
;
670 struct SimpleDeclarationAST
: public DeclarationAST
672 DECLARE_AST_NODE(SimpleDeclaration
)
674 const ListNode
<std::size_t> *storage_specifiers
;
675 const ListNode
<std::size_t> *function_specifiers
;
676 TypeSpecifierAST
*type_specifier
;
677 const ListNode
<InitDeclaratorAST
*> *init_declarators
;
678 WinDeclSpecAST
*win_decl_specifiers
;
681 struct SimpleTypeSpecifierAST
: public TypeSpecifierAST
683 DECLARE_AST_NODE(SimpleTypeSpecifier
)
685 const ListNode
<std::size_t> *integrals
;
688 ExpressionAST
*expression
;
692 struct SizeofExpressionAST
: public ExpressionAST
694 DECLARE_AST_NODE(SizeofExpression
)
696 std::size_t sizeof_token
;
698 ExpressionAST
*expression
;
701 struct StringLiteralAST
: public AST
703 DECLARE_AST_NODE(StringLiteral
)
705 const ListNode
<std::size_t> *literals
;
708 struct SubscriptExpressionAST
: public ExpressionAST
710 DECLARE_AST_NODE(SubscriptExpression
)
712 ExpressionAST
*subscript
;
715 struct SwitchStatementAST
: public StatementAST
717 DECLARE_AST_NODE(SwitchStatement
)
719 ConditionAST
*condition
;
720 StatementAST
*statement
;
723 struct TemplateArgumentAST
: public AST
725 DECLARE_AST_NODE(TemplateArgument
)
728 ExpressionAST
*expression
;
731 struct TemplateDeclarationAST
: public DeclarationAST
733 DECLARE_AST_NODE(TemplateDeclaration
)
735 std::size_t exported
;
736 const ListNode
<TemplateParameterAST
*> *template_parameters
;
737 DeclarationAST
* declaration
;
740 struct TemplateParameterAST
: public AST
742 DECLARE_AST_NODE(TemplateParameter
)
744 TypeParameterAST
*type_parameter
;
745 ParameterDeclarationAST
*parameter_declaration
;
748 struct ThrowExpressionAST
: public ExpressionAST
750 DECLARE_AST_NODE(ThrowExpression
)
752 std::size_t throw_token
;
753 ExpressionAST
*expression
;
756 struct TranslationUnitAST
: public AST
758 DECLARE_AST_NODE(TranslationUnit
)
760 const ListNode
<DeclarationAST
*> *declarations
;
763 struct TryBlockStatementAST
: public StatementAST
765 DECLARE_AST_NODE(TryBlockStatement
)
768 struct TypeIdAST
: public AST
770 DECLARE_AST_NODE(TypeId
)
772 TypeSpecifierAST
*type_specifier
;
773 DeclaratorAST
*declarator
;
776 struct TypeIdentificationAST
: public ExpressionAST
778 DECLARE_AST_NODE(TypeIdentification
)
780 std::size_t typename_token
;
782 ExpressionAST
*expression
;
785 struct TypeParameterAST
: public AST
787 DECLARE_AST_NODE(TypeParameter
)
792 const ListNode
<TemplateParameterAST
*> *template_parameters
;
793 NameAST
*template_name
;
796 struct TypedefAST
: public DeclarationAST
798 DECLARE_AST_NODE(Typedef
)
800 TypeSpecifierAST
*type_specifier
;
801 const ListNode
<InitDeclaratorAST
*> *init_declarators
;
804 struct UnaryExpressionAST
: public ExpressionAST
806 DECLARE_AST_NODE(UnaryExpression
)
809 ExpressionAST
*expression
;
812 struct UnqualifiedNameAST
: public AST
814 DECLARE_AST_NODE(UnqualifiedName
)
818 OperatorFunctionIdAST
*operator_id
;
819 const ListNode
<TemplateArgumentAST
*> *template_arguments
;
822 struct UsingAST
: public DeclarationAST
824 DECLARE_AST_NODE(Using
)
826 std::size_t type_name
;
830 struct UsingDirectiveAST
: public DeclarationAST
832 DECLARE_AST_NODE(UsingDirective
)
837 struct WhileStatementAST
: public StatementAST
839 DECLARE_AST_NODE(WhileStatement
)
841 ConditionAST
*condition
;
842 StatementAST
*statement
;
845 struct WinDeclSpecAST
: public AST
847 DECLARE_AST_NODE(WinDeclSpec
)
849 std::size_t specifier
;
850 std::size_t modifier
;
853 struct QPropertyAST
: public DeclarationAST
855 DECLARE_AST_NODE(QPropertyAST
)
858 struct QEnumsAST
: public DeclarationAST
860 DECLARE_AST_NODE(QEnumsAST
)
864 _Tp
*CreateNode(pool
*memory_pool
)
866 _Tp
*node
= reinterpret_cast<_Tp
*>(memory_pool
->allocate(sizeof(_Tp
)));
867 node
->kind
= _Tp::__node_kind
;
872 _Tp
ast_cast(AST
*item
)
874 if (item
&& static_cast<_Tp
>(0)->__node_kind
== item
->kind
)
875 return static_cast<_Tp
>(item
);
882 // kate: space-indent on; indent-width 2; replace-tabs on;