blob: 426f56f4380e5da5eb91cfd51fad39700d8f898f [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"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlaaacda92009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattner4b009652007-07-25 00:24:17 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlaaacda92009-05-29 18:02:33 +000034
Chris Lattner4b009652007-07-25 00:24:17 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlaaacda92009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattner34c61332009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
72///
73/// FIXME: The GCC grammar/code for this construct implies we need two
74/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
76/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redl0c986032009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner34a01ad2007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000102
Chris Lattner34a01ad2007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
111
112 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner34a01ad2007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150 }
151 } else { // not an identifier
152 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000153 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // __attribute__(( nonnull() ))
155 ConsumeParen(); // ignore the right paren loc for now
156 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
157 0, SourceLocation(), 0, 0, CurrAttr);
158 } else {
159 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000160 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 bool ArgExprsOk = true;
162
163 // now parse the list of expressions
164 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000165 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000166 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000167 ArgExprsOk = false;
168 SkipUntil(tok::r_paren);
169 break;
170 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000171 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000172 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000173 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000174 break;
175 ConsumeToken(); // Eat the comma, move to the next argument
176 }
177 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000178 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000179 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
181 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000182 CurrAttr);
183 }
184 }
185 }
186 } else {
187 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
188 0, SourceLocation(), 0, 0, CurrAttr);
189 }
190 }
191 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000192 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000193 SourceLocation Loc = Tok.getLocation();;
194 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
195 SkipUntil(tok::r_paren, false);
196 }
197 if (EndLoc)
198 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000199 }
200 return CurrAttr;
201}
202
Eli Friedmancd231842009-06-08 07:21:15 +0000203/// ParseMicrosoftDeclSpec - Parse an __declspec construct
204///
205/// [MS] decl-specifier:
206/// __declspec ( extended-decl-modifier-seq )
207///
208/// [MS] extended-decl-modifier-seq:
209/// extended-decl-modifier[opt]
210/// extended-decl-modifier extended-decl-modifier-seq
211
Eli Friedman891d82f2009-06-08 23:27:34 +0000212AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000213 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmancd231842009-06-08 07:21:15 +0000214
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000215 ConsumeToken();
Eli Friedmancd231842009-06-08 07:21:15 +0000216 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
217 "declspec")) {
218 SkipUntil(tok::r_paren, true); // skip until ) or ;
219 return CurrAttr;
220 }
Eli Friedman891d82f2009-06-08 23:27:34 +0000221 while (Tok.getIdentifierInfo()) {
Eli Friedmancd231842009-06-08 07:21:15 +0000222 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
223 SourceLocation AttrNameLoc = ConsumeToken();
224 if (Tok.is(tok::l_paren)) {
225 ConsumeParen();
226 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
227 // correctly.
228 OwningExprResult ArgExpr(ParseAssignmentExpression());
229 if (!ArgExpr.isInvalid()) {
230 ExprTy* ExprList = ArgExpr.take();
231 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
232 SourceLocation(), &ExprList, 1,
233 CurrAttr, true);
234 }
235 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
236 SkipUntil(tok::r_paren, false);
237 } else {
238 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
239 0, 0, CurrAttr, true);
240 }
241 }
242 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
243 SkipUntil(tok::r_paren, false);
Eli Friedman891d82f2009-06-08 23:27:34 +0000244 return CurrAttr;
245}
246
247AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
248 // Treat these like attributes
249 // FIXME: Allow Sema to distinguish between these and real attributes!
250 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
251 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
252 Tok.is(tok::kw___w64)) {
253 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
254 SourceLocation AttrNameLoc = ConsumeToken();
255 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
256 // FIXME: Support these properly!
257 continue;
258 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
259 SourceLocation(), 0, 0, CurrAttr, true);
260 }
261 return CurrAttr;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000262}
263
Chris Lattner4b009652007-07-25 00:24:17 +0000264/// ParseDeclaration - Parse a full 'declaration', which consists of
265/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000266/// 'Context' should be a Declarator::TheContext value. This returns the
267/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000268///
269/// declaration: [C99 6.7]
270/// block-declaration ->
271/// simple-declaration
272/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000273/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000274/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000275/// [C++] using-directive
Douglas Gregorcad27f62009-06-22 23:06:13 +0000276/// [C++] using-declaration
Sebastian Redla8cecf62009-03-24 22:27:57 +0000277/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000278/// others... [FIXME]
279///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000280Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
281 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000282 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000283 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000284 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000285 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000286 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000287 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000288 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000289 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000290 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000291 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000292 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000293 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000294 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000295 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000296 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000297 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000298 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000299 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000300
301 // This routine returns a DeclGroup, if the thing we parsed only contains a
302 // single decl, convert it now.
303 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000304}
305
306/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
307/// declaration-specifiers init-declarator-list[opt] ';'
308///[C90/C++]init-declarator-list ';' [TODO]
309/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000310///
311/// If RequireSemi is false, this does not check for a ';' at the end of the
312/// declaration.
313Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000314 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000315 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000316 // Parse the common declaration-specifiers piece.
317 DeclSpec DS;
318 ParseDeclarationSpecifiers(DS);
319
320 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
321 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000322 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000323 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000324 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
325 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000326 }
327
328 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
329 ParseDeclarator(DeclaratorInfo);
330
Chris Lattner2c41d482009-03-29 17:18:04 +0000331 DeclGroupPtrTy DG =
332 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000333
Chris Lattner9802a0a2009-04-02 04:16:50 +0000334 DeclEnd = Tok.getLocation();
335
Chris Lattnerf8016042009-03-29 17:27:48 +0000336 // If the client wants to check what comes after the declaration, just return
337 // immediately without checking anything!
338 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000339
340 if (Tok.is(tok::semi)) {
341 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000342 return DG;
343 }
344
Chris Lattner2c41d482009-03-29 17:18:04 +0000345 Diag(Tok, diag::err_expected_semi_declation);
346 // Skip to end of block or statement
347 SkipUntil(tok::r_brace, true, true);
348 if (Tok.is(tok::semi))
349 ConsumeToken();
350 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000351}
352
Douglas Gregore3298aa2009-05-12 21:31:51 +0000353/// \brief Parse 'declaration' after parsing 'declaration-specifiers
354/// declarator'. This method parses the remainder of the declaration
355/// (including any attributes or initializer, among other things) and
356/// finalizes the declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000357///
Chris Lattner4b009652007-07-25 00:24:17 +0000358/// init-declarator: [C99 6.7]
359/// declarator
360/// declarator '=' initializer
361/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
362/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000363/// [C++] declarator initializer[opt]
364///
365/// [C++] initializer:
366/// [C++] '=' initializer-clause
367/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000368/// [C++0x] '=' 'default' [TODO]
369/// [C++0x] '=' 'delete'
370///
371/// According to the standard grammar, =default and =delete are function
372/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000373///
Douglas Gregore3298aa2009-05-12 21:31:51 +0000374Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
375 // If a simple-asm-expr is present, parse it.
376 if (Tok.is(tok::kw_asm)) {
377 SourceLocation Loc;
378 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
379 if (AsmLabel.isInvalid()) {
380 SkipUntil(tok::semi, true, true);
381 return DeclPtrTy();
382 }
383
384 D.setAsmLabel(AsmLabel.release());
385 D.SetRangeEnd(Loc);
386 }
387
388 // If attributes are present, parse them.
389 if (Tok.is(tok::kw___attribute)) {
390 SourceLocation Loc;
391 AttributeList *AttrList = ParseAttributes(&Loc);
392 D.AddAttributes(AttrList, Loc);
393 }
394
395 // Inform the current actions module that we just parsed this declarator.
396 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
397
398 // Parse declarator '=' initializer.
399 if (Tok.is(tok::equal)) {
400 ConsumeToken();
401 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
402 SourceLocation DelLoc = ConsumeToken();
403 Actions.SetDeclDeleted(ThisDecl, DelLoc);
404 } else {
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000405 if (getLang().CPlusPlus)
406 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
407
Douglas Gregore3298aa2009-05-12 21:31:51 +0000408 OwningExprResult Init(ParseInitializer());
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000409
410 if (getLang().CPlusPlus)
411 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
412
Douglas Gregore3298aa2009-05-12 21:31:51 +0000413 if (Init.isInvalid()) {
414 SkipUntil(tok::semi, true, true);
415 return DeclPtrTy();
416 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000417 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000418 }
419 } else if (Tok.is(tok::l_paren)) {
420 // Parse C++ direct initializer: '(' expression-list ')'
421 SourceLocation LParenLoc = ConsumeParen();
422 ExprVector Exprs(Actions);
423 CommaLocsTy CommaLocs;
424
425 if (ParseExpressionList(Exprs, CommaLocs)) {
426 SkipUntil(tok::r_paren);
427 } else {
428 // Match the ')'.
429 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
430
431 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
432 "Unexpected number of commas!");
433 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
434 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000435 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000436 }
437 } else {
438 Actions.ActOnUninitializedDecl(ThisDecl);
439 }
440
441 return ThisDecl;
442}
443
444/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
445/// parsing 'declaration-specifiers declarator'. This method is split out this
446/// way to handle the ambiguity between top-level function-definitions and
447/// declarations.
448///
449/// init-declarator-list: [C99 6.7]
450/// init-declarator
451/// init-declarator-list ',' init-declarator
452///
453/// According to the standard grammar, =default and =delete are function
454/// definitions, but that definitely doesn't fit with the parser here.
455///
Chris Lattnera17991f2009-03-29 16:50:03 +0000456Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000457ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000458 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
459 // that we parse together here.
460 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000461
462 // At this point, we know that it is not a function definition. Parse the
463 // rest of the init-declarator-list.
464 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000465 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
466 if (ThisDecl.get())
467 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000468
Chris Lattner4b009652007-07-25 00:24:17 +0000469 // If we don't have a comma, it is either the end of the list (a ';') or an
470 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000471 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000472 break;
473
474 // Consume the comma.
475 ConsumeToken();
476
477 // Parse the next declarator.
478 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000479
480 // Accept attributes in an init-declarator. In the first declarator in a
481 // declaration, these would be part of the declspec. In subsequent
482 // declarators, they become part of the declarator itself, so that they
483 // don't apply to declarators after *this* one. Examples:
484 // short __attribute__((common)) var; -> declspec
485 // short var __attribute__((common)); -> declarator
486 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000487 if (Tok.is(tok::kw___attribute)) {
488 SourceLocation Loc;
489 AttributeList *AttrList = ParseAttributes(&Loc);
490 D.AddAttributes(AttrList, Loc);
491 }
Chris Lattner926cf542008-10-20 04:57:38 +0000492
Chris Lattner4b009652007-07-25 00:24:17 +0000493 ParseDeclarator(D);
494 }
495
Eli Friedman4d57af22009-05-29 01:49:24 +0000496 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
497 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000498 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000499}
500
501/// ParseSpecifierQualifierList
502/// specifier-qualifier-list:
503/// type-specifier specifier-qualifier-list[opt]
504/// type-qualifier specifier-qualifier-list[opt]
505/// [GNU] attributes specifier-qualifier-list[opt]
506///
507void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
508 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
509 /// parse declaration-specifiers and complain about extra stuff.
510 ParseDeclarationSpecifiers(DS);
511
512 // Validate declspec for type-name.
513 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000514 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
515 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000516 Diag(Tok, diag::err_typename_requires_specqual);
517
518 // Issue diagnostic and remove storage class if present.
519 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
520 if (DS.getStorageClassSpecLoc().isValid())
521 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
522 else
523 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
524 DS.ClearStorageClassSpecs();
525 }
526
527 // Issue diagnostic and remove function specfier if present.
528 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000529 if (DS.isInlineSpecified())
530 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
531 if (DS.isVirtualSpecified())
532 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
533 if (DS.isExplicitSpecified())
534 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000535 DS.ClearFunctionSpecs();
536 }
537}
538
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000539/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
540/// specified token is valid after the identifier in a declarator which
541/// immediately follows the declspec. For example, these things are valid:
542///
543/// int x [ 4]; // direct-declarator
544/// int x ( int y); // direct-declarator
545/// int(int x ) // direct-declarator
546/// int x ; // simple-declaration
547/// int x = 17; // init-declarator-list
548/// int x , y; // init-declarator-list
549/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000550/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000551/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000552///
553/// This is not, because 'x' does not immediately follow the declspec (though
554/// ')' happens to be valid anyway).
555/// int (x)
556///
557static bool isValidAfterIdentifierInDeclarator(const Token &T) {
558 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
559 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000560 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000561}
562
Chris Lattner82353c62009-04-14 21:34:55 +0000563
564/// ParseImplicitInt - This method is called when we have an non-typename
565/// identifier in a declspec (which normally terminates the decl spec) when
566/// the declspec has no type specifier. In this case, the declspec is either
567/// malformed or is "implicit int" (in K&R and C89).
568///
569/// This method handles diagnosing this prettily and returns false if the
570/// declspec is done being processed. If it recovers and thinks there may be
571/// other pieces of declspec after it, it returns true.
572///
Chris Lattner52cd7622009-04-14 22:17:06 +0000573bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000574 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000575 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000576 assert(Tok.is(tok::identifier) && "should have identifier");
577
Chris Lattner82353c62009-04-14 21:34:55 +0000578 SourceLocation Loc = Tok.getLocation();
579 // If we see an identifier that is not a type name, we normally would
580 // parse it as the identifer being declared. However, when a typename
581 // is typo'd or the definition is not included, this will incorrectly
582 // parse the typename as the identifier name and fall over misparsing
583 // later parts of the diagnostic.
584 //
585 // As such, we try to do some look-ahead in cases where this would
586 // otherwise be an "implicit-int" case to see if this is invalid. For
587 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
588 // an identifier with implicit int, we'd get a parse error because the
589 // next token is obviously invalid for a type. Parse these as a case
590 // with an invalid type specifier.
591 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
592
593 // Since we know that this either implicit int (which is rare) or an
594 // error, we'd do lookahead to try to do better recovery.
595 if (isValidAfterIdentifierInDeclarator(NextToken())) {
596 // If this token is valid for implicit int, e.g. "static x = 4", then
597 // we just avoid eating the identifier, so it will be parsed as the
598 // identifier in the declarator.
599 return false;
600 }
601
602 // Otherwise, if we don't consume this token, we are going to emit an
603 // error anyway. Try to recover from various common problems. Check
604 // to see if this was a reference to a tag name without a tag specified.
605 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000606 //
607 // C++ doesn't need this, and isTagName doesn't take SS.
608 if (SS == 0) {
609 const char *TagName = 0;
610 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000611
Chris Lattner82353c62009-04-14 21:34:55 +0000612 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
613 default: break;
614 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
615 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
616 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
617 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
618 }
Chris Lattner82353c62009-04-14 21:34:55 +0000619
Chris Lattner52cd7622009-04-14 22:17:06 +0000620 if (TagName) {
621 Diag(Loc, diag::err_use_of_tag_name_without_tag)
622 << Tok.getIdentifierInfo() << TagName
623 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
624
625 // Parse this as a tag as if the missing tag were present.
626 if (TagKind == tok::kw_enum)
627 ParseEnumSpecifier(Loc, DS, AS);
628 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000629 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000630 return true;
631 }
Chris Lattner82353c62009-04-14 21:34:55 +0000632 }
633
634 // Since this is almost certainly an invalid type name, emit a
635 // diagnostic that says it, eat the token, and mark the declspec as
636 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000637 SourceRange R;
638 if (SS) R = SS->getRange();
639
640 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000641 const char *PrevSpec;
642 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
643 DS.SetRangeEnd(Tok.getLocation());
644 ConsumeToken();
645
646 // TODO: Could inject an invalid typedef decl in an enclosing scope to
647 // avoid rippling error messages on subsequent uses of the same type,
648 // could be useful if #include was forgotten.
649 return false;
650}
651
Chris Lattner4b009652007-07-25 00:24:17 +0000652/// ParseDeclarationSpecifiers
653/// declaration-specifiers: [C99 6.7]
654/// storage-class-specifier declaration-specifiers[opt]
655/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000656/// [C99] function-specifier declaration-specifiers[opt]
657/// [GNU] attributes declaration-specifiers[opt]
658///
659/// storage-class-specifier: [C99 6.7.1]
660/// 'typedef'
661/// 'extern'
662/// 'static'
663/// 'auto'
664/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000665/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000666/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000667/// function-specifier: [C99 6.7.4]
668/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000669/// [C++] 'virtual'
670/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000671/// 'friend': [C++ dcl.friend]
672
Chris Lattner4b009652007-07-25 00:24:17 +0000673///
Douglas Gregor52473432008-12-24 02:52:09 +0000674void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000675 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000676 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000677 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000678 while (1) {
679 int isInvalid = false;
680 const char *PrevSpec = 0;
681 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000682
Chris Lattner4b009652007-07-25 00:24:17 +0000683 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000684 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000685 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000686 // If this is not a declaration specifier token, we're done reading decl
687 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000688 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000689 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000690
691 case tok::coloncolon: // ::foo::bar
692 // Annotate C++ scope specifiers. If we get one, loop.
693 if (TryAnnotateCXXScopeToken())
694 continue;
695 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000696
697 case tok::annot_cxxscope: {
698 if (DS.hasTypeSpecifier())
699 goto DoneWithDeclSpec;
700
701 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000702 Token Next = NextToken();
703 if (Next.is(tok::annot_template_id) &&
704 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000705 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000706 // We have a qualified template-id, e.g., N::A<int>
707 CXXScopeSpec SS;
708 ParseOptionalCXXScopeSpecifier(SS);
709 assert(Tok.is(tok::annot_template_id) &&
710 "ParseOptionalCXXScopeSpecifier not working");
711 AnnotateTemplateIdTokenAsType(&SS);
712 continue;
713 }
714
715 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000716 goto DoneWithDeclSpec;
717
718 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000719 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000720 SS.setRange(Tok.getAnnotationRange());
721
722 // If the next token is the name of the class type that the C++ scope
723 // denotes, followed by a '(', then this is a constructor declaration.
724 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000725 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000726 CurScope, &SS) &&
727 GetLookAheadToken(2).is(tok::l_paren))
728 goto DoneWithDeclSpec;
729
Douglas Gregor1075a162009-02-04 17:00:24 +0000730 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
731 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000732
Chris Lattner52cd7622009-04-14 22:17:06 +0000733 // If the referenced identifier is not a type, then this declspec is
734 // erroneous: We already checked about that it has no type specifier, and
735 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
736 // typename.
737 if (TypeRep == 0) {
738 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000739 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000740 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000741 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000742
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000743 ConsumeToken(); // The C++ scope.
744
Douglas Gregora60c62e2009-02-09 15:09:02 +0000745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000746 TypeRep);
747 if (isInvalid)
748 break;
749
750 DS.SetRangeEnd(Tok.getLocation());
751 ConsumeToken(); // The typename.
752
753 continue;
754 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000755
756 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000757 if (Tok.getAnnotationValue())
758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
759 Tok.getAnnotationValue());
760 else
761 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000762 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
763 ConsumeToken(); // The typename
764
765 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
766 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
767 // Objective-C interface. If we don't have Objective-C or a '<', this is
768 // just a normal reference to a typedef name.
769 if (!Tok.is(tok::less) || !getLang().ObjC1)
770 continue;
771
772 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000773 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000774 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
775 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
776
777 DS.SetRangeEnd(EndProtoLoc);
778 continue;
779 }
780
Chris Lattnerfda18db2008-07-26 01:18:38 +0000781 // typedef-name
782 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000783 // In C++, check to see if this is a scope specifier like foo::bar::, if
784 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000785 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
786 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000787
Chris Lattnerfda18db2008-07-26 01:18:38 +0000788 // This identifier can only be a typedef name if we haven't already seen
789 // a type-specifier. Without this check we misparse:
790 // typedef int X; struct Y { short X; }; as 'short int'.
791 if (DS.hasTypeSpecifier())
792 goto DoneWithDeclSpec;
793
794 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000795 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
796 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000797
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000798 // If this is not a typedef name, don't parse it as part of the declspec,
799 // it must be an implicit int or an error.
800 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000801 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000802 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000803 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000804
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000805 // C++: If the identifier is actually the name of the class type
806 // being defined and the next token is a '(', then this is a
807 // constructor declaration. We're done with the decl-specifiers
808 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000809 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000810 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
811 NextToken().getKind() == tok::l_paren)
812 goto DoneWithDeclSpec;
813
Douglas Gregora60c62e2009-02-09 15:09:02 +0000814 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000815 TypeRep);
816 if (isInvalid)
817 break;
818
819 DS.SetRangeEnd(Tok.getLocation());
820 ConsumeToken(); // The identifier
821
822 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
823 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
824 // Objective-C interface. If we don't have Objective-C or a '<', this is
825 // just a normal reference to a typedef name.
826 if (!Tok.is(tok::less) || !getLang().ObjC1)
827 continue;
828
829 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000830 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000831 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000832 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000833
834 DS.SetRangeEnd(EndProtoLoc);
835
Steve Narofff7683302008-09-22 10:28:57 +0000836 // Need to support trailing type qualifiers (e.g. "id<p> const").
837 // If a type specifier follows, it will be diagnosed elsewhere.
838 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000839 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000840
841 // type-name
842 case tok::annot_template_id: {
843 TemplateIdAnnotation *TemplateId
844 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000845 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000846 // This template-id does not refer to a type name, so we're
847 // done with the type-specifiers.
848 goto DoneWithDeclSpec;
849 }
850
851 // Turn the template-id annotation token into a type annotation
852 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000853 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000854 continue;
855 }
856
Chris Lattner4b009652007-07-25 00:24:17 +0000857 // GNU attributes support.
858 case tok::kw___attribute:
859 DS.AddAttributes(ParseAttributes());
860 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000861
862 // Microsoft declspec support.
863 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000864 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000865 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000866
Steve Naroffedd04d52008-12-25 14:16:32 +0000867 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000868 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000869 // FIXME: Add handling here!
870 break;
871
872 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000873 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000874 case tok::kw___cdecl:
875 case tok::kw___stdcall:
876 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000877 DS.AddAttributes(ParseMicrosoftTypeAttributes());
878 continue;
879
Chris Lattner4b009652007-07-25 00:24:17 +0000880 // storage-class-specifier
881 case tok::kw_typedef:
882 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
883 break;
884 case tok::kw_extern:
885 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000886 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000887 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
888 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000889 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000890 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
891 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000892 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000893 case tok::kw_static:
894 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000895 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000896 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
897 break;
898 case tok::kw_auto:
899 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
900 break;
901 case tok::kw_register:
902 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
903 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000904 case tok::kw_mutable:
905 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
906 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000907 case tok::kw___thread:
908 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
909 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000910
Chris Lattner4b009652007-07-25 00:24:17 +0000911 // function-specifier
912 case tok::kw_inline:
913 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
914 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000915 case tok::kw_virtual:
916 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
917 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000918 case tok::kw_explicit:
919 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
920 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000921
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000922 // friend
923 case tok::kw_friend:
924 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
925 break;
926
Chris Lattnerc297b722009-01-21 19:48:37 +0000927 // type-specifier
928 case tok::kw_short:
929 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
930 break;
931 case tok::kw_long:
932 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
933 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
934 else
935 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
936 break;
937 case tok::kw_signed:
938 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
939 break;
940 case tok::kw_unsigned:
941 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
942 break;
943 case tok::kw__Complex:
944 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
945 break;
946 case tok::kw__Imaginary:
947 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
948 break;
949 case tok::kw_void:
950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
951 break;
952 case tok::kw_char:
953 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
954 break;
955 case tok::kw_int:
956 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
957 break;
958 case tok::kw_float:
959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
960 break;
961 case tok::kw_double:
962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
963 break;
964 case tok::kw_wchar_t:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
966 break;
967 case tok::kw_bool:
968 case tok::kw__Bool:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
970 break;
971 case tok::kw__Decimal32:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
973 break;
974 case tok::kw__Decimal64:
975 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
976 break;
977 case tok::kw__Decimal128:
978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
979 break;
980
981 // class-specifier:
982 case tok::kw_class:
983 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000984 case tok::kw_union: {
985 tok::TokenKind Kind = Tok.getKind();
986 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000987 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000988 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000989 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000990
991 // enum-specifier:
992 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000993 ConsumeToken();
994 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000995 continue;
996
997 // cv-qualifier:
998 case tok::kw_const:
999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
1000 break;
1001 case tok::kw_volatile:
1002 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1003 getLang())*2;
1004 break;
1005 case tok::kw_restrict:
1006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1007 getLang())*2;
1008 break;
1009
Douglas Gregord3022602009-03-27 23:10:48 +00001010 // C++ typename-specifier:
1011 case tok::kw_typename:
1012 if (TryAnnotateTypeOrScopeToken())
1013 continue;
1014 break;
1015
Chris Lattnerc297b722009-01-21 19:48:37 +00001016 // GNU typeof support.
1017 case tok::kw_typeof:
1018 ParseTypeofSpecifier(DS);
1019 continue;
1020
Steve Naroff5f0466b2008-06-05 00:02:44 +00001021 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001022 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001023 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1024 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001025 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001026 goto DoneWithDeclSpec;
1027
1028 {
1029 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001030 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001031 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +00001032 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001033 DS.SetRangeEnd(EndProtoLoc);
1034
Chris Lattnerf006a222008-11-18 07:48:38 +00001035 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001036 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001037 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001038 // Need to support trailing type qualifiers (e.g. "id<p> const").
1039 // If a type specifier follows, it will be diagnosed elsewhere.
1040 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001041 }
Chris Lattner4b009652007-07-25 00:24:17 +00001042 }
1043 // If the specifier combination wasn't legal, issue a diagnostic.
1044 if (isInvalid) {
1045 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001046 // Pick between error or extwarn.
1047 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1048 : diag::ext_duplicate_declspec;
1049 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001050 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001051 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001052 ConsumeToken();
1053 }
1054}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001055
Chris Lattnerd706dc82009-01-06 06:59:53 +00001056/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001057/// primarily follow the C++ grammar with additions for C99 and GNU,
1058/// which together subsume the C grammar. Note that the C++
1059/// type-specifier also includes the C type-qualifier (for const,
1060/// volatile, and C99 restrict). Returns true if a type-specifier was
1061/// found (and parsed), false otherwise.
1062///
1063/// type-specifier: [C++ 7.1.5]
1064/// simple-type-specifier
1065/// class-specifier
1066/// enum-specifier
1067/// elaborated-type-specifier [TODO]
1068/// cv-qualifier
1069///
1070/// cv-qualifier: [C++ 7.1.5.1]
1071/// 'const'
1072/// 'volatile'
1073/// [C99] 'restrict'
1074///
1075/// simple-type-specifier: [ C++ 7.1.5.2]
1076/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1077/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1078/// 'char'
1079/// 'wchar_t'
1080/// 'bool'
1081/// 'short'
1082/// 'int'
1083/// 'long'
1084/// 'signed'
1085/// 'unsigned'
1086/// 'float'
1087/// 'double'
1088/// 'void'
1089/// [C99] '_Bool'
1090/// [C99] '_Complex'
1091/// [C99] '_Imaginary' // Removed in TC2?
1092/// [GNU] '_Decimal32'
1093/// [GNU] '_Decimal64'
1094/// [GNU] '_Decimal128'
1095/// [GNU] typeof-specifier
1096/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1097/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +00001098bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1099 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001100 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001101 SourceLocation Loc = Tok.getLocation();
1102
1103 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001104 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001105 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001106 // Annotate typenames and C++ scope specifiers. If we get one, just
1107 // recurse to handle whatever we get.
1108 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001109 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001110 // Otherwise, not a type specifier.
1111 return false;
1112 case tok::coloncolon: // ::foo::bar
1113 if (NextToken().is(tok::kw_new) || // ::new
1114 NextToken().is(tok::kw_delete)) // ::delete
1115 return false;
1116
1117 // Annotate typenames and C++ scope specifiers. If we get one, just
1118 // recurse to handle whatever we get.
1119 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001120 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001121 // Otherwise, not a type specifier.
1122 return false;
1123
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001124 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001125 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001126 if (Tok.getAnnotationValue())
1127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1128 Tok.getAnnotationValue());
1129 else
1130 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001131 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1132 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001133
1134 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1135 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1136 // Objective-C interface. If we don't have Objective-C or a '<', this is
1137 // just a normal reference to a typedef name.
1138 if (!Tok.is(tok::less) || !getLang().ObjC1)
1139 return true;
1140
1141 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001142 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001143 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1144 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1145
1146 DS.SetRangeEnd(EndProtoLoc);
1147 return true;
1148 }
1149
1150 case tok::kw_short:
1151 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1152 break;
1153 case tok::kw_long:
1154 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1155 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1156 else
1157 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1158 break;
1159 case tok::kw_signed:
1160 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1161 break;
1162 case tok::kw_unsigned:
1163 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1164 break;
1165 case tok::kw__Complex:
1166 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1167 break;
1168 case tok::kw__Imaginary:
1169 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1170 break;
1171 case tok::kw_void:
1172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1173 break;
1174 case tok::kw_char:
1175 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1176 break;
1177 case tok::kw_int:
1178 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1179 break;
1180 case tok::kw_float:
1181 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1182 break;
1183 case tok::kw_double:
1184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1185 break;
1186 case tok::kw_wchar_t:
1187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1188 break;
1189 case tok::kw_bool:
1190 case tok::kw__Bool:
1191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1192 break;
1193 case tok::kw__Decimal32:
1194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1195 break;
1196 case tok::kw__Decimal64:
1197 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1198 break;
1199 case tok::kw__Decimal128:
1200 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1201 break;
1202
1203 // class-specifier:
1204 case tok::kw_class:
1205 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001206 case tok::kw_union: {
1207 tok::TokenKind Kind = Tok.getKind();
1208 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001209 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001210 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001211 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001212
1213 // enum-specifier:
1214 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001215 ConsumeToken();
1216 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001217 return true;
1218
1219 // cv-qualifier:
1220 case tok::kw_const:
1221 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1222 getLang())*2;
1223 break;
1224 case tok::kw_volatile:
1225 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1226 getLang())*2;
1227 break;
1228 case tok::kw_restrict:
1229 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1230 getLang())*2;
1231 break;
1232
1233 // GNU typeof support.
1234 case tok::kw_typeof:
1235 ParseTypeofSpecifier(DS);
1236 return true;
1237
Eli Friedman891d82f2009-06-08 23:27:34 +00001238 case tok::kw___ptr64:
1239 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001240 case tok::kw___cdecl:
1241 case tok::kw___stdcall:
1242 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001243 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001244 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001245
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001246 default:
1247 // Not a type-specifier; do nothing.
1248 return false;
1249 }
1250
1251 // If the specifier combination wasn't legal, issue a diagnostic.
1252 if (isInvalid) {
1253 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001254 // Pick between error or extwarn.
1255 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1256 : diag::ext_duplicate_declspec;
1257 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001258 }
1259 DS.SetRangeEnd(Tok.getLocation());
1260 ConsumeToken(); // whatever we parsed above.
1261 return true;
1262}
Chris Lattner4b009652007-07-25 00:24:17 +00001263
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001264/// ParseStructDeclaration - Parse a struct declaration without the terminating
1265/// semicolon.
1266///
Chris Lattner4b009652007-07-25 00:24:17 +00001267/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001268/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001269/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001270/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001271/// struct-declarator-list:
1272/// struct-declarator
1273/// struct-declarator-list ',' struct-declarator
1274/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1275/// struct-declarator:
1276/// declarator
1277/// [GNU] declarator attributes[opt]
1278/// declarator[opt] ':' constant-expression
1279/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1280///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001281void Parser::
1282ParseStructDeclaration(DeclSpec &DS,
1283 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001284 if (Tok.is(tok::kw___extension__)) {
1285 // __extension__ silences extension warnings in the subexpression.
1286 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001287 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001288 return ParseStructDeclaration(DS, Fields);
1289 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001290
1291 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001292 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001293 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001294
Douglas Gregorb748fc52009-01-12 22:49:06 +00001295 // If there are no declarators, this is a free-standing declaration
1296 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001297 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001298 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001299 return;
1300 }
1301
1302 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001303 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001304 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001305 FieldDeclarator &DeclaratorInfo = Fields.back();
1306
Steve Naroffa9adf112007-08-20 22:28:22 +00001307 /// struct-declarator: declarator
1308 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001309 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001310 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001311
Chris Lattner34a01ad2007-10-09 17:33:22 +00001312 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001313 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001314 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001315 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001316 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001317 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001318 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001319 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001320
Steve Naroffa9adf112007-08-20 22:28:22 +00001321 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001322 if (Tok.is(tok::kw___attribute)) {
1323 SourceLocation Loc;
1324 AttributeList *AttrList = ParseAttributes(&Loc);
1325 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1326 }
1327
Steve Naroffa9adf112007-08-20 22:28:22 +00001328 // If we don't have a comma, it is either the end of the list (a ';')
1329 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001330 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001331 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001332
Steve Naroffa9adf112007-08-20 22:28:22 +00001333 // Consume the comma.
1334 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001335
Steve Naroffa9adf112007-08-20 22:28:22 +00001336 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001337 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001338
Steve Naroffa9adf112007-08-20 22:28:22 +00001339 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001340 if (Tok.is(tok::kw___attribute)) {
1341 SourceLocation Loc;
1342 AttributeList *AttrList = ParseAttributes(&Loc);
1343 Fields.back().D.AddAttributes(AttrList, Loc);
1344 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001345 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001346}
1347
1348/// ParseStructUnionBody
1349/// struct-contents:
1350/// struct-declaration-list
1351/// [EXT] empty
1352/// [GNU] "struct-declaration-list" without terminatoring ';'
1353/// struct-declaration-list:
1354/// struct-declaration
1355/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001356/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001357///
Chris Lattner4b009652007-07-25 00:24:17 +00001358void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001359 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001360 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1361 PP.getSourceManager(),
1362 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001363
Chris Lattner4b009652007-07-25 00:24:17 +00001364 SourceLocation LBraceLoc = ConsumeBrace();
1365
Douglas Gregorcab994d2009-01-09 22:42:13 +00001366 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001367 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1368
Chris Lattner4b009652007-07-25 00:24:17 +00001369 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1370 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001371 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001372 Diag(Tok, diag::ext_empty_struct_union_enum)
1373 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001374
Chris Lattner5261d0c2009-03-28 19:18:32 +00001375 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001376 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1377
Chris Lattner4b009652007-07-25 00:24:17 +00001378 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001379 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001380 // Each iteration of this loop reads one struct-declaration.
1381
1382 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001383 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001384 Diag(Tok, diag::ext_extra_struct_semi)
1385 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001386 ConsumeToken();
1387 continue;
1388 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001389
1390 // Parse all the comma separated declarators.
1391 DeclSpec DS;
1392 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001393 if (!Tok.is(tok::at)) {
1394 ParseStructDeclaration(DS, FieldDeclarators);
1395
1396 // Convert them all to fields.
1397 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1398 FieldDeclarator &FD = FieldDeclarators[i];
1399 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001400 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1401 DS.getSourceRange().getBegin(),
1402 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001403 FieldDecls.push_back(Field);
1404 }
1405 } else { // Handle @defs
1406 ConsumeToken();
1407 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1408 Diag(Tok, diag::err_unexpected_at);
1409 SkipUntil(tok::semi, true, true);
1410 continue;
1411 }
1412 ConsumeToken();
1413 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1414 if (!Tok.is(tok::identifier)) {
1415 Diag(Tok, diag::err_expected_ident);
1416 SkipUntil(tok::semi, true, true);
1417 continue;
1418 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001419 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001420 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1421 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001422 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1423 ConsumeToken();
1424 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1425 }
Chris Lattner4b009652007-07-25 00:24:17 +00001426
Chris Lattner34a01ad2007-10-09 17:33:22 +00001427 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001428 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001429 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001430 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001431 break;
1432 } else {
1433 Diag(Tok, diag::err_expected_semi_decl_list);
1434 // Skip to end of block or statement
1435 SkipUntil(tok::r_brace, true, true);
1436 }
1437 }
1438
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001439 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001440
Chris Lattner4b009652007-07-25 00:24:17 +00001441 AttributeList *AttrList = 0;
1442 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001443 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001444 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001445
1446 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001447 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001448 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001449 AttrList);
1450 StructScope.Exit();
1451 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001452}
1453
1454
1455/// ParseEnumSpecifier
1456/// enum-specifier: [C99 6.7.2.2]
1457/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001458///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001459/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1460/// '}' attributes[opt]
1461/// 'enum' identifier
1462/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001463///
1464/// [C++] elaborated-type-specifier:
1465/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1466///
Chris Lattner197b4342009-04-12 21:49:30 +00001467void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1468 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001469 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001470
1471 AttributeList *Attr = 0;
1472 // If attributes exist after tag, parse them.
1473 if (Tok.is(tok::kw___attribute))
1474 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001475
1476 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001477 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001478 if (Tok.isNot(tok::identifier)) {
1479 Diag(Tok, diag::err_expected_ident);
1480 if (Tok.isNot(tok::l_brace)) {
1481 // Has no name and is not a definition.
1482 // Skip the rest of this declarator, up until the comma or semicolon.
1483 SkipUntil(tok::comma, true);
1484 return;
1485 }
1486 }
1487 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001488
1489 // Must have either 'enum name' or 'enum {...}'.
1490 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1491 Diag(Tok, diag::err_expected_ident_lbrace);
1492
1493 // Skip the rest of this declarator, up until the comma or semicolon.
1494 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001495 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001496 }
1497
1498 // If an identifier is present, consume and remember it.
1499 IdentifierInfo *Name = 0;
1500 SourceLocation NameLoc;
1501 if (Tok.is(tok::identifier)) {
1502 Name = Tok.getIdentifierInfo();
1503 NameLoc = ConsumeToken();
1504 }
1505
1506 // There are three options here. If we have 'enum foo;', then this is a
1507 // forward declaration. If we have 'enum foo {...' then this is a
1508 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1509 //
1510 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1511 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1512 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1513 //
1514 Action::TagKind TK;
1515 if (Tok.is(tok::l_brace))
1516 TK = Action::TK_Definition;
1517 else if (Tok.is(tok::semi))
1518 TK = Action::TK_Declaration;
1519 else
1520 TK = Action::TK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001521 bool Owned = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001522 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001523 StartLoc, SS, Name, NameLoc, Attr, AS,
1524 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001525
Chris Lattner34a01ad2007-10-09 17:33:22 +00001526 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001527 ParseEnumBody(StartLoc, TagDecl);
1528
1529 // TODO: semantic analysis on the declspec for enums.
1530 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001531 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +00001532 TagDecl.getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +00001533 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001534}
1535
1536/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1537/// enumerator-list:
1538/// enumerator
1539/// enumerator-list ',' enumerator
1540/// enumerator:
1541/// enumeration-constant
1542/// enumeration-constant '=' constant-expression
1543/// enumeration-constant:
1544/// identifier
1545///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001546void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001547 // Enter the scope of the enum body and start the definition.
1548 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001549 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001550
Chris Lattner4b009652007-07-25 00:24:17 +00001551 SourceLocation LBraceLoc = ConsumeBrace();
1552
Chris Lattnerc9a92452007-08-27 17:24:30 +00001553 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001554 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001555 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001556
Chris Lattner5261d0c2009-03-28 19:18:32 +00001557 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001558
Chris Lattner5261d0c2009-03-28 19:18:32 +00001559 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001560
1561 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001562 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001563 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1564 SourceLocation IdentLoc = ConsumeToken();
1565
1566 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001567 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001568 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001569 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001570 AssignedVal = ParseConstantExpression();
1571 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001572 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001573 }
1574
1575 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001576 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1577 LastEnumConstDecl,
1578 IdentLoc, Ident,
1579 EqualLoc,
1580 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001581 EnumConstantDecls.push_back(EnumConstDecl);
1582 LastEnumConstDecl = EnumConstDecl;
1583
Chris Lattner34a01ad2007-10-09 17:33:22 +00001584 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001585 break;
1586 SourceLocation CommaLoc = ConsumeToken();
1587
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001588 if (Tok.isNot(tok::identifier) &&
1589 !(getLang().C99 || getLang().CPlusPlus0x))
1590 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1591 << getLang().CPlusPlus
1592 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001593 }
1594
1595 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001596 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001597
Mike Stump155750e2009-05-16 07:06:02 +00001598 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001599 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001600
Chris Lattner5261d0c2009-03-28 19:18:32 +00001601 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001602 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001603 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001604 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001605
1606 EnumScope.Exit();
1607 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001608}
1609
1610/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001611/// start of a type-qualifier-list.
1612bool Parser::isTypeQualifier() const {
1613 switch (Tok.getKind()) {
1614 default: return false;
1615 // type-qualifier
1616 case tok::kw_const:
1617 case tok::kw_volatile:
1618 case tok::kw_restrict:
1619 return true;
1620 }
1621}
1622
1623/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001624/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001625bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001626 switch (Tok.getKind()) {
1627 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001628
1629 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001630 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001631 // Annotate typenames and C++ scope specifiers. If we get one, just
1632 // recurse to handle whatever we get.
1633 if (TryAnnotateTypeOrScopeToken())
1634 return isTypeSpecifierQualifier();
1635 // Otherwise, not a type specifier.
1636 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001637
Chris Lattnerb75fde62009-01-04 23:41:41 +00001638 case tok::coloncolon: // ::foo::bar
1639 if (NextToken().is(tok::kw_new) || // ::new
1640 NextToken().is(tok::kw_delete)) // ::delete
1641 return false;
1642
1643 // Annotate typenames and C++ scope specifiers. If we get one, just
1644 // recurse to handle whatever we get.
1645 if (TryAnnotateTypeOrScopeToken())
1646 return isTypeSpecifierQualifier();
1647 // Otherwise, not a type specifier.
1648 return false;
1649
Chris Lattner4b009652007-07-25 00:24:17 +00001650 // GNU attributes support.
1651 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001652 // GNU typeof support.
1653 case tok::kw_typeof:
1654
Chris Lattner4b009652007-07-25 00:24:17 +00001655 // type-specifiers
1656 case tok::kw_short:
1657 case tok::kw_long:
1658 case tok::kw_signed:
1659 case tok::kw_unsigned:
1660 case tok::kw__Complex:
1661 case tok::kw__Imaginary:
1662 case tok::kw_void:
1663 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001664 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001665 case tok::kw_int:
1666 case tok::kw_float:
1667 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001668 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001669 case tok::kw__Bool:
1670 case tok::kw__Decimal32:
1671 case tok::kw__Decimal64:
1672 case tok::kw__Decimal128:
1673
Chris Lattner2e78db32008-04-13 18:59:07 +00001674 // struct-or-union-specifier (C99) or class-specifier (C++)
1675 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001676 case tok::kw_struct:
1677 case tok::kw_union:
1678 // enum-specifier
1679 case tok::kw_enum:
1680
1681 // type-qualifier
1682 case tok::kw_const:
1683 case tok::kw_volatile:
1684 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001685
1686 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001687 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001688 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001689
1690 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1691 case tok::less:
1692 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001693
1694 case tok::kw___cdecl:
1695 case tok::kw___stdcall:
1696 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001697 case tok::kw___w64:
1698 case tok::kw___ptr64:
1699 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001700 }
1701}
1702
1703/// isDeclarationSpecifier() - Return true if the current token is part of a
1704/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001705bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001706 switch (Tok.getKind()) {
1707 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001708
1709 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001710 // Unfortunate hack to support "Class.factoryMethod" notation.
1711 if (getLang().ObjC1 && NextToken().is(tok::period))
1712 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001713 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001714
Douglas Gregord3022602009-03-27 23:10:48 +00001715 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001716 // Annotate typenames and C++ scope specifiers. If we get one, just
1717 // recurse to handle whatever we get.
1718 if (TryAnnotateTypeOrScopeToken())
1719 return isDeclarationSpecifier();
1720 // Otherwise, not a declaration specifier.
1721 return false;
1722 case tok::coloncolon: // ::foo::bar
1723 if (NextToken().is(tok::kw_new) || // ::new
1724 NextToken().is(tok::kw_delete)) // ::delete
1725 return false;
1726
1727 // Annotate typenames and C++ scope specifiers. If we get one, just
1728 // recurse to handle whatever we get.
1729 if (TryAnnotateTypeOrScopeToken())
1730 return isDeclarationSpecifier();
1731 // Otherwise, not a declaration specifier.
1732 return false;
1733
Chris Lattner4b009652007-07-25 00:24:17 +00001734 // storage-class-specifier
1735 case tok::kw_typedef:
1736 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001737 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001738 case tok::kw_static:
1739 case tok::kw_auto:
1740 case tok::kw_register:
1741 case tok::kw___thread:
1742
1743 // type-specifiers
1744 case tok::kw_short:
1745 case tok::kw_long:
1746 case tok::kw_signed:
1747 case tok::kw_unsigned:
1748 case tok::kw__Complex:
1749 case tok::kw__Imaginary:
1750 case tok::kw_void:
1751 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001752 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001753 case tok::kw_int:
1754 case tok::kw_float:
1755 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001756 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001757 case tok::kw__Bool:
1758 case tok::kw__Decimal32:
1759 case tok::kw__Decimal64:
1760 case tok::kw__Decimal128:
1761
Chris Lattner2e78db32008-04-13 18:59:07 +00001762 // struct-or-union-specifier (C99) or class-specifier (C++)
1763 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001764 case tok::kw_struct:
1765 case tok::kw_union:
1766 // enum-specifier
1767 case tok::kw_enum:
1768
1769 // type-qualifier
1770 case tok::kw_const:
1771 case tok::kw_volatile:
1772 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001773
Chris Lattner4b009652007-07-25 00:24:17 +00001774 // function-specifier
1775 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001776 case tok::kw_virtual:
1777 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001778
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001779 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001780 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001781
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001782 // GNU typeof support.
1783 case tok::kw_typeof:
1784
1785 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001786 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001787 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001788
1789 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1790 case tok::less:
1791 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001792
Steve Naroffab1a3632009-01-06 19:34:12 +00001793 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001794 case tok::kw___cdecl:
1795 case tok::kw___stdcall:
1796 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001797 case tok::kw___w64:
1798 case tok::kw___ptr64:
1799 case tok::kw___forceinline:
1800 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001801 }
1802}
1803
1804
1805/// ParseTypeQualifierListOpt
1806/// type-qualifier-list: [C99 6.7.5]
1807/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001808/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001809/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001810/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001811///
Chris Lattner460696f2008-12-18 07:02:59 +00001812void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001813 while (1) {
1814 int isInvalid = false;
1815 const char *PrevSpec = 0;
1816 SourceLocation Loc = Tok.getLocation();
1817
1818 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001819 case tok::kw_const:
1820 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1821 getLang())*2;
1822 break;
1823 case tok::kw_volatile:
1824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1825 getLang())*2;
1826 break;
1827 case tok::kw_restrict:
1828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1829 getLang())*2;
1830 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001831 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001832 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001833 case tok::kw___cdecl:
1834 case tok::kw___stdcall:
1835 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001836 if (AttributesAllowed) {
1837 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1838 continue;
1839 }
1840 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001841 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001842 if (AttributesAllowed) {
1843 DS.AddAttributes(ParseAttributes());
1844 continue; // do *not* consume the next token!
1845 }
1846 // otherwise, FALL THROUGH!
1847 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001848 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001849 // If this is not a type-qualifier token, we're done reading type
1850 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001851 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001852 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001853 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001854
Chris Lattner4b009652007-07-25 00:24:17 +00001855 // If the specifier combination wasn't legal, issue a diagnostic.
1856 if (isInvalid) {
1857 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001858 // Pick between error or extwarn.
1859 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1860 : diag::ext_duplicate_declspec;
1861 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001862 }
1863 ConsumeToken();
1864 }
1865}
1866
1867
1868/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1869///
1870void Parser::ParseDeclarator(Declarator &D) {
1871 /// This implements the 'declarator' production in the C grammar, then checks
1872 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001873 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001874}
1875
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001876/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1877/// is parsed by the function passed to it. Pass null, and the direct-declarator
1878/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001879/// ptr-operator production.
1880///
Sebastian Redl75555032009-01-24 21:16:55 +00001881/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1882/// [C] pointer[opt] direct-declarator
1883/// [C++] direct-declarator
1884/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001885///
1886/// pointer: [C99 6.7.5]
1887/// '*' type-qualifier-list[opt]
1888/// '*' type-qualifier-list[opt] pointer
1889///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001890/// ptr-operator:
1891/// '*' cv-qualifier-seq[opt]
1892/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001893/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001894/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001895/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001896/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001897void Parser::ParseDeclaratorInternal(Declarator &D,
1898 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001899
Sebastian Redl75555032009-01-24 21:16:55 +00001900 // C++ member pointers start with a '::' or a nested-name.
1901 // Member pointers get special handling, since there's no place for the
1902 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001903 if (getLang().CPlusPlus &&
1904 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1905 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001906 CXXScopeSpec SS;
1907 if (ParseOptionalCXXScopeSpecifier(SS)) {
1908 if(Tok.isNot(tok::star)) {
1909 // The scope spec really belongs to the direct-declarator.
1910 D.getCXXScopeSpec() = SS;
1911 if (DirectDeclParser)
1912 (this->*DirectDeclParser)(D);
1913 return;
1914 }
1915
1916 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001917 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001918 DeclSpec DS;
1919 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001920 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001921
1922 // Recurse to parse whatever is left.
1923 ParseDeclaratorInternal(D, DirectDeclParser);
1924
1925 // Sema will have to catch (syntactically invalid) pointers into global
1926 // scope. It has to catch pointers into namespace scope anyway.
1927 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001928 Loc, DS.TakeAttributes()),
1929 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001930 return;
1931 }
1932 }
1933
1934 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001935 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001936 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001937 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001938 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001939 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001940 if (DirectDeclParser)
1941 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001942 return;
1943 }
Sebastian Redl75555032009-01-24 21:16:55 +00001944
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001945 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1946 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001947 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001948 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001949
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001950 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001951 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001952 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001953
Chris Lattner4b009652007-07-25 00:24:17 +00001954 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001955 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001956
Chris Lattner4b009652007-07-25 00:24:17 +00001957 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001958 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001959 if (Kind == tok::star)
1960 // Remember that we parsed a pointer type, and remember the type-quals.
1961 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001962 DS.TakeAttributes()),
1963 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001964 else
1965 // Remember that we parsed a Block type, and remember the type-quals.
1966 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001967 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001968 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001969 } else {
1970 // Is a reference
1971 DeclSpec DS;
1972
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001973 // Complain about rvalue references in C++03, but then go on and build
1974 // the declarator.
1975 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1976 Diag(Loc, diag::err_rvalue_reference);
1977
Chris Lattner4b009652007-07-25 00:24:17 +00001978 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1979 // cv-qualifiers are introduced through the use of a typedef or of a
1980 // template type argument, in which case the cv-qualifiers are ignored.
1981 //
1982 // [GNU] Retricted references are allowed.
1983 // [GNU] Attributes on references are allowed.
1984 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001985 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001986
1987 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1988 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1989 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001990 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001991 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1992 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001993 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001994 }
1995
1996 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001997 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001998
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001999 if (D.getNumTypeObjects() > 0) {
2000 // C++ [dcl.ref]p4: There shall be no references to references.
2001 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2002 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002003 if (const IdentifierInfo *II = D.getIdentifier())
2004 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2005 << II;
2006 else
2007 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2008 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002009
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002010 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002011 // can go ahead and build the (technically ill-formed)
2012 // declarator: reference collapsing will take care of it.
2013 }
2014 }
2015
Chris Lattner4b009652007-07-25 00:24:17 +00002016 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002017 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002018 DS.TakeAttributes(),
2019 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002020 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002021 }
2022}
2023
2024/// ParseDirectDeclarator
2025/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002026/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002027/// '(' declarator ')'
2028/// [GNU] '(' attributes declarator ')'
2029/// [C90] direct-declarator '[' constant-expression[opt] ']'
2030/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2031/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2032/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2033/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2034/// direct-declarator '(' parameter-type-list ')'
2035/// direct-declarator '(' identifier-list[opt] ')'
2036/// [GNU] direct-declarator '(' parameter-forward-declarations
2037/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002038/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2039/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002040/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002041///
2042/// declarator-id: [C++ 8]
2043/// id-expression
2044/// '::'[opt] nested-name-specifier[opt] type-name
2045///
2046/// id-expression: [C++ 5.1]
2047/// unqualified-id
2048/// qualified-id [TODO]
2049///
2050/// unqualified-id: [C++ 5.1]
2051/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002052/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002053/// conversion-function-id [TODO]
2054/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002055/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002056///
Chris Lattner4b009652007-07-25 00:24:17 +00002057void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002058 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002059
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002060 if (getLang().CPlusPlus) {
2061 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002062 // ParseDeclaratorInternal might already have parsed the scope.
2063 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2064 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002065 if (afterCXXScope) {
2066 // Change the declaration context for name lookup, until this function
2067 // is exited (and the declarator has been parsed).
2068 DeclScopeObj.EnterDeclaratorScope();
2069 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002070
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002071 if (Tok.is(tok::identifier)) {
2072 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002073
2074 // If this identifier is the name of the current class, it's a
2075 // constructor name.
2076 if (!D.getDeclSpec().hasTypeSpecifier() &&
2077 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2078 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2079 Tok.getLocation(), CurScope),
2080 Tok.getLocation());
2081 // This is a normal identifier.
2082 } else
2083 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002084 ConsumeToken();
2085 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002086 } else if (Tok.is(tok::annot_template_id)) {
2087 TemplateIdAnnotation *TemplateId
2088 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2089
2090 // FIXME: Could this template-id name a constructor?
2091
2092 // FIXME: This is an egregious hack, where we silently ignore
2093 // the specialization (which should be a function template
2094 // specialization name) and use the name instead. This hack
2095 // will go away when we have support for function
2096 // specializations.
2097 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2098 TemplateId->Destroy();
2099 ConsumeToken();
2100 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002101 } else if (Tok.is(tok::kw_operator)) {
2102 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002103 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002104
Douglas Gregor853dd392008-12-26 15:00:45 +00002105 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002106 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2107 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002108 } else {
2109 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002110 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2111 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2112 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002113 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002114 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002115 }
2116 goto PastIdentifier;
2117 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002118 // This should be a C++ destructor.
2119 SourceLocation TildeLoc = ConsumeToken();
2120 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002121 // FIXME: Inaccurate.
2122 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002123 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002124 TypeResult Type = ParseClassName(EndLoc);
2125 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002126 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002127 else
2128 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002129 } else {
2130 Diag(Tok, diag::err_expected_class_name);
2131 D.SetIdentifier(0, TildeLoc);
2132 }
2133 goto PastIdentifier;
2134 }
2135
2136 // If we reached this point, token is not identifier and not '~'.
2137
2138 if (afterCXXScope) {
2139 Diag(Tok, diag::err_expected_unqualified_id);
2140 D.SetIdentifier(0, Tok.getLocation());
2141 D.setInvalidType(true);
2142 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002143 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002144 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002145 }
2146
2147 // If we reached this point, we are either in C/ObjC or the token didn't
2148 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002149 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2150 assert(!getLang().CPlusPlus &&
2151 "There's a C++-specific check for tok::identifier above");
2152 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2153 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2154 ConsumeToken();
2155 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002156 // direct-declarator: '(' declarator ')'
2157 // direct-declarator: '(' attributes declarator ')'
2158 // Example: 'char (*X)' or 'int (*XX)(void)'
2159 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002160 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002161 // This could be something simple like "int" (in which case the declarator
2162 // portion is empty), if an abstract-declarator is allowed.
2163 D.SetIdentifier(0, Tok.getLocation());
2164 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002165 if (D.getContext() == Declarator::MemberContext)
2166 Diag(Tok, diag::err_expected_member_name_or_semi)
2167 << D.getDeclSpec().getSourceRange();
2168 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002169 Diag(Tok, diag::err_expected_unqualified_id);
2170 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002171 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002172 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002173 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002174 }
2175
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002176 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002177 assert(D.isPastIdentifier() &&
2178 "Haven't past the location of the identifier yet?");
2179
2180 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002181 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002182 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2183 // In such a case, check if we actually have a function declarator; if it
2184 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002185 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2186 // When not in file scope, warn for ambiguous function declarators, just
2187 // in case the author intended it as a variable definition.
2188 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2189 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2190 break;
2191 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002192 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002193 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002194 ParseBracketDeclarator(D);
2195 } else {
2196 break;
2197 }
2198 }
2199}
2200
Chris Lattnera0d056d2008-04-06 05:45:57 +00002201/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2202/// only called before the identifier, so these are most likely just grouping
2203/// parens for precedence. If we find that these are actually function
2204/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2205///
2206/// direct-declarator:
2207/// '(' declarator ')'
2208/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002209/// direct-declarator '(' parameter-type-list ')'
2210/// direct-declarator '(' identifier-list[opt] ')'
2211/// [GNU] direct-declarator '(' parameter-forward-declarations
2212/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002213///
2214void Parser::ParseParenDeclarator(Declarator &D) {
2215 SourceLocation StartLoc = ConsumeParen();
2216 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2217
Chris Lattner1f185292008-10-20 02:05:46 +00002218 // Eat any attributes before we look at whether this is a grouping or function
2219 // declarator paren. If this is a grouping paren, the attribute applies to
2220 // the type being built up, for example:
2221 // int (__attribute__(()) *x)(long y)
2222 // If this ends up not being a grouping paren, the attribute applies to the
2223 // first argument, for example:
2224 // int (__attribute__(()) int x)
2225 // In either case, we need to eat any attributes to be able to determine what
2226 // sort of paren this is.
2227 //
2228 AttributeList *AttrList = 0;
2229 bool RequiresArg = false;
2230 if (Tok.is(tok::kw___attribute)) {
2231 AttrList = ParseAttributes();
2232
2233 // We require that the argument list (if this is a non-grouping paren) be
2234 // present even if the attribute list was empty.
2235 RequiresArg = true;
2236 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002237 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002238 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2239 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2240 Tok.is(tok::kw___ptr64)) {
2241 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2242 }
Chris Lattner1f185292008-10-20 02:05:46 +00002243
Chris Lattnera0d056d2008-04-06 05:45:57 +00002244 // If we haven't past the identifier yet (or where the identifier would be
2245 // stored, if this is an abstract declarator), then this is probably just
2246 // grouping parens. However, if this could be an abstract-declarator, then
2247 // this could also be the start of function arguments (consider 'void()').
2248 bool isGrouping;
2249
2250 if (!D.mayOmitIdentifier()) {
2251 // If this can't be an abstract-declarator, this *must* be a grouping
2252 // paren, because we haven't seen the identifier yet.
2253 isGrouping = true;
2254 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002255 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002256 isDeclarationSpecifier()) { // 'int(int)' is a function.
2257 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2258 // considered to be a type, not a K&R identifier-list.
2259 isGrouping = false;
2260 } else {
2261 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2262 isGrouping = true;
2263 }
2264
2265 // If this is a grouping paren, handle:
2266 // direct-declarator: '(' declarator ')'
2267 // direct-declarator: '(' attributes declarator ')'
2268 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002269 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002270 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002271 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002272 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002273
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002274 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002275 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002276 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002277
2278 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002279 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002280 return;
2281 }
2282
2283 // Okay, if this wasn't a grouping paren, it must be the start of a function
2284 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002285 // identifier (and remember where it would have been), then call into
2286 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002287 D.SetIdentifier(0, Tok.getLocation());
2288
Chris Lattner1f185292008-10-20 02:05:46 +00002289 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002290}
2291
2292/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2293/// declarator D up to a paren, which indicates that we are parsing function
2294/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002295///
Chris Lattner1f185292008-10-20 02:05:46 +00002296/// If AttrList is non-null, then the caller parsed those arguments immediately
2297/// after the open paren - they should be considered to be the first argument of
2298/// a parameter. If RequiresArg is true, then the first argument of the
2299/// function is required to be present and required to not be an identifier
2300/// list.
2301///
Chris Lattner4b009652007-07-25 00:24:17 +00002302/// This method also handles this portion of the grammar:
2303/// parameter-type-list: [C99 6.7.5]
2304/// parameter-list
2305/// parameter-list ',' '...'
2306///
2307/// parameter-list: [C99 6.7.5]
2308/// parameter-declaration
2309/// parameter-list ',' parameter-declaration
2310///
2311/// parameter-declaration: [C99 6.7.5]
2312/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002313/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002314/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002315/// declaration-specifiers abstract-declarator[opt]
2316/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002317/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002318/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2319///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002320/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002321/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002322///
Chris Lattner1f185292008-10-20 02:05:46 +00002323void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2324 AttributeList *AttrList,
2325 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002326 // lparen is already consumed!
2327 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002328
Chris Lattner1f185292008-10-20 02:05:46 +00002329 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002330 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002331 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002332 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002333 delete AttrList;
2334 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002335
Sebastian Redl0c986032009-02-09 18:23:29 +00002336 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002337
2338 // cv-qualifier-seq[opt].
2339 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002340 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002341 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002342 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002343 llvm::SmallVector<TypeTy*, 2> Exceptions;
2344 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002345 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002346 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002347 if (!DS.getSourceRange().getEnd().isInvalid())
2348 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002349
2350 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002351 if (Tok.is(tok::kw_throw)) {
2352 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002353 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002354 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2355 hasAnyExceptionSpec);
2356 assert(Exceptions.size() == ExceptionRanges.size() &&
2357 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002358 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002359 }
2360
Chris Lattner9f7564b2008-04-06 06:57:35 +00002361 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002362 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002363 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002364 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002365 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002366 /*arglist*/ 0, 0,
2367 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002368 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002369 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002370 Exceptions.data(),
2371 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002372 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002373 LParenLoc, D),
2374 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002375 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002376 }
2377
Chris Lattner1f185292008-10-20 02:05:46 +00002378 // Alternatively, this parameter list may be an identifier list form for a
2379 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002380 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002381 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002382 // K&R identifier lists can't have typedefs as identifiers, per
2383 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002384 if (RequiresArg) {
2385 Diag(Tok, diag::err_argument_required_after_attribute);
2386 delete AttrList;
2387 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002388 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2389 // normal declarators, not for abstract-declarators.
2390 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002391 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002392 }
2393
2394 // Finally, a normal, non-empty parameter type list.
2395
2396 // Build up an array of information about the parsed arguments.
2397 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002398
2399 // Enter function-declaration scope, limiting any declarators to the
2400 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002401 ParseScope PrototypeScope(this,
2402 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002403
2404 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002405 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002406 while (1) {
2407 if (Tok.is(tok::ellipsis)) {
2408 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002409 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002410 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002411 }
2412
Chris Lattner9f7564b2008-04-06 06:57:35 +00002413 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002414
Chris Lattner9f7564b2008-04-06 06:57:35 +00002415 // Parse the declaration-specifiers.
2416 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002417
2418 // If the caller parsed attributes for the first argument, add them now.
2419 if (AttrList) {
2420 DS.AddAttributes(AttrList);
2421 AttrList = 0; // Only apply the attributes to the first parameter.
2422 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002423 ParseDeclarationSpecifiers(DS);
2424
Chris Lattner9f7564b2008-04-06 06:57:35 +00002425 // Parse the declarator. This is "PrototypeContext", because we must
2426 // accept either 'declarator' or 'abstract-declarator' here.
2427 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2428 ParseDeclarator(ParmDecl);
2429
2430 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002431 if (Tok.is(tok::kw___attribute)) {
2432 SourceLocation Loc;
2433 AttributeList *AttrList = ParseAttributes(&Loc);
2434 ParmDecl.AddAttributes(AttrList, Loc);
2435 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002436
Chris Lattner9f7564b2008-04-06 06:57:35 +00002437 // Remember this parsed parameter in ParamInfo.
2438 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2439
Douglas Gregor605de8d2008-12-16 21:30:33 +00002440 // DefArgToks is used when the parsing of default arguments needs
2441 // to be delayed.
2442 CachedTokens *DefArgToks = 0;
2443
Chris Lattner9f7564b2008-04-06 06:57:35 +00002444 // If no parameter was specified, verify that *something* was specified,
2445 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002446 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2447 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002448 // Completely missing, emit error.
2449 Diag(DSStart, diag::err_missing_param);
2450 } else {
2451 // Otherwise, we have something. Add it and let semantic analysis try
2452 // to grok it and add the result to the ParamInfo we are building.
2453
2454 // Inform the actions module about the parameter declarator, so it gets
2455 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002456 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002457
2458 // Parse the default argument, if any. We parse the default
2459 // arguments in all dialects; the semantic analysis in
2460 // ActOnParamDefaultArgument will reject the default argument in
2461 // C.
2462 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002463 SourceLocation EqualLoc = Tok.getLocation();
2464
Chris Lattner3e254fb2008-04-08 04:40:51 +00002465 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002466 if (D.getContext() == Declarator::MemberContext) {
2467 // If we're inside a class definition, cache the tokens
2468 // corresponding to the default argument. We'll actually parse
2469 // them when we see the end of the class definition.
2470 // FIXME: Templates will require something similar.
2471 // FIXME: Can we use a smart pointer for Toks?
2472 DefArgToks = new CachedTokens;
2473
2474 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2475 tok::semi, false)) {
2476 delete DefArgToks;
2477 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002478 Actions.ActOnParamDefaultArgumentError(Param);
2479 } else
Anders Carlssona116e6e2009-06-12 16:51:40 +00002480 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2481 (*DefArgToks)[1].getLocation());
Chris Lattner3e254fb2008-04-08 04:40:51 +00002482 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002483 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002484 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002485
2486 OwningExprResult DefArgResult(ParseAssignmentExpression());
2487 if (DefArgResult.isInvalid()) {
2488 Actions.ActOnParamDefaultArgumentError(Param);
2489 SkipUntil(tok::comma, tok::r_paren, true, true);
2490 } else {
2491 // Inform the actions module about the default argument
2492 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002493 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002494 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002495 }
2496 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002497
2498 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002499 ParmDecl.getIdentifierLoc(), Param,
2500 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002501 }
2502
2503 // If the next token is a comma, consume it and keep reading arguments.
2504 if (Tok.isNot(tok::comma)) break;
2505
2506 // Consume the comma.
2507 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002508 }
2509
Chris Lattner9f7564b2008-04-06 06:57:35 +00002510 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002511 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002512
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002513 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002514 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002515
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002516 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002517 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002518 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002519 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002520 llvm::SmallVector<TypeTy*, 2> Exceptions;
2521 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002522 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002523 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002524 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002525 if (!DS.getSourceRange().getEnd().isInvalid())
2526 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002527
2528 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002529 if (Tok.is(tok::kw_throw)) {
2530 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002531 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002532 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2533 hasAnyExceptionSpec);
2534 assert(Exceptions.size() == ExceptionRanges.size() &&
2535 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002536 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002537 }
2538
Chris Lattner4b009652007-07-25 00:24:17 +00002539 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002540 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002541 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002542 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002543 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002544 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002545 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002546 Exceptions.data(),
2547 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002548 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002549 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002550}
2551
Chris Lattner35d9c912008-04-06 06:34:08 +00002552/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2553/// we found a K&R-style identifier list instead of a type argument list. The
2554/// current token is known to be the first identifier in the list.
2555///
2556/// identifier-list: [C99 6.7.5]
2557/// identifier
2558/// identifier-list ',' identifier
2559///
2560void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2561 Declarator &D) {
2562 // Build up an array of information about the parsed arguments.
2563 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2564 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2565
2566 // If there was no identifier specified for the declarator, either we are in
2567 // an abstract-declarator, or we are in a parameter declarator which was found
2568 // to be abstract. In abstract-declarators, identifier lists are not valid:
2569 // diagnose this.
2570 if (!D.getIdentifier())
2571 Diag(Tok, diag::ext_ident_list_in_param);
2572
2573 // Tok is known to be the first identifier in the list. Remember this
2574 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002575 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002576 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002577 Tok.getLocation(),
2578 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002579
Chris Lattner113a56b2008-04-06 06:39:19 +00002580 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002581
2582 while (Tok.is(tok::comma)) {
2583 // Eat the comma.
2584 ConsumeToken();
2585
Chris Lattner113a56b2008-04-06 06:39:19 +00002586 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002587 if (Tok.isNot(tok::identifier)) {
2588 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002589 SkipUntil(tok::r_paren);
2590 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002591 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002592
Chris Lattner35d9c912008-04-06 06:34:08 +00002593 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002594
2595 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002596 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002597 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002598
2599 // Verify that the argument identifier has not already been mentioned.
2600 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002601 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002602 } else {
2603 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002604 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002605 Tok.getLocation(),
2606 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002607 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002608
2609 // Eat the identifier.
2610 ConsumeToken();
2611 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002612
2613 // If we have the closing ')', eat it and we're done.
2614 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2615
Chris Lattner113a56b2008-04-06 06:39:19 +00002616 // Remember that we parsed a function type, and remember the attributes. This
2617 // function type is always a K&R style function type, which is not varargs and
2618 // has no prototype.
2619 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002620 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002621 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002622 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002623 /*exception*/false,
2624 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002625 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002626 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002627}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002628
Chris Lattner4b009652007-07-25 00:24:17 +00002629/// [C90] direct-declarator '[' constant-expression[opt] ']'
2630/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2631/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2632/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2633/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2634void Parser::ParseBracketDeclarator(Declarator &D) {
2635 SourceLocation StartLoc = ConsumeBracket();
2636
Chris Lattner1525c3a2008-12-18 07:27:21 +00002637 // C array syntax has many features, but by-far the most common is [] and [4].
2638 // This code does a fast path to handle some of the most obvious cases.
2639 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002640 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002641 // Remember that we parsed the empty array type.
2642 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002643 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2644 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002645 return;
2646 } else if (Tok.getKind() == tok::numeric_constant &&
2647 GetLookAheadToken(1).is(tok::r_square)) {
2648 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002649 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002650 ConsumeToken();
2651
Sebastian Redl0c986032009-02-09 18:23:29 +00002652 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002653
2654 // If there was an error parsing the assignment-expression, recover.
2655 if (ExprRes.isInvalid())
2656 ExprRes.release(); // Deallocate expr, just use [].
2657
2658 // Remember that we parsed a array type, and remember its features.
2659 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002660 ExprRes.release(), StartLoc),
2661 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002662 return;
2663 }
2664
Chris Lattner4b009652007-07-25 00:24:17 +00002665 // If valid, this location is the position where we read the 'static' keyword.
2666 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002667 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002668 StaticLoc = ConsumeToken();
2669
2670 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002671 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002672 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002673 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002674
2675 // If we haven't already read 'static', check to see if there is one after the
2676 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002677 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002678 StaticLoc = ConsumeToken();
2679
2680 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2681 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002682 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002683
2684 // Handle the case where we have '[*]' as the array size. However, a leading
2685 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2686 // the the token after the star is a ']'. Since stars in arrays are
2687 // infrequent, use of lookahead is not costly here.
2688 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002689 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002690
Chris Lattner306d4df2008-12-18 06:50:14 +00002691 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002692 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002693 StaticLoc = SourceLocation(); // Drop the static.
2694 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002695 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002696 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002697 // Note, in C89, this production uses the constant-expr production instead
2698 // of assignment-expr. The only difference is that assignment-expr allows
2699 // things like '=' and '*='. Sema rejects these in C89 mode because they
2700 // are not i-c-e's, so we don't need to distinguish between the two here.
2701
Douglas Gregor98189262009-06-19 23:52:42 +00002702 // Parse the constant-expression or assignment-expression now (depending
2703 // on dialect).
2704 if (getLang().CPlusPlus)
2705 NumElements = ParseConstantExpression();
2706 else
2707 NumElements = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +00002708 }
2709
2710 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002711 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002712 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002713 // If the expression was invalid, skip it.
2714 SkipUntil(tok::r_square);
2715 return;
2716 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002717
2718 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2719
Chris Lattner1525c3a2008-12-18 07:27:21 +00002720 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002721 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2722 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002723 NumElements.release(), StartLoc),
2724 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002725}
2726
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002727/// [GNU] typeof-specifier:
2728/// typeof ( expressions )
2729/// typeof ( type-name )
2730/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002731///
2732void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002733 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002734 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002735 SourceLocation StartLoc = ConsumeToken();
2736
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002737 bool isCastExpr;
2738 TypeTy *CastTy;
2739 SourceRange CastRange;
2740 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2741 isCastExpr,
2742 CastTy,
2743 CastRange);
2744
2745 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002746 // FIXME: Not accurate, the range gets one token more than it should.
2747 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002748 else
2749 DS.SetRangeEnd(CastRange.getEnd());
2750
2751 if (isCastExpr) {
2752 if (!CastTy) {
2753 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002754 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002755 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002756
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002757 const char *PrevSpec = 0;
2758 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2759 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2760 CastTy))
2761 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2762 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002763 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002764
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002765 // If we get here, the operand to the typeof was an expresion.
2766 if (Operand.isInvalid()) {
2767 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002768 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002769 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002770
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002771 const char *PrevSpec = 0;
2772 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2773 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2774 Operand.release()))
2775 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002776}