blob: 1fe0d93d1c0272b0ccccb7fe0738314cec62fc3f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000236 case tok::kw_export:
237 case tok::kw_template:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000238 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000248 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000249 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000251 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000252
253 // This routine returns a DeclGroup, if the thing we parsed only contains a
254 // single decl, convert it now.
255 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000256}
257
258/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
259/// declaration-specifiers init-declarator-list[opt] ';'
260///[C90/C++]init-declarator-list ';' [TODO]
261/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000262///
263/// If RequireSemi is false, this does not check for a ';' at the end of the
264/// declaration.
265Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000267 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000275 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 }
279
280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattner2c41d482009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000285
Chris Lattner9802a0a2009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattnerf8016042009-03-29 17:27:48 +0000288 // If the client wants to check what comes after the declaration, just return
289 // immediately without checking anything!
290 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattner2c41d482009-03-29 17:18:04 +0000297 Diag(Tok, diag::err_expected_semi_declation);
298 // Skip to end of block or statement
299 SkipUntil(tok::r_brace, true, true);
300 if (Tok.is(tok::semi))
301 ConsumeToken();
302 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000305
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
307/// parsing 'declaration-specifiers declarator'. This method is split out this
308/// way to handle the ambiguity between top-level function-definitions and
309/// declarations.
310///
Chris Lattner4b009652007-07-25 00:24:17 +0000311/// init-declarator-list: [C99 6.7]
312/// init-declarator
313/// init-declarator-list ',' init-declarator
314/// init-declarator: [C99 6.7]
315/// declarator
316/// declarator '=' initializer
317/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
318/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000319/// [C++] declarator initializer[opt]
320///
321/// [C++] initializer:
322/// [C++] '=' initializer-clause
323/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000324/// [C++0x] '=' 'default' [TODO]
325/// [C++0x] '=' 'delete'
326///
327/// According to the standard grammar, =default and =delete are function
328/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000329///
Chris Lattnera17991f2009-03-29 16:50:03 +0000330Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000331ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000332 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
333 // that we parse together here.
334 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000335
336 // At this point, we know that it is not a function definition. Parse the
337 // rest of the init-declarator-list.
338 while (1) {
339 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000340 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000341 SourceLocation Loc;
342 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000343 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000344 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000345 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000346 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000347
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000348 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000349 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000350 }
Chris Lattner4b009652007-07-25 00:24:17 +0000351
352 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000353 if (Tok.is(tok::kw___attribute)) {
354 SourceLocation Loc;
355 AttributeList *AttrList = ParseAttributes(&Loc);
356 D.AddAttributes(AttrList, Loc);
357 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000358
359 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000360 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
361 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000362
Chris Lattner4b009652007-07-25 00:24:17 +0000363 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000364 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000365 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000366 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
367 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 } else {
370 OwningExprResult Init(ParseInitializer());
371 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000372 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000373 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000374 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000375 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000376 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000377 } else if (Tok.is(tok::l_paren)) {
378 // Parse C++ direct initializer: '(' expression-list ')'
379 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000380 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000381 CommaLocsTy CommaLocs;
382
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000383 if (ParseExpressionList(Exprs, CommaLocs)) {
384 SkipUntil(tok::r_paren);
Chris Lattner1b8e26c2009-04-12 22:23:27 +0000385 } else {
386 // Match the ')'.
387 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000388
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000389 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
390 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000391 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000392 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000393 &CommaLocs[0], RParenLoc);
394 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000395 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000396 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000397 }
398
Chris Lattner4b009652007-07-25 00:24:17 +0000399 // If we don't have a comma, it is either the end of the list (a ';') or an
400 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000401 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000402 break;
403
404 // Consume the comma.
405 ConsumeToken();
406
407 // Parse the next declarator.
408 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000409
410 // Accept attributes in an init-declarator. In the first declarator in a
411 // declaration, these would be part of the declspec. In subsequent
412 // declarators, they become part of the declarator itself, so that they
413 // don't apply to declarators after *this* one. Examples:
414 // short __attribute__((common)) var; -> declspec
415 // short var __attribute__((common)); -> declarator
416 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000417 if (Tok.is(tok::kw___attribute)) {
418 SourceLocation Loc;
419 AttributeList *AttrList = ParseAttributes(&Loc);
420 D.AddAttributes(AttrList, Loc);
421 }
Chris Lattner926cf542008-10-20 04:57:38 +0000422
Chris Lattner4b009652007-07-25 00:24:17 +0000423 ParseDeclarator(D);
424 }
425
Chris Lattner2c41d482009-03-29 17:18:04 +0000426 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
427 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000428}
429
430/// ParseSpecifierQualifierList
431/// specifier-qualifier-list:
432/// type-specifier specifier-qualifier-list[opt]
433/// type-qualifier specifier-qualifier-list[opt]
434/// [GNU] attributes specifier-qualifier-list[opt]
435///
436void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
437 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
438 /// parse declaration-specifiers and complain about extra stuff.
439 ParseDeclarationSpecifiers(DS);
440
441 // Validate declspec for type-name.
442 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000443 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000444 Diag(Tok, diag::err_typename_requires_specqual);
445
446 // Issue diagnostic and remove storage class if present.
447 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
448 if (DS.getStorageClassSpecLoc().isValid())
449 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
450 else
451 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
452 DS.ClearStorageClassSpecs();
453 }
454
455 // Issue diagnostic and remove function specfier if present.
456 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000457 if (DS.isInlineSpecified())
458 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isVirtualSpecified())
460 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
461 if (DS.isExplicitSpecified())
462 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000463 DS.ClearFunctionSpecs();
464 }
465}
466
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000467/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
468/// specified token is valid after the identifier in a declarator which
469/// immediately follows the declspec. For example, these things are valid:
470///
471/// int x [ 4]; // direct-declarator
472/// int x ( int y); // direct-declarator
473/// int(int x ) // direct-declarator
474/// int x ; // simple-declaration
475/// int x = 17; // init-declarator-list
476/// int x , y; // init-declarator-list
477/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerca6cc362009-04-12 22:29:43 +0000478/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000479///
480/// This is not, because 'x' does not immediately follow the declspec (though
481/// ')' happens to be valid anyway).
482/// int (x)
483///
484static bool isValidAfterIdentifierInDeclarator(const Token &T) {
485 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
486 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerca6cc362009-04-12 22:29:43 +0000487 T.is(tok::kw_asm) || T.is(tok::l_brace);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000488}
489
Chris Lattner4b009652007-07-25 00:24:17 +0000490/// ParseDeclarationSpecifiers
491/// declaration-specifiers: [C99 6.7]
492/// storage-class-specifier declaration-specifiers[opt]
493/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000494/// [C99] function-specifier declaration-specifiers[opt]
495/// [GNU] attributes declaration-specifiers[opt]
496///
497/// storage-class-specifier: [C99 6.7.1]
498/// 'typedef'
499/// 'extern'
500/// 'static'
501/// 'auto'
502/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000503/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000504/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000505/// function-specifier: [C99 6.7.4]
506/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000507/// [C++] 'virtual'
508/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000509///
Douglas Gregor52473432008-12-24 02:52:09 +0000510void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000511 TemplateParameterLists *TemplateParams,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000512 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000513 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000514 while (1) {
515 int isInvalid = false;
516 const char *PrevSpec = 0;
517 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000518
Chris Lattner4b009652007-07-25 00:24:17 +0000519 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000520 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000521 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000522 // If this is not a declaration specifier token, we're done reading decl
523 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000524 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000525 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000526
527 case tok::coloncolon: // ::foo::bar
528 // Annotate C++ scope specifiers. If we get one, loop.
529 if (TryAnnotateCXXScopeToken())
530 continue;
531 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000532
533 case tok::annot_cxxscope: {
534 if (DS.hasTypeSpecifier())
535 goto DoneWithDeclSpec;
536
537 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000538 Token Next = NextToken();
539 if (Next.is(tok::annot_template_id) &&
540 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000541 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000542 // We have a qualified template-id, e.g., N::A<int>
543 CXXScopeSpec SS;
544 ParseOptionalCXXScopeSpecifier(SS);
545 assert(Tok.is(tok::annot_template_id) &&
546 "ParseOptionalCXXScopeSpecifier not working");
547 AnnotateTemplateIdTokenAsType(&SS);
548 continue;
549 }
550
551 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000552 goto DoneWithDeclSpec;
553
554 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000555 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000556 SS.setRange(Tok.getAnnotationRange());
557
558 // If the next token is the name of the class type that the C++ scope
559 // denotes, followed by a '(', then this is a constructor declaration.
560 // We're done with the decl-specifiers.
561 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
562 CurScope, &SS) &&
563 GetLookAheadToken(2).is(tok::l_paren))
564 goto DoneWithDeclSpec;
565
Douglas Gregor1075a162009-02-04 17:00:24 +0000566 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
567 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000568
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000569 if (TypeRep == 0)
570 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000571
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000572 ConsumeToken(); // The C++ scope.
573
Douglas Gregora60c62e2009-02-09 15:09:02 +0000574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000575 TypeRep);
576 if (isInvalid)
577 break;
578
579 DS.SetRangeEnd(Tok.getLocation());
580 ConsumeToken(); // The typename.
581
582 continue;
583 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000584
585 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000586 if (Tok.getAnnotationValue())
587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
588 Tok.getAnnotationValue());
589 else
590 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000591 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
592 ConsumeToken(); // The typename
593
594 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
595 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
596 // Objective-C interface. If we don't have Objective-C or a '<', this is
597 // just a normal reference to a typedef name.
598 if (!Tok.is(tok::less) || !getLang().ObjC1)
599 continue;
600
601 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000602 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000603 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
604 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
605
606 DS.SetRangeEnd(EndProtoLoc);
607 continue;
608 }
609
Chris Lattnerfda18db2008-07-26 01:18:38 +0000610 // typedef-name
611 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000612 // In C++, check to see if this is a scope specifier like foo::bar::, if
613 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000614 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
615 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000616
Chris Lattnerfda18db2008-07-26 01:18:38 +0000617 // This identifier can only be a typedef name if we haven't already seen
618 // a type-specifier. Without this check we misparse:
619 // typedef int X; struct Y { short X; }; as 'short int'.
620 if (DS.hasTypeSpecifier())
621 goto DoneWithDeclSpec;
622
623 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000624 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
625 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000626
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000627 // If this is not a typedef name, don't parse it as part of the declspec,
628 // it must be an implicit int or an error.
629 if (TypeRep == 0) {
630 // If we see an identifier that is not a type name, we normally would
631 // parse it as the identifer being declared. However, when a typename
632 // is typo'd or the definition is not included, this will incorrectly
633 // parse the typename as the identifier name and fall over misparsing
634 // later parts of the diagnostic.
635 //
636 // As such, we try to do some look-ahead in cases where this would
637 // otherwise be an "implicit-int" case to see if this is invalid. For
638 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
639 // an identifier with implicit int, we'd get a parse error because the
640 // next token is obviously invalid for a type. Parse these as a case
641 // with an invalid type specifier.
642 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
643
644 // Since we know that this either implicit int (which is rare) or an
645 // error, we'd do lookahead to try to do better recovery.
646 if (isValidAfterIdentifierInDeclarator(NextToken())) {
647 // If this token is valid for implicit int, e.g. "static x = 4", then
648 // we just avoid eating the identifier, so it will be parsed as the
649 // identifier in the declarator.
650 goto DoneWithDeclSpec;
651 }
652
653 // Otherwise, if we don't consume this token, we are going to emit an
Chris Lattner197b4342009-04-12 21:49:30 +0000654 // error anyway. Try to recover from various common problems. Check
655 // to see if this was a reference to a tag name without a tag specified.
Chris Lattner4a2e8b22009-04-12 22:30:22 +0000656 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner197b4342009-04-12 21:49:30 +0000657 const char *TagName = 0;
658 tok::TokenKind TagKind = tok::unknown;
659
660 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
661 default: break;
662 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
663 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
664 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
665 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
666 }
667 if (TagName) {
668 Diag(Loc, diag::err_use_of_tag_name_without_tag)
669 << Tok.getIdentifierInfo() << TagName
670 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
671
672 // Parse this as a tag as if the missing tag were present.
673 if (TagKind == tok::kw_enum)
674 ParseEnumSpecifier(Loc, DS, AS);
675 else
676 ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
677 continue;
678 }
679
680 // Since this is almost certainly an invalid type name, emit a
Chris Lattner85066912009-04-12 22:12:26 +0000681 // diagnostic that says it, eat the token, and mark the declspec as
682 // invalid.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000683 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo();
Chris Lattner85066912009-04-12 22:12:26 +0000684 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000685 DS.SetRangeEnd(Tok.getLocation());
686 ConsumeToken();
687
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000688 // TODO: Could inject an invalid typedef decl in an enclosing scope to
689 // avoid rippling error messages on subsequent uses of the same type,
690 // could be useful if #include was forgotten.
691
Chris Lattnerfda18db2008-07-26 01:18:38 +0000692 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000693 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000694
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000695 // C++: If the identifier is actually the name of the class type
696 // being defined and the next token is a '(', then this is a
697 // constructor declaration. We're done with the decl-specifiers
698 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000699 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000700 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
701 NextToken().getKind() == tok::l_paren)
702 goto DoneWithDeclSpec;
703
Douglas Gregora60c62e2009-02-09 15:09:02 +0000704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000705 TypeRep);
706 if (isInvalid)
707 break;
708
709 DS.SetRangeEnd(Tok.getLocation());
710 ConsumeToken(); // The identifier
711
712 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
713 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
714 // Objective-C interface. If we don't have Objective-C or a '<', this is
715 // just a normal reference to a typedef name.
716 if (!Tok.is(tok::less) || !getLang().ObjC1)
717 continue;
718
719 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000720 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000721 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000722 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000723
724 DS.SetRangeEnd(EndProtoLoc);
725
Steve Narofff7683302008-09-22 10:28:57 +0000726 // Need to support trailing type qualifiers (e.g. "id<p> const").
727 // If a type specifier follows, it will be diagnosed elsewhere.
728 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000729 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000730
731 // type-name
732 case tok::annot_template_id: {
733 TemplateIdAnnotation *TemplateId
734 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000735 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000736 // This template-id does not refer to a type name, so we're
737 // done with the type-specifiers.
738 goto DoneWithDeclSpec;
739 }
740
741 // Turn the template-id annotation token into a type annotation
742 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000743 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000744 continue;
745 }
746
Chris Lattner4b009652007-07-25 00:24:17 +0000747 // GNU attributes support.
748 case tok::kw___attribute:
749 DS.AddAttributes(ParseAttributes());
750 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000751
752 // Microsoft declspec support.
753 case tok::kw___declspec:
754 if (!PP.getLangOptions().Microsoft)
755 goto DoneWithDeclSpec;
756 FuzzyParseMicrosoftDeclSpec();
757 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000758
Steve Naroffedd04d52008-12-25 14:16:32 +0000759 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000760 case tok::kw___forceinline:
761 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000762 case tok::kw___cdecl:
763 case tok::kw___stdcall:
764 case tok::kw___fastcall:
765 if (!PP.getLangOptions().Microsoft)
766 goto DoneWithDeclSpec;
767 // Just ignore it.
768 break;
769
Chris Lattner4b009652007-07-25 00:24:17 +0000770 // storage-class-specifier
771 case tok::kw_typedef:
772 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
773 break;
774 case tok::kw_extern:
775 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000776 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000777 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
778 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000779 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000780 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
781 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000782 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000783 case tok::kw_static:
784 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000785 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000786 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
787 break;
788 case tok::kw_auto:
789 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
790 break;
791 case tok::kw_register:
792 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
793 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000794 case tok::kw_mutable:
795 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
796 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000797 case tok::kw___thread:
798 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
799 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000800
Chris Lattner4b009652007-07-25 00:24:17 +0000801 // function-specifier
802 case tok::kw_inline:
803 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
804 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000805 case tok::kw_virtual:
806 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
807 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000808 case tok::kw_explicit:
809 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
810 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000811
812 // type-specifier
813 case tok::kw_short:
814 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
815 break;
816 case tok::kw_long:
817 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
818 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
819 else
820 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
821 break;
822 case tok::kw_signed:
823 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
824 break;
825 case tok::kw_unsigned:
826 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
827 break;
828 case tok::kw__Complex:
829 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
830 break;
831 case tok::kw__Imaginary:
832 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
833 break;
834 case tok::kw_void:
835 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
836 break;
837 case tok::kw_char:
838 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
839 break;
840 case tok::kw_int:
841 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
842 break;
843 case tok::kw_float:
844 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
845 break;
846 case tok::kw_double:
847 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
848 break;
849 case tok::kw_wchar_t:
850 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
851 break;
852 case tok::kw_bool:
853 case tok::kw__Bool:
854 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
855 break;
856 case tok::kw__Decimal32:
857 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
858 break;
859 case tok::kw__Decimal64:
860 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
861 break;
862 case tok::kw__Decimal128:
863 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
864 break;
865
866 // class-specifier:
867 case tok::kw_class:
868 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000869 case tok::kw_union: {
870 tok::TokenKind Kind = Tok.getKind();
871 ConsumeToken();
872 ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000873 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000874 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000875
876 // enum-specifier:
877 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000878 ConsumeToken();
879 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000880 continue;
881
882 // cv-qualifier:
883 case tok::kw_const:
884 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
885 break;
886 case tok::kw_volatile:
887 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
888 getLang())*2;
889 break;
890 case tok::kw_restrict:
891 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
892 getLang())*2;
893 break;
894
Douglas Gregord3022602009-03-27 23:10:48 +0000895 // C++ typename-specifier:
896 case tok::kw_typename:
897 if (TryAnnotateTypeOrScopeToken())
898 continue;
899 break;
900
Chris Lattnerc297b722009-01-21 19:48:37 +0000901 // GNU typeof support.
902 case tok::kw_typeof:
903 ParseTypeofSpecifier(DS);
904 continue;
905
Steve Naroff5f0466b2008-06-05 00:02:44 +0000906 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000907 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000908 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
909 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000910 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000911 goto DoneWithDeclSpec;
912
913 {
914 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000915 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000916 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000917 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000918 DS.SetRangeEnd(EndProtoLoc);
919
Chris Lattnerf006a222008-11-18 07:48:38 +0000920 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000921 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000922 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000923 // Need to support trailing type qualifiers (e.g. "id<p> const").
924 // If a type specifier follows, it will be diagnosed elsewhere.
925 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000926 }
Chris Lattner4b009652007-07-25 00:24:17 +0000927 }
928 // If the specifier combination wasn't legal, issue a diagnostic.
929 if (isInvalid) {
930 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000931 // Pick between error or extwarn.
932 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
933 : diag::ext_duplicate_declspec;
934 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000935 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000936 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000937 ConsumeToken();
938 }
939}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000940
Chris Lattnerd706dc82009-01-06 06:59:53 +0000941/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000942/// primarily follow the C++ grammar with additions for C99 and GNU,
943/// which together subsume the C grammar. Note that the C++
944/// type-specifier also includes the C type-qualifier (for const,
945/// volatile, and C99 restrict). Returns true if a type-specifier was
946/// found (and parsed), false otherwise.
947///
948/// type-specifier: [C++ 7.1.5]
949/// simple-type-specifier
950/// class-specifier
951/// enum-specifier
952/// elaborated-type-specifier [TODO]
953/// cv-qualifier
954///
955/// cv-qualifier: [C++ 7.1.5.1]
956/// 'const'
957/// 'volatile'
958/// [C99] 'restrict'
959///
960/// simple-type-specifier: [ C++ 7.1.5.2]
961/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
962/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
963/// 'char'
964/// 'wchar_t'
965/// 'bool'
966/// 'short'
967/// 'int'
968/// 'long'
969/// 'signed'
970/// 'unsigned'
971/// 'float'
972/// 'double'
973/// 'void'
974/// [C99] '_Bool'
975/// [C99] '_Complex'
976/// [C99] '_Imaginary' // Removed in TC2?
977/// [GNU] '_Decimal32'
978/// [GNU] '_Decimal64'
979/// [GNU] '_Decimal128'
980/// [GNU] typeof-specifier
981/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
982/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000983bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
984 const char *&PrevSpec,
985 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000986 SourceLocation Loc = Tok.getLocation();
987
988 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000989 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000990 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000991 // Annotate typenames and C++ scope specifiers. If we get one, just
992 // recurse to handle whatever we get.
993 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000994 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000995 // Otherwise, not a type specifier.
996 return false;
997 case tok::coloncolon: // ::foo::bar
998 if (NextToken().is(tok::kw_new) || // ::new
999 NextToken().is(tok::kw_delete)) // ::delete
1000 return false;
1001
1002 // Annotate typenames and C++ scope specifiers. If we get one, just
1003 // recurse to handle whatever we get.
1004 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001005 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001006 // Otherwise, not a type specifier.
1007 return false;
1008
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001009 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001010 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001011 if (Tok.getAnnotationValue())
1012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1013 Tok.getAnnotationValue());
1014 else
1015 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001016 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1017 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001018
1019 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1020 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1021 // Objective-C interface. If we don't have Objective-C or a '<', this is
1022 // just a normal reference to a typedef name.
1023 if (!Tok.is(tok::less) || !getLang().ObjC1)
1024 return true;
1025
1026 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001027 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001028 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1029 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1030
1031 DS.SetRangeEnd(EndProtoLoc);
1032 return true;
1033 }
1034
1035 case tok::kw_short:
1036 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1037 break;
1038 case tok::kw_long:
1039 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1040 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1041 else
1042 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1043 break;
1044 case tok::kw_signed:
1045 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1046 break;
1047 case tok::kw_unsigned:
1048 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1049 break;
1050 case tok::kw__Complex:
1051 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1052 break;
1053 case tok::kw__Imaginary:
1054 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1055 break;
1056 case tok::kw_void:
1057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1058 break;
1059 case tok::kw_char:
1060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1061 break;
1062 case tok::kw_int:
1063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1064 break;
1065 case tok::kw_float:
1066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1067 break;
1068 case tok::kw_double:
1069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1070 break;
1071 case tok::kw_wchar_t:
1072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1073 break;
1074 case tok::kw_bool:
1075 case tok::kw__Bool:
1076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1077 break;
1078 case tok::kw__Decimal32:
1079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1080 break;
1081 case tok::kw__Decimal64:
1082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1083 break;
1084 case tok::kw__Decimal128:
1085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1086 break;
1087
1088 // class-specifier:
1089 case tok::kw_class:
1090 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001091 case tok::kw_union: {
1092 tok::TokenKind Kind = Tok.getKind();
1093 ConsumeToken();
1094 ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001095 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001096 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001097
1098 // enum-specifier:
1099 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001100 ConsumeToken();
1101 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001102 return true;
1103
1104 // cv-qualifier:
1105 case tok::kw_const:
1106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1107 getLang())*2;
1108 break;
1109 case tok::kw_volatile:
1110 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1111 getLang())*2;
1112 break;
1113 case tok::kw_restrict:
1114 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1115 getLang())*2;
1116 break;
1117
1118 // GNU typeof support.
1119 case tok::kw_typeof:
1120 ParseTypeofSpecifier(DS);
1121 return true;
1122
Steve Naroffedd04d52008-12-25 14:16:32 +00001123 case tok::kw___cdecl:
1124 case tok::kw___stdcall:
1125 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001126 if (!PP.getLangOptions().Microsoft) return false;
1127 ConsumeToken();
1128 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001129
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001130 default:
1131 // Not a type-specifier; do nothing.
1132 return false;
1133 }
1134
1135 // If the specifier combination wasn't legal, issue a diagnostic.
1136 if (isInvalid) {
1137 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001138 // Pick between error or extwarn.
1139 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1140 : diag::ext_duplicate_declspec;
1141 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001142 }
1143 DS.SetRangeEnd(Tok.getLocation());
1144 ConsumeToken(); // whatever we parsed above.
1145 return true;
1146}
Chris Lattner4b009652007-07-25 00:24:17 +00001147
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001148/// ParseStructDeclaration - Parse a struct declaration without the terminating
1149/// semicolon.
1150///
Chris Lattner4b009652007-07-25 00:24:17 +00001151/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001152/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001153/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001154/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001155/// struct-declarator-list:
1156/// struct-declarator
1157/// struct-declarator-list ',' struct-declarator
1158/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1159/// struct-declarator:
1160/// declarator
1161/// [GNU] declarator attributes[opt]
1162/// declarator[opt] ':' constant-expression
1163/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1164///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001165void Parser::
1166ParseStructDeclaration(DeclSpec &DS,
1167 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001168 if (Tok.is(tok::kw___extension__)) {
1169 // __extension__ silences extension warnings in the subexpression.
1170 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001171 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001172 return ParseStructDeclaration(DS, Fields);
1173 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001174
1175 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001176 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001177 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001178
Douglas Gregorb748fc52009-01-12 22:49:06 +00001179 // If there are no declarators, this is a free-standing declaration
1180 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001181 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001182 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001183 return;
1184 }
1185
1186 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001187 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001188 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001189 FieldDeclarator &DeclaratorInfo = Fields.back();
1190
Steve Naroffa9adf112007-08-20 22:28:22 +00001191 /// struct-declarator: declarator
1192 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001193 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001194 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001195
Chris Lattner34a01ad2007-10-09 17:33:22 +00001196 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001197 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001198 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001199 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001200 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001201 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001202 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001203 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001204
Steve Naroffa9adf112007-08-20 22:28:22 +00001205 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001206 if (Tok.is(tok::kw___attribute)) {
1207 SourceLocation Loc;
1208 AttributeList *AttrList = ParseAttributes(&Loc);
1209 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1210 }
1211
Steve Naroffa9adf112007-08-20 22:28:22 +00001212 // If we don't have a comma, it is either the end of the list (a ';')
1213 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001214 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001215 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001216
Steve Naroffa9adf112007-08-20 22:28:22 +00001217 // Consume the comma.
1218 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001219
Steve Naroffa9adf112007-08-20 22:28:22 +00001220 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001221 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001222
Steve Naroffa9adf112007-08-20 22:28:22 +00001223 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001224 if (Tok.is(tok::kw___attribute)) {
1225 SourceLocation Loc;
1226 AttributeList *AttrList = ParseAttributes(&Loc);
1227 Fields.back().D.AddAttributes(AttrList, Loc);
1228 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001229 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001230}
1231
1232/// ParseStructUnionBody
1233/// struct-contents:
1234/// struct-declaration-list
1235/// [EXT] empty
1236/// [GNU] "struct-declaration-list" without terminatoring ';'
1237/// struct-declaration-list:
1238/// struct-declaration
1239/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001240/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001241///
Chris Lattner4b009652007-07-25 00:24:17 +00001242void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001243 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001244 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1245 PP.getSourceManager(),
1246 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001247
Chris Lattner4b009652007-07-25 00:24:17 +00001248 SourceLocation LBraceLoc = ConsumeBrace();
1249
Douglas Gregorcab994d2009-01-09 22:42:13 +00001250 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001251 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1252
Chris Lattner4b009652007-07-25 00:24:17 +00001253 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1254 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001255 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001256 Diag(Tok, diag::ext_empty_struct_union_enum)
1257 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001258
Chris Lattner5261d0c2009-03-28 19:18:32 +00001259 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001260 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1261
Chris Lattner4b009652007-07-25 00:24:17 +00001262 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001263 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001264 // Each iteration of this loop reads one struct-declaration.
1265
1266 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001267 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001268 Diag(Tok, diag::ext_extra_struct_semi)
1269 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001270 ConsumeToken();
1271 continue;
1272 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001273
1274 // Parse all the comma separated declarators.
1275 DeclSpec DS;
1276 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001277 if (!Tok.is(tok::at)) {
1278 ParseStructDeclaration(DS, FieldDeclarators);
1279
1280 // Convert them all to fields.
1281 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1282 FieldDeclarator &FD = FieldDeclarators[i];
1283 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001284 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1285 DS.getSourceRange().getBegin(),
1286 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001287 FieldDecls.push_back(Field);
1288 }
1289 } else { // Handle @defs
1290 ConsumeToken();
1291 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1292 Diag(Tok, diag::err_unexpected_at);
1293 SkipUntil(tok::semi, true, true);
1294 continue;
1295 }
1296 ConsumeToken();
1297 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1298 if (!Tok.is(tok::identifier)) {
1299 Diag(Tok, diag::err_expected_ident);
1300 SkipUntil(tok::semi, true, true);
1301 continue;
1302 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001303 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001304 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1305 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001306 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1307 ConsumeToken();
1308 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1309 }
Chris Lattner4b009652007-07-25 00:24:17 +00001310
Chris Lattner34a01ad2007-10-09 17:33:22 +00001311 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001312 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001313 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001314 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001315 break;
1316 } else {
1317 Diag(Tok, diag::err_expected_semi_decl_list);
1318 // Skip to end of block or statement
1319 SkipUntil(tok::r_brace, true, true);
1320 }
1321 }
1322
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001323 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001324
Chris Lattner4b009652007-07-25 00:24:17 +00001325 AttributeList *AttrList = 0;
1326 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001327 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001328 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001329
1330 Actions.ActOnFields(CurScope,
1331 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1332 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001333 AttrList);
1334 StructScope.Exit();
1335 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001336}
1337
1338
1339/// ParseEnumSpecifier
1340/// enum-specifier: [C99 6.7.2.2]
1341/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001342///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001343/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1344/// '}' attributes[opt]
1345/// 'enum' identifier
1346/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001347///
1348/// [C++] elaborated-type-specifier:
1349/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1350///
Chris Lattner197b4342009-04-12 21:49:30 +00001351void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1352 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001353 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001354
1355 AttributeList *Attr = 0;
1356 // If attributes exist after tag, parse them.
1357 if (Tok.is(tok::kw___attribute))
1358 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001359
1360 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001361 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001362 if (Tok.isNot(tok::identifier)) {
1363 Diag(Tok, diag::err_expected_ident);
1364 if (Tok.isNot(tok::l_brace)) {
1365 // Has no name and is not a definition.
1366 // Skip the rest of this declarator, up until the comma or semicolon.
1367 SkipUntil(tok::comma, true);
1368 return;
1369 }
1370 }
1371 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001372
1373 // Must have either 'enum name' or 'enum {...}'.
1374 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1375 Diag(Tok, diag::err_expected_ident_lbrace);
1376
1377 // Skip the rest of this declarator, up until the comma or semicolon.
1378 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001379 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001380 }
1381
1382 // If an identifier is present, consume and remember it.
1383 IdentifierInfo *Name = 0;
1384 SourceLocation NameLoc;
1385 if (Tok.is(tok::identifier)) {
1386 Name = Tok.getIdentifierInfo();
1387 NameLoc = ConsumeToken();
1388 }
1389
1390 // There are three options here. If we have 'enum foo;', then this is a
1391 // forward declaration. If we have 'enum foo {...' then this is a
1392 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1393 //
1394 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1395 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1396 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1397 //
1398 Action::TagKind TK;
1399 if (Tok.is(tok::l_brace))
1400 TK = Action::TK_Definition;
1401 else if (Tok.is(tok::semi))
1402 TK = Action::TK_Declaration;
1403 else
1404 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001405 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1406 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001407
Chris Lattner34a01ad2007-10-09 17:33:22 +00001408 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001409 ParseEnumBody(StartLoc, TagDecl);
1410
1411 // TODO: semantic analysis on the declspec for enums.
1412 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001413 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1414 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001415 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001416}
1417
1418/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1419/// enumerator-list:
1420/// enumerator
1421/// enumerator-list ',' enumerator
1422/// enumerator:
1423/// enumeration-constant
1424/// enumeration-constant '=' constant-expression
1425/// enumeration-constant:
1426/// identifier
1427///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001428void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001429 // Enter the scope of the enum body and start the definition.
1430 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001431 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001432
Chris Lattner4b009652007-07-25 00:24:17 +00001433 SourceLocation LBraceLoc = ConsumeBrace();
1434
Chris Lattnerc9a92452007-08-27 17:24:30 +00001435 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001436 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001437 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001438
Chris Lattner5261d0c2009-03-28 19:18:32 +00001439 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001440
Chris Lattner5261d0c2009-03-28 19:18:32 +00001441 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001442
1443 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001444 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001445 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1446 SourceLocation IdentLoc = ConsumeToken();
1447
1448 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001449 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001450 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001451 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001452 AssignedVal = ParseConstantExpression();
1453 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001454 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001455 }
1456
1457 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001458 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1459 LastEnumConstDecl,
1460 IdentLoc, Ident,
1461 EqualLoc,
1462 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001463 EnumConstantDecls.push_back(EnumConstDecl);
1464 LastEnumConstDecl = EnumConstDecl;
1465
Chris Lattner34a01ad2007-10-09 17:33:22 +00001466 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001467 break;
1468 SourceLocation CommaLoc = ConsumeToken();
1469
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001470 if (Tok.isNot(tok::identifier) &&
1471 !(getLang().C99 || getLang().CPlusPlus0x))
1472 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1473 << getLang().CPlusPlus
1474 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001475 }
1476
1477 // Eat the }.
1478 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1479
Steve Naroff0acc9c92007-09-15 18:49:24 +00001480 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001481 EnumConstantDecls.size());
1482
Chris Lattner5261d0c2009-03-28 19:18:32 +00001483 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001484 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001485 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001486 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001487
1488 EnumScope.Exit();
1489 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001490}
1491
1492/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001493/// start of a type-qualifier-list.
1494bool Parser::isTypeQualifier() const {
1495 switch (Tok.getKind()) {
1496 default: return false;
1497 // type-qualifier
1498 case tok::kw_const:
1499 case tok::kw_volatile:
1500 case tok::kw_restrict:
1501 return true;
1502 }
1503}
1504
1505/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001506/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001507bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001508 switch (Tok.getKind()) {
1509 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001510
1511 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001512 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001513 // Annotate typenames and C++ scope specifiers. If we get one, just
1514 // recurse to handle whatever we get.
1515 if (TryAnnotateTypeOrScopeToken())
1516 return isTypeSpecifierQualifier();
1517 // Otherwise, not a type specifier.
1518 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001519
Chris Lattnerb75fde62009-01-04 23:41:41 +00001520 case tok::coloncolon: // ::foo::bar
1521 if (NextToken().is(tok::kw_new) || // ::new
1522 NextToken().is(tok::kw_delete)) // ::delete
1523 return false;
1524
1525 // Annotate typenames and C++ scope specifiers. If we get one, just
1526 // recurse to handle whatever we get.
1527 if (TryAnnotateTypeOrScopeToken())
1528 return isTypeSpecifierQualifier();
1529 // Otherwise, not a type specifier.
1530 return false;
1531
Chris Lattner4b009652007-07-25 00:24:17 +00001532 // GNU attributes support.
1533 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001534 // GNU typeof support.
1535 case tok::kw_typeof:
1536
Chris Lattner4b009652007-07-25 00:24:17 +00001537 // type-specifiers
1538 case tok::kw_short:
1539 case tok::kw_long:
1540 case tok::kw_signed:
1541 case tok::kw_unsigned:
1542 case tok::kw__Complex:
1543 case tok::kw__Imaginary:
1544 case tok::kw_void:
1545 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001546 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001547 case tok::kw_int:
1548 case tok::kw_float:
1549 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001550 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001551 case tok::kw__Bool:
1552 case tok::kw__Decimal32:
1553 case tok::kw__Decimal64:
1554 case tok::kw__Decimal128:
1555
Chris Lattner2e78db32008-04-13 18:59:07 +00001556 // struct-or-union-specifier (C99) or class-specifier (C++)
1557 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001558 case tok::kw_struct:
1559 case tok::kw_union:
1560 // enum-specifier
1561 case tok::kw_enum:
1562
1563 // type-qualifier
1564 case tok::kw_const:
1565 case tok::kw_volatile:
1566 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001567
1568 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001569 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001570 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001571
1572 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1573 case tok::less:
1574 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001575
1576 case tok::kw___cdecl:
1577 case tok::kw___stdcall:
1578 case tok::kw___fastcall:
1579 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001580 }
1581}
1582
1583/// isDeclarationSpecifier() - Return true if the current token is part of a
1584/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001585bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001586 switch (Tok.getKind()) {
1587 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001588
1589 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001590 // Unfortunate hack to support "Class.factoryMethod" notation.
1591 if (getLang().ObjC1 && NextToken().is(tok::period))
1592 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001593 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001594
Douglas Gregord3022602009-03-27 23:10:48 +00001595 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001596 // Annotate typenames and C++ scope specifiers. If we get one, just
1597 // recurse to handle whatever we get.
1598 if (TryAnnotateTypeOrScopeToken())
1599 return isDeclarationSpecifier();
1600 // Otherwise, not a declaration specifier.
1601 return false;
1602 case tok::coloncolon: // ::foo::bar
1603 if (NextToken().is(tok::kw_new) || // ::new
1604 NextToken().is(tok::kw_delete)) // ::delete
1605 return false;
1606
1607 // Annotate typenames and C++ scope specifiers. If we get one, just
1608 // recurse to handle whatever we get.
1609 if (TryAnnotateTypeOrScopeToken())
1610 return isDeclarationSpecifier();
1611 // Otherwise, not a declaration specifier.
1612 return false;
1613
Chris Lattner4b009652007-07-25 00:24:17 +00001614 // storage-class-specifier
1615 case tok::kw_typedef:
1616 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001617 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001618 case tok::kw_static:
1619 case tok::kw_auto:
1620 case tok::kw_register:
1621 case tok::kw___thread:
1622
1623 // type-specifiers
1624 case tok::kw_short:
1625 case tok::kw_long:
1626 case tok::kw_signed:
1627 case tok::kw_unsigned:
1628 case tok::kw__Complex:
1629 case tok::kw__Imaginary:
1630 case tok::kw_void:
1631 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001632 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001633 case tok::kw_int:
1634 case tok::kw_float:
1635 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001636 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001637 case tok::kw__Bool:
1638 case tok::kw__Decimal32:
1639 case tok::kw__Decimal64:
1640 case tok::kw__Decimal128:
1641
Chris Lattner2e78db32008-04-13 18:59:07 +00001642 // struct-or-union-specifier (C99) or class-specifier (C++)
1643 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001644 case tok::kw_struct:
1645 case tok::kw_union:
1646 // enum-specifier
1647 case tok::kw_enum:
1648
1649 // type-qualifier
1650 case tok::kw_const:
1651 case tok::kw_volatile:
1652 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001653
Chris Lattner4b009652007-07-25 00:24:17 +00001654 // function-specifier
1655 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001656 case tok::kw_virtual:
1657 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001658
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001659 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001660 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001661
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001662 // GNU typeof support.
1663 case tok::kw_typeof:
1664
1665 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001666 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001667 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001668
1669 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1670 case tok::less:
1671 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001672
Steve Naroffab1a3632009-01-06 19:34:12 +00001673 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001674 case tok::kw___cdecl:
1675 case tok::kw___stdcall:
1676 case tok::kw___fastcall:
1677 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001678 }
1679}
1680
1681
1682/// ParseTypeQualifierListOpt
1683/// type-qualifier-list: [C99 6.7.5]
1684/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001685/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001686/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001687/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001688///
Chris Lattner460696f2008-12-18 07:02:59 +00001689void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001690 while (1) {
1691 int isInvalid = false;
1692 const char *PrevSpec = 0;
1693 SourceLocation Loc = Tok.getLocation();
1694
1695 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001696 case tok::kw_const:
1697 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1698 getLang())*2;
1699 break;
1700 case tok::kw_volatile:
1701 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1702 getLang())*2;
1703 break;
1704 case tok::kw_restrict:
1705 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1706 getLang())*2;
1707 break;
Steve Naroffad620402008-12-25 14:41:26 +00001708 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001709 case tok::kw___cdecl:
1710 case tok::kw___stdcall:
1711 case tok::kw___fastcall:
1712 if (!PP.getLangOptions().Microsoft)
1713 goto DoneWithTypeQuals;
1714 // Just ignore it.
1715 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001716 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001717 if (AttributesAllowed) {
1718 DS.AddAttributes(ParseAttributes());
1719 continue; // do *not* consume the next token!
1720 }
1721 // otherwise, FALL THROUGH!
1722 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001723 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001724 // If this is not a type-qualifier token, we're done reading type
1725 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001726 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001727 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001728 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001729
Chris Lattner4b009652007-07-25 00:24:17 +00001730 // If the specifier combination wasn't legal, issue a diagnostic.
1731 if (isInvalid) {
1732 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001733 // Pick between error or extwarn.
1734 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1735 : diag::ext_duplicate_declspec;
1736 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001737 }
1738 ConsumeToken();
1739 }
1740}
1741
1742
1743/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1744///
1745void Parser::ParseDeclarator(Declarator &D) {
1746 /// This implements the 'declarator' production in the C grammar, then checks
1747 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001748 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001749}
1750
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001751/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1752/// is parsed by the function passed to it. Pass null, and the direct-declarator
1753/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001754/// ptr-operator production.
1755///
Sebastian Redl75555032009-01-24 21:16:55 +00001756/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1757/// [C] pointer[opt] direct-declarator
1758/// [C++] direct-declarator
1759/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001760///
1761/// pointer: [C99 6.7.5]
1762/// '*' type-qualifier-list[opt]
1763/// '*' type-qualifier-list[opt] pointer
1764///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001765/// ptr-operator:
1766/// '*' cv-qualifier-seq[opt]
1767/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001768/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001769/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001770/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001771/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001772void Parser::ParseDeclaratorInternal(Declarator &D,
1773 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001774
Sebastian Redl75555032009-01-24 21:16:55 +00001775 // C++ member pointers start with a '::' or a nested-name.
1776 // Member pointers get special handling, since there's no place for the
1777 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001778 if (getLang().CPlusPlus &&
1779 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1780 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001781 CXXScopeSpec SS;
1782 if (ParseOptionalCXXScopeSpecifier(SS)) {
1783 if(Tok.isNot(tok::star)) {
1784 // The scope spec really belongs to the direct-declarator.
1785 D.getCXXScopeSpec() = SS;
1786 if (DirectDeclParser)
1787 (this->*DirectDeclParser)(D);
1788 return;
1789 }
1790
1791 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001792 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001793 DeclSpec DS;
1794 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001795 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001796
1797 // Recurse to parse whatever is left.
1798 ParseDeclaratorInternal(D, DirectDeclParser);
1799
1800 // Sema will have to catch (syntactically invalid) pointers into global
1801 // scope. It has to catch pointers into namespace scope anyway.
1802 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001803 Loc, DS.TakeAttributes()),
1804 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001805 return;
1806 }
1807 }
1808
1809 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001810 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001811 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001812 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001813 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001814 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001815 if (DirectDeclParser)
1816 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001817 return;
1818 }
Sebastian Redl75555032009-01-24 21:16:55 +00001819
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001820 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1821 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001822 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001823 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001824
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001825 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001826 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001827 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001828
Chris Lattner4b009652007-07-25 00:24:17 +00001829 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001830 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001831
Chris Lattner4b009652007-07-25 00:24:17 +00001832 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001833 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001834 if (Kind == tok::star)
1835 // Remember that we parsed a pointer type, and remember the type-quals.
1836 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001837 DS.TakeAttributes()),
1838 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001839 else
1840 // Remember that we parsed a Block type, and remember the type-quals.
1841 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001842 Loc),
1843 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001844 } else {
1845 // Is a reference
1846 DeclSpec DS;
1847
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001848 // Complain about rvalue references in C++03, but then go on and build
1849 // the declarator.
1850 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1851 Diag(Loc, diag::err_rvalue_reference);
1852
Chris Lattner4b009652007-07-25 00:24:17 +00001853 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1854 // cv-qualifiers are introduced through the use of a typedef or of a
1855 // template type argument, in which case the cv-qualifiers are ignored.
1856 //
1857 // [GNU] Retricted references are allowed.
1858 // [GNU] Attributes on references are allowed.
1859 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001860 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001861
1862 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1863 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1864 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001865 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001866 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1867 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001868 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001869 }
1870
1871 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001872 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001873
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001874 if (D.getNumTypeObjects() > 0) {
1875 // C++ [dcl.ref]p4: There shall be no references to references.
1876 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1877 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001878 if (const IdentifierInfo *II = D.getIdentifier())
1879 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1880 << II;
1881 else
1882 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1883 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001884
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001885 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001886 // can go ahead and build the (technically ill-formed)
1887 // declarator: reference collapsing will take care of it.
1888 }
1889 }
1890
Chris Lattner4b009652007-07-25 00:24:17 +00001891 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001892 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001893 DS.TakeAttributes(),
1894 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001895 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001896 }
1897}
1898
1899/// ParseDirectDeclarator
1900/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001901/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001902/// '(' declarator ')'
1903/// [GNU] '(' attributes declarator ')'
1904/// [C90] direct-declarator '[' constant-expression[opt] ']'
1905/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1906/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1907/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1908/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1909/// direct-declarator '(' parameter-type-list ')'
1910/// direct-declarator '(' identifier-list[opt] ')'
1911/// [GNU] direct-declarator '(' parameter-forward-declarations
1912/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001913/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1914/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001915/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001916///
1917/// declarator-id: [C++ 8]
1918/// id-expression
1919/// '::'[opt] nested-name-specifier[opt] type-name
1920///
1921/// id-expression: [C++ 5.1]
1922/// unqualified-id
1923/// qualified-id [TODO]
1924///
1925/// unqualified-id: [C++ 5.1]
1926/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001927/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001928/// conversion-function-id [TODO]
1929/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001930/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001931///
Chris Lattner4b009652007-07-25 00:24:17 +00001932void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001933 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001934
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001935 if (getLang().CPlusPlus) {
1936 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001937 // ParseDeclaratorInternal might already have parsed the scope.
1938 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1939 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001940 if (afterCXXScope) {
1941 // Change the declaration context for name lookup, until this function
1942 // is exited (and the declarator has been parsed).
1943 DeclScopeObj.EnterDeclaratorScope();
1944 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001945
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001946 if (Tok.is(tok::identifier)) {
1947 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001948
Douglas Gregor2fa10442008-12-18 19:37:40 +00001949 // If this identifier is the name of the current class, it's a
1950 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001951 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001952 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001953 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001954 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001955 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001956 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001957 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1958 ConsumeToken();
1959 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001960 } else if (Tok.is(tok::annot_template_id)) {
1961 TemplateIdAnnotation *TemplateId
1962 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1963
1964 // FIXME: Could this template-id name a constructor?
1965
1966 // FIXME: This is an egregious hack, where we silently ignore
1967 // the specialization (which should be a function template
1968 // specialization name) and use the name instead. This hack
1969 // will go away when we have support for function
1970 // specializations.
1971 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1972 TemplateId->Destroy();
1973 ConsumeToken();
1974 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001975 } else if (Tok.is(tok::kw_operator)) {
1976 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001977 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001978
Douglas Gregor853dd392008-12-26 15:00:45 +00001979 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001980 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1981 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001982 } else {
1983 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001984 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1985 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1986 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001987 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001988 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001989 }
1990 goto PastIdentifier;
1991 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001992 // This should be a C++ destructor.
1993 SourceLocation TildeLoc = ConsumeToken();
1994 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001995 // FIXME: Inaccurate.
1996 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001997 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00001998 TypeResult Type = ParseClassName(EndLoc);
1999 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002000 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002001 else
2002 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002003 } else {
2004 Diag(Tok, diag::err_expected_class_name);
2005 D.SetIdentifier(0, TildeLoc);
2006 }
2007 goto PastIdentifier;
2008 }
2009
2010 // If we reached this point, token is not identifier and not '~'.
2011
2012 if (afterCXXScope) {
2013 Diag(Tok, diag::err_expected_unqualified_id);
2014 D.SetIdentifier(0, Tok.getLocation());
2015 D.setInvalidType(true);
2016 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002017 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002018 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002019 }
2020
2021 // If we reached this point, we are either in C/ObjC or the token didn't
2022 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002023 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2024 assert(!getLang().CPlusPlus &&
2025 "There's a C++-specific check for tok::identifier above");
2026 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2027 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2028 ConsumeToken();
2029 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002030 // direct-declarator: '(' declarator ')'
2031 // direct-declarator: '(' attributes declarator ')'
2032 // Example: 'char (*X)' or 'int (*XX)(void)'
2033 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002034 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002035 // This could be something simple like "int" (in which case the declarator
2036 // portion is empty), if an abstract-declarator is allowed.
2037 D.SetIdentifier(0, Tok.getLocation());
2038 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002039 if (D.getContext() == Declarator::MemberContext)
2040 Diag(Tok, diag::err_expected_member_name_or_semi)
2041 << D.getDeclSpec().getSourceRange();
2042 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002043 Diag(Tok, diag::err_expected_unqualified_id);
2044 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002045 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002046 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002047 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002048 }
2049
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002050 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002051 assert(D.isPastIdentifier() &&
2052 "Haven't past the location of the identifier yet?");
2053
2054 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002055 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002056 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2057 // In such a case, check if we actually have a function declarator; if it
2058 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002059 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2060 // When not in file scope, warn for ambiguous function declarators, just
2061 // in case the author intended it as a variable definition.
2062 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2063 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2064 break;
2065 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002066 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002067 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002068 ParseBracketDeclarator(D);
2069 } else {
2070 break;
2071 }
2072 }
2073}
2074
Chris Lattnera0d056d2008-04-06 05:45:57 +00002075/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2076/// only called before the identifier, so these are most likely just grouping
2077/// parens for precedence. If we find that these are actually function
2078/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2079///
2080/// direct-declarator:
2081/// '(' declarator ')'
2082/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002083/// direct-declarator '(' parameter-type-list ')'
2084/// direct-declarator '(' identifier-list[opt] ')'
2085/// [GNU] direct-declarator '(' parameter-forward-declarations
2086/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002087///
2088void Parser::ParseParenDeclarator(Declarator &D) {
2089 SourceLocation StartLoc = ConsumeParen();
2090 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2091
Chris Lattner1f185292008-10-20 02:05:46 +00002092 // Eat any attributes before we look at whether this is a grouping or function
2093 // declarator paren. If this is a grouping paren, the attribute applies to
2094 // the type being built up, for example:
2095 // int (__attribute__(()) *x)(long y)
2096 // If this ends up not being a grouping paren, the attribute applies to the
2097 // first argument, for example:
2098 // int (__attribute__(()) int x)
2099 // In either case, we need to eat any attributes to be able to determine what
2100 // sort of paren this is.
2101 //
2102 AttributeList *AttrList = 0;
2103 bool RequiresArg = false;
2104 if (Tok.is(tok::kw___attribute)) {
2105 AttrList = ParseAttributes();
2106
2107 // We require that the argument list (if this is a non-grouping paren) be
2108 // present even if the attribute list was empty.
2109 RequiresArg = true;
2110 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002111 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002112 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2113 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002114 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002115
Chris Lattnera0d056d2008-04-06 05:45:57 +00002116 // If we haven't past the identifier yet (or where the identifier would be
2117 // stored, if this is an abstract declarator), then this is probably just
2118 // grouping parens. However, if this could be an abstract-declarator, then
2119 // this could also be the start of function arguments (consider 'void()').
2120 bool isGrouping;
2121
2122 if (!D.mayOmitIdentifier()) {
2123 // If this can't be an abstract-declarator, this *must* be a grouping
2124 // paren, because we haven't seen the identifier yet.
2125 isGrouping = true;
2126 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002127 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002128 isDeclarationSpecifier()) { // 'int(int)' is a function.
2129 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2130 // considered to be a type, not a K&R identifier-list.
2131 isGrouping = false;
2132 } else {
2133 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2134 isGrouping = true;
2135 }
2136
2137 // If this is a grouping paren, handle:
2138 // direct-declarator: '(' declarator ')'
2139 // direct-declarator: '(' attributes declarator ')'
2140 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002141 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002142 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002143 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002144 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002145
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002146 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002147 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002148 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002149
2150 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002151 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002152 return;
2153 }
2154
2155 // Okay, if this wasn't a grouping paren, it must be the start of a function
2156 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002157 // identifier (and remember where it would have been), then call into
2158 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002159 D.SetIdentifier(0, Tok.getLocation());
2160
Chris Lattner1f185292008-10-20 02:05:46 +00002161 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002162}
2163
2164/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2165/// declarator D up to a paren, which indicates that we are parsing function
2166/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002167///
Chris Lattner1f185292008-10-20 02:05:46 +00002168/// If AttrList is non-null, then the caller parsed those arguments immediately
2169/// after the open paren - they should be considered to be the first argument of
2170/// a parameter. If RequiresArg is true, then the first argument of the
2171/// function is required to be present and required to not be an identifier
2172/// list.
2173///
Chris Lattner4b009652007-07-25 00:24:17 +00002174/// This method also handles this portion of the grammar:
2175/// parameter-type-list: [C99 6.7.5]
2176/// parameter-list
2177/// parameter-list ',' '...'
2178///
2179/// parameter-list: [C99 6.7.5]
2180/// parameter-declaration
2181/// parameter-list ',' parameter-declaration
2182///
2183/// parameter-declaration: [C99 6.7.5]
2184/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002185/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002186/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002187/// declaration-specifiers abstract-declarator[opt]
2188/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002189/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002190/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2191///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002192/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002193/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002194///
Chris Lattner1f185292008-10-20 02:05:46 +00002195void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2196 AttributeList *AttrList,
2197 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002198 // lparen is already consumed!
2199 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002200
Chris Lattner1f185292008-10-20 02:05:46 +00002201 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002202 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002203 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002204 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002205 delete AttrList;
2206 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002207
Sebastian Redl0c986032009-02-09 18:23:29 +00002208 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002209
2210 // cv-qualifier-seq[opt].
2211 DeclSpec DS;
2212 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002213 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002214 if (!DS.getSourceRange().getEnd().isInvalid())
2215 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002216
2217 // Parse exception-specification[opt].
2218 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002219 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002220 }
2221
Chris Lattner9f7564b2008-04-06 06:57:35 +00002222 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002223 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002224 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002225 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002226 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002227 /*arglist*/ 0, 0,
2228 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002229 LParenLoc, D),
2230 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002231 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002232 }
2233
2234 // Alternatively, this parameter list may be an identifier list form for a
2235 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002236 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002237 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002238 // K&R identifier lists can't have typedefs as identifiers, per
2239 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002240 if (RequiresArg) {
2241 Diag(Tok, diag::err_argument_required_after_attribute);
2242 delete AttrList;
2243 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002244 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2245 // normal declarators, not for abstract-declarators.
2246 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002247 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002248 }
2249
2250 // Finally, a normal, non-empty parameter type list.
2251
2252 // Build up an array of information about the parsed arguments.
2253 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002254
2255 // Enter function-declaration scope, limiting any declarators to the
2256 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002257 ParseScope PrototypeScope(this,
2258 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002259
2260 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002261 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002262 while (1) {
2263 if (Tok.is(tok::ellipsis)) {
2264 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002265 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002266 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002267 }
2268
Chris Lattner9f7564b2008-04-06 06:57:35 +00002269 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002270
Chris Lattner9f7564b2008-04-06 06:57:35 +00002271 // Parse the declaration-specifiers.
2272 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002273
2274 // If the caller parsed attributes for the first argument, add them now.
2275 if (AttrList) {
2276 DS.AddAttributes(AttrList);
2277 AttrList = 0; // Only apply the attributes to the first parameter.
2278 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002279 ParseDeclarationSpecifiers(DS);
2280
Chris Lattner9f7564b2008-04-06 06:57:35 +00002281 // Parse the declarator. This is "PrototypeContext", because we must
2282 // accept either 'declarator' or 'abstract-declarator' here.
2283 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2284 ParseDeclarator(ParmDecl);
2285
2286 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002287 if (Tok.is(tok::kw___attribute)) {
2288 SourceLocation Loc;
2289 AttributeList *AttrList = ParseAttributes(&Loc);
2290 ParmDecl.AddAttributes(AttrList, Loc);
2291 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002292
Chris Lattner9f7564b2008-04-06 06:57:35 +00002293 // Remember this parsed parameter in ParamInfo.
2294 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2295
Douglas Gregor605de8d2008-12-16 21:30:33 +00002296 // DefArgToks is used when the parsing of default arguments needs
2297 // to be delayed.
2298 CachedTokens *DefArgToks = 0;
2299
Chris Lattner9f7564b2008-04-06 06:57:35 +00002300 // If no parameter was specified, verify that *something* was specified,
2301 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002302 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2303 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002304 // Completely missing, emit error.
2305 Diag(DSStart, diag::err_missing_param);
2306 } else {
2307 // Otherwise, we have something. Add it and let semantic analysis try
2308 // to grok it and add the result to the ParamInfo we are building.
2309
2310 // Inform the actions module about the parameter declarator, so it gets
2311 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002312 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002313
2314 // Parse the default argument, if any. We parse the default
2315 // arguments in all dialects; the semantic analysis in
2316 // ActOnParamDefaultArgument will reject the default argument in
2317 // C.
2318 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002319 SourceLocation EqualLoc = Tok.getLocation();
2320
Chris Lattner3e254fb2008-04-08 04:40:51 +00002321 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002322 if (D.getContext() == Declarator::MemberContext) {
2323 // If we're inside a class definition, cache the tokens
2324 // corresponding to the default argument. We'll actually parse
2325 // them when we see the end of the class definition.
2326 // FIXME: Templates will require something similar.
2327 // FIXME: Can we use a smart pointer for Toks?
2328 DefArgToks = new CachedTokens;
2329
2330 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2331 tok::semi, false)) {
2332 delete DefArgToks;
2333 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002334 Actions.ActOnParamDefaultArgumentError(Param);
2335 } else
2336 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002337 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002338 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002339 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002340
2341 OwningExprResult DefArgResult(ParseAssignmentExpression());
2342 if (DefArgResult.isInvalid()) {
2343 Actions.ActOnParamDefaultArgumentError(Param);
2344 SkipUntil(tok::comma, tok::r_paren, true, true);
2345 } else {
2346 // Inform the actions module about the default argument
2347 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002348 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002349 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002350 }
2351 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002352
2353 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002354 ParmDecl.getIdentifierLoc(), Param,
2355 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002356 }
2357
2358 // If the next token is a comma, consume it and keep reading arguments.
2359 if (Tok.isNot(tok::comma)) break;
2360
2361 // Consume the comma.
2362 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002363 }
2364
Chris Lattner9f7564b2008-04-06 06:57:35 +00002365 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002366 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002367
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002368 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002369 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002370
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002371 DeclSpec DS;
2372 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002373 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002374 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002375 if (!DS.getSourceRange().getEnd().isInvalid())
2376 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002377
2378 // Parse exception-specification[opt].
2379 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002380 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002381 }
2382
Chris Lattner4b009652007-07-25 00:24:17 +00002383 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002384 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002385 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002386 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002387 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002388 LParenLoc, D),
2389 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002390}
2391
Chris Lattner35d9c912008-04-06 06:34:08 +00002392/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2393/// we found a K&R-style identifier list instead of a type argument list. The
2394/// current token is known to be the first identifier in the list.
2395///
2396/// identifier-list: [C99 6.7.5]
2397/// identifier
2398/// identifier-list ',' identifier
2399///
2400void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2401 Declarator &D) {
2402 // Build up an array of information about the parsed arguments.
2403 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2404 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2405
2406 // If there was no identifier specified for the declarator, either we are in
2407 // an abstract-declarator, or we are in a parameter declarator which was found
2408 // to be abstract. In abstract-declarators, identifier lists are not valid:
2409 // diagnose this.
2410 if (!D.getIdentifier())
2411 Diag(Tok, diag::ext_ident_list_in_param);
2412
2413 // Tok is known to be the first identifier in the list. Remember this
2414 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002415 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002416 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002417 Tok.getLocation(),
2418 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002419
Chris Lattner113a56b2008-04-06 06:39:19 +00002420 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002421
2422 while (Tok.is(tok::comma)) {
2423 // Eat the comma.
2424 ConsumeToken();
2425
Chris Lattner113a56b2008-04-06 06:39:19 +00002426 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002427 if (Tok.isNot(tok::identifier)) {
2428 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002429 SkipUntil(tok::r_paren);
2430 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002431 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002432
Chris Lattner35d9c912008-04-06 06:34:08 +00002433 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002434
2435 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002436 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002437 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002438
2439 // Verify that the argument identifier has not already been mentioned.
2440 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002441 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002442 } else {
2443 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002444 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002445 Tok.getLocation(),
2446 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002447 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002448
2449 // Eat the identifier.
2450 ConsumeToken();
2451 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002452
2453 // If we have the closing ')', eat it and we're done.
2454 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2455
Chris Lattner113a56b2008-04-06 06:39:19 +00002456 // Remember that we parsed a function type, and remember the attributes. This
2457 // function type is always a K&R style function type, which is not varargs and
2458 // has no prototype.
2459 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002460 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002461 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002462 /*TypeQuals*/0, LParenLoc, D),
2463 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002464}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002465
Chris Lattner4b009652007-07-25 00:24:17 +00002466/// [C90] direct-declarator '[' constant-expression[opt] ']'
2467/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2468/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2469/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2470/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2471void Parser::ParseBracketDeclarator(Declarator &D) {
2472 SourceLocation StartLoc = ConsumeBracket();
2473
Chris Lattner1525c3a2008-12-18 07:27:21 +00002474 // C array syntax has many features, but by-far the most common is [] and [4].
2475 // This code does a fast path to handle some of the most obvious cases.
2476 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002477 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002478 // Remember that we parsed the empty array type.
2479 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002480 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2481 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002482 return;
2483 } else if (Tok.getKind() == tok::numeric_constant &&
2484 GetLookAheadToken(1).is(tok::r_square)) {
2485 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002486 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002487 ConsumeToken();
2488
Sebastian Redl0c986032009-02-09 18:23:29 +00002489 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002490
2491 // If there was an error parsing the assignment-expression, recover.
2492 if (ExprRes.isInvalid())
2493 ExprRes.release(); // Deallocate expr, just use [].
2494
2495 // Remember that we parsed a array type, and remember its features.
2496 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002497 ExprRes.release(), StartLoc),
2498 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002499 return;
2500 }
2501
Chris Lattner4b009652007-07-25 00:24:17 +00002502 // If valid, this location is the position where we read the 'static' keyword.
2503 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002504 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002505 StaticLoc = ConsumeToken();
2506
2507 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002508 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002509 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002510 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002511
2512 // If we haven't already read 'static', check to see if there is one after the
2513 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002514 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002515 StaticLoc = ConsumeToken();
2516
2517 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2518 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002519 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002520
2521 // Handle the case where we have '[*]' as the array size. However, a leading
2522 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2523 // the the token after the star is a ']'. Since stars in arrays are
2524 // infrequent, use of lookahead is not costly here.
2525 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002526 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002527
Chris Lattner306d4df2008-12-18 06:50:14 +00002528 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002529 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002530 StaticLoc = SourceLocation(); // Drop the static.
2531 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002532 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002533 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002534 // Note, in C89, this production uses the constant-expr production instead
2535 // of assignment-expr. The only difference is that assignment-expr allows
2536 // things like '=' and '*='. Sema rejects these in C89 mode because they
2537 // are not i-c-e's, so we don't need to distinguish between the two here.
2538
Chris Lattner4b009652007-07-25 00:24:17 +00002539 // Parse the assignment-expression now.
2540 NumElements = ParseAssignmentExpression();
2541 }
2542
2543 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002544 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002545 // If the expression was invalid, skip it.
2546 SkipUntil(tok::r_square);
2547 return;
2548 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002549
2550 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2551
Chris Lattner1525c3a2008-12-18 07:27:21 +00002552 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002553 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2554 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002555 NumElements.release(), StartLoc),
2556 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002557}
2558
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002559/// [GNU] typeof-specifier:
2560/// typeof ( expressions )
2561/// typeof ( type-name )
2562/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002563///
2564void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002565 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002566 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002567 SourceLocation StartLoc = ConsumeToken();
2568
Chris Lattner34a01ad2007-10-09 17:33:22 +00002569 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002570 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002571 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002572 return;
2573 }
2574
Sebastian Redl14ca7412008-12-11 21:36:32 +00002575 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002576 if (Result.isInvalid()) {
2577 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002578 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002579 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002580
2581 const char *PrevSpec = 0;
2582 // Check for duplicate type specifiers.
2583 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002584 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002585 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002586
2587 // FIXME: Not accurate, the range gets one token more than it should.
2588 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002589 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002590 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002591
Steve Naroff7cbb1462007-07-31 12:34:36 +00002592 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2593
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002594 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002595 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002596
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002597 assert((Ty.isInvalid() || Ty.get()) &&
2598 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002599
Chris Lattner34a01ad2007-10-09 17:33:22 +00002600 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002601 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002602 return;
2603 }
2604 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002605
2606 if (Ty.isInvalid())
2607 DS.SetTypeSpecError();
2608 else {
2609 const char *PrevSpec = 0;
2610 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2611 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2612 Ty.get()))
2613 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2614 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002615 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002616 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002617
2618 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002619 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002620 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002621 return;
2622 }
2623 RParenLoc = ConsumeParen();
2624 const char *PrevSpec = 0;
2625 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2626 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002627 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002628 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002629 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002630 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002631}
2632
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002633