Parser.h 15.2 KB
Newer Older
con's avatar
con committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef CPLUSPLUS_PARSER_H
#define CPLUSPLUS_PARSER_H

#include "CPlusPlusForwardDeclarations.h"
#include "ASTfwd.h"
#include "Token.h"
#include "TranslationUnit.h"
28
#include "MemoryPool.h"
Roberto Raggi's avatar
Roberto Raggi committed
29
#include <map>
con's avatar
con committed
30

Roberto Raggi's avatar
Roberto Raggi committed
31
namespace CPlusPlus {
con's avatar
con committed
32
33
34
35
36
37
38
39
40
41
42

class CPLUSPLUS_EXPORT Parser
{
public:
    Parser(TranslationUnit *translationUnit);
    ~Parser();

    bool parseTranslationUnit(TranslationUnitAST *&node);

public:
    bool parseExpressionList(ExpressionListAST *&node);
43
44
    bool parseAbstractCoreDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list);
    bool parseAbstractDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list);
con's avatar
con committed
45
46
    bool parseEmptyDeclaration(DeclarationAST *&node);
    bool parseAccessDeclaration(DeclarationAST *&node);
47
48
49
    bool parseQtPropertyDeclaration(DeclarationAST *&node);
    bool parseQtEnumDeclaration(DeclarationAST *&node);
    bool parseQtFlags(DeclarationAST *&node);
50
    bool parseQtInterfaces(DeclarationAST *&node);
con's avatar
con committed
51
52
53
    bool parseAdditiveExpression(ExpressionAST *&node);
    bool parseAndExpression(ExpressionAST *&node);
    bool parseAsmDefinition(DeclarationAST *&node);
54
55
56
    bool parseAsmOperandList();
    bool parseAsmOperand();
    bool parseAsmClobberList();
con's avatar
con committed
57
    bool parseAssignmentExpression(ExpressionAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
58
59
    bool parseBaseClause(BaseSpecifierListAST *&node);
    bool parseBaseSpecifier(BaseSpecifierListAST *&node);
con's avatar
con committed
60
61
62
    bool parseBlockDeclaration(DeclarationAST *&node);
    bool parseCppCastExpression(ExpressionAST *&node);
    bool parseCastExpression(ExpressionAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
63
    bool parseClassSpecifier(SpecifierListAST *&node);
con's avatar
con committed
64
65
66
67
68
69
70
71
72
73
    bool parseCommaExpression(ExpressionAST *&node);
    bool parseCompoundStatement(StatementAST *&node);
    bool parseBreakStatement(StatementAST *&node);
    bool parseContinueStatement(StatementAST *&node);
    bool parseGotoStatement(StatementAST *&node);
    bool parseReturnStatement(StatementAST *&node);
    bool parseCondition(ExpressionAST *&node);
    bool parseConditionalExpression(ExpressionAST *&node);
    bool parseConstantExpression(ExpressionAST *&node);
    bool parseCtorInitializer(CtorInitializerAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
74
    bool parseCvQualifiers(SpecifierListAST *&node);
75
    bool parseRefQualifier(unsigned &ref_qualifier);
76
    bool parseOverrideFinalQualifiers(SpecifierListAST *&node);
77
    bool parseDeclaratorOrAbstractDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list);
con's avatar
con committed
78
    bool parseDeclaration(DeclarationAST *&node);
79
    bool parseSimpleDeclaration(DeclarationAST *&node, ClassSpecifierAST *declaringClass = 0);
con's avatar
con committed
80
    bool parseDeclarationStatement(StatementAST *&node);
81
82
    bool parseCoreDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list, ClassSpecifierAST *declaringClass);
    bool parseDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list, ClassSpecifierAST *declaringClass = 0);
