blob: 7153bad5abcd08e6588911e85ffc193829c88c71 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnereaaebc72009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlab197ba2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner04d66662007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102
Chris Lattner04d66662007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner04d66662007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 }
151 } else { // not an identifier
152 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000153 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redla55e52c2008-11-25 22:21:31 +0000160 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 bool ArgExprsOk = true;
162
163 // now parse the list of expressions
164 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000165 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000166 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 ArgExprsOk = false;
168 SkipUntil(tok::r_paren);
169 break;
170 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000171 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
Chris Lattner04d66662007-10-09 17:33:22 +0000173 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 break;
175 ConsumeToken(); // Eat the comma, move to the next argument
176 }
177 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000178 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
181 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +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))
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
200 return CurrAttr;
201}
202
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000212AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000213 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000214
Steve Narofff59e17e2008-12-24 20:59:21 +0000215 ConsumeToken();
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000221 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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 Friedman290eeb02009-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 Narofff59e17e2008-12-24 20:59:21 +0000262}
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264/// ParseDeclaration - Parse a full 'declaration', which consists of
265/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000266/// 'Context' should be a Declarator::TheContext value. This returns the
267/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000268///
269/// declaration: [C99 6.7]
270/// block-declaration ->
271/// simple-declaration
272/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000273/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000274/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000275/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000276/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000277/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000278/// others... [FIXME]
279///
Chris Lattner97144fc2009-04-02 04:16:50 +0000280Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
281 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000282 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000283 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000284 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000285 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000286 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000287 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000288 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000289 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000290 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000291 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000292 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000293 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000294 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000295 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000296 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000298 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000299 }
Chris Lattner682bf922009-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 Lattner8f08cb72007-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 Lattnercd147752009-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 Lattner97144fc2009-04-02 04:16:50 +0000314 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000315 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000322 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000324 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
325 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 }
327
328 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
329 ParseDeclarator(DeclaratorInfo);
330
Chris Lattner23c4b182009-03-29 17:18:04 +0000331 DeclGroupPtrTy DG =
332 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000333
Chris Lattner97144fc2009-04-02 04:16:50 +0000334 DeclEnd = Tok.getLocation();
335
Chris Lattnercd147752009-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 Lattner23c4b182009-03-29 17:18:04 +0000339
340 if (Tok.is(tok::semi)) {
341 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000342 return DG;
343 }
344
Chris Lattner23c4b182009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000351}
352
Douglas Gregor1426e532009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000357///
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000363/// [C++] declarator initializer[opt]
364///
365/// [C++] initializer:
366/// [C++] '=' initializer-clause
367/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000373///
Douglas Gregore542c862009-06-23 23:11:28 +0000374Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
375 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000376 // If a simple-asm-expr is present, parse it.
377 if (Tok.is(tok::kw_asm)) {
378 SourceLocation Loc;
379 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
380 if (AsmLabel.isInvalid()) {
381 SkipUntil(tok::semi, true, true);
382 return DeclPtrTy();
383 }
384
385 D.setAsmLabel(AsmLabel.release());
386 D.SetRangeEnd(Loc);
387 }
388
389 // If attributes are present, parse them.
390 if (Tok.is(tok::kw___attribute)) {
391 SourceLocation Loc;
392 AttributeList *AttrList = ParseAttributes(&Loc);
393 D.AddAttributes(AttrList, Loc);
394 }
395
396 // Inform the current actions module that we just parsed this declarator.
Douglas Gregore542c862009-06-23 23:11:28 +0000397 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
398 Actions.ActOnTemplateDeclarator(CurScope,
399 Action::MultiTemplateParamsArg(Actions,
400 TemplateInfo.TemplateParams->data(),
401 TemplateInfo.TemplateParams->size()),
402 D)
403 : Actions.ActOnDeclarator(CurScope, D);
Douglas Gregor1426e532009-05-12 21:31:51 +0000404
405 // Parse declarator '=' initializer.
406 if (Tok.is(tok::equal)) {
407 ConsumeToken();
408 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
409 SourceLocation DelLoc = ConsumeToken();
410 Actions.SetDeclDeleted(ThisDecl, DelLoc);
411 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000412 if (getLang().CPlusPlus)
413 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
414
Douglas Gregor1426e532009-05-12 21:31:51 +0000415 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000416
417 if (getLang().CPlusPlus)
418 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
419
Douglas Gregor1426e532009-05-12 21:31:51 +0000420 if (Init.isInvalid()) {
421 SkipUntil(tok::semi, true, true);
422 return DeclPtrTy();
423 }
Anders Carlssonf5dcd382009-05-30 21:37:25 +0000424 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000425 }
426 } else if (Tok.is(tok::l_paren)) {
427 // Parse C++ direct initializer: '(' expression-list ')'
428 SourceLocation LParenLoc = ConsumeParen();
429 ExprVector Exprs(Actions);
430 CommaLocsTy CommaLocs;
431
432 if (ParseExpressionList(Exprs, CommaLocs)) {
433 SkipUntil(tok::r_paren);
434 } else {
435 // Match the ')'.
436 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
437
438 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
439 "Unexpected number of commas!");
440 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
441 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000442 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000443 }
444 } else {
445 Actions.ActOnUninitializedDecl(ThisDecl);
446 }
447
448 return ThisDecl;
449}
450
451/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
452/// parsing 'declaration-specifiers declarator'. This method is split out this
453/// way to handle the ambiguity between top-level function-definitions and
454/// declarations.
455///
456/// init-declarator-list: [C99 6.7]
457/// init-declarator
458/// init-declarator-list ',' init-declarator
459///
460/// According to the standard grammar, =default and =delete are function
461/// definitions, but that definitely doesn't fit with the parser here.
462///
Chris Lattner682bf922009-03-29 16:50:03 +0000463Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000464ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000465 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
466 // that we parse together here.
467 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Reid Spencer5f016e22007-07-11 17:01:13 +0000468
469 // At this point, we know that it is not a function definition. Parse the
470 // rest of the init-declarator-list.
471 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000472 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
473 if (ThisDecl.get())
474 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 // If we don't have a comma, it is either the end of the list (a ';') or an
477 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000478 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 break;
480
481 // Consume the comma.
482 ConsumeToken();
483
484 // Parse the next declarator.
485 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000486
487 // Accept attributes in an init-declarator. In the first declarator in a
488 // declaration, these would be part of the declspec. In subsequent
489 // declarators, they become part of the declarator itself, so that they
490 // don't apply to declarators after *this* one. Examples:
491 // short __attribute__((common)) var; -> declspec
492 // short var __attribute__((common)); -> declarator
493 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000494 if (Tok.is(tok::kw___attribute)) {
495 SourceLocation Loc;
496 AttributeList *AttrList = ParseAttributes(&Loc);
497 D.AddAttributes(AttrList, Loc);
498 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000499
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 ParseDeclarator(D);
501 }
502
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000503 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
504 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000505 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000506}
507
508/// ParseSpecifierQualifierList
509/// specifier-qualifier-list:
510/// type-specifier specifier-qualifier-list[opt]
511/// type-qualifier specifier-qualifier-list[opt]
512/// [GNU] attributes specifier-qualifier-list[opt]
513///
514void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
515 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
516 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 ParseDeclarationSpecifiers(DS);
518
519 // Validate declspec for type-name.
520 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000521 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
522 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 Diag(Tok, diag::err_typename_requires_specqual);
524
525 // Issue diagnostic and remove storage class if present.
526 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
527 if (DS.getStorageClassSpecLoc().isValid())
528 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
529 else
530 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
531 DS.ClearStorageClassSpecs();
532 }
533
534 // Issue diagnostic and remove function specfier if present.
535 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000536 if (DS.isInlineSpecified())
537 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
538 if (DS.isVirtualSpecified())
539 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
540 if (DS.isExplicitSpecified())
541 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 DS.ClearFunctionSpecs();
543 }
544}
545
Chris Lattnerc199ab32009-04-12 20:42:31 +0000546/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
547/// specified token is valid after the identifier in a declarator which
548/// immediately follows the declspec. For example, these things are valid:
549///
550/// int x [ 4]; // direct-declarator
551/// int x ( int y); // direct-declarator
552/// int(int x ) // direct-declarator
553/// int x ; // simple-declaration
554/// int x = 17; // init-declarator-list
555/// int x , y; // init-declarator-list
556/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000557/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000558/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000559///
560/// This is not, because 'x' does not immediately follow the declspec (though
561/// ')' happens to be valid anyway).
562/// int (x)
563///
564static bool isValidAfterIdentifierInDeclarator(const Token &T) {
565 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
566 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000567 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000568}
569
Chris Lattnere40c2952009-04-14 21:34:55 +0000570
571/// ParseImplicitInt - This method is called when we have an non-typename
572/// identifier in a declspec (which normally terminates the decl spec) when
573/// the declspec has no type specifier. In this case, the declspec is either
574/// malformed or is "implicit int" (in K&R and C89).
575///
576/// This method handles diagnosing this prettily and returns false if the
577/// declspec is done being processed. If it recovers and thinks there may be
578/// other pieces of declspec after it, it returns true.
579///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000580bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000581 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000582 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000583 assert(Tok.is(tok::identifier) && "should have identifier");
584
Chris Lattnere40c2952009-04-14 21:34:55 +0000585 SourceLocation Loc = Tok.getLocation();
586 // If we see an identifier that is not a type name, we normally would
587 // parse it as the identifer being declared. However, when a typename
588 // is typo'd or the definition is not included, this will incorrectly
589 // parse the typename as the identifier name and fall over misparsing
590 // later parts of the diagnostic.
591 //
592 // As such, we try to do some look-ahead in cases where this would
593 // otherwise be an "implicit-int" case to see if this is invalid. For
594 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
595 // an identifier with implicit int, we'd get a parse error because the
596 // next token is obviously invalid for a type. Parse these as a case
597 // with an invalid type specifier.
598 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
599
600 // Since we know that this either implicit int (which is rare) or an
601 // error, we'd do lookahead to try to do better recovery.
602 if (isValidAfterIdentifierInDeclarator(NextToken())) {
603 // If this token is valid for implicit int, e.g. "static x = 4", then
604 // we just avoid eating the identifier, so it will be parsed as the
605 // identifier in the declarator.
606 return false;
607 }
608
609 // Otherwise, if we don't consume this token, we are going to emit an
610 // error anyway. Try to recover from various common problems. Check
611 // to see if this was a reference to a tag name without a tag specified.
612 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000613 //
614 // C++ doesn't need this, and isTagName doesn't take SS.
615 if (SS == 0) {
616 const char *TagName = 0;
617 tok::TokenKind TagKind = tok::unknown;
Chris Lattnere40c2952009-04-14 21:34:55 +0000618
Chris Lattnere40c2952009-04-14 21:34:55 +0000619 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
620 default: break;
621 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
622 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
623 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
624 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
625 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000626
Chris Lattnerf4382f52009-04-14 22:17:06 +0000627 if (TagName) {
628 Diag(Loc, diag::err_use_of_tag_name_without_tag)
629 << Tok.getIdentifierInfo() << TagName
630 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
631
632 // Parse this as a tag as if the missing tag were present.
633 if (TagKind == tok::kw_enum)
634 ParseEnumSpecifier(Loc, DS, AS);
635 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000636 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000637 return true;
638 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000639 }
640
641 // Since this is almost certainly an invalid type name, emit a
642 // diagnostic that says it, eat the token, and mark the declspec as
643 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000644 SourceRange R;
645 if (SS) R = SS->getRange();
646
647 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000648 const char *PrevSpec;
649 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
650 DS.SetRangeEnd(Tok.getLocation());
651 ConsumeToken();
652
653 // TODO: Could inject an invalid typedef decl in an enclosing scope to
654 // avoid rippling error messages on subsequent uses of the same type,
655 // could be useful if #include was forgotten.
656 return false;
657}
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659/// ParseDeclarationSpecifiers
660/// declaration-specifiers: [C99 6.7]
661/// storage-class-specifier declaration-specifiers[opt]
662/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000663/// [C99] function-specifier declaration-specifiers[opt]
664/// [GNU] attributes declaration-specifiers[opt]
665///
666/// storage-class-specifier: [C99 6.7.1]
667/// 'typedef'
668/// 'extern'
669/// 'static'
670/// 'auto'
671/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000672/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000673/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// function-specifier: [C99 6.7.4]
675/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000676/// [C++] 'virtual'
677/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000678/// 'friend': [C++ dcl.friend]
679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000681void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000682 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerc199ab32009-04-12 20:42:31 +0000683 AccessSpecifier AS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000684 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 while (1) {
686 int isInvalid = false;
687 const char *PrevSpec = 0;
688 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000691 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000692 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // If this is not a declaration specifier token, we're done reading decl
694 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000695 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000697
698 case tok::coloncolon: // ::foo::bar
699 // Annotate C++ scope specifiers. If we get one, loop.
700 if (TryAnnotateCXXScopeToken())
701 continue;
702 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000703
704 case tok::annot_cxxscope: {
705 if (DS.hasTypeSpecifier())
706 goto DoneWithDeclSpec;
707
708 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000709 Token Next = NextToken();
710 if (Next.is(tok::annot_template_id) &&
711 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000712 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000713 // We have a qualified template-id, e.g., N::A<int>
714 CXXScopeSpec SS;
715 ParseOptionalCXXScopeSpecifier(SS);
716 assert(Tok.is(tok::annot_template_id) &&
717 "ParseOptionalCXXScopeSpecifier not working");
718 AnnotateTemplateIdTokenAsType(&SS);
719 continue;
720 }
721
722 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000723 goto DoneWithDeclSpec;
724
725 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000726 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000727 SS.setRange(Tok.getAnnotationRange());
728
729 // If the next token is the name of the class type that the C++ scope
730 // denotes, followed by a '(', then this is a constructor declaration.
731 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000732 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000733 CurScope, &SS) &&
734 GetLookAheadToken(2).is(tok::l_paren))
735 goto DoneWithDeclSpec;
736
Douglas Gregorb696ea32009-02-04 17:00:24 +0000737 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
738 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000739
Chris Lattnerf4382f52009-04-14 22:17:06 +0000740 // If the referenced identifier is not a type, then this declspec is
741 // erroneous: We already checked about that it has no type specifier, and
742 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
743 // typename.
744 if (TypeRep == 0) {
745 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000746 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000747 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000748 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000749
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000750 ConsumeToken(); // The C++ scope.
751
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000753 TypeRep);
754 if (isInvalid)
755 break;
756
757 DS.SetRangeEnd(Tok.getLocation());
758 ConsumeToken(); // The typename.
759
760 continue;
761 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000762
763 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000764 if (Tok.getAnnotationValue())
765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
766 Tok.getAnnotationValue());
767 else
768 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000769 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
770 ConsumeToken(); // The typename
771
772 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
773 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
774 // Objective-C interface. If we don't have Objective-C or a '<', this is
775 // just a normal reference to a typedef name.
776 if (!Tok.is(tok::less) || !getLang().ObjC1)
777 continue;
778
779 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000780 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000781 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
782 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
783
784 DS.SetRangeEnd(EndProtoLoc);
785 continue;
786 }
787
Chris Lattner3bd934a2008-07-26 01:18:38 +0000788 // typedef-name
789 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000790 // In C++, check to see if this is a scope specifier like foo::bar::, if
791 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000792 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
793 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000794
Chris Lattner3bd934a2008-07-26 01:18:38 +0000795 // This identifier can only be a typedef name if we haven't already seen
796 // a type-specifier. Without this check we misparse:
797 // typedef int X; struct Y { short X; }; as 'short int'.
798 if (DS.hasTypeSpecifier())
799 goto DoneWithDeclSpec;
800
801 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000802 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
803 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000804
Chris Lattnerc199ab32009-04-12 20:42:31 +0000805 // If this is not a typedef name, don't parse it as part of the declspec,
806 // it must be an implicit int or an error.
807 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000808 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000809 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000810 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000811
Douglas Gregorb48fe382008-10-31 09:07:45 +0000812 // C++: If the identifier is actually the name of the class type
813 // being defined and the next token is a '(', then this is a
814 // constructor declaration. We're done with the decl-specifiers
815 // and will treat this token as an identifier.
Chris Lattnerc199ab32009-04-12 20:42:31 +0000816 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000817 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
818 NextToken().getKind() == tok::l_paren)
819 goto DoneWithDeclSpec;
820
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000821 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000822 TypeRep);
823 if (isInvalid)
824 break;
825
826 DS.SetRangeEnd(Tok.getLocation());
827 ConsumeToken(); // The identifier
828
829 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
830 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
831 // Objective-C interface. If we don't have Objective-C or a '<', this is
832 // just a normal reference to a typedef name.
833 if (!Tok.is(tok::less) || !getLang().ObjC1)
834 continue;
835
836 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000837 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000838 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000839 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000840
841 DS.SetRangeEnd(EndProtoLoc);
842
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000843 // Need to support trailing type qualifiers (e.g. "id<p> const").
844 // If a type specifier follows, it will be diagnosed elsewhere.
845 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000846 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000847
848 // type-name
849 case tok::annot_template_id: {
850 TemplateIdAnnotation *TemplateId
851 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000852 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000853 // This template-id does not refer to a type name, so we're
854 // done with the type-specifiers.
855 goto DoneWithDeclSpec;
856 }
857
858 // Turn the template-id annotation token into a type annotation
859 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000860 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000861 continue;
862 }
863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 // GNU attributes support.
865 case tok::kw___attribute:
866 DS.AddAttributes(ParseAttributes());
867 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000868
869 // Microsoft declspec support.
870 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000871 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000872 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000873
Steve Naroff239f0732008-12-25 14:16:32 +0000874 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000875 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000876 // FIXME: Add handling here!
877 break;
878
879 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000880 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000881 case tok::kw___cdecl:
882 case tok::kw___stdcall:
883 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000884 DS.AddAttributes(ParseMicrosoftTypeAttributes());
885 continue;
886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 // storage-class-specifier
888 case tok::kw_typedef:
889 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
890 break;
891 case tok::kw_extern:
892 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000893 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
895 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000896 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000897 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
898 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000899 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 case tok::kw_static:
901 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000902 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
904 break;
905 case tok::kw_auto:
906 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
907 break;
908 case tok::kw_register:
909 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
910 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000911 case tok::kw_mutable:
912 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
913 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 case tok::kw___thread:
915 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
916 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000917
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // function-specifier
919 case tok::kw_inline:
920 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
921 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000922 case tok::kw_virtual:
923 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
924 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000925 case tok::kw_explicit:
926 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
927 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000928
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000929 // friend
930 case tok::kw_friend:
931 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
932 break;
933
Chris Lattner80d0c892009-01-21 19:48:37 +0000934 // type-specifier
935 case tok::kw_short:
936 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
937 break;
938 case tok::kw_long:
939 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
940 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
941 else
942 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
943 break;
944 case tok::kw_signed:
945 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
946 break;
947 case tok::kw_unsigned:
948 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
949 break;
950 case tok::kw__Complex:
951 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
952 break;
953 case tok::kw__Imaginary:
954 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
955 break;
956 case tok::kw_void:
957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
958 break;
959 case tok::kw_char:
960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
961 break;
962 case tok::kw_int:
963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
964 break;
965 case tok::kw_float:
966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
967 break;
968 case tok::kw_double:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
970 break;
971 case tok::kw_wchar_t:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
973 break;
974 case tok::kw_bool:
975 case tok::kw__Bool:
976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
977 break;
978 case tok::kw__Decimal32:
979 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
980 break;
981 case tok::kw__Decimal64:
982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
983 break;
984 case tok::kw__Decimal128:
985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
986 break;
987
988 // class-specifier:
989 case tok::kw_class:
990 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +0000991 case tok::kw_union: {
992 tok::TokenKind Kind = Tok.getKind();
993 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000994 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000995 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +0000996 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000997
998 // enum-specifier:
999 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001000 ConsumeToken();
1001 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001002 continue;
1003
1004 // cv-qualifier:
1005 case tok::kw_const:
1006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
1007 break;
1008 case tok::kw_volatile:
1009 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1010 getLang())*2;
1011 break;
1012 case tok::kw_restrict:
1013 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1014 getLang())*2;
1015 break;
1016
Douglas Gregord57959a2009-03-27 23:10:48 +00001017 // C++ typename-specifier:
1018 case tok::kw_typename:
1019 if (TryAnnotateTypeOrScopeToken())
1020 continue;
1021 break;
1022
Chris Lattner80d0c892009-01-21 19:48:37 +00001023 // GNU typeof support.
1024 case tok::kw_typeof:
1025 ParseTypeofSpecifier(DS);
1026 continue;
1027
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001028 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001029 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001030 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1031 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001032 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001033 goto DoneWithDeclSpec;
1034
1035 {
1036 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001037 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +00001038 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +00001039 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +00001040 DS.SetRangeEnd(EndProtoLoc);
1041
Chris Lattner1ab3b962008-11-18 07:48:38 +00001042 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001043 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001044 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001045 // Need to support trailing type qualifiers (e.g. "id<p> const").
1046 // If a type specifier follows, it will be diagnosed elsewhere.
1047 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001048 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 }
1050 // If the specifier combination wasn't legal, issue a diagnostic.
1051 if (isInvalid) {
1052 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001053 // Pick between error or extwarn.
1054 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1055 : diag::ext_duplicate_declspec;
1056 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001058 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 ConsumeToken();
1060 }
1061}
Douglas Gregoradcac882008-12-01 23:54:00 +00001062
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001063/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001064/// primarily follow the C++ grammar with additions for C99 and GNU,
1065/// which together subsume the C grammar. Note that the C++
1066/// type-specifier also includes the C type-qualifier (for const,
1067/// volatile, and C99 restrict). Returns true if a type-specifier was
1068/// found (and parsed), false otherwise.
1069///
1070/// type-specifier: [C++ 7.1.5]
1071/// simple-type-specifier
1072/// class-specifier
1073/// enum-specifier
1074/// elaborated-type-specifier [TODO]
1075/// cv-qualifier
1076///
1077/// cv-qualifier: [C++ 7.1.5.1]
1078/// 'const'
1079/// 'volatile'
1080/// [C99] 'restrict'
1081///
1082/// simple-type-specifier: [ C++ 7.1.5.2]
1083/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1084/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1085/// 'char'
1086/// 'wchar_t'
1087/// 'bool'
1088/// 'short'
1089/// 'int'
1090/// 'long'
1091/// 'signed'
1092/// 'unsigned'
1093/// 'float'
1094/// 'double'
1095/// 'void'
1096/// [C99] '_Bool'
1097/// [C99] '_Complex'
1098/// [C99] '_Imaginary' // Removed in TC2?
1099/// [GNU] '_Decimal32'
1100/// [GNU] '_Decimal64'
1101/// [GNU] '_Decimal128'
1102/// [GNU] typeof-specifier
1103/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1104/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001105bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1106 const char *&PrevSpec,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001107 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001108 SourceLocation Loc = Tok.getLocation();
1109
1110 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001111 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001112 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001113 // Annotate typenames and C++ scope specifiers. If we get one, just
1114 // recurse to handle whatever we get.
1115 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001116 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001117 // Otherwise, not a type specifier.
1118 return false;
1119 case tok::coloncolon: // ::foo::bar
1120 if (NextToken().is(tok::kw_new) || // ::new
1121 NextToken().is(tok::kw_delete)) // ::delete
1122 return false;
1123
1124 // Annotate typenames and C++ scope specifiers. If we get one, just
1125 // recurse to handle whatever we get.
1126 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001127 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001128 // Otherwise, not a type specifier.
1129 return false;
1130
Douglas Gregor12e083c2008-11-07 15:42:26 +00001131 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001132 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001133 if (Tok.getAnnotationValue())
1134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1135 Tok.getAnnotationValue());
1136 else
1137 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001138 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1139 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001140
1141 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1142 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1143 // Objective-C interface. If we don't have Objective-C or a '<', this is
1144 // just a normal reference to a typedef name.
1145 if (!Tok.is(tok::less) || !getLang().ObjC1)
1146 return true;
1147
1148 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001149 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001150 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1151 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1152
1153 DS.SetRangeEnd(EndProtoLoc);
1154 return true;
1155 }
1156
1157 case tok::kw_short:
1158 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1159 break;
1160 case tok::kw_long:
1161 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1162 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1163 else
1164 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1165 break;
1166 case tok::kw_signed:
1167 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1168 break;
1169 case tok::kw_unsigned:
1170 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1171 break;
1172 case tok::kw__Complex:
1173 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1174 break;
1175 case tok::kw__Imaginary:
1176 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1177 break;
1178 case tok::kw_void:
1179 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1180 break;
1181 case tok::kw_char:
1182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1183 break;
1184 case tok::kw_int:
1185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1186 break;
1187 case tok::kw_float:
1188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1189 break;
1190 case tok::kw_double:
1191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1192 break;
1193 case tok::kw_wchar_t:
1194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1195 break;
1196 case tok::kw_bool:
1197 case tok::kw__Bool:
1198 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1199 break;
1200 case tok::kw__Decimal32:
1201 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1202 break;
1203 case tok::kw__Decimal64:
1204 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1205 break;
1206 case tok::kw__Decimal128:
1207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1208 break;
1209
1210 // class-specifier:
1211 case tok::kw_class:
1212 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001213 case tok::kw_union: {
1214 tok::TokenKind Kind = Tok.getKind();
1215 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001216 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001217 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001218 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001219
1220 // enum-specifier:
1221 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001222 ConsumeToken();
1223 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001224 return true;
1225
1226 // cv-qualifier:
1227 case tok::kw_const:
1228 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1229 getLang())*2;
1230 break;
1231 case tok::kw_volatile:
1232 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1233 getLang())*2;
1234 break;
1235 case tok::kw_restrict:
1236 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1237 getLang())*2;
1238 break;
1239
1240 // GNU typeof support.
1241 case tok::kw_typeof:
1242 ParseTypeofSpecifier(DS);
1243 return true;
1244
Eli Friedman290eeb02009-06-08 23:27:34 +00001245 case tok::kw___ptr64:
1246 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001247 case tok::kw___cdecl:
1248 case tok::kw___stdcall:
1249 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001250 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001251 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001252
Douglas Gregor12e083c2008-11-07 15:42:26 +00001253 default:
1254 // Not a type-specifier; do nothing.
1255 return false;
1256 }
1257
1258 // If the specifier combination wasn't legal, issue a diagnostic.
1259 if (isInvalid) {
1260 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001261 // Pick between error or extwarn.
1262 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1263 : diag::ext_duplicate_declspec;
1264 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001265 }
1266 DS.SetRangeEnd(Tok.getLocation());
1267 ConsumeToken(); // whatever we parsed above.
1268 return true;
1269}
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001271/// ParseStructDeclaration - Parse a struct declaration without the terminating
1272/// semicolon.
1273///
Reid Spencer5f016e22007-07-11 17:01:13 +00001274/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001275/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001276/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001277/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001278/// struct-declarator-list:
1279/// struct-declarator
1280/// struct-declarator-list ',' struct-declarator
1281/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1282/// struct-declarator:
1283/// declarator
1284/// [GNU] declarator attributes[opt]
1285/// declarator[opt] ':' constant-expression
1286/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1287///
Chris Lattnere1359422008-04-10 06:46:29 +00001288void Parser::
1289ParseStructDeclaration(DeclSpec &DS,
1290 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001291 if (Tok.is(tok::kw___extension__)) {
1292 // __extension__ silences extension warnings in the subexpression.
1293 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001294 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001295 return ParseStructDeclaration(DS, Fields);
1296 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001297
1298 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001299 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001300 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001301
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001302 // If there are no declarators, this is a free-standing declaration
1303 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001304 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001305 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001306 return;
1307 }
1308
1309 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001310 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001311 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001312 FieldDeclarator &DeclaratorInfo = Fields.back();
1313
Steve Naroff28a7ca82007-08-20 22:28:22 +00001314 /// struct-declarator: declarator
1315 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001316 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001317 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001318
Chris Lattner04d66662007-10-09 17:33:22 +00001319 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001320 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001321 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001322 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001323 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001324 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001325 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001326 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001327
Steve Naroff28a7ca82007-08-20 22:28:22 +00001328 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001329 if (Tok.is(tok::kw___attribute)) {
1330 SourceLocation Loc;
1331 AttributeList *AttrList = ParseAttributes(&Loc);
1332 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1333 }
1334
Steve Naroff28a7ca82007-08-20 22:28:22 +00001335 // If we don't have a comma, it is either the end of the list (a ';')
1336 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001337 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001338 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001339
Steve Naroff28a7ca82007-08-20 22:28:22 +00001340 // Consume the comma.
1341 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001342
Steve Naroff28a7ca82007-08-20 22:28:22 +00001343 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001344 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001345
Steve Naroff28a7ca82007-08-20 22:28:22 +00001346 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001347 if (Tok.is(tok::kw___attribute)) {
1348 SourceLocation Loc;
1349 AttributeList *AttrList = ParseAttributes(&Loc);
1350 Fields.back().D.AddAttributes(AttrList, Loc);
1351 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001352 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001353}
1354
1355/// ParseStructUnionBody
1356/// struct-contents:
1357/// struct-declaration-list
1358/// [EXT] empty
1359/// [GNU] "struct-declaration-list" without terminatoring ';'
1360/// struct-declaration-list:
1361/// struct-declaration
1362/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001363/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001364///
Reid Spencer5f016e22007-07-11 17:01:13 +00001365void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001366 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001367 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1368 PP.getSourceManager(),
1369 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001370
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 SourceLocation LBraceLoc = ConsumeBrace();
1372
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001373 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001374 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1375
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1377 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001378 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001379 Diag(Tok, diag::ext_empty_struct_union_enum)
1380 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001381
Chris Lattnerb28317a2009-03-28 19:18:32 +00001382 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001383 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1384
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001386 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Each iteration of this loop reads one struct-declaration.
1388
1389 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001390 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001391 Diag(Tok, diag::ext_extra_struct_semi)
1392 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 ConsumeToken();
1394 continue;
1395 }
Chris Lattnere1359422008-04-10 06:46:29 +00001396
1397 // Parse all the comma separated declarators.
1398 DeclSpec DS;
1399 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001400 if (!Tok.is(tok::at)) {
1401 ParseStructDeclaration(DS, FieldDeclarators);
1402
1403 // Convert them all to fields.
1404 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1405 FieldDeclarator &FD = FieldDeclarators[i];
1406 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001407 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1408 DS.getSourceRange().getBegin(),
1409 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001410 FieldDecls.push_back(Field);
1411 }
1412 } else { // Handle @defs
1413 ConsumeToken();
1414 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1415 Diag(Tok, diag::err_unexpected_at);
1416 SkipUntil(tok::semi, true, true);
1417 continue;
1418 }
1419 ConsumeToken();
1420 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1421 if (!Tok.is(tok::identifier)) {
1422 Diag(Tok, diag::err_expected_ident);
1423 SkipUntil(tok::semi, true, true);
1424 continue;
1425 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001426 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001427 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1428 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001429 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1430 ConsumeToken();
1431 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1432 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001433
Chris Lattner04d66662007-10-09 17:33:22 +00001434 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001436 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001437 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 break;
1439 } else {
1440 Diag(Tok, diag::err_expected_semi_decl_list);
1441 // Skip to end of block or statement
1442 SkipUntil(tok::r_brace, true, true);
1443 }
1444 }
1445
Steve Naroff60fccee2007-10-29 21:38:07 +00001446 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001447
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 AttributeList *AttrList = 0;
1449 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001450 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001451 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001452
1453 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001454 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001455 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001456 AttrList);
1457 StructScope.Exit();
1458 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001459}
1460
1461
1462/// ParseEnumSpecifier
1463/// enum-specifier: [C99 6.7.2.2]
1464/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001465///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001466/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1467/// '}' attributes[opt]
1468/// 'enum' identifier
1469/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001470///
1471/// [C++] elaborated-type-specifier:
1472/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1473///
Chris Lattner4c97d762009-04-12 21:49:30 +00001474void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1475 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001477
1478 AttributeList *Attr = 0;
1479 // If attributes exist after tag, parse them.
1480 if (Tok.is(tok::kw___attribute))
1481 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001482
1483 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001484 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001485 if (Tok.isNot(tok::identifier)) {
1486 Diag(Tok, diag::err_expected_ident);
1487 if (Tok.isNot(tok::l_brace)) {
1488 // Has no name and is not a definition.
1489 // Skip the rest of this declarator, up until the comma or semicolon.
1490 SkipUntil(tok::comma, true);
1491 return;
1492 }
1493 }
1494 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001495
1496 // Must have either 'enum name' or 'enum {...}'.
1497 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1498 Diag(Tok, diag::err_expected_ident_lbrace);
1499
1500 // Skip the rest of this declarator, up until the comma or semicolon.
1501 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001503 }
1504
1505 // If an identifier is present, consume and remember it.
1506 IdentifierInfo *Name = 0;
1507 SourceLocation NameLoc;
1508 if (Tok.is(tok::identifier)) {
1509 Name = Tok.getIdentifierInfo();
1510 NameLoc = ConsumeToken();
1511 }
1512
1513 // There are three options here. If we have 'enum foo;', then this is a
1514 // forward declaration. If we have 'enum foo {...' then this is a
1515 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1516 //
1517 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1518 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1519 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1520 //
1521 Action::TagKind TK;
1522 if (Tok.is(tok::l_brace))
1523 TK = Action::TK_Definition;
1524 else if (Tok.is(tok::semi))
1525 TK = Action::TK_Declaration;
1526 else
1527 TK = Action::TK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001528 bool Owned = false;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001529 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001530 StartLoc, SS, Name, NameLoc, Attr, AS,
1531 Owned);
Reid Spencer5f016e22007-07-11 17:01:13 +00001532
Chris Lattner04d66662007-10-09 17:33:22 +00001533 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 ParseEnumBody(StartLoc, TagDecl);
1535
1536 // TODO: semantic analysis on the declspec for enums.
1537 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001538 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +00001539 TagDecl.getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001540 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001541}
1542
1543/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1544/// enumerator-list:
1545/// enumerator
1546/// enumerator-list ',' enumerator
1547/// enumerator:
1548/// enumeration-constant
1549/// enumeration-constant '=' constant-expression
1550/// enumeration-constant:
1551/// identifier
1552///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001553void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001554 // Enter the scope of the enum body and start the definition.
1555 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001556 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001557
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 SourceLocation LBraceLoc = ConsumeBrace();
1559
Chris Lattner7946dd32007-08-27 17:24:30 +00001560 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001561 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001562 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001563
Chris Lattnerb28317a2009-03-28 19:18:32 +00001564 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001565
Chris Lattnerb28317a2009-03-28 19:18:32 +00001566 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001567
1568 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001569 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1571 SourceLocation IdentLoc = ConsumeToken();
1572
1573 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001574 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001575 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001577 AssignedVal = ParseConstantExpression();
1578 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 }
1581
1582 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001583 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1584 LastEnumConstDecl,
1585 IdentLoc, Ident,
1586 EqualLoc,
1587 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 EnumConstantDecls.push_back(EnumConstDecl);
1589 LastEnumConstDecl = EnumConstDecl;
1590
Chris Lattner04d66662007-10-09 17:33:22 +00001591 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 break;
1593 SourceLocation CommaLoc = ConsumeToken();
1594
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001595 if (Tok.isNot(tok::identifier) &&
1596 !(getLang().C99 || getLang().CPlusPlus0x))
1597 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1598 << getLang().CPlusPlus
1599 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 }
1601
1602 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001603 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001604
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001605 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001606 EnumConstantDecls.data(), EnumConstantDecls.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001607
Chris Lattnerb28317a2009-03-28 19:18:32 +00001608 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001610 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001612
1613 EnumScope.Exit();
1614 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001615}
1616
1617/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001618/// start of a type-qualifier-list.
1619bool Parser::isTypeQualifier() const {
1620 switch (Tok.getKind()) {
1621 default: return false;
1622 // type-qualifier
1623 case tok::kw_const:
1624 case tok::kw_volatile:
1625 case tok::kw_restrict:
1626 return true;
1627 }
1628}
1629
1630/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001631/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001632bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 switch (Tok.getKind()) {
1634 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001635
1636 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001637 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001638 // Annotate typenames and C++ scope specifiers. If we get one, just
1639 // recurse to handle whatever we get.
1640 if (TryAnnotateTypeOrScopeToken())
1641 return isTypeSpecifierQualifier();
1642 // Otherwise, not a type specifier.
1643 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001644
Chris Lattner166a8fc2009-01-04 23:41:41 +00001645 case tok::coloncolon: // ::foo::bar
1646 if (NextToken().is(tok::kw_new) || // ::new
1647 NextToken().is(tok::kw_delete)) // ::delete
1648 return false;
1649
1650 // Annotate typenames and C++ scope specifiers. If we get one, just
1651 // recurse to handle whatever we get.
1652 if (TryAnnotateTypeOrScopeToken())
1653 return isTypeSpecifierQualifier();
1654 // Otherwise, not a type specifier.
1655 return false;
1656
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 // GNU attributes support.
1658 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001659 // GNU typeof support.
1660 case tok::kw_typeof:
1661
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 // type-specifiers
1663 case tok::kw_short:
1664 case tok::kw_long:
1665 case tok::kw_signed:
1666 case tok::kw_unsigned:
1667 case tok::kw__Complex:
1668 case tok::kw__Imaginary:
1669 case tok::kw_void:
1670 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001671 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 case tok::kw_int:
1673 case tok::kw_float:
1674 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001675 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 case tok::kw__Bool:
1677 case tok::kw__Decimal32:
1678 case tok::kw__Decimal64:
1679 case tok::kw__Decimal128:
1680
Chris Lattner99dc9142008-04-13 18:59:07 +00001681 // struct-or-union-specifier (C99) or class-specifier (C++)
1682 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 case tok::kw_struct:
1684 case tok::kw_union:
1685 // enum-specifier
1686 case tok::kw_enum:
1687
1688 // type-qualifier
1689 case tok::kw_const:
1690 case tok::kw_volatile:
1691 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001692
1693 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001694 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001696
1697 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1698 case tok::less:
1699 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001700
1701 case tok::kw___cdecl:
1702 case tok::kw___stdcall:
1703 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001704 case tok::kw___w64:
1705 case tok::kw___ptr64:
1706 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 }
1708}
1709
1710/// isDeclarationSpecifier() - Return true if the current token is part of a
1711/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001712bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 switch (Tok.getKind()) {
1714 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001715
1716 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001717 // Unfortunate hack to support "Class.factoryMethod" notation.
1718 if (getLang().ObjC1 && NextToken().is(tok::period))
1719 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001720 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001721
Douglas Gregord57959a2009-03-27 23:10:48 +00001722 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001723 // Annotate typenames and C++ scope specifiers. If we get one, just
1724 // recurse to handle whatever we get.
1725 if (TryAnnotateTypeOrScopeToken())
1726 return isDeclarationSpecifier();
1727 // Otherwise, not a declaration specifier.
1728 return false;
1729 case tok::coloncolon: // ::foo::bar
1730 if (NextToken().is(tok::kw_new) || // ::new
1731 NextToken().is(tok::kw_delete)) // ::delete
1732 return false;
1733
1734 // Annotate typenames and C++ scope specifiers. If we get one, just
1735 // recurse to handle whatever we get.
1736 if (TryAnnotateTypeOrScopeToken())
1737 return isDeclarationSpecifier();
1738 // Otherwise, not a declaration specifier.
1739 return false;
1740
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 // storage-class-specifier
1742 case tok::kw_typedef:
1743 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001744 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 case tok::kw_static:
1746 case tok::kw_auto:
1747 case tok::kw_register:
1748 case tok::kw___thread:
1749
1750 // type-specifiers
1751 case tok::kw_short:
1752 case tok::kw_long:
1753 case tok::kw_signed:
1754 case tok::kw_unsigned:
1755 case tok::kw__Complex:
1756 case tok::kw__Imaginary:
1757 case tok::kw_void:
1758 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001759 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 case tok::kw_int:
1761 case tok::kw_float:
1762 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001763 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 case tok::kw__Bool:
1765 case tok::kw__Decimal32:
1766 case tok::kw__Decimal64:
1767 case tok::kw__Decimal128:
1768
Chris Lattner99dc9142008-04-13 18:59:07 +00001769 // struct-or-union-specifier (C99) or class-specifier (C++)
1770 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 case tok::kw_struct:
1772 case tok::kw_union:
1773 // enum-specifier
1774 case tok::kw_enum:
1775
1776 // type-qualifier
1777 case tok::kw_const:
1778 case tok::kw_volatile:
1779 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001780
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 // function-specifier
1782 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001783 case tok::kw_virtual:
1784 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001785
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001786 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001787 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001788
Chris Lattner1ef08762007-08-09 17:01:07 +00001789 // GNU typeof support.
1790 case tok::kw_typeof:
1791
1792 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001793 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001795
1796 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1797 case tok::less:
1798 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001799
Steve Naroff47f52092009-01-06 19:34:12 +00001800 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001801 case tok::kw___cdecl:
1802 case tok::kw___stdcall:
1803 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001804 case tok::kw___w64:
1805 case tok::kw___ptr64:
1806 case tok::kw___forceinline:
1807 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 }
1809}
1810
1811
1812/// ParseTypeQualifierListOpt
1813/// type-qualifier-list: [C99 6.7.5]
1814/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001815/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001816/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001817/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001818///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001819void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 while (1) {
1821 int isInvalid = false;
1822 const char *PrevSpec = 0;
1823 SourceLocation Loc = Tok.getLocation();
1824
1825 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 case tok::kw_const:
1827 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1828 getLang())*2;
1829 break;
1830 case tok::kw_volatile:
1831 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1832 getLang())*2;
1833 break;
1834 case tok::kw_restrict:
1835 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1836 getLang())*2;
1837 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001838 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001839 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001840 case tok::kw___cdecl:
1841 case tok::kw___stdcall:
1842 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001843 if (AttributesAllowed) {
1844 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1845 continue;
1846 }
1847 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001849 if (AttributesAllowed) {
1850 DS.AddAttributes(ParseAttributes());
1851 continue; // do *not* consume the next token!
1852 }
1853 // otherwise, FALL THROUGH!
1854 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001855 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001856 // If this is not a type-qualifier token, we're done reading type
1857 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001858 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001859 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001861
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 // If the specifier combination wasn't legal, issue a diagnostic.
1863 if (isInvalid) {
1864 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001865 // Pick between error or extwarn.
1866 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1867 : diag::ext_duplicate_declspec;
1868 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001869 }
1870 ConsumeToken();
1871 }
1872}
1873
1874
1875/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1876///
1877void Parser::ParseDeclarator(Declarator &D) {
1878 /// This implements the 'declarator' production in the C grammar, then checks
1879 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001880 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001881}
1882
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001883/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1884/// is parsed by the function passed to it. Pass null, and the direct-declarator
1885/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001886/// ptr-operator production.
1887///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001888/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1889/// [C] pointer[opt] direct-declarator
1890/// [C++] direct-declarator
1891/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001892///
1893/// pointer: [C99 6.7.5]
1894/// '*' type-qualifier-list[opt]
1895/// '*' type-qualifier-list[opt] pointer
1896///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001897/// ptr-operator:
1898/// '*' cv-qualifier-seq[opt]
1899/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001900/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001901/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001902/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001903/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001904void Parser::ParseDeclaratorInternal(Declarator &D,
1905 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001906
Sebastian Redlf30208a2009-01-24 21:16:55 +00001907 // C++ member pointers start with a '::' or a nested-name.
1908 // Member pointers get special handling, since there's no place for the
1909 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001910 if (getLang().CPlusPlus &&
1911 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1912 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001913 CXXScopeSpec SS;
1914 if (ParseOptionalCXXScopeSpecifier(SS)) {
1915 if(Tok.isNot(tok::star)) {
1916 // The scope spec really belongs to the direct-declarator.
1917 D.getCXXScopeSpec() = SS;
1918 if (DirectDeclParser)
1919 (this->*DirectDeclParser)(D);
1920 return;
1921 }
1922
1923 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001924 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001925 DeclSpec DS;
1926 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001927 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001928
1929 // Recurse to parse whatever is left.
1930 ParseDeclaratorInternal(D, DirectDeclParser);
1931
1932 // Sema will have to catch (syntactically invalid) pointers into global
1933 // scope. It has to catch pointers into namespace scope anyway.
1934 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001935 Loc, DS.TakeAttributes()),
1936 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001937 return;
1938 }
1939 }
1940
1941 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001942 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001943 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001944 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001945 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001946 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001947 if (DirectDeclParser)
1948 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001949 return;
1950 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001951
Sebastian Redl05532f22009-03-15 22:02:01 +00001952 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1953 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001954 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001955 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001956
Chris Lattner9af55002009-03-27 04:18:06 +00001957 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00001958 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001960
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001962 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001963
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001965 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001966 if (Kind == tok::star)
1967 // Remember that we parsed a pointer type, and remember the type-quals.
1968 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001969 DS.TakeAttributes()),
1970 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001971 else
1972 // Remember that we parsed a Block type, and remember the type-quals.
1973 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00001974 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001975 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001976 } else {
1977 // Is a reference
1978 DeclSpec DS;
1979
Sebastian Redl743de1f2009-03-23 00:00:23 +00001980 // Complain about rvalue references in C++03, but then go on and build
1981 // the declarator.
1982 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1983 Diag(Loc, diag::err_rvalue_reference);
1984
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1986 // cv-qualifiers are introduced through the use of a typedef or of a
1987 // template type argument, in which case the cv-qualifiers are ignored.
1988 //
1989 // [GNU] Retricted references are allowed.
1990 // [GNU] Attributes on references are allowed.
1991 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001992 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001993
1994 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1995 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1996 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001997 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1999 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002000 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002001 }
2002
2003 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002004 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002005
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002006 if (D.getNumTypeObjects() > 0) {
2007 // C++ [dcl.ref]p4: There shall be no references to references.
2008 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2009 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002010 if (const IdentifierInfo *II = D.getIdentifier())
2011 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2012 << II;
2013 else
2014 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2015 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002016
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002017 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002018 // can go ahead and build the (technically ill-formed)
2019 // declarator: reference collapsing will take care of it.
2020 }
2021 }
2022
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002024 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002025 DS.TakeAttributes(),
2026 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002027 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 }
2029}
2030
2031/// ParseDirectDeclarator
2032/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002033/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002034/// '(' declarator ')'
2035/// [GNU] '(' attributes declarator ')'
2036/// [C90] direct-declarator '[' constant-expression[opt] ']'
2037/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2038/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2039/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2040/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2041/// direct-declarator '(' parameter-type-list ')'
2042/// direct-declarator '(' identifier-list[opt] ')'
2043/// [GNU] direct-declarator '(' parameter-forward-declarations
2044/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002045/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2046/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002047/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002048///
2049/// declarator-id: [C++ 8]
2050/// id-expression
2051/// '::'[opt] nested-name-specifier[opt] type-name
2052///
2053/// id-expression: [C++ 5.1]
2054/// unqualified-id
2055/// qualified-id [TODO]
2056///
2057/// unqualified-id: [C++ 5.1]
2058/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002059/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002060/// conversion-function-id [TODO]
2061/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002062/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002063///
Reid Spencer5f016e22007-07-11 17:01:13 +00002064void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002065 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002066
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002067 if (getLang().CPlusPlus) {
2068 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002069 // ParseDeclaratorInternal might already have parsed the scope.
2070 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2071 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002072 if (afterCXXScope) {
2073 // Change the declaration context for name lookup, until this function
2074 // is exited (and the declarator has been parsed).
2075 DeclScopeObj.EnterDeclaratorScope();
2076 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002077
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002078 if (Tok.is(tok::identifier)) {
2079 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002080
2081 // If this identifier is the name of the current class, it's a
2082 // constructor name.
2083 if (!D.getDeclSpec().hasTypeSpecifier() &&
2084 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2085 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2086 Tok.getLocation(), CurScope),
2087 Tok.getLocation());
2088 // This is a normal identifier.
2089 } else
2090 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002091 ConsumeToken();
2092 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002093 } else if (Tok.is(tok::annot_template_id)) {
2094 TemplateIdAnnotation *TemplateId
2095 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2096
2097 // FIXME: Could this template-id name a constructor?
2098
2099 // FIXME: This is an egregious hack, where we silently ignore
2100 // the specialization (which should be a function template
2101 // specialization name) and use the name instead. This hack
2102 // will go away when we have support for function
2103 // specializations.
2104 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2105 TemplateId->Destroy();
2106 ConsumeToken();
2107 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002108 } else if (Tok.is(tok::kw_operator)) {
2109 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002110 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002111
Douglas Gregor70316a02008-12-26 15:00:45 +00002112 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002113 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2114 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002115 } else {
2116 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002117 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2118 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2119 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002120 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002121 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002122 }
2123 goto PastIdentifier;
2124 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002125 // This should be a C++ destructor.
2126 SourceLocation TildeLoc = ConsumeToken();
2127 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002128 // FIXME: Inaccurate.
2129 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002130 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00002131 TypeResult Type = ParseClassName(EndLoc);
2132 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002133 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002134 else
2135 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002136 } else {
2137 Diag(Tok, diag::err_expected_class_name);
2138 D.SetIdentifier(0, TildeLoc);
2139 }
2140 goto PastIdentifier;
2141 }
2142
2143 // If we reached this point, token is not identifier and not '~'.
2144
2145 if (afterCXXScope) {
2146 Diag(Tok, diag::err_expected_unqualified_id);
2147 D.SetIdentifier(0, Tok.getLocation());
2148 D.setInvalidType(true);
2149 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002150 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002151 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002152 }
2153
2154 // If we reached this point, we are either in C/ObjC or the token didn't
2155 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002156 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2157 assert(!getLang().CPlusPlus &&
2158 "There's a C++-specific check for tok::identifier above");
2159 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2160 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2161 ConsumeToken();
2162 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002163 // direct-declarator: '(' declarator ')'
2164 // direct-declarator: '(' attributes declarator ')'
2165 // Example: 'char (*X)' or 'int (*XX)(void)'
2166 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002167 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 // This could be something simple like "int" (in which case the declarator
2169 // portion is empty), if an abstract-declarator is allowed.
2170 D.SetIdentifier(0, Tok.getLocation());
2171 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002172 if (D.getContext() == Declarator::MemberContext)
2173 Diag(Tok, diag::err_expected_member_name_or_semi)
2174 << D.getDeclSpec().getSourceRange();
2175 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002176 Diag(Tok, diag::err_expected_unqualified_id);
2177 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002178 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002180 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 }
2182
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002183 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 assert(D.isPastIdentifier() &&
2185 "Haven't past the location of the identifier yet?");
2186
2187 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002188 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002189 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2190 // In such a case, check if we actually have a function declarator; if it
2191 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002192 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2193 // When not in file scope, warn for ambiguous function declarators, just
2194 // in case the author intended it as a variable definition.
2195 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2196 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2197 break;
2198 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002199 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002200 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 ParseBracketDeclarator(D);
2202 } else {
2203 break;
2204 }
2205 }
2206}
2207
Chris Lattneref4715c2008-04-06 05:45:57 +00002208/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2209/// only called before the identifier, so these are most likely just grouping
2210/// parens for precedence. If we find that these are actually function
2211/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2212///
2213/// direct-declarator:
2214/// '(' declarator ')'
2215/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002216/// direct-declarator '(' parameter-type-list ')'
2217/// direct-declarator '(' identifier-list[opt] ')'
2218/// [GNU] direct-declarator '(' parameter-forward-declarations
2219/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002220///
2221void Parser::ParseParenDeclarator(Declarator &D) {
2222 SourceLocation StartLoc = ConsumeParen();
2223 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2224
Chris Lattner7399ee02008-10-20 02:05:46 +00002225 // Eat any attributes before we look at whether this is a grouping or function
2226 // declarator paren. If this is a grouping paren, the attribute applies to
2227 // the type being built up, for example:
2228 // int (__attribute__(()) *x)(long y)
2229 // If this ends up not being a grouping paren, the attribute applies to the
2230 // first argument, for example:
2231 // int (__attribute__(()) int x)
2232 // In either case, we need to eat any attributes to be able to determine what
2233 // sort of paren this is.
2234 //
2235 AttributeList *AttrList = 0;
2236 bool RequiresArg = false;
2237 if (Tok.is(tok::kw___attribute)) {
2238 AttrList = ParseAttributes();
2239
2240 // We require that the argument list (if this is a non-grouping paren) be
2241 // present even if the attribute list was empty.
2242 RequiresArg = true;
2243 }
Steve Naroff239f0732008-12-25 14:16:32 +00002244 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002245 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2246 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2247 Tok.is(tok::kw___ptr64)) {
2248 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2249 }
Chris Lattner7399ee02008-10-20 02:05:46 +00002250
Chris Lattneref4715c2008-04-06 05:45:57 +00002251 // If we haven't past the identifier yet (or where the identifier would be
2252 // stored, if this is an abstract declarator), then this is probably just
2253 // grouping parens. However, if this could be an abstract-declarator, then
2254 // this could also be the start of function arguments (consider 'void()').
2255 bool isGrouping;
2256
2257 if (!D.mayOmitIdentifier()) {
2258 // If this can't be an abstract-declarator, this *must* be a grouping
2259 // paren, because we haven't seen the identifier yet.
2260 isGrouping = true;
2261 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002262 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002263 isDeclarationSpecifier()) { // 'int(int)' is a function.
2264 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2265 // considered to be a type, not a K&R identifier-list.
2266 isGrouping = false;
2267 } else {
2268 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2269 isGrouping = true;
2270 }
2271
2272 // If this is a grouping paren, handle:
2273 // direct-declarator: '(' declarator ')'
2274 // direct-declarator: '(' attributes declarator ')'
2275 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002276 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002277 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002278 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002279 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002280
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002281 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002282 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002283 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002284
2285 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002286 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002287 return;
2288 }
2289
2290 // Okay, if this wasn't a grouping paren, it must be the start of a function
2291 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002292 // identifier (and remember where it would have been), then call into
2293 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002294 D.SetIdentifier(0, Tok.getLocation());
2295
Chris Lattner7399ee02008-10-20 02:05:46 +00002296 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002297}
2298
2299/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2300/// declarator D up to a paren, which indicates that we are parsing function
2301/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002302///
Chris Lattner7399ee02008-10-20 02:05:46 +00002303/// If AttrList is non-null, then the caller parsed those arguments immediately
2304/// after the open paren - they should be considered to be the first argument of
2305/// a parameter. If RequiresArg is true, then the first argument of the
2306/// function is required to be present and required to not be an identifier
2307/// list.
2308///
Reid Spencer5f016e22007-07-11 17:01:13 +00002309/// This method also handles this portion of the grammar:
2310/// parameter-type-list: [C99 6.7.5]
2311/// parameter-list
2312/// parameter-list ',' '...'
2313///
2314/// parameter-list: [C99 6.7.5]
2315/// parameter-declaration
2316/// parameter-list ',' parameter-declaration
2317///
2318/// parameter-declaration: [C99 6.7.5]
2319/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002320/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002321/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002322/// declaration-specifiers abstract-declarator[opt]
2323/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002324/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002325/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2326///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002327/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002328/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002329///
Chris Lattner7399ee02008-10-20 02:05:46 +00002330void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2331 AttributeList *AttrList,
2332 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002333 // lparen is already consumed!
2334 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002335
Chris Lattner7399ee02008-10-20 02:05:46 +00002336 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002337 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002338 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002339 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002340 delete AttrList;
2341 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002342
Sebastian Redlab197ba2009-02-09 18:23:29 +00002343 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002344
2345 // cv-qualifier-seq[opt].
2346 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002347 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002348 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002349 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002350 llvm::SmallVector<TypeTy*, 2> Exceptions;
2351 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002352 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002353 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002354 if (!DS.getSourceRange().getEnd().isInvalid())
2355 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002356
2357 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002358 if (Tok.is(tok::kw_throw)) {
2359 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002360 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002361 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2362 hasAnyExceptionSpec);
2363 assert(Exceptions.size() == ExceptionRanges.size() &&
2364 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002365 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002366 }
2367
Chris Lattnerf97409f2008-04-06 06:57:35 +00002368 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002369 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002370 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002371 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002372 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002373 /*arglist*/ 0, 0,
2374 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002375 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002376 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002377 Exceptions.data(),
2378 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002379 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002380 LParenLoc, D),
2381 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002382 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002383 }
2384
Chris Lattner7399ee02008-10-20 02:05:46 +00002385 // Alternatively, this parameter list may be an identifier list form for a
2386 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002387 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002388 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002389 // K&R identifier lists can't have typedefs as identifiers, per
2390 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002391 if (RequiresArg) {
2392 Diag(Tok, diag::err_argument_required_after_attribute);
2393 delete AttrList;
2394 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002395 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2396 // normal declarators, not for abstract-declarators.
2397 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002398 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002399 }
2400
2401 // Finally, a normal, non-empty parameter type list.
2402
2403 // Build up an array of information about the parsed arguments.
2404 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002405
2406 // Enter function-declaration scope, limiting any declarators to the
2407 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002408 ParseScope PrototypeScope(this,
2409 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002410
2411 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002412 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002413 while (1) {
2414 if (Tok.is(tok::ellipsis)) {
2415 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002416 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002417 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002418 }
2419
Chris Lattnerf97409f2008-04-06 06:57:35 +00002420 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002421
Chris Lattnerf97409f2008-04-06 06:57:35 +00002422 // Parse the declaration-specifiers.
2423 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002424
2425 // If the caller parsed attributes for the first argument, add them now.
2426 if (AttrList) {
2427 DS.AddAttributes(AttrList);
2428 AttrList = 0; // Only apply the attributes to the first parameter.
2429 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002430 ParseDeclarationSpecifiers(DS);
2431
Chris Lattnerf97409f2008-04-06 06:57:35 +00002432 // Parse the declarator. This is "PrototypeContext", because we must
2433 // accept either 'declarator' or 'abstract-declarator' here.
2434 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2435 ParseDeclarator(ParmDecl);
2436
2437 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002438 if (Tok.is(tok::kw___attribute)) {
2439 SourceLocation Loc;
2440 AttributeList *AttrList = ParseAttributes(&Loc);
2441 ParmDecl.AddAttributes(AttrList, Loc);
2442 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002443
Chris Lattnerf97409f2008-04-06 06:57:35 +00002444 // Remember this parsed parameter in ParamInfo.
2445 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2446
Douglas Gregor72b505b2008-12-16 21:30:33 +00002447 // DefArgToks is used when the parsing of default arguments needs
2448 // to be delayed.
2449 CachedTokens *DefArgToks = 0;
2450
Chris Lattnerf97409f2008-04-06 06:57:35 +00002451 // If no parameter was specified, verify that *something* was specified,
2452 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002453 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2454 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002455 // Completely missing, emit error.
2456 Diag(DSStart, diag::err_missing_param);
2457 } else {
2458 // Otherwise, we have something. Add it and let semantic analysis try
2459 // to grok it and add the result to the ParamInfo we are building.
2460
2461 // Inform the actions module about the parameter declarator, so it gets
2462 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002463 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002464
2465 // Parse the default argument, if any. We parse the default
2466 // arguments in all dialects; the semantic analysis in
2467 // ActOnParamDefaultArgument will reject the default argument in
2468 // C.
2469 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002470 SourceLocation EqualLoc = Tok.getLocation();
2471
Chris Lattner04421082008-04-08 04:40:51 +00002472 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002473 if (D.getContext() == Declarator::MemberContext) {
2474 // If we're inside a class definition, cache the tokens
2475 // corresponding to the default argument. We'll actually parse
2476 // them when we see the end of the class definition.
2477 // FIXME: Templates will require something similar.
2478 // FIXME: Can we use a smart pointer for Toks?
2479 DefArgToks = new CachedTokens;
2480
2481 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2482 tok::semi, false)) {
2483 delete DefArgToks;
2484 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002485 Actions.ActOnParamDefaultArgumentError(Param);
2486 } else
Anders Carlsson5e300d12009-06-12 16:51:40 +00002487 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2488 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002489 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002490 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002491 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002492
2493 OwningExprResult DefArgResult(ParseAssignmentExpression());
2494 if (DefArgResult.isInvalid()) {
2495 Actions.ActOnParamDefaultArgumentError(Param);
2496 SkipUntil(tok::comma, tok::r_paren, true, true);
2497 } else {
2498 // Inform the actions module about the default argument
2499 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002500 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002501 }
Chris Lattner04421082008-04-08 04:40:51 +00002502 }
2503 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002504
2505 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002506 ParmDecl.getIdentifierLoc(), Param,
2507 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002508 }
2509
2510 // If the next token is a comma, consume it and keep reading arguments.
2511 if (Tok.isNot(tok::comma)) break;
2512
2513 // Consume the comma.
2514 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002515 }
2516
Chris Lattnerf97409f2008-04-06 06:57:35 +00002517 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002518 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002519
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002520 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002521 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002522
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002523 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002524 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002525 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002526 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002527 llvm::SmallVector<TypeTy*, 2> Exceptions;
2528 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002529 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002530 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002531 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002532 if (!DS.getSourceRange().getEnd().isInvalid())
2533 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002534
2535 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002536 if (Tok.is(tok::kw_throw)) {
2537 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002538 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002539 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2540 hasAnyExceptionSpec);
2541 assert(Exceptions.size() == ExceptionRanges.size() &&
2542 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002543 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002544 }
2545
Reid Spencer5f016e22007-07-11 17:01:13 +00002546 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002547 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002548 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002549 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002550 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002551 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002552 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002553 Exceptions.data(),
2554 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002555 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002556 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002557}
2558
Chris Lattner66d28652008-04-06 06:34:08 +00002559/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2560/// we found a K&R-style identifier list instead of a type argument list. The
2561/// current token is known to be the first identifier in the list.
2562///
2563/// identifier-list: [C99 6.7.5]
2564/// identifier
2565/// identifier-list ',' identifier
2566///
2567void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2568 Declarator &D) {
2569 // Build up an array of information about the parsed arguments.
2570 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2571 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2572
2573 // If there was no identifier specified for the declarator, either we are in
2574 // an abstract-declarator, or we are in a parameter declarator which was found
2575 // to be abstract. In abstract-declarators, identifier lists are not valid:
2576 // diagnose this.
2577 if (!D.getIdentifier())
2578 Diag(Tok, diag::ext_ident_list_in_param);
2579
2580 // Tok is known to be the first identifier in the list. Remember this
2581 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002582 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002583 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002584 Tok.getLocation(),
2585 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002586
Chris Lattner50c64772008-04-06 06:39:19 +00002587 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002588
2589 while (Tok.is(tok::comma)) {
2590 // Eat the comma.
2591 ConsumeToken();
2592
Chris Lattner50c64772008-04-06 06:39:19 +00002593 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002594 if (Tok.isNot(tok::identifier)) {
2595 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002596 SkipUntil(tok::r_paren);
2597 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002598 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002599
Chris Lattner66d28652008-04-06 06:34:08 +00002600 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002601
2602 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002603 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002604 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002605
2606 // Verify that the argument identifier has not already been mentioned.
2607 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002608 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002609 } else {
2610 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002611 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002612 Tok.getLocation(),
2613 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002614 }
Chris Lattner66d28652008-04-06 06:34:08 +00002615
2616 // Eat the identifier.
2617 ConsumeToken();
2618 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002619
2620 // If we have the closing ')', eat it and we're done.
2621 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2622
Chris Lattner50c64772008-04-06 06:39:19 +00002623 // Remember that we parsed a function type, and remember the attributes. This
2624 // function type is always a K&R style function type, which is not varargs and
2625 // has no prototype.
2626 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002627 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002628 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002629 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002630 /*exception*/false,
2631 SourceLocation(), false, 0, 0, 0,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002632 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002633 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002634}
Chris Lattneref4715c2008-04-06 05:45:57 +00002635
Reid Spencer5f016e22007-07-11 17:01:13 +00002636/// [C90] direct-declarator '[' constant-expression[opt] ']'
2637/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2638/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2639/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2640/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2641void Parser::ParseBracketDeclarator(Declarator &D) {
2642 SourceLocation StartLoc = ConsumeBracket();
2643
Chris Lattner378c7e42008-12-18 07:27:21 +00002644 // C array syntax has many features, but by-far the most common is [] and [4].
2645 // This code does a fast path to handle some of the most obvious cases.
2646 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002648 // Remember that we parsed the empty array type.
2649 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002650 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2651 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002652 return;
2653 } else if (Tok.getKind() == tok::numeric_constant &&
2654 GetLookAheadToken(1).is(tok::r_square)) {
2655 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002656 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002657 ConsumeToken();
2658
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002660
2661 // If there was an error parsing the assignment-expression, recover.
2662 if (ExprRes.isInvalid())
2663 ExprRes.release(); // Deallocate expr, just use [].
2664
2665 // Remember that we parsed a array type, and remember its features.
2666 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002667 ExprRes.release(), StartLoc),
2668 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002669 return;
2670 }
2671
Reid Spencer5f016e22007-07-11 17:01:13 +00002672 // If valid, this location is the position where we read the 'static' keyword.
2673 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002674 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 StaticLoc = ConsumeToken();
2676
2677 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002678 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002679 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002680 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002681
2682 // If we haven't already read 'static', check to see if there is one after the
2683 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002684 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 StaticLoc = ConsumeToken();
2686
2687 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2688 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002689 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002690
2691 // Handle the case where we have '[*]' as the array size. However, a leading
2692 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2693 // the the token after the star is a ']'. Since stars in arrays are
2694 // infrequent, use of lookahead is not costly here.
2695 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002696 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002697
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002698 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002699 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002700 StaticLoc = SourceLocation(); // Drop the static.
2701 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002702 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002703 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002704 // Note, in C89, this production uses the constant-expr production instead
2705 // of assignment-expr. The only difference is that assignment-expr allows
2706 // things like '=' and '*='. Sema rejects these in C89 mode because they
2707 // are not i-c-e's, so we don't need to distinguish between the two here.
2708
Douglas Gregore0762c92009-06-19 23:52:42 +00002709 // Parse the constant-expression or assignment-expression now (depending
2710 // on dialect).
2711 if (getLang().CPlusPlus)
2712 NumElements = ParseConstantExpression();
2713 else
2714 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002715 }
2716
2717 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002718 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002719 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 // If the expression was invalid, skip it.
2721 SkipUntil(tok::r_square);
2722 return;
2723 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002724
2725 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2726
Chris Lattner378c7e42008-12-18 07:27:21 +00002727 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002728 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2729 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002730 NumElements.release(), StartLoc),
2731 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002732}
2733
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002734/// [GNU] typeof-specifier:
2735/// typeof ( expressions )
2736/// typeof ( type-name )
2737/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002738///
2739void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002740 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002741 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002742 SourceLocation StartLoc = ConsumeToken();
2743
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002744 bool isCastExpr;
2745 TypeTy *CastTy;
2746 SourceRange CastRange;
2747 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2748 isCastExpr,
2749 CastTy,
2750 CastRange);
2751
2752 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002753 // FIXME: Not accurate, the range gets one token more than it should.
2754 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002755 else
2756 DS.SetRangeEnd(CastRange.getEnd());
2757
2758 if (isCastExpr) {
2759 if (!CastTy) {
2760 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002761 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002762 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002763
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002764 const char *PrevSpec = 0;
2765 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2766 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2767 CastTy))
2768 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2769 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002770 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002771
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002772 // If we get here, the operand to the typeof was an expresion.
2773 if (Operand.isInvalid()) {
2774 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002775 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002776 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002777
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002778 const char *PrevSpec = 0;
2779 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2780 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2781 Operand.release()))
2782 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002783}