blob: 104ca0336b5ff9b24af70d025abd59d48778e4de [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.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattnera17991f2009-03-29 16:50:03 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context) {
232 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000233 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000234 case tok::kw_export:
235 case tok::kw_template:
Chris Lattnera17991f2009-03-29 16:50:03 +0000236 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context);
237 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000238 case tok::kw_namespace:
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 SingleDecl = ParseNamespace(Context);
240 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000241 case tok::kw_using:
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 SingleDecl = ParseUsingDirectiveOrDeclaration(Context);
243 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000244 case tok::kw_static_assert:
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 SingleDecl = ParseStaticAssertDeclaration();
246 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000247 default:
248 return ParseSimpleDeclaration(Context);
249 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000250
251 // This routine returns a DeclGroup, if the thing we parsed only contains a
252 // single decl, convert it now.
253 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000254}
255
256/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
257/// declaration-specifiers init-declarator-list[opt] ';'
258///[C90/C++]init-declarator-list ';' [TODO]
259/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000260///
261/// If RequireSemi is false, this does not check for a ';' at the end of the
262/// declaration.
263Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
264 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // Parse the common declaration-specifiers piece.
266 DeclSpec DS;
267 ParseDeclarationSpecifiers(DS);
268
269 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
270 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000271 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000272 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000273 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
274 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000275 }
276
277 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
278 ParseDeclarator(DeclaratorInfo);
279
Chris Lattner2c41d482009-03-29 17:18:04 +0000280 DeclGroupPtrTy DG =
281 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000282
283 // If the client wants to check what comes after the declaration, just return
284 // immediately without checking anything!
285 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000286
287 if (Tok.is(tok::semi)) {
288 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000289 return DG;
290 }
291
Chris Lattner2c41d482009-03-29 17:18:04 +0000292 Diag(Tok, diag::err_expected_semi_declation);
293 // Skip to end of block or statement
294 SkipUntil(tok::r_brace, true, true);
295 if (Tok.is(tok::semi))
296 ConsumeToken();
297 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000298}
299
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000300
Chris Lattner4b009652007-07-25 00:24:17 +0000301/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
302/// parsing 'declaration-specifiers declarator'. This method is split out this
303/// way to handle the ambiguity between top-level function-definitions and
304/// declarations.
305///
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// init-declarator-list: [C99 6.7]
307/// init-declarator
308/// init-declarator-list ',' init-declarator
309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000324///
Chris Lattnera17991f2009-03-29 16:50:03 +0000325Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000326ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000327 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
328 // that we parse together here.
329 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 // At this point, we know that it is not a function definition. Parse the
332 // rest of the init-declarator-list.
333 while (1) {
334 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000335 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000336 SourceLocation Loc;
337 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000338 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000339 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000340 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000341 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000342
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000343 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000344 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000345 }
Chris Lattner4b009652007-07-25 00:24:17 +0000346
347 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000348 if (Tok.is(tok::kw___attribute)) {
349 SourceLocation Loc;
350 AttributeList *AttrList = ParseAttributes(&Loc);
351 D.AddAttributes(AttrList, Loc);
352 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000353
354 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000355 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
356 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000357
Chris Lattner4b009652007-07-25 00:24:17 +0000358 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000359 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000360 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000361 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
362 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000363 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000364 } else {
365 OwningExprResult Init(ParseInitializer());
366 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000367 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000370 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000371 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000372 } else if (Tok.is(tok::l_paren)) {
373 // Parse C++ direct initializer: '(' expression-list ')'
374 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000375 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000376 CommaLocsTy CommaLocs;
377
378 bool InvalidExpr = false;
379 if (ParseExpressionList(Exprs, CommaLocs)) {
380 SkipUntil(tok::r_paren);
381 InvalidExpr = true;
382 }
383 // Match the ')'.
384 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
385
386 if (!InvalidExpr) {
387 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
388 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000389 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000390 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000391 &CommaLocs[0], RParenLoc);
392 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000393 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000394 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000395 }
396
Chris Lattner4b009652007-07-25 00:24:17 +0000397 // If we don't have a comma, it is either the end of the list (a ';') or an
398 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000399 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000400 break;
401
402 // Consume the comma.
403 ConsumeToken();
404
405 // Parse the next declarator.
406 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000407
408 // Accept attributes in an init-declarator. In the first declarator in a
409 // declaration, these would be part of the declspec. In subsequent
410 // declarators, they become part of the declarator itself, so that they
411 // don't apply to declarators after *this* one. Examples:
412 // short __attribute__((common)) var; -> declspec
413 // short var __attribute__((common)); -> declarator
414 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Chris Lattner926cf542008-10-20 04:57:38 +0000420
Chris Lattner4b009652007-07-25 00:24:17 +0000421 ParseDeclarator(D);
422 }
423
Chris Lattner2c41d482009-03-29 17:18:04 +0000424 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
425 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000426}
427
428/// ParseSpecifierQualifierList
429/// specifier-qualifier-list:
430/// type-specifier specifier-qualifier-list[opt]
431/// type-qualifier specifier-qualifier-list[opt]
432/// [GNU] attributes specifier-qualifier-list[opt]
433///
434void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
435 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
436 /// parse declaration-specifiers and complain about extra stuff.
437 ParseDeclarationSpecifiers(DS);
438
439 // Validate declspec for type-name.
440 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000441 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000442 Diag(Tok, diag::err_typename_requires_specqual);
443
444 // Issue diagnostic and remove storage class if present.
445 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
446 if (DS.getStorageClassSpecLoc().isValid())
447 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
448 else
449 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
450 DS.ClearStorageClassSpecs();
451 }
452
453 // Issue diagnostic and remove function specfier if present.
454 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000455 if (DS.isInlineSpecified())
456 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
457 if (DS.isVirtualSpecified())
458 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isExplicitSpecified())
460 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000461 DS.ClearFunctionSpecs();
462 }
463}
464
465/// ParseDeclarationSpecifiers
466/// declaration-specifiers: [C99 6.7]
467/// storage-class-specifier declaration-specifiers[opt]
468/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000469/// [C99] function-specifier declaration-specifiers[opt]
470/// [GNU] attributes declaration-specifiers[opt]
471///
472/// storage-class-specifier: [C99 6.7.1]
473/// 'typedef'
474/// 'extern'
475/// 'static'
476/// 'auto'
477/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000478/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000479/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000480/// function-specifier: [C99 6.7.4]
481/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000482/// [C++] 'virtual'
483/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000484///
Douglas Gregor52473432008-12-24 02:52:09 +0000485void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000486 TemplateParameterLists *TemplateParams,
487 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000488 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 while (1) {
490 int isInvalid = false;
491 const char *PrevSpec = 0;
492 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000495 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000496 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000497 // If this is not a declaration specifier token, we're done reading decl
498 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000499 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000500 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000501
502 case tok::coloncolon: // ::foo::bar
503 // Annotate C++ scope specifiers. If we get one, loop.
504 if (TryAnnotateCXXScopeToken())
505 continue;
506 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000507
508 case tok::annot_cxxscope: {
509 if (DS.hasTypeSpecifier())
510 goto DoneWithDeclSpec;
511
512 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000513 Token Next = NextToken();
514 if (Next.is(tok::annot_template_id) &&
515 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000516 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000517 // We have a qualified template-id, e.g., N::A<int>
518 CXXScopeSpec SS;
519 ParseOptionalCXXScopeSpecifier(SS);
520 assert(Tok.is(tok::annot_template_id) &&
521 "ParseOptionalCXXScopeSpecifier not working");
522 AnnotateTemplateIdTokenAsType(&SS);
523 continue;
524 }
525
526 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000527 goto DoneWithDeclSpec;
528
529 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000530 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000531 SS.setRange(Tok.getAnnotationRange());
532
533 // If the next token is the name of the class type that the C++ scope
534 // denotes, followed by a '(', then this is a constructor declaration.
535 // We're done with the decl-specifiers.
536 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
537 CurScope, &SS) &&
538 GetLookAheadToken(2).is(tok::l_paren))
539 goto DoneWithDeclSpec;
540
Douglas Gregor1075a162009-02-04 17:00:24 +0000541 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
542 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000543
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000544 if (TypeRep == 0)
545 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000546
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000547 ConsumeToken(); // The C++ scope.
548
Douglas Gregora60c62e2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000550 TypeRep);
551 if (isInvalid)
552 break;
553
554 DS.SetRangeEnd(Tok.getLocation());
555 ConsumeToken(); // The typename.
556
557 continue;
558 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000559
560 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000561 if (Tok.getAnnotationValue())
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
563 Tok.getAnnotationValue());
564 else
565 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000566 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
567 ConsumeToken(); // The typename
568
569 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
570 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
571 // Objective-C interface. If we don't have Objective-C or a '<', this is
572 // just a normal reference to a typedef name.
573 if (!Tok.is(tok::less) || !getLang().ObjC1)
574 continue;
575
576 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000577 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000578 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
579 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
580
581 DS.SetRangeEnd(EndProtoLoc);
582 continue;
583 }
584
Chris Lattnerfda18db2008-07-26 01:18:38 +0000585 // typedef-name
586 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000587 // In C++, check to see if this is a scope specifier like foo::bar::, if
588 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000589 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
590 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000591
Chris Lattnerfda18db2008-07-26 01:18:38 +0000592 // This identifier can only be a typedef name if we haven't already seen
593 // a type-specifier. Without this check we misparse:
594 // typedef int X; struct Y { short X; }; as 'short int'.
595 if (DS.hasTypeSpecifier())
596 goto DoneWithDeclSpec;
597
598 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000599 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
600 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000601
Chris Lattnerfda18db2008-07-26 01:18:38 +0000602 if (TypeRep == 0)
603 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000604
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000605 // C++: If the identifier is actually the name of the class type
606 // being defined and the next token is a '(', then this is a
607 // constructor declaration. We're done with the decl-specifiers
608 // and will treat this token as an identifier.
609 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000610 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000611 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
612 NextToken().getKind() == tok::l_paren)
613 goto DoneWithDeclSpec;
614
Douglas Gregora60c62e2009-02-09 15:09:02 +0000615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000616 TypeRep);
617 if (isInvalid)
618 break;
619
620 DS.SetRangeEnd(Tok.getLocation());
621 ConsumeToken(); // The identifier
622
623 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
624 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
625 // Objective-C interface. If we don't have Objective-C or a '<', this is
626 // just a normal reference to a typedef name.
627 if (!Tok.is(tok::less) || !getLang().ObjC1)
628 continue;
629
630 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000631 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000632 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000633 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000634
635 DS.SetRangeEnd(EndProtoLoc);
636
Steve Narofff7683302008-09-22 10:28:57 +0000637 // Need to support trailing type qualifiers (e.g. "id<p> const").
638 // If a type specifier follows, it will be diagnosed elsewhere.
639 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000640 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000641
642 // type-name
643 case tok::annot_template_id: {
644 TemplateIdAnnotation *TemplateId
645 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000646 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000647 // This template-id does not refer to a type name, so we're
648 // done with the type-specifiers.
649 goto DoneWithDeclSpec;
650 }
651
652 // Turn the template-id annotation token into a type annotation
653 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000654 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000655 continue;
656 }
657
Chris Lattner4b009652007-07-25 00:24:17 +0000658 // GNU attributes support.
659 case tok::kw___attribute:
660 DS.AddAttributes(ParseAttributes());
661 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000662
663 // Microsoft declspec support.
664 case tok::kw___declspec:
665 if (!PP.getLangOptions().Microsoft)
666 goto DoneWithDeclSpec;
667 FuzzyParseMicrosoftDeclSpec();
668 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000669
Steve Naroffedd04d52008-12-25 14:16:32 +0000670 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000671 case tok::kw___forceinline:
672 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000673 case tok::kw___cdecl:
674 case tok::kw___stdcall:
675 case tok::kw___fastcall:
676 if (!PP.getLangOptions().Microsoft)
677 goto DoneWithDeclSpec;
678 // Just ignore it.
679 break;
680
Chris Lattner4b009652007-07-25 00:24:17 +0000681 // storage-class-specifier
682 case tok::kw_typedef:
683 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
684 break;
685 case tok::kw_extern:
686 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000687 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
689 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000690 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
692 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000693 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000694 case tok::kw_static:
695 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000696 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000697 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
698 break;
699 case tok::kw_auto:
700 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
701 break;
702 case tok::kw_register:
703 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
704 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000705 case tok::kw_mutable:
706 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
707 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000708 case tok::kw___thread:
709 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
710 break;
711
Chris Lattner4b009652007-07-25 00:24:17 +0000712 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000713
Chris Lattner4b009652007-07-25 00:24:17 +0000714 // function-specifier
715 case tok::kw_inline:
716 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
717 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000718 case tok::kw_virtual:
719 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
720 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000721 case tok::kw_explicit:
722 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
723 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000724
725 // type-specifier
726 case tok::kw_short:
727 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
728 break;
729 case tok::kw_long:
730 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
731 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
732 else
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
734 break;
735 case tok::kw_signed:
736 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
737 break;
738 case tok::kw_unsigned:
739 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
740 break;
741 case tok::kw__Complex:
742 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
743 break;
744 case tok::kw__Imaginary:
745 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
746 break;
747 case tok::kw_void:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
749 break;
750 case tok::kw_char:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
752 break;
753 case tok::kw_int:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
755 break;
756 case tok::kw_float:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
758 break;
759 case tok::kw_double:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
761 break;
762 case tok::kw_wchar_t:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
764 break;
765 case tok::kw_bool:
766 case tok::kw__Bool:
767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
768 break;
769 case tok::kw__Decimal32:
770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
771 break;
772 case tok::kw__Decimal64:
773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
774 break;
775 case tok::kw__Decimal128:
776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
777 break;
778
779 // class-specifier:
780 case tok::kw_class:
781 case tok::kw_struct:
782 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000783 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000784 continue;
785
786 // enum-specifier:
787 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000788 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000789 continue;
790
791 // cv-qualifier:
792 case tok::kw_const:
793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
794 break;
795 case tok::kw_volatile:
796 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
797 getLang())*2;
798 break;
799 case tok::kw_restrict:
800 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
801 getLang())*2;
802 break;
803
Douglas Gregord3022602009-03-27 23:10:48 +0000804 // C++ typename-specifier:
805 case tok::kw_typename:
806 if (TryAnnotateTypeOrScopeToken())
807 continue;
808 break;
809
Chris Lattnerc297b722009-01-21 19:48:37 +0000810 // GNU typeof support.
811 case tok::kw_typeof:
812 ParseTypeofSpecifier(DS);
813 continue;
814
Steve Naroff5f0466b2008-06-05 00:02:44 +0000815 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000816 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000817 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
818 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000819 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000820 goto DoneWithDeclSpec;
821
822 {
823 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000824 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000825 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000826 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000827 DS.SetRangeEnd(EndProtoLoc);
828
Chris Lattnerf006a222008-11-18 07:48:38 +0000829 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
830 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000831 // Need to support trailing type qualifiers (e.g. "id<p> const").
832 // If a type specifier follows, it will be diagnosed elsewhere.
833 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000834 }
Chris Lattner4b009652007-07-25 00:24:17 +0000835 }
836 // If the specifier combination wasn't legal, issue a diagnostic.
837 if (isInvalid) {
838 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000839 // Pick between error or extwarn.
840 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
841 : diag::ext_duplicate_declspec;
842 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000843 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000844 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000845 ConsumeToken();
846 }
847}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000848
Chris Lattnerd706dc82009-01-06 06:59:53 +0000849/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000850/// primarily follow the C++ grammar with additions for C99 and GNU,
851/// which together subsume the C grammar. Note that the C++
852/// type-specifier also includes the C type-qualifier (for const,
853/// volatile, and C99 restrict). Returns true if a type-specifier was
854/// found (and parsed), false otherwise.
855///
856/// type-specifier: [C++ 7.1.5]
857/// simple-type-specifier
858/// class-specifier
859/// enum-specifier
860/// elaborated-type-specifier [TODO]
861/// cv-qualifier
862///
863/// cv-qualifier: [C++ 7.1.5.1]
864/// 'const'
865/// 'volatile'
866/// [C99] 'restrict'
867///
868/// simple-type-specifier: [ C++ 7.1.5.2]
869/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
870/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
871/// 'char'
872/// 'wchar_t'
873/// 'bool'
874/// 'short'
875/// 'int'
876/// 'long'
877/// 'signed'
878/// 'unsigned'
879/// 'float'
880/// 'double'
881/// 'void'
882/// [C99] '_Bool'
883/// [C99] '_Complex'
884/// [C99] '_Imaginary' // Removed in TC2?
885/// [GNU] '_Decimal32'
886/// [GNU] '_Decimal64'
887/// [GNU] '_Decimal128'
888/// [GNU] typeof-specifier
889/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
890/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000891bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
892 const char *&PrevSpec,
893 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000894 SourceLocation Loc = Tok.getLocation();
895
896 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000897 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000898 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000899 // Annotate typenames and C++ scope specifiers. If we get one, just
900 // recurse to handle whatever we get.
901 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000902 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000903 // Otherwise, not a type specifier.
904 return false;
905 case tok::coloncolon: // ::foo::bar
906 if (NextToken().is(tok::kw_new) || // ::new
907 NextToken().is(tok::kw_delete)) // ::delete
908 return false;
909
910 // Annotate typenames and C++ scope specifiers. If we get one, just
911 // recurse to handle whatever we get.
912 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000913 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000914 // Otherwise, not a type specifier.
915 return false;
916
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000917 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000918 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000919 if (Tok.getAnnotationValue())
920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
921 Tok.getAnnotationValue());
922 else
923 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000924 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
925 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000926
927 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
928 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
929 // Objective-C interface. If we don't have Objective-C or a '<', this is
930 // just a normal reference to a typedef name.
931 if (!Tok.is(tok::less) || !getLang().ObjC1)
932 return true;
933
934 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000935 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000936 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
937 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
938
939 DS.SetRangeEnd(EndProtoLoc);
940 return true;
941 }
942
943 case tok::kw_short:
944 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
945 break;
946 case tok::kw_long:
947 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
948 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
949 else
950 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
951 break;
952 case tok::kw_signed:
953 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
954 break;
955 case tok::kw_unsigned:
956 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
957 break;
958 case tok::kw__Complex:
959 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
960 break;
961 case tok::kw__Imaginary:
962 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
963 break;
964 case tok::kw_void:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
966 break;
967 case tok::kw_char:
968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
969 break;
970 case tok::kw_int:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
972 break;
973 case tok::kw_float:
974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
975 break;
976 case tok::kw_double:
977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
978 break;
979 case tok::kw_wchar_t:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
981 break;
982 case tok::kw_bool:
983 case tok::kw__Bool:
984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
985 break;
986 case tok::kw__Decimal32:
987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
988 break;
989 case tok::kw__Decimal64:
990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
991 break;
992 case tok::kw__Decimal128:
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
994 break;
995
996 // class-specifier:
997 case tok::kw_class:
998 case tok::kw_struct:
999 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +00001000 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001001 return true;
1002
1003 // enum-specifier:
1004 case tok::kw_enum:
1005 ParseEnumSpecifier(DS);
1006 return true;
1007
1008 // cv-qualifier:
1009 case tok::kw_const:
1010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1011 getLang())*2;
1012 break;
1013 case tok::kw_volatile:
1014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1015 getLang())*2;
1016 break;
1017 case tok::kw_restrict:
1018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1019 getLang())*2;
1020 break;
1021
1022 // GNU typeof support.
1023 case tok::kw_typeof:
1024 ParseTypeofSpecifier(DS);
1025 return true;
1026
Steve Naroffedd04d52008-12-25 14:16:32 +00001027 case tok::kw___cdecl:
1028 case tok::kw___stdcall:
1029 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001030 if (!PP.getLangOptions().Microsoft) return false;
1031 ConsumeToken();
1032 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001033
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001034 default:
1035 // Not a type-specifier; do nothing.
1036 return false;
1037 }
1038
1039 // If the specifier combination wasn't legal, issue a diagnostic.
1040 if (isInvalid) {
1041 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001042 // Pick between error or extwarn.
1043 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1044 : diag::ext_duplicate_declspec;
1045 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001046 }
1047 DS.SetRangeEnd(Tok.getLocation());
1048 ConsumeToken(); // whatever we parsed above.
1049 return true;
1050}
Chris Lattner4b009652007-07-25 00:24:17 +00001051
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001052/// ParseStructDeclaration - Parse a struct declaration without the terminating
1053/// semicolon.
1054///
Chris Lattner4b009652007-07-25 00:24:17 +00001055/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001056/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001057/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001058/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001059/// struct-declarator-list:
1060/// struct-declarator
1061/// struct-declarator-list ',' struct-declarator
1062/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1063/// struct-declarator:
1064/// declarator
1065/// [GNU] declarator attributes[opt]
1066/// declarator[opt] ':' constant-expression
1067/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1068///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001069void Parser::
1070ParseStructDeclaration(DeclSpec &DS,
1071 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001072 if (Tok.is(tok::kw___extension__)) {
1073 // __extension__ silences extension warnings in the subexpression.
1074 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001075 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001076 return ParseStructDeclaration(DS, Fields);
1077 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001078
1079 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001080 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001082
Douglas Gregorb748fc52009-01-12 22:49:06 +00001083 // If there are no declarators, this is a free-standing declaration
1084 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001085 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001086 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001087 return;
1088 }
1089
1090 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001091 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001092 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001093 FieldDeclarator &DeclaratorInfo = Fields.back();
1094
Steve Naroffa9adf112007-08-20 22:28:22 +00001095 /// struct-declarator: declarator
1096 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001097 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001098 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001099
Chris Lattner34a01ad2007-10-09 17:33:22 +00001100 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001102 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001103 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001104 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001105 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001106 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001107 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001108
Steve Naroffa9adf112007-08-20 22:28:22 +00001109 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001110 if (Tok.is(tok::kw___attribute)) {
1111 SourceLocation Loc;
1112 AttributeList *AttrList = ParseAttributes(&Loc);
1113 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1114 }
1115
Steve Naroffa9adf112007-08-20 22:28:22 +00001116 // If we don't have a comma, it is either the end of the list (a ';')
1117 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001119 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001120
Steve Naroffa9adf112007-08-20 22:28:22 +00001121 // Consume the comma.
1122 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001123
Steve Naroffa9adf112007-08-20 22:28:22 +00001124 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001125 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001126
Steve Naroffa9adf112007-08-20 22:28:22 +00001127 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001128 if (Tok.is(tok::kw___attribute)) {
1129 SourceLocation Loc;
1130 AttributeList *AttrList = ParseAttributes(&Loc);
1131 Fields.back().D.AddAttributes(AttrList, Loc);
1132 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001133 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001134}
1135
1136/// ParseStructUnionBody
1137/// struct-contents:
1138/// struct-declaration-list
1139/// [EXT] empty
1140/// [GNU] "struct-declaration-list" without terminatoring ';'
1141/// struct-declaration-list:
1142/// struct-declaration
1143/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001144/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001145///
Chris Lattner4b009652007-07-25 00:24:17 +00001146void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001147 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001148 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1149 PP.getSourceManager(),
1150 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001151
Chris Lattner4b009652007-07-25 00:24:17 +00001152 SourceLocation LBraceLoc = ConsumeBrace();
1153
Douglas Gregorcab994d2009-01-09 22:42:13 +00001154 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001155 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1156
Chris Lattner4b009652007-07-25 00:24:17 +00001157 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1158 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001159 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001160 Diag(Tok, diag::ext_empty_struct_union_enum)
1161 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001162
Chris Lattner5261d0c2009-03-28 19:18:32 +00001163 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001164 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1165
Chris Lattner4b009652007-07-25 00:24:17 +00001166 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 // Each iteration of this loop reads one struct-declaration.
1169
1170 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001171 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001172 Diag(Tok, diag::ext_extra_struct_semi)
1173 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001174 ConsumeToken();
1175 continue;
1176 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001177
1178 // Parse all the comma separated declarators.
1179 DeclSpec DS;
1180 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001181 if (!Tok.is(tok::at)) {
1182 ParseStructDeclaration(DS, FieldDeclarators);
1183
1184 // Convert them all to fields.
1185 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1186 FieldDeclarator &FD = FieldDeclarators[i];
1187 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001188 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1189 DS.getSourceRange().getBegin(),
1190 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001191 FieldDecls.push_back(Field);
1192 }
1193 } else { // Handle @defs
1194 ConsumeToken();
1195 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1196 Diag(Tok, diag::err_unexpected_at);
1197 SkipUntil(tok::semi, true, true);
1198 continue;
1199 }
1200 ConsumeToken();
1201 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1202 if (!Tok.is(tok::identifier)) {
1203 Diag(Tok, diag::err_expected_ident);
1204 SkipUntil(tok::semi, true, true);
1205 continue;
1206 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001207 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001208 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1209 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001210 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1211 ConsumeToken();
1212 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1213 }
Chris Lattner4b009652007-07-25 00:24:17 +00001214
Chris Lattner34a01ad2007-10-09 17:33:22 +00001215 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001216 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001217 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001218 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001219 break;
1220 } else {
1221 Diag(Tok, diag::err_expected_semi_decl_list);
1222 // Skip to end of block or statement
1223 SkipUntil(tok::r_brace, true, true);
1224 }
1225 }
1226
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001227 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001228
Chris Lattner4b009652007-07-25 00:24:17 +00001229 AttributeList *AttrList = 0;
1230 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001231 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001232 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001233
1234 Actions.ActOnFields(CurScope,
1235 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1236 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001237 AttrList);
1238 StructScope.Exit();
1239 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001240}
1241
1242
1243/// ParseEnumSpecifier
1244/// enum-specifier: [C99 6.7.2.2]
1245/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001246///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001247/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1248/// '}' attributes[opt]
1249/// 'enum' identifier
1250/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001251///
1252/// [C++] elaborated-type-specifier:
1253/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1254///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001255void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001256 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001257 SourceLocation StartLoc = ConsumeToken();
1258
1259 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001260
1261 AttributeList *Attr = 0;
1262 // If attributes exist after tag, parse them.
1263 if (Tok.is(tok::kw___attribute))
1264 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001265
1266 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001267 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001268 if (Tok.isNot(tok::identifier)) {
1269 Diag(Tok, diag::err_expected_ident);
1270 if (Tok.isNot(tok::l_brace)) {
1271 // Has no name and is not a definition.
1272 // Skip the rest of this declarator, up until the comma or semicolon.
1273 SkipUntil(tok::comma, true);
1274 return;
1275 }
1276 }
1277 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001278
1279 // Must have either 'enum name' or 'enum {...}'.
1280 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1281 Diag(Tok, diag::err_expected_ident_lbrace);
1282
1283 // Skip the rest of this declarator, up until the comma or semicolon.
1284 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001285 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001286 }
1287
1288 // If an identifier is present, consume and remember it.
1289 IdentifierInfo *Name = 0;
1290 SourceLocation NameLoc;
1291 if (Tok.is(tok::identifier)) {
1292 Name = Tok.getIdentifierInfo();
1293 NameLoc = ConsumeToken();
1294 }
1295
1296 // There are three options here. If we have 'enum foo;', then this is a
1297 // forward declaration. If we have 'enum foo {...' then this is a
1298 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1299 //
1300 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1301 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1302 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1303 //
1304 Action::TagKind TK;
1305 if (Tok.is(tok::l_brace))
1306 TK = Action::TK_Definition;
1307 else if (Tok.is(tok::semi))
1308 TK = Action::TK_Declaration;
1309 else
1310 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001311 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1312 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001313
Chris Lattner34a01ad2007-10-09 17:33:22 +00001314 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001315 ParseEnumBody(StartLoc, TagDecl);
1316
1317 // TODO: semantic analysis on the declspec for enums.
1318 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001319 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1320 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001321 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001322}
1323
1324/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1325/// enumerator-list:
1326/// enumerator
1327/// enumerator-list ',' enumerator
1328/// enumerator:
1329/// enumeration-constant
1330/// enumeration-constant '=' constant-expression
1331/// enumeration-constant:
1332/// identifier
1333///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001334void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001335 // Enter the scope of the enum body and start the definition.
1336 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001337 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001338
Chris Lattner4b009652007-07-25 00:24:17 +00001339 SourceLocation LBraceLoc = ConsumeBrace();
1340
Chris Lattnerc9a92452007-08-27 17:24:30 +00001341 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001342 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001343 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001344
Chris Lattner5261d0c2009-03-28 19:18:32 +00001345 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001346
Chris Lattner5261d0c2009-03-28 19:18:32 +00001347 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001348
1349 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001350 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001351 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1352 SourceLocation IdentLoc = ConsumeToken();
1353
1354 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001355 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001356 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001357 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001358 AssignedVal = ParseConstantExpression();
1359 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001360 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001361 }
1362
1363 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001364 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1365 LastEnumConstDecl,
1366 IdentLoc, Ident,
1367 EqualLoc,
1368 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001369 EnumConstantDecls.push_back(EnumConstDecl);
1370 LastEnumConstDecl = EnumConstDecl;
1371
Chris Lattner34a01ad2007-10-09 17:33:22 +00001372 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001373 break;
1374 SourceLocation CommaLoc = ConsumeToken();
1375
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001376 if (Tok.isNot(tok::identifier) &&
1377 !(getLang().C99 || getLang().CPlusPlus0x))
1378 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1379 << getLang().CPlusPlus
1380 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001381 }
1382
1383 // Eat the }.
1384 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1385
Steve Naroff0acc9c92007-09-15 18:49:24 +00001386 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001387 EnumConstantDecls.size());
1388
Chris Lattner5261d0c2009-03-28 19:18:32 +00001389 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001390 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001391 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001392 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001393
1394 EnumScope.Exit();
1395 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001396}
1397
1398/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001399/// start of a type-qualifier-list.
1400bool Parser::isTypeQualifier() const {
1401 switch (Tok.getKind()) {
1402 default: return false;
1403 // type-qualifier
1404 case tok::kw_const:
1405 case tok::kw_volatile:
1406 case tok::kw_restrict:
1407 return true;
1408 }
1409}
1410
1411/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001412/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001413bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001414 switch (Tok.getKind()) {
1415 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001416
1417 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001418 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001419 // Annotate typenames and C++ scope specifiers. If we get one, just
1420 // recurse to handle whatever we get.
1421 if (TryAnnotateTypeOrScopeToken())
1422 return isTypeSpecifierQualifier();
1423 // Otherwise, not a type specifier.
1424 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001425
Chris Lattnerb75fde62009-01-04 23:41:41 +00001426 case tok::coloncolon: // ::foo::bar
1427 if (NextToken().is(tok::kw_new) || // ::new
1428 NextToken().is(tok::kw_delete)) // ::delete
1429 return false;
1430
1431 // Annotate typenames and C++ scope specifiers. If we get one, just
1432 // recurse to handle whatever we get.
1433 if (TryAnnotateTypeOrScopeToken())
1434 return isTypeSpecifierQualifier();
1435 // Otherwise, not a type specifier.
1436 return false;
1437
Chris Lattner4b009652007-07-25 00:24:17 +00001438 // GNU attributes support.
1439 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001440 // GNU typeof support.
1441 case tok::kw_typeof:
1442
Chris Lattner4b009652007-07-25 00:24:17 +00001443 // type-specifiers
1444 case tok::kw_short:
1445 case tok::kw_long:
1446 case tok::kw_signed:
1447 case tok::kw_unsigned:
1448 case tok::kw__Complex:
1449 case tok::kw__Imaginary:
1450 case tok::kw_void:
1451 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001452 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001453 case tok::kw_int:
1454 case tok::kw_float:
1455 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001456 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001457 case tok::kw__Bool:
1458 case tok::kw__Decimal32:
1459 case tok::kw__Decimal64:
1460 case tok::kw__Decimal128:
1461
Chris Lattner2e78db32008-04-13 18:59:07 +00001462 // struct-or-union-specifier (C99) or class-specifier (C++)
1463 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001464 case tok::kw_struct:
1465 case tok::kw_union:
1466 // enum-specifier
1467 case tok::kw_enum:
1468
1469 // type-qualifier
1470 case tok::kw_const:
1471 case tok::kw_volatile:
1472 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001473
1474 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001475 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001476 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001477
1478 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1479 case tok::less:
1480 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001481
1482 case tok::kw___cdecl:
1483 case tok::kw___stdcall:
1484 case tok::kw___fastcall:
1485 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001486 }
1487}
1488
1489/// isDeclarationSpecifier() - Return true if the current token is part of a
1490/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001491bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001492 switch (Tok.getKind()) {
1493 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001494
1495 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001496 // Unfortunate hack to support "Class.factoryMethod" notation.
1497 if (getLang().ObjC1 && NextToken().is(tok::period))
1498 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001499 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001500
Douglas Gregord3022602009-03-27 23:10:48 +00001501 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001502 // Annotate typenames and C++ scope specifiers. If we get one, just
1503 // recurse to handle whatever we get.
1504 if (TryAnnotateTypeOrScopeToken())
1505 return isDeclarationSpecifier();
1506 // Otherwise, not a declaration specifier.
1507 return false;
1508 case tok::coloncolon: // ::foo::bar
1509 if (NextToken().is(tok::kw_new) || // ::new
1510 NextToken().is(tok::kw_delete)) // ::delete
1511 return false;
1512
1513 // Annotate typenames and C++ scope specifiers. If we get one, just
1514 // recurse to handle whatever we get.
1515 if (TryAnnotateTypeOrScopeToken())
1516 return isDeclarationSpecifier();
1517 // Otherwise, not a declaration specifier.
1518 return false;
1519
Chris Lattner4b009652007-07-25 00:24:17 +00001520 // storage-class-specifier
1521 case tok::kw_typedef:
1522 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001523 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001524 case tok::kw_static:
1525 case tok::kw_auto:
1526 case tok::kw_register:
1527 case tok::kw___thread:
1528
1529 // type-specifiers
1530 case tok::kw_short:
1531 case tok::kw_long:
1532 case tok::kw_signed:
1533 case tok::kw_unsigned:
1534 case tok::kw__Complex:
1535 case tok::kw__Imaginary:
1536 case tok::kw_void:
1537 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001538 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001539 case tok::kw_int:
1540 case tok::kw_float:
1541 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001542 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001543 case tok::kw__Bool:
1544 case tok::kw__Decimal32:
1545 case tok::kw__Decimal64:
1546 case tok::kw__Decimal128:
1547
Chris Lattner2e78db32008-04-13 18:59:07 +00001548 // struct-or-union-specifier (C99) or class-specifier (C++)
1549 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001550 case tok::kw_struct:
1551 case tok::kw_union:
1552 // enum-specifier
1553 case tok::kw_enum:
1554
1555 // type-qualifier
1556 case tok::kw_const:
1557 case tok::kw_volatile:
1558 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001559
Chris Lattner4b009652007-07-25 00:24:17 +00001560 // function-specifier
1561 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001562 case tok::kw_virtual:
1563 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001564
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001565 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001566 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001567
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001568 // GNU typeof support.
1569 case tok::kw_typeof:
1570
1571 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001572 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001573 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +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
Steve Naroffab1a3632009-01-06 19:34:12 +00001579 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001580 case tok::kw___cdecl:
1581 case tok::kw___stdcall:
1582 case tok::kw___fastcall:
1583 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001584 }
1585}
1586
1587
1588/// ParseTypeQualifierListOpt
1589/// type-qualifier-list: [C99 6.7.5]
1590/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001591/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001592/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001593/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001594///
Chris Lattner460696f2008-12-18 07:02:59 +00001595void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001596 while (1) {
1597 int isInvalid = false;
1598 const char *PrevSpec = 0;
1599 SourceLocation Loc = Tok.getLocation();
1600
1601 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001602 case tok::kw_const:
1603 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1604 getLang())*2;
1605 break;
1606 case tok::kw_volatile:
1607 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1608 getLang())*2;
1609 break;
1610 case tok::kw_restrict:
1611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1612 getLang())*2;
1613 break;
Steve Naroffad620402008-12-25 14:41:26 +00001614 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001615 case tok::kw___cdecl:
1616 case tok::kw___stdcall:
1617 case tok::kw___fastcall:
1618 if (!PP.getLangOptions().Microsoft)
1619 goto DoneWithTypeQuals;
1620 // Just ignore it.
1621 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001622 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001623 if (AttributesAllowed) {
1624 DS.AddAttributes(ParseAttributes());
1625 continue; // do *not* consume the next token!
1626 }
1627 // otherwise, FALL THROUGH!
1628 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001629 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001630 // If this is not a type-qualifier token, we're done reading type
1631 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001632 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001633 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001634 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001635
Chris Lattner4b009652007-07-25 00:24:17 +00001636 // If the specifier combination wasn't legal, issue a diagnostic.
1637 if (isInvalid) {
1638 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001639 // Pick between error or extwarn.
1640 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1641 : diag::ext_duplicate_declspec;
1642 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001643 }
1644 ConsumeToken();
1645 }
1646}
1647
1648
1649/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1650///
1651void Parser::ParseDeclarator(Declarator &D) {
1652 /// This implements the 'declarator' production in the C grammar, then checks
1653 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001654 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001655}
1656
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001657/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1658/// is parsed by the function passed to it. Pass null, and the direct-declarator
1659/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001660/// ptr-operator production.
1661///
Sebastian Redl75555032009-01-24 21:16:55 +00001662/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1663/// [C] pointer[opt] direct-declarator
1664/// [C++] direct-declarator
1665/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001666///
1667/// pointer: [C99 6.7.5]
1668/// '*' type-qualifier-list[opt]
1669/// '*' type-qualifier-list[opt] pointer
1670///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001671/// ptr-operator:
1672/// '*' cv-qualifier-seq[opt]
1673/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001674/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001675/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001676/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001677/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001678void Parser::ParseDeclaratorInternal(Declarator &D,
1679 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001680
Sebastian Redl75555032009-01-24 21:16:55 +00001681 // C++ member pointers start with a '::' or a nested-name.
1682 // Member pointers get special handling, since there's no place for the
1683 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001684 if (getLang().CPlusPlus &&
1685 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1686 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001687 CXXScopeSpec SS;
1688 if (ParseOptionalCXXScopeSpecifier(SS)) {
1689 if(Tok.isNot(tok::star)) {
1690 // The scope spec really belongs to the direct-declarator.
1691 D.getCXXScopeSpec() = SS;
1692 if (DirectDeclParser)
1693 (this->*DirectDeclParser)(D);
1694 return;
1695 }
1696
1697 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001698 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001699 DeclSpec DS;
1700 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001701 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001702
1703 // Recurse to parse whatever is left.
1704 ParseDeclaratorInternal(D, DirectDeclParser);
1705
1706 // Sema will have to catch (syntactically invalid) pointers into global
1707 // scope. It has to catch pointers into namespace scope anyway.
1708 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001709 Loc, DS.TakeAttributes()),
1710 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001711 return;
1712 }
1713 }
1714
1715 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001716 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001717 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001718 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001719 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001720 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001721 if (DirectDeclParser)
1722 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001723 return;
1724 }
Sebastian Redl75555032009-01-24 21:16:55 +00001725
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001726 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1727 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001728 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001729 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001730
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001731 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001732 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001733 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001734
Chris Lattner4b009652007-07-25 00:24:17 +00001735 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001736 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001737
Chris Lattner4b009652007-07-25 00:24:17 +00001738 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001739 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001740 if (Kind == tok::star)
1741 // Remember that we parsed a pointer type, and remember the type-quals.
1742 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001743 DS.TakeAttributes()),
1744 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001745 else
1746 // Remember that we parsed a Block type, and remember the type-quals.
1747 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001748 Loc),
1749 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001750 } else {
1751 // Is a reference
1752 DeclSpec DS;
1753
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001754 // Complain about rvalue references in C++03, but then go on and build
1755 // the declarator.
1756 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1757 Diag(Loc, diag::err_rvalue_reference);
1758
Chris Lattner4b009652007-07-25 00:24:17 +00001759 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1760 // cv-qualifiers are introduced through the use of a typedef or of a
1761 // template type argument, in which case the cv-qualifiers are ignored.
1762 //
1763 // [GNU] Retricted references are allowed.
1764 // [GNU] Attributes on references are allowed.
1765 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001766 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001767
1768 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1769 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1770 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001771 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001772 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1773 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001774 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001775 }
1776
1777 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001778 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001779
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001780 if (D.getNumTypeObjects() > 0) {
1781 // C++ [dcl.ref]p4: There shall be no references to references.
1782 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1783 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001784 if (const IdentifierInfo *II = D.getIdentifier())
1785 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1786 << II;
1787 else
1788 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1789 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001790
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001791 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001792 // can go ahead and build the (technically ill-formed)
1793 // declarator: reference collapsing will take care of it.
1794 }
1795 }
1796
Chris Lattner4b009652007-07-25 00:24:17 +00001797 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001798 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001799 DS.TakeAttributes(),
1800 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001801 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001802 }
1803}
1804
1805/// ParseDirectDeclarator
1806/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001807/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001808/// '(' declarator ')'
1809/// [GNU] '(' attributes declarator ')'
1810/// [C90] direct-declarator '[' constant-expression[opt] ']'
1811/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1812/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1813/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1814/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1815/// direct-declarator '(' parameter-type-list ')'
1816/// direct-declarator '(' identifier-list[opt] ')'
1817/// [GNU] direct-declarator '(' parameter-forward-declarations
1818/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001819/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1820/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001821/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001822///
1823/// declarator-id: [C++ 8]
1824/// id-expression
1825/// '::'[opt] nested-name-specifier[opt] type-name
1826///
1827/// id-expression: [C++ 5.1]
1828/// unqualified-id
1829/// qualified-id [TODO]
1830///
1831/// unqualified-id: [C++ 5.1]
1832/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001833/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001834/// conversion-function-id [TODO]
1835/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001836/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001837///
Chris Lattner4b009652007-07-25 00:24:17 +00001838void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001839 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001840
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001841 if (getLang().CPlusPlus) {
1842 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001843 // ParseDeclaratorInternal might already have parsed the scope.
1844 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1845 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001846 if (afterCXXScope) {
1847 // Change the declaration context for name lookup, until this function
1848 // is exited (and the declarator has been parsed).
1849 DeclScopeObj.EnterDeclaratorScope();
1850 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001851
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001852 if (Tok.is(tok::identifier)) {
1853 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001854
Douglas Gregor2fa10442008-12-18 19:37:40 +00001855 // If this identifier is the name of the current class, it's a
1856 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001857 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001858 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001859 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001860 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001861 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001862 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001863 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1864 ConsumeToken();
1865 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001866 } else if (Tok.is(tok::annot_template_id)) {
1867 TemplateIdAnnotation *TemplateId
1868 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1869
1870 // FIXME: Could this template-id name a constructor?
1871
1872 // FIXME: This is an egregious hack, where we silently ignore
1873 // the specialization (which should be a function template
1874 // specialization name) and use the name instead. This hack
1875 // will go away when we have support for function
1876 // specializations.
1877 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1878 TemplateId->Destroy();
1879 ConsumeToken();
1880 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001881 } else if (Tok.is(tok::kw_operator)) {
1882 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001883 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001884
Douglas Gregor853dd392008-12-26 15:00:45 +00001885 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001886 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1887 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001888 } else {
1889 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001890 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1891 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1892 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001893 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001894 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001895 }
1896 goto PastIdentifier;
1897 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001898 // This should be a C++ destructor.
1899 SourceLocation TildeLoc = ConsumeToken();
1900 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001901 // FIXME: Inaccurate.
1902 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001903 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00001904 TypeResult Type = ParseClassName(EndLoc);
1905 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001906 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00001907 else
1908 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001909 } else {
1910 Diag(Tok, diag::err_expected_class_name);
1911 D.SetIdentifier(0, TildeLoc);
1912 }
1913 goto PastIdentifier;
1914 }
1915
1916 // If we reached this point, token is not identifier and not '~'.
1917
1918 if (afterCXXScope) {
1919 Diag(Tok, diag::err_expected_unqualified_id);
1920 D.SetIdentifier(0, Tok.getLocation());
1921 D.setInvalidType(true);
1922 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001923 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001924 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001925 }
1926
1927 // If we reached this point, we are either in C/ObjC or the token didn't
1928 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001929 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1930 assert(!getLang().CPlusPlus &&
1931 "There's a C++-specific check for tok::identifier above");
1932 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1933 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1934 ConsumeToken();
1935 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001936 // direct-declarator: '(' declarator ')'
1937 // direct-declarator: '(' attributes declarator ')'
1938 // Example: 'char (*X)' or 'int (*XX)(void)'
1939 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001940 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001941 // This could be something simple like "int" (in which case the declarator
1942 // portion is empty), if an abstract-declarator is allowed.
1943 D.SetIdentifier(0, Tok.getLocation());
1944 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001945 if (D.getContext() == Declarator::MemberContext)
1946 Diag(Tok, diag::err_expected_member_name_or_semi)
1947 << D.getDeclSpec().getSourceRange();
1948 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001949 Diag(Tok, diag::err_expected_unqualified_id);
1950 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001951 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001952 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001953 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001954 }
1955
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001956 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001957 assert(D.isPastIdentifier() &&
1958 "Haven't past the location of the identifier yet?");
1959
1960 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001961 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001962 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1963 // In such a case, check if we actually have a function declarator; if it
1964 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001965 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1966 // When not in file scope, warn for ambiguous function declarators, just
1967 // in case the author intended it as a variable definition.
1968 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1969 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1970 break;
1971 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001972 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001973 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001974 ParseBracketDeclarator(D);
1975 } else {
1976 break;
1977 }
1978 }
1979}
1980
Chris Lattnera0d056d2008-04-06 05:45:57 +00001981/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1982/// only called before the identifier, so these are most likely just grouping
1983/// parens for precedence. If we find that these are actually function
1984/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1985///
1986/// direct-declarator:
1987/// '(' declarator ')'
1988/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001989/// direct-declarator '(' parameter-type-list ')'
1990/// direct-declarator '(' identifier-list[opt] ')'
1991/// [GNU] direct-declarator '(' parameter-forward-declarations
1992/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001993///
1994void Parser::ParseParenDeclarator(Declarator &D) {
1995 SourceLocation StartLoc = ConsumeParen();
1996 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1997
Chris Lattner1f185292008-10-20 02:05:46 +00001998 // Eat any attributes before we look at whether this is a grouping or function
1999 // declarator paren. If this is a grouping paren, the attribute applies to
2000 // the type being built up, for example:
2001 // int (__attribute__(()) *x)(long y)
2002 // If this ends up not being a grouping paren, the attribute applies to the
2003 // first argument, for example:
2004 // int (__attribute__(()) int x)
2005 // In either case, we need to eat any attributes to be able to determine what
2006 // sort of paren this is.
2007 //
2008 AttributeList *AttrList = 0;
2009 bool RequiresArg = false;
2010 if (Tok.is(tok::kw___attribute)) {
2011 AttrList = ParseAttributes();
2012
2013 // We require that the argument list (if this is a non-grouping paren) be
2014 // present even if the attribute list was empty.
2015 RequiresArg = true;
2016 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002017 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002018 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2019 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002020 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002021
Chris Lattnera0d056d2008-04-06 05:45:57 +00002022 // If we haven't past the identifier yet (or where the identifier would be
2023 // stored, if this is an abstract declarator), then this is probably just
2024 // grouping parens. However, if this could be an abstract-declarator, then
2025 // this could also be the start of function arguments (consider 'void()').
2026 bool isGrouping;
2027
2028 if (!D.mayOmitIdentifier()) {
2029 // If this can't be an abstract-declarator, this *must* be a grouping
2030 // paren, because we haven't seen the identifier yet.
2031 isGrouping = true;
2032 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002033 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002034 isDeclarationSpecifier()) { // 'int(int)' is a function.
2035 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2036 // considered to be a type, not a K&R identifier-list.
2037 isGrouping = false;
2038 } else {
2039 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2040 isGrouping = true;
2041 }
2042
2043 // If this is a grouping paren, handle:
2044 // direct-declarator: '(' declarator ')'
2045 // direct-declarator: '(' attributes declarator ')'
2046 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002047 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002048 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002049 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002050 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002051
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002052 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002053 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002054 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002055
2056 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002057 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002058 return;
2059 }
2060
2061 // Okay, if this wasn't a grouping paren, it must be the start of a function
2062 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002063 // identifier (and remember where it would have been), then call into
2064 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002065 D.SetIdentifier(0, Tok.getLocation());
2066
Chris Lattner1f185292008-10-20 02:05:46 +00002067 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002068}
2069
2070/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2071/// declarator D up to a paren, which indicates that we are parsing function
2072/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002073///
Chris Lattner1f185292008-10-20 02:05:46 +00002074/// If AttrList is non-null, then the caller parsed those arguments immediately
2075/// after the open paren - they should be considered to be the first argument of
2076/// a parameter. If RequiresArg is true, then the first argument of the
2077/// function is required to be present and required to not be an identifier
2078/// list.
2079///
Chris Lattner4b009652007-07-25 00:24:17 +00002080/// This method also handles this portion of the grammar:
2081/// parameter-type-list: [C99 6.7.5]
2082/// parameter-list
2083/// parameter-list ',' '...'
2084///
2085/// parameter-list: [C99 6.7.5]
2086/// parameter-declaration
2087/// parameter-list ',' parameter-declaration
2088///
2089/// parameter-declaration: [C99 6.7.5]
2090/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002091/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002092/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002093/// declaration-specifiers abstract-declarator[opt]
2094/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002095/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002096/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2097///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002098/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002099/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002100///
Chris Lattner1f185292008-10-20 02:05:46 +00002101void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2102 AttributeList *AttrList,
2103 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002104 // lparen is already consumed!
2105 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002106
Chris Lattner1f185292008-10-20 02:05:46 +00002107 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002108 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002109 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002110 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002111 delete AttrList;
2112 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002113
Sebastian Redl0c986032009-02-09 18:23:29 +00002114 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002115
2116 // cv-qualifier-seq[opt].
2117 DeclSpec DS;
2118 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002119 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002120 if (!DS.getSourceRange().getEnd().isInvalid())
2121 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002122
2123 // Parse exception-specification[opt].
2124 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002125 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002126 }
2127
Chris Lattner9f7564b2008-04-06 06:57:35 +00002128 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002129 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002130 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002131 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002132 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002133 /*arglist*/ 0, 0,
2134 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002135 LParenLoc, D),
2136 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002137 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002138 }
2139
2140 // Alternatively, this parameter list may be an identifier list form for a
2141 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002142 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002143 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002144 // K&R identifier lists can't have typedefs as identifiers, per
2145 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002146 if (RequiresArg) {
2147 Diag(Tok, diag::err_argument_required_after_attribute);
2148 delete AttrList;
2149 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002150 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2151 // normal declarators, not for abstract-declarators.
2152 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002153 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002154 }
2155
2156 // Finally, a normal, non-empty parameter type list.
2157
2158 // Build up an array of information about the parsed arguments.
2159 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002160
2161 // Enter function-declaration scope, limiting any declarators to the
2162 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002163 ParseScope PrototypeScope(this,
2164 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002165
2166 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002167 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002168 while (1) {
2169 if (Tok.is(tok::ellipsis)) {
2170 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002171 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002172 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002173 }
2174
Chris Lattner9f7564b2008-04-06 06:57:35 +00002175 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002176
Chris Lattner9f7564b2008-04-06 06:57:35 +00002177 // Parse the declaration-specifiers.
2178 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002179
2180 // If the caller parsed attributes for the first argument, add them now.
2181 if (AttrList) {
2182 DS.AddAttributes(AttrList);
2183 AttrList = 0; // Only apply the attributes to the first parameter.
2184 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002185 ParseDeclarationSpecifiers(DS);
2186
Chris Lattner9f7564b2008-04-06 06:57:35 +00002187 // Parse the declarator. This is "PrototypeContext", because we must
2188 // accept either 'declarator' or 'abstract-declarator' here.
2189 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2190 ParseDeclarator(ParmDecl);
2191
2192 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002193 if (Tok.is(tok::kw___attribute)) {
2194 SourceLocation Loc;
2195 AttributeList *AttrList = ParseAttributes(&Loc);
2196 ParmDecl.AddAttributes(AttrList, Loc);
2197 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002198
Chris Lattner9f7564b2008-04-06 06:57:35 +00002199 // Remember this parsed parameter in ParamInfo.
2200 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2201
Douglas Gregor605de8d2008-12-16 21:30:33 +00002202 // DefArgToks is used when the parsing of default arguments needs
2203 // to be delayed.
2204 CachedTokens *DefArgToks = 0;
2205
Chris Lattner9f7564b2008-04-06 06:57:35 +00002206 // If no parameter was specified, verify that *something* was specified,
2207 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002208 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2209 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002210 // Completely missing, emit error.
2211 Diag(DSStart, diag::err_missing_param);
2212 } else {
2213 // Otherwise, we have something. Add it and let semantic analysis try
2214 // to grok it and add the result to the ParamInfo we are building.
2215
2216 // Inform the actions module about the parameter declarator, so it gets
2217 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002218 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002219
2220 // Parse the default argument, if any. We parse the default
2221 // arguments in all dialects; the semantic analysis in
2222 // ActOnParamDefaultArgument will reject the default argument in
2223 // C.
2224 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002225 SourceLocation EqualLoc = Tok.getLocation();
2226
Chris Lattner3e254fb2008-04-08 04:40:51 +00002227 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002228 if (D.getContext() == Declarator::MemberContext) {
2229 // If we're inside a class definition, cache the tokens
2230 // corresponding to the default argument. We'll actually parse
2231 // them when we see the end of the class definition.
2232 // FIXME: Templates will require something similar.
2233 // FIXME: Can we use a smart pointer for Toks?
2234 DefArgToks = new CachedTokens;
2235
2236 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2237 tok::semi, false)) {
2238 delete DefArgToks;
2239 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002240 Actions.ActOnParamDefaultArgumentError(Param);
2241 } else
2242 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002243 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002244 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002245 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002246
2247 OwningExprResult DefArgResult(ParseAssignmentExpression());
2248 if (DefArgResult.isInvalid()) {
2249 Actions.ActOnParamDefaultArgumentError(Param);
2250 SkipUntil(tok::comma, tok::r_paren, true, true);
2251 } else {
2252 // Inform the actions module about the default argument
2253 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002254 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002255 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002256 }
2257 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002258
2259 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002260 ParmDecl.getIdentifierLoc(), Param,
2261 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002262 }
2263
2264 // If the next token is a comma, consume it and keep reading arguments.
2265 if (Tok.isNot(tok::comma)) break;
2266
2267 // Consume the comma.
2268 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002269 }
2270
Chris Lattner9f7564b2008-04-06 06:57:35 +00002271 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002272 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002273
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002274 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002275 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002276
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002277 DeclSpec DS;
2278 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002279 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002280 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002281 if (!DS.getSourceRange().getEnd().isInvalid())
2282 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002283
2284 // Parse exception-specification[opt].
2285 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002286 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002287 }
2288
Chris Lattner4b009652007-07-25 00:24:17 +00002289 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002290 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002291 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002292 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002293 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002294 LParenLoc, D),
2295 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002296}
2297
Chris Lattner35d9c912008-04-06 06:34:08 +00002298/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2299/// we found a K&R-style identifier list instead of a type argument list. The
2300/// current token is known to be the first identifier in the list.
2301///
2302/// identifier-list: [C99 6.7.5]
2303/// identifier
2304/// identifier-list ',' identifier
2305///
2306void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2307 Declarator &D) {
2308 // Build up an array of information about the parsed arguments.
2309 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2310 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2311
2312 // If there was no identifier specified for the declarator, either we are in
2313 // an abstract-declarator, or we are in a parameter declarator which was found
2314 // to be abstract. In abstract-declarators, identifier lists are not valid:
2315 // diagnose this.
2316 if (!D.getIdentifier())
2317 Diag(Tok, diag::ext_ident_list_in_param);
2318
2319 // Tok is known to be the first identifier in the list. Remember this
2320 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002321 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002322 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002323 Tok.getLocation(),
2324 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002325
Chris Lattner113a56b2008-04-06 06:39:19 +00002326 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002327
2328 while (Tok.is(tok::comma)) {
2329 // Eat the comma.
2330 ConsumeToken();
2331
Chris Lattner113a56b2008-04-06 06:39:19 +00002332 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002333 if (Tok.isNot(tok::identifier)) {
2334 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002335 SkipUntil(tok::r_paren);
2336 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002337 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002338
Chris Lattner35d9c912008-04-06 06:34:08 +00002339 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002340
2341 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002342 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002343 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002344
2345 // Verify that the argument identifier has not already been mentioned.
2346 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002347 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002348 } else {
2349 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002350 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002351 Tok.getLocation(),
2352 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002353 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002354
2355 // Eat the identifier.
2356 ConsumeToken();
2357 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002358
2359 // If we have the closing ')', eat it and we're done.
2360 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2361
Chris Lattner113a56b2008-04-06 06:39:19 +00002362 // Remember that we parsed a function type, and remember the attributes. This
2363 // function type is always a K&R style function type, which is not varargs and
2364 // has no prototype.
2365 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002366 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002367 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002368 /*TypeQuals*/0, LParenLoc, D),
2369 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002370}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002371
Chris Lattner4b009652007-07-25 00:24:17 +00002372/// [C90] direct-declarator '[' constant-expression[opt] ']'
2373/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2374/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2375/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2376/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2377void Parser::ParseBracketDeclarator(Declarator &D) {
2378 SourceLocation StartLoc = ConsumeBracket();
2379
Chris Lattner1525c3a2008-12-18 07:27:21 +00002380 // C array syntax has many features, but by-far the most common is [] and [4].
2381 // This code does a fast path to handle some of the most obvious cases.
2382 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002383 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002384 // Remember that we parsed the empty array type.
2385 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002386 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2387 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002388 return;
2389 } else if (Tok.getKind() == tok::numeric_constant &&
2390 GetLookAheadToken(1).is(tok::r_square)) {
2391 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002392 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002393 ConsumeToken();
2394
Sebastian Redl0c986032009-02-09 18:23:29 +00002395 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002396
2397 // If there was an error parsing the assignment-expression, recover.
2398 if (ExprRes.isInvalid())
2399 ExprRes.release(); // Deallocate expr, just use [].
2400
2401 // Remember that we parsed a array type, and remember its features.
2402 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002403 ExprRes.release(), StartLoc),
2404 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002405 return;
2406 }
2407
Chris Lattner4b009652007-07-25 00:24:17 +00002408 // If valid, this location is the position where we read the 'static' keyword.
2409 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002410 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002411 StaticLoc = ConsumeToken();
2412
2413 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002414 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002415 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002416 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002417
2418 // If we haven't already read 'static', check to see if there is one after the
2419 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002420 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002421 StaticLoc = ConsumeToken();
2422
2423 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2424 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002425 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002426
2427 // Handle the case where we have '[*]' as the array size. However, a leading
2428 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2429 // the the token after the star is a ']'. Since stars in arrays are
2430 // infrequent, use of lookahead is not costly here.
2431 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002432 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002433
Chris Lattner306d4df2008-12-18 06:50:14 +00002434 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002435 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002436 StaticLoc = SourceLocation(); // Drop the static.
2437 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002438 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002439 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002440 // Note, in C89, this production uses the constant-expr production instead
2441 // of assignment-expr. The only difference is that assignment-expr allows
2442 // things like '=' and '*='. Sema rejects these in C89 mode because they
2443 // are not i-c-e's, so we don't need to distinguish between the two here.
2444
Chris Lattner4b009652007-07-25 00:24:17 +00002445 // Parse the assignment-expression now.
2446 NumElements = ParseAssignmentExpression();
2447 }
2448
2449 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002450 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002451 // If the expression was invalid, skip it.
2452 SkipUntil(tok::r_square);
2453 return;
2454 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002455
2456 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2457
Chris Lattner1525c3a2008-12-18 07:27:21 +00002458 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002459 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2460 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002461 NumElements.release(), StartLoc),
2462 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002463}
2464
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002465/// [GNU] typeof-specifier:
2466/// typeof ( expressions )
2467/// typeof ( type-name )
2468/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002469///
2470void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002471 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002472 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002473 SourceLocation StartLoc = ConsumeToken();
2474
Chris Lattner34a01ad2007-10-09 17:33:22 +00002475 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002476 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002477 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002478 return;
2479 }
2480
Sebastian Redl14ca7412008-12-11 21:36:32 +00002481 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002482 if (Result.isInvalid()) {
2483 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002484 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002485 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002486
2487 const char *PrevSpec = 0;
2488 // Check for duplicate type specifiers.
2489 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002490 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002491 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002492
2493 // FIXME: Not accurate, the range gets one token more than it should.
2494 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002495 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002496 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002497
Steve Naroff7cbb1462007-07-31 12:34:36 +00002498 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2499
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002500 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002501 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002502
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002503 assert((Ty.isInvalid() || Ty.get()) &&
2504 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002505
Chris Lattner34a01ad2007-10-09 17:33:22 +00002506 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002507 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002508 return;
2509 }
2510 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002511
2512 if (Ty.isInvalid())
2513 DS.SetTypeSpecError();
2514 else {
2515 const char *PrevSpec = 0;
2516 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2517 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2518 Ty.get()))
2519 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2520 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002521 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002522 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002523
2524 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002525 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002526 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002527 return;
2528 }
2529 RParenLoc = ConsumeParen();
2530 const char *PrevSpec = 0;
2531 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2532 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002533 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002534 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002535 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002536 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002537}
2538
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002539