blob: b3c2d8c555613236e6a498c7bb9d1ede675ead04 [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
Chris Lattner34c61332009-04-25 08:06:05 +000040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000041 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();
Chris Lattnera52aec42009-04-14 21:16:09 +0000443 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
444 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000445 Diag(Tok, diag::err_typename_requires_specqual);
446
447 // Issue diagnostic and remove storage class if present.
448 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
449 if (DS.getStorageClassSpecLoc().isValid())
450 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
451 else
452 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
453 DS.ClearStorageClassSpecs();
454 }
455
456 // Issue diagnostic and remove function specfier if present.
457 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000458 if (DS.isInlineSpecified())
459 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
460 if (DS.isVirtualSpecified())
461 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
462 if (DS.isExplicitSpecified())
463 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000464 DS.ClearFunctionSpecs();
465 }
466}
467
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000468/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
469/// specified token is valid after the identifier in a declarator which
470/// immediately follows the declspec. For example, these things are valid:
471///
472/// int x [ 4]; // direct-declarator
473/// int x ( int y); // direct-declarator
474/// int(int x ) // direct-declarator
475/// int x ; // simple-declaration
476/// int x = 17; // init-declarator-list
477/// int x , y; // init-declarator-list
478/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000479/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000480/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000481///
482/// This is not, because 'x' does not immediately follow the declspec (though
483/// ')' happens to be valid anyway).
484/// int (x)
485///
486static bool isValidAfterIdentifierInDeclarator(const Token &T) {
487 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
488 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000489 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000490}
491
Chris Lattner82353c62009-04-14 21:34:55 +0000492
493/// ParseImplicitInt - This method is called when we have an non-typename
494/// identifier in a declspec (which normally terminates the decl spec) when
495/// the declspec has no type specifier. In this case, the declspec is either
496/// malformed or is "implicit int" (in K&R and C89).
497///
498/// This method handles diagnosing this prettily and returns false if the
499/// declspec is done being processed. If it recovers and thinks there may be
500/// other pieces of declspec after it, it returns true.
501///
Chris Lattner52cd7622009-04-14 22:17:06 +0000502bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Chris Lattner82353c62009-04-14 21:34:55 +0000503 TemplateParameterLists *TemplateParams,
504 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000505 assert(Tok.is(tok::identifier) && "should have identifier");
506
Chris Lattner82353c62009-04-14 21:34:55 +0000507 SourceLocation Loc = Tok.getLocation();
508 // If we see an identifier that is not a type name, we normally would
509 // parse it as the identifer being declared. However, when a typename
510 // is typo'd or the definition is not included, this will incorrectly
511 // parse the typename as the identifier name and fall over misparsing
512 // later parts of the diagnostic.
513 //
514 // As such, we try to do some look-ahead in cases where this would
515 // otherwise be an "implicit-int" case to see if this is invalid. For
516 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
517 // an identifier with implicit int, we'd get a parse error because the
518 // next token is obviously invalid for a type. Parse these as a case
519 // with an invalid type specifier.
520 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
521
522 // Since we know that this either implicit int (which is rare) or an
523 // error, we'd do lookahead to try to do better recovery.
524 if (isValidAfterIdentifierInDeclarator(NextToken())) {
525 // If this token is valid for implicit int, e.g. "static x = 4", then
526 // we just avoid eating the identifier, so it will be parsed as the
527 // identifier in the declarator.
528 return false;
529 }
530
531 // Otherwise, if we don't consume this token, we are going to emit an
532 // error anyway. Try to recover from various common problems. Check
533 // to see if this was a reference to a tag name without a tag specified.
534 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000535 //
536 // C++ doesn't need this, and isTagName doesn't take SS.
537 if (SS == 0) {
538 const char *TagName = 0;
539 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000540
Chris Lattner82353c62009-04-14 21:34:55 +0000541 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
542 default: break;
543 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
544 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
545 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
546 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
547 }
Chris Lattner82353c62009-04-14 21:34:55 +0000548
Chris Lattner52cd7622009-04-14 22:17:06 +0000549 if (TagName) {
550 Diag(Loc, diag::err_use_of_tag_name_without_tag)
551 << Tok.getIdentifierInfo() << TagName
552 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
553
554 // Parse this as a tag as if the missing tag were present.
555 if (TagKind == tok::kw_enum)
556 ParseEnumSpecifier(Loc, DS, AS);
557 else
558 ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
559 return true;
560 }
Chris Lattner82353c62009-04-14 21:34:55 +0000561 }
562
563 // Since this is almost certainly an invalid type name, emit a
564 // diagnostic that says it, eat the token, and mark the declspec as
565 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000566 SourceRange R;
567 if (SS) R = SS->getRange();
568
569 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000570 const char *PrevSpec;
571 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
572 DS.SetRangeEnd(Tok.getLocation());
573 ConsumeToken();
574
575 // TODO: Could inject an invalid typedef decl in an enclosing scope to
576 // avoid rippling error messages on subsequent uses of the same type,
577 // could be useful if #include was forgotten.
578 return false;
579}
580
Chris Lattner4b009652007-07-25 00:24:17 +0000581/// ParseDeclarationSpecifiers
582/// declaration-specifiers: [C99 6.7]
583/// storage-class-specifier declaration-specifiers[opt]
584/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000585/// [C99] function-specifier declaration-specifiers[opt]
586/// [GNU] attributes declaration-specifiers[opt]
587///
588/// storage-class-specifier: [C99 6.7.1]
589/// 'typedef'
590/// 'extern'
591/// 'static'
592/// 'auto'
593/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000594/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000595/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000596/// function-specifier: [C99 6.7.4]
597/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000598/// [C++] 'virtual'
599/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000600/// 'friend': [C++ dcl.friend]
601
Chris Lattner4b009652007-07-25 00:24:17 +0000602///
Douglas Gregor52473432008-12-24 02:52:09 +0000603void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000604 TemplateParameterLists *TemplateParams,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000605 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000606 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000607 while (1) {
608 int isInvalid = false;
609 const char *PrevSpec = 0;
610 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000611
Chris Lattner4b009652007-07-25 00:24:17 +0000612 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000613 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000614 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000615 // If this is not a declaration specifier token, we're done reading decl
616 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000617 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000618 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000619
620 case tok::coloncolon: // ::foo::bar
621 // Annotate C++ scope specifiers. If we get one, loop.
622 if (TryAnnotateCXXScopeToken())
623 continue;
624 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000625
626 case tok::annot_cxxscope: {
627 if (DS.hasTypeSpecifier())
628 goto DoneWithDeclSpec;
629
630 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000631 Token Next = NextToken();
632 if (Next.is(tok::annot_template_id) &&
633 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000634 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000635 // We have a qualified template-id, e.g., N::A<int>
636 CXXScopeSpec SS;
637 ParseOptionalCXXScopeSpecifier(SS);
638 assert(Tok.is(tok::annot_template_id) &&
639 "ParseOptionalCXXScopeSpecifier not working");
640 AnnotateTemplateIdTokenAsType(&SS);
641 continue;
642 }
643
644 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000645 goto DoneWithDeclSpec;
646
647 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000648 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000649 SS.setRange(Tok.getAnnotationRange());
650
651 // If the next token is the name of the class type that the C++ scope
652 // denotes, followed by a '(', then this is a constructor declaration.
653 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000654 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000655 CurScope, &SS) &&
656 GetLookAheadToken(2).is(tok::l_paren))
657 goto DoneWithDeclSpec;
658
Douglas Gregor1075a162009-02-04 17:00:24 +0000659 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
660 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000661
Chris Lattner52cd7622009-04-14 22:17:06 +0000662 // If the referenced identifier is not a type, then this declspec is
663 // erroneous: We already checked about that it has no type specifier, and
664 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
665 // typename.
666 if (TypeRep == 0) {
667 ConsumeToken(); // Eat the scope spec so the identifier is current.
668 if (ParseImplicitInt(DS, &SS, TemplateParams, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000669 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000670 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000671
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000672 ConsumeToken(); // The C++ scope.
673
Douglas Gregora60c62e2009-02-09 15:09:02 +0000674 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000675 TypeRep);
676 if (isInvalid)
677 break;
678
679 DS.SetRangeEnd(Tok.getLocation());
680 ConsumeToken(); // The typename.
681
682 continue;
683 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000684
685 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000686 if (Tok.getAnnotationValue())
687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
688 Tok.getAnnotationValue());
689 else
690 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000691 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
692 ConsumeToken(); // The typename
693
694 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
695 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
696 // Objective-C interface. If we don't have Objective-C or a '<', this is
697 // just a normal reference to a typedef name.
698 if (!Tok.is(tok::less) || !getLang().ObjC1)
699 continue;
700
701 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000702 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000703 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
704 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
705
706 DS.SetRangeEnd(EndProtoLoc);
707 continue;
708 }
709
Chris Lattnerfda18db2008-07-26 01:18:38 +0000710 // typedef-name
711 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000712 // In C++, check to see if this is a scope specifier like foo::bar::, if
713 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000714 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
715 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000716
Chris Lattnerfda18db2008-07-26 01:18:38 +0000717 // This identifier can only be a typedef name if we haven't already seen
718 // a type-specifier. Without this check we misparse:
719 // typedef int X; struct Y { short X; }; as 'short int'.
720 if (DS.hasTypeSpecifier())
721 goto DoneWithDeclSpec;
722
723 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000724 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
725 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000726
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000727 // If this is not a typedef name, don't parse it as part of the declspec,
728 // it must be an implicit int or an error.
729 if (TypeRep == 0) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000730 if (ParseImplicitInt(DS, 0, TemplateParams, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000731 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000732 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000733
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000734 // C++: If the identifier is actually the name of the class type
735 // being defined and the next token is a '(', then this is a
736 // constructor declaration. We're done with the decl-specifiers
737 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000738 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000739 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
740 NextToken().getKind() == tok::l_paren)
741 goto DoneWithDeclSpec;
742
Douglas Gregora60c62e2009-02-09 15:09:02 +0000743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000744 TypeRep);
745 if (isInvalid)
746 break;
747
748 DS.SetRangeEnd(Tok.getLocation());
749 ConsumeToken(); // The identifier
750
751 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
752 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
753 // Objective-C interface. If we don't have Objective-C or a '<', this is
754 // just a normal reference to a typedef name.
755 if (!Tok.is(tok::less) || !getLang().ObjC1)
756 continue;
757
758 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000759 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000760 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000761 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000762
763 DS.SetRangeEnd(EndProtoLoc);
764
Steve Narofff7683302008-09-22 10:28:57 +0000765 // Need to support trailing type qualifiers (e.g. "id<p> const").
766 // If a type specifier follows, it will be diagnosed elsewhere.
767 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000768 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000769
770 // type-name
771 case tok::annot_template_id: {
772 TemplateIdAnnotation *TemplateId
773 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000774 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000775 // This template-id does not refer to a type name, so we're
776 // done with the type-specifiers.
777 goto DoneWithDeclSpec;
778 }
779
780 // Turn the template-id annotation token into a type annotation
781 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000782 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000783 continue;
784 }
785
Chris Lattner4b009652007-07-25 00:24:17 +0000786 // GNU attributes support.
787 case tok::kw___attribute:
788 DS.AddAttributes(ParseAttributes());
789 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000790
791 // Microsoft declspec support.
792 case tok::kw___declspec:
793 if (!PP.getLangOptions().Microsoft)
794 goto DoneWithDeclSpec;
795 FuzzyParseMicrosoftDeclSpec();
796 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000797
Steve Naroffedd04d52008-12-25 14:16:32 +0000798 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000799 case tok::kw___forceinline:
800 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000801 case tok::kw___cdecl:
802 case tok::kw___stdcall:
803 case tok::kw___fastcall:
804 if (!PP.getLangOptions().Microsoft)
805 goto DoneWithDeclSpec;
806 // Just ignore it.
807 break;
808
Chris Lattner4b009652007-07-25 00:24:17 +0000809 // storage-class-specifier
810 case tok::kw_typedef:
811 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
812 break;
813 case tok::kw_extern:
814 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000815 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000816 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
817 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000818 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000819 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
820 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000821 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000822 case tok::kw_static:
823 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000824 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000825 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
826 break;
827 case tok::kw_auto:
828 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
829 break;
830 case tok::kw_register:
831 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
832 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000833 case tok::kw_mutable:
834 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
835 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000836 case tok::kw___thread:
837 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
838 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000839
Chris Lattner4b009652007-07-25 00:24:17 +0000840 // function-specifier
841 case tok::kw_inline:
842 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
843 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000844 case tok::kw_virtual:
845 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
846 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000847 case tok::kw_explicit:
848 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
849 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000850
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000851 // friend
852 case tok::kw_friend:
853 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
854 break;
855
Chris Lattnerc297b722009-01-21 19:48:37 +0000856 // type-specifier
857 case tok::kw_short:
858 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
859 break;
860 case tok::kw_long:
861 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
862 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
863 else
864 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
865 break;
866 case tok::kw_signed:
867 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
868 break;
869 case tok::kw_unsigned:
870 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
871 break;
872 case tok::kw__Complex:
873 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
874 break;
875 case tok::kw__Imaginary:
876 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
877 break;
878 case tok::kw_void:
879 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
880 break;
881 case tok::kw_char:
882 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
883 break;
884 case tok::kw_int:
885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
886 break;
887 case tok::kw_float:
888 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
889 break;
890 case tok::kw_double:
891 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
892 break;
893 case tok::kw_wchar_t:
894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
895 break;
896 case tok::kw_bool:
897 case tok::kw__Bool:
898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
899 break;
900 case tok::kw__Decimal32:
901 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
902 break;
903 case tok::kw__Decimal64:
904 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
905 break;
906 case tok::kw__Decimal128:
907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
908 break;
909
910 // class-specifier:
911 case tok::kw_class:
912 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000913 case tok::kw_union: {
914 tok::TokenKind Kind = Tok.getKind();
915 ConsumeToken();
916 ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000917 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000918 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000919
920 // enum-specifier:
921 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000922 ConsumeToken();
923 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000924 continue;
925
926 // cv-qualifier:
927 case tok::kw_const:
928 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
929 break;
930 case tok::kw_volatile:
931 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
932 getLang())*2;
933 break;
934 case tok::kw_restrict:
935 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
936 getLang())*2;
937 break;
938
Douglas Gregord3022602009-03-27 23:10:48 +0000939 // C++ typename-specifier:
940 case tok::kw_typename:
941 if (TryAnnotateTypeOrScopeToken())
942 continue;
943 break;
944
Chris Lattnerc297b722009-01-21 19:48:37 +0000945 // GNU typeof support.
946 case tok::kw_typeof:
947 ParseTypeofSpecifier(DS);
948 continue;
949
Steve Naroff5f0466b2008-06-05 00:02:44 +0000950 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000951 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000952 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
953 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000954 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000955 goto DoneWithDeclSpec;
956
957 {
958 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000959 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000960 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000961 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000962 DS.SetRangeEnd(EndProtoLoc);
963
Chris Lattnerf006a222008-11-18 07:48:38 +0000964 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000965 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000966 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000967 // Need to support trailing type qualifiers (e.g. "id<p> const").
968 // If a type specifier follows, it will be diagnosed elsewhere.
969 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000970 }
Chris Lattner4b009652007-07-25 00:24:17 +0000971 }
972 // If the specifier combination wasn't legal, issue a diagnostic.
973 if (isInvalid) {
974 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000975 // Pick between error or extwarn.
976 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
977 : diag::ext_duplicate_declspec;
978 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000979 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000980 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000981 ConsumeToken();
982 }
983}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000984
Chris Lattnerd706dc82009-01-06 06:59:53 +0000985/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000986/// primarily follow the C++ grammar with additions for C99 and GNU,
987/// which together subsume the C grammar. Note that the C++
988/// type-specifier also includes the C type-qualifier (for const,
989/// volatile, and C99 restrict). Returns true if a type-specifier was
990/// found (and parsed), false otherwise.
991///
992/// type-specifier: [C++ 7.1.5]
993/// simple-type-specifier
994/// class-specifier
995/// enum-specifier
996/// elaborated-type-specifier [TODO]
997/// cv-qualifier
998///
999/// cv-qualifier: [C++ 7.1.5.1]
1000/// 'const'
1001/// 'volatile'
1002/// [C99] 'restrict'
1003///
1004/// simple-type-specifier: [ C++ 7.1.5.2]
1005/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1006/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1007/// 'char'
1008/// 'wchar_t'
1009/// 'bool'
1010/// 'short'
1011/// 'int'
1012/// 'long'
1013/// 'signed'
1014/// 'unsigned'
1015/// 'float'
1016/// 'double'
1017/// 'void'
1018/// [C99] '_Bool'
1019/// [C99] '_Complex'
1020/// [C99] '_Imaginary' // Removed in TC2?
1021/// [GNU] '_Decimal32'
1022/// [GNU] '_Decimal64'
1023/// [GNU] '_Decimal128'
1024/// [GNU] typeof-specifier
1025/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1026/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +00001027bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1028 const char *&PrevSpec,
1029 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001030 SourceLocation Loc = Tok.getLocation();
1031
1032 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001033 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001034 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001035 // Annotate typenames and C++ scope specifiers. If we get one, just
1036 // recurse to handle whatever we get.
1037 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001038 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001039 // Otherwise, not a type specifier.
1040 return false;
1041 case tok::coloncolon: // ::foo::bar
1042 if (NextToken().is(tok::kw_new) || // ::new
1043 NextToken().is(tok::kw_delete)) // ::delete
1044 return false;
1045
1046 // Annotate typenames and C++ scope specifiers. If we get one, just
1047 // recurse to handle whatever we get.
1048 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001049 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001050 // Otherwise, not a type specifier.
1051 return false;
1052
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001053 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001054 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001055 if (Tok.getAnnotationValue())
1056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1057 Tok.getAnnotationValue());
1058 else
1059 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001060 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1061 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001062
1063 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1064 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1065 // Objective-C interface. If we don't have Objective-C or a '<', this is
1066 // just a normal reference to a typedef name.
1067 if (!Tok.is(tok::less) || !getLang().ObjC1)
1068 return true;
1069
1070 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001071 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001072 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1073 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1074
1075 DS.SetRangeEnd(EndProtoLoc);
1076 return true;
1077 }
1078
1079 case tok::kw_short:
1080 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1081 break;
1082 case tok::kw_long:
1083 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1084 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1085 else
1086 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1087 break;
1088 case tok::kw_signed:
1089 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1090 break;
1091 case tok::kw_unsigned:
1092 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1093 break;
1094 case tok::kw__Complex:
1095 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1096 break;
1097 case tok::kw__Imaginary:
1098 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1099 break;
1100 case tok::kw_void:
1101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1102 break;
1103 case tok::kw_char:
1104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1105 break;
1106 case tok::kw_int:
1107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1108 break;
1109 case tok::kw_float:
1110 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1111 break;
1112 case tok::kw_double:
1113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1114 break;
1115 case tok::kw_wchar_t:
1116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1117 break;
1118 case tok::kw_bool:
1119 case tok::kw__Bool:
1120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1121 break;
1122 case tok::kw__Decimal32:
1123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1124 break;
1125 case tok::kw__Decimal64:
1126 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1127 break;
1128 case tok::kw__Decimal128:
1129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1130 break;
1131
1132 // class-specifier:
1133 case tok::kw_class:
1134 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001135 case tok::kw_union: {
1136 tok::TokenKind Kind = Tok.getKind();
1137 ConsumeToken();
1138 ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001139 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001140 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001141
1142 // enum-specifier:
1143 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001144 ConsumeToken();
1145 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001146 return true;
1147
1148 // cv-qualifier:
1149 case tok::kw_const:
1150 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1151 getLang())*2;
1152 break;
1153 case tok::kw_volatile:
1154 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1155 getLang())*2;
1156 break;
1157 case tok::kw_restrict:
1158 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1159 getLang())*2;
1160 break;
1161
1162 // GNU typeof support.
1163 case tok::kw_typeof:
1164 ParseTypeofSpecifier(DS);
1165 return true;
1166
Steve Naroffedd04d52008-12-25 14:16:32 +00001167 case tok::kw___cdecl:
1168 case tok::kw___stdcall:
1169 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001170 if (!PP.getLangOptions().Microsoft) return false;
1171 ConsumeToken();
1172 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001173
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001174 default:
1175 // Not a type-specifier; do nothing.
1176 return false;
1177 }
1178
1179 // If the specifier combination wasn't legal, issue a diagnostic.
1180 if (isInvalid) {
1181 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001182 // Pick between error or extwarn.
1183 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1184 : diag::ext_duplicate_declspec;
1185 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001186 }
1187 DS.SetRangeEnd(Tok.getLocation());
1188 ConsumeToken(); // whatever we parsed above.
1189 return true;
1190}
Chris Lattner4b009652007-07-25 00:24:17 +00001191
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001192/// ParseStructDeclaration - Parse a struct declaration without the terminating
1193/// semicolon.
1194///
Chris Lattner4b009652007-07-25 00:24:17 +00001195/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001196/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001197/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001198/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001199/// struct-declarator-list:
1200/// struct-declarator
1201/// struct-declarator-list ',' struct-declarator
1202/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1203/// struct-declarator:
1204/// declarator
1205/// [GNU] declarator attributes[opt]
1206/// declarator[opt] ':' constant-expression
1207/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1208///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001209void Parser::
1210ParseStructDeclaration(DeclSpec &DS,
1211 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001212 if (Tok.is(tok::kw___extension__)) {
1213 // __extension__ silences extension warnings in the subexpression.
1214 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001215 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001216 return ParseStructDeclaration(DS, Fields);
1217 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001218
1219 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001220 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001221 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001222
Douglas Gregorb748fc52009-01-12 22:49:06 +00001223 // If there are no declarators, this is a free-standing declaration
1224 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001225 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001226 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001227 return;
1228 }
1229
1230 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001231 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001232 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001233 FieldDeclarator &DeclaratorInfo = Fields.back();
1234
Steve Naroffa9adf112007-08-20 22:28:22 +00001235 /// struct-declarator: declarator
1236 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001237 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001238 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001239
Chris Lattner34a01ad2007-10-09 17:33:22 +00001240 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001241 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001242 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001243 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001244 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001245 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001246 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001247 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001248
Steve Naroffa9adf112007-08-20 22:28:22 +00001249 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001250 if (Tok.is(tok::kw___attribute)) {
1251 SourceLocation Loc;
1252 AttributeList *AttrList = ParseAttributes(&Loc);
1253 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1254 }
1255
Steve Naroffa9adf112007-08-20 22:28:22 +00001256 // If we don't have a comma, it is either the end of the list (a ';')
1257 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001258 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001259 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001260
Steve Naroffa9adf112007-08-20 22:28:22 +00001261 // Consume the comma.
1262 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001263
Steve Naroffa9adf112007-08-20 22:28:22 +00001264 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001265 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001266
Steve Naroffa9adf112007-08-20 22:28:22 +00001267 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001268 if (Tok.is(tok::kw___attribute)) {
1269 SourceLocation Loc;
1270 AttributeList *AttrList = ParseAttributes(&Loc);
1271 Fields.back().D.AddAttributes(AttrList, Loc);
1272 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001273 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001274}
1275
1276/// ParseStructUnionBody
1277/// struct-contents:
1278/// struct-declaration-list
1279/// [EXT] empty
1280/// [GNU] "struct-declaration-list" without terminatoring ';'
1281/// struct-declaration-list:
1282/// struct-declaration
1283/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001284/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001285///
Chris Lattner4b009652007-07-25 00:24:17 +00001286void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001287 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001288 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1289 PP.getSourceManager(),
1290 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001291
Chris Lattner4b009652007-07-25 00:24:17 +00001292 SourceLocation LBraceLoc = ConsumeBrace();
1293
Douglas Gregorcab994d2009-01-09 22:42:13 +00001294 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001295 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1296
Chris Lattner4b009652007-07-25 00:24:17 +00001297 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1298 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001299 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001300 Diag(Tok, diag::ext_empty_struct_union_enum)
1301 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001302
Chris Lattner5261d0c2009-03-28 19:18:32 +00001303 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001304 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1305
Chris Lattner4b009652007-07-25 00:24:17 +00001306 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001307 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001308 // Each iteration of this loop reads one struct-declaration.
1309
1310 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001311 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001312 Diag(Tok, diag::ext_extra_struct_semi)
1313 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001314 ConsumeToken();
1315 continue;
1316 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001317
1318 // Parse all the comma separated declarators.
1319 DeclSpec DS;
1320 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001321 if (!Tok.is(tok::at)) {
1322 ParseStructDeclaration(DS, FieldDeclarators);
1323
1324 // Convert them all to fields.
1325 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1326 FieldDeclarator &FD = FieldDeclarators[i];
1327 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001328 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1329 DS.getSourceRange().getBegin(),
1330 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001331 FieldDecls.push_back(Field);
1332 }
1333 } else { // Handle @defs
1334 ConsumeToken();
1335 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1336 Diag(Tok, diag::err_unexpected_at);
1337 SkipUntil(tok::semi, true, true);
1338 continue;
1339 }
1340 ConsumeToken();
1341 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1342 if (!Tok.is(tok::identifier)) {
1343 Diag(Tok, diag::err_expected_ident);
1344 SkipUntil(tok::semi, true, true);
1345 continue;
1346 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001347 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001348 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1349 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001350 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1351 ConsumeToken();
1352 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1353 }
Chris Lattner4b009652007-07-25 00:24:17 +00001354
Chris Lattner34a01ad2007-10-09 17:33:22 +00001355 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001356 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001357 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001358 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001359 break;
1360 } else {
1361 Diag(Tok, diag::err_expected_semi_decl_list);
1362 // Skip to end of block or statement
1363 SkipUntil(tok::r_brace, true, true);
1364 }
1365 }
1366
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001367 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001368
Chris Lattner4b009652007-07-25 00:24:17 +00001369 AttributeList *AttrList = 0;
1370 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001371 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001372 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001373
1374 Actions.ActOnFields(CurScope,
1375 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1376 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001377 AttrList);
1378 StructScope.Exit();
1379 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001380}
1381
1382
1383/// ParseEnumSpecifier
1384/// enum-specifier: [C99 6.7.2.2]
1385/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001386///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001387/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1388/// '}' attributes[opt]
1389/// 'enum' identifier
1390/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001391///
1392/// [C++] elaborated-type-specifier:
1393/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1394///
Chris Lattner197b4342009-04-12 21:49:30 +00001395void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1396 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001397 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001398
1399 AttributeList *Attr = 0;
1400 // If attributes exist after tag, parse them.
1401 if (Tok.is(tok::kw___attribute))
1402 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001403
1404 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001405 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001406 if (Tok.isNot(tok::identifier)) {
1407 Diag(Tok, diag::err_expected_ident);
1408 if (Tok.isNot(tok::l_brace)) {
1409 // Has no name and is not a definition.
1410 // Skip the rest of this declarator, up until the comma or semicolon.
1411 SkipUntil(tok::comma, true);
1412 return;
1413 }
1414 }
1415 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001416
1417 // Must have either 'enum name' or 'enum {...}'.
1418 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1419 Diag(Tok, diag::err_expected_ident_lbrace);
1420
1421 // Skip the rest of this declarator, up until the comma or semicolon.
1422 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001423 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001424 }
1425
1426 // If an identifier is present, consume and remember it.
1427 IdentifierInfo *Name = 0;
1428 SourceLocation NameLoc;
1429 if (Tok.is(tok::identifier)) {
1430 Name = Tok.getIdentifierInfo();
1431 NameLoc = ConsumeToken();
1432 }
1433
1434 // There are three options here. If we have 'enum foo;', then this is a
1435 // forward declaration. If we have 'enum foo {...' then this is a
1436 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1437 //
1438 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1439 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1440 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1441 //
1442 Action::TagKind TK;
1443 if (Tok.is(tok::l_brace))
1444 TK = Action::TK_Definition;
1445 else if (Tok.is(tok::semi))
1446 TK = Action::TK_Declaration;
1447 else
1448 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001449 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1450 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001451
Chris Lattner34a01ad2007-10-09 17:33:22 +00001452 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001453 ParseEnumBody(StartLoc, TagDecl);
1454
1455 // TODO: semantic analysis on the declspec for enums.
1456 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001457 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1458 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001459 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001460}
1461
1462/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1463/// enumerator-list:
1464/// enumerator
1465/// enumerator-list ',' enumerator
1466/// enumerator:
1467/// enumeration-constant
1468/// enumeration-constant '=' constant-expression
1469/// enumeration-constant:
1470/// identifier
1471///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001472void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001473 // Enter the scope of the enum body and start the definition.
1474 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001475 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001476
Chris Lattner4b009652007-07-25 00:24:17 +00001477 SourceLocation LBraceLoc = ConsumeBrace();
1478
Chris Lattnerc9a92452007-08-27 17:24:30 +00001479 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001480 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001481 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001482
Chris Lattner5261d0c2009-03-28 19:18:32 +00001483 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001484
Chris Lattner5261d0c2009-03-28 19:18:32 +00001485 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001486
1487 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001488 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001489 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1490 SourceLocation IdentLoc = ConsumeToken();
1491
1492 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001493 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001494 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001495 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001496 AssignedVal = ParseConstantExpression();
1497 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001498 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001499 }
1500
1501 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001502 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1503 LastEnumConstDecl,
1504 IdentLoc, Ident,
1505 EqualLoc,
1506 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001507 EnumConstantDecls.push_back(EnumConstDecl);
1508 LastEnumConstDecl = EnumConstDecl;
1509
Chris Lattner34a01ad2007-10-09 17:33:22 +00001510 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001511 break;
1512 SourceLocation CommaLoc = ConsumeToken();
1513
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001514 if (Tok.isNot(tok::identifier) &&
1515 !(getLang().C99 || getLang().CPlusPlus0x))
1516 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1517 << getLang().CPlusPlus
1518 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001519 }
1520
1521 // Eat the }.
1522 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1523
Steve Naroff0acc9c92007-09-15 18:49:24 +00001524 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001525 EnumConstantDecls.size());
1526
Chris Lattner5261d0c2009-03-28 19:18:32 +00001527 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001528 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001529 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001530 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001531
1532 EnumScope.Exit();
1533 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001534}
1535
1536/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001537/// start of a type-qualifier-list.
1538bool Parser::isTypeQualifier() const {
1539 switch (Tok.getKind()) {
1540 default: return false;
1541 // type-qualifier
1542 case tok::kw_const:
1543 case tok::kw_volatile:
1544 case tok::kw_restrict:
1545 return true;
1546 }
1547}
1548
1549/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001550/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001551bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001552 switch (Tok.getKind()) {
1553 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001554
1555 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001556 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001557 // Annotate typenames and C++ scope specifiers. If we get one, just
1558 // recurse to handle whatever we get.
1559 if (TryAnnotateTypeOrScopeToken())
1560 return isTypeSpecifierQualifier();
1561 // Otherwise, not a type specifier.
1562 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001563
Chris Lattnerb75fde62009-01-04 23:41:41 +00001564 case tok::coloncolon: // ::foo::bar
1565 if (NextToken().is(tok::kw_new) || // ::new
1566 NextToken().is(tok::kw_delete)) // ::delete
1567 return false;
1568
1569 // Annotate typenames and C++ scope specifiers. If we get one, just
1570 // recurse to handle whatever we get.
1571 if (TryAnnotateTypeOrScopeToken())
1572 return isTypeSpecifierQualifier();
1573 // Otherwise, not a type specifier.
1574 return false;
1575
Chris Lattner4b009652007-07-25 00:24:17 +00001576 // GNU attributes support.
1577 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001578 // GNU typeof support.
1579 case tok::kw_typeof:
1580
Chris Lattner4b009652007-07-25 00:24:17 +00001581 // type-specifiers
1582 case tok::kw_short:
1583 case tok::kw_long:
1584 case tok::kw_signed:
1585 case tok::kw_unsigned:
1586 case tok::kw__Complex:
1587 case tok::kw__Imaginary:
1588 case tok::kw_void:
1589 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001590 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001591 case tok::kw_int:
1592 case tok::kw_float:
1593 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001594 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001595 case tok::kw__Bool:
1596 case tok::kw__Decimal32:
1597 case tok::kw__Decimal64:
1598 case tok::kw__Decimal128:
1599
Chris Lattner2e78db32008-04-13 18:59:07 +00001600 // struct-or-union-specifier (C99) or class-specifier (C++)
1601 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001602 case tok::kw_struct:
1603 case tok::kw_union:
1604 // enum-specifier
1605 case tok::kw_enum:
1606
1607 // type-qualifier
1608 case tok::kw_const:
1609 case tok::kw_volatile:
1610 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001611
1612 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001613 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001614 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001615
1616 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1617 case tok::less:
1618 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001619
1620 case tok::kw___cdecl:
1621 case tok::kw___stdcall:
1622 case tok::kw___fastcall:
1623 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001624 }
1625}
1626
1627/// isDeclarationSpecifier() - Return true if the current token is part of a
1628/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001629bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001630 switch (Tok.getKind()) {
1631 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001632
1633 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001634 // Unfortunate hack to support "Class.factoryMethod" notation.
1635 if (getLang().ObjC1 && NextToken().is(tok::period))
1636 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001637 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001638
Douglas Gregord3022602009-03-27 23:10:48 +00001639 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001640 // Annotate typenames and C++ scope specifiers. If we get one, just
1641 // recurse to handle whatever we get.
1642 if (TryAnnotateTypeOrScopeToken())
1643 return isDeclarationSpecifier();
1644 // Otherwise, not a declaration specifier.
1645 return false;
1646 case tok::coloncolon: // ::foo::bar
1647 if (NextToken().is(tok::kw_new) || // ::new
1648 NextToken().is(tok::kw_delete)) // ::delete
1649 return false;
1650
1651 // Annotate typenames and C++ scope specifiers. If we get one, just
1652 // recurse to handle whatever we get.
1653 if (TryAnnotateTypeOrScopeToken())
1654 return isDeclarationSpecifier();
1655 // Otherwise, not a declaration specifier.
1656 return false;
1657
Chris Lattner4b009652007-07-25 00:24:17 +00001658 // storage-class-specifier
1659 case tok::kw_typedef:
1660 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001661 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001662 case tok::kw_static:
1663 case tok::kw_auto:
1664 case tok::kw_register:
1665 case tok::kw___thread:
1666
1667 // type-specifiers
1668 case tok::kw_short:
1669 case tok::kw_long:
1670 case tok::kw_signed:
1671 case tok::kw_unsigned:
1672 case tok::kw__Complex:
1673 case tok::kw__Imaginary:
1674 case tok::kw_void:
1675 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001676 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001677 case tok::kw_int:
1678 case tok::kw_float:
1679 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001680 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001681 case tok::kw__Bool:
1682 case tok::kw__Decimal32:
1683 case tok::kw__Decimal64:
1684 case tok::kw__Decimal128:
1685
Chris Lattner2e78db32008-04-13 18:59:07 +00001686 // struct-or-union-specifier (C99) or class-specifier (C++)
1687 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001688 case tok::kw_struct:
1689 case tok::kw_union:
1690 // enum-specifier
1691 case tok::kw_enum:
1692
1693 // type-qualifier
1694 case tok::kw_const:
1695 case tok::kw_volatile:
1696 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001697
Chris Lattner4b009652007-07-25 00:24:17 +00001698 // function-specifier
1699 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001700 case tok::kw_virtual:
1701 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001702
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001703 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001704 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001705
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001706 // GNU typeof support.
1707 case tok::kw_typeof:
1708
1709 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001710 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001711 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001712
1713 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1714 case tok::less:
1715 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001716
Steve Naroffab1a3632009-01-06 19:34:12 +00001717 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001718 case tok::kw___cdecl:
1719 case tok::kw___stdcall:
1720 case tok::kw___fastcall:
1721 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001722 }
1723}
1724
1725
1726/// ParseTypeQualifierListOpt
1727/// type-qualifier-list: [C99 6.7.5]
1728/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001729/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001730/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001731/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001732///
Chris Lattner460696f2008-12-18 07:02:59 +00001733void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001734 while (1) {
1735 int isInvalid = false;
1736 const char *PrevSpec = 0;
1737 SourceLocation Loc = Tok.getLocation();
1738
1739 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001740 case tok::kw_const:
1741 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1742 getLang())*2;
1743 break;
1744 case tok::kw_volatile:
1745 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1746 getLang())*2;
1747 break;
1748 case tok::kw_restrict:
1749 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1750 getLang())*2;
1751 break;
Steve Naroffad620402008-12-25 14:41:26 +00001752 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001753 case tok::kw___cdecl:
1754 case tok::kw___stdcall:
1755 case tok::kw___fastcall:
1756 if (!PP.getLangOptions().Microsoft)
1757 goto DoneWithTypeQuals;
1758 // Just ignore it.
1759 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001760 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001761 if (AttributesAllowed) {
1762 DS.AddAttributes(ParseAttributes());
1763 continue; // do *not* consume the next token!
1764 }
1765 // otherwise, FALL THROUGH!
1766 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001767 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001768 // If this is not a type-qualifier token, we're done reading type
1769 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001770 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001771 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001772 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001773
Chris Lattner4b009652007-07-25 00:24:17 +00001774 // If the specifier combination wasn't legal, issue a diagnostic.
1775 if (isInvalid) {
1776 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001777 // Pick between error or extwarn.
1778 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1779 : diag::ext_duplicate_declspec;
1780 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001781 }
1782 ConsumeToken();
1783 }
1784}
1785
1786
1787/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1788///
1789void Parser::ParseDeclarator(Declarator &D) {
1790 /// This implements the 'declarator' production in the C grammar, then checks
1791 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001792 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001793}
1794
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001795/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1796/// is parsed by the function passed to it. Pass null, and the direct-declarator
1797/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001798/// ptr-operator production.
1799///
Sebastian Redl75555032009-01-24 21:16:55 +00001800/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1801/// [C] pointer[opt] direct-declarator
1802/// [C++] direct-declarator
1803/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001804///
1805/// pointer: [C99 6.7.5]
1806/// '*' type-qualifier-list[opt]
1807/// '*' type-qualifier-list[opt] pointer
1808///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001809/// ptr-operator:
1810/// '*' cv-qualifier-seq[opt]
1811/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001812/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001813/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001814/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001815/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001816void Parser::ParseDeclaratorInternal(Declarator &D,
1817 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001818
Sebastian Redl75555032009-01-24 21:16:55 +00001819 // C++ member pointers start with a '::' or a nested-name.
1820 // Member pointers get special handling, since there's no place for the
1821 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001822 if (getLang().CPlusPlus &&
1823 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1824 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001825 CXXScopeSpec SS;
1826 if (ParseOptionalCXXScopeSpecifier(SS)) {
1827 if(Tok.isNot(tok::star)) {
1828 // The scope spec really belongs to the direct-declarator.
1829 D.getCXXScopeSpec() = SS;
1830 if (DirectDeclParser)
1831 (this->*DirectDeclParser)(D);
1832 return;
1833 }
1834
1835 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001836 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001837 DeclSpec DS;
1838 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001839 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001840
1841 // Recurse to parse whatever is left.
1842 ParseDeclaratorInternal(D, DirectDeclParser);
1843
1844 // Sema will have to catch (syntactically invalid) pointers into global
1845 // scope. It has to catch pointers into namespace scope anyway.
1846 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001847 Loc, DS.TakeAttributes()),
1848 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001849 return;
1850 }
1851 }
1852
1853 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001854 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001855 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001856 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001857 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001858 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001859 if (DirectDeclParser)
1860 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001861 return;
1862 }
Sebastian Redl75555032009-01-24 21:16:55 +00001863
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001864 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1865 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001866 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001867 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001868
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001869 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001870 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001871 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001872
Chris Lattner4b009652007-07-25 00:24:17 +00001873 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001874 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001875
Chris Lattner4b009652007-07-25 00:24:17 +00001876 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001877 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001878 if (Kind == tok::star)
1879 // Remember that we parsed a pointer type, and remember the type-quals.
1880 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001881 DS.TakeAttributes()),
1882 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001883 else
1884 // Remember that we parsed a Block type, and remember the type-quals.
1885 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001886 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001887 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001888 } else {
1889 // Is a reference
1890 DeclSpec DS;
1891
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001892 // Complain about rvalue references in C++03, but then go on and build
1893 // the declarator.
1894 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1895 Diag(Loc, diag::err_rvalue_reference);
1896
Chris Lattner4b009652007-07-25 00:24:17 +00001897 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1898 // cv-qualifiers are introduced through the use of a typedef or of a
1899 // template type argument, in which case the cv-qualifiers are ignored.
1900 //
1901 // [GNU] Retricted references are allowed.
1902 // [GNU] Attributes on references are allowed.
1903 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001904 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001905
1906 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1907 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1908 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001909 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001910 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1911 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001912 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001913 }
1914
1915 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001916 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001917
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001918 if (D.getNumTypeObjects() > 0) {
1919 // C++ [dcl.ref]p4: There shall be no references to references.
1920 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1921 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001922 if (const IdentifierInfo *II = D.getIdentifier())
1923 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1924 << II;
1925 else
1926 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1927 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001928
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001929 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001930 // can go ahead and build the (technically ill-formed)
1931 // declarator: reference collapsing will take care of it.
1932 }
1933 }
1934
Chris Lattner4b009652007-07-25 00:24:17 +00001935 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001936 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001937 DS.TakeAttributes(),
1938 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001939 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001940 }
1941}
1942
1943/// ParseDirectDeclarator
1944/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001945/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001946/// '(' declarator ')'
1947/// [GNU] '(' attributes declarator ')'
1948/// [C90] direct-declarator '[' constant-expression[opt] ']'
1949/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1950/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1951/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1952/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1953/// direct-declarator '(' parameter-type-list ')'
1954/// direct-declarator '(' identifier-list[opt] ')'
1955/// [GNU] direct-declarator '(' parameter-forward-declarations
1956/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001957/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1958/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001959/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001960///
1961/// declarator-id: [C++ 8]
1962/// id-expression
1963/// '::'[opt] nested-name-specifier[opt] type-name
1964///
1965/// id-expression: [C++ 5.1]
1966/// unqualified-id
1967/// qualified-id [TODO]
1968///
1969/// unqualified-id: [C++ 5.1]
1970/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001971/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001972/// conversion-function-id [TODO]
1973/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001974/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001975///
Chris Lattner4b009652007-07-25 00:24:17 +00001976void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001977 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001978
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001979 if (getLang().CPlusPlus) {
1980 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001981 // ParseDeclaratorInternal might already have parsed the scope.
1982 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1983 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001984 if (afterCXXScope) {
1985 // Change the declaration context for name lookup, until this function
1986 // is exited (and the declarator has been parsed).
1987 DeclScopeObj.EnterDeclaratorScope();
1988 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001989
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001990 if (Tok.is(tok::identifier)) {
1991 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00001992
1993 // If this identifier is the name of the current class, it's a
1994 // constructor name.
1995 if (!D.getDeclSpec().hasTypeSpecifier() &&
1996 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
1997 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
1998 Tok.getLocation(), CurScope),
1999 Tok.getLocation());
2000 // This is a normal identifier.
2001 } else
2002 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002003 ConsumeToken();
2004 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002005 } else if (Tok.is(tok::annot_template_id)) {
2006 TemplateIdAnnotation *TemplateId
2007 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2008
2009 // FIXME: Could this template-id name a constructor?
2010
2011 // FIXME: This is an egregious hack, where we silently ignore
2012 // the specialization (which should be a function template
2013 // specialization name) and use the name instead. This hack
2014 // will go away when we have support for function
2015 // specializations.
2016 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2017 TemplateId->Destroy();
2018 ConsumeToken();
2019 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002020 } else if (Tok.is(tok::kw_operator)) {
2021 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002022 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002023
Douglas Gregor853dd392008-12-26 15:00:45 +00002024 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002025 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2026 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002027 } else {
2028 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002029 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2030 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2031 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002032 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002033 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002034 }
2035 goto PastIdentifier;
2036 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002037 // This should be a C++ destructor.
2038 SourceLocation TildeLoc = ConsumeToken();
2039 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002040 // FIXME: Inaccurate.
2041 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002042 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002043 TypeResult Type = ParseClassName(EndLoc);
2044 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002045 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002046 else
2047 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002048 } else {
2049 Diag(Tok, diag::err_expected_class_name);
2050 D.SetIdentifier(0, TildeLoc);
2051 }
2052 goto PastIdentifier;
2053 }
2054
2055 // If we reached this point, token is not identifier and not '~'.
2056
2057 if (afterCXXScope) {
2058 Diag(Tok, diag::err_expected_unqualified_id);
2059 D.SetIdentifier(0, Tok.getLocation());
2060 D.setInvalidType(true);
2061 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002062 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002063 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002064 }
2065
2066 // If we reached this point, we are either in C/ObjC or the token didn't
2067 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002068 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2069 assert(!getLang().CPlusPlus &&
2070 "There's a C++-specific check for tok::identifier above");
2071 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2072 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2073 ConsumeToken();
2074 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002075 // direct-declarator: '(' declarator ')'
2076 // direct-declarator: '(' attributes declarator ')'
2077 // Example: 'char (*X)' or 'int (*XX)(void)'
2078 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002079 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002080 // This could be something simple like "int" (in which case the declarator
2081 // portion is empty), if an abstract-declarator is allowed.
2082 D.SetIdentifier(0, Tok.getLocation());
2083 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002084 if (D.getContext() == Declarator::MemberContext)
2085 Diag(Tok, diag::err_expected_member_name_or_semi)
2086 << D.getDeclSpec().getSourceRange();
2087 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002088 Diag(Tok, diag::err_expected_unqualified_id);
2089 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002090 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002091 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002092 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002093 }
2094
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002095 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002096 assert(D.isPastIdentifier() &&
2097 "Haven't past the location of the identifier yet?");
2098
2099 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002100 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002101 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2102 // In such a case, check if we actually have a function declarator; if it
2103 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002104 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2105 // When not in file scope, warn for ambiguous function declarators, just
2106 // in case the author intended it as a variable definition.
2107 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2108 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2109 break;
2110 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002111 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002112 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002113 ParseBracketDeclarator(D);
2114 } else {
2115 break;
2116 }
2117 }
2118}
2119
Chris Lattnera0d056d2008-04-06 05:45:57 +00002120/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2121/// only called before the identifier, so these are most likely just grouping
2122/// parens for precedence. If we find that these are actually function
2123/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2124///
2125/// direct-declarator:
2126/// '(' declarator ')'
2127/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002128/// direct-declarator '(' parameter-type-list ')'
2129/// direct-declarator '(' identifier-list[opt] ')'
2130/// [GNU] direct-declarator '(' parameter-forward-declarations
2131/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002132///
2133void Parser::ParseParenDeclarator(Declarator &D) {
2134 SourceLocation StartLoc = ConsumeParen();
2135 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2136
Chris Lattner1f185292008-10-20 02:05:46 +00002137 // Eat any attributes before we look at whether this is a grouping or function
2138 // declarator paren. If this is a grouping paren, the attribute applies to
2139 // the type being built up, for example:
2140 // int (__attribute__(()) *x)(long y)
2141 // If this ends up not being a grouping paren, the attribute applies to the
2142 // first argument, for example:
2143 // int (__attribute__(()) int x)
2144 // In either case, we need to eat any attributes to be able to determine what
2145 // sort of paren this is.
2146 //
2147 AttributeList *AttrList = 0;
2148 bool RequiresArg = false;
2149 if (Tok.is(tok::kw___attribute)) {
2150 AttrList = ParseAttributes();
2151
2152 // We require that the argument list (if this is a non-grouping paren) be
2153 // present even if the attribute list was empty.
2154 RequiresArg = true;
2155 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002156 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002157 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2158 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002159 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002160
Chris Lattnera0d056d2008-04-06 05:45:57 +00002161 // If we haven't past the identifier yet (or where the identifier would be
2162 // stored, if this is an abstract declarator), then this is probably just
2163 // grouping parens. However, if this could be an abstract-declarator, then
2164 // this could also be the start of function arguments (consider 'void()').
2165 bool isGrouping;
2166
2167 if (!D.mayOmitIdentifier()) {
2168 // If this can't be an abstract-declarator, this *must* be a grouping
2169 // paren, because we haven't seen the identifier yet.
2170 isGrouping = true;
2171 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002172 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002173 isDeclarationSpecifier()) { // 'int(int)' is a function.
2174 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2175 // considered to be a type, not a K&R identifier-list.
2176 isGrouping = false;
2177 } else {
2178 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2179 isGrouping = true;
2180 }
2181
2182 // If this is a grouping paren, handle:
2183 // direct-declarator: '(' declarator ')'
2184 // direct-declarator: '(' attributes declarator ')'
2185 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002186 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002187 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002188 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002189 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002190
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002191 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002192 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002193 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002194
2195 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002196 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002197 return;
2198 }
2199
2200 // Okay, if this wasn't a grouping paren, it must be the start of a function
2201 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002202 // identifier (and remember where it would have been), then call into
2203 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002204 D.SetIdentifier(0, Tok.getLocation());
2205
Chris Lattner1f185292008-10-20 02:05:46 +00002206 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002207}
2208
2209/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2210/// declarator D up to a paren, which indicates that we are parsing function
2211/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002212///
Chris Lattner1f185292008-10-20 02:05:46 +00002213/// If AttrList is non-null, then the caller parsed those arguments immediately
2214/// after the open paren - they should be considered to be the first argument of
2215/// a parameter. If RequiresArg is true, then the first argument of the
2216/// function is required to be present and required to not be an identifier
2217/// list.
2218///
Chris Lattner4b009652007-07-25 00:24:17 +00002219/// This method also handles this portion of the grammar:
2220/// parameter-type-list: [C99 6.7.5]
2221/// parameter-list
2222/// parameter-list ',' '...'
2223///
2224/// parameter-list: [C99 6.7.5]
2225/// parameter-declaration
2226/// parameter-list ',' parameter-declaration
2227///
2228/// parameter-declaration: [C99 6.7.5]
2229/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002230/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002231/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002232/// declaration-specifiers abstract-declarator[opt]
2233/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002234/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002235/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2236///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002237/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002238/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002239///
Chris Lattner1f185292008-10-20 02:05:46 +00002240void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2241 AttributeList *AttrList,
2242 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002243 // lparen is already consumed!
2244 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002245
Chris Lattner1f185292008-10-20 02:05:46 +00002246 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002247 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002248 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002249 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002250 delete AttrList;
2251 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002252
Sebastian Redl0c986032009-02-09 18:23:29 +00002253 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002254
2255 // cv-qualifier-seq[opt].
2256 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002257 bool hasExceptionSpec = false;
2258 bool hasAnyExceptionSpec = false;
2259 // FIXME: Does an empty vector ever allocate? Exception specifications are
2260 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2261 std::vector<TypeTy*> Exceptions;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002262 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002263 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002264 if (!DS.getSourceRange().getEnd().isInvalid())
2265 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002266
2267 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002268 if (Tok.is(tok::kw_throw)) {
2269 hasExceptionSpec = true;
2270 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2271 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002272 }
2273
Chris Lattner9f7564b2008-04-06 06:57:35 +00002274 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002275 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002276 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002277 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002278 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002279 /*arglist*/ 0, 0,
2280 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002281 hasExceptionSpec,
2282 hasAnyExceptionSpec,
2283 Exceptions.empty() ? 0 :
2284 &Exceptions[0],
2285 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002286 LParenLoc, D),
2287 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002288 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002289 }
2290
2291 // Alternatively, this parameter list may be an identifier list form for a
2292 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002293 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002294 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002295 // K&R identifier lists can't have typedefs as identifiers, per
2296 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002297 if (RequiresArg) {
2298 Diag(Tok, diag::err_argument_required_after_attribute);
2299 delete AttrList;
2300 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002301 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2302 // normal declarators, not for abstract-declarators.
2303 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002304 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002305 }
2306
2307 // Finally, a normal, non-empty parameter type list.
2308
2309 // Build up an array of information about the parsed arguments.
2310 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002311
2312 // Enter function-declaration scope, limiting any declarators to the
2313 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002314 ParseScope PrototypeScope(this,
2315 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002316
2317 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002318 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002319 while (1) {
2320 if (Tok.is(tok::ellipsis)) {
2321 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002322 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002323 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002324 }
2325
Chris Lattner9f7564b2008-04-06 06:57:35 +00002326 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002327
Chris Lattner9f7564b2008-04-06 06:57:35 +00002328 // Parse the declaration-specifiers.
2329 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002330
2331 // If the caller parsed attributes for the first argument, add them now.
2332 if (AttrList) {
2333 DS.AddAttributes(AttrList);
2334 AttrList = 0; // Only apply the attributes to the first parameter.
2335 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002336 ParseDeclarationSpecifiers(DS);
2337
Chris Lattner9f7564b2008-04-06 06:57:35 +00002338 // Parse the declarator. This is "PrototypeContext", because we must
2339 // accept either 'declarator' or 'abstract-declarator' here.
2340 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2341 ParseDeclarator(ParmDecl);
2342
2343 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002344 if (Tok.is(tok::kw___attribute)) {
2345 SourceLocation Loc;
2346 AttributeList *AttrList = ParseAttributes(&Loc);
2347 ParmDecl.AddAttributes(AttrList, Loc);
2348 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002349
Chris Lattner9f7564b2008-04-06 06:57:35 +00002350 // Remember this parsed parameter in ParamInfo.
2351 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2352
Douglas Gregor605de8d2008-12-16 21:30:33 +00002353 // DefArgToks is used when the parsing of default arguments needs
2354 // to be delayed.
2355 CachedTokens *DefArgToks = 0;
2356
Chris Lattner9f7564b2008-04-06 06:57:35 +00002357 // If no parameter was specified, verify that *something* was specified,
2358 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002359 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2360 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002361 // Completely missing, emit error.
2362 Diag(DSStart, diag::err_missing_param);
2363 } else {
2364 // Otherwise, we have something. Add it and let semantic analysis try
2365 // to grok it and add the result to the ParamInfo we are building.
2366
2367 // Inform the actions module about the parameter declarator, so it gets
2368 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002369 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002370
2371 // Parse the default argument, if any. We parse the default
2372 // arguments in all dialects; the semantic analysis in
2373 // ActOnParamDefaultArgument will reject the default argument in
2374 // C.
2375 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002376 SourceLocation EqualLoc = Tok.getLocation();
2377
Chris Lattner3e254fb2008-04-08 04:40:51 +00002378 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002379 if (D.getContext() == Declarator::MemberContext) {
2380 // If we're inside a class definition, cache the tokens
2381 // corresponding to the default argument. We'll actually parse
2382 // them when we see the end of the class definition.
2383 // FIXME: Templates will require something similar.
2384 // FIXME: Can we use a smart pointer for Toks?
2385 DefArgToks = new CachedTokens;
2386
2387 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2388 tok::semi, false)) {
2389 delete DefArgToks;
2390 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002391 Actions.ActOnParamDefaultArgumentError(Param);
2392 } else
2393 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002394 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002395 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002396 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002397
2398 OwningExprResult DefArgResult(ParseAssignmentExpression());
2399 if (DefArgResult.isInvalid()) {
2400 Actions.ActOnParamDefaultArgumentError(Param);
2401 SkipUntil(tok::comma, tok::r_paren, true, true);
2402 } else {
2403 // Inform the actions module about the default argument
2404 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002405 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002406 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002407 }
2408 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002409
2410 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002411 ParmDecl.getIdentifierLoc(), Param,
2412 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002413 }
2414
2415 // If the next token is a comma, consume it and keep reading arguments.
2416 if (Tok.isNot(tok::comma)) break;
2417
2418 // Consume the comma.
2419 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002420 }
2421
Chris Lattner9f7564b2008-04-06 06:57:35 +00002422 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002423 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002424
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002425 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002426 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002427
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002428 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002429 bool hasExceptionSpec = false;
2430 bool hasAnyExceptionSpec = false;
2431 // FIXME: Does an empty vector ever allocate? Exception specifications are
2432 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2433 std::vector<TypeTy*> Exceptions;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002434 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002435 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002436 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002437 if (!DS.getSourceRange().getEnd().isInvalid())
2438 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002439
2440 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002441 if (Tok.is(tok::kw_throw)) {
2442 hasExceptionSpec = true;
2443 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2444 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002445 }
2446
Chris Lattner4b009652007-07-25 00:24:17 +00002447 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002448 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002449 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002450 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002451 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002452 hasExceptionSpec,
2453 hasAnyExceptionSpec,
2454 Exceptions.empty() ? 0 :
2455 &Exceptions[0],
2456 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002457 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002458}
2459
Chris Lattner35d9c912008-04-06 06:34:08 +00002460/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2461/// we found a K&R-style identifier list instead of a type argument list. The
2462/// current token is known to be the first identifier in the list.
2463///
2464/// identifier-list: [C99 6.7.5]
2465/// identifier
2466/// identifier-list ',' identifier
2467///
2468void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2469 Declarator &D) {
2470 // Build up an array of information about the parsed arguments.
2471 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2472 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2473
2474 // If there was no identifier specified for the declarator, either we are in
2475 // an abstract-declarator, or we are in a parameter declarator which was found
2476 // to be abstract. In abstract-declarators, identifier lists are not valid:
2477 // diagnose this.
2478 if (!D.getIdentifier())
2479 Diag(Tok, diag::ext_ident_list_in_param);
2480
2481 // Tok is known to be the first identifier in the list. Remember this
2482 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002483 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002484 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002485 Tok.getLocation(),
2486 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002487
Chris Lattner113a56b2008-04-06 06:39:19 +00002488 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002489
2490 while (Tok.is(tok::comma)) {
2491 // Eat the comma.
2492 ConsumeToken();
2493
Chris Lattner113a56b2008-04-06 06:39:19 +00002494 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002495 if (Tok.isNot(tok::identifier)) {
2496 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002497 SkipUntil(tok::r_paren);
2498 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002499 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002500
Chris Lattner35d9c912008-04-06 06:34:08 +00002501 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002502
2503 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002504 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002505 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002506
2507 // Verify that the argument identifier has not already been mentioned.
2508 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002509 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002510 } else {
2511 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002512 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002513 Tok.getLocation(),
2514 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002515 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002516
2517 // Eat the identifier.
2518 ConsumeToken();
2519 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002520
2521 // If we have the closing ')', eat it and we're done.
2522 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2523
Chris Lattner113a56b2008-04-06 06:39:19 +00002524 // Remember that we parsed a function type, and remember the attributes. This
2525 // function type is always a K&R style function type, which is not varargs and
2526 // has no prototype.
2527 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002528 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002529 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002530 /*TypeQuals*/0,
2531 /*exception*/false, false, 0, 0,
2532 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002533 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002534}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002535
Chris Lattner4b009652007-07-25 00:24:17 +00002536/// [C90] direct-declarator '[' constant-expression[opt] ']'
2537/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2538/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2539/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2540/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2541void Parser::ParseBracketDeclarator(Declarator &D) {
2542 SourceLocation StartLoc = ConsumeBracket();
2543
Chris Lattner1525c3a2008-12-18 07:27:21 +00002544 // C array syntax has many features, but by-far the most common is [] and [4].
2545 // This code does a fast path to handle some of the most obvious cases.
2546 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002547 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002548 // Remember that we parsed the empty array type.
2549 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002550 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2551 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002552 return;
2553 } else if (Tok.getKind() == tok::numeric_constant &&
2554 GetLookAheadToken(1).is(tok::r_square)) {
2555 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002556 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002557 ConsumeToken();
2558
Sebastian Redl0c986032009-02-09 18:23:29 +00002559 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002560
2561 // If there was an error parsing the assignment-expression, recover.
2562 if (ExprRes.isInvalid())
2563 ExprRes.release(); // Deallocate expr, just use [].
2564
2565 // Remember that we parsed a array type, and remember its features.
2566 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002567 ExprRes.release(), StartLoc),
2568 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002569 return;
2570 }
2571
Chris Lattner4b009652007-07-25 00:24:17 +00002572 // If valid, this location is the position where we read the 'static' keyword.
2573 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002574 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002575 StaticLoc = ConsumeToken();
2576
2577 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002578 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002579 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002580 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002581
2582 // If we haven't already read 'static', check to see if there is one after the
2583 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002584 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002585 StaticLoc = ConsumeToken();
2586
2587 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2588 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002589 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002590
2591 // Handle the case where we have '[*]' as the array size. However, a leading
2592 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2593 // the the token after the star is a ']'. Since stars in arrays are
2594 // infrequent, use of lookahead is not costly here.
2595 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002596 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002597
Chris Lattner306d4df2008-12-18 06:50:14 +00002598 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002599 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002600 StaticLoc = SourceLocation(); // Drop the static.
2601 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002602 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002603 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002604 // Note, in C89, this production uses the constant-expr production instead
2605 // of assignment-expr. The only difference is that assignment-expr allows
2606 // things like '=' and '*='. Sema rejects these in C89 mode because they
2607 // are not i-c-e's, so we don't need to distinguish between the two here.
2608
Chris Lattner4b009652007-07-25 00:24:17 +00002609 // Parse the assignment-expression now.
2610 NumElements = ParseAssignmentExpression();
2611 }
2612
2613 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002614 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002615 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002616 // If the expression was invalid, skip it.
2617 SkipUntil(tok::r_square);
2618 return;
2619 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002620
2621 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2622
Chris Lattner1525c3a2008-12-18 07:27:21 +00002623 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002624 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2625 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002626 NumElements.release(), StartLoc),
2627 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002628}
2629
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002630/// [GNU] typeof-specifier:
2631/// typeof ( expressions )
2632/// typeof ( type-name )
2633/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002634///
2635void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002636 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002637 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002638 SourceLocation StartLoc = ConsumeToken();
2639
Chris Lattner34a01ad2007-10-09 17:33:22 +00002640 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002641 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002642 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002643 return;
2644 }
2645
Sebastian Redl14ca7412008-12-11 21:36:32 +00002646 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002647 if (Result.isInvalid()) {
2648 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002649 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002650 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002651
2652 const char *PrevSpec = 0;
2653 // Check for duplicate type specifiers.
2654 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002655 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002656 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002657
2658 // FIXME: Not accurate, the range gets one token more than it should.
2659 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002660 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002661 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002662
Steve Naroff7cbb1462007-07-31 12:34:36 +00002663 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2664
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002665 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002666 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002667
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002668 assert((Ty.isInvalid() || Ty.get()) &&
2669 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002670
Chris Lattner34a01ad2007-10-09 17:33:22 +00002671 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002672 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002673 return;
2674 }
2675 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002676
2677 if (Ty.isInvalid())
2678 DS.SetTypeSpecError();
2679 else {
2680 const char *PrevSpec = 0;
2681 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2682 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2683 Ty.get()))
2684 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2685 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002686 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002687 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002688
2689 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002690 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002691 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002692 return;
2693 }
2694 RParenLoc = ConsumeParen();
2695 const char *PrevSpec = 0;
2696 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2697 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002698 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002699 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002700 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002701 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002702}
2703
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002704