blob: 40fbc0ccad42d251bf8ae8ce5e9f36b3fd39a9d7 [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
276/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000277/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000278/// others... [FIXME]
279///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000280Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
281 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000282 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000283 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000284 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000285 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000286 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000287 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000288 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000289 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000290 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000291 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000292 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000293 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000294 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000295 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000296 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000297 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000298 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000299 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000300
301 // This routine returns a DeclGroup, if the thing we parsed only contains a
302 // single decl, convert it now.
303 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000304}
305
306/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
307/// declaration-specifiers init-declarator-list[opt] ';'
308///[C90/C++]init-declarator-list ';' [TODO]
309/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000310///
311/// If RequireSemi is false, this does not check for a ';' at the end of the
312/// declaration.
313Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000314 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000315 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000316 // Parse the common declaration-specifiers piece.
317 DeclSpec DS;
318 ParseDeclarationSpecifiers(DS);
319
320 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
321 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000322 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000323 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000324 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
325 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000326 }
327
328 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
329 ParseDeclarator(DeclaratorInfo);
330
Chris Lattner2c41d482009-03-29 17:18:04 +0000331 DeclGroupPtrTy DG =
332 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000333
Chris Lattner9802a0a2009-04-02 04:16:50 +0000334 DeclEnd = Tok.getLocation();
335
Chris Lattnerf8016042009-03-29 17:27:48 +0000336 // If the client wants to check what comes after the declaration, just return
337 // immediately without checking anything!
338 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000339
340 if (Tok.is(tok::semi)) {
341 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000342 return DG;
343 }
344
Chris Lattner2c41d482009-03-29 17:18:04 +0000345 Diag(Tok, diag::err_expected_semi_declation);
346 // Skip to end of block or statement
347 SkipUntil(tok::r_brace, true, true);
348 if (Tok.is(tok::semi))
349 ConsumeToken();
350 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000351}
352
Douglas Gregore3298aa2009-05-12 21:31:51 +0000353/// \brief Parse 'declaration' after parsing 'declaration-specifiers
354/// declarator'. This method parses the remainder of the declaration
355/// (including any attributes or initializer, among other things) and
356/// finalizes the declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000357///
Chris Lattner4b009652007-07-25 00:24:17 +0000358/// init-declarator: [C99 6.7]
359/// declarator
360/// declarator '=' initializer
361/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
362/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000363/// [C++] declarator initializer[opt]
364///
365/// [C++] initializer:
366/// [C++] '=' initializer-clause
367/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000368/// [C++0x] '=' 'default' [TODO]
369/// [C++0x] '=' 'delete'
370///
371/// According to the standard grammar, =default and =delete are function
372/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000373///
Douglas Gregore3298aa2009-05-12 21:31:51 +0000374Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
375 // If a simple-asm-expr is present, parse it.
376 if (Tok.is(tok::kw_asm)) {
377 SourceLocation Loc;
378 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
379 if (AsmLabel.isInvalid()) {
380 SkipUntil(tok::semi, true, true);
381 return DeclPtrTy();
382 }
383
384 D.setAsmLabel(AsmLabel.release());
385 D.SetRangeEnd(Loc);
386 }
387
388 // If attributes are present, parse them.
389 if (Tok.is(tok::kw___attribute)) {
390 SourceLocation Loc;
391 AttributeList *AttrList = ParseAttributes(&Loc);
392 D.AddAttributes(AttrList, Loc);
393 }
394
395 // Inform the current actions module that we just parsed this declarator.
396 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
397
398 // Parse declarator '=' initializer.
399 if (Tok.is(tok::equal)) {
400 ConsumeToken();
401 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
402 SourceLocation DelLoc = ConsumeToken();
403 Actions.SetDeclDeleted(ThisDecl, DelLoc);
404 } else {
405 OwningExprResult Init(ParseInitializer());
406 if (Init.isInvalid()) {
407 SkipUntil(tok::semi, true, true);
408 return DeclPtrTy();
409 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000410 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000411 }
412 } else if (Tok.is(tok::l_paren)) {
413 // Parse C++ direct initializer: '(' expression-list ')'
414 SourceLocation LParenLoc = ConsumeParen();
415 ExprVector Exprs(Actions);
416 CommaLocsTy CommaLocs;
417
418 if (ParseExpressionList(Exprs, CommaLocs)) {
419 SkipUntil(tok::r_paren);
420 } else {
421 // Match the ')'.
422 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
423
424 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
425 "Unexpected number of commas!");
426 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
427 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000428 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000429 }
430 } else {
431 Actions.ActOnUninitializedDecl(ThisDecl);
432 }
433
434 return ThisDecl;
435}
436
437/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
438/// parsing 'declaration-specifiers declarator'. This method is split out this
439/// way to handle the ambiguity between top-level function-definitions and
440/// declarations.
441///
442/// init-declarator-list: [C99 6.7]
443/// init-declarator
444/// init-declarator-list ',' init-declarator
445///
446/// According to the standard grammar, =default and =delete are function
447/// definitions, but that definitely doesn't fit with the parser here.
448///
Chris Lattnera17991f2009-03-29 16:50:03 +0000449Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000450ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000451 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
452 // that we parse together here.
453 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000454
455 // At this point, we know that it is not a function definition. Parse the
456 // rest of the init-declarator-list.
457 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000458 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
459 if (ThisDecl.get())
460 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000461
Chris Lattner4b009652007-07-25 00:24:17 +0000462 // If we don't have a comma, it is either the end of the list (a ';') or an
463 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000464 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000465 break;
466
467 // Consume the comma.
468 ConsumeToken();
469
470 // Parse the next declarator.
471 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000472
473 // Accept attributes in an init-declarator. In the first declarator in a
474 // declaration, these would be part of the declspec. In subsequent
475 // declarators, they become part of the declarator itself, so that they
476 // don't apply to declarators after *this* one. Examples:
477 // short __attribute__((common)) var; -> declspec
478 // short var __attribute__((common)); -> declarator
479 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000480 if (Tok.is(tok::kw___attribute)) {
481 SourceLocation Loc;
482 AttributeList *AttrList = ParseAttributes(&Loc);
483 D.AddAttributes(AttrList, Loc);
484 }
Chris Lattner926cf542008-10-20 04:57:38 +0000485
Chris Lattner4b009652007-07-25 00:24:17 +0000486 ParseDeclarator(D);
487 }
488
Eli Friedman4d57af22009-05-29 01:49:24 +0000489 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
490 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000491 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000492}
493
494/// ParseSpecifierQualifierList
495/// specifier-qualifier-list:
496/// type-specifier specifier-qualifier-list[opt]
497/// type-qualifier specifier-qualifier-list[opt]
498/// [GNU] attributes specifier-qualifier-list[opt]
499///
500void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
501 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
502 /// parse declaration-specifiers and complain about extra stuff.
503 ParseDeclarationSpecifiers(DS);
504
505 // Validate declspec for type-name.
506 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000507 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
508 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000509 Diag(Tok, diag::err_typename_requires_specqual);
510
511 // Issue diagnostic and remove storage class if present.
512 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
513 if (DS.getStorageClassSpecLoc().isValid())
514 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
515 else
516 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
517 DS.ClearStorageClassSpecs();
518 }
519
520 // Issue diagnostic and remove function specfier if present.
521 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000522 if (DS.isInlineSpecified())
523 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
524 if (DS.isVirtualSpecified())
525 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
526 if (DS.isExplicitSpecified())
527 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000528 DS.ClearFunctionSpecs();
529 }
530}
531
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000532/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
533/// specified token is valid after the identifier in a declarator which
534/// immediately follows the declspec. For example, these things are valid:
535///
536/// int x [ 4]; // direct-declarator
537/// int x ( int y); // direct-declarator
538/// int(int x ) // direct-declarator
539/// int x ; // simple-declaration
540/// int x = 17; // init-declarator-list
541/// int x , y; // init-declarator-list
542/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000543/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000544/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000545///
546/// This is not, because 'x' does not immediately follow the declspec (though
547/// ')' happens to be valid anyway).
548/// int (x)
549///
550static bool isValidAfterIdentifierInDeclarator(const Token &T) {
551 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
552 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000553 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000554}
555
Chris Lattner82353c62009-04-14 21:34:55 +0000556
557/// ParseImplicitInt - This method is called when we have an non-typename
558/// identifier in a declspec (which normally terminates the decl spec) when
559/// the declspec has no type specifier. In this case, the declspec is either
560/// malformed or is "implicit int" (in K&R and C89).
561///
562/// This method handles diagnosing this prettily and returns false if the
563/// declspec is done being processed. If it recovers and thinks there may be
564/// other pieces of declspec after it, it returns true.
565///
Chris Lattner52cd7622009-04-14 22:17:06 +0000566bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000567 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000568 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000569 assert(Tok.is(tok::identifier) && "should have identifier");
570
Chris Lattner82353c62009-04-14 21:34:55 +0000571 SourceLocation Loc = Tok.getLocation();
572 // If we see an identifier that is not a type name, we normally would
573 // parse it as the identifer being declared. However, when a typename
574 // is typo'd or the definition is not included, this will incorrectly
575 // parse the typename as the identifier name and fall over misparsing
576 // later parts of the diagnostic.
577 //
578 // As such, we try to do some look-ahead in cases where this would
579 // otherwise be an "implicit-int" case to see if this is invalid. For
580 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
581 // an identifier with implicit int, we'd get a parse error because the
582 // next token is obviously invalid for a type. Parse these as a case
583 // with an invalid type specifier.
584 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
585
586 // Since we know that this either implicit int (which is rare) or an
587 // error, we'd do lookahead to try to do better recovery.
588 if (isValidAfterIdentifierInDeclarator(NextToken())) {
589 // If this token is valid for implicit int, e.g. "static x = 4", then
590 // we just avoid eating the identifier, so it will be parsed as the
591 // identifier in the declarator.
592 return false;
593 }
594
595 // Otherwise, if we don't consume this token, we are going to emit an
596 // error anyway. Try to recover from various common problems. Check
597 // to see if this was a reference to a tag name without a tag specified.
598 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000599 //
600 // C++ doesn't need this, and isTagName doesn't take SS.
601 if (SS == 0) {
602 const char *TagName = 0;
603 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000604
Chris Lattner82353c62009-04-14 21:34:55 +0000605 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
606 default: break;
607 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
608 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
609 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
610 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
611 }
Chris Lattner82353c62009-04-14 21:34:55 +0000612
Chris Lattner52cd7622009-04-14 22:17:06 +0000613 if (TagName) {
614 Diag(Loc, diag::err_use_of_tag_name_without_tag)
615 << Tok.getIdentifierInfo() << TagName
616 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
617
618 // Parse this as a tag as if the missing tag were present.
619 if (TagKind == tok::kw_enum)
620 ParseEnumSpecifier(Loc, DS, AS);
621 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000622 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000623 return true;
624 }
Chris Lattner82353c62009-04-14 21:34:55 +0000625 }
626
627 // Since this is almost certainly an invalid type name, emit a
628 // diagnostic that says it, eat the token, and mark the declspec as
629 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000630 SourceRange R;
631 if (SS) R = SS->getRange();
632
633 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000634 const char *PrevSpec;
635 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
636 DS.SetRangeEnd(Tok.getLocation());
637 ConsumeToken();
638
639 // TODO: Could inject an invalid typedef decl in an enclosing scope to
640 // avoid rippling error messages on subsequent uses of the same type,
641 // could be useful if #include was forgotten.
642 return false;
643}
644
Chris Lattner4b009652007-07-25 00:24:17 +0000645/// ParseDeclarationSpecifiers
646/// declaration-specifiers: [C99 6.7]
647/// storage-class-specifier declaration-specifiers[opt]
648/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000649/// [C99] function-specifier declaration-specifiers[opt]
650/// [GNU] attributes declaration-specifiers[opt]
651///
652/// storage-class-specifier: [C99 6.7.1]
653/// 'typedef'
654/// 'extern'
655/// 'static'
656/// 'auto'
657/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000658/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000659/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000660/// function-specifier: [C99 6.7.4]
661/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000662/// [C++] 'virtual'
663/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000664/// 'friend': [C++ dcl.friend]
665
Chris Lattner4b009652007-07-25 00:24:17 +0000666///
Douglas Gregor52473432008-12-24 02:52:09 +0000667void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000668 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000669 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000670 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000671 while (1) {
672 int isInvalid = false;
673 const char *PrevSpec = 0;
674 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000675
Chris Lattner4b009652007-07-25 00:24:17 +0000676 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000677 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000678 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000679 // If this is not a declaration specifier token, we're done reading decl
680 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000681 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000682 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000683
684 case tok::coloncolon: // ::foo::bar
685 // Annotate C++ scope specifiers. If we get one, loop.
686 if (TryAnnotateCXXScopeToken())
687 continue;
688 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000689
690 case tok::annot_cxxscope: {
691 if (DS.hasTypeSpecifier())
692 goto DoneWithDeclSpec;
693
694 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000695 Token Next = NextToken();
696 if (Next.is(tok::annot_template_id) &&
697 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000698 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000699 // We have a qualified template-id, e.g., N::A<int>
700 CXXScopeSpec SS;
701 ParseOptionalCXXScopeSpecifier(SS);
702 assert(Tok.is(tok::annot_template_id) &&
703 "ParseOptionalCXXScopeSpecifier not working");
704 AnnotateTemplateIdTokenAsType(&SS);
705 continue;
706 }
707
708 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000709 goto DoneWithDeclSpec;
710
711 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000712 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000713 SS.setRange(Tok.getAnnotationRange());
714
715 // If the next token is the name of the class type that the C++ scope
716 // denotes, followed by a '(', then this is a constructor declaration.
717 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000718 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000719 CurScope, &SS) &&
720 GetLookAheadToken(2).is(tok::l_paren))
721 goto DoneWithDeclSpec;
722
Douglas Gregor1075a162009-02-04 17:00:24 +0000723 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
724 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000725
Chris Lattner52cd7622009-04-14 22:17:06 +0000726 // If the referenced identifier is not a type, then this declspec is
727 // erroneous: We already checked about that it has no type specifier, and
728 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
729 // typename.
730 if (TypeRep == 0) {
731 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000732 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000733 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000734 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000735
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000736 ConsumeToken(); // The C++ scope.
737
Douglas Gregora60c62e2009-02-09 15:09:02 +0000738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000739 TypeRep);
740 if (isInvalid)
741 break;
742
743 DS.SetRangeEnd(Tok.getLocation());
744 ConsumeToken(); // The typename.
745
746 continue;
747 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000748
749 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000750 if (Tok.getAnnotationValue())
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
752 Tok.getAnnotationValue());
753 else
754 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000755 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
756 ConsumeToken(); // The typename
757
758 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
759 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
760 // Objective-C interface. If we don't have Objective-C or a '<', this is
761 // just a normal reference to a typedef name.
762 if (!Tok.is(tok::less) || !getLang().ObjC1)
763 continue;
764
765 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000766 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000767 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
768 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
769
770 DS.SetRangeEnd(EndProtoLoc);
771 continue;
772 }
773
Chris Lattnerfda18db2008-07-26 01:18:38 +0000774 // typedef-name
775 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000776 // In C++, check to see if this is a scope specifier like foo::bar::, if
777 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000778 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
779 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000780
Chris Lattnerfda18db2008-07-26 01:18:38 +0000781 // This identifier can only be a typedef name if we haven't already seen
782 // a type-specifier. Without this check we misparse:
783 // typedef int X; struct Y { short X; }; as 'short int'.
784 if (DS.hasTypeSpecifier())
785 goto DoneWithDeclSpec;
786
787 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000788 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
789 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000790
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000791 // If this is not a typedef name, don't parse it as part of the declspec,
792 // it must be an implicit int or an error.
793 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000794 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000795 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000796 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000797
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000798 // C++: If the identifier is actually the name of the class type
799 // being defined and the next token is a '(', then this is a
800 // constructor declaration. We're done with the decl-specifiers
801 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000802 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000803 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
804 NextToken().getKind() == tok::l_paren)
805 goto DoneWithDeclSpec;
806
Douglas Gregora60c62e2009-02-09 15:09:02 +0000807 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000808 TypeRep);
809 if (isInvalid)
810 break;
811
812 DS.SetRangeEnd(Tok.getLocation());
813 ConsumeToken(); // The identifier
814
815 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
816 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
817 // Objective-C interface. If we don't have Objective-C or a '<', this is
818 // just a normal reference to a typedef name.
819 if (!Tok.is(tok::less) || !getLang().ObjC1)
820 continue;
821
822 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000823 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000824 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000825 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000826
827 DS.SetRangeEnd(EndProtoLoc);
828
Steve Narofff7683302008-09-22 10:28:57 +0000829 // Need to support trailing type qualifiers (e.g. "id<p> const").
830 // If a type specifier follows, it will be diagnosed elsewhere.
831 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000832 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000833
834 // type-name
835 case tok::annot_template_id: {
836 TemplateIdAnnotation *TemplateId
837 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000838 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000839 // This template-id does not refer to a type name, so we're
840 // done with the type-specifiers.
841 goto DoneWithDeclSpec;
842 }
843
844 // Turn the template-id annotation token into a type annotation
845 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000846 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000847 continue;
848 }
849
Chris Lattner4b009652007-07-25 00:24:17 +0000850 // GNU attributes support.
851 case tok::kw___attribute:
852 DS.AddAttributes(ParseAttributes());
853 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000854
855 // Microsoft declspec support.
856 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000857 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000858 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000859
Steve Naroffedd04d52008-12-25 14:16:32 +0000860 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000861 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000862 // FIXME: Add handling here!
863 break;
864
865 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000866 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000867 case tok::kw___cdecl:
868 case tok::kw___stdcall:
869 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000870 DS.AddAttributes(ParseMicrosoftTypeAttributes());
871 continue;
872
Chris Lattner4b009652007-07-25 00:24:17 +0000873 // storage-class-specifier
874 case tok::kw_typedef:
875 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
876 break;
877 case tok::kw_extern:
878 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000879 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000880 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
881 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000882 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000883 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
884 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000885 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000886 case tok::kw_static:
887 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000888 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000889 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
890 break;
891 case tok::kw_auto:
892 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
893 break;
894 case tok::kw_register:
895 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
896 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000897 case tok::kw_mutable:
898 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
899 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000900 case tok::kw___thread:
901 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
902 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000903
Chris Lattner4b009652007-07-25 00:24:17 +0000904 // function-specifier
905 case tok::kw_inline:
906 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
907 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000908 case tok::kw_virtual:
909 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
910 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000911 case tok::kw_explicit:
912 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
913 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000914
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000915 // friend
916 case tok::kw_friend:
917 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
918 break;
919
Chris Lattnerc297b722009-01-21 19:48:37 +0000920 // type-specifier
921 case tok::kw_short:
922 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
923 break;
924 case tok::kw_long:
925 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
926 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
927 else
928 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
929 break;
930 case tok::kw_signed:
931 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
932 break;
933 case tok::kw_unsigned:
934 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
935 break;
936 case tok::kw__Complex:
937 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
938 break;
939 case tok::kw__Imaginary:
940 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
941 break;
942 case tok::kw_void:
943 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
944 break;
945 case tok::kw_char:
946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
947 break;
948 case tok::kw_int:
949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
950 break;
951 case tok::kw_float:
952 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
953 break;
954 case tok::kw_double:
955 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
956 break;
957 case tok::kw_wchar_t:
958 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
959 break;
960 case tok::kw_bool:
961 case tok::kw__Bool:
962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
963 break;
964 case tok::kw__Decimal32:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
966 break;
967 case tok::kw__Decimal64:
968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
969 break;
970 case tok::kw__Decimal128:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
972 break;
973
974 // class-specifier:
975 case tok::kw_class:
976 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000977 case tok::kw_union: {
978 tok::TokenKind Kind = Tok.getKind();
979 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000980 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000981 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000982 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000983
984 // enum-specifier:
985 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000986 ConsumeToken();
987 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000988 continue;
989
990 // cv-qualifier:
991 case tok::kw_const:
992 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
993 break;
994 case tok::kw_volatile:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
996 getLang())*2;
997 break;
998 case tok::kw_restrict:
999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1000 getLang())*2;
1001 break;
1002
Douglas Gregord3022602009-03-27 23:10:48 +00001003 // C++ typename-specifier:
1004 case tok::kw_typename:
1005 if (TryAnnotateTypeOrScopeToken())
1006 continue;
1007 break;
1008
Chris Lattnerc297b722009-01-21 19:48:37 +00001009 // GNU typeof support.
1010 case tok::kw_typeof:
1011 ParseTypeofSpecifier(DS);
1012 continue;
1013
Steve Naroff5f0466b2008-06-05 00:02:44 +00001014 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001015 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001016 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1017 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001018 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001019 goto DoneWithDeclSpec;
1020
1021 {
1022 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001023 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001024 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +00001025 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001026 DS.SetRangeEnd(EndProtoLoc);
1027
Chris Lattnerf006a222008-11-18 07:48:38 +00001028 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001029 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001030 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001031 // Need to support trailing type qualifiers (e.g. "id<p> const").
1032 // If a type specifier follows, it will be diagnosed elsewhere.
1033 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001034 }
Chris Lattner4b009652007-07-25 00:24:17 +00001035 }
1036 // If the specifier combination wasn't legal, issue a diagnostic.
1037 if (isInvalid) {
1038 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001039 // Pick between error or extwarn.
1040 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1041 : diag::ext_duplicate_declspec;
1042 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001043 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001044 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001045 ConsumeToken();
1046 }
1047}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001048
Chris Lattnerd706dc82009-01-06 06:59:53 +00001049/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001050/// primarily follow the C++ grammar with additions for C99 and GNU,
1051/// which together subsume the C grammar. Note that the C++
1052/// type-specifier also includes the C type-qualifier (for const,
1053/// volatile, and C99 restrict). Returns true if a type-specifier was
1054/// found (and parsed), false otherwise.
1055///
1056/// type-specifier: [C++ 7.1.5]
1057/// simple-type-specifier
1058/// class-specifier
1059/// enum-specifier
1060/// elaborated-type-specifier [TODO]
1061/// cv-qualifier
1062///
1063/// cv-qualifier: [C++ 7.1.5.1]
1064/// 'const'
1065/// 'volatile'
1066/// [C99] 'restrict'
1067///
1068/// simple-type-specifier: [ C++ 7.1.5.2]
1069/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1070/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1071/// 'char'
1072/// 'wchar_t'
1073/// 'bool'
1074/// 'short'
1075/// 'int'
1076/// 'long'
1077/// 'signed'
1078/// 'unsigned'
1079/// 'float'
1080/// 'double'
1081/// 'void'
1082/// [C99] '_Bool'
1083/// [C99] '_Complex'
1084/// [C99] '_Imaginary' // Removed in TC2?
1085/// [GNU] '_Decimal32'
1086/// [GNU] '_Decimal64'
1087/// [GNU] '_Decimal128'
1088/// [GNU] typeof-specifier
1089/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1090/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +00001091bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1092 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001093 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001094 SourceLocation Loc = Tok.getLocation();
1095
1096 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001097 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001098 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001099 // Annotate typenames and C++ scope specifiers. If we get one, just
1100 // recurse to handle whatever we get.
1101 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001102 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001103 // Otherwise, not a type specifier.
1104 return false;
1105 case tok::coloncolon: // ::foo::bar
1106 if (NextToken().is(tok::kw_new) || // ::new
1107 NextToken().is(tok::kw_delete)) // ::delete
1108 return false;
1109
1110 // Annotate typenames and C++ scope specifiers. If we get one, just
1111 // recurse to handle whatever we get.
1112 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001113 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001114 // Otherwise, not a type specifier.
1115 return false;
1116
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001117 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001118 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001119 if (Tok.getAnnotationValue())
1120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1121 Tok.getAnnotationValue());
1122 else
1123 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001124 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1125 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001126
1127 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1128 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1129 // Objective-C interface. If we don't have Objective-C or a '<', this is
1130 // just a normal reference to a typedef name.
1131 if (!Tok.is(tok::less) || !getLang().ObjC1)
1132 return true;
1133
1134 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001135 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001136 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1137 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1138
1139 DS.SetRangeEnd(EndProtoLoc);
1140 return true;
1141 }
1142
1143 case tok::kw_short:
1144 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1145 break;
1146 case tok::kw_long:
1147 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1148 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1149 else
1150 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1151 break;
1152 case tok::kw_signed:
1153 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1154 break;
1155 case tok::kw_unsigned:
1156 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1157 break;
1158 case tok::kw__Complex:
1159 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1160 break;
1161 case tok::kw__Imaginary:
1162 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1163 break;
1164 case tok::kw_void:
1165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1166 break;
1167 case tok::kw_char:
1168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1169 break;
1170 case tok::kw_int:
1171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1172 break;
1173 case tok::kw_float:
1174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1175 break;
1176 case tok::kw_double:
1177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1178 break;
1179 case tok::kw_wchar_t:
1180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1181 break;
1182 case tok::kw_bool:
1183 case tok::kw__Bool:
1184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1185 break;
1186 case tok::kw__Decimal32:
1187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1188 break;
1189 case tok::kw__Decimal64:
1190 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1191 break;
1192 case tok::kw__Decimal128:
1193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1194 break;
1195
1196 // class-specifier:
1197 case tok::kw_class:
1198 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001199 case tok::kw_union: {
1200 tok::TokenKind Kind = Tok.getKind();
1201 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001202 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001203 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001204 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001205
1206 // enum-specifier:
1207 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001208 ConsumeToken();
1209 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001210 return true;
1211
1212 // cv-qualifier:
1213 case tok::kw_const:
1214 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1215 getLang())*2;
1216 break;
1217 case tok::kw_volatile:
1218 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1219 getLang())*2;
1220 break;
1221 case tok::kw_restrict:
1222 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1223 getLang())*2;
1224 break;
1225
1226 // GNU typeof support.
1227 case tok::kw_typeof:
1228 ParseTypeofSpecifier(DS);
1229 return true;
1230
Eli Friedman891d82f2009-06-08 23:27:34 +00001231 case tok::kw___ptr64:
1232 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001233 case tok::kw___cdecl:
1234 case tok::kw___stdcall:
1235 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001236 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001237 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001238
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001239 default:
1240 // Not a type-specifier; do nothing.
1241 return false;
1242 }
1243
1244 // If the specifier combination wasn't legal, issue a diagnostic.
1245 if (isInvalid) {
1246 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001247 // Pick between error or extwarn.
1248 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1249 : diag::ext_duplicate_declspec;
1250 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001251 }
1252 DS.SetRangeEnd(Tok.getLocation());
1253 ConsumeToken(); // whatever we parsed above.
1254 return true;
1255}
Chris Lattner4b009652007-07-25 00:24:17 +00001256
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001257/// ParseStructDeclaration - Parse a struct declaration without the terminating
1258/// semicolon.
1259///
Chris Lattner4b009652007-07-25 00:24:17 +00001260/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001261/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001262/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001263/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001264/// struct-declarator-list:
1265/// struct-declarator
1266/// struct-declarator-list ',' struct-declarator
1267/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1268/// struct-declarator:
1269/// declarator
1270/// [GNU] declarator attributes[opt]
1271/// declarator[opt] ':' constant-expression
1272/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1273///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001274void Parser::
1275ParseStructDeclaration(DeclSpec &DS,
1276 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001277 if (Tok.is(tok::kw___extension__)) {
1278 // __extension__ silences extension warnings in the subexpression.
1279 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001280 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001281 return ParseStructDeclaration(DS, Fields);
1282 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001283
1284 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001285 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001286 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001287
Douglas Gregorb748fc52009-01-12 22:49:06 +00001288 // If there are no declarators, this is a free-standing declaration
1289 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001290 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001291 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001292 return;
1293 }
1294
1295 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001296 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001297 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001298 FieldDeclarator &DeclaratorInfo = Fields.back();
1299
Steve Naroffa9adf112007-08-20 22:28:22 +00001300 /// struct-declarator: declarator
1301 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001302 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001303 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001304
Chris Lattner34a01ad2007-10-09 17:33:22 +00001305 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001306 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001307 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001308 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001309 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001310 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001311 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001312 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001313
Steve Naroffa9adf112007-08-20 22:28:22 +00001314 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001315 if (Tok.is(tok::kw___attribute)) {
1316 SourceLocation Loc;
1317 AttributeList *AttrList = ParseAttributes(&Loc);
1318 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1319 }
1320
Steve Naroffa9adf112007-08-20 22:28:22 +00001321 // If we don't have a comma, it is either the end of the list (a ';')
1322 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001323 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001324 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001325
Steve Naroffa9adf112007-08-20 22:28:22 +00001326 // Consume the comma.
1327 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001328
Steve Naroffa9adf112007-08-20 22:28:22 +00001329 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001330 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001331
Steve Naroffa9adf112007-08-20 22:28:22 +00001332 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001333 if (Tok.is(tok::kw___attribute)) {
1334 SourceLocation Loc;
1335 AttributeList *AttrList = ParseAttributes(&Loc);
1336 Fields.back().D.AddAttributes(AttrList, Loc);
1337 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001338 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001339}
1340
1341/// ParseStructUnionBody
1342/// struct-contents:
1343/// struct-declaration-list
1344/// [EXT] empty
1345/// [GNU] "struct-declaration-list" without terminatoring ';'
1346/// struct-declaration-list:
1347/// struct-declaration
1348/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001349/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001350///
Chris Lattner4b009652007-07-25 00:24:17 +00001351void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001352 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001353 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1354 PP.getSourceManager(),
1355 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001356
Chris Lattner4b009652007-07-25 00:24:17 +00001357 SourceLocation LBraceLoc = ConsumeBrace();
1358
Douglas Gregorcab994d2009-01-09 22:42:13 +00001359 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001360 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1361
Chris Lattner4b009652007-07-25 00:24:17 +00001362 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1363 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001364 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001365 Diag(Tok, diag::ext_empty_struct_union_enum)
1366 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001367
Chris Lattner5261d0c2009-03-28 19:18:32 +00001368 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001369 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1370
Chris Lattner4b009652007-07-25 00:24:17 +00001371 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001372 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001373 // Each iteration of this loop reads one struct-declaration.
1374
1375 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001376 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001377 Diag(Tok, diag::ext_extra_struct_semi)
1378 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001379 ConsumeToken();
1380 continue;
1381 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001382
1383 // Parse all the comma separated declarators.
1384 DeclSpec DS;
1385 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001386 if (!Tok.is(tok::at)) {
1387 ParseStructDeclaration(DS, FieldDeclarators);
1388
1389 // Convert them all to fields.
1390 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1391 FieldDeclarator &FD = FieldDeclarators[i];
1392 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001393 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1394 DS.getSourceRange().getBegin(),
1395 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001396 FieldDecls.push_back(Field);
1397 }
1398 } else { // Handle @defs
1399 ConsumeToken();
1400 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1401 Diag(Tok, diag::err_unexpected_at);
1402 SkipUntil(tok::semi, true, true);
1403 continue;
1404 }
1405 ConsumeToken();
1406 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1407 if (!Tok.is(tok::identifier)) {
1408 Diag(Tok, diag::err_expected_ident);
1409 SkipUntil(tok::semi, true, true);
1410 continue;
1411 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001412 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001413 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1414 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001415 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1416 ConsumeToken();
1417 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1418 }
Chris Lattner4b009652007-07-25 00:24:17 +00001419
Chris Lattner34a01ad2007-10-09 17:33:22 +00001420 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001421 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001422 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001423 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001424 break;
1425 } else {
1426 Diag(Tok, diag::err_expected_semi_decl_list);
1427 // Skip to end of block or statement
1428 SkipUntil(tok::r_brace, true, true);
1429 }
1430 }
1431
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001432 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001433
Chris Lattner4b009652007-07-25 00:24:17 +00001434 AttributeList *AttrList = 0;
1435 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001436 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001437 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001438
1439 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001440 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001441 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001442 AttrList);
1443 StructScope.Exit();
1444 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001445}
1446
1447
1448/// ParseEnumSpecifier
1449/// enum-specifier: [C99 6.7.2.2]
1450/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001451///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001452/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1453/// '}' attributes[opt]
1454/// 'enum' identifier
1455/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001456///
1457/// [C++] elaborated-type-specifier:
1458/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1459///
Chris Lattner197b4342009-04-12 21:49:30 +00001460void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1461 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001462 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001463
1464 AttributeList *Attr = 0;
1465 // If attributes exist after tag, parse them.
1466 if (Tok.is(tok::kw___attribute))
1467 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001468
1469 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001470 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001471 if (Tok.isNot(tok::identifier)) {
1472 Diag(Tok, diag::err_expected_ident);
1473 if (Tok.isNot(tok::l_brace)) {
1474 // Has no name and is not a definition.
1475 // Skip the rest of this declarator, up until the comma or semicolon.
1476 SkipUntil(tok::comma, true);
1477 return;
1478 }
1479 }
1480 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001481
1482 // Must have either 'enum name' or 'enum {...}'.
1483 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1484 Diag(Tok, diag::err_expected_ident_lbrace);
1485
1486 // Skip the rest of this declarator, up until the comma or semicolon.
1487 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001488 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001489 }
1490
1491 // If an identifier is present, consume and remember it.
1492 IdentifierInfo *Name = 0;
1493 SourceLocation NameLoc;
1494 if (Tok.is(tok::identifier)) {
1495 Name = Tok.getIdentifierInfo();
1496 NameLoc = ConsumeToken();
1497 }
1498
1499 // There are three options here. If we have 'enum foo;', then this is a
1500 // forward declaration. If we have 'enum foo {...' then this is a
1501 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1502 //
1503 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1504 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1505 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1506 //
1507 Action::TagKind TK;
1508 if (Tok.is(tok::l_brace))
1509 TK = Action::TK_Definition;
1510 else if (Tok.is(tok::semi))
1511 TK = Action::TK_Declaration;
1512 else
1513 TK = Action::TK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001514 bool Owned = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001515 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001516 StartLoc, SS, Name, NameLoc, Attr, AS,
1517 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001518
Chris Lattner34a01ad2007-10-09 17:33:22 +00001519 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001520 ParseEnumBody(StartLoc, TagDecl);
1521
1522 // TODO: semantic analysis on the declspec for enums.
1523 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001524 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +00001525 TagDecl.getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +00001526 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001527}
1528
1529/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1530/// enumerator-list:
1531/// enumerator
1532/// enumerator-list ',' enumerator
1533/// enumerator:
1534/// enumeration-constant
1535/// enumeration-constant '=' constant-expression
1536/// enumeration-constant:
1537/// identifier
1538///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001539void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001540 // Enter the scope of the enum body and start the definition.
1541 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001542 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001543
Chris Lattner4b009652007-07-25 00:24:17 +00001544 SourceLocation LBraceLoc = ConsumeBrace();
1545
Chris Lattnerc9a92452007-08-27 17:24:30 +00001546 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001547 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001548 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001549
Chris Lattner5261d0c2009-03-28 19:18:32 +00001550 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001551
Chris Lattner5261d0c2009-03-28 19:18:32 +00001552 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001553
1554 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001555 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001556 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1557 SourceLocation IdentLoc = ConsumeToken();
1558
1559 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001560 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001561 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001562 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001563 AssignedVal = ParseConstantExpression();
1564 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001565 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001566 }
1567
1568 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001569 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1570 LastEnumConstDecl,
1571 IdentLoc, Ident,
1572 EqualLoc,
1573 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001574 EnumConstantDecls.push_back(EnumConstDecl);
1575 LastEnumConstDecl = EnumConstDecl;
1576
Chris Lattner34a01ad2007-10-09 17:33:22 +00001577 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001578 break;
1579 SourceLocation CommaLoc = ConsumeToken();
1580
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001581 if (Tok.isNot(tok::identifier) &&
1582 !(getLang().C99 || getLang().CPlusPlus0x))
1583 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1584 << getLang().CPlusPlus
1585 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001586 }
1587
1588 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001589 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001590
Mike Stump155750e2009-05-16 07:06:02 +00001591 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001592 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001593
Chris Lattner5261d0c2009-03-28 19:18:32 +00001594 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001595 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001596 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001597 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001598
1599 EnumScope.Exit();
1600 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001601}
1602
1603/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001604/// start of a type-qualifier-list.
1605bool Parser::isTypeQualifier() const {
1606 switch (Tok.getKind()) {
1607 default: return false;
1608 // type-qualifier
1609 case tok::kw_const:
1610 case tok::kw_volatile:
1611 case tok::kw_restrict:
1612 return true;
1613 }
1614}
1615
1616/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001617/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001618bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001619 switch (Tok.getKind()) {
1620 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001621
1622 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001623 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001624 // Annotate typenames and C++ scope specifiers. If we get one, just
1625 // recurse to handle whatever we get.
1626 if (TryAnnotateTypeOrScopeToken())
1627 return isTypeSpecifierQualifier();
1628 // Otherwise, not a type specifier.
1629 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001630
Chris Lattnerb75fde62009-01-04 23:41:41 +00001631 case tok::coloncolon: // ::foo::bar
1632 if (NextToken().is(tok::kw_new) || // ::new
1633 NextToken().is(tok::kw_delete)) // ::delete
1634 return false;
1635
1636 // Annotate typenames and C++ scope specifiers. If we get one, just
1637 // recurse to handle whatever we get.
1638 if (TryAnnotateTypeOrScopeToken())
1639 return isTypeSpecifierQualifier();
1640 // Otherwise, not a type specifier.
1641 return false;
1642
Chris Lattner4b009652007-07-25 00:24:17 +00001643 // GNU attributes support.
1644 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001645 // GNU typeof support.
1646 case tok::kw_typeof:
1647
Chris Lattner4b009652007-07-25 00:24:17 +00001648 // type-specifiers
1649 case tok::kw_short:
1650 case tok::kw_long:
1651 case tok::kw_signed:
1652 case tok::kw_unsigned:
1653 case tok::kw__Complex:
1654 case tok::kw__Imaginary:
1655 case tok::kw_void:
1656 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001657 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001658 case tok::kw_int:
1659 case tok::kw_float:
1660 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001661 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001662 case tok::kw__Bool:
1663 case tok::kw__Decimal32:
1664 case tok::kw__Decimal64:
1665 case tok::kw__Decimal128:
1666
Chris Lattner2e78db32008-04-13 18:59:07 +00001667 // struct-or-union-specifier (C99) or class-specifier (C++)
1668 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001669 case tok::kw_struct:
1670 case tok::kw_union:
1671 // enum-specifier
1672 case tok::kw_enum:
1673
1674 // type-qualifier
1675 case tok::kw_const:
1676 case tok::kw_volatile:
1677 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001678
1679 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001680 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001681 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001682
1683 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1684 case tok::less:
1685 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001686
1687 case tok::kw___cdecl:
1688 case tok::kw___stdcall:
1689 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001690 case tok::kw___w64:
1691 case tok::kw___ptr64:
1692 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001693 }
1694}
1695
1696/// isDeclarationSpecifier() - Return true if the current token is part of a
1697/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001698bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001699 switch (Tok.getKind()) {
1700 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001701
1702 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001703 // Unfortunate hack to support "Class.factoryMethod" notation.
1704 if (getLang().ObjC1 && NextToken().is(tok::period))
1705 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001706 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001707
Douglas Gregord3022602009-03-27 23:10:48 +00001708 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001709 // Annotate typenames and C++ scope specifiers. If we get one, just
1710 // recurse to handle whatever we get.
1711 if (TryAnnotateTypeOrScopeToken())
1712 return isDeclarationSpecifier();
1713 // Otherwise, not a declaration specifier.
1714 return false;
1715 case tok::coloncolon: // ::foo::bar
1716 if (NextToken().is(tok::kw_new) || // ::new
1717 NextToken().is(tok::kw_delete)) // ::delete
1718 return false;
1719
1720 // Annotate typenames and C++ scope specifiers. If we get one, just
1721 // recurse to handle whatever we get.
1722 if (TryAnnotateTypeOrScopeToken())
1723 return isDeclarationSpecifier();
1724 // Otherwise, not a declaration specifier.
1725 return false;
1726
Chris Lattner4b009652007-07-25 00:24:17 +00001727 // storage-class-specifier
1728 case tok::kw_typedef:
1729 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001730 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001731 case tok::kw_static:
1732 case tok::kw_auto:
1733 case tok::kw_register:
1734 case tok::kw___thread:
1735
1736 // type-specifiers
1737 case tok::kw_short:
1738 case tok::kw_long:
1739 case tok::kw_signed:
1740 case tok::kw_unsigned:
1741 case tok::kw__Complex:
1742 case tok::kw__Imaginary:
1743 case tok::kw_void:
1744 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001745 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001746 case tok::kw_int:
1747 case tok::kw_float:
1748 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001749 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001750 case tok::kw__Bool:
1751 case tok::kw__Decimal32:
1752 case tok::kw__Decimal64:
1753 case tok::kw__Decimal128:
1754
Chris Lattner2e78db32008-04-13 18:59:07 +00001755 // struct-or-union-specifier (C99) or class-specifier (C++)
1756 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001757 case tok::kw_struct:
1758 case tok::kw_union:
1759 // enum-specifier
1760 case tok::kw_enum:
1761
1762 // type-qualifier
1763 case tok::kw_const:
1764 case tok::kw_volatile:
1765 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001766
Chris Lattner4b009652007-07-25 00:24:17 +00001767 // function-specifier
1768 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001769 case tok::kw_virtual:
1770 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001771
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001772 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001773 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001774
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001775 // GNU typeof support.
1776 case tok::kw_typeof:
1777
1778 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001779 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001780 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001781
1782 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1783 case tok::less:
1784 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001785
Steve Naroffab1a3632009-01-06 19:34:12 +00001786 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001787 case tok::kw___cdecl:
1788 case tok::kw___stdcall:
1789 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001790 case tok::kw___w64:
1791 case tok::kw___ptr64:
1792 case tok::kw___forceinline:
1793 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001794 }
1795}
1796
1797
1798/// ParseTypeQualifierListOpt
1799/// type-qualifier-list: [C99 6.7.5]
1800/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001801/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001802/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001803/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001804///
Chris Lattner460696f2008-12-18 07:02:59 +00001805void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001806 while (1) {
1807 int isInvalid = false;
1808 const char *PrevSpec = 0;
1809 SourceLocation Loc = Tok.getLocation();
1810
1811 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001812 case tok::kw_const:
1813 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1814 getLang())*2;
1815 break;
1816 case tok::kw_volatile:
1817 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1818 getLang())*2;
1819 break;
1820 case tok::kw_restrict:
1821 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1822 getLang())*2;
1823 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001824 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001825 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001826 case tok::kw___cdecl:
1827 case tok::kw___stdcall:
1828 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001829 if (AttributesAllowed) {
1830 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1831 continue;
1832 }
1833 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001834 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001835 if (AttributesAllowed) {
1836 DS.AddAttributes(ParseAttributes());
1837 continue; // do *not* consume the next token!
1838 }
1839 // otherwise, FALL THROUGH!
1840 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001841 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001842 // If this is not a type-qualifier token, we're done reading type
1843 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001844 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001845 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001846 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001847
Chris Lattner4b009652007-07-25 00:24:17 +00001848 // If the specifier combination wasn't legal, issue a diagnostic.
1849 if (isInvalid) {
1850 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001851 // Pick between error or extwarn.
1852 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1853 : diag::ext_duplicate_declspec;
1854 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001855 }
1856 ConsumeToken();
1857 }
1858}
1859
1860
1861/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1862///
1863void Parser::ParseDeclarator(Declarator &D) {
1864 /// This implements the 'declarator' production in the C grammar, then checks
1865 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001866 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001867}
1868
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001869/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1870/// is parsed by the function passed to it. Pass null, and the direct-declarator
1871/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001872/// ptr-operator production.
1873///
Sebastian Redl75555032009-01-24 21:16:55 +00001874/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1875/// [C] pointer[opt] direct-declarator
1876/// [C++] direct-declarator
1877/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001878///
1879/// pointer: [C99 6.7.5]
1880/// '*' type-qualifier-list[opt]
1881/// '*' type-qualifier-list[opt] pointer
1882///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001883/// ptr-operator:
1884/// '*' cv-qualifier-seq[opt]
1885/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001886/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001887/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001888/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001889/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001890void Parser::ParseDeclaratorInternal(Declarator &D,
1891 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001892
Sebastian Redl75555032009-01-24 21:16:55 +00001893 // C++ member pointers start with a '::' or a nested-name.
1894 // Member pointers get special handling, since there's no place for the
1895 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001896 if (getLang().CPlusPlus &&
1897 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1898 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001899 CXXScopeSpec SS;
1900 if (ParseOptionalCXXScopeSpecifier(SS)) {
1901 if(Tok.isNot(tok::star)) {
1902 // The scope spec really belongs to the direct-declarator.
1903 D.getCXXScopeSpec() = SS;
1904 if (DirectDeclParser)
1905 (this->*DirectDeclParser)(D);
1906 return;
1907 }
1908
1909 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001910 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001911 DeclSpec DS;
1912 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001913 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001914
1915 // Recurse to parse whatever is left.
1916 ParseDeclaratorInternal(D, DirectDeclParser);
1917
1918 // Sema will have to catch (syntactically invalid) pointers into global
1919 // scope. It has to catch pointers into namespace scope anyway.
1920 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001921 Loc, DS.TakeAttributes()),
1922 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001923 return;
1924 }
1925 }
1926
1927 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001928 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001929 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001930 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001931 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001932 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001933 if (DirectDeclParser)
1934 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001935 return;
1936 }
Sebastian Redl75555032009-01-24 21:16:55 +00001937
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001938 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1939 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001940 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001941 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001942
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001943 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001944 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001945 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001946
Chris Lattner4b009652007-07-25 00:24:17 +00001947 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001948 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001949
Chris Lattner4b009652007-07-25 00:24:17 +00001950 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001951 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001952 if (Kind == tok::star)
1953 // Remember that we parsed a pointer type, and remember the type-quals.
1954 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001955 DS.TakeAttributes()),
1956 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001957 else
1958 // Remember that we parsed a Block type, and remember the type-quals.
1959 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001960 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001961 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001962 } else {
1963 // Is a reference
1964 DeclSpec DS;
1965
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001966 // Complain about rvalue references in C++03, but then go on and build
1967 // the declarator.
1968 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1969 Diag(Loc, diag::err_rvalue_reference);
1970
Chris Lattner4b009652007-07-25 00:24:17 +00001971 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1972 // cv-qualifiers are introduced through the use of a typedef or of a
1973 // template type argument, in which case the cv-qualifiers are ignored.
1974 //
1975 // [GNU] Retricted references are allowed.
1976 // [GNU] Attributes on references are allowed.
1977 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001978 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001979
1980 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1981 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1982 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001983 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001984 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1985 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001986 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001987 }
1988
1989 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001990 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001991
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001992 if (D.getNumTypeObjects() > 0) {
1993 // C++ [dcl.ref]p4: There shall be no references to references.
1994 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1995 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001996 if (const IdentifierInfo *II = D.getIdentifier())
1997 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1998 << II;
1999 else
2000 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2001 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002002
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002003 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002004 // can go ahead and build the (technically ill-formed)
2005 // declarator: reference collapsing will take care of it.
2006 }
2007 }
2008
Chris Lattner4b009652007-07-25 00:24:17 +00002009 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002010 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002011 DS.TakeAttributes(),
2012 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002013 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002014 }
2015}
2016
2017/// ParseDirectDeclarator
2018/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002019/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002020/// '(' declarator ')'
2021/// [GNU] '(' attributes declarator ')'
2022/// [C90] direct-declarator '[' constant-expression[opt] ']'
2023/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2024/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2025/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2026/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2027/// direct-declarator '(' parameter-type-list ')'
2028/// direct-declarator '(' identifier-list[opt] ')'
2029/// [GNU] direct-declarator '(' parameter-forward-declarations
2030/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002031/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2032/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002033/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002034///
2035/// declarator-id: [C++ 8]
2036/// id-expression
2037/// '::'[opt] nested-name-specifier[opt] type-name
2038///
2039/// id-expression: [C++ 5.1]
2040/// unqualified-id
2041/// qualified-id [TODO]
2042///
2043/// unqualified-id: [C++ 5.1]
2044/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002045/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002046/// conversion-function-id [TODO]
2047/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002048/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002049///
Chris Lattner4b009652007-07-25 00:24:17 +00002050void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002051 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002052
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002053 if (getLang().CPlusPlus) {
2054 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002055 // ParseDeclaratorInternal might already have parsed the scope.
2056 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2057 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002058 if (afterCXXScope) {
2059 // Change the declaration context for name lookup, until this function
2060 // is exited (and the declarator has been parsed).
2061 DeclScopeObj.EnterDeclaratorScope();
2062 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002063
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002064 if (Tok.is(tok::identifier)) {
2065 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002066
2067 // If this identifier is the name of the current class, it's a
2068 // constructor name.
2069 if (!D.getDeclSpec().hasTypeSpecifier() &&
2070 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2071 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2072 Tok.getLocation(), CurScope),
2073 Tok.getLocation());
2074 // This is a normal identifier.
2075 } else
2076 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002077 ConsumeToken();
2078 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002079 } else if (Tok.is(tok::annot_template_id)) {
2080 TemplateIdAnnotation *TemplateId
2081 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2082
2083 // FIXME: Could this template-id name a constructor?
2084
2085 // FIXME: This is an egregious hack, where we silently ignore
2086 // the specialization (which should be a function template
2087 // specialization name) and use the name instead. This hack
2088 // will go away when we have support for function
2089 // specializations.
2090 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2091 TemplateId->Destroy();
2092 ConsumeToken();
2093 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002094 } else if (Tok.is(tok::kw_operator)) {
2095 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002096 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002097
Douglas Gregor853dd392008-12-26 15:00:45 +00002098 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002099 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2100 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002101 } else {
2102 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002103 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2104 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2105 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002106 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002107 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002108 }
2109 goto PastIdentifier;
2110 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002111 // This should be a C++ destructor.
2112 SourceLocation TildeLoc = ConsumeToken();
2113 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002114 // FIXME: Inaccurate.
2115 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002116 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002117 TypeResult Type = ParseClassName(EndLoc);
2118 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002119 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002120 else
2121 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002122 } else {
2123 Diag(Tok, diag::err_expected_class_name);
2124 D.SetIdentifier(0, TildeLoc);
2125 }
2126 goto PastIdentifier;
2127 }
2128
2129 // If we reached this point, token is not identifier and not '~'.
2130
2131 if (afterCXXScope) {
2132 Diag(Tok, diag::err_expected_unqualified_id);
2133 D.SetIdentifier(0, Tok.getLocation());
2134 D.setInvalidType(true);
2135 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002136 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002137 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002138 }
2139
2140 // If we reached this point, we are either in C/ObjC or the token didn't
2141 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002142 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2143 assert(!getLang().CPlusPlus &&
2144 "There's a C++-specific check for tok::identifier above");
2145 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2146 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2147 ConsumeToken();
2148 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002149 // direct-declarator: '(' declarator ')'
2150 // direct-declarator: '(' attributes declarator ')'
2151 // Example: 'char (*X)' or 'int (*XX)(void)'
2152 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002153 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002154 // This could be something simple like "int" (in which case the declarator
2155 // portion is empty), if an abstract-declarator is allowed.
2156 D.SetIdentifier(0, Tok.getLocation());
2157 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002158 if (D.getContext() == Declarator::MemberContext)
2159 Diag(Tok, diag::err_expected_member_name_or_semi)
2160 << D.getDeclSpec().getSourceRange();
2161 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002162 Diag(Tok, diag::err_expected_unqualified_id);
2163 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002164 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002165 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002166 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002167 }
2168
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002169 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002170 assert(D.isPastIdentifier() &&
2171 "Haven't past the location of the identifier yet?");
2172
2173 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002174 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002175 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2176 // In such a case, check if we actually have a function declarator; if it
2177 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002178 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2179 // When not in file scope, warn for ambiguous function declarators, just
2180 // in case the author intended it as a variable definition.
2181 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2182 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2183 break;
2184 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002185 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002186 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002187 ParseBracketDeclarator(D);
2188 } else {
2189 break;
2190 }
2191 }
2192}
2193
Chris Lattnera0d056d2008-04-06 05:45:57 +00002194/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2195/// only called before the identifier, so these are most likely just grouping
2196/// parens for precedence. If we find that these are actually function
2197/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2198///
2199/// direct-declarator:
2200/// '(' declarator ')'
2201/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002202/// direct-declarator '(' parameter-type-list ')'
2203/// direct-declarator '(' identifier-list[opt] ')'
2204/// [GNU] direct-declarator '(' parameter-forward-declarations
2205/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002206///
2207void Parser::ParseParenDeclarator(Declarator &D) {
2208 SourceLocation StartLoc = ConsumeParen();
2209 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2210
Chris Lattner1f185292008-10-20 02:05:46 +00002211 // Eat any attributes before we look at whether this is a grouping or function
2212 // declarator paren. If this is a grouping paren, the attribute applies to
2213 // the type being built up, for example:
2214 // int (__attribute__(()) *x)(long y)
2215 // If this ends up not being a grouping paren, the attribute applies to the
2216 // first argument, for example:
2217 // int (__attribute__(()) int x)
2218 // In either case, we need to eat any attributes to be able to determine what
2219 // sort of paren this is.
2220 //
2221 AttributeList *AttrList = 0;
2222 bool RequiresArg = false;
2223 if (Tok.is(tok::kw___attribute)) {
2224 AttrList = ParseAttributes();
2225
2226 // We require that the argument list (if this is a non-grouping paren) be
2227 // present even if the attribute list was empty.
2228 RequiresArg = true;
2229 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002230 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002231 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2232 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2233 Tok.is(tok::kw___ptr64)) {
2234 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2235 }
Chris Lattner1f185292008-10-20 02:05:46 +00002236
Chris Lattnera0d056d2008-04-06 05:45:57 +00002237 // If we haven't past the identifier yet (or where the identifier would be
2238 // stored, if this is an abstract declarator), then this is probably just
2239 // grouping parens. However, if this could be an abstract-declarator, then
2240 // this could also be the start of function arguments (consider 'void()').
2241 bool isGrouping;
2242
2243 if (!D.mayOmitIdentifier()) {
2244 // If this can't be an abstract-declarator, this *must* be a grouping
2245 // paren, because we haven't seen the identifier yet.
2246 isGrouping = true;
2247 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002248 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002249 isDeclarationSpecifier()) { // 'int(int)' is a function.
2250 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2251 // considered to be a type, not a K&R identifier-list.
2252 isGrouping = false;
2253 } else {
2254 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2255 isGrouping = true;
2256 }
2257
2258 // If this is a grouping paren, handle:
2259 // direct-declarator: '(' declarator ')'
2260 // direct-declarator: '(' attributes declarator ')'
2261 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002262 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002263 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002264 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002265 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002266
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002267 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002268 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002269 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002270
2271 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002272 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002273 return;
2274 }
2275
2276 // Okay, if this wasn't a grouping paren, it must be the start of a function
2277 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002278 // identifier (and remember where it would have been), then call into
2279 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002280 D.SetIdentifier(0, Tok.getLocation());
2281
Chris Lattner1f185292008-10-20 02:05:46 +00002282 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002283}
2284
2285/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2286/// declarator D up to a paren, which indicates that we are parsing function
2287/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002288///
Chris Lattner1f185292008-10-20 02:05:46 +00002289/// If AttrList is non-null, then the caller parsed those arguments immediately
2290/// after the open paren - they should be considered to be the first argument of
2291/// a parameter. If RequiresArg is true, then the first argument of the
2292/// function is required to be present and required to not be an identifier
2293/// list.
2294///
Chris Lattner4b009652007-07-25 00:24:17 +00002295/// This method also handles this portion of the grammar:
2296/// parameter-type-list: [C99 6.7.5]
2297/// parameter-list
2298/// parameter-list ',' '...'
2299///
2300/// parameter-list: [C99 6.7.5]
2301/// parameter-declaration
2302/// parameter-list ',' parameter-declaration
2303///
2304/// parameter-declaration: [C99 6.7.5]
2305/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002306/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002307/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002308/// declaration-specifiers abstract-declarator[opt]
2309/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002310/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002311/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2312///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002313/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002314/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002315///
Chris Lattner1f185292008-10-20 02:05:46 +00002316void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2317 AttributeList *AttrList,
2318 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002319 // lparen is already consumed!
2320 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002321
Chris Lattner1f185292008-10-20 02:05:46 +00002322 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002323 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002324 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002325 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002326 delete AttrList;
2327 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002328
Sebastian Redl0c986032009-02-09 18:23:29 +00002329 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002330
2331 // cv-qualifier-seq[opt].
2332 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002333 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002334 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002335 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002336 llvm::SmallVector<TypeTy*, 2> Exceptions;
2337 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002338 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002339 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002340 if (!DS.getSourceRange().getEnd().isInvalid())
2341 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002342
2343 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002344 if (Tok.is(tok::kw_throw)) {
2345 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002346 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002347 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2348 hasAnyExceptionSpec);
2349 assert(Exceptions.size() == ExceptionRanges.size() &&
2350 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002351 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002352 }
2353
Chris Lattner9f7564b2008-04-06 06:57:35 +00002354 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002355 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002356 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002357 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002358 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002359 /*arglist*/ 0, 0,
2360 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002361 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002362 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002363 Exceptions.data(),
2364 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002365 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002366 LParenLoc, D),
2367 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002368 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002369 }
2370
Chris Lattner1f185292008-10-20 02:05:46 +00002371 // Alternatively, this parameter list may be an identifier list form for a
2372 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002373 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002374 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002375 // K&R identifier lists can't have typedefs as identifiers, per
2376 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002377 if (RequiresArg) {
2378 Diag(Tok, diag::err_argument_required_after_attribute);
2379 delete AttrList;
2380 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002381 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2382 // normal declarators, not for abstract-declarators.
2383 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002384 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002385 }
2386
2387 // Finally, a normal, non-empty parameter type list.
2388
2389 // Build up an array of information about the parsed arguments.
2390 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002391
2392 // Enter function-declaration scope, limiting any declarators to the
2393 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002394 ParseScope PrototypeScope(this,
2395 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002396
2397 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002398 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002399 while (1) {
2400 if (Tok.is(tok::ellipsis)) {
2401 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002402 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002403 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002404 }
2405
Chris Lattner9f7564b2008-04-06 06:57:35 +00002406 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002407
Chris Lattner9f7564b2008-04-06 06:57:35 +00002408 // Parse the declaration-specifiers.
2409 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002410
2411 // If the caller parsed attributes for the first argument, add them now.
2412 if (AttrList) {
2413 DS.AddAttributes(AttrList);
2414 AttrList = 0; // Only apply the attributes to the first parameter.
2415 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002416 ParseDeclarationSpecifiers(DS);
2417
Chris Lattner9f7564b2008-04-06 06:57:35 +00002418 // Parse the declarator. This is "PrototypeContext", because we must
2419 // accept either 'declarator' or 'abstract-declarator' here.
2420 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2421 ParseDeclarator(ParmDecl);
2422
2423 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002424 if (Tok.is(tok::kw___attribute)) {
2425 SourceLocation Loc;
2426 AttributeList *AttrList = ParseAttributes(&Loc);
2427 ParmDecl.AddAttributes(AttrList, Loc);
2428 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002429
Chris Lattner9f7564b2008-04-06 06:57:35 +00002430 // Remember this parsed parameter in ParamInfo.
2431 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2432
Douglas Gregor605de8d2008-12-16 21:30:33 +00002433 // DefArgToks is used when the parsing of default arguments needs
2434 // to be delayed.
2435 CachedTokens *DefArgToks = 0;
2436
Chris Lattner9f7564b2008-04-06 06:57:35 +00002437 // If no parameter was specified, verify that *something* was specified,
2438 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002439 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2440 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002441 // Completely missing, emit error.
2442 Diag(DSStart, diag::err_missing_param);
2443 } else {
2444 // Otherwise, we have something. Add it and let semantic analysis try
2445 // to grok it and add the result to the ParamInfo we are building.
2446
2447 // Inform the actions module about the parameter declarator, so it gets
2448 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002449 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002450
2451 // Parse the default argument, if any. We parse the default
2452 // arguments in all dialects; the semantic analysis in
2453 // ActOnParamDefaultArgument will reject the default argument in
2454 // C.
2455 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002456 SourceLocation EqualLoc = Tok.getLocation();
2457
Chris Lattner3e254fb2008-04-08 04:40:51 +00002458 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002459 if (D.getContext() == Declarator::MemberContext) {
2460 // If we're inside a class definition, cache the tokens
2461 // corresponding to the default argument. We'll actually parse
2462 // them when we see the end of the class definition.
2463 // FIXME: Templates will require something similar.
2464 // FIXME: Can we use a smart pointer for Toks?
2465 DefArgToks = new CachedTokens;
2466
2467 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2468 tok::semi, false)) {
2469 delete DefArgToks;
2470 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002471 Actions.ActOnParamDefaultArgumentError(Param);
2472 } else
2473 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002474 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002475 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002476 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002477
2478 OwningExprResult DefArgResult(ParseAssignmentExpression());
2479 if (DefArgResult.isInvalid()) {
2480 Actions.ActOnParamDefaultArgumentError(Param);
2481 SkipUntil(tok::comma, tok::r_paren, true, true);
2482 } else {
2483 // Inform the actions module about the default argument
2484 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002485 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002486 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002487 }
2488 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002489
2490 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002491 ParmDecl.getIdentifierLoc(), Param,
2492 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002493 }
2494
2495 // If the next token is a comma, consume it and keep reading arguments.
2496 if (Tok.isNot(tok::comma)) break;
2497
2498 // Consume the comma.
2499 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002500 }
2501
Chris Lattner9f7564b2008-04-06 06:57:35 +00002502 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002503 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002504
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002505 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002506 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002507
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002508 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002509 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002510 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002511 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002512 llvm::SmallVector<TypeTy*, 2> Exceptions;
2513 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002514 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002515 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002516 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002517 if (!DS.getSourceRange().getEnd().isInvalid())
2518 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002519
2520 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002521 if (Tok.is(tok::kw_throw)) {
2522 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002523 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002524 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2525 hasAnyExceptionSpec);
2526 assert(Exceptions.size() == ExceptionRanges.size() &&
2527 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002528 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002529 }
2530
Chris Lattner4b009652007-07-25 00:24:17 +00002531 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002532 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002533 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002534 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002535 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002536 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002537 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002538 Exceptions.data(),
2539 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002540 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002541 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002542}
2543
Chris Lattner35d9c912008-04-06 06:34:08 +00002544/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2545/// we found a K&R-style identifier list instead of a type argument list. The
2546/// current token is known to be the first identifier in the list.
2547///
2548/// identifier-list: [C99 6.7.5]
2549/// identifier
2550/// identifier-list ',' identifier
2551///
2552void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2553 Declarator &D) {
2554 // Build up an array of information about the parsed arguments.
2555 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2556 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2557
2558 // If there was no identifier specified for the declarator, either we are in
2559 // an abstract-declarator, or we are in a parameter declarator which was found
2560 // to be abstract. In abstract-declarators, identifier lists are not valid:
2561 // diagnose this.
2562 if (!D.getIdentifier())
2563 Diag(Tok, diag::ext_ident_list_in_param);
2564
2565 // Tok is known to be the first identifier in the list. Remember this
2566 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002567 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002568 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002569 Tok.getLocation(),
2570 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002571
Chris Lattner113a56b2008-04-06 06:39:19 +00002572 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002573
2574 while (Tok.is(tok::comma)) {
2575 // Eat the comma.
2576 ConsumeToken();
2577
Chris Lattner113a56b2008-04-06 06:39:19 +00002578 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002579 if (Tok.isNot(tok::identifier)) {
2580 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002581 SkipUntil(tok::r_paren);
2582 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002583 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002584
Chris Lattner35d9c912008-04-06 06:34:08 +00002585 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002586
2587 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002588 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002589 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002590
2591 // Verify that the argument identifier has not already been mentioned.
2592 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002593 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002594 } else {
2595 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002596 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002597 Tok.getLocation(),
2598 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002599 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002600
2601 // Eat the identifier.
2602 ConsumeToken();
2603 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002604
2605 // If we have the closing ')', eat it and we're done.
2606 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2607
Chris Lattner113a56b2008-04-06 06:39:19 +00002608 // Remember that we parsed a function type, and remember the attributes. This
2609 // function type is always a K&R style function type, which is not varargs and
2610 // has no prototype.
2611 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002612 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002613 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002614 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002615 /*exception*/false,
2616 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002617 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002618 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002619}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002620
Chris Lattner4b009652007-07-25 00:24:17 +00002621/// [C90] direct-declarator '[' constant-expression[opt] ']'
2622/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2623/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2624/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2625/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2626void Parser::ParseBracketDeclarator(Declarator &D) {
2627 SourceLocation StartLoc = ConsumeBracket();
2628
Chris Lattner1525c3a2008-12-18 07:27:21 +00002629 // C array syntax has many features, but by-far the most common is [] and [4].
2630 // This code does a fast path to handle some of the most obvious cases.
2631 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002632 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002633 // Remember that we parsed the empty array type.
2634 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002635 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2636 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002637 return;
2638 } else if (Tok.getKind() == tok::numeric_constant &&
2639 GetLookAheadToken(1).is(tok::r_square)) {
2640 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002641 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002642 ConsumeToken();
2643
Sebastian Redl0c986032009-02-09 18:23:29 +00002644 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002645
2646 // If there was an error parsing the assignment-expression, recover.
2647 if (ExprRes.isInvalid())
2648 ExprRes.release(); // Deallocate expr, just use [].
2649
2650 // Remember that we parsed a array type, and remember its features.
2651 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002652 ExprRes.release(), StartLoc),
2653 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002654 return;
2655 }
2656
Chris Lattner4b009652007-07-25 00:24:17 +00002657 // If valid, this location is the position where we read the 'static' keyword.
2658 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002659 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002660 StaticLoc = ConsumeToken();
2661
2662 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002663 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002664 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002665 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002666
2667 // If we haven't already read 'static', check to see if there is one after the
2668 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002669 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002670 StaticLoc = ConsumeToken();
2671
2672 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2673 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002674 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002675
2676 // Handle the case where we have '[*]' as the array size. However, a leading
2677 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2678 // the the token after the star is a ']'. Since stars in arrays are
2679 // infrequent, use of lookahead is not costly here.
2680 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002681 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002682
Chris Lattner306d4df2008-12-18 06:50:14 +00002683 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002684 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002685 StaticLoc = SourceLocation(); // Drop the static.
2686 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002687 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002688 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002689 // Note, in C89, this production uses the constant-expr production instead
2690 // of assignment-expr. The only difference is that assignment-expr allows
2691 // things like '=' and '*='. Sema rejects these in C89 mode because they
2692 // are not i-c-e's, so we don't need to distinguish between the two here.
2693
Chris Lattner4b009652007-07-25 00:24:17 +00002694 // Parse the assignment-expression now.
2695 NumElements = ParseAssignmentExpression();
2696 }
2697
2698 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002699 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002700 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002701 // If the expression was invalid, skip it.
2702 SkipUntil(tok::r_square);
2703 return;
2704 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002705
2706 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2707
Chris Lattner1525c3a2008-12-18 07:27:21 +00002708 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002709 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2710 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002711 NumElements.release(), StartLoc),
2712 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002713}
2714
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002715/// [GNU] typeof-specifier:
2716/// typeof ( expressions )
2717/// typeof ( type-name )
2718/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002719///
2720void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002721 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002722 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002723 SourceLocation StartLoc = ConsumeToken();
2724
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002725 bool isCastExpr;
2726 TypeTy *CastTy;
2727 SourceRange CastRange;
2728 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2729 isCastExpr,
2730 CastTy,
2731 CastRange);
2732
2733 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002734 // FIXME: Not accurate, the range gets one token more than it should.
2735 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002736 else
2737 DS.SetRangeEnd(CastRange.getEnd());
2738
2739 if (isCastExpr) {
2740 if (!CastTy) {
2741 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002742 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002743 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002744
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002745 const char *PrevSpec = 0;
2746 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2747 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2748 CastTy))
2749 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2750 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002751 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002752
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002753 // If we get here, the operand to the typeof was an expresion.
2754 if (Operand.isInvalid()) {
2755 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002756 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002757 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002758
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002759 const char *PrevSpec = 0;
2760 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2761 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2762 Operand.release()))
2763 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002764}