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