blob: e233a2216286153e0e1a619dd40fa134fee5ba03 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattnera17991f2009-03-29 16:50:03 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context) {
232 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000233 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000234 case tok::kw_export:
235 case tok::kw_template:
Chris Lattnera17991f2009-03-29 16:50:03 +0000236 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context);
237 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000238 case tok::kw_namespace:
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 SingleDecl = ParseNamespace(Context);
240 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000241 case tok::kw_using:
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 SingleDecl = ParseUsingDirectiveOrDeclaration(Context);
243 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000244 case tok::kw_static_assert:
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 SingleDecl = ParseStaticAssertDeclaration();
246 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000247 default:
248 return ParseSimpleDeclaration(Context);
249 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000250
251 // This routine returns a DeclGroup, if the thing we parsed only contains a
252 // single decl, convert it now.
253 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000254}
255
256/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
257/// declaration-specifiers init-declarator-list[opt] ';'
258///[C90/C++]init-declarator-list ';' [TODO]
259/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000260///
261/// If RequireSemi is false, this does not check for a ';' at the end of the
262/// declaration.
263Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
264 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // Parse the common declaration-specifiers piece.
266 DeclSpec DS;
267 ParseDeclarationSpecifiers(DS);
268
269 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
270 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000271 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000272 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000273 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
274 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000275 }
276
277 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
278 ParseDeclarator(DeclaratorInfo);
279
Chris Lattner2c41d482009-03-29 17:18:04 +0000280 DeclGroupPtrTy DG =
281 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000282
283 // If the client wants to check what comes after the declaration, just return
284 // immediately without checking anything!
285 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000286
287 if (Tok.is(tok::semi)) {
288 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000289 return DG;
290 }
291
Chris Lattner2c41d482009-03-29 17:18:04 +0000292 Diag(Tok, diag::err_expected_semi_declation);
293 // Skip to end of block or statement
294 SkipUntil(tok::r_brace, true, true);
295 if (Tok.is(tok::semi))
296 ConsumeToken();
297 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000298}
299
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000300
Chris Lattner4b009652007-07-25 00:24:17 +0000301/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
302/// parsing 'declaration-specifiers declarator'. This method is split out this
303/// way to handle the ambiguity between top-level function-definitions and
304/// declarations.
305///
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// init-declarator-list: [C99 6.7]
307/// init-declarator
308/// init-declarator-list ',' init-declarator
309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000324///
Chris Lattnera17991f2009-03-29 16:50:03 +0000325Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000326ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000327 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
328 // that we parse together here.
329 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 // At this point, we know that it is not a function definition. Parse the
332 // rest of the init-declarator-list.
333 while (1) {
334 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000335 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000336 SourceLocation Loc;
337 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000338 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000339 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000340 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000341 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000342
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000343 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000344 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000345 }
Chris Lattner4b009652007-07-25 00:24:17 +0000346
347 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000348 if (Tok.is(tok::kw___attribute)) {
349 SourceLocation Loc;
350 AttributeList *AttrList = ParseAttributes(&Loc);
351 D.AddAttributes(AttrList, Loc);
352 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000353
354 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000355 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
356 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000357
Chris Lattner4b009652007-07-25 00:24:17 +0000358 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000359 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000360 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000361 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
362 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000363 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000364 } else {
365 OwningExprResult Init(ParseInitializer());
366 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000367 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000370 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000371 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000372 } else if (Tok.is(tok::l_paren)) {
373 // Parse C++ direct initializer: '(' expression-list ')'
374 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000375 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000376 CommaLocsTy CommaLocs;
377
378 bool InvalidExpr = false;
379 if (ParseExpressionList(Exprs, CommaLocs)) {
380 SkipUntil(tok::r_paren);
381 InvalidExpr = true;
382 }
383 // Match the ')'.
384 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
385
386 if (!InvalidExpr) {
387 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
388 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000389 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000390 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000391 &CommaLocs[0], RParenLoc);
392 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000393 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000394 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000395 }
396
Chris Lattner4b009652007-07-25 00:24:17 +0000397 // If we don't have a comma, it is either the end of the list (a ';') or an
398 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000399 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000400 break;
401
402 // Consume the comma.
403 ConsumeToken();
404
405 // Parse the next declarator.
406 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000407
408 // Accept attributes in an init-declarator. In the first declarator in a
409 // declaration, these would be part of the declspec. In subsequent
410 // declarators, they become part of the declarator itself, so that they
411 // don't apply to declarators after *this* one. Examples:
412 // short __attribute__((common)) var; -> declspec
413 // short var __attribute__((common)); -> declarator
414 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Chris Lattner926cf542008-10-20 04:57:38 +0000420
Chris Lattner4b009652007-07-25 00:24:17 +0000421 ParseDeclarator(D);
422 }
423
Chris Lattner2c41d482009-03-29 17:18:04 +0000424 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
425 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000426}
427
428/// ParseSpecifierQualifierList
429/// specifier-qualifier-list:
430/// type-specifier specifier-qualifier-list[opt]
431/// type-qualifier specifier-qualifier-list[opt]
432/// [GNU] attributes specifier-qualifier-list[opt]
433///
434void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
435 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
436 /// parse declaration-specifiers and complain about extra stuff.
437 ParseDeclarationSpecifiers(DS);
438
439 // Validate declspec for type-name.
440 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000441 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000442 Diag(Tok, diag::err_typename_requires_specqual);
443
444 // Issue diagnostic and remove storage class if present.
445 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
446 if (DS.getStorageClassSpecLoc().isValid())
447 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
448 else
449 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
450 DS.ClearStorageClassSpecs();
451 }
452
453 // Issue diagnostic and remove function specfier if present.
454 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000455 if (DS.isInlineSpecified())
456 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
457 if (DS.isVirtualSpecified())
458 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isExplicitSpecified())
460 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000461 DS.ClearFunctionSpecs();
462 }
463}
464
465/// ParseDeclarationSpecifiers
466/// declaration-specifiers: [C99 6.7]
467/// storage-class-specifier declaration-specifiers[opt]
468/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000469/// [C99] function-specifier declaration-specifiers[opt]
470/// [GNU] attributes declaration-specifiers[opt]
471///
472/// storage-class-specifier: [C99 6.7.1]
473/// 'typedef'
474/// 'extern'
475/// 'static'
476/// 'auto'
477/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000478/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000479/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000480/// function-specifier: [C99 6.7.4]
481/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000482/// [C++] 'virtual'
483/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000484///
Douglas Gregor52473432008-12-24 02:52:09 +0000485void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000486 TemplateParameterLists *TemplateParams,
487 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000488 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 while (1) {
490 int isInvalid = false;
491 const char *PrevSpec = 0;
492 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000495 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000496 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000497 // If this is not a declaration specifier token, we're done reading decl
498 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000499 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000500 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000501
502 case tok::coloncolon: // ::foo::bar
503 // Annotate C++ scope specifiers. If we get one, loop.
504 if (TryAnnotateCXXScopeToken())
505 continue;
506 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000507
508 case tok::annot_cxxscope: {
509 if (DS.hasTypeSpecifier())
510 goto DoneWithDeclSpec;
511
512 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000513 Token Next = NextToken();
514 if (Next.is(tok::annot_template_id) &&
515 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000516 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000517 // We have a qualified template-id, e.g., N::A<int>
518 CXXScopeSpec SS;
519 ParseOptionalCXXScopeSpecifier(SS);
520 assert(Tok.is(tok::annot_template_id) &&
521 "ParseOptionalCXXScopeSpecifier not working");
522 AnnotateTemplateIdTokenAsType(&SS);
523 continue;
524 }
525
526 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000527 goto DoneWithDeclSpec;
528
529 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000530 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000531 SS.setRange(Tok.getAnnotationRange());
532
533 // If the next token is the name of the class type that the C++ scope
534 // denotes, followed by a '(', then this is a constructor declaration.
535 // We're done with the decl-specifiers.
536 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
537 CurScope, &SS) &&
538 GetLookAheadToken(2).is(tok::l_paren))
539 goto DoneWithDeclSpec;
540
Douglas Gregor1075a162009-02-04 17:00:24 +0000541 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
542 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000543
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000544 if (TypeRep == 0)
545 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000546
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000547 ConsumeToken(); // The C++ scope.
548
Douglas Gregora60c62e2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000550 TypeRep);
551 if (isInvalid)
552 break;
553
554 DS.SetRangeEnd(Tok.getLocation());
555 ConsumeToken(); // The typename.
556
557 continue;
558 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000559
560 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000561 if (Tok.getAnnotationValue())
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
563 Tok.getAnnotationValue());
564 else
565 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000566 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
567 ConsumeToken(); // The typename
568
569 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
570 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
571 // Objective-C interface. If we don't have Objective-C or a '<', this is
572 // just a normal reference to a typedef name.
573 if (!Tok.is(tok::less) || !getLang().ObjC1)
574 continue;
575
576 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000577 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000578 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
579 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
580
581 DS.SetRangeEnd(EndProtoLoc);
582 continue;
583 }
584
Chris Lattnerfda18db2008-07-26 01:18:38 +0000585 // typedef-name
586 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000587 // In C++, check to see if this is a scope specifier like foo::bar::, if
588 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000589 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
590 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000591
Chris Lattnerfda18db2008-07-26 01:18:38 +0000592 // This identifier can only be a typedef name if we haven't already seen
593 // a type-specifier. Without this check we misparse:
594 // typedef int X; struct Y { short X; }; as 'short int'.
595 if (DS.hasTypeSpecifier())
596 goto DoneWithDeclSpec;
597
598 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000599 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
600 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000601
Chris Lattnerfda18db2008-07-26 01:18:38 +0000602 if (TypeRep == 0)
603 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000604
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000605 // C++: If the identifier is actually the name of the class type
606 // being defined and the next token is a '(', then this is a
607 // constructor declaration. We're done with the decl-specifiers
608 // and will treat this token as an identifier.
609 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000610 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000611 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
612 NextToken().getKind() == tok::l_paren)
613 goto DoneWithDeclSpec;
614
Douglas Gregora60c62e2009-02-09 15:09:02 +0000615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000616 TypeRep);
617 if (isInvalid)
618 break;
619
620 DS.SetRangeEnd(Tok.getLocation());
621 ConsumeToken(); // The identifier
622
623 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
624 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
625 // Objective-C interface. If we don't have Objective-C or a '<', this is
626 // just a normal reference to a typedef name.
627 if (!Tok.is(tok::less) || !getLang().ObjC1)
628 continue;
629
630 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000631 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000632 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000633 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000634
635 DS.SetRangeEnd(EndProtoLoc);
636
Steve Narofff7683302008-09-22 10:28:57 +0000637 // Need to support trailing type qualifiers (e.g. "id<p> const").
638 // If a type specifier follows, it will be diagnosed elsewhere.
639 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000640 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000641
642 // type-name
643 case tok::annot_template_id: {
644 TemplateIdAnnotation *TemplateId
645 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000646 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000647 // This template-id does not refer to a type name, so we're
648 // done with the type-specifiers.
649 goto DoneWithDeclSpec;
650 }
651
652 // Turn the template-id annotation token into a type annotation
653 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000654 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000655 continue;
656 }
657
Chris Lattner4b009652007-07-25 00:24:17 +0000658 // GNU attributes support.
659 case tok::kw___attribute:
660 DS.AddAttributes(ParseAttributes());
661 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000662
663 // Microsoft declspec support.
664 case tok::kw___declspec:
665 if (!PP.getLangOptions().Microsoft)
666 goto DoneWithDeclSpec;
667 FuzzyParseMicrosoftDeclSpec();
668 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000669
Steve Naroffedd04d52008-12-25 14:16:32 +0000670 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000671 case tok::kw___forceinline:
672 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000673 case tok::kw___cdecl:
674 case tok::kw___stdcall:
675 case tok::kw___fastcall:
676 if (!PP.getLangOptions().Microsoft)
677 goto DoneWithDeclSpec;
678 // Just ignore it.
679 break;
680
Chris Lattner4b009652007-07-25 00:24:17 +0000681 // storage-class-specifier
682 case tok::kw_typedef:
683 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
684 break;
685 case tok::kw_extern:
686 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000687 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
689 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000690 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
692 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000693 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000694 case tok::kw_static:
695 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000696 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000697 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
698 break;
699 case tok::kw_auto:
700 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
701 break;
702 case tok::kw_register:
703 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
704 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000705 case tok::kw_mutable:
706 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
707 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000708 case tok::kw___thread:
709 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
710 break;
711
Chris Lattner4b009652007-07-25 00:24:17 +0000712 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000713
Chris Lattner4b009652007-07-25 00:24:17 +0000714 // function-specifier
715 case tok::kw_inline:
716 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
717 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000718 case tok::kw_virtual:
719 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
720 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000721 case tok::kw_explicit:
722 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
723 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000724
725 // type-specifier
726 case tok::kw_short:
727 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
728 break;
729 case tok::kw_long:
730 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
731 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
732 else
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
734 break;
735 case tok::kw_signed:
736 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
737 break;
738 case tok::kw_unsigned:
739 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
740 break;
741 case tok::kw__Complex:
742 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
743 break;
744 case tok::kw__Imaginary:
745 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
746 break;
747 case tok::kw_void:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
749 break;
750 case tok::kw_char:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
752 break;
753 case tok::kw_int:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
755 break;
756 case tok::kw_float:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
758 break;
759 case tok::kw_double:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
761 break;
762 case tok::kw_wchar_t:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
764 break;
765 case tok::kw_bool:
766 case tok::kw__Bool:
767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
768 break;
769 case tok::kw__Decimal32:
770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
771 break;
772 case tok::kw__Decimal64:
773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
774 break;
775 case tok::kw__Decimal128:
776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
777 break;
778
779 // class-specifier:
780 case tok::kw_class:
781 case tok::kw_struct:
782 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000783 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000784 continue;
785
786 // enum-specifier:
787 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000788 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000789 continue;
790
791 // cv-qualifier:
792 case tok::kw_const:
793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
794 break;
795 case tok::kw_volatile:
796 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
797 getLang())*2;
798 break;
799 case tok::kw_restrict:
800 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
801 getLang())*2;
802 break;
803
Douglas Gregord3022602009-03-27 23:10:48 +0000804 // C++ typename-specifier:
805 case tok::kw_typename:
806 if (TryAnnotateTypeOrScopeToken())
807 continue;
808 break;
809
Chris Lattnerc297b722009-01-21 19:48:37 +0000810 // GNU typeof support.
811 case tok::kw_typeof:
812 ParseTypeofSpecifier(DS);
813 continue;
814
Steve Naroff5f0466b2008-06-05 00:02:44 +0000815 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000816 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000817 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
818 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000819 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000820 goto DoneWithDeclSpec;
821
822 {
823 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000824 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000825 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000826 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000827 DS.SetRangeEnd(EndProtoLoc);
828
Chris Lattnerf006a222008-11-18 07:48:38 +0000829 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
830 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000831 // Need to support trailing type qualifiers (e.g. "id<p> const").
832 // If a type specifier follows, it will be diagnosed elsewhere.
833 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000834 }
Chris Lattner4b009652007-07-25 00:24:17 +0000835 }
836 // If the specifier combination wasn't legal, issue a diagnostic.
837 if (isInvalid) {
838 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000839 // Pick between error or extwarn.
840 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
841 : diag::ext_duplicate_declspec;
842 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000843 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000844 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000845 ConsumeToken();
846 }
847}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000848
Chris Lattnerd706dc82009-01-06 06:59:53 +0000849/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000850/// primarily follow the C++ grammar with additions for C99 and GNU,
851/// which together subsume the C grammar. Note that the C++
852/// type-specifier also includes the C type-qualifier (for const,
853/// volatile, and C99 restrict). Returns true if a type-specifier was
854/// found (and parsed), false otherwise.
855///
856/// type-specifier: [C++ 7.1.5]
857/// simple-type-specifier
858/// class-specifier
859/// enum-specifier
860/// elaborated-type-specifier [TODO]
861/// cv-qualifier
862///
863/// cv-qualifier: [C++ 7.1.5.1]
864/// 'const'
865/// 'volatile'
866/// [C99] 'restrict'
867///
868/// simple-type-specifier: [ C++ 7.1.5.2]
869/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
870/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
871/// 'char'
872/// 'wchar_t'
873/// 'bool'
874/// 'short'
875/// 'int'
876/// 'long'
877/// 'signed'
878/// 'unsigned'
879/// 'float'
880/// 'double'
881/// 'void'
882/// [C99] '_Bool'
883/// [C99] '_Complex'
884/// [C99] '_Imaginary' // Removed in TC2?
885/// [GNU] '_Decimal32'
886/// [GNU] '_Decimal64'
887/// [GNU] '_Decimal128'
888/// [GNU] typeof-specifier
889/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
890/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000891bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
892 const char *&PrevSpec,
893 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000894 SourceLocation Loc = Tok.getLocation();
895
896 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000897 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000898 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000899 // Annotate typenames and C++ scope specifiers. If we get one, just
900 // recurse to handle whatever we get.
901 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000902 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000903 // Otherwise, not a type specifier.
904 return false;
905 case tok::coloncolon: // ::foo::bar
906 if (NextToken().is(tok::kw_new) || // ::new
907 NextToken().is(tok::kw_delete)) // ::delete
908 return false;
909
910 // Annotate typenames and C++ scope specifiers. If we get one, just
911 // recurse to handle whatever we get.
912 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000913 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000914 // Otherwise, not a type specifier.
915 return false;
916
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000917 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000918 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000919 if (Tok.getAnnotationValue())
920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
921 Tok.getAnnotationValue());
922 else
923 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000924 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
925 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000926
927 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
928 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
929 // Objective-C interface. If we don't have Objective-C or a '<', this is
930 // just a normal reference to a typedef name.
931 if (!Tok.is(tok::less) || !getLang().ObjC1)
932 return true;
933
934 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000935 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000936 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
937 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
938
939 DS.SetRangeEnd(EndProtoLoc);
940 return true;
941 }
942
943 case tok::kw_short:
944 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
945 break;
946 case tok::kw_long:
947 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
948 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
949 else
950 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
951 break;
952 case tok::kw_signed:
953 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
954 break;
955 case tok::kw_unsigned:
956 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
957 break;
958 case tok::kw__Complex:
959 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
960 break;
961 case tok::kw__Imaginary:
962 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
963 break;
964 case tok::kw_void:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
966 break;
967 case tok::kw_char:
968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
969 break;
970 case tok::kw_int:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
972 break;
973 case tok::kw_float:
974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
975 break;
976 case tok::kw_double:
977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
978 break;
979 case tok::kw_wchar_t:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
981 break;
982 case tok::kw_bool:
983 case tok::kw__Bool:
984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
985 break;
986 case tok::kw__Decimal32:
987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
988 break;
989 case tok::kw__Decimal64:
990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
991 break;
992 case tok::kw__Decimal128:
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
994 break;
995
996 // class-specifier:
997 case tok::kw_class:
998 case tok::kw_struct:
999 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +00001000 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001001 return true;
1002
1003 // enum-specifier:
1004 case tok::kw_enum:
1005 ParseEnumSpecifier(DS);
1006 return true;
1007
1008 // cv-qualifier:
1009 case tok::kw_const:
1010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1011 getLang())*2;
1012 break;
1013 case tok::kw_volatile:
1014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1015 getLang())*2;
1016 break;
1017 case tok::kw_restrict:
1018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1019 getLang())*2;
1020 break;
1021
1022 // GNU typeof support.
1023 case tok::kw_typeof:
1024 ParseTypeofSpecifier(DS);
1025 return true;
1026
Steve Naroffedd04d52008-12-25 14:16:32 +00001027 case tok::kw___cdecl:
1028 case tok::kw___stdcall:
1029 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001030 if (!PP.getLangOptions().Microsoft) return false;
1031 ConsumeToken();
1032 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001033
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001034 default:
1035 // Not a type-specifier; do nothing.
1036 return false;
1037 }
1038
1039 // If the specifier combination wasn't legal, issue a diagnostic.
1040 if (isInvalid) {
1041 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001042 // Pick between error or extwarn.
1043 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1044 : diag::ext_duplicate_declspec;
1045 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001046 }
1047 DS.SetRangeEnd(Tok.getLocation());
1048 ConsumeToken(); // whatever we parsed above.
1049 return true;
1050}
Chris Lattner4b009652007-07-25 00:24:17 +00001051
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001052/// ParseStructDeclaration - Parse a struct declaration without the terminating
1053/// semicolon.
1054///
Chris Lattner4b009652007-07-25 00:24:17 +00001055/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001056/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001057/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001058/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001059/// struct-declarator-list:
1060/// struct-declarator
1061/// struct-declarator-list ',' struct-declarator
1062/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1063/// struct-declarator:
1064/// declarator
1065/// [GNU] declarator attributes[opt]
1066/// declarator[opt] ':' constant-expression
1067/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1068///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001069void Parser::
1070ParseStructDeclaration(DeclSpec &DS,
1071 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001072 if (Tok.is(tok::kw___extension__)) {
1073 // __extension__ silences extension warnings in the subexpression.
1074 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001075 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001076 return ParseStructDeclaration(DS, Fields);
1077 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001078
1079 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001080 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001082
Douglas Gregorb748fc52009-01-12 22:49:06 +00001083 // If there are no declarators, this is a free-standing declaration
1084 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001085 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001086 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001087 return;
1088 }
1089
1090 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001091 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001092 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001093 FieldDeclarator &DeclaratorInfo = Fields.back();
1094
Steve Naroffa9adf112007-08-20 22:28:22 +00001095 /// struct-declarator: declarator
1096 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001097 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001098 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001099
Chris Lattner34a01ad2007-10-09 17:33:22 +00001100 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001102 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001103 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001104 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001105 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001106 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001107 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001108
Steve Naroffa9adf112007-08-20 22:28:22 +00001109 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001110 if (Tok.is(tok::kw___attribute)) {
1111 SourceLocation Loc;
1112 AttributeList *AttrList = ParseAttributes(&Loc);
1113 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1114 }
1115
Steve Naroffa9adf112007-08-20 22:28:22 +00001116 // If we don't have a comma, it is either the end of the list (a ';')
1117 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001119 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001120
Steve Naroffa9adf112007-08-20 22:28:22 +00001121 // Consume the comma.
1122 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001123
Steve Naroffa9adf112007-08-20 22:28:22 +00001124 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001125 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001126
Steve Naroffa9adf112007-08-20 22:28:22 +00001127 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001128 if (Tok.is(tok::kw___attribute)) {
1129 SourceLocation Loc;
1130 AttributeList *AttrList = ParseAttributes(&Loc);
1131 Fields.back().D.AddAttributes(AttrList, Loc);
1132 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001133 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001134}
1135
1136/// ParseStructUnionBody
1137/// struct-contents:
1138/// struct-declaration-list
1139/// [EXT] empty
1140/// [GNU] "struct-declaration-list" without terminatoring ';'
1141/// struct-declaration-list:
1142/// struct-declaration
1143/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001144/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001145///
Chris Lattner4b009652007-07-25 00:24:17 +00001146void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001147 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001148 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1149 PP.getSourceManager(),
1150 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001151
Chris Lattner4b009652007-07-25 00:24:17 +00001152 SourceLocation LBraceLoc = ConsumeBrace();
1153
Douglas Gregorcab994d2009-01-09 22:42:13 +00001154 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001155 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1156
Chris Lattner4b009652007-07-25 00:24:17 +00001157 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1158 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001159 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001160 Diag(Tok, diag::ext_empty_struct_union_enum)
1161 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001162
Chris Lattner5261d0c2009-03-28 19:18:32 +00001163 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001164 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1165
Chris Lattner4b009652007-07-25 00:24:17 +00001166 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 // Each iteration of this loop reads one struct-declaration.
1169
1170 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001171 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001172 Diag(Tok, diag::ext_extra_struct_semi);
1173 ConsumeToken();
1174 continue;
1175 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001176
1177 // Parse all the comma separated declarators.
1178 DeclSpec DS;
1179 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001180 if (!Tok.is(tok::at)) {
1181 ParseStructDeclaration(DS, FieldDeclarators);
1182
1183 // Convert them all to fields.
1184 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1185 FieldDeclarator &FD = FieldDeclarators[i];
1186 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001187 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1188 DS.getSourceRange().getBegin(),
1189 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001190 FieldDecls.push_back(Field);
1191 }
1192 } else { // Handle @defs
1193 ConsumeToken();
1194 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1195 Diag(Tok, diag::err_unexpected_at);
1196 SkipUntil(tok::semi, true, true);
1197 continue;
1198 }
1199 ConsumeToken();
1200 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1201 if (!Tok.is(tok::identifier)) {
1202 Diag(Tok, diag::err_expected_ident);
1203 SkipUntil(tok::semi, true, true);
1204 continue;
1205 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001206 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001207 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1208 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001209 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1210 ConsumeToken();
1211 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1212 }
Chris Lattner4b009652007-07-25 00:24:17 +00001213
Chris Lattner34a01ad2007-10-09 17:33:22 +00001214 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001215 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001216 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001217 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001218 break;
1219 } else {
1220 Diag(Tok, diag::err_expected_semi_decl_list);
1221 // Skip to end of block or statement
1222 SkipUntil(tok::r_brace, true, true);
1223 }
1224 }
1225
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001226 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001227
Chris Lattner4b009652007-07-25 00:24:17 +00001228 AttributeList *AttrList = 0;
1229 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001230 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001231 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001232
1233 Actions.ActOnFields(CurScope,
1234 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1235 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001236 AttrList);
1237 StructScope.Exit();
1238 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001239}
1240
1241
1242/// ParseEnumSpecifier
1243/// enum-specifier: [C99 6.7.2.2]
1244/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001245///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001246/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1247/// '}' attributes[opt]
1248/// 'enum' identifier
1249/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001250///
1251/// [C++] elaborated-type-specifier:
1252/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1253///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001254void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001255 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001256 SourceLocation StartLoc = ConsumeToken();
1257
1258 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001259
1260 AttributeList *Attr = 0;
1261 // If attributes exist after tag, parse them.
1262 if (Tok.is(tok::kw___attribute))
1263 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001264
1265 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001266 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001267 if (Tok.isNot(tok::identifier)) {
1268 Diag(Tok, diag::err_expected_ident);
1269 if (Tok.isNot(tok::l_brace)) {
1270 // Has no name and is not a definition.
1271 // Skip the rest of this declarator, up until the comma or semicolon.
1272 SkipUntil(tok::comma, true);
1273 return;
1274 }
1275 }
1276 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001277
1278 // Must have either 'enum name' or 'enum {...}'.
1279 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1280 Diag(Tok, diag::err_expected_ident_lbrace);
1281
1282 // Skip the rest of this declarator, up until the comma or semicolon.
1283 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001284 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001285 }
1286
1287 // If an identifier is present, consume and remember it.
1288 IdentifierInfo *Name = 0;
1289 SourceLocation NameLoc;
1290 if (Tok.is(tok::identifier)) {
1291 Name = Tok.getIdentifierInfo();
1292 NameLoc = ConsumeToken();
1293 }
1294
1295 // There are three options here. If we have 'enum foo;', then this is a
1296 // forward declaration. If we have 'enum foo {...' then this is a
1297 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1298 //
1299 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1300 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1301 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1302 //
1303 Action::TagKind TK;
1304 if (Tok.is(tok::l_brace))
1305 TK = Action::TK_Definition;
1306 else if (Tok.is(tok::semi))
1307 TK = Action::TK_Declaration;
1308 else
1309 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001310 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1311 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001312
Chris Lattner34a01ad2007-10-09 17:33:22 +00001313 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001314 ParseEnumBody(StartLoc, TagDecl);
1315
1316 // TODO: semantic analysis on the declspec for enums.
1317 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001318 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1319 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001320 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001321}
1322
1323/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1324/// enumerator-list:
1325/// enumerator
1326/// enumerator-list ',' enumerator
1327/// enumerator:
1328/// enumeration-constant
1329/// enumeration-constant '=' constant-expression
1330/// enumeration-constant:
1331/// identifier
1332///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001333void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001334 // Enter the scope of the enum body and start the definition.
1335 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001336 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001337
Chris Lattner4b009652007-07-25 00:24:17 +00001338 SourceLocation LBraceLoc = ConsumeBrace();
1339
Chris Lattnerc9a92452007-08-27 17:24:30 +00001340 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001341 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001342 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001343
Chris Lattner5261d0c2009-03-28 19:18:32 +00001344 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001345
Chris Lattner5261d0c2009-03-28 19:18:32 +00001346 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001347
1348 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001349 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001350 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1351 SourceLocation IdentLoc = ConsumeToken();
1352
1353 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001354 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001355 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001356 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001357 AssignedVal = ParseConstantExpression();
1358 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001359 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001360 }
1361
1362 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001363 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1364 LastEnumConstDecl,
1365 IdentLoc, Ident,
1366 EqualLoc,
1367 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001368 EnumConstantDecls.push_back(EnumConstDecl);
1369 LastEnumConstDecl = EnumConstDecl;
1370
Chris Lattner34a01ad2007-10-09 17:33:22 +00001371 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001372 break;
1373 SourceLocation CommaLoc = ConsumeToken();
1374
Chris Lattner34a01ad2007-10-09 17:33:22 +00001375 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001376 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1377 }
1378
1379 // Eat the }.
1380 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1381
Steve Naroff0acc9c92007-09-15 18:49:24 +00001382 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001383 EnumConstantDecls.size());
1384
Chris Lattner5261d0c2009-03-28 19:18:32 +00001385 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001386 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001387 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001388 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001389
1390 EnumScope.Exit();
1391 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001392}
1393
1394/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001395/// start of a type-qualifier-list.
1396bool Parser::isTypeQualifier() const {
1397 switch (Tok.getKind()) {
1398 default: return false;
1399 // type-qualifier
1400 case tok::kw_const:
1401 case tok::kw_volatile:
1402 case tok::kw_restrict:
1403 return true;
1404 }
1405}
1406
1407/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001408/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001409bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001410 switch (Tok.getKind()) {
1411 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001412
1413 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001414 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001415 // Annotate typenames and C++ scope specifiers. If we get one, just
1416 // recurse to handle whatever we get.
1417 if (TryAnnotateTypeOrScopeToken())
1418 return isTypeSpecifierQualifier();
1419 // Otherwise, not a type specifier.
1420 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001421
Chris Lattnerb75fde62009-01-04 23:41:41 +00001422 case tok::coloncolon: // ::foo::bar
1423 if (NextToken().is(tok::kw_new) || // ::new
1424 NextToken().is(tok::kw_delete)) // ::delete
1425 return false;
1426
1427 // Annotate typenames and C++ scope specifiers. If we get one, just
1428 // recurse to handle whatever we get.
1429 if (TryAnnotateTypeOrScopeToken())
1430 return isTypeSpecifierQualifier();
1431 // Otherwise, not a type specifier.
1432 return false;
1433
Chris Lattner4b009652007-07-25 00:24:17 +00001434 // GNU attributes support.
1435 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001436 // GNU typeof support.
1437 case tok::kw_typeof:
1438
Chris Lattner4b009652007-07-25 00:24:17 +00001439 // type-specifiers
1440 case tok::kw_short:
1441 case tok::kw_long:
1442 case tok::kw_signed:
1443 case tok::kw_unsigned:
1444 case tok::kw__Complex:
1445 case tok::kw__Imaginary:
1446 case tok::kw_void:
1447 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001448 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001449 case tok::kw_int:
1450 case tok::kw_float:
1451 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001452 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001453 case tok::kw__Bool:
1454 case tok::kw__Decimal32:
1455 case tok::kw__Decimal64:
1456 case tok::kw__Decimal128:
1457
Chris Lattner2e78db32008-04-13 18:59:07 +00001458 // struct-or-union-specifier (C99) or class-specifier (C++)
1459 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001460 case tok::kw_struct:
1461 case tok::kw_union:
1462 // enum-specifier
1463 case tok::kw_enum:
1464
1465 // type-qualifier
1466 case tok::kw_const:
1467 case tok::kw_volatile:
1468 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001469
1470 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001471 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001472 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001473
1474 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1475 case tok::less:
1476 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001477
1478 case tok::kw___cdecl:
1479 case tok::kw___stdcall:
1480 case tok::kw___fastcall:
1481 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001482 }
1483}
1484
1485/// isDeclarationSpecifier() - Return true if the current token is part of a
1486/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001487bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001488 switch (Tok.getKind()) {
1489 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001490
1491 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001492 // Unfortunate hack to support "Class.factoryMethod" notation.
1493 if (getLang().ObjC1 && NextToken().is(tok::period))
1494 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001495 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001496
Douglas Gregord3022602009-03-27 23:10:48 +00001497 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001498 // Annotate typenames and C++ scope specifiers. If we get one, just
1499 // recurse to handle whatever we get.
1500 if (TryAnnotateTypeOrScopeToken())
1501 return isDeclarationSpecifier();
1502 // Otherwise, not a declaration specifier.
1503 return false;
1504 case tok::coloncolon: // ::foo::bar
1505 if (NextToken().is(tok::kw_new) || // ::new
1506 NextToken().is(tok::kw_delete)) // ::delete
1507 return false;
1508
1509 // Annotate typenames and C++ scope specifiers. If we get one, just
1510 // recurse to handle whatever we get.
1511 if (TryAnnotateTypeOrScopeToken())
1512 return isDeclarationSpecifier();
1513 // Otherwise, not a declaration specifier.
1514 return false;
1515
Chris Lattner4b009652007-07-25 00:24:17 +00001516 // storage-class-specifier
1517 case tok::kw_typedef:
1518 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001519 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001520 case tok::kw_static:
1521 case tok::kw_auto:
1522 case tok::kw_register:
1523 case tok::kw___thread:
1524
1525 // type-specifiers
1526 case tok::kw_short:
1527 case tok::kw_long:
1528 case tok::kw_signed:
1529 case tok::kw_unsigned:
1530 case tok::kw__Complex:
1531 case tok::kw__Imaginary:
1532 case tok::kw_void:
1533 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001534 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001535 case tok::kw_int:
1536 case tok::kw_float:
1537 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001538 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001539 case tok::kw__Bool:
1540 case tok::kw__Decimal32:
1541 case tok::kw__Decimal64:
1542 case tok::kw__Decimal128:
1543
Chris Lattner2e78db32008-04-13 18:59:07 +00001544 // struct-or-union-specifier (C99) or class-specifier (C++)
1545 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001546 case tok::kw_struct:
1547 case tok::kw_union:
1548 // enum-specifier
1549 case tok::kw_enum:
1550
1551 // type-qualifier
1552 case tok::kw_const:
1553 case tok::kw_volatile:
1554 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001555
Chris Lattner4b009652007-07-25 00:24:17 +00001556 // function-specifier
1557 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001558 case tok::kw_virtual:
1559 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001560
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001561 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001562 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001563
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001564 // GNU typeof support.
1565 case tok::kw_typeof:
1566
1567 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001568 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001569 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001570
1571 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1572 case tok::less:
1573 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001574
Steve Naroffab1a3632009-01-06 19:34:12 +00001575 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001576 case tok::kw___cdecl:
1577 case tok::kw___stdcall:
1578 case tok::kw___fastcall:
1579 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001580 }
1581}
1582
1583
1584/// ParseTypeQualifierListOpt
1585/// type-qualifier-list: [C99 6.7.5]
1586/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001587/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001588/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001589/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001590///
Chris Lattner460696f2008-12-18 07:02:59 +00001591void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001592 while (1) {
1593 int isInvalid = false;
1594 const char *PrevSpec = 0;
1595 SourceLocation Loc = Tok.getLocation();
1596
1597 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001598 case tok::kw_const:
1599 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1600 getLang())*2;
1601 break;
1602 case tok::kw_volatile:
1603 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1604 getLang())*2;
1605 break;
1606 case tok::kw_restrict:
1607 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1608 getLang())*2;
1609 break;
Steve Naroffad620402008-12-25 14:41:26 +00001610 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001611 case tok::kw___cdecl:
1612 case tok::kw___stdcall:
1613 case tok::kw___fastcall:
1614 if (!PP.getLangOptions().Microsoft)
1615 goto DoneWithTypeQuals;
1616 // Just ignore it.
1617 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001618 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001619 if (AttributesAllowed) {
1620 DS.AddAttributes(ParseAttributes());
1621 continue; // do *not* consume the next token!
1622 }
1623 // otherwise, FALL THROUGH!
1624 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001625 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001626 // If this is not a type-qualifier token, we're done reading type
1627 // qualifiers. First verify that DeclSpec's are consistent.
1628 DS.Finish(Diags, PP.getSourceManager(), getLang());
1629 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001630 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001631
Chris Lattner4b009652007-07-25 00:24:17 +00001632 // If the specifier combination wasn't legal, issue a diagnostic.
1633 if (isInvalid) {
1634 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001635 // Pick between error or extwarn.
1636 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1637 : diag::ext_duplicate_declspec;
1638 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001639 }
1640 ConsumeToken();
1641 }
1642}
1643
1644
1645/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1646///
1647void Parser::ParseDeclarator(Declarator &D) {
1648 /// This implements the 'declarator' production in the C grammar, then checks
1649 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001650 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001651}
1652
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001653/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1654/// is parsed by the function passed to it. Pass null, and the direct-declarator
1655/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001656/// ptr-operator production.
1657///
Sebastian Redl75555032009-01-24 21:16:55 +00001658/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1659/// [C] pointer[opt] direct-declarator
1660/// [C++] direct-declarator
1661/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001662///
1663/// pointer: [C99 6.7.5]
1664/// '*' type-qualifier-list[opt]
1665/// '*' type-qualifier-list[opt] pointer
1666///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001667/// ptr-operator:
1668/// '*' cv-qualifier-seq[opt]
1669/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001670/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001671/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001672/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001673/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001674void Parser::ParseDeclaratorInternal(Declarator &D,
1675 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001676
Sebastian Redl75555032009-01-24 21:16:55 +00001677 // C++ member pointers start with a '::' or a nested-name.
1678 // Member pointers get special handling, since there's no place for the
1679 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001680 if (getLang().CPlusPlus &&
1681 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1682 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001683 CXXScopeSpec SS;
1684 if (ParseOptionalCXXScopeSpecifier(SS)) {
1685 if(Tok.isNot(tok::star)) {
1686 // The scope spec really belongs to the direct-declarator.
1687 D.getCXXScopeSpec() = SS;
1688 if (DirectDeclParser)
1689 (this->*DirectDeclParser)(D);
1690 return;
1691 }
1692
1693 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001694 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001695 DeclSpec DS;
1696 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001697 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001698
1699 // Recurse to parse whatever is left.
1700 ParseDeclaratorInternal(D, DirectDeclParser);
1701
1702 // Sema will have to catch (syntactically invalid) pointers into global
1703 // scope. It has to catch pointers into namespace scope anyway.
1704 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001705 Loc, DS.TakeAttributes()),
1706 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001707 return;
1708 }
1709 }
1710
1711 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001712 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001713 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001714 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001715 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001716 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001717 if (DirectDeclParser)
1718 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001719 return;
1720 }
Sebastian Redl75555032009-01-24 21:16:55 +00001721
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001722 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1723 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001724 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001725 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001726
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001727 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001728 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001729 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001730
Chris Lattner4b009652007-07-25 00:24:17 +00001731 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001732 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001733
Chris Lattner4b009652007-07-25 00:24:17 +00001734 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001735 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001736 if (Kind == tok::star)
1737 // Remember that we parsed a pointer type, and remember the type-quals.
1738 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001739 DS.TakeAttributes()),
1740 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001741 else
1742 // Remember that we parsed a Block type, and remember the type-quals.
1743 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001744 Loc),
1745 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001746 } else {
1747 // Is a reference
1748 DeclSpec DS;
1749
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001750 // Complain about rvalue references in C++03, but then go on and build
1751 // the declarator.
1752 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1753 Diag(Loc, diag::err_rvalue_reference);
1754
Chris Lattner4b009652007-07-25 00:24:17 +00001755 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1756 // cv-qualifiers are introduced through the use of a typedef or of a
1757 // template type argument, in which case the cv-qualifiers are ignored.
1758 //
1759 // [GNU] Retricted references are allowed.
1760 // [GNU] Attributes on references are allowed.
1761 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001762 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001763
1764 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1765 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1766 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001767 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001768 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1769 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001770 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001771 }
1772
1773 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001774 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001775
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001776 if (D.getNumTypeObjects() > 0) {
1777 // C++ [dcl.ref]p4: There shall be no references to references.
1778 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1779 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001780 if (const IdentifierInfo *II = D.getIdentifier())
1781 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1782 << II;
1783 else
1784 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1785 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001786
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001787 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001788 // can go ahead and build the (technically ill-formed)
1789 // declarator: reference collapsing will take care of it.
1790 }
1791 }
1792
Chris Lattner4b009652007-07-25 00:24:17 +00001793 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001794 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001795 DS.TakeAttributes(),
1796 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001797 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001798 }
1799}
1800
1801/// ParseDirectDeclarator
1802/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001803/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001804/// '(' declarator ')'
1805/// [GNU] '(' attributes declarator ')'
1806/// [C90] direct-declarator '[' constant-expression[opt] ']'
1807/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1808/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1809/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1810/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1811/// direct-declarator '(' parameter-type-list ')'
1812/// direct-declarator '(' identifier-list[opt] ')'
1813/// [GNU] direct-declarator '(' parameter-forward-declarations
1814/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001815/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1816/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001817/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001818///
1819/// declarator-id: [C++ 8]
1820/// id-expression
1821/// '::'[opt] nested-name-specifier[opt] type-name
1822///
1823/// id-expression: [C++ 5.1]
1824/// unqualified-id
1825/// qualified-id [TODO]
1826///
1827/// unqualified-id: [C++ 5.1]
1828/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001829/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001830/// conversion-function-id [TODO]
1831/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001832/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001833///
Chris Lattner4b009652007-07-25 00:24:17 +00001834void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001835 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001836
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001837 if (getLang().CPlusPlus) {
1838 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001839 // ParseDeclaratorInternal might already have parsed the scope.
1840 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1841 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001842 if (afterCXXScope) {
1843 // Change the declaration context for name lookup, until this function
1844 // is exited (and the declarator has been parsed).
1845 DeclScopeObj.EnterDeclaratorScope();
1846 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001847
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001848 if (Tok.is(tok::identifier)) {
1849 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001850
Douglas Gregor2fa10442008-12-18 19:37:40 +00001851 // If this identifier is the name of the current class, it's a
1852 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001853 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001854 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001855 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001856 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001857 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001858 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001859 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1860 ConsumeToken();
1861 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001862 } else if (Tok.is(tok::annot_template_id)) {
1863 TemplateIdAnnotation *TemplateId
1864 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1865
1866 // FIXME: Could this template-id name a constructor?
1867
1868 // FIXME: This is an egregious hack, where we silently ignore
1869 // the specialization (which should be a function template
1870 // specialization name) and use the name instead. This hack
1871 // will go away when we have support for function
1872 // specializations.
1873 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1874 TemplateId->Destroy();
1875 ConsumeToken();
1876 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001877 } else if (Tok.is(tok::kw_operator)) {
1878 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001879 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001880
Douglas Gregor853dd392008-12-26 15:00:45 +00001881 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001882 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1883 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001884 } else {
1885 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001886 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1887 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1888 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001889 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001890 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001891 }
1892 goto PastIdentifier;
1893 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001894 // This should be a C++ destructor.
1895 SourceLocation TildeLoc = ConsumeToken();
1896 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001897 // FIXME: Inaccurate.
1898 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001899 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00001900 TypeResult Type = ParseClassName(EndLoc);
1901 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001902 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00001903 else
1904 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001905 } else {
1906 Diag(Tok, diag::err_expected_class_name);
1907 D.SetIdentifier(0, TildeLoc);
1908 }
1909 goto PastIdentifier;
1910 }
1911
1912 // If we reached this point, token is not identifier and not '~'.
1913
1914 if (afterCXXScope) {
1915 Diag(Tok, diag::err_expected_unqualified_id);
1916 D.SetIdentifier(0, Tok.getLocation());
1917 D.setInvalidType(true);
1918 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001919 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001920 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001921 }
1922
1923 // If we reached this point, we are either in C/ObjC or the token didn't
1924 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001925 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1926 assert(!getLang().CPlusPlus &&
1927 "There's a C++-specific check for tok::identifier above");
1928 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1929 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1930 ConsumeToken();
1931 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001932 // direct-declarator: '(' declarator ')'
1933 // direct-declarator: '(' attributes declarator ')'
1934 // Example: 'char (*X)' or 'int (*XX)(void)'
1935 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001936 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001937 // This could be something simple like "int" (in which case the declarator
1938 // portion is empty), if an abstract-declarator is allowed.
1939 D.SetIdentifier(0, Tok.getLocation());
1940 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001941 if (D.getContext() == Declarator::MemberContext)
1942 Diag(Tok, diag::err_expected_member_name_or_semi)
1943 << D.getDeclSpec().getSourceRange();
1944 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001945 Diag(Tok, diag::err_expected_unqualified_id);
1946 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001947 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001948 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001949 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001950 }
1951
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001952 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001953 assert(D.isPastIdentifier() &&
1954 "Haven't past the location of the identifier yet?");
1955
1956 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001957 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001958 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1959 // In such a case, check if we actually have a function declarator; if it
1960 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001961 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1962 // When not in file scope, warn for ambiguous function declarators, just
1963 // in case the author intended it as a variable definition.
1964 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1965 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1966 break;
1967 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001968 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001969 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001970 ParseBracketDeclarator(D);
1971 } else {
1972 break;
1973 }
1974 }
1975}
1976
Chris Lattnera0d056d2008-04-06 05:45:57 +00001977/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1978/// only called before the identifier, so these are most likely just grouping
1979/// parens for precedence. If we find that these are actually function
1980/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1981///
1982/// direct-declarator:
1983/// '(' declarator ')'
1984/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001985/// direct-declarator '(' parameter-type-list ')'
1986/// direct-declarator '(' identifier-list[opt] ')'
1987/// [GNU] direct-declarator '(' parameter-forward-declarations
1988/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001989///
1990void Parser::ParseParenDeclarator(Declarator &D) {
1991 SourceLocation StartLoc = ConsumeParen();
1992 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1993
Chris Lattner1f185292008-10-20 02:05:46 +00001994 // Eat any attributes before we look at whether this is a grouping or function
1995 // declarator paren. If this is a grouping paren, the attribute applies to
1996 // the type being built up, for example:
1997 // int (__attribute__(()) *x)(long y)
1998 // If this ends up not being a grouping paren, the attribute applies to the
1999 // first argument, for example:
2000 // int (__attribute__(()) int x)
2001 // In either case, we need to eat any attributes to be able to determine what
2002 // sort of paren this is.
2003 //
2004 AttributeList *AttrList = 0;
2005 bool RequiresArg = false;
2006 if (Tok.is(tok::kw___attribute)) {
2007 AttrList = ParseAttributes();
2008
2009 // We require that the argument list (if this is a non-grouping paren) be
2010 // present even if the attribute list was empty.
2011 RequiresArg = true;
2012 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002013 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002014 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2015 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002016 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002017
Chris Lattnera0d056d2008-04-06 05:45:57 +00002018 // If we haven't past the identifier yet (or where the identifier would be
2019 // stored, if this is an abstract declarator), then this is probably just
2020 // grouping parens. However, if this could be an abstract-declarator, then
2021 // this could also be the start of function arguments (consider 'void()').
2022 bool isGrouping;
2023
2024 if (!D.mayOmitIdentifier()) {
2025 // If this can't be an abstract-declarator, this *must* be a grouping
2026 // paren, because we haven't seen the identifier yet.
2027 isGrouping = true;
2028 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002029 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002030 isDeclarationSpecifier()) { // 'int(int)' is a function.
2031 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2032 // considered to be a type, not a K&R identifier-list.
2033 isGrouping = false;
2034 } else {
2035 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2036 isGrouping = true;
2037 }
2038
2039 // If this is a grouping paren, handle:
2040 // direct-declarator: '(' declarator ')'
2041 // direct-declarator: '(' attributes declarator ')'
2042 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002043 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002044 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002045 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002046 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002047
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002048 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002049 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002050 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002051
2052 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002053 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002054 return;
2055 }
2056
2057 // Okay, if this wasn't a grouping paren, it must be the start of a function
2058 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002059 // identifier (and remember where it would have been), then call into
2060 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002061 D.SetIdentifier(0, Tok.getLocation());
2062
Chris Lattner1f185292008-10-20 02:05:46 +00002063 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002064}
2065
2066/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2067/// declarator D up to a paren, which indicates that we are parsing function
2068/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002069///
Chris Lattner1f185292008-10-20 02:05:46 +00002070/// If AttrList is non-null, then the caller parsed those arguments immediately
2071/// after the open paren - they should be considered to be the first argument of
2072/// a parameter. If RequiresArg is true, then the first argument of the
2073/// function is required to be present and required to not be an identifier
2074/// list.
2075///
Chris Lattner4b009652007-07-25 00:24:17 +00002076/// This method also handles this portion of the grammar:
2077/// parameter-type-list: [C99 6.7.5]
2078/// parameter-list
2079/// parameter-list ',' '...'
2080///
2081/// parameter-list: [C99 6.7.5]
2082/// parameter-declaration
2083/// parameter-list ',' parameter-declaration
2084///
2085/// parameter-declaration: [C99 6.7.5]
2086/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002087/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002088/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002089/// declaration-specifiers abstract-declarator[opt]
2090/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002091/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002092/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2093///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002094/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002095/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002096///
Chris Lattner1f185292008-10-20 02:05:46 +00002097void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2098 AttributeList *AttrList,
2099 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002100 // lparen is already consumed!
2101 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002102
Chris Lattner1f185292008-10-20 02:05:46 +00002103 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002104 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002105 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002106 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002107 delete AttrList;
2108 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002109
Sebastian Redl0c986032009-02-09 18:23:29 +00002110 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002111
2112 // cv-qualifier-seq[opt].
2113 DeclSpec DS;
2114 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002115 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002116 if (!DS.getSourceRange().getEnd().isInvalid())
2117 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002118
2119 // Parse exception-specification[opt].
2120 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002121 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002122 }
2123
Chris Lattner9f7564b2008-04-06 06:57:35 +00002124 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002125 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002126 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002127 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002128 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002129 /*arglist*/ 0, 0,
2130 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002131 LParenLoc, D),
2132 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002133 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002134 }
2135
2136 // Alternatively, this parameter list may be an identifier list form for a
2137 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002138 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002139 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002140 // K&R identifier lists can't have typedefs as identifiers, per
2141 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002142 if (RequiresArg) {
2143 Diag(Tok, diag::err_argument_required_after_attribute);
2144 delete AttrList;
2145 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002146 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2147 // normal declarators, not for abstract-declarators.
2148 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002149 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002150 }
2151
2152 // Finally, a normal, non-empty parameter type list.
2153
2154 // Build up an array of information about the parsed arguments.
2155 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002156
2157 // Enter function-declaration scope, limiting any declarators to the
2158 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002159 ParseScope PrototypeScope(this,
2160 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002161
2162 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002163 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002164 while (1) {
2165 if (Tok.is(tok::ellipsis)) {
2166 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002167 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002168 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002169 }
2170
Chris Lattner9f7564b2008-04-06 06:57:35 +00002171 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002172
Chris Lattner9f7564b2008-04-06 06:57:35 +00002173 // Parse the declaration-specifiers.
2174 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002175
2176 // If the caller parsed attributes for the first argument, add them now.
2177 if (AttrList) {
2178 DS.AddAttributes(AttrList);
2179 AttrList = 0; // Only apply the attributes to the first parameter.
2180 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002181 ParseDeclarationSpecifiers(DS);
2182
Chris Lattner9f7564b2008-04-06 06:57:35 +00002183 // Parse the declarator. This is "PrototypeContext", because we must
2184 // accept either 'declarator' or 'abstract-declarator' here.
2185 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2186 ParseDeclarator(ParmDecl);
2187
2188 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002189 if (Tok.is(tok::kw___attribute)) {
2190 SourceLocation Loc;
2191 AttributeList *AttrList = ParseAttributes(&Loc);
2192 ParmDecl.AddAttributes(AttrList, Loc);
2193 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002194
Chris Lattner9f7564b2008-04-06 06:57:35 +00002195 // Remember this parsed parameter in ParamInfo.
2196 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2197
Douglas Gregor605de8d2008-12-16 21:30:33 +00002198 // DefArgToks is used when the parsing of default arguments needs
2199 // to be delayed.
2200 CachedTokens *DefArgToks = 0;
2201
Chris Lattner9f7564b2008-04-06 06:57:35 +00002202 // If no parameter was specified, verify that *something* was specified,
2203 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002204 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2205 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002206 // Completely missing, emit error.
2207 Diag(DSStart, diag::err_missing_param);
2208 } else {
2209 // Otherwise, we have something. Add it and let semantic analysis try
2210 // to grok it and add the result to the ParamInfo we are building.
2211
2212 // Inform the actions module about the parameter declarator, so it gets
2213 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002214 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002215
2216 // Parse the default argument, if any. We parse the default
2217 // arguments in all dialects; the semantic analysis in
2218 // ActOnParamDefaultArgument will reject the default argument in
2219 // C.
2220 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002221 SourceLocation EqualLoc = Tok.getLocation();
2222
Chris Lattner3e254fb2008-04-08 04:40:51 +00002223 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002224 if (D.getContext() == Declarator::MemberContext) {
2225 // If we're inside a class definition, cache the tokens
2226 // corresponding to the default argument. We'll actually parse
2227 // them when we see the end of the class definition.
2228 // FIXME: Templates will require something similar.
2229 // FIXME: Can we use a smart pointer for Toks?
2230 DefArgToks = new CachedTokens;
2231
2232 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2233 tok::semi, false)) {
2234 delete DefArgToks;
2235 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002236 Actions.ActOnParamDefaultArgumentError(Param);
2237 } else
2238 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002239 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002240 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002241 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002242
2243 OwningExprResult DefArgResult(ParseAssignmentExpression());
2244 if (DefArgResult.isInvalid()) {
2245 Actions.ActOnParamDefaultArgumentError(Param);
2246 SkipUntil(tok::comma, tok::r_paren, true, true);
2247 } else {
2248 // Inform the actions module about the default argument
2249 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002250 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002251 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002252 }
2253 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002254
2255 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002256 ParmDecl.getIdentifierLoc(), Param,
2257 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002258 }
2259
2260 // If the next token is a comma, consume it and keep reading arguments.
2261 if (Tok.isNot(tok::comma)) break;
2262
2263 // Consume the comma.
2264 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002265 }
2266
Chris Lattner9f7564b2008-04-06 06:57:35 +00002267 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002268 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002269
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002270 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002271 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002272
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002273 DeclSpec DS;
2274 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002275 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002276 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002277 if (!DS.getSourceRange().getEnd().isInvalid())
2278 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002279
2280 // Parse exception-specification[opt].
2281 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002282 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002283 }
2284
Chris Lattner4b009652007-07-25 00:24:17 +00002285 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002286 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002287 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002288 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002289 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002290 LParenLoc, D),
2291 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002292}
2293
Chris Lattner35d9c912008-04-06 06:34:08 +00002294/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2295/// we found a K&R-style identifier list instead of a type argument list. The
2296/// current token is known to be the first identifier in the list.
2297///
2298/// identifier-list: [C99 6.7.5]
2299/// identifier
2300/// identifier-list ',' identifier
2301///
2302void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2303 Declarator &D) {
2304 // Build up an array of information about the parsed arguments.
2305 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2306 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2307
2308 // If there was no identifier specified for the declarator, either we are in
2309 // an abstract-declarator, or we are in a parameter declarator which was found
2310 // to be abstract. In abstract-declarators, identifier lists are not valid:
2311 // diagnose this.
2312 if (!D.getIdentifier())
2313 Diag(Tok, diag::ext_ident_list_in_param);
2314
2315 // Tok is known to be the first identifier in the list. Remember this
2316 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002317 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002318 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002319 Tok.getLocation(),
2320 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002321
Chris Lattner113a56b2008-04-06 06:39:19 +00002322 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002323
2324 while (Tok.is(tok::comma)) {
2325 // Eat the comma.
2326 ConsumeToken();
2327
Chris Lattner113a56b2008-04-06 06:39:19 +00002328 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002329 if (Tok.isNot(tok::identifier)) {
2330 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002331 SkipUntil(tok::r_paren);
2332 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002333 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002334
Chris Lattner35d9c912008-04-06 06:34:08 +00002335 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002336
2337 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002338 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002339 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002340
2341 // Verify that the argument identifier has not already been mentioned.
2342 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002343 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002344 } else {
2345 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002346 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002347 Tok.getLocation(),
2348 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002349 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002350
2351 // Eat the identifier.
2352 ConsumeToken();
2353 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002354
2355 // If we have the closing ')', eat it and we're done.
2356 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2357
Chris Lattner113a56b2008-04-06 06:39:19 +00002358 // Remember that we parsed a function type, and remember the attributes. This
2359 // function type is always a K&R style function type, which is not varargs and
2360 // has no prototype.
2361 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002362 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002363 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002364 /*TypeQuals*/0, LParenLoc, D),
2365 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002366}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002367
Chris Lattner4b009652007-07-25 00:24:17 +00002368/// [C90] direct-declarator '[' constant-expression[opt] ']'
2369/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2370/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2371/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2372/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2373void Parser::ParseBracketDeclarator(Declarator &D) {
2374 SourceLocation StartLoc = ConsumeBracket();
2375
Chris Lattner1525c3a2008-12-18 07:27:21 +00002376 // C array syntax has many features, but by-far the most common is [] and [4].
2377 // This code does a fast path to handle some of the most obvious cases.
2378 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002379 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002380 // Remember that we parsed the empty array type.
2381 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002382 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2383 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002384 return;
2385 } else if (Tok.getKind() == tok::numeric_constant &&
2386 GetLookAheadToken(1).is(tok::r_square)) {
2387 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002388 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002389 ConsumeToken();
2390
Sebastian Redl0c986032009-02-09 18:23:29 +00002391 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002392
2393 // If there was an error parsing the assignment-expression, recover.
2394 if (ExprRes.isInvalid())
2395 ExprRes.release(); // Deallocate expr, just use [].
2396
2397 // Remember that we parsed a array type, and remember its features.
2398 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002399 ExprRes.release(), StartLoc),
2400 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002401 return;
2402 }
2403
Chris Lattner4b009652007-07-25 00:24:17 +00002404 // If valid, this location is the position where we read the 'static' keyword.
2405 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002406 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002407 StaticLoc = ConsumeToken();
2408
2409 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002410 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002411 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002412 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002413
2414 // If we haven't already read 'static', check to see if there is one after the
2415 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002416 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002417 StaticLoc = ConsumeToken();
2418
2419 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2420 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002421 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002422
2423 // Handle the case where we have '[*]' as the array size. However, a leading
2424 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2425 // the the token after the star is a ']'. Since stars in arrays are
2426 // infrequent, use of lookahead is not costly here.
2427 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002428 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002429
Chris Lattner306d4df2008-12-18 06:50:14 +00002430 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002431 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002432 StaticLoc = SourceLocation(); // Drop the static.
2433 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002434 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002435 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002436 // Note, in C89, this production uses the constant-expr production instead
2437 // of assignment-expr. The only difference is that assignment-expr allows
2438 // things like '=' and '*='. Sema rejects these in C89 mode because they
2439 // are not i-c-e's, so we don't need to distinguish between the two here.
2440
Chris Lattner4b009652007-07-25 00:24:17 +00002441 // Parse the assignment-expression now.
2442 NumElements = ParseAssignmentExpression();
2443 }
2444
2445 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002446 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002447 // If the expression was invalid, skip it.
2448 SkipUntil(tok::r_square);
2449 return;
2450 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002451
2452 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2453
Chris Lattner1525c3a2008-12-18 07:27:21 +00002454 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002455 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2456 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002457 NumElements.release(), StartLoc),
2458 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002459}
2460
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002461/// [GNU] typeof-specifier:
2462/// typeof ( expressions )
2463/// typeof ( type-name )
2464/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002465///
2466void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002467 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002468 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002469 SourceLocation StartLoc = ConsumeToken();
2470
Chris Lattner34a01ad2007-10-09 17:33:22 +00002471 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002472 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002473 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002474 return;
2475 }
2476
Sebastian Redl14ca7412008-12-11 21:36:32 +00002477 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002478 if (Result.isInvalid()) {
2479 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002480 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002481 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002482
2483 const char *PrevSpec = 0;
2484 // Check for duplicate type specifiers.
2485 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002486 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002487 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002488
2489 // FIXME: Not accurate, the range gets one token more than it should.
2490 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002491 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002492 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002493
Steve Naroff7cbb1462007-07-31 12:34:36 +00002494 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2495
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002496 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002497 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002498
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002499 assert((Ty.isInvalid() || Ty.get()) &&
2500 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002501
Chris Lattner34a01ad2007-10-09 17:33:22 +00002502 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002503 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002504 return;
2505 }
2506 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002507
2508 if (Ty.isInvalid())
2509 DS.SetTypeSpecError();
2510 else {
2511 const char *PrevSpec = 0;
2512 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2513 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2514 Ty.get()))
2515 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2516 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002517 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002518 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002519
2520 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002521 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002522 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002523 return;
2524 }
2525 RParenLoc = ConsumeParen();
2526 const char *PrevSpec = 0;
2527 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2528 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002529 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002530 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002531 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002532 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002533}
2534
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002535