|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com> |
|
42 // |
|
43 // Permission is hereby granted, free of charge, to any person obtaining a copy |
|
44 // of this software and associated documentation files (the "Software"), to deal |
|
45 // in the Software without restriction, including without limitation the rights |
|
46 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
47 // copies of the Software, and to permit persons to whom the Software is |
|
48 // furnished to do so, subject to the following conditions: |
|
49 // |
|
50 // The above copyright notice and this permission notice shall be included in |
|
51 // all copies or substantial portions of the Software. |
|
52 // |
|
53 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
54 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
55 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
56 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
57 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
58 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
59 // THE SOFTWARE. |
|
60 |
|
61 #ifndef CPLUSPLUS_PARSER_H |
|
62 #define CPLUSPLUS_PARSER_H |
|
63 |
|
64 #include "CPlusPlusForwardDeclarations.h" |
|
65 #include "ASTfwd.h" |
|
66 #include "Token.h" |
|
67 #include "TranslationUnit.h" |
|
68 #include <map> |
|
69 |
|
70 namespace CPlusPlus { |
|
71 |
|
72 class CPLUSPLUS_EXPORT Parser |
|
73 { |
|
74 public: |
|
75 Parser(TranslationUnit *translationUnit); |
|
76 ~Parser(); |
|
77 |
|
78 bool qtMocRunEnabled() const; |
|
79 void setQtMocRunEnabled(bool onoff); |
|
80 |
|
81 bool objCEnabled() const; |
|
82 void setObjCEnabled(bool onoff); |
|
83 |
|
84 bool parseTranslationUnit(TranslationUnitAST *&node); |
|
85 |
|
86 public: |
|
87 bool parseAccessSpecifier(SpecifierAST *&node); |
|
88 bool parseExpressionList(ExpressionListAST *&node); |
|
89 bool parseAbstractCoreDeclarator(DeclaratorAST *&node); |
|
90 bool parseAbstractDeclarator(DeclaratorAST *&node); |
|
91 bool parseEmptyDeclaration(DeclarationAST *&node); |
|
92 bool parseAccessDeclaration(DeclarationAST *&node); |
|
93 #ifdef ICHECK_BUILD |
|
94 bool parseQPropertyDeclaration(DeclarationAST *&node); |
|
95 bool parseQEnumDeclaration(DeclarationAST *&node); |
|
96 bool parseQFlags(DeclarationAST *&node); |
|
97 bool parseQDeclareFlags(DeclarationAST *&node); |
|
98 #endif |
|
99 bool parseAdditiveExpression(ExpressionAST *&node); |
|
100 bool parseAndExpression(ExpressionAST *&node); |
|
101 bool parseAsmDefinition(DeclarationAST *&node); |
|
102 bool parseAsmOperandList(); |
|
103 bool parseAsmOperand(); |
|
104 bool parseAsmClobberList(); |
|
105 bool parseAssignmentExpression(ExpressionAST *&node); |
|
106 bool parseBaseClause(BaseSpecifierListAST *&node); |
|
107 bool parseBaseSpecifier(BaseSpecifierListAST *&node); |
|
108 bool parseBlockDeclaration(DeclarationAST *&node); |
|
109 bool parseCppCastExpression(ExpressionAST *&node); |
|
110 bool parseCastExpression(ExpressionAST *&node); |
|
111 bool parseClassSpecifier(SpecifierListAST *&node); |
|
112 bool parseCommaExpression(ExpressionAST *&node); |
|
113 bool parseCompoundStatement(StatementAST *&node); |
|
114 bool parseBreakStatement(StatementAST *&node); |
|
115 bool parseContinueStatement(StatementAST *&node); |
|
116 bool parseGotoStatement(StatementAST *&node); |
|
117 bool parseReturnStatement(StatementAST *&node); |
|
118 bool parseCondition(ExpressionAST *&node); |
|
119 bool parseConditionalExpression(ExpressionAST *&node); |
|
120 bool parseConstantExpression(ExpressionAST *&node); |
|
121 bool parseCtorInitializer(CtorInitializerAST *&node); |
|
122 bool parseCvQualifiers(SpecifierListAST *&node); |
|
123 bool parseDeclaratorOrAbstractDeclarator(DeclaratorAST *&node); |
|
124 bool parseDeclaration(DeclarationAST *&node); |
|
125 bool parseSimpleDeclaration(DeclarationAST *&node, bool acceptStructDeclarator = false); |
|
126 bool parseDeclarationStatement(StatementAST *&node); |
|
127 bool parseCoreDeclarator(DeclaratorAST *&node); |
|
128 bool parseDeclarator(DeclaratorAST *&node, bool stopAtCppInitializer = false); |
|
129 bool parseDeleteExpression(ExpressionAST *&node); |
|
130 bool parseDoStatement(StatementAST *&node); |
|
131 bool parseElaboratedTypeSpecifier(SpecifierListAST *&node); |
|
132 bool parseEnumSpecifier(SpecifierListAST *&node); |
|
133 bool parseEnumerator(EnumeratorListAST *&node); |
|
134 bool parseEqualityExpression(ExpressionAST *&node); |
|
135 bool parseExceptionDeclaration(ExceptionDeclarationAST *&node); |
|
136 bool parseExceptionSpecification(ExceptionSpecificationAST *&node); |
|
137 bool parseExclusiveOrExpression(ExpressionAST *&node); |
|
138 bool parseExpression(ExpressionAST *&node); |
|
139 bool parseExpressionOrDeclarationStatement(StatementAST *&node); |
|
140 bool parseExpressionStatement(StatementAST *&node); |
|
141 bool parseForInitStatement(StatementAST *&node); |
|
142 bool parseForeachStatement(StatementAST *&node); |
|
143 bool parseForStatement(StatementAST *&node); |
|
144 bool parseFunctionBody(StatementAST *&node); |
|
145 bool parseIfStatement(StatementAST *&node); |
|
146 bool parseInclusiveOrExpression(ExpressionAST *&node); |
|
147 bool parseInitDeclarator(DeclaratorAST *&node, bool acceptStructDeclarator); |
|
148 bool parseInitializerList(ExpressionListAST *&node); |
|
149 bool parseInitializer(ExpressionAST *&node, unsigned *equals_token); |
|
150 bool parseInitializerClause(ExpressionAST *&node); |
|
151 bool parseLabeledStatement(StatementAST *&node); |
|
152 bool parseLinkageBody(DeclarationAST *&node); |
|
153 bool parseLinkageSpecification(DeclarationAST *&node); |
|
154 bool parseLogicalAndExpression(ExpressionAST *&node); |
|
155 bool parseLogicalOrExpression(ExpressionAST *&node); |
|
156 bool parseMemInitializer(MemInitializerListAST *&node); |
|
157 bool parseMemInitializerList(MemInitializerListAST *&node); |
|
158 bool parseMemberSpecification(DeclarationAST *&node); |
|
159 bool parseMultiplicativeExpression(ExpressionAST *&node); |
|
160 bool parseTemplateId(NameAST *&node); |
|
161 bool parseClassOrNamespaceName(NameAST *&node); |
|
162 bool parseNameId(NameAST *&node); |
|
163 bool parseName(NameAST *&node, bool acceptTemplateId = true); |
|
164 bool parseNestedNameSpecifier(NestedNameSpecifierListAST *&node, bool acceptTemplateId); |
|
165 bool parseNestedNameSpecifierOpt(NestedNameSpecifierListAST *&name, bool acceptTemplateId); |
|
166 bool parseNamespace(DeclarationAST *&node); |
|
167 bool parseNamespaceAliasDefinition(DeclarationAST *&node); |
|
168 bool parseNewArrayDeclarator(NewArrayDeclaratorListAST *&node); |
|
169 bool parseNewExpression(ExpressionAST *&node); |
|
170 bool parseNewPlacement(NewPlacementAST *&node); |
|
171 bool parseNewInitializer(NewInitializerAST *&node); |
|
172 bool parseNewTypeId(NewTypeIdAST *&node); |
|
173 bool parseOperator(OperatorAST *&node); |
|
174 bool parseConversionFunctionId(NameAST *&node); |
|
175 bool parseOperatorFunctionId(NameAST *&node); |
|
176 bool parseParameterDeclaration(DeclarationAST *&node); |
|
177 bool parseParameterDeclarationClause(ParameterDeclarationClauseAST *&node); |
|
178 bool parseParameterDeclarationList(DeclarationListAST *&node); |
|
179 bool parsePmExpression(ExpressionAST *&node); |
|
180 bool parseTypeidExpression(ExpressionAST *&node); |
|
181 bool parseTypenameCallExpression(ExpressionAST *&node); |
|
182 bool parseCorePostfixExpression(ExpressionAST *&node); |
|
183 bool parsePostfixExpression(ExpressionAST *&node); |
|
184 bool parsePostfixExpressionInternal(ExpressionAST *&node); |
|
185 bool parsePrimaryExpression(ExpressionAST *&node); |
|
186 bool parseNestedExpression(ExpressionAST *&node); |
|
187 bool parsePtrOperator(PtrOperatorListAST *&node); |
|
188 bool parseRelationalExpression(ExpressionAST *&node); |
|
189 bool parseShiftExpression(ExpressionAST *&node); |
|
190 bool parseStatement(StatementAST *&node); |
|
191 bool parseThisExpression(ExpressionAST *&node); |
|
192 bool parseBoolLiteral(ExpressionAST *&node); |
|
193 bool parseNumericLiteral(ExpressionAST *&node); |
|
194 bool parseStringLiteral(ExpressionAST *&node); |
|
195 bool parseSwitchStatement(StatementAST *&node); |
|
196 bool parseTemplateArgument(ExpressionAST *&node); |
|
197 bool parseTemplateArgumentList(TemplateArgumentListAST *&node); |
|
198 bool parseTemplateDeclaration(DeclarationAST *&node); |
|
199 bool parseTemplateParameter(DeclarationAST *&node); |
|
200 bool parseTemplateParameterList(DeclarationListAST *&node); |
|
201 bool parseThrowExpression(ExpressionAST *&node); |
|
202 bool parseTryBlockStatement(StatementAST *&node); |
|
203 bool parseCatchClause(CatchClauseListAST *&node); |
|
204 bool parseTypeId(ExpressionAST *&node); |
|
205 bool parseTypeIdList(ExpressionListAST *&node); |
|
206 bool parseTypenameTypeParameter(DeclarationAST *&node); |
|
207 bool parseTemplateTypeParameter(DeclarationAST *&node); |
|
208 bool parseTypeParameter(DeclarationAST *&node); |
|
209 |
|
210 bool parseBuiltinTypeSpecifier(SpecifierListAST *&node); |
|
211 bool parseAttributeSpecifier(SpecifierListAST *&node); |
|
212 bool parseAttributeList(AttributeListAST *&node); |
|
213 |
|
214 bool parseSimpleTypeSpecifier(SpecifierListAST *&node) |
|
215 { return parseDeclSpecifierSeq(node, true, true); } |
|
216 |
|
217 bool parseTypeSpecifier(SpecifierListAST *&node) |
|
218 { return parseDeclSpecifierSeq(node, true); } |
|
219 |
|
220 bool parseDeclSpecifierSeq(SpecifierListAST *&node, |
|
221 bool onlyTypeSpecifiers = false, |
|
222 bool simplified = false); |
|
223 bool parseUnaryExpression(ExpressionAST *&node); |
|
224 bool parseUnqualifiedName(NameAST *&node, bool acceptTemplateId = true); |
|
225 bool parseUsing(DeclarationAST *&node); |
|
226 bool parseUsingDirective(DeclarationAST *&node); |
|
227 bool parseWhileStatement(StatementAST *&node); |
|
228 |
|
229 void parseExpressionWithOperatorPrecedence(ExpressionAST *&lhs, int minPrecedence); |
|
230 |
|
231 // Qt MOC run |
|
232 bool parseQtMethod(ExpressionAST *&node); |
|
233 |
|
234 // ObjC++ |
|
235 bool parseObjCExpression(ExpressionAST *&node); |
|
236 bool parseObjCClassForwardDeclaration(DeclarationAST *&node); |
|
237 bool parseObjCInterface(DeclarationAST *&node, |
|
238 SpecifierListAST *attributes = 0); |
|
239 bool parseObjCProtocol(DeclarationAST *&node, |
|
240 SpecifierListAST *attributes = 0); |
|
241 |
|
242 bool parseObjCSynchronizedStatement(StatementAST *&node); |
|
243 bool parseObjCEncodeExpression(ExpressionAST *&node); |
|
244 bool parseObjCProtocolExpression(ExpressionAST *&node); |
|
245 bool parseObjCSelectorExpression(ExpressionAST *&node); |
|
246 bool parseObjCStringLiteral(ExpressionAST *&node); |
|
247 bool parseObjCMessageExpression(ExpressionAST *&node); |
|
248 bool parseObjCMessageReceiver(ExpressionAST *&node); |
|
249 bool parseObjCMessageArguments(ObjCSelectorAST *&selNode, ObjCMessageArgumentListAST *& argNode); |
|
250 bool parseObjCSelectorArg(ObjCSelectorArgumentAST *&selNode, ObjCMessageArgumentAST *&argNode); |
|
251 bool parseObjCMethodDefinitionList(DeclarationListAST *&node); |
|
252 bool parseObjCMethodDefinition(DeclarationAST *&node); |
|
253 |
|
254 bool parseObjCProtocolRefs(ObjCProtocolRefsAST *&node); |
|
255 bool parseObjClassInstanceVariables(ObjCInstanceVariablesDeclarationAST *&node); |
|
256 bool parseObjCInterfaceMemberDeclaration(DeclarationAST *&node); |
|
257 bool parseObjCInstanceVariableDeclaration(DeclarationAST *&node); |
|
258 bool parseObjCPropertyDeclaration(DeclarationAST *&node, |
|
259 SpecifierListAST *attributes = 0); |
|
260 bool parseObjCImplementation(DeclarationAST *&node); |
|
261 bool parseObjCMethodPrototype(ObjCMethodPrototypeAST *&node); |
|
262 bool parseObjCPropertyAttribute(ObjCPropertyAttributeAST *&node); |
|
263 bool parseObjCTypeName(ObjCTypeNameAST *&node); |
|
264 bool parseObjCSelector(unsigned &selector_token); |
|
265 bool parseObjCKeywordDeclaration(ObjCSelectorArgumentAST *&argument, ObjCMessageArgumentDeclarationAST *&node); |
|
266 bool parseObjCTypeQualifiers(unsigned &type_qualifier); |
|
267 bool peekAtObjCContextKeyword(int kind); |
|
268 bool parseObjCContextKeyword(int kind, unsigned &in_token); |
|
269 |
|
270 bool lookAtObjCSelector() const; |
|
271 |
|
272 bool skipUntil(int token); |
|
273 void skipUntilDeclaration(); |
|
274 bool skipUntilStatement(); |
|
275 bool skip(int l, int r); |
|
276 |
|
277 bool lookAtCVQualifier() const; |
|
278 bool lookAtFunctionSpecifier() const; |
|
279 bool lookAtStorageClassSpecifier() const; |
|
280 bool lookAtBuiltinTypeSpecifier() const; |
|
281 bool lookAtClassKey() const; |
|
282 |
|
283 void match(int kind, unsigned *token); |
|
284 |
|
285 bool maybeAmbiguousStatement(DeclarationStatementAST *ast) const; |
|
286 bool maybeForwardOrClassDeclaration(SpecifierListAST *decl_specifier_seq) const; |
|
287 bool isPointerDeclaration(DeclarationStatementAST *ast) const; |
|
288 |
|
289 private: |
|
290 bool switchTemplateArguments(bool templateArguments); |
|
291 bool blockErrors(bool block); |
|
292 |
|
293 inline const Token &tok(int i = 1) const |
|
294 { return _translationUnit->tokenAt(_tokenIndex + i - 1); } |
|
295 |
|
296 inline int LA(int n = 1) const |
|
297 { return _translationUnit->tokenKind(_tokenIndex + n - 1); } |
|
298 |
|
299 inline int consumeToken() |
|
300 { return _tokenIndex++; } |
|
301 |
|
302 inline unsigned cursor() const |
|
303 { return _tokenIndex; } |
|
304 |
|
305 inline void rewind(unsigned cursor) |
|
306 { _tokenIndex = cursor; } |
|
307 |
|
308 struct TemplateArgumentListEntry { |
|
309 unsigned index; |
|
310 unsigned cursor; |
|
311 TemplateArgumentListAST *ast; |
|
312 |
|
313 TemplateArgumentListEntry(unsigned index = 0, unsigned cursor = 0, TemplateArgumentListAST *ast = 0) |
|
314 : index(index), cursor(cursor), ast(ast) {} |
|
315 }; |
|
316 |
|
317 TemplateArgumentListEntry *templateArgumentListEntry(unsigned tokenIndex); |
|
318 |
|
319 private: |
|
320 TranslationUnit *_translationUnit; |
|
321 Control *_control; |
|
322 MemoryPool *_pool; |
|
323 unsigned _tokenIndex; |
|
324 bool _templateArguments: 1; |
|
325 bool _qtMocRunEnabled: 1; |
|
326 bool _objCEnabled: 1; |
|
327 bool _inFunctionBody: 1; |
|
328 bool _inObjCImplementationContext: 1; |
|
329 int _expressionDepth; |
|
330 |
|
331 std::map<unsigned, TemplateArgumentListEntry> _templateArgumentList; |
|
332 |
|
333 class Rewind; |
|
334 friend class Rewind; |
|
335 |
|
336 private: |
|
337 Parser(const Parser& source); |
|
338 void operator =(const Parser& source); |
|
339 }; |
|
340 |
|
341 } // end of namespace CPlusPlus |
|
342 |
|
343 |
|
344 #endif // CPLUSPLUS_PARSER_H |