blob: ae00ada3110d0c2d6b4d7062f175ce2ec52d144d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000236 case tok::kw_export:
237 case tok::kw_template:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000238 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000248 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000249 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000251 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000252
253 // This routine returns a DeclGroup, if the thing we parsed only contains a
254 // single decl, convert it now.
255 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000256}
257
258/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
259/// declaration-specifiers init-declarator-list[opt] ';'
260///[C90/C++]init-declarator-list ';' [TODO]
261/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000262///
263/// If RequireSemi is false, this does not check for a ';' at the end of the
264/// declaration.
265Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000267 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000275 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 }
279
280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattner2c41d482009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000285
Chris Lattner9802a0a2009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattnerf8016042009-03-29 17:27:48 +0000288 // If the client wants to check what comes after the declaration, just return
289 // immediately without checking anything!
290 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattner2c41d482009-03-29 17:18:04 +0000297 Diag(Tok, diag::err_expected_semi_declation);
298 // Skip to end of block or statement
299 SkipUntil(tok::r_brace, true, true);
300 if (Tok.is(tok::semi))
301 ConsumeToken();
302 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000305
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
307/// parsing 'declaration-specifiers declarator'. This method is split out this
308/// way to handle the ambiguity between top-level function-definitions and
309/// declarations.
310///
Chris Lattner4b009652007-07-25 00:24:17 +0000311/// init-declarator-list: [C99 6.7]
312/// init-declarator
313/// init-declarator-list ',' init-declarator
314/// init-declarator: [C99 6.7]
315/// declarator
316/// declarator '=' initializer
317/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
318/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000319/// [C++] declarator initializer[opt]
320///
321/// [C++] initializer:
322/// [C++] '=' initializer-clause
323/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000324/// [C++0x] '=' 'default' [TODO]
325/// [C++0x] '=' 'delete'
326///
327/// According to the standard grammar, =default and =delete are function
328/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000329///
Chris Lattnera17991f2009-03-29 16:50:03 +0000330Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000331ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000332 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
333 // that we parse together here.
334 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000335
336 // At this point, we know that it is not a function definition. Parse the
337 // rest of the init-declarator-list.
338 while (1) {
339 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000340 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000341 SourceLocation Loc;
342 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000343 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000344 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000345 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000346 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000347
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000348 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000349 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000350 }
Chris Lattner4b009652007-07-25 00:24:17 +0000351
352 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000353 if (Tok.is(tok::kw___attribute)) {
354 SourceLocation Loc;
355 AttributeList *AttrList = ParseAttributes(&Loc);
356 D.AddAttributes(AttrList, Loc);
357 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000358
359 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000360 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
361 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000362
Chris Lattner4b009652007-07-25 00:24:17 +0000363 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000364 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000365 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000366 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
367 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 } else {
370 OwningExprResult Init(ParseInitializer());
371 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000372 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000373 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000374 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000375 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000376 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000377 } else if (Tok.is(tok::l_paren)) {
378 // Parse C++ direct initializer: '(' expression-list ')'
379 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000380 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000381 CommaLocsTy CommaLocs;
382
383 bool InvalidExpr = false;
384 if (ParseExpressionList(Exprs, CommaLocs)) {
385 SkipUntil(tok::r_paren);
386 InvalidExpr = true;
387 }
388 // Match the ')'.
389 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
390
391 if (!InvalidExpr) {
392 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
393 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000394 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000395 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000396 &CommaLocs[0], RParenLoc);
397 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000398 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000399 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000400 }
401
Chris Lattner4b009652007-07-25 00:24:17 +0000402 // If we don't have a comma, it is either the end of the list (a ';') or an
403 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000404 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000405 break;
406
407 // Consume the comma.
408 ConsumeToken();
409
410 // Parse the next declarator.
411 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000412
413 // Accept attributes in an init-declarator. In the first declarator in a
414 // declaration, these would be part of the declspec. In subsequent
415 // declarators, they become part of the declarator itself, so that they
416 // don't apply to declarators after *this* one. Examples:
417 // short __attribute__((common)) var; -> declspec
418 // short var __attribute__((common)); -> declarator
419 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000420 if (Tok.is(tok::kw___attribute)) {
421 SourceLocation Loc;
422 AttributeList *AttrList = ParseAttributes(&Loc);
423 D.AddAttributes(AttrList, Loc);
424 }
Chris Lattner926cf542008-10-20 04:57:38 +0000425
Chris Lattner4b009652007-07-25 00:24:17 +0000426 ParseDeclarator(D);
427 }
428
Chris Lattner2c41d482009-03-29 17:18:04 +0000429 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
430 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000431}
432
433/// ParseSpecifierQualifierList
434/// specifier-qualifier-list:
435/// type-specifier specifier-qualifier-list[opt]
436/// type-qualifier specifier-qualifier-list[opt]
437/// [GNU] attributes specifier-qualifier-list[opt]
438///
439void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
440 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
441 /// parse declaration-specifiers and complain about extra stuff.
442 ParseDeclarationSpecifiers(DS);
443
444 // Validate declspec for type-name.
445 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000446 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000447 Diag(Tok, diag::err_typename_requires_specqual);
448
449 // Issue diagnostic and remove storage class if present.
450 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
451 if (DS.getStorageClassSpecLoc().isValid())
452 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
453 else
454 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
455 DS.ClearStorageClassSpecs();
456 }
457
458 // Issue diagnostic and remove function specfier if present.
459 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000460 if (DS.isInlineSpecified())
461 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
462 if (DS.isVirtualSpecified())
463 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
464 if (DS.isExplicitSpecified())
465 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000466 DS.ClearFunctionSpecs();
467 }
468}
469
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000470/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
471/// specified token is valid after the identifier in a declarator which
472/// immediately follows the declspec. For example, these things are valid:
473///
474/// int x [ 4]; // direct-declarator
475/// int x ( int y); // direct-declarator
476/// int(int x ) // direct-declarator
477/// int x ; // simple-declaration
478/// int x = 17; // init-declarator-list
479/// int x , y; // init-declarator-list
480/// int x __asm__ ("foo"); // init-declarator-list
481///
482/// This is not, because 'x' does not immediately follow the declspec (though
483/// ')' happens to be valid anyway).
484/// int (x)
485///
486static bool isValidAfterIdentifierInDeclarator(const Token &T) {
487 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
488 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
489 T.is(tok::kw_asm);
490
491}
492
Chris Lattner4b009652007-07-25 00:24:17 +0000493/// ParseDeclarationSpecifiers
494/// declaration-specifiers: [C99 6.7]
495/// storage-class-specifier declaration-specifiers[opt]
496/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000497/// [C99] function-specifier declaration-specifiers[opt]
498/// [GNU] attributes declaration-specifiers[opt]
499///
500/// storage-class-specifier: [C99 6.7.1]
501/// 'typedef'
502/// 'extern'
503/// 'static'
504/// 'auto'
505/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000506/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000507/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000508/// function-specifier: [C99 6.7.4]
509/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000510/// [C++] 'virtual'
511/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000512///
Douglas Gregor52473432008-12-24 02:52:09 +0000513void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000514 TemplateParameterLists *TemplateParams,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000515 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000516 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000517 while (1) {
518 int isInvalid = false;
519 const char *PrevSpec = 0;
520 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000521
Chris Lattner4b009652007-07-25 00:24:17 +0000522 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000523 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000524 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000525 // If this is not a declaration specifier token, we're done reading decl
526 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000527 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000528 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000529
530 case tok::coloncolon: // ::foo::bar
531 // Annotate C++ scope specifiers. If we get one, loop.
532 if (TryAnnotateCXXScopeToken())
533 continue;
534 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535
536 case tok::annot_cxxscope: {
537 if (DS.hasTypeSpecifier())
538 goto DoneWithDeclSpec;
539
540 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000541 Token Next = NextToken();
542 if (Next.is(tok::annot_template_id) &&
543 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000544 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000545 // We have a qualified template-id, e.g., N::A<int>
546 CXXScopeSpec SS;
547 ParseOptionalCXXScopeSpecifier(SS);
548 assert(Tok.is(tok::annot_template_id) &&
549 "ParseOptionalCXXScopeSpecifier not working");
550 AnnotateTemplateIdTokenAsType(&SS);
551 continue;
552 }
553
554 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000555 goto DoneWithDeclSpec;
556
557 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000558 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000559 SS.setRange(Tok.getAnnotationRange());
560
561 // If the next token is the name of the class type that the C++ scope
562 // denotes, followed by a '(', then this is a constructor declaration.
563 // We're done with the decl-specifiers.
564 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
565 CurScope, &SS) &&
566 GetLookAheadToken(2).is(tok::l_paren))
567 goto DoneWithDeclSpec;
568
Douglas Gregor1075a162009-02-04 17:00:24 +0000569 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
570 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000571
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000572 if (TypeRep == 0)
573 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000574
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000575 ConsumeToken(); // The C++ scope.
576
Douglas Gregora60c62e2009-02-09 15:09:02 +0000577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000578 TypeRep);
579 if (isInvalid)
580 break;
581
582 DS.SetRangeEnd(Tok.getLocation());
583 ConsumeToken(); // The typename.
584
585 continue;
586 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000587
588 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000589 if (Tok.getAnnotationValue())
590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
591 Tok.getAnnotationValue());
592 else
593 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000594 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
595 ConsumeToken(); // The typename
596
597 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
598 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
599 // Objective-C interface. If we don't have Objective-C or a '<', this is
600 // just a normal reference to a typedef name.
601 if (!Tok.is(tok::less) || !getLang().ObjC1)
602 continue;
603
604 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000605 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000606 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
607 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
608
609 DS.SetRangeEnd(EndProtoLoc);
610 continue;
611 }
612
Chris Lattnerfda18db2008-07-26 01:18:38 +0000613 // typedef-name
614 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000615 // In C++, check to see if this is a scope specifier like foo::bar::, if
616 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000617 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
618 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000619
Chris Lattnerfda18db2008-07-26 01:18:38 +0000620 // This identifier can only be a typedef name if we haven't already seen
621 // a type-specifier. Without this check we misparse:
622 // typedef int X; struct Y { short X; }; as 'short int'.
623 if (DS.hasTypeSpecifier())
624 goto DoneWithDeclSpec;
625
626 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000627 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
628 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000629
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000630 // If this is not a typedef name, don't parse it as part of the declspec,
631 // it must be an implicit int or an error.
632 if (TypeRep == 0) {
633 // If we see an identifier that is not a type name, we normally would
634 // parse it as the identifer being declared. However, when a typename
635 // is typo'd or the definition is not included, this will incorrectly
636 // parse the typename as the identifier name and fall over misparsing
637 // later parts of the diagnostic.
638 //
639 // As such, we try to do some look-ahead in cases where this would
640 // otherwise be an "implicit-int" case to see if this is invalid. For
641 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
642 // an identifier with implicit int, we'd get a parse error because the
643 // next token is obviously invalid for a type. Parse these as a case
644 // with an invalid type specifier.
645 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
646
647 // Since we know that this either implicit int (which is rare) or an
648 // error, we'd do lookahead to try to do better recovery.
649 if (isValidAfterIdentifierInDeclarator(NextToken())) {
650 // If this token is valid for implicit int, e.g. "static x = 4", then
651 // we just avoid eating the identifier, so it will be parsed as the
652 // identifier in the declarator.
653 goto DoneWithDeclSpec;
654 }
655
656 // Otherwise, if we don't consume this token, we are going to emit an
Chris Lattner197b4342009-04-12 21:49:30 +0000657 // error anyway. Try to recover from various common problems. Check
658 // to see if this was a reference to a tag name without a tag specified.
659 // This is a common problem in C (saying 'foo' insteat of 'struct foo').
660 const char *TagName = 0;
661 tok::TokenKind TagKind = tok::unknown;
662
663 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
664 default: break;
665 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
666 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
667 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
668 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
669 }
670 if (TagName) {
671 Diag(Loc, diag::err_use_of_tag_name_without_tag)
672 << Tok.getIdentifierInfo() << TagName
673 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
674
675 // Parse this as a tag as if the missing tag were present.
676 if (TagKind == tok::kw_enum)
677 ParseEnumSpecifier(Loc, DS, AS);
678 else
679 ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
680 continue;
681 }
682
683 // Since this is almost certainly an invalid type name, emit a
684 // diagnostic that says it, eat the token, and pretend we saw an 'int'.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000685 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo();
686 DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
687 DS.SetRangeEnd(Tok.getLocation());
688 ConsumeToken();
689
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000690 // TODO: Could inject an invalid typedef decl in an enclosing scope to
691 // avoid rippling error messages on subsequent uses of the same type,
692 // could be useful if #include was forgotten.
693
694 // FIXME: Mark DeclSpec as invalid.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000695 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000696 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000697
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000698 // C++: If the identifier is actually the name of the class type
699 // being defined and the next token is a '(', then this is a
700 // constructor declaration. We're done with the decl-specifiers
701 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000702 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000703 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
704 NextToken().getKind() == tok::l_paren)
705 goto DoneWithDeclSpec;
706
Douglas Gregora60c62e2009-02-09 15:09:02 +0000707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000708 TypeRep);
709 if (isInvalid)
710 break;
711
712 DS.SetRangeEnd(Tok.getLocation());
713 ConsumeToken(); // The identifier
714
715 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
716 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
717 // Objective-C interface. If we don't have Objective-C or a '<', this is
718 // just a normal reference to a typedef name.
719 if (!Tok.is(tok::less) || !getLang().ObjC1)
720 continue;
721
722 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000723 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000724 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000725 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000726
727 DS.SetRangeEnd(EndProtoLoc);
728
Steve Narofff7683302008-09-22 10:28:57 +0000729 // Need to support trailing type qualifiers (e.g. "id<p> const").
730 // If a type specifier follows, it will be diagnosed elsewhere.
731 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000732 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000733
734 // type-name
735 case tok::annot_template_id: {
736 TemplateIdAnnotation *TemplateId
737 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000738 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000739 // This template-id does not refer to a type name, so we're
740 // done with the type-specifiers.
741 goto DoneWithDeclSpec;
742 }
743
744 // Turn the template-id annotation token into a type annotation
745 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000746 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000747 continue;
748 }
749
Chris Lattner4b009652007-07-25 00:24:17 +0000750 // GNU attributes support.
751 case tok::kw___attribute:
752 DS.AddAttributes(ParseAttributes());
753 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000754
755 // Microsoft declspec support.
756 case tok::kw___declspec:
757 if (!PP.getLangOptions().Microsoft)
758 goto DoneWithDeclSpec;
759 FuzzyParseMicrosoftDeclSpec();
760 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000761
Steve Naroffedd04d52008-12-25 14:16:32 +0000762 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000763 case tok::kw___forceinline:
764 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000765 case tok::kw___cdecl:
766 case tok::kw___stdcall:
767 case tok::kw___fastcall:
768 if (!PP.getLangOptions().Microsoft)
769 goto DoneWithDeclSpec;
770 // Just ignore it.
771 break;
772
Chris Lattner4b009652007-07-25 00:24:17 +0000773 // storage-class-specifier
774 case tok::kw_typedef:
775 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
776 break;
777 case tok::kw_extern:
778 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000779 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000780 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
781 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000782 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000783 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
784 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000785 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000786 case tok::kw_static:
787 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000788 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000789 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
790 break;
791 case tok::kw_auto:
792 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
793 break;
794 case tok::kw_register:
795 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
796 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000797 case tok::kw_mutable:
798 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
799 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000800 case tok::kw___thread:
801 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
802 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000803
Chris Lattner4b009652007-07-25 00:24:17 +0000804 // function-specifier
805 case tok::kw_inline:
806 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
807 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000808 case tok::kw_virtual:
809 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
810 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000811 case tok::kw_explicit:
812 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
813 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000814
815 // type-specifier
816 case tok::kw_short:
817 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
818 break;
819 case tok::kw_long:
820 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
821 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
822 else
823 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
824 break;
825 case tok::kw_signed:
826 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
827 break;
828 case tok::kw_unsigned:
829 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
830 break;
831 case tok::kw__Complex:
832 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
833 break;
834 case tok::kw__Imaginary:
835 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
836 break;
837 case tok::kw_void:
838 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
839 break;
840 case tok::kw_char:
841 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
842 break;
843 case tok::kw_int:
844 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
845 break;
846 case tok::kw_float:
847 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
848 break;
849 case tok::kw_double:
850 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
851 break;
852 case tok::kw_wchar_t:
853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
854 break;
855 case tok::kw_bool:
856 case tok::kw__Bool:
857 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
858 break;
859 case tok::kw__Decimal32:
860 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
861 break;
862 case tok::kw__Decimal64:
863 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
864 break;
865 case tok::kw__Decimal128:
866 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
867 break;
868
869 // class-specifier:
870 case tok::kw_class:
871 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000872 case tok::kw_union: {
873 tok::TokenKind Kind = Tok.getKind();
874 ConsumeToken();
875 ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000876 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000877 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000878
879 // enum-specifier:
880 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000881 ConsumeToken();
882 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000883 continue;
884
885 // cv-qualifier:
886 case tok::kw_const:
887 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
888 break;
889 case tok::kw_volatile:
890 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
891 getLang())*2;
892 break;
893 case tok::kw_restrict:
894 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
895 getLang())*2;
896 break;
897
Douglas Gregord3022602009-03-27 23:10:48 +0000898 // C++ typename-specifier:
899 case tok::kw_typename:
900 if (TryAnnotateTypeOrScopeToken())
901 continue;
902 break;
903
Chris Lattnerc297b722009-01-21 19:48:37 +0000904 // GNU typeof support.
905 case tok::kw_typeof:
906 ParseTypeofSpecifier(DS);
907 continue;
908
Steve Naroff5f0466b2008-06-05 00:02:44 +0000909 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000910 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000911 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
912 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000913 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000914 goto DoneWithDeclSpec;
915
916 {
917 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000918 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000919 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000920 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000921 DS.SetRangeEnd(EndProtoLoc);
922
Chris Lattnerf006a222008-11-18 07:48:38 +0000923 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000924 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000925 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000926 // Need to support trailing type qualifiers (e.g. "id<p> const").
927 // If a type specifier follows, it will be diagnosed elsewhere.
928 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000929 }
Chris Lattner4b009652007-07-25 00:24:17 +0000930 }
931 // If the specifier combination wasn't legal, issue a diagnostic.
932 if (isInvalid) {
933 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000934 // Pick between error or extwarn.
935 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
936 : diag::ext_duplicate_declspec;
937 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000938 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000939 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000940 ConsumeToken();
941 }
942}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000943
Chris Lattnerd706dc82009-01-06 06:59:53 +0000944/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000945/// primarily follow the C++ grammar with additions for C99 and GNU,
946/// which together subsume the C grammar. Note that the C++
947/// type-specifier also includes the C type-qualifier (for const,
948/// volatile, and C99 restrict). Returns true if a type-specifier was
949/// found (and parsed), false otherwise.
950///
951/// type-specifier: [C++ 7.1.5]
952/// simple-type-specifier
953/// class-specifier
954/// enum-specifier
955/// elaborated-type-specifier [TODO]
956/// cv-qualifier
957///
958/// cv-qualifier: [C++ 7.1.5.1]
959/// 'const'
960/// 'volatile'
961/// [C99] 'restrict'
962///
963/// simple-type-specifier: [ C++ 7.1.5.2]
964/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
965/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
966/// 'char'
967/// 'wchar_t'
968/// 'bool'
969/// 'short'
970/// 'int'
971/// 'long'
972/// 'signed'
973/// 'unsigned'
974/// 'float'
975/// 'double'
976/// 'void'
977/// [C99] '_Bool'
978/// [C99] '_Complex'
979/// [C99] '_Imaginary' // Removed in TC2?
980/// [GNU] '_Decimal32'
981/// [GNU] '_Decimal64'
982/// [GNU] '_Decimal128'
983/// [GNU] typeof-specifier
984/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
985/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000986bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
987 const char *&PrevSpec,
988 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000989 SourceLocation Loc = Tok.getLocation();
990
991 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000992 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000993 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000994 // Annotate typenames and C++ scope specifiers. If we get one, just
995 // recurse to handle whatever we get.
996 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000997 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000998 // Otherwise, not a type specifier.
999 return false;
1000 case tok::coloncolon: // ::foo::bar
1001 if (NextToken().is(tok::kw_new) || // ::new
1002 NextToken().is(tok::kw_delete)) // ::delete
1003 return false;
1004
1005 // Annotate typenames and C++ scope specifiers. If we get one, just
1006 // recurse to handle whatever we get.
1007 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001008 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001009 // Otherwise, not a type specifier.
1010 return false;
1011
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001012 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001013 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001014 if (Tok.getAnnotationValue())
1015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1016 Tok.getAnnotationValue());
1017 else
1018 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001019 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1020 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001021
1022 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1023 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1024 // Objective-C interface. If we don't have Objective-C or a '<', this is
1025 // just a normal reference to a typedef name.
1026 if (!Tok.is(tok::less) || !getLang().ObjC1)
1027 return true;
1028
1029 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001030 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001031 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1032 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1033
1034 DS.SetRangeEnd(EndProtoLoc);
1035 return true;
1036 }
1037
1038 case tok::kw_short:
1039 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1040 break;
1041 case tok::kw_long:
1042 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1043 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1044 else
1045 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1046 break;
1047 case tok::kw_signed:
1048 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1049 break;
1050 case tok::kw_unsigned:
1051 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1052 break;
1053 case tok::kw__Complex:
1054 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1055 break;
1056 case tok::kw__Imaginary:
1057 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1058 break;
1059 case tok::kw_void:
1060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1061 break;
1062 case tok::kw_char:
1063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1064 break;
1065 case tok::kw_int:
1066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1067 break;
1068 case tok::kw_float:
1069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1070 break;
1071 case tok::kw_double:
1072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1073 break;
1074 case tok::kw_wchar_t:
1075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1076 break;
1077 case tok::kw_bool:
1078 case tok::kw__Bool:
1079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1080 break;
1081 case tok::kw__Decimal32:
1082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1083 break;
1084 case tok::kw__Decimal64:
1085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1086 break;
1087 case tok::kw__Decimal128:
1088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1089 break;
1090
1091 // class-specifier:
1092 case tok::kw_class:
1093 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001094 case tok::kw_union: {
1095 tok::TokenKind Kind = Tok.getKind();
1096 ConsumeToken();
1097 ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001098 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001099 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001100
1101 // enum-specifier:
1102 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001103 ConsumeToken();
1104 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001105 return true;
1106
1107 // cv-qualifier:
1108 case tok::kw_const:
1109 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1110 getLang())*2;
1111 break;
1112 case tok::kw_volatile:
1113 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1114 getLang())*2;
1115 break;
1116 case tok::kw_restrict:
1117 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1118 getLang())*2;
1119 break;
1120
1121 // GNU typeof support.
1122 case tok::kw_typeof:
1123 ParseTypeofSpecifier(DS);
1124 return true;
1125
Steve Naroffedd04d52008-12-25 14:16:32 +00001126 case tok::kw___cdecl:
1127 case tok::kw___stdcall:
1128 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001129 if (!PP.getLangOptions().Microsoft) return false;
1130 ConsumeToken();
1131 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001132
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001133 default:
1134 // Not a type-specifier; do nothing.
1135 return false;
1136 }
1137
1138 // If the specifier combination wasn't legal, issue a diagnostic.
1139 if (isInvalid) {
1140 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001141 // Pick between error or extwarn.
1142 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1143 : diag::ext_duplicate_declspec;
1144 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001145 }
1146 DS.SetRangeEnd(Tok.getLocation());
1147 ConsumeToken(); // whatever we parsed above.
1148 return true;
1149}
Chris Lattner4b009652007-07-25 00:24:17 +00001150
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001151/// ParseStructDeclaration - Parse a struct declaration without the terminating
1152/// semicolon.
1153///
Chris Lattner4b009652007-07-25 00:24:17 +00001154/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001155/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001156/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001157/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001158/// struct-declarator-list:
1159/// struct-declarator
1160/// struct-declarator-list ',' struct-declarator
1161/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1162/// struct-declarator:
1163/// declarator
1164/// [GNU] declarator attributes[opt]
1165/// declarator[opt] ':' constant-expression
1166/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1167///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001168void Parser::
1169ParseStructDeclaration(DeclSpec &DS,
1170 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001171 if (Tok.is(tok::kw___extension__)) {
1172 // __extension__ silences extension warnings in the subexpression.
1173 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001174 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001175 return ParseStructDeclaration(DS, Fields);
1176 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001177
1178 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001179 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001180 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001181
Douglas Gregorb748fc52009-01-12 22:49:06 +00001182 // If there are no declarators, this is a free-standing declaration
1183 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001184 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001185 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001186 return;
1187 }
1188
1189 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001190 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001191 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001192 FieldDeclarator &DeclaratorInfo = Fields.back();
1193
Steve Naroffa9adf112007-08-20 22:28:22 +00001194 /// struct-declarator: declarator
1195 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001196 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001197 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001198
Chris Lattner34a01ad2007-10-09 17:33:22 +00001199 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001200 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001201 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001202 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001203 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001204 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001205 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001206 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001207
Steve Naroffa9adf112007-08-20 22:28:22 +00001208 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001209 if (Tok.is(tok::kw___attribute)) {
1210 SourceLocation Loc;
1211 AttributeList *AttrList = ParseAttributes(&Loc);
1212 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1213 }
1214
Steve Naroffa9adf112007-08-20 22:28:22 +00001215 // If we don't have a comma, it is either the end of the list (a ';')
1216 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001217 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001218 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001219
Steve Naroffa9adf112007-08-20 22:28:22 +00001220 // Consume the comma.
1221 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001222
Steve Naroffa9adf112007-08-20 22:28:22 +00001223 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001224 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001225
Steve Naroffa9adf112007-08-20 22:28:22 +00001226 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001227 if (Tok.is(tok::kw___attribute)) {
1228 SourceLocation Loc;
1229 AttributeList *AttrList = ParseAttributes(&Loc);
1230 Fields.back().D.AddAttributes(AttrList, Loc);
1231 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001232 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001233}
1234
1235/// ParseStructUnionBody
1236/// struct-contents:
1237/// struct-declaration-list
1238/// [EXT] empty
1239/// [GNU] "struct-declaration-list" without terminatoring ';'
1240/// struct-declaration-list:
1241/// struct-declaration
1242/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001243/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001244///
Chris Lattner4b009652007-07-25 00:24:17 +00001245void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001246 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001247 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1248 PP.getSourceManager(),
1249 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001250
Chris Lattner4b009652007-07-25 00:24:17 +00001251 SourceLocation LBraceLoc = ConsumeBrace();
1252
Douglas Gregorcab994d2009-01-09 22:42:13 +00001253 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001254 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1255
Chris Lattner4b009652007-07-25 00:24:17 +00001256 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1257 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001258 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001259 Diag(Tok, diag::ext_empty_struct_union_enum)
1260 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001261
Chris Lattner5261d0c2009-03-28 19:18:32 +00001262 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001263 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1264
Chris Lattner4b009652007-07-25 00:24:17 +00001265 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001266 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001267 // Each iteration of this loop reads one struct-declaration.
1268
1269 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001270 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001271 Diag(Tok, diag::ext_extra_struct_semi)
1272 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001273 ConsumeToken();
1274 continue;
1275 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001276
1277 // Parse all the comma separated declarators.
1278 DeclSpec DS;
1279 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001280 if (!Tok.is(tok::at)) {
1281 ParseStructDeclaration(DS, FieldDeclarators);
1282
1283 // Convert them all to fields.
1284 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1285 FieldDeclarator &FD = FieldDeclarators[i];
1286 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001287 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1288 DS.getSourceRange().getBegin(),
1289 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001290 FieldDecls.push_back(Field);
1291 }
1292 } else { // Handle @defs
1293 ConsumeToken();
1294 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1295 Diag(Tok, diag::err_unexpected_at);
1296 SkipUntil(tok::semi, true, true);
1297 continue;
1298 }
1299 ConsumeToken();
1300 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1301 if (!Tok.is(tok::identifier)) {
1302 Diag(Tok, diag::err_expected_ident);
1303 SkipUntil(tok::semi, true, true);
1304 continue;
1305 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001306 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001307 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1308 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001309 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1310 ConsumeToken();
1311 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1312 }
Chris Lattner4b009652007-07-25 00:24:17 +00001313
Chris Lattner34a01ad2007-10-09 17:33:22 +00001314 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001315 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001316 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001317 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001318 break;
1319 } else {
1320 Diag(Tok, diag::err_expected_semi_decl_list);
1321 // Skip to end of block or statement
1322 SkipUntil(tok::r_brace, true, true);
1323 }
1324 }
1325
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001326 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001327
Chris Lattner4b009652007-07-25 00:24:17 +00001328 AttributeList *AttrList = 0;
1329 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001330 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001331 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001332
1333 Actions.ActOnFields(CurScope,
1334 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1335 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001336 AttrList);
1337 StructScope.Exit();
1338 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001339}
1340
1341
1342/// ParseEnumSpecifier
1343/// enum-specifier: [C99 6.7.2.2]
1344/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001345///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001346/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1347/// '}' attributes[opt]
1348/// 'enum' identifier
1349/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001350///
1351/// [C++] elaborated-type-specifier:
1352/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1353///
Chris Lattner197b4342009-04-12 21:49:30 +00001354void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1355 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001356 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001357
1358 AttributeList *Attr = 0;
1359 // If attributes exist after tag, parse them.
1360 if (Tok.is(tok::kw___attribute))
1361 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001362
1363 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001364 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001365 if (Tok.isNot(tok::identifier)) {
1366 Diag(Tok, diag::err_expected_ident);
1367 if (Tok.isNot(tok::l_brace)) {
1368 // Has no name and is not a definition.
1369 // Skip the rest of this declarator, up until the comma or semicolon.
1370 SkipUntil(tok::comma, true);
1371 return;
1372 }
1373 }
1374 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001375
1376 // Must have either 'enum name' or 'enum {...}'.
1377 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1378 Diag(Tok, diag::err_expected_ident_lbrace);
1379
1380 // Skip the rest of this declarator, up until the comma or semicolon.
1381 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001382 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001383 }
1384
1385 // If an identifier is present, consume and remember it.
1386 IdentifierInfo *Name = 0;
1387 SourceLocation NameLoc;
1388 if (Tok.is(tok::identifier)) {
1389 Name = Tok.getIdentifierInfo();
1390 NameLoc = ConsumeToken();
1391 }
1392
1393 // There are three options here. If we have 'enum foo;', then this is a
1394 // forward declaration. If we have 'enum foo {...' then this is a
1395 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1396 //
1397 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1398 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1399 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1400 //
1401 Action::TagKind TK;
1402 if (Tok.is(tok::l_brace))
1403 TK = Action::TK_Definition;
1404 else if (Tok.is(tok::semi))
1405 TK = Action::TK_Declaration;
1406 else
1407 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001408 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1409 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001410
Chris Lattner34a01ad2007-10-09 17:33:22 +00001411 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001412 ParseEnumBody(StartLoc, TagDecl);
1413
1414 // TODO: semantic analysis on the declspec for enums.
1415 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001416 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1417 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001418 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001419}
1420
1421/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1422/// enumerator-list:
1423/// enumerator
1424/// enumerator-list ',' enumerator
1425/// enumerator:
1426/// enumeration-constant
1427/// enumeration-constant '=' constant-expression
1428/// enumeration-constant:
1429/// identifier
1430///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001431void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001432 // Enter the scope of the enum body and start the definition.
1433 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001434 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001435
Chris Lattner4b009652007-07-25 00:24:17 +00001436 SourceLocation LBraceLoc = ConsumeBrace();
1437
Chris Lattnerc9a92452007-08-27 17:24:30 +00001438 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001439 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001440 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001441
Chris Lattner5261d0c2009-03-28 19:18:32 +00001442 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001443
Chris Lattner5261d0c2009-03-28 19:18:32 +00001444 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001445
1446 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001447 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001448 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1449 SourceLocation IdentLoc = ConsumeToken();
1450
1451 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001452 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001453 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001454 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001455 AssignedVal = ParseConstantExpression();
1456 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001457 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001458 }
1459
1460 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001461 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1462 LastEnumConstDecl,
1463 IdentLoc, Ident,
1464 EqualLoc,
1465 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001466 EnumConstantDecls.push_back(EnumConstDecl);
1467 LastEnumConstDecl = EnumConstDecl;
1468
Chris Lattner34a01ad2007-10-09 17:33:22 +00001469 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001470 break;
1471 SourceLocation CommaLoc = ConsumeToken();
1472
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001473 if (Tok.isNot(tok::identifier) &&
1474 !(getLang().C99 || getLang().CPlusPlus0x))
1475 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1476 << getLang().CPlusPlus
1477 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001478 }
1479
1480 // Eat the }.
1481 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1482
Steve Naroff0acc9c92007-09-15 18:49:24 +00001483 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001484 EnumConstantDecls.size());
1485
Chris Lattner5261d0c2009-03-28 19:18:32 +00001486 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001487 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001488 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001489 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001490
1491 EnumScope.Exit();
1492 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001493}
1494
1495/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001496/// start of a type-qualifier-list.
1497bool Parser::isTypeQualifier() const {
1498 switch (Tok.getKind()) {
1499 default: return false;
1500 // type-qualifier
1501 case tok::kw_const:
1502 case tok::kw_volatile:
1503 case tok::kw_restrict:
1504 return true;
1505 }
1506}
1507
1508/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001509/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001510bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001511 switch (Tok.getKind()) {
1512 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001513
1514 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001515 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001516 // Annotate typenames and C++ scope specifiers. If we get one, just
1517 // recurse to handle whatever we get.
1518 if (TryAnnotateTypeOrScopeToken())
1519 return isTypeSpecifierQualifier();
1520 // Otherwise, not a type specifier.
1521 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001522
Chris Lattnerb75fde62009-01-04 23:41:41 +00001523 case tok::coloncolon: // ::foo::bar
1524 if (NextToken().is(tok::kw_new) || // ::new
1525 NextToken().is(tok::kw_delete)) // ::delete
1526 return false;
1527
1528 // Annotate typenames and C++ scope specifiers. If we get one, just
1529 // recurse to handle whatever we get.
1530 if (TryAnnotateTypeOrScopeToken())
1531 return isTypeSpecifierQualifier();
1532 // Otherwise, not a type specifier.
1533 return false;
1534
Chris Lattner4b009652007-07-25 00:24:17 +00001535 // GNU attributes support.
1536 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001537 // GNU typeof support.
1538 case tok::kw_typeof:
1539
Chris Lattner4b009652007-07-25 00:24:17 +00001540 // type-specifiers
1541 case tok::kw_short:
1542 case tok::kw_long:
1543 case tok::kw_signed:
1544 case tok::kw_unsigned:
1545 case tok::kw__Complex:
1546 case tok::kw__Imaginary:
1547 case tok::kw_void:
1548 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001549 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001550 case tok::kw_int:
1551 case tok::kw_float:
1552 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001553 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001554 case tok::kw__Bool:
1555 case tok::kw__Decimal32:
1556 case tok::kw__Decimal64:
1557 case tok::kw__Decimal128:
1558
Chris Lattner2e78db32008-04-13 18:59:07 +00001559 // struct-or-union-specifier (C99) or class-specifier (C++)
1560 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001561 case tok::kw_struct:
1562 case tok::kw_union:
1563 // enum-specifier
1564 case tok::kw_enum:
1565
1566 // type-qualifier
1567 case tok::kw_const:
1568 case tok::kw_volatile:
1569 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001570
1571 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001572 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001573 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001574
1575 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1576 case tok::less:
1577 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001578
1579 case tok::kw___cdecl:
1580 case tok::kw___stdcall:
1581 case tok::kw___fastcall:
1582 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001583 }
1584}
1585
1586/// isDeclarationSpecifier() - Return true if the current token is part of a
1587/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001588bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001589 switch (Tok.getKind()) {
1590 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001591
1592 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001593 // Unfortunate hack to support "Class.factoryMethod" notation.
1594 if (getLang().ObjC1 && NextToken().is(tok::period))
1595 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001596 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001597
Douglas Gregord3022602009-03-27 23:10:48 +00001598 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001599 // Annotate typenames and C++ scope specifiers. If we get one, just
1600 // recurse to handle whatever we get.
1601 if (TryAnnotateTypeOrScopeToken())
1602 return isDeclarationSpecifier();
1603 // Otherwise, not a declaration specifier.
1604 return false;
1605 case tok::coloncolon: // ::foo::bar
1606 if (NextToken().is(tok::kw_new) || // ::new
1607 NextToken().is(tok::kw_delete)) // ::delete
1608 return false;
1609
1610 // Annotate typenames and C++ scope specifiers. If we get one, just
1611 // recurse to handle whatever we get.
1612 if (TryAnnotateTypeOrScopeToken())
1613 return isDeclarationSpecifier();
1614 // Otherwise, not a declaration specifier.
1615 return false;
1616
Chris Lattner4b009652007-07-25 00:24:17 +00001617 // storage-class-specifier
1618 case tok::kw_typedef:
1619 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001620 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001621 case tok::kw_static:
1622 case tok::kw_auto:
1623 case tok::kw_register:
1624 case tok::kw___thread:
1625
1626 // type-specifiers
1627 case tok::kw_short:
1628 case tok::kw_long:
1629 case tok::kw_signed:
1630 case tok::kw_unsigned:
1631 case tok::kw__Complex:
1632 case tok::kw__Imaginary:
1633 case tok::kw_void:
1634 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001635 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001636 case tok::kw_int:
1637 case tok::kw_float:
1638 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001639 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001640 case tok::kw__Bool:
1641 case tok::kw__Decimal32:
1642 case tok::kw__Decimal64:
1643 case tok::kw__Decimal128:
1644
Chris Lattner2e78db32008-04-13 18:59:07 +00001645 // struct-or-union-specifier (C99) or class-specifier (C++)
1646 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001647 case tok::kw_struct:
1648 case tok::kw_union:
1649 // enum-specifier
1650 case tok::kw_enum:
1651
1652 // type-qualifier
1653 case tok::kw_const:
1654 case tok::kw_volatile:
1655 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001656
Chris Lattner4b009652007-07-25 00:24:17 +00001657 // function-specifier
1658 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001659 case tok::kw_virtual:
1660 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001661
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001662 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001663 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001664
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001665 // GNU typeof support.
1666 case tok::kw_typeof:
1667
1668 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001669 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001670 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001671
1672 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1673 case tok::less:
1674 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001675
Steve Naroffab1a3632009-01-06 19:34:12 +00001676 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001677 case tok::kw___cdecl:
1678 case tok::kw___stdcall:
1679 case tok::kw___fastcall:
1680 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001681 }
1682}
1683
1684
1685/// ParseTypeQualifierListOpt
1686/// type-qualifier-list: [C99 6.7.5]
1687/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001688/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001689/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001690/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001691///
Chris Lattner460696f2008-12-18 07:02:59 +00001692void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001693 while (1) {
1694 int isInvalid = false;
1695 const char *PrevSpec = 0;
1696 SourceLocation Loc = Tok.getLocation();
1697
1698 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001699 case tok::kw_const:
1700 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1701 getLang())*2;
1702 break;
1703 case tok::kw_volatile:
1704 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1705 getLang())*2;
1706 break;
1707 case tok::kw_restrict:
1708 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1709 getLang())*2;
1710 break;
Steve Naroffad620402008-12-25 14:41:26 +00001711 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001712 case tok::kw___cdecl:
1713 case tok::kw___stdcall:
1714 case tok::kw___fastcall:
1715 if (!PP.getLangOptions().Microsoft)
1716 goto DoneWithTypeQuals;
1717 // Just ignore it.
1718 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001719 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001720 if (AttributesAllowed) {
1721 DS.AddAttributes(ParseAttributes());
1722 continue; // do *not* consume the next token!
1723 }
1724 // otherwise, FALL THROUGH!
1725 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001726 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001727 // If this is not a type-qualifier token, we're done reading type
1728 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001729 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001730 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001731 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001732
Chris Lattner4b009652007-07-25 00:24:17 +00001733 // If the specifier combination wasn't legal, issue a diagnostic.
1734 if (isInvalid) {
1735 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001736 // Pick between error or extwarn.
1737 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1738 : diag::ext_duplicate_declspec;
1739 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001740 }
1741 ConsumeToken();
1742 }
1743}
1744
1745
1746/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1747///
1748void Parser::ParseDeclarator(Declarator &D) {
1749 /// This implements the 'declarator' production in the C grammar, then checks
1750 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001751 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001752}
1753
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001754/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1755/// is parsed by the function passed to it. Pass null, and the direct-declarator
1756/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001757/// ptr-operator production.
1758///
Sebastian Redl75555032009-01-24 21:16:55 +00001759/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1760/// [C] pointer[opt] direct-declarator
1761/// [C++] direct-declarator
1762/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001763///
1764/// pointer: [C99 6.7.5]
1765/// '*' type-qualifier-list[opt]
1766/// '*' type-qualifier-list[opt] pointer
1767///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001768/// ptr-operator:
1769/// '*' cv-qualifier-seq[opt]
1770/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001771/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001772/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001773/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001774/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001775void Parser::ParseDeclaratorInternal(Declarator &D,
1776 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001777
Sebastian Redl75555032009-01-24 21:16:55 +00001778 // C++ member pointers start with a '::' or a nested-name.
1779 // Member pointers get special handling, since there's no place for the
1780 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001781 if (getLang().CPlusPlus &&
1782 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1783 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001784 CXXScopeSpec SS;
1785 if (ParseOptionalCXXScopeSpecifier(SS)) {
1786 if(Tok.isNot(tok::star)) {
1787 // The scope spec really belongs to the direct-declarator.
1788 D.getCXXScopeSpec() = SS;
1789 if (DirectDeclParser)
1790 (this->*DirectDeclParser)(D);
1791 return;
1792 }
1793
1794 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001795 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001796 DeclSpec DS;
1797 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001798 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001799
1800 // Recurse to parse whatever is left.
1801 ParseDeclaratorInternal(D, DirectDeclParser);
1802
1803 // Sema will have to catch (syntactically invalid) pointers into global
1804 // scope. It has to catch pointers into namespace scope anyway.
1805 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001806 Loc, DS.TakeAttributes()),
1807 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001808 return;
1809 }
1810 }
1811
1812 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001813 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001814 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001815 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001816 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001817 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001818 if (DirectDeclParser)
1819 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001820 return;
1821 }
Sebastian Redl75555032009-01-24 21:16:55 +00001822
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001823 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1824 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001825 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001826 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001827
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001828 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001829 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001830 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001831
Chris Lattner4b009652007-07-25 00:24:17 +00001832 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001833 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001834
Chris Lattner4b009652007-07-25 00:24:17 +00001835 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001836 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001837 if (Kind == tok::star)
1838 // Remember that we parsed a pointer type, and remember the type-quals.
1839 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001840 DS.TakeAttributes()),
1841 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001842 else
1843 // Remember that we parsed a Block type, and remember the type-quals.
1844 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001845 Loc),
1846 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001847 } else {
1848 // Is a reference
1849 DeclSpec DS;
1850
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001851 // Complain about rvalue references in C++03, but then go on and build
1852 // the declarator.
1853 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1854 Diag(Loc, diag::err_rvalue_reference);
1855
Chris Lattner4b009652007-07-25 00:24:17 +00001856 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1857 // cv-qualifiers are introduced through the use of a typedef or of a
1858 // template type argument, in which case the cv-qualifiers are ignored.
1859 //
1860 // [GNU] Retricted references are allowed.
1861 // [GNU] Attributes on references are allowed.
1862 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001863 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001864
1865 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1866 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1867 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001868 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001869 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1870 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001871 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001872 }
1873
1874 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001875 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001876
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001877 if (D.getNumTypeObjects() > 0) {
1878 // C++ [dcl.ref]p4: There shall be no references to references.
1879 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1880 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001881 if (const IdentifierInfo *II = D.getIdentifier())
1882 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1883 << II;
1884 else
1885 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1886 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001887
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001888 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001889 // can go ahead and build the (technically ill-formed)
1890 // declarator: reference collapsing will take care of it.
1891 }
1892 }
1893
Chris Lattner4b009652007-07-25 00:24:17 +00001894 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001895 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001896 DS.TakeAttributes(),
1897 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001898 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001899 }
1900}
1901
1902/// ParseDirectDeclarator
1903/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001904/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001905/// '(' declarator ')'
1906/// [GNU] '(' attributes declarator ')'
1907/// [C90] direct-declarator '[' constant-expression[opt] ']'
1908/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1909/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1910/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1911/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1912/// direct-declarator '(' parameter-type-list ')'
1913/// direct-declarator '(' identifier-list[opt] ')'
1914/// [GNU] direct-declarator '(' parameter-forward-declarations
1915/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001916/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1917/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001918/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001919///
1920/// declarator-id: [C++ 8]
1921/// id-expression
1922/// '::'[opt] nested-name-specifier[opt] type-name
1923///
1924/// id-expression: [C++ 5.1]
1925/// unqualified-id
1926/// qualified-id [TODO]
1927///
1928/// unqualified-id: [C++ 5.1]
1929/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001930/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001931/// conversion-function-id [TODO]
1932/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001933/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001934///
Chris Lattner4b009652007-07-25 00:24:17 +00001935void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001936 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001937
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001938 if (getLang().CPlusPlus) {
1939 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001940 // ParseDeclaratorInternal might already have parsed the scope.
1941 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1942 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001943 if (afterCXXScope) {
1944 // Change the declaration context for name lookup, until this function
1945 // is exited (and the declarator has been parsed).
1946 DeclScopeObj.EnterDeclaratorScope();
1947 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001948
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001949 if (Tok.is(tok::identifier)) {
1950 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001951
Douglas Gregor2fa10442008-12-18 19:37:40 +00001952 // If this identifier is the name of the current class, it's a
1953 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001954 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001955 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001956 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001957 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001958 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001959 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001960 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1961 ConsumeToken();
1962 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001963 } else if (Tok.is(tok::annot_template_id)) {
1964 TemplateIdAnnotation *TemplateId
1965 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1966
1967 // FIXME: Could this template-id name a constructor?
1968
1969 // FIXME: This is an egregious hack, where we silently ignore
1970 // the specialization (which should be a function template
1971 // specialization name) and use the name instead. This hack
1972 // will go away when we have support for function
1973 // specializations.
1974 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1975 TemplateId->Destroy();
1976 ConsumeToken();
1977 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001978 } else if (Tok.is(tok::kw_operator)) {
1979 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001980 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001981
Douglas Gregor853dd392008-12-26 15:00:45 +00001982 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001983 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1984 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001985 } else {
1986 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001987 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1988 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1989 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001990 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001991 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001992 }
1993 goto PastIdentifier;
1994 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001995 // This should be a C++ destructor.
1996 SourceLocation TildeLoc = ConsumeToken();
1997 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001998 // FIXME: Inaccurate.
1999 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002000 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002001 TypeResult Type = ParseClassName(EndLoc);
2002 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002003 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002004 else
2005 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002006 } else {
2007 Diag(Tok, diag::err_expected_class_name);
2008 D.SetIdentifier(0, TildeLoc);
2009 }
2010 goto PastIdentifier;
2011 }
2012
2013 // If we reached this point, token is not identifier and not '~'.
2014
2015 if (afterCXXScope) {
2016 Diag(Tok, diag::err_expected_unqualified_id);
2017 D.SetIdentifier(0, Tok.getLocation());
2018 D.setInvalidType(true);
2019 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002020 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002021 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002022 }
2023
2024 // If we reached this point, we are either in C/ObjC or the token didn't
2025 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002026 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2027 assert(!getLang().CPlusPlus &&
2028 "There's a C++-specific check for tok::identifier above");
2029 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2030 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2031 ConsumeToken();
2032 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002033 // direct-declarator: '(' declarator ')'
2034 // direct-declarator: '(' attributes declarator ')'
2035 // Example: 'char (*X)' or 'int (*XX)(void)'
2036 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002037 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002038 // This could be something simple like "int" (in which case the declarator
2039 // portion is empty), if an abstract-declarator is allowed.
2040 D.SetIdentifier(0, Tok.getLocation());
2041 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002042 if (D.getContext() == Declarator::MemberContext)
2043 Diag(Tok, diag::err_expected_member_name_or_semi)
2044 << D.getDeclSpec().getSourceRange();
2045 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002046 Diag(Tok, diag::err_expected_unqualified_id);
2047 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002048 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002049 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002050 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002051 }
2052
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002053 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002054 assert(D.isPastIdentifier() &&
2055 "Haven't past the location of the identifier yet?");
2056
2057 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002058 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002059 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2060 // In such a case, check if we actually have a function declarator; if it
2061 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002062 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2063 // When not in file scope, warn for ambiguous function declarators, just
2064 // in case the author intended it as a variable definition.
2065 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2066 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2067 break;
2068 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002069 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002070 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002071 ParseBracketDeclarator(D);
2072 } else {
2073 break;
2074 }
2075 }
2076}
2077
Chris Lattnera0d056d2008-04-06 05:45:57 +00002078/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2079/// only called before the identifier, so these are most likely just grouping
2080/// parens for precedence. If we find that these are actually function
2081/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2082///
2083/// direct-declarator:
2084/// '(' declarator ')'
2085/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002086/// direct-declarator '(' parameter-type-list ')'
2087/// direct-declarator '(' identifier-list[opt] ')'
2088/// [GNU] direct-declarator '(' parameter-forward-declarations
2089/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002090///
2091void Parser::ParseParenDeclarator(Declarator &D) {
2092 SourceLocation StartLoc = ConsumeParen();
2093 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2094
Chris Lattner1f185292008-10-20 02:05:46 +00002095 // Eat any attributes before we look at whether this is a grouping or function
2096 // declarator paren. If this is a grouping paren, the attribute applies to
2097 // the type being built up, for example:
2098 // int (__attribute__(()) *x)(long y)
2099 // If this ends up not being a grouping paren, the attribute applies to the
2100 // first argument, for example:
2101 // int (__attribute__(()) int x)
2102 // In either case, we need to eat any attributes to be able to determine what
2103 // sort of paren this is.
2104 //
2105 AttributeList *AttrList = 0;
2106 bool RequiresArg = false;
2107 if (Tok.is(tok::kw___attribute)) {
2108 AttrList = ParseAttributes();
2109
2110 // We require that the argument list (if this is a non-grouping paren) be
2111 // present even if the attribute list was empty.
2112 RequiresArg = true;
2113 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002114 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002115 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2116 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002117 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002118
Chris Lattnera0d056d2008-04-06 05:45:57 +00002119 // If we haven't past the identifier yet (or where the identifier would be
2120 // stored, if this is an abstract declarator), then this is probably just
2121 // grouping parens. However, if this could be an abstract-declarator, then
2122 // this could also be the start of function arguments (consider 'void()').
2123 bool isGrouping;
2124
2125 if (!D.mayOmitIdentifier()) {
2126 // If this can't be an abstract-declarator, this *must* be a grouping
2127 // paren, because we haven't seen the identifier yet.
2128 isGrouping = true;
2129 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002130 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002131 isDeclarationSpecifier()) { // 'int(int)' is a function.
2132 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2133 // considered to be a type, not a K&R identifier-list.
2134 isGrouping = false;
2135 } else {
2136 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2137 isGrouping = true;
2138 }
2139
2140 // If this is a grouping paren, handle:
2141 // direct-declarator: '(' declarator ')'
2142 // direct-declarator: '(' attributes declarator ')'
2143 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002144 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002145 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002146 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002147 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002148
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002149 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002150 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002151 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002152
2153 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002154 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002155 return;
2156 }
2157
2158 // Okay, if this wasn't a grouping paren, it must be the start of a function
2159 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002160 // identifier (and remember where it would have been), then call into
2161 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002162 D.SetIdentifier(0, Tok.getLocation());
2163
Chris Lattner1f185292008-10-20 02:05:46 +00002164 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002165}
2166
2167/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2168/// declarator D up to a paren, which indicates that we are parsing function
2169/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002170///
Chris Lattner1f185292008-10-20 02:05:46 +00002171/// If AttrList is non-null, then the caller parsed those arguments immediately
2172/// after the open paren - they should be considered to be the first argument of
2173/// a parameter. If RequiresArg is true, then the first argument of the
2174/// function is required to be present and required to not be an identifier
2175/// list.
2176///
Chris Lattner4b009652007-07-25 00:24:17 +00002177/// This method also handles this portion of the grammar:
2178/// parameter-type-list: [C99 6.7.5]
2179/// parameter-list
2180/// parameter-list ',' '...'
2181///
2182/// parameter-list: [C99 6.7.5]
2183/// parameter-declaration
2184/// parameter-list ',' parameter-declaration
2185///
2186/// parameter-declaration: [C99 6.7.5]
2187/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002188/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002189/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002190/// declaration-specifiers abstract-declarator[opt]
2191/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002192/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002193/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2194///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002195/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002196/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002197///
Chris Lattner1f185292008-10-20 02:05:46 +00002198void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2199 AttributeList *AttrList,
2200 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002201 // lparen is already consumed!
2202 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002203
Chris Lattner1f185292008-10-20 02:05:46 +00002204 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002205 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002206 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002207 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002208 delete AttrList;
2209 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002210
Sebastian Redl0c986032009-02-09 18:23:29 +00002211 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002212
2213 // cv-qualifier-seq[opt].
2214 DeclSpec DS;
2215 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002216 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002217 if (!DS.getSourceRange().getEnd().isInvalid())
2218 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002219
2220 // Parse exception-specification[opt].
2221 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002222 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002223 }
2224
Chris Lattner9f7564b2008-04-06 06:57:35 +00002225 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002226 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002227 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002228 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002229 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002230 /*arglist*/ 0, 0,
2231 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002232 LParenLoc, D),
2233 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002234 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002235 }
2236
2237 // Alternatively, this parameter list may be an identifier list form for a
2238 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002239 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002240 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002241 // K&R identifier lists can't have typedefs as identifiers, per
2242 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002243 if (RequiresArg) {
2244 Diag(Tok, diag::err_argument_required_after_attribute);
2245 delete AttrList;
2246 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002247 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2248 // normal declarators, not for abstract-declarators.
2249 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002250 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002251 }
2252
2253 // Finally, a normal, non-empty parameter type list.
2254
2255 // Build up an array of information about the parsed arguments.
2256 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002257
2258 // Enter function-declaration scope, limiting any declarators to the
2259 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002260 ParseScope PrototypeScope(this,
2261 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002262
2263 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002264 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002265 while (1) {
2266 if (Tok.is(tok::ellipsis)) {
2267 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002268 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002269 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002270 }
2271
Chris Lattner9f7564b2008-04-06 06:57:35 +00002272 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002273
Chris Lattner9f7564b2008-04-06 06:57:35 +00002274 // Parse the declaration-specifiers.
2275 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002276
2277 // If the caller parsed attributes for the first argument, add them now.
2278 if (AttrList) {
2279 DS.AddAttributes(AttrList);
2280 AttrList = 0; // Only apply the attributes to the first parameter.
2281 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002282 ParseDeclarationSpecifiers(DS);
2283
Chris Lattner9f7564b2008-04-06 06:57:35 +00002284 // Parse the declarator. This is "PrototypeContext", because we must
2285 // accept either 'declarator' or 'abstract-declarator' here.
2286 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2287 ParseDeclarator(ParmDecl);
2288
2289 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002290 if (Tok.is(tok::kw___attribute)) {
2291 SourceLocation Loc;
2292 AttributeList *AttrList = ParseAttributes(&Loc);
2293 ParmDecl.AddAttributes(AttrList, Loc);
2294 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002295
Chris Lattner9f7564b2008-04-06 06:57:35 +00002296 // Remember this parsed parameter in ParamInfo.
2297 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2298
Douglas Gregor605de8d2008-12-16 21:30:33 +00002299 // DefArgToks is used when the parsing of default arguments needs
2300 // to be delayed.
2301 CachedTokens *DefArgToks = 0;
2302
Chris Lattner9f7564b2008-04-06 06:57:35 +00002303 // If no parameter was specified, verify that *something* was specified,
2304 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002305 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2306 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002307 // Completely missing, emit error.
2308 Diag(DSStart, diag::err_missing_param);
2309 } else {
2310 // Otherwise, we have something. Add it and let semantic analysis try
2311 // to grok it and add the result to the ParamInfo we are building.
2312
2313 // Inform the actions module about the parameter declarator, so it gets
2314 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002315 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002316
2317 // Parse the default argument, if any. We parse the default
2318 // arguments in all dialects; the semantic analysis in
2319 // ActOnParamDefaultArgument will reject the default argument in
2320 // C.
2321 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002322 SourceLocation EqualLoc = Tok.getLocation();
2323
Chris Lattner3e254fb2008-04-08 04:40:51 +00002324 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002325 if (D.getContext() == Declarator::MemberContext) {
2326 // If we're inside a class definition, cache the tokens
2327 // corresponding to the default argument. We'll actually parse
2328 // them when we see the end of the class definition.
2329 // FIXME: Templates will require something similar.
2330 // FIXME: Can we use a smart pointer for Toks?
2331 DefArgToks = new CachedTokens;
2332
2333 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2334 tok::semi, false)) {
2335 delete DefArgToks;
2336 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002337 Actions.ActOnParamDefaultArgumentError(Param);
2338 } else
2339 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002340 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002341 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002342 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002343
2344 OwningExprResult DefArgResult(ParseAssignmentExpression());
2345 if (DefArgResult.isInvalid()) {
2346 Actions.ActOnParamDefaultArgumentError(Param);
2347 SkipUntil(tok::comma, tok::r_paren, true, true);
2348 } else {
2349 // Inform the actions module about the default argument
2350 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002351 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002352 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002353 }
2354 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002355
2356 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002357 ParmDecl.getIdentifierLoc(), Param,
2358 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002359 }
2360
2361 // If the next token is a comma, consume it and keep reading arguments.
2362 if (Tok.isNot(tok::comma)) break;
2363
2364 // Consume the comma.
2365 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002366 }
2367
Chris Lattner9f7564b2008-04-06 06:57:35 +00002368 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002369 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002370
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002371 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002372 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002373
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002374 DeclSpec DS;
2375 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002376 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002377 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002378 if (!DS.getSourceRange().getEnd().isInvalid())
2379 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002380
2381 // Parse exception-specification[opt].
2382 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002383 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002384 }
2385
Chris Lattner4b009652007-07-25 00:24:17 +00002386 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002387 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002388 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002389 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002390 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002391 LParenLoc, D),
2392 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002393}
2394
Chris Lattner35d9c912008-04-06 06:34:08 +00002395/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2396/// we found a K&R-style identifier list instead of a type argument list. The
2397/// current token is known to be the first identifier in the list.
2398///
2399/// identifier-list: [C99 6.7.5]
2400/// identifier
2401/// identifier-list ',' identifier
2402///
2403void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2404 Declarator &D) {
2405 // Build up an array of information about the parsed arguments.
2406 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2407 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2408
2409 // If there was no identifier specified for the declarator, either we are in
2410 // an abstract-declarator, or we are in a parameter declarator which was found
2411 // to be abstract. In abstract-declarators, identifier lists are not valid:
2412 // diagnose this.
2413 if (!D.getIdentifier())
2414 Diag(Tok, diag::ext_ident_list_in_param);
2415
2416 // Tok is known to be the first identifier in the list. Remember this
2417 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002418 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002419 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002420 Tok.getLocation(),
2421 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002422
Chris Lattner113a56b2008-04-06 06:39:19 +00002423 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002424
2425 while (Tok.is(tok::comma)) {
2426 // Eat the comma.
2427 ConsumeToken();
2428
Chris Lattner113a56b2008-04-06 06:39:19 +00002429 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002430 if (Tok.isNot(tok::identifier)) {
2431 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002432 SkipUntil(tok::r_paren);
2433 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002434 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002435
Chris Lattner35d9c912008-04-06 06:34:08 +00002436 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002437
2438 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002439 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002440 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002441
2442 // Verify that the argument identifier has not already been mentioned.
2443 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002444 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002445 } else {
2446 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002447 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002448 Tok.getLocation(),
2449 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002450 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002451
2452 // Eat the identifier.
2453 ConsumeToken();
2454 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002455
2456 // If we have the closing ')', eat it and we're done.
2457 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2458
Chris Lattner113a56b2008-04-06 06:39:19 +00002459 // Remember that we parsed a function type, and remember the attributes. This
2460 // function type is always a K&R style function type, which is not varargs and
2461 // has no prototype.
2462 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002463 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002464 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002465 /*TypeQuals*/0, LParenLoc, D),
2466 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002467}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002468
Chris Lattner4b009652007-07-25 00:24:17 +00002469/// [C90] direct-declarator '[' constant-expression[opt] ']'
2470/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2471/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2472/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2473/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2474void Parser::ParseBracketDeclarator(Declarator &D) {
2475 SourceLocation StartLoc = ConsumeBracket();
2476
Chris Lattner1525c3a2008-12-18 07:27:21 +00002477 // C array syntax has many features, but by-far the most common is [] and [4].
2478 // This code does a fast path to handle some of the most obvious cases.
2479 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002480 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002481 // Remember that we parsed the empty array type.
2482 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002483 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2484 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002485 return;
2486 } else if (Tok.getKind() == tok::numeric_constant &&
2487 GetLookAheadToken(1).is(tok::r_square)) {
2488 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002489 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002490 ConsumeToken();
2491
Sebastian Redl0c986032009-02-09 18:23:29 +00002492 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002493
2494 // If there was an error parsing the assignment-expression, recover.
2495 if (ExprRes.isInvalid())
2496 ExprRes.release(); // Deallocate expr, just use [].
2497
2498 // Remember that we parsed a array type, and remember its features.
2499 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002500 ExprRes.release(), StartLoc),
2501 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002502 return;
2503 }
2504
Chris Lattner4b009652007-07-25 00:24:17 +00002505 // If valid, this location is the position where we read the 'static' keyword.
2506 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002507 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002508 StaticLoc = ConsumeToken();
2509
2510 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002511 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002512 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002513 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002514
2515 // If we haven't already read 'static', check to see if there is one after the
2516 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002517 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002518 StaticLoc = ConsumeToken();
2519
2520 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2521 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002522 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002523
2524 // Handle the case where we have '[*]' as the array size. However, a leading
2525 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2526 // the the token after the star is a ']'. Since stars in arrays are
2527 // infrequent, use of lookahead is not costly here.
2528 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002529 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002530
Chris Lattner306d4df2008-12-18 06:50:14 +00002531 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002532 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002533 StaticLoc = SourceLocation(); // Drop the static.
2534 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002535 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002536 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002537 // Note, in C89, this production uses the constant-expr production instead
2538 // of assignment-expr. The only difference is that assignment-expr allows
2539 // things like '=' and '*='. Sema rejects these in C89 mode because they
2540 // are not i-c-e's, so we don't need to distinguish between the two here.
2541
Chris Lattner4b009652007-07-25 00:24:17 +00002542 // Parse the assignment-expression now.
2543 NumElements = ParseAssignmentExpression();
2544 }
2545
2546 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002547 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002548 // If the expression was invalid, skip it.
2549 SkipUntil(tok::r_square);
2550 return;
2551 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002552
2553 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2554
Chris Lattner1525c3a2008-12-18 07:27:21 +00002555 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002556 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2557 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002558 NumElements.release(), StartLoc),
2559 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002560}
2561
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002562/// [GNU] typeof-specifier:
2563/// typeof ( expressions )
2564/// typeof ( type-name )
2565/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002566///
2567void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002568 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002569 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002570 SourceLocation StartLoc = ConsumeToken();
2571
Chris Lattner34a01ad2007-10-09 17:33:22 +00002572 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002573 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002574 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002575 return;
2576 }
2577
Sebastian Redl14ca7412008-12-11 21:36:32 +00002578 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002579 if (Result.isInvalid()) {
2580 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002581 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002582 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002583
2584 const char *PrevSpec = 0;
2585 // Check for duplicate type specifiers.
2586 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002587 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002588 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002589
2590 // FIXME: Not accurate, the range gets one token more than it should.
2591 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002592 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002593 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002594
Steve Naroff7cbb1462007-07-31 12:34:36 +00002595 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2596
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002597 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002598 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002599
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002600 assert((Ty.isInvalid() || Ty.get()) &&
2601 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002602
Chris Lattner34a01ad2007-10-09 17:33:22 +00002603 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002604 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002605 return;
2606 }
2607 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002608
2609 if (Ty.isInvalid())
2610 DS.SetTypeSpecError();
2611 else {
2612 const char *PrevSpec = 0;
2613 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2614 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2615 Ty.get()))
2616 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2617 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002618 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002619 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002620
2621 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002622 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002623 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002624 return;
2625 }
2626 RParenLoc = ConsumeParen();
2627 const char *PrevSpec = 0;
2628 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2629 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002630 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002631 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002632 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002633 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002634}
2635
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002636