con's avatar
con committed
83
84
    bool parseDeleteExpression(ExpressionAST *&node);
    bool parseDoStatement(StatementAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
85
86
    bool parseElaboratedTypeSpecifier(SpecifierListAST *&node);
    bool parseEnumSpecifier(SpecifierListAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
87
    bool parseEnumerator(EnumeratorListAST *&node);
con's avatar
con committed
88
89
90
91
92
93
94
95
    bool parseEqualityExpression(ExpressionAST *&node);
    bool parseExceptionDeclaration(ExceptionDeclarationAST *&node);
    bool parseExceptionSpecification(ExceptionSpecificationAST *&node);
    bool parseExclusiveOrExpression(ExpressionAST *&node);
    bool parseExpression(ExpressionAST *&node);
    bool parseExpressionOrDeclarationStatement(StatementAST *&node);
    bool parseExpressionStatement(StatementAST *&node);
    bool parseForInitStatement(StatementAST *&node);
96
    bool parseForeachStatement(StatementAST *&node);
con's avatar
con committed
97
98
99
100
    bool parseForStatement(StatementAST *&node);
    bool parseFunctionBody(StatementAST *&node);
    bool parseIfStatement(StatementAST *&node);
    bool parseInclusiveOrExpression(ExpressionAST *&node);
101
    bool parseInitDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specifier_list, ClassSpecifierAST *declaringClass);
con's avatar
con committed
102
    bool parseInitializerList(ExpressionListAST *&node);
103
    bool parseInitializer(ExpressionAST *&node, unsigned *equals_token);
con's avatar
con committed
104
105
106
107
108
109
    bool parseInitializerClause(ExpressionAST *&node);
    bool parseLabeledStatement(StatementAST *&node);
    bool parseLinkageBody(DeclarationAST *&node);
    bool parseLinkageSpecification(DeclarationAST *&node);
    bool parseLogicalAndExpression(ExpressionAST *&node);
    bool parseLogicalOrExpression(ExpressionAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
110
111
    bool parseMemInitializer(MemInitializerListAST *&node);
    bool parseMemInitializerList(MemInitializerListAST *&node);
112
    bool parseMemberSpecification(DeclarationAST *&node, ClassSpecifierAST *declaringClass);
con's avatar
con committed
113
    bool parseMultiplicativeExpression(ExpressionAST *&node);
114
    bool parseTemplateId(NameAST *&node, unsigned template_token = 0);
con's avatar
con committed
115
    bool parseClassOrNamespaceName(NameAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
116
    bool parseNameId(NameAST *&node);
con's avatar
con committed
117
    bool parseName(NameAST *&node, bool acceptTemplateId = true);
Roberto Raggi's avatar
Roberto Raggi committed
118
119
    bool parseNestedNameSpecifier(NestedNameSpecifierListAST *&node, bool acceptTemplateId);
    bool parseNestedNameSpecifierOpt(NestedNameSpecifierListAST *&name, bool acceptTemplateId);
120
    bool parseStaticAssertDeclaration(DeclarationAST *&node);
con's avatar
con committed
121
122
    bool parseNamespace(DeclarationAST *&node);
    bool parseNamespaceAliasDefinition(DeclarationAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
123
    bool parseNewArrayDeclarator(NewArrayDeclaratorListAST *&node);
con's avatar
con committed
124
    bool parseNewExpression(ExpressionAST *&node);
125
    bool parseExpressionListParen(ExpressionAST *&node);
126
    bool parseNewInitializer(ExpressionAST *&node);
con's avatar
con committed
127
128
129
130
    bool parseNewTypeId(NewTypeIdAST *&node);
    bool parseOperator(OperatorAST *&node);
    bool parseConversionFunctionId(NameAST *&node);
    bool parseOperatorFunctionId(NameAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
131
    bool parseParameterDeclaration(ParameterDeclarationAST *&node);
con's avatar
con committed
132
    bool parseParameterDeclarationClause(ParameterDeclarationClauseAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
133
    bool parseParameterDeclarationList(ParameterDeclarationListAST *&node);
con's avatar
con committed
134
135
136
137
138
139
140
141
    bool parsePmExpression(ExpressionAST *&node);
    bool parseTypeidExpression(ExpressionAST *&node);
    bool parseTypenameCallExpression(ExpressionAST *&node);
    bool parseCorePostfixExpression(ExpressionAST *&node);
    bool parsePostfixExpression(ExpressionAST *&node);
    bool parsePostfixExpressionInternal(ExpressionAST *&node);
    bool parsePrimaryExpression(ExpressionAST *&node);
    bool parseNestedExpression(ExpressionAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
142
    bool parsePtrOperator(PtrOperatorListAST *&node);
con's avatar
con committed
143
144
145
146
147
148
    bool parseRelationalExpression(ExpressionAST *&node);
    bool parseShiftExpression(ExpressionAST *&node);
    bool parseStatement(StatementAST *&node);
    bool parseThisExpression(ExpressionAST *&node);
    bool parseBoolLiteral(ExpressionAST *&node);
    bool parseNumericLiteral(ExpressionAST *&node);
Leandro Melo's avatar
Leandro Melo committed
149
    bool parsePointerLiteral(ExpressionAST *&node);
con's avatar
con committed
150
151
152
    bool parseStringLiteral(ExpressionAST *&node);
    bool parseSwitchStatement(StatementAST *&node);
    bool parseTemplateArgument(ExpressionAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
153
    bool parseTemplateArgumentList(ExpressionListAST *&node);
con's avatar
con committed
154
155
    bool parseTemplateDeclaration(DeclarationAST *&node);
    bool parseTemplateParameter(DeclarationAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
156
    bool parseTemplateParameterList(DeclarationListAST *&node);
con's avatar
con committed
157
    bool parseThrowExpression(ExpressionAST *&node);
158
    bool parseTryBlockStatement(StatementAST *&node, CtorInitializerAST **placeholder);
Roberto Raggi's avatar
Roberto Raggi committed
159
    bool parseCatchClause(CatchClauseListAST *&node);
con's avatar
con committed
160
161
162
163
164
165
    bool parseTypeId(ExpressionAST *&node);
    bool parseTypeIdList(ExpressionListAST *&node);
    bool parseTypenameTypeParameter(DeclarationAST *&node);
    bool parseTemplateTypeParameter(DeclarationAST *&node);
    bool parseTypeParameter(DeclarationAST *&node);

Roberto Raggi's avatar
Roberto Raggi committed
166
167
    bool parseBuiltinTypeSpecifier(SpecifierListAST *&node);
    bool parseAttributeSpecifier(SpecifierListAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
168
    bool parseAttributeList(AttributeListAST *&node);
con's avatar
con committed
169

Roberto Raggi's avatar
Roberto Raggi committed
170
    bool parseSimpleTypeSpecifier(SpecifierListAST *&node)
con's avatar
con committed
171
172
    { return parseDeclSpecifierSeq(node, true, true); }

Roberto Raggi's avatar
Roberto Raggi committed
173
    bool parseTypeSpecifier(SpecifierListAST *&node)
con's avatar
con committed
174
175
    { return parseDeclSpecifierSeq(node, true); }

Roberto Raggi's avatar
Roberto Raggi committed
176
    bool parseDeclSpecifierSeq(SpecifierListAST *&node,
con's avatar
con committed
177
178
179
180
181
182
                               bool onlyTypeSpecifiers = false,
                               bool simplified = false);
    bool parseUnaryExpression(ExpressionAST *&node);
    bool parseUnqualifiedName(NameAST *&node, bool acceptTemplateId = true);
    bool parseUsing(DeclarationAST *&node);
    bool parseUsingDirective(DeclarationAST *&node);
183
    bool parseAliasDeclaration(DeclarationAST *&node);
con's avatar
con committed
184
185
    bool parseWhileStatement(StatementAST *&node);

186
187
    void parseExpressionWithOperatorPrecedence(ExpressionAST *&lhs, int minPrecedence);

Roberto Raggi's avatar
Roberto Raggi committed
188
189
190
    // Qt MOC run
    bool parseQtMethod(ExpressionAST *&node);

191
    // C++0x
Roberto Raggi's avatar
Roberto Raggi committed
192
193
194
195
196
197
    bool parseInitializer0x(ExpressionAST *&node, unsigned *equals_token);
    bool parseBraceOrEqualInitializer0x(ExpressionAST *&node);
    bool parseInitializerClause0x(ExpressionAST *&node);
    bool parseInitializerList0x(ExpressionListAST *&node);
    bool parseBracedInitList0x(ExpressionAST *&node);

198
199
200
201
202
203
204
205
206
    bool parseLambdaExpression(ExpressionAST *&node);
    bool parseLambdaIntroducer(LambdaIntroducerAST *&node);
    bool parseLambdaCapture(LambdaCaptureAST *&node);
    bool parseLambdaDeclarator(LambdaDeclaratorAST *&node);
    bool parseCapture(CaptureAST *&node);
    bool parseCaptureList(CaptureListAST *&node);
    bool parseTrailingReturnType(TrailingReturnTypeAST *&node);
    bool parseTrailingTypeSpecifierSeq(SpecifierListAST *&node);

207
    // ObjC++
208
    bool parseObjCExpression(ExpressionAST *&node);
Erik Verbruggen's avatar
Erik Verbruggen committed
209
    bool parseObjCClassForwardDeclaration(DeclarationAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
210
    bool parseObjCInterface(DeclarationAST *&node,
Roberto Raggi's avatar
Roberto Raggi committed
211
                            SpecifierListAST *attributes = 0);
Roberto Raggi's avatar
Roberto Raggi committed
212
    bool parseObjCProtocol(DeclarationAST *&node,
Roberto Raggi's avatar
Roberto Raggi committed
213
                           SpecifierListAST *attributes = 0);
214

215
    bool parseObjCTryStatement(StatementAST *&node);
216
    bool parseObjCSynchronizedStatement(StatementAST *&node);
217
    bool parseObjCThrowStatement(StatementAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
218
    bool parseObjCEncodeExpression(ExpressionAST *&node);
219
    bool parseObjCProtocolExpression(ExpressionAST *&node);
220
221
    bool parseObjCSelectorExpression(ExpressionAST *&node);
    bool parseObjCStringLiteral(ExpressionAST *&node);
222
    bool parseObjCMessageExpression(ExpressionAST *&node);
223
    bool parseObjCMessageReceiver(ExpressionAST *&node);
224
225
    bool parseObjCMessageArguments(ObjCSelectorAST *&selNode, ObjCMessageArgumentListAST *& argNode);
    bool parseObjCSelectorArg(ObjCSelectorArgumentAST *&selNode, ObjCMessageArgumentAST *&argNode);
Erik Verbruggen's avatar
Erik Verbruggen committed
226
227
    bool parseObjCMethodDefinitionList(DeclarationListAST *&node);
    bool parseObjCMethodDefinition(DeclarationAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
228

Erik Verbruggen's avatar
Erik Verbruggen committed
229
    bool parseObjCProtocolRefs(ObjCProtocolRefsAST *&node);
230
    bool parseObjClassInstanceVariables(ObjCInstanceVariablesDeclarationAST *&node);
231
    bool parseObjCInterfaceMemberDeclaration(DeclarationAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
232
233
    bool parseObjCInstanceVariableDeclaration(DeclarationAST *&node);
    bool parseObjCPropertyDeclaration(DeclarationAST *&node,
Roberto Raggi's avatar
Roberto Raggi committed
234
                                      SpecifierListAST *attributes = 0);
235
    bool parseObjCImplementation(DeclarationAST *&node);
236
    bool parseObjCMethodPrototype(ObjCMethodPrototypeAST *&node);
237
    bool parseObjCPropertyAttribute(ObjCPropertyAttributeAST *&node);
238
    bool parseObjCTypeName(ObjCTypeNameAST *&node);
239
    bool parseObjCSelector(unsigned &selector_token);
240
    bool parseObjCKeywordDeclaration(ObjCSelectorArgumentAST *&argument, ObjCMessageArgumentDeclarationAST *&node);
241
    bool parseObjCTypeQualifiers(unsigned &type_qualifier);
242
    bool peekAtObjCContextKeyword(int kind);
243
    bool parseObjCContextKeyword(int kind, unsigned &in_token);
Roberto Raggi's avatar
Roberto Raggi committed
244
245
246

    bool lookAtObjCSelector() const;

con's avatar
con committed
247
    bool skipUntil(int token);
248
    void skipUntilDeclaration();
con's avatar
con committed
249
250
    bool skipUntilStatement();
    bool skip(int l, int r);
251
    int find(int token, int stopAt);
con's avatar
con committed
252

253
    bool lookAtTypeParameter();
con's avatar
con committed
254
255
256
257
258
259
    bool lookAtCVQualifier() const;
    bool lookAtFunctionSpecifier() const;
    bool lookAtStorageClassSpecifier() const;
    bool lookAtBuiltinTypeSpecifier() const;
    bool lookAtClassKey() const;

260
261
262
    const Identifier *className(ClassSpecifierAST *ast) const;
    const Identifier *identifier(NameAST *name) const;

con's avatar
con committed
263
264
    void match(int kind, unsigned *token);

265
    bool maybeAmbiguousStatement(DeclarationStatementAST *ast, StatementAST *&node);
Roberto Raggi's avatar
Roberto Raggi committed
266
    bool maybeForwardOrClassDeclaration(SpecifierListAST *decl_specifier_seq) const;
con's avatar
con committed
267

268
269
    int peekAtQtContextKeyword() const;

con's avatar
con committed
270
    bool switchTemplateArguments(bool templateArguments);
271
    bool maybeSplitGreaterGreaterToken(int n = 1);
272

273
    bool blockErrors(bool block) { return _translationUnit->blockErrors(block); }
274
275
276
    void warning(unsigned index, const char *format, ...);
    void error(unsigned index, const char *format, ...);
    void fatal(unsigned index, const char *format, ...);
con's avatar
con committed
277

Roberto Raggi's avatar
Roberto Raggi committed
278
279
    inline const Token &tok(int i = 1) const
    { return _translationUnit->tokenAt(_tokenIndex + i - 1); }
con's avatar
con committed
280
281
282
283
284
285
286
287
288
289

    inline int LA(int n = 1) const
    { return _translationUnit->tokenKind(_tokenIndex + n - 1); }

    inline int consumeToken()
    { return _tokenIndex++; }

    inline unsigned cursor() const
    { return _tokenIndex; }

290
    void rewind(unsigned cursor);
con's avatar
con committed
291

292
293
294
    struct TemplateArgumentListEntry {
        unsigned index;
        unsigned cursor;
Roberto Raggi's avatar
Roberto Raggi committed
295
        ExpressionListAST *ast;
296

Roberto Raggi's avatar
Roberto Raggi committed
297
        TemplateArgumentListEntry(unsigned index = 0, unsigned cursor = 0, ExpressionListAST *ast = 0)
298
299
300
301
            : index(index), cursor(cursor), ast(ast) {}
    };

    TemplateArgumentListEntry *templateArgumentListEntry(unsigned tokenIndex);
302
    void clearTemplateArgumentList() { _templateArgumentList.clear(); }
303

con's avatar
con committed
304
305
306
307
private:
    TranslationUnit *_translationUnit;
    Control *_control;
    MemoryPool *_pool;
308
    LanguageFeatures _languageFeatures;
con's avatar
con committed
309
310
311
    unsigned _tokenIndex;
    bool _templateArguments: 1;
    bool _inFunctionBody: 1;
Roberto Raggi's avatar
Roberto Raggi committed
312
    bool _inObjCImplementationContext: 1;
313
    bool _inExpressionStatement: 1;
314
    int _expressionDepth;
315
    int _statementDepth;
316

317
    MemoryPool _expressionStatementTempPool;
Roberto Raggi's avatar
Roberto Raggi committed
318
    std::map<unsigned, TemplateArgumentListEntry> _templateArgumentList;
319

320
321
322
    class Rewind;
    friend class Rewind;

con's avatar
con committed
323
324
325
326
327
private:
    Parser(const Parser& source);
    void operator =(const Parser& source);
};

328
} // namespace CPlusPlus
Roberto Raggi's avatar
Roberto Raggi committed
329

con's avatar
con committed
330
331

#endif // CPLUSPLUS_PARSER_H