blob: 9a8e34211d02b13e35fe56064edbcd07047f97c0 [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 Gregor2ae1d772009-06-23 23:11:28 +0000374Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
375 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000376 // If a simple-asm-expr is present, parse it.
377 if (Tok.is(tok::kw_asm)) {
378 SourceLocation Loc;
379 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
380 if (AsmLabel.isInvalid()) {
381 SkipUntil(tok::semi, true, true);
382 return DeclPtrTy();
383 }
384
385 D.setAsmLabel(AsmLabel.release());
386 D.SetRangeEnd(Loc);
387 }
388
389 // If attributes are present, parse them.
390 if (Tok.is(tok::kw___attribute)) {
391 SourceLocation Loc;
392 AttributeList *AttrList = ParseAttributes(&Loc);
393 D.AddAttributes(AttrList, Loc);
394 }
395
396 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000397 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
398 Actions.ActOnTemplateDeclarator(CurScope,
399 Action::MultiTemplateParamsArg(Actions,
400 TemplateInfo.TemplateParams->data(),
401 TemplateInfo.TemplateParams->size()),
402 D)
403 : Actions.ActOnDeclarator(CurScope, D);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000404
405 // Parse declarator '=' initializer.
406 if (Tok.is(tok::equal)) {
407 ConsumeToken();
408 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
409 SourceLocation DelLoc = ConsumeToken();
410 Actions.SetDeclDeleted(ThisDecl, DelLoc);
411 } else {
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000412 if (getLang().CPlusPlus)
413 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
414
Douglas Gregore3298aa2009-05-12 21:31:51 +0000415 OwningExprResult Init(ParseInitializer());
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000416
417 if (getLang().CPlusPlus)
418 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
419
Douglas Gregore3298aa2009-05-12 21:31:51 +0000420 if (Init.isInvalid()) {
421 SkipUntil(tok::semi, true, true);
422 return DeclPtrTy();
423 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000424 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000425 }
426 } else if (Tok.is(tok::l_paren)) {
427 // Parse C++ direct initializer: '(' expression-list ')'
428 SourceLocation LParenLoc = ConsumeParen();
429 ExprVector Exprs(Actions);
430 CommaLocsTy CommaLocs;
431
432 if (ParseExpressionList(Exprs, CommaLocs)) {
433 SkipUntil(tok::r_paren);
434 } else {
435 // Match the ')'.
436 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
437
438 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
439 "Unexpected number of commas!");
440 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
441 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000442 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000443 }
444 } else {
445 Actions.ActOnUninitializedDecl(ThisDecl);
446 }
447
448 return ThisDecl;
449}
450
451/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
452/// parsing 'declaration-specifiers declarator'. This method is split out this
453/// way to handle the ambiguity between top-level function-definitions and
454/// declarations.
455///
456/// init-declarator-list: [C99 6.7]
457/// init-declarator
458/// init-declarator-list ',' init-declarator
459///
460/// According to the standard grammar, =default and =delete are function
461/// definitions, but that definitely doesn't fit with the parser here.
462///
Chris Lattnera17991f2009-03-29 16:50:03 +0000463Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000464ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000465 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
466 // that we parse together here.
467 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000468
469 // At this point, we know that it is not a function definition. Parse the
470 // rest of the init-declarator-list.
471 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000472 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
473 if (ThisDecl.get())
474 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000475
Chris Lattner4b009652007-07-25 00:24:17 +0000476 // If we don't have a comma, it is either the end of the list (a ';') or an
477 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000478 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000479 break;
480
481 // Consume the comma.
482 ConsumeToken();
483
484 // Parse the next declarator.
485 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000486
487 // Accept attributes in an init-declarator. In the first declarator in a
488 // declaration, these would be part of the declspec. In subsequent
489 // declarators, they become part of the declarator itself, so that they
490 // don't apply to declarators after *this* one. Examples:
491 // short __attribute__((common)) var; -> declspec
492 // short var __attribute__((common)); -> declarator
493 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000494 if (Tok.is(tok::kw___attribute)) {
495 SourceLocation Loc;
496 AttributeList *AttrList = ParseAttributes(&Loc);
497 D.AddAttributes(AttrList, Loc);
498 }
Chris Lattner926cf542008-10-20 04:57:38 +0000499
Chris Lattner4b009652007-07-25 00:24:17 +0000500 ParseDeclarator(D);
501 }
502
Eli Friedman4d57af22009-05-29 01:49:24 +0000503 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
504 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000505 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000506}
507
508/// ParseSpecifierQualifierList
509/// specifier-qualifier-list:
510/// type-specifier specifier-qualifier-list[opt]
511/// type-qualifier specifier-qualifier-list[opt]
512/// [GNU] attributes specifier-qualifier-list[opt]
513///
514void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
515 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
516 /// parse declaration-specifiers and complain about extra stuff.
517 ParseDeclarationSpecifiers(DS);
518
519 // Validate declspec for type-name.
520 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000521 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
522 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000523 Diag(Tok, diag::err_typename_requires_specqual);
524
525 // Issue diagnostic and remove storage class if present.
526 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
527 if (DS.getStorageClassSpecLoc().isValid())
528 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
529 else
530 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
531 DS.ClearStorageClassSpecs();
532 }
533
534 // Issue diagnostic and remove function specfier if present.
535 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000536 if (DS.isInlineSpecified())
537 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
538 if (DS.isVirtualSpecified())
539 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
540 if (DS.isExplicitSpecified())
541 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000542 DS.ClearFunctionSpecs();
543 }
544}
545
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000546/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
547/// specified token is valid after the identifier in a declarator which
548/// immediately follows the declspec. For example, these things are valid:
549///
550/// int x [ 4]; // direct-declarator
551/// int x ( int y); // direct-declarator
552/// int(int x ) // direct-declarator
553/// int x ; // simple-declaration
554/// int x = 17; // init-declarator-list
555/// int x , y; // init-declarator-list
556/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000557/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000558/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000559///
560/// This is not, because 'x' does not immediately follow the declspec (though
561/// ')' happens to be valid anyway).
562/// int (x)
563///
564static bool isValidAfterIdentifierInDeclarator(const Token &T) {
565 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
566 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000567 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000568}
569
Chris Lattner82353c62009-04-14 21:34:55 +0000570
571/// ParseImplicitInt - This method is called when we have an non-typename
572/// identifier in a declspec (which normally terminates the decl spec) when
573/// the declspec has no type specifier. In this case, the declspec is either
574/// malformed or is "implicit int" (in K&R and C89).
575///
576/// This method handles diagnosing this prettily and returns false if the
577/// declspec is done being processed. If it recovers and thinks there may be
578/// other pieces of declspec after it, it returns true.
579///
Chris Lattner52cd7622009-04-14 22:17:06 +0000580bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000581 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000582 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000583 assert(Tok.is(tok::identifier) && "should have identifier");
584
Chris Lattner82353c62009-04-14 21:34:55 +0000585 SourceLocation Loc = Tok.getLocation();
586 // If we see an identifier that is not a type name, we normally would
587 // parse it as the identifer being declared. However, when a typename
588 // is typo'd or the definition is not included, this will incorrectly
589 // parse the typename as the identifier name and fall over misparsing
590 // later parts of the diagnostic.
591 //
592 // As such, we try to do some look-ahead in cases where this would
593 // otherwise be an "implicit-int" case to see if this is invalid. For
594 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
595 // an identifier with implicit int, we'd get a parse error because the
596 // next token is obviously invalid for a type. Parse these as a case
597 // with an invalid type specifier.
598 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
599
600 // Since we know that this either implicit int (which is rare) or an
601 // error, we'd do lookahead to try to do better recovery.
602 if (isValidAfterIdentifierInDeclarator(NextToken())) {
603 // If this token is valid for implicit int, e.g. "static x = 4", then
604 // we just avoid eating the identifier, so it will be parsed as the
605 // identifier in the declarator.
606 return false;
607 }
608
609 // Otherwise, if we don't consume this token, we are going to emit an
610 // error anyway. Try to recover from various common problems. Check
611 // to see if this was a reference to a tag name without a tag specified.
612 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000613 //
614 // C++ doesn't need this, and isTagName doesn't take SS.
615 if (SS == 0) {
616 const char *TagName = 0;
617 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000618
Chris Lattner82353c62009-04-14 21:34:55 +0000619 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
620 default: break;
621 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
622 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
623 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
624 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
625 }
Chris Lattner82353c62009-04-14 21:34:55 +0000626
Chris Lattner52cd7622009-04-14 22:17:06 +0000627 if (TagName) {
628 Diag(Loc, diag::err_use_of_tag_name_without_tag)
629 << Tok.getIdentifierInfo() << TagName
630 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
631
632 // Parse this as a tag as if the missing tag were present.
633 if (TagKind == tok::kw_enum)
634 ParseEnumSpecifier(Loc, DS, AS);
635 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000636 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000637 return true;
638 }
Chris Lattner82353c62009-04-14 21:34:55 +0000639 }
640
641 // Since this is almost certainly an invalid type name, emit a
642 // diagnostic that says it, eat the token, and mark the declspec as
643 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000644 SourceRange R;
645 if (SS) R = SS->getRange();
646
647 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000648 const char *PrevSpec;
649 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
650 DS.SetRangeEnd(Tok.getLocation());
651 ConsumeToken();
652
653 // TODO: Could inject an invalid typedef decl in an enclosing scope to
654 // avoid rippling error messages on subsequent uses of the same type,
655 // could be useful if #include was forgotten.
656 return false;
657}
658
Chris Lattner4b009652007-07-25 00:24:17 +0000659/// ParseDeclarationSpecifiers
660/// declaration-specifiers: [C99 6.7]
661/// storage-class-specifier declaration-specifiers[opt]
662/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000663/// [C99] function-specifier declaration-specifiers[opt]
664/// [GNU] attributes declaration-specifiers[opt]
665///
666/// storage-class-specifier: [C99 6.7.1]
667/// 'typedef'
668/// 'extern'
669/// 'static'
670/// 'auto'
671/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000672/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000673/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000674/// function-specifier: [C99 6.7.4]
675/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000676/// [C++] 'virtual'
677/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000678/// 'friend': [C++ dcl.friend]
679
Chris Lattner4b009652007-07-25 00:24:17 +0000680///
Douglas Gregor52473432008-12-24 02:52:09 +0000681void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000682 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000683 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000684 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000685 while (1) {
686 int isInvalid = false;
687 const char *PrevSpec = 0;
688 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000689
Chris Lattner4b009652007-07-25 00:24:17 +0000690 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000691 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000692 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000693 // If this is not a declaration specifier token, we're done reading decl
694 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000695 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000696 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000697
698 case tok::coloncolon: // ::foo::bar
699 // Annotate C++ scope specifiers. If we get one, loop.
700 if (TryAnnotateCXXScopeToken())
701 continue;
702 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000703
704 case tok::annot_cxxscope: {
705 if (DS.hasTypeSpecifier())
706 goto DoneWithDeclSpec;
707
708 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000709 Token Next = NextToken();
710 if (Next.is(tok::annot_template_id) &&
711 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000712 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000713 // We have a qualified template-id, e.g., N::A<int>
714 CXXScopeSpec SS;
715 ParseOptionalCXXScopeSpecifier(SS);
716 assert(Tok.is(tok::annot_template_id) &&
717 "ParseOptionalCXXScopeSpecifier not working");
718 AnnotateTemplateIdTokenAsType(&SS);
719 continue;
720 }
721
722 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000723 goto DoneWithDeclSpec;
724
725 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000726 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000727 SS.setRange(Tok.getAnnotationRange());
728
729 // If the next token is the name of the class type that the C++ scope
730 // denotes, followed by a '(', then this is a constructor declaration.
731 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000732 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000733 CurScope, &SS) &&
734 GetLookAheadToken(2).is(tok::l_paren))
735 goto DoneWithDeclSpec;
736
Douglas Gregor1075a162009-02-04 17:00:24 +0000737 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
738 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000739
Chris Lattner52cd7622009-04-14 22:17:06 +0000740 // If the referenced identifier is not a type, then this declspec is
741 // erroneous: We already checked about that it has no type specifier, and
742 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
743 // typename.
744 if (TypeRep == 0) {
745 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000746 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000747 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000748 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000749
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000750 ConsumeToken(); // The C++ scope.
751
Douglas Gregora60c62e2009-02-09 15:09:02 +0000752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000753 TypeRep);
754 if (isInvalid)
755 break;
756
757 DS.SetRangeEnd(Tok.getLocation());
758 ConsumeToken(); // The typename.
759
760 continue;
761 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000762
763 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000764 if (Tok.getAnnotationValue())
765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
766 Tok.getAnnotationValue());
767 else
768 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000769 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
770 ConsumeToken(); // The typename
771
772 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
773 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
774 // Objective-C interface. If we don't have Objective-C or a '<', this is
775 // just a normal reference to a typedef name.
776 if (!Tok.is(tok::less) || !getLang().ObjC1)
777 continue;
778
779 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000780 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000781 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
782 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
783
784 DS.SetRangeEnd(EndProtoLoc);
785 continue;
786 }
787
Chris Lattnerfda18db2008-07-26 01:18:38 +0000788 // typedef-name
789 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000790 // In C++, check to see if this is a scope specifier like foo::bar::, if
791 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000792 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
793 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000794
Chris Lattnerfda18db2008-07-26 01:18:38 +0000795 // This identifier can only be a typedef name if we haven't already seen
796 // a type-specifier. Without this check we misparse:
797 // typedef int X; struct Y { short X; }; as 'short int'.
798 if (DS.hasTypeSpecifier())
799 goto DoneWithDeclSpec;
800
801 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000802 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
803 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000804
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000805 // If this is not a typedef name, don't parse it as part of the declspec,
806 // it must be an implicit int or an error.
807 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000808 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000809 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000810 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000811
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000812 // C++: If the identifier is actually the name of the class type
813 // being defined and the next token is a '(', then this is a
814 // constructor declaration. We're done with the decl-specifiers
815 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000816 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000817 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
818 NextToken().getKind() == tok::l_paren)
819 goto DoneWithDeclSpec;
820
Douglas Gregora60c62e2009-02-09 15:09:02 +0000821 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000822 TypeRep);
823 if (isInvalid)
824 break;
825
826 DS.SetRangeEnd(Tok.getLocation());
827 ConsumeToken(); // The identifier
828
829 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
830 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
831 // Objective-C interface. If we don't have Objective-C or a '<', this is
832 // just a normal reference to a typedef name.
833 if (!Tok.is(tok::less) || !getLang().ObjC1)
834 continue;
835
836 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000837 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000838 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000839 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000840
841 DS.SetRangeEnd(EndProtoLoc);
842
Steve Narofff7683302008-09-22 10:28:57 +0000843 // Need to support trailing type qualifiers (e.g. "id<p> const").
844 // If a type specifier follows, it will be diagnosed elsewhere.
845 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000846 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000847
848 // type-name
849 case tok::annot_template_id: {
850 TemplateIdAnnotation *TemplateId
851 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000852 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000853 // This template-id does not refer to a type name, so we're
854 // done with the type-specifiers.
855 goto DoneWithDeclSpec;
856 }
857
858 // Turn the template-id annotation token into a type annotation
859 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000860 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000861 continue;
862 }
863
Chris Lattner4b009652007-07-25 00:24:17 +0000864 // GNU attributes support.
865 case tok::kw___attribute:
866 DS.AddAttributes(ParseAttributes());
867 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000868
869 // Microsoft declspec support.
870 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000871 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000872 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000873
Steve Naroffedd04d52008-12-25 14:16:32 +0000874 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000875 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000876 // FIXME: Add handling here!
877 break;
878
879 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000880 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000881 case tok::kw___cdecl:
882 case tok::kw___stdcall:
883 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000884 DS.AddAttributes(ParseMicrosoftTypeAttributes());
885 continue;
886
Chris Lattner4b009652007-07-25 00:24:17 +0000887 // storage-class-specifier
888 case tok::kw_typedef:
889 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
890 break;
891 case tok::kw_extern:
892 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000893 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000894 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
895 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000896 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000897 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
898 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000899 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000900 case tok::kw_static:
901 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000902 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000903 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
904 break;
905 case tok::kw_auto:
906 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
907 break;
908 case tok::kw_register:
909 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
910 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000911 case tok::kw_mutable:
912 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
913 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000914 case tok::kw___thread:
915 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
916 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000917
Chris Lattner4b009652007-07-25 00:24:17 +0000918 // function-specifier
919 case tok::kw_inline:
920 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
921 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000922 case tok::kw_virtual:
923 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
924 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000925 case tok::kw_explicit:
926 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
927 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000928
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000929 // friend
930 case tok::kw_friend:
931 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
932 break;
933
Chris Lattnerc297b722009-01-21 19:48:37 +0000934 // type-specifier
935 case tok::kw_short:
936 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
937 break;
938 case tok::kw_long:
939 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
940 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
941 else
942 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
943 break;
944 case tok::kw_signed:
945 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
946 break;
947 case tok::kw_unsigned:
948 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
949 break;
950 case tok::kw__Complex:
951 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
952 break;
953 case tok::kw__Imaginary:
954 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
955 break;
956 case tok::kw_void:
957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
958 break;
959 case tok::kw_char:
960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
961 break;
962 case tok::kw_int:
963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
964 break;
965 case tok::kw_float:
966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
967 break;
968 case tok::kw_double:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
970 break;
971 case tok::kw_wchar_t:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
973 break;
974 case tok::kw_bool:
975 case tok::kw__Bool:
976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
977 break;
978 case tok::kw__Decimal32:
979 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
980 break;
981 case tok::kw__Decimal64:
982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
983 break;
984 case tok::kw__Decimal128:
985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
986 break;
987
988 // class-specifier:
989 case tok::kw_class:
990 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000991 case tok::kw_union: {
992 tok::TokenKind Kind = Tok.getKind();
993 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000994 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000995 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000996 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000997
998 // enum-specifier:
999 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001000 ConsumeToken();
1001 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001002 continue;
1003
1004 // cv-qualifier:
1005 case tok::kw_const:
1006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
1007 break;
1008 case tok::kw_volatile:
1009 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1010 getLang())*2;
1011 break;
1012 case tok::kw_restrict:
1013 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1014 getLang())*2;
1015 break;
1016
Douglas Gregord3022602009-03-27 23:10:48 +00001017 // C++ typename-specifier:
1018 case tok::kw_typename:
1019 if (TryAnnotateTypeOrScopeToken())
1020 continue;
1021 break;
1022
Chris Lattnerc297b722009-01-21 19:48:37 +00001023 // GNU typeof support.
1024 case tok::kw_typeof:
1025 ParseTypeofSpecifier(DS);
1026 continue;
1027
Anders Carlssoneed418b2009-06-24 17:47:40 +00001028 case tok::kw_decltype:
1029 ParseDecltypeSpecifier(DS);
1030 continue;
1031
Steve Naroff5f0466b2008-06-05 00:02:44 +00001032 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001033 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001034 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1035 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001036 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001037 goto DoneWithDeclSpec;
1038
1039 {
1040 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001041 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001042 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +00001043 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001044 DS.SetRangeEnd(EndProtoLoc);
1045
Chris Lattnerf006a222008-11-18 07:48:38 +00001046 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001047 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001048 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001049 // Need to support trailing type qualifiers (e.g. "id<p> const").
1050 // If a type specifier follows, it will be diagnosed elsewhere.
1051 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001052 }
Chris Lattner4b009652007-07-25 00:24:17 +00001053 }
1054 // If the specifier combination wasn't legal, issue a diagnostic.
1055 if (isInvalid) {
1056 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001057 // Pick between error or extwarn.
1058 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1059 : diag::ext_duplicate_declspec;
1060 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001061 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001062 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001063 ConsumeToken();
1064 }
1065}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001066
Chris Lattnerd706dc82009-01-06 06:59:53 +00001067/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001068/// primarily follow the C++ grammar with additions for C99 and GNU,
1069/// which together subsume the C grammar. Note that the C++
1070/// type-specifier also includes the C type-qualifier (for const,
1071/// volatile, and C99 restrict). Returns true if a type-specifier was
1072/// found (and parsed), false otherwise.
1073///
1074/// type-specifier: [C++ 7.1.5]
1075/// simple-type-specifier
1076/// class-specifier
1077/// enum-specifier
1078/// elaborated-type-specifier [TODO]
1079/// cv-qualifier
1080///
1081/// cv-qualifier: [C++ 7.1.5.1]
1082/// 'const'
1083/// 'volatile'
1084/// [C99] 'restrict'
1085///
1086/// simple-type-specifier: [ C++ 7.1.5.2]
1087/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1088/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1089/// 'char'
1090/// 'wchar_t'
1091/// 'bool'
1092/// 'short'
1093/// 'int'
1094/// 'long'
1095/// 'signed'
1096/// 'unsigned'
1097/// 'float'
1098/// 'double'
1099/// 'void'
1100/// [C99] '_Bool'
1101/// [C99] '_Complex'
1102/// [C99] '_Imaginary' // Removed in TC2?
1103/// [GNU] '_Decimal32'
1104/// [GNU] '_Decimal64'
1105/// [GNU] '_Decimal128'
1106/// [GNU] typeof-specifier
1107/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1108/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlssoneed418b2009-06-24 17:47:40 +00001109/// [C++0x] 'decltype' ( expression )
Chris Lattnerd706dc82009-01-06 06:59:53 +00001110bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1111 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001112 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001113 SourceLocation Loc = Tok.getLocation();
1114
1115 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001116 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001117 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001118 // Annotate typenames and C++ scope specifiers. If we get one, just
1119 // recurse to handle whatever we get.
1120 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001121 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001122 // Otherwise, not a type specifier.
1123 return false;
1124 case tok::coloncolon: // ::foo::bar
1125 if (NextToken().is(tok::kw_new) || // ::new
1126 NextToken().is(tok::kw_delete)) // ::delete
1127 return false;
1128
1129 // Annotate typenames and C++ scope specifiers. If we get one, just
1130 // recurse to handle whatever we get.
1131 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001132 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001133 // Otherwise, not a type specifier.
1134 return false;
1135
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001136 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001137 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001138 if (Tok.getAnnotationValue())
1139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1140 Tok.getAnnotationValue());
1141 else
1142 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001143 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1144 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001145
1146 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1147 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1148 // Objective-C interface. If we don't have Objective-C or a '<', this is
1149 // just a normal reference to a typedef name.
1150 if (!Tok.is(tok::less) || !getLang().ObjC1)
1151 return true;
1152
1153 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001154 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001155 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1156 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1157
1158 DS.SetRangeEnd(EndProtoLoc);
1159 return true;
1160 }
1161
1162 case tok::kw_short:
1163 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1164 break;
1165 case tok::kw_long:
1166 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1167 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1168 else
1169 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1170 break;
1171 case tok::kw_signed:
1172 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1173 break;
1174 case tok::kw_unsigned:
1175 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1176 break;
1177 case tok::kw__Complex:
1178 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1179 break;
1180 case tok::kw__Imaginary:
1181 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1182 break;
1183 case tok::kw_void:
1184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1185 break;
1186 case tok::kw_char:
1187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1188 break;
1189 case tok::kw_int:
1190 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1191 break;
1192 case tok::kw_float:
1193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1194 break;
1195 case tok::kw_double:
1196 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1197 break;
1198 case tok::kw_wchar_t:
1199 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1200 break;
1201 case tok::kw_bool:
1202 case tok::kw__Bool:
1203 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1204 break;
1205 case tok::kw__Decimal32:
1206 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1207 break;
1208 case tok::kw__Decimal64:
1209 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1210 break;
1211 case tok::kw__Decimal128:
1212 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1213 break;
1214
1215 // class-specifier:
1216 case tok::kw_class:
1217 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001218 case tok::kw_union: {
1219 tok::TokenKind Kind = Tok.getKind();
1220 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001221 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001222 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001223 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001224
1225 // enum-specifier:
1226 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001227 ConsumeToken();
1228 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001229 return true;
1230
1231 // cv-qualifier:
1232 case tok::kw_const:
1233 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1234 getLang())*2;
1235 break;
1236 case tok::kw_volatile:
1237 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1238 getLang())*2;
1239 break;
1240 case tok::kw_restrict:
1241 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1242 getLang())*2;
1243 break;
1244
1245 // GNU typeof support.
1246 case tok::kw_typeof:
1247 ParseTypeofSpecifier(DS);
1248 return true;
1249
Anders Carlssoneed418b2009-06-24 17:47:40 +00001250 // C++0x decltype support.
1251 case tok::kw_decltype:
1252 ParseDecltypeSpecifier(DS);
1253 return true;
1254
Eli Friedman891d82f2009-06-08 23:27:34 +00001255 case tok::kw___ptr64:
1256 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001257 case tok::kw___cdecl:
1258 case tok::kw___stdcall:
1259 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001260 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001261 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001262
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001263 default:
1264 // Not a type-specifier; do nothing.
1265 return false;
1266 }
1267
1268 // If the specifier combination wasn't legal, issue a diagnostic.
1269 if (isInvalid) {
1270 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001271 // Pick between error or extwarn.
1272 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1273 : diag::ext_duplicate_declspec;
1274 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001275 }
1276 DS.SetRangeEnd(Tok.getLocation());
1277 ConsumeToken(); // whatever we parsed above.
1278 return true;
1279}
Chris Lattner4b009652007-07-25 00:24:17 +00001280
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001281/// ParseStructDeclaration - Parse a struct declaration without the terminating
1282/// semicolon.
1283///
Chris Lattner4b009652007-07-25 00:24:17 +00001284/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001285/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001286/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001287/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001288/// struct-declarator-list:
1289/// struct-declarator
1290/// struct-declarator-list ',' struct-declarator
1291/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1292/// struct-declarator:
1293/// declarator
1294/// [GNU] declarator attributes[opt]
1295/// declarator[opt] ':' constant-expression
1296/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1297///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001298void Parser::
1299ParseStructDeclaration(DeclSpec &DS,
1300 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001301 if (Tok.is(tok::kw___extension__)) {
1302 // __extension__ silences extension warnings in the subexpression.
1303 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001304 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001305 return ParseStructDeclaration(DS, Fields);
1306 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001307
1308 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001309 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001310 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001311
Douglas Gregorb748fc52009-01-12 22:49:06 +00001312 // If there are no declarators, this is a free-standing declaration
1313 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001314 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001315 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001316 return;
1317 }
1318
1319 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001320 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001321 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001322 FieldDeclarator &DeclaratorInfo = Fields.back();
1323
Steve Naroffa9adf112007-08-20 22:28:22 +00001324 /// struct-declarator: declarator
1325 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001326 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001327 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001328
Chris Lattner34a01ad2007-10-09 17:33:22 +00001329 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001330 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001331 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001332 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001333 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001334 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001335 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001336 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001337
Steve Naroffa9adf112007-08-20 22:28:22 +00001338 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001339 if (Tok.is(tok::kw___attribute)) {
1340 SourceLocation Loc;
1341 AttributeList *AttrList = ParseAttributes(&Loc);
1342 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1343 }
1344
Steve Naroffa9adf112007-08-20 22:28:22 +00001345 // If we don't have a comma, it is either the end of the list (a ';')
1346 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001348 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001349
Steve Naroffa9adf112007-08-20 22:28:22 +00001350 // Consume the comma.
1351 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001352
Steve Naroffa9adf112007-08-20 22:28:22 +00001353 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001354 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001355
Steve Naroffa9adf112007-08-20 22:28:22 +00001356 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001357 if (Tok.is(tok::kw___attribute)) {
1358 SourceLocation Loc;
1359 AttributeList *AttrList = ParseAttributes(&Loc);
1360 Fields.back().D.AddAttributes(AttrList, Loc);
1361 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001362 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001363}
1364
1365/// ParseStructUnionBody
1366/// struct-contents:
1367/// struct-declaration-list
1368/// [EXT] empty
1369/// [GNU] "struct-declaration-list" without terminatoring ';'
1370/// struct-declaration-list:
1371/// struct-declaration
1372/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001373/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001374///
Chris Lattner4b009652007-07-25 00:24:17 +00001375void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001376 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001377 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1378 PP.getSourceManager(),
1379 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001380
Chris Lattner4b009652007-07-25 00:24:17 +00001381 SourceLocation LBraceLoc = ConsumeBrace();
1382
Douglas Gregorcab994d2009-01-09 22:42:13 +00001383 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001384 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1385
Chris Lattner4b009652007-07-25 00:24:17 +00001386 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1387 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001388 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001389 Diag(Tok, diag::ext_empty_struct_union_enum)
1390 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001391
Chris Lattner5261d0c2009-03-28 19:18:32 +00001392 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001393 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1394
Chris Lattner4b009652007-07-25 00:24:17 +00001395 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001396 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001397 // Each iteration of this loop reads one struct-declaration.
1398
1399 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001400 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001401 Diag(Tok, diag::ext_extra_struct_semi)
1402 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001403 ConsumeToken();
1404 continue;
1405 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001406
1407 // Parse all the comma separated declarators.
1408 DeclSpec DS;
1409 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001410 if (!Tok.is(tok::at)) {
1411 ParseStructDeclaration(DS, FieldDeclarators);
1412
1413 // Convert them all to fields.
1414 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1415 FieldDeclarator &FD = FieldDeclarators[i];
1416 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001417 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1418 DS.getSourceRange().getBegin(),
1419 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001420 FieldDecls.push_back(Field);
1421 }
1422 } else { // Handle @defs
1423 ConsumeToken();
1424 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1425 Diag(Tok, diag::err_unexpected_at);
1426 SkipUntil(tok::semi, true, true);
1427 continue;
1428 }
1429 ConsumeToken();
1430 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1431 if (!Tok.is(tok::identifier)) {
1432 Diag(Tok, diag::err_expected_ident);
1433 SkipUntil(tok::semi, true, true);
1434 continue;
1435 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001436 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001437 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1438 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001439 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1440 ConsumeToken();
1441 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1442 }
Chris Lattner4b009652007-07-25 00:24:17 +00001443
Chris Lattner34a01ad2007-10-09 17:33:22 +00001444 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001445 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001446 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001447 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001448 break;
1449 } else {
1450 Diag(Tok, diag::err_expected_semi_decl_list);
1451 // Skip to end of block or statement
1452 SkipUntil(tok::r_brace, true, true);
1453 }
1454 }
1455
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001456 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001457
Chris Lattner4b009652007-07-25 00:24:17 +00001458 AttributeList *AttrList = 0;
1459 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001460 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001461 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001462
1463 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001464 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001465 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001466 AttrList);
1467 StructScope.Exit();
1468 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001469}
1470
1471
1472/// ParseEnumSpecifier
1473/// enum-specifier: [C99 6.7.2.2]
1474/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001475///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001476/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1477/// '}' attributes[opt]
1478/// 'enum' identifier
1479/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001480///
1481/// [C++] elaborated-type-specifier:
1482/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1483///
Chris Lattner197b4342009-04-12 21:49:30 +00001484void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1485 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001486 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001487
1488 AttributeList *Attr = 0;
1489 // If attributes exist after tag, parse them.
1490 if (Tok.is(tok::kw___attribute))
1491 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001492
1493 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001494 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001495 if (Tok.isNot(tok::identifier)) {
1496 Diag(Tok, diag::err_expected_ident);
1497 if (Tok.isNot(tok::l_brace)) {
1498 // Has no name and is not a definition.
1499 // Skip the rest of this declarator, up until the comma or semicolon.
1500 SkipUntil(tok::comma, true);
1501 return;
1502 }
1503 }
1504 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001505
1506 // Must have either 'enum name' or 'enum {...}'.
1507 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1508 Diag(Tok, diag::err_expected_ident_lbrace);
1509
1510 // Skip the rest of this declarator, up until the comma or semicolon.
1511 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001512 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001513 }
1514
1515 // If an identifier is present, consume and remember it.
1516 IdentifierInfo *Name = 0;
1517 SourceLocation NameLoc;
1518 if (Tok.is(tok::identifier)) {
1519 Name = Tok.getIdentifierInfo();
1520 NameLoc = ConsumeToken();
1521 }
1522
1523 // There are three options here. If we have 'enum foo;', then this is a
1524 // forward declaration. If we have 'enum foo {...' then this is a
1525 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1526 //
1527 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1528 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1529 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1530 //
1531 Action::TagKind TK;
1532 if (Tok.is(tok::l_brace))
1533 TK = Action::TK_Definition;
1534 else if (Tok.is(tok::semi))
1535 TK = Action::TK_Declaration;
1536 else
1537 TK = Action::TK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001538 bool Owned = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001539 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001540 StartLoc, SS, Name, NameLoc, Attr, AS,
1541 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001542
Chris Lattner34a01ad2007-10-09 17:33:22 +00001543 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001544 ParseEnumBody(StartLoc, TagDecl);
1545
1546 // TODO: semantic analysis on the declspec for enums.
1547 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001548 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +00001549 TagDecl.getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +00001550 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001551}
1552
1553/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1554/// enumerator-list:
1555/// enumerator
1556/// enumerator-list ',' enumerator
1557/// enumerator:
1558/// enumeration-constant
1559/// enumeration-constant '=' constant-expression
1560/// enumeration-constant:
1561/// identifier
1562///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001563void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001564 // Enter the scope of the enum body and start the definition.
1565 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001566 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001567
Chris Lattner4b009652007-07-25 00:24:17 +00001568 SourceLocation LBraceLoc = ConsumeBrace();
1569
Chris Lattnerc9a92452007-08-27 17:24:30 +00001570 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001571 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001572 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001573
Chris Lattner5261d0c2009-03-28 19:18:32 +00001574 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001575
Chris Lattner5261d0c2009-03-28 19:18:32 +00001576 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001577
1578 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001579 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001580 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1581 SourceLocation IdentLoc = ConsumeToken();
1582
1583 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001584 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001585 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001586 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001587 AssignedVal = ParseConstantExpression();
1588 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001589 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001590 }
1591
1592 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001593 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1594 LastEnumConstDecl,
1595 IdentLoc, Ident,
1596 EqualLoc,
1597 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001598 EnumConstantDecls.push_back(EnumConstDecl);
1599 LastEnumConstDecl = EnumConstDecl;
1600
Chris Lattner34a01ad2007-10-09 17:33:22 +00001601 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001602 break;
1603 SourceLocation CommaLoc = ConsumeToken();
1604
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001605 if (Tok.isNot(tok::identifier) &&
1606 !(getLang().C99 || getLang().CPlusPlus0x))
1607 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1608 << getLang().CPlusPlus
1609 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001610 }
1611
1612 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001613 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001614
Mike Stump155750e2009-05-16 07:06:02 +00001615 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001616 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001617
Chris Lattner5261d0c2009-03-28 19:18:32 +00001618 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001619 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001620 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001621 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001622
1623 EnumScope.Exit();
1624 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001625}
1626
1627/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001628/// start of a type-qualifier-list.
1629bool Parser::isTypeQualifier() const {
1630 switch (Tok.getKind()) {
1631 default: return false;
1632 // type-qualifier
1633 case tok::kw_const:
1634 case tok::kw_volatile:
1635 case tok::kw_restrict:
1636 return true;
1637 }
1638}
1639
1640/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001641/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001642bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001643 switch (Tok.getKind()) {
1644 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001645
1646 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001647 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001648 // Annotate typenames and C++ scope specifiers. If we get one, just
1649 // recurse to handle whatever we get.
1650 if (TryAnnotateTypeOrScopeToken())
1651 return isTypeSpecifierQualifier();
1652 // Otherwise, not a type specifier.
1653 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001654
Chris Lattnerb75fde62009-01-04 23:41:41 +00001655 case tok::coloncolon: // ::foo::bar
1656 if (NextToken().is(tok::kw_new) || // ::new
1657 NextToken().is(tok::kw_delete)) // ::delete
1658 return false;
1659
1660 // Annotate typenames and C++ scope specifiers. If we get one, just
1661 // recurse to handle whatever we get.
1662 if (TryAnnotateTypeOrScopeToken())
1663 return isTypeSpecifierQualifier();
1664 // Otherwise, not a type specifier.
1665 return false;
1666
Chris Lattner4b009652007-07-25 00:24:17 +00001667 // GNU attributes support.
1668 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001669 // GNU typeof support.
1670 case tok::kw_typeof:
1671
Chris Lattner4b009652007-07-25 00:24:17 +00001672 // type-specifiers
1673 case tok::kw_short:
1674 case tok::kw_long:
1675 case tok::kw_signed:
1676 case tok::kw_unsigned:
1677 case tok::kw__Complex:
1678 case tok::kw__Imaginary:
1679 case tok::kw_void:
1680 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001681 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001682 case tok::kw_int:
1683 case tok::kw_float:
1684 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001685 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001686 case tok::kw__Bool:
1687 case tok::kw__Decimal32:
1688 case tok::kw__Decimal64:
1689 case tok::kw__Decimal128:
1690
Chris Lattner2e78db32008-04-13 18:59:07 +00001691 // struct-or-union-specifier (C99) or class-specifier (C++)
1692 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001693 case tok::kw_struct:
1694 case tok::kw_union:
1695 // enum-specifier
1696 case tok::kw_enum:
1697
1698 // type-qualifier
1699 case tok::kw_const:
1700 case tok::kw_volatile:
1701 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001702
1703 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001704 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001705 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001706
1707 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1708 case tok::less:
1709 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001710
1711 case tok::kw___cdecl:
1712 case tok::kw___stdcall:
1713 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001714 case tok::kw___w64:
1715 case tok::kw___ptr64:
1716 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001717 }
1718}
1719
1720/// isDeclarationSpecifier() - Return true if the current token is part of a
1721/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001722bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001723 switch (Tok.getKind()) {
1724 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001725
1726 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001727 // Unfortunate hack to support "Class.factoryMethod" notation.
1728 if (getLang().ObjC1 && NextToken().is(tok::period))
1729 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001730 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001731
Douglas Gregord3022602009-03-27 23:10:48 +00001732 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001733 // Annotate typenames and C++ scope specifiers. If we get one, just
1734 // recurse to handle whatever we get.
1735 if (TryAnnotateTypeOrScopeToken())
1736 return isDeclarationSpecifier();
1737 // Otherwise, not a declaration specifier.
1738 return false;
1739 case tok::coloncolon: // ::foo::bar
1740 if (NextToken().is(tok::kw_new) || // ::new
1741 NextToken().is(tok::kw_delete)) // ::delete
1742 return false;
1743
1744 // Annotate typenames and C++ scope specifiers. If we get one, just
1745 // recurse to handle whatever we get.
1746 if (TryAnnotateTypeOrScopeToken())
1747 return isDeclarationSpecifier();
1748 // Otherwise, not a declaration specifier.
1749 return false;
1750
Chris Lattner4b009652007-07-25 00:24:17 +00001751 // storage-class-specifier
1752 case tok::kw_typedef:
1753 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001754 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001755 case tok::kw_static:
1756 case tok::kw_auto:
1757 case tok::kw_register:
1758 case tok::kw___thread:
1759
1760 // type-specifiers
1761 case tok::kw_short:
1762 case tok::kw_long:
1763 case tok::kw_signed:
1764 case tok::kw_unsigned:
1765 case tok::kw__Complex:
1766 case tok::kw__Imaginary:
1767 case tok::kw_void:
1768 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001769 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001770 case tok::kw_int:
1771 case tok::kw_float:
1772 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001773 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001774 case tok::kw__Bool:
1775 case tok::kw__Decimal32:
1776 case tok::kw__Decimal64:
1777 case tok::kw__Decimal128:
1778
Chris Lattner2e78db32008-04-13 18:59:07 +00001779 // struct-or-union-specifier (C99) or class-specifier (C++)
1780 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001781 case tok::kw_struct:
1782 case tok::kw_union:
1783 // enum-specifier
1784 case tok::kw_enum:
1785
1786 // type-qualifier
1787 case tok::kw_const:
1788 case tok::kw_volatile:
1789 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001790
Chris Lattner4b009652007-07-25 00:24:17 +00001791 // function-specifier
1792 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001793 case tok::kw_virtual:
1794 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001795
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001796 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001797 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001798
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001799 // GNU typeof support.
1800 case tok::kw_typeof:
1801
1802 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001803 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001804 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001805
1806 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1807 case tok::less:
1808 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001809
Steve Naroffab1a3632009-01-06 19:34:12 +00001810 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001811 case tok::kw___cdecl:
1812 case tok::kw___stdcall:
1813 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001814 case tok::kw___w64:
1815 case tok::kw___ptr64:
1816 case tok::kw___forceinline:
1817 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001818 }
1819}
1820
1821
1822/// ParseTypeQualifierListOpt
1823/// type-qualifier-list: [C99 6.7.5]
1824/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001825/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001826/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001827/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001828///
Chris Lattner460696f2008-12-18 07:02:59 +00001829void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001830 while (1) {
1831 int isInvalid = false;
1832 const char *PrevSpec = 0;
1833 SourceLocation Loc = Tok.getLocation();
1834
1835 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001836 case tok::kw_const:
1837 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1838 getLang())*2;
1839 break;
1840 case tok::kw_volatile:
1841 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1842 getLang())*2;
1843 break;
1844 case tok::kw_restrict:
1845 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1846 getLang())*2;
1847 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001848 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001849 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001850 case tok::kw___cdecl:
1851 case tok::kw___stdcall:
1852 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001853 if (AttributesAllowed) {
1854 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1855 continue;
1856 }
1857 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001858 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001859 if (AttributesAllowed) {
1860 DS.AddAttributes(ParseAttributes());
1861 continue; // do *not* consume the next token!
1862 }
1863 // otherwise, FALL THROUGH!
1864 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001865 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001866 // If this is not a type-qualifier token, we're done reading type
1867 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001868 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001869 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001870 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001871
Chris Lattner4b009652007-07-25 00:24:17 +00001872 // If the specifier combination wasn't legal, issue a diagnostic.
1873 if (isInvalid) {
1874 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001875 // Pick between error or extwarn.
1876 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1877 : diag::ext_duplicate_declspec;
1878 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001879 }
1880 ConsumeToken();
1881 }
1882}
1883
1884
1885/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1886///
1887void Parser::ParseDeclarator(Declarator &D) {
1888 /// This implements the 'declarator' production in the C grammar, then checks
1889 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001890 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001891}
1892
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001893/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1894/// is parsed by the function passed to it. Pass null, and the direct-declarator
1895/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001896/// ptr-operator production.
1897///
Sebastian Redl75555032009-01-24 21:16:55 +00001898/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1899/// [C] pointer[opt] direct-declarator
1900/// [C++] direct-declarator
1901/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001902///
1903/// pointer: [C99 6.7.5]
1904/// '*' type-qualifier-list[opt]
1905/// '*' type-qualifier-list[opt] pointer
1906///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001907/// ptr-operator:
1908/// '*' cv-qualifier-seq[opt]
1909/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001910/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001911/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001912/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001913/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001914void Parser::ParseDeclaratorInternal(Declarator &D,
1915 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001916
Sebastian Redl75555032009-01-24 21:16:55 +00001917 // C++ member pointers start with a '::' or a nested-name.
1918 // Member pointers get special handling, since there's no place for the
1919 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001920 if (getLang().CPlusPlus &&
1921 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1922 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001923 CXXScopeSpec SS;
1924 if (ParseOptionalCXXScopeSpecifier(SS)) {
1925 if(Tok.isNot(tok::star)) {
1926 // The scope spec really belongs to the direct-declarator.
1927 D.getCXXScopeSpec() = SS;
1928 if (DirectDeclParser)
1929 (this->*DirectDeclParser)(D);
1930 return;
1931 }
1932
1933 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001934 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001935 DeclSpec DS;
1936 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001937 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001938
1939 // Recurse to parse whatever is left.
1940 ParseDeclaratorInternal(D, DirectDeclParser);
1941
1942 // Sema will have to catch (syntactically invalid) pointers into global
1943 // scope. It has to catch pointers into namespace scope anyway.
1944 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001945 Loc, DS.TakeAttributes()),
1946 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001947 return;
1948 }
1949 }
1950
1951 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001952 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001953 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001954 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001955 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001956 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001957 if (DirectDeclParser)
1958 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001959 return;
1960 }
Sebastian Redl75555032009-01-24 21:16:55 +00001961
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001962 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1963 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001964 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001965 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001966
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001967 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001968 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001969 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001970
Chris Lattner4b009652007-07-25 00:24:17 +00001971 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001972 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001973
Chris Lattner4b009652007-07-25 00:24:17 +00001974 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001975 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001976 if (Kind == tok::star)
1977 // Remember that we parsed a pointer type, and remember the type-quals.
1978 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001979 DS.TakeAttributes()),
1980 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001981 else
1982 // Remember that we parsed a Block type, and remember the type-quals.
1983 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001984 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001985 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001986 } else {
1987 // Is a reference
1988 DeclSpec DS;
1989
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001990 // Complain about rvalue references in C++03, but then go on and build
1991 // the declarator.
1992 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1993 Diag(Loc, diag::err_rvalue_reference);
1994
Chris Lattner4b009652007-07-25 00:24:17 +00001995 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1996 // cv-qualifiers are introduced through the use of a typedef or of a
1997 // template type argument, in which case the cv-qualifiers are ignored.
1998 //
1999 // [GNU] Retricted references are allowed.
2000 // [GNU] Attributes on references are allowed.
2001 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002002 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00002003
2004 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2005 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2006 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002007 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00002008 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2009 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002010 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00002011 }
2012
2013 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002014 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00002015
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002016 if (D.getNumTypeObjects() > 0) {
2017 // C++ [dcl.ref]p4: There shall be no references to references.
2018 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2019 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002020 if (const IdentifierInfo *II = D.getIdentifier())
2021 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2022 << II;
2023 else
2024 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2025 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002026
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002027 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002028 // can go ahead and build the (technically ill-formed)
2029 // declarator: reference collapsing will take care of it.
2030 }
2031 }
2032
Chris Lattner4b009652007-07-25 00:24:17 +00002033 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002034 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002035 DS.TakeAttributes(),
2036 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002037 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002038 }
2039}
2040
2041/// ParseDirectDeclarator
2042/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002043/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002044/// '(' declarator ')'
2045/// [GNU] '(' attributes declarator ')'
2046/// [C90] direct-declarator '[' constant-expression[opt] ']'
2047/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2048/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2049/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2050/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2051/// direct-declarator '(' parameter-type-list ')'
2052/// direct-declarator '(' identifier-list[opt] ')'
2053/// [GNU] direct-declarator '(' parameter-forward-declarations
2054/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002055/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2056/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002057/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002058///
2059/// declarator-id: [C++ 8]
2060/// id-expression
2061/// '::'[opt] nested-name-specifier[opt] type-name
2062///
2063/// id-expression: [C++ 5.1]
2064/// unqualified-id
2065/// qualified-id [TODO]
2066///
2067/// unqualified-id: [C++ 5.1]
2068/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002069/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002070/// conversion-function-id [TODO]
2071/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002072/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002073///
Chris Lattner4b009652007-07-25 00:24:17 +00002074void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002075 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002076
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002077 if (getLang().CPlusPlus) {
2078 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002079 // ParseDeclaratorInternal might already have parsed the scope.
2080 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2081 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002082 if (afterCXXScope) {
2083 // Change the declaration context for name lookup, until this function
2084 // is exited (and the declarator has been parsed).
2085 DeclScopeObj.EnterDeclaratorScope();
2086 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002087
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002088 if (Tok.is(tok::identifier)) {
2089 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002090
2091 // If this identifier is the name of the current class, it's a
2092 // constructor name.
2093 if (!D.getDeclSpec().hasTypeSpecifier() &&
2094 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2095 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2096 Tok.getLocation(), CurScope),
2097 Tok.getLocation());
2098 // This is a normal identifier.
2099 } else
2100 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002101 ConsumeToken();
2102 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002103 } else if (Tok.is(tok::annot_template_id)) {
2104 TemplateIdAnnotation *TemplateId
2105 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2106
2107 // FIXME: Could this template-id name a constructor?
2108
2109 // FIXME: This is an egregious hack, where we silently ignore
2110 // the specialization (which should be a function template
2111 // specialization name) and use the name instead. This hack
2112 // will go away when we have support for function
2113 // specializations.
2114 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2115 TemplateId->Destroy();
2116 ConsumeToken();
2117 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002118 } else if (Tok.is(tok::kw_operator)) {
2119 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002120 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002121
Douglas Gregor853dd392008-12-26 15:00:45 +00002122 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002123 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2124 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002125 } else {
2126 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002127 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2128 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2129 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002130 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002131 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002132 }
2133 goto PastIdentifier;
2134 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002135 // This should be a C++ destructor.
2136 SourceLocation TildeLoc = ConsumeToken();
2137 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002138 // FIXME: Inaccurate.
2139 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002140 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002141 TypeResult Type = ParseClassName(EndLoc);
2142 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002143 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002144 else
2145 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002146 } else {
2147 Diag(Tok, diag::err_expected_class_name);
2148 D.SetIdentifier(0, TildeLoc);
2149 }
2150 goto PastIdentifier;
2151 }
2152
2153 // If we reached this point, token is not identifier and not '~'.
2154
2155 if (afterCXXScope) {
2156 Diag(Tok, diag::err_expected_unqualified_id);
2157 D.SetIdentifier(0, Tok.getLocation());
2158 D.setInvalidType(true);
2159 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002160 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002161 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002162 }
2163
2164 // If we reached this point, we are either in C/ObjC or the token didn't
2165 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002166 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2167 assert(!getLang().CPlusPlus &&
2168 "There's a C++-specific check for tok::identifier above");
2169 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2170 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2171 ConsumeToken();
2172 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002173 // direct-declarator: '(' declarator ')'
2174 // direct-declarator: '(' attributes declarator ')'
2175 // Example: 'char (*X)' or 'int (*XX)(void)'
2176 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002177 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002178 // This could be something simple like "int" (in which case the declarator
2179 // portion is empty), if an abstract-declarator is allowed.
2180 D.SetIdentifier(0, Tok.getLocation());
2181 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002182 if (D.getContext() == Declarator::MemberContext)
2183 Diag(Tok, diag::err_expected_member_name_or_semi)
2184 << D.getDeclSpec().getSourceRange();
2185 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002186 Diag(Tok, diag::err_expected_unqualified_id);
2187 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002188 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002189 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002190 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002191 }
2192
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002193 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002194 assert(D.isPastIdentifier() &&
2195 "Haven't past the location of the identifier yet?");
2196
2197 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002198 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002199 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2200 // In such a case, check if we actually have a function declarator; if it
2201 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002202 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2203 // When not in file scope, warn for ambiguous function declarators, just
2204 // in case the author intended it as a variable definition.
2205 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2206 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2207 break;
2208 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002209 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002210 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002211 ParseBracketDeclarator(D);
2212 } else {
2213 break;
2214 }
2215 }
2216}
2217
Chris Lattnera0d056d2008-04-06 05:45:57 +00002218/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2219/// only called before the identifier, so these are most likely just grouping
2220/// parens for precedence. If we find that these are actually function
2221/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2222///
2223/// direct-declarator:
2224/// '(' declarator ')'
2225/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002226/// direct-declarator '(' parameter-type-list ')'
2227/// direct-declarator '(' identifier-list[opt] ')'
2228/// [GNU] direct-declarator '(' parameter-forward-declarations
2229/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002230///
2231void Parser::ParseParenDeclarator(Declarator &D) {
2232 SourceLocation StartLoc = ConsumeParen();
2233 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2234
Chris Lattner1f185292008-10-20 02:05:46 +00002235 // Eat any attributes before we look at whether this is a grouping or function
2236 // declarator paren. If this is a grouping paren, the attribute applies to
2237 // the type being built up, for example:
2238 // int (__attribute__(()) *x)(long y)
2239 // If this ends up not being a grouping paren, the attribute applies to the
2240 // first argument, for example:
2241 // int (__attribute__(()) int x)
2242 // In either case, we need to eat any attributes to be able to determine what
2243 // sort of paren this is.
2244 //
2245 AttributeList *AttrList = 0;
2246 bool RequiresArg = false;
2247 if (Tok.is(tok::kw___attribute)) {
2248 AttrList = ParseAttributes();
2249
2250 // We require that the argument list (if this is a non-grouping paren) be
2251 // present even if the attribute list was empty.
2252 RequiresArg = true;
2253 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002254 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002255 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2256 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2257 Tok.is(tok::kw___ptr64)) {
2258 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2259 }
Chris Lattner1f185292008-10-20 02:05:46 +00002260
Chris Lattnera0d056d2008-04-06 05:45:57 +00002261 // If we haven't past the identifier yet (or where the identifier would be
2262 // stored, if this is an abstract declarator), then this is probably just
2263 // grouping parens. However, if this could be an abstract-declarator, then
2264 // this could also be the start of function arguments (consider 'void()').
2265 bool isGrouping;
2266
2267 if (!D.mayOmitIdentifier()) {
2268 // If this can't be an abstract-declarator, this *must* be a grouping
2269 // paren, because we haven't seen the identifier yet.
2270 isGrouping = true;
2271 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002272 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002273 isDeclarationSpecifier()) { // 'int(int)' is a function.
2274 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2275 // considered to be a type, not a K&R identifier-list.
2276 isGrouping = false;
2277 } else {
2278 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2279 isGrouping = true;
2280 }
2281
2282 // If this is a grouping paren, handle:
2283 // direct-declarator: '(' declarator ')'
2284 // direct-declarator: '(' attributes declarator ')'
2285 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002286 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002287 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002288 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002289 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002290
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002291 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002292 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002293 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002294
2295 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002296 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002297 return;
2298 }
2299
2300 // Okay, if this wasn't a grouping paren, it must be the start of a function
2301 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002302 // identifier (and remember where it would have been), then call into
2303 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002304 D.SetIdentifier(0, Tok.getLocation());
2305
Chris Lattner1f185292008-10-20 02:05:46 +00002306 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002307}
2308
2309/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2310/// declarator D up to a paren, which indicates that we are parsing function
2311/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002312///
Chris Lattner1f185292008-10-20 02:05:46 +00002313/// If AttrList is non-null, then the caller parsed those arguments immediately
2314/// after the open paren - they should be considered to be the first argument of
2315/// a parameter. If RequiresArg is true, then the first argument of the
2316/// function is required to be present and required to not be an identifier
2317/// list.
2318///
Chris Lattner4b009652007-07-25 00:24:17 +00002319/// This method also handles this portion of the grammar:
2320/// parameter-type-list: [C99 6.7.5]
2321/// parameter-list
2322/// parameter-list ',' '...'
2323///
2324/// parameter-list: [C99 6.7.5]
2325/// parameter-declaration
2326/// parameter-list ',' parameter-declaration
2327///
2328/// parameter-declaration: [C99 6.7.5]
2329/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002330/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002331/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002332/// declaration-specifiers abstract-declarator[opt]
2333/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002334/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002335/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2336///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002337/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002338/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002339///
Chris Lattner1f185292008-10-20 02:05:46 +00002340void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2341 AttributeList *AttrList,
2342 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002343 // lparen is already consumed!
2344 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002345
Chris Lattner1f185292008-10-20 02:05:46 +00002346 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002347 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002348 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002349 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002350 delete AttrList;
2351 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002352
Sebastian Redl0c986032009-02-09 18:23:29 +00002353 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002354
2355 // cv-qualifier-seq[opt].
2356 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002357 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002358 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002359 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002360 llvm::SmallVector<TypeTy*, 2> Exceptions;
2361 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002362 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002363 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002364 if (!DS.getSourceRange().getEnd().isInvalid())
2365 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002366
2367 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002368 if (Tok.is(tok::kw_throw)) {
2369 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002370 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002371 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2372 hasAnyExceptionSpec);
2373 assert(Exceptions.size() == ExceptionRanges.size() &&
2374 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002375 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002376 }
2377
Chris Lattner9f7564b2008-04-06 06:57:35 +00002378 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002379 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002380 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002381 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002382 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002383 /*arglist*/ 0, 0,
2384 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002385 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002386 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002387 Exceptions.data(),
2388 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002389 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002390 LParenLoc, D),
2391 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002392 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002393 }
2394
Chris Lattner1f185292008-10-20 02:05:46 +00002395 // Alternatively, this parameter list may be an identifier list form for a
2396 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002397 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002398 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002399 // K&R identifier lists can't have typedefs as identifiers, per
2400 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002401 if (RequiresArg) {
2402 Diag(Tok, diag::err_argument_required_after_attribute);
2403 delete AttrList;
2404 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002405 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2406 // normal declarators, not for abstract-declarators.
2407 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002408 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002409 }
2410
2411 // Finally, a normal, non-empty parameter type list.
2412
2413 // Build up an array of information about the parsed arguments.
2414 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002415
2416 // Enter function-declaration scope, limiting any declarators to the
2417 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002418 ParseScope PrototypeScope(this,
2419 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002420
2421 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002422 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002423 while (1) {
2424 if (Tok.is(tok::ellipsis)) {
2425 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002426 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002427 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002428 }
2429
Chris Lattner9f7564b2008-04-06 06:57:35 +00002430 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002431
Chris Lattner9f7564b2008-04-06 06:57:35 +00002432 // Parse the declaration-specifiers.
2433 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002434
2435 // If the caller parsed attributes for the first argument, add them now.
2436 if (AttrList) {
2437 DS.AddAttributes(AttrList);
2438 AttrList = 0; // Only apply the attributes to the first parameter.
2439 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002440 ParseDeclarationSpecifiers(DS);
2441
Chris Lattner9f7564b2008-04-06 06:57:35 +00002442 // Parse the declarator. This is "PrototypeContext", because we must
2443 // accept either 'declarator' or 'abstract-declarator' here.
2444 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2445 ParseDeclarator(ParmDecl);
2446
2447 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002448 if (Tok.is(tok::kw___attribute)) {
2449 SourceLocation Loc;
2450 AttributeList *AttrList = ParseAttributes(&Loc);
2451 ParmDecl.AddAttributes(AttrList, Loc);
2452 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002453
Chris Lattner9f7564b2008-04-06 06:57:35 +00002454 // Remember this parsed parameter in ParamInfo.
2455 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2456
Douglas Gregor605de8d2008-12-16 21:30:33 +00002457 // DefArgToks is used when the parsing of default arguments needs
2458 // to be delayed.
2459 CachedTokens *DefArgToks = 0;
2460
Chris Lattner9f7564b2008-04-06 06:57:35 +00002461 // If no parameter was specified, verify that *something* was specified,
2462 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002463 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2464 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002465 // Completely missing, emit error.
2466 Diag(DSStart, diag::err_missing_param);
2467 } else {
2468 // Otherwise, we have something. Add it and let semantic analysis try
2469 // to grok it and add the result to the ParamInfo we are building.
2470
2471 // Inform the actions module about the parameter declarator, so it gets
2472 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002473 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002474
2475 // Parse the default argument, if any. We parse the default
2476 // arguments in all dialects; the semantic analysis in
2477 // ActOnParamDefaultArgument will reject the default argument in
2478 // C.
2479 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002480 SourceLocation EqualLoc = Tok.getLocation();
2481
Chris Lattner3e254fb2008-04-08 04:40:51 +00002482 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002483 if (D.getContext() == Declarator::MemberContext) {
2484 // If we're inside a class definition, cache the tokens
2485 // corresponding to the default argument. We'll actually parse
2486 // them when we see the end of the class definition.
2487 // FIXME: Templates will require something similar.
2488 // FIXME: Can we use a smart pointer for Toks?
2489 DefArgToks = new CachedTokens;
2490
2491 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2492 tok::semi, false)) {
2493 delete DefArgToks;
2494 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002495 Actions.ActOnParamDefaultArgumentError(Param);
2496 } else
Anders Carlssona116e6e2009-06-12 16:51:40 +00002497 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2498 (*DefArgToks)[1].getLocation());
Chris Lattner3e254fb2008-04-08 04:40:51 +00002499 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002500 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002501 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002502
2503 OwningExprResult DefArgResult(ParseAssignmentExpression());
2504 if (DefArgResult.isInvalid()) {
2505 Actions.ActOnParamDefaultArgumentError(Param);
2506 SkipUntil(tok::comma, tok::r_paren, true, true);
2507 } else {
2508 // Inform the actions module about the default argument
2509 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002510 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002511 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002512 }
2513 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002514
2515 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002516 ParmDecl.getIdentifierLoc(), Param,
2517 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002518 }
2519
2520 // If the next token is a comma, consume it and keep reading arguments.
2521 if (Tok.isNot(tok::comma)) break;
2522
2523 // Consume the comma.
2524 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002525 }
2526
Chris Lattner9f7564b2008-04-06 06:57:35 +00002527 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002528 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002529
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002530 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002531 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002532
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002533 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002534 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002535 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002536 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002537 llvm::SmallVector<TypeTy*, 2> Exceptions;
2538 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002539 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002540 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002541 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002542 if (!DS.getSourceRange().getEnd().isInvalid())
2543 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002544
2545 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002546 if (Tok.is(tok::kw_throw)) {
2547 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002548 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002549 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2550 hasAnyExceptionSpec);
2551 assert(Exceptions.size() == ExceptionRanges.size() &&
2552 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002553 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002554 }
2555
Chris Lattner4b009652007-07-25 00:24:17 +00002556 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002557 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002558 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002559 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002560 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002561 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002562 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002563 Exceptions.data(),
2564 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002565 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002566 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002567}
2568
Chris Lattner35d9c912008-04-06 06:34:08 +00002569/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2570/// we found a K&R-style identifier list instead of a type argument list. The
2571/// current token is known to be the first identifier in the list.
2572///
2573/// identifier-list: [C99 6.7.5]
2574/// identifier
2575/// identifier-list ',' identifier
2576///
2577void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2578 Declarator &D) {
2579 // Build up an array of information about the parsed arguments.
2580 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2581 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2582
2583 // If there was no identifier specified for the declarator, either we are in
2584 // an abstract-declarator, or we are in a parameter declarator which was found
2585 // to be abstract. In abstract-declarators, identifier lists are not valid:
2586 // diagnose this.
2587 if (!D.getIdentifier())
2588 Diag(Tok, diag::ext_ident_list_in_param);
2589
2590 // Tok is known to be the first identifier in the list. Remember this
2591 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002592 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002593 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002594 Tok.getLocation(),
2595 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002596
Chris Lattner113a56b2008-04-06 06:39:19 +00002597 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002598
2599 while (Tok.is(tok::comma)) {
2600 // Eat the comma.
2601 ConsumeToken();
2602
Chris Lattner113a56b2008-04-06 06:39:19 +00002603 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002604 if (Tok.isNot(tok::identifier)) {
2605 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002606 SkipUntil(tok::r_paren);
2607 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002608 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002609
Chris Lattner35d9c912008-04-06 06:34:08 +00002610 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002611
2612 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002613 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002614 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002615
2616 // Verify that the argument identifier has not already been mentioned.
2617 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002618 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002619 } else {
2620 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002621 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002622 Tok.getLocation(),
2623 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002624 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002625
2626 // Eat the identifier.
2627 ConsumeToken();
2628 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002629
2630 // If we have the closing ')', eat it and we're done.
2631 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2632
Chris Lattner113a56b2008-04-06 06:39:19 +00002633 // Remember that we parsed a function type, and remember the attributes. This
2634 // function type is always a K&R style function type, which is not varargs and
2635 // has no prototype.
2636 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002637 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002638 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002639 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002640 /*exception*/false,
2641 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002642 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002643 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002644}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002645
Chris Lattner4b009652007-07-25 00:24:17 +00002646/// [C90] direct-declarator '[' constant-expression[opt] ']'
2647/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2648/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2649/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2650/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2651void Parser::ParseBracketDeclarator(Declarator &D) {
2652 SourceLocation StartLoc = ConsumeBracket();
2653
Chris Lattner1525c3a2008-12-18 07:27:21 +00002654 // C array syntax has many features, but by-far the most common is [] and [4].
2655 // This code does a fast path to handle some of the most obvious cases.
2656 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002657 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002658 // Remember that we parsed the empty array type.
2659 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002660 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2661 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002662 return;
2663 } else if (Tok.getKind() == tok::numeric_constant &&
2664 GetLookAheadToken(1).is(tok::r_square)) {
2665 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002666 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002667 ConsumeToken();
2668
Sebastian Redl0c986032009-02-09 18:23:29 +00002669 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002670
2671 // If there was an error parsing the assignment-expression, recover.
2672 if (ExprRes.isInvalid())
2673 ExprRes.release(); // Deallocate expr, just use [].
2674
2675 // Remember that we parsed a array type, and remember its features.
2676 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002677 ExprRes.release(), StartLoc),
2678 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002679 return;
2680 }
2681
Chris Lattner4b009652007-07-25 00:24:17 +00002682 // If valid, this location is the position where we read the 'static' keyword.
2683 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002684 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002685 StaticLoc = ConsumeToken();
2686
2687 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002688 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002689 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002690 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002691
2692 // If we haven't already read 'static', check to see if there is one after the
2693 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002694 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002695 StaticLoc = ConsumeToken();
2696
2697 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2698 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002699 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002700
2701 // Handle the case where we have '[*]' as the array size. However, a leading
2702 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2703 // the the token after the star is a ']'. Since stars in arrays are
2704 // infrequent, use of lookahead is not costly here.
2705 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002706 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002707
Chris Lattner306d4df2008-12-18 06:50:14 +00002708 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002709 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002710 StaticLoc = SourceLocation(); // Drop the static.
2711 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002712 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002713 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002714 // Note, in C89, this production uses the constant-expr production instead
2715 // of assignment-expr. The only difference is that assignment-expr allows
2716 // things like '=' and '*='. Sema rejects these in C89 mode because they
2717 // are not i-c-e's, so we don't need to distinguish between the two here.
2718
Douglas Gregor98189262009-06-19 23:52:42 +00002719 // Parse the constant-expression or assignment-expression now (depending
2720 // on dialect).
2721 if (getLang().CPlusPlus)
2722 NumElements = ParseConstantExpression();
2723 else
2724 NumElements = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +00002725 }
2726
2727 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002728 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002729 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002730 // If the expression was invalid, skip it.
2731 SkipUntil(tok::r_square);
2732 return;
2733 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002734
2735 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2736
Chris Lattner1525c3a2008-12-18 07:27:21 +00002737 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002738 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2739 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002740 NumElements.release(), StartLoc),
2741 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002742}
2743
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002744/// [GNU] typeof-specifier:
2745/// typeof ( expressions )
2746/// typeof ( type-name )
2747/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002748///
2749void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002750 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002751 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002752 SourceLocation StartLoc = ConsumeToken();
2753
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002754 bool isCastExpr;
2755 TypeTy *CastTy;
2756 SourceRange CastRange;
2757 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2758 isCastExpr,
2759 CastTy,
2760 CastRange);
2761
2762 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002763 // FIXME: Not accurate, the range gets one token more than it should.
2764 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002765 else
2766 DS.SetRangeEnd(CastRange.getEnd());
2767
2768 if (isCastExpr) {
2769 if (!CastTy) {
2770 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002771 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002772 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002773
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002774 const char *PrevSpec = 0;
2775 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2776 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2777 CastTy))
2778 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2779 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002780 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002781
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002782 // If we get here, the operand to the typeof was an expresion.
2783 if (Operand.isInvalid()) {
2784 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002785 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002786 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002787
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002788 const char *PrevSpec = 0;
2789 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2790 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2791 Operand.release()))
2792 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002793}