blob: cdf84bfad688164899850874d28569b004acbf65 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl511ed552008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor220cac52009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000034 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000039
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000041 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044}
45
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000046/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000061/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000065///
Steve Naroff0f2fe172007-06-01 17:11:19 +000066/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000081
Sebastian Redlf6591ca2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000084
Steve Naroffb8371e12007-06-09 03:39:29 +000085 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000086
Chris Lattner76c72282007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101
Chris Lattner76c72282007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110
111 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000113 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner76c72282007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000139 }
Chris Lattner76c72282007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner76c72282007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl511ed552008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000171 }
Chris Lattner76c72282007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000181 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
183 }
184 }
185 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 }
189 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 }
199 return CurrAttr;
200}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000201
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner53361ac2006-08-10 05:19:57 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redlf769df52009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner49836b42009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000236 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000237 case tok::kw_export:
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000238 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000239 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner49836b42009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000242 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner49836b42009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000245 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner49836b42009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000248 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000249 default:
Chris Lattner49836b42009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnera5235172007-08-25 06:57:03 +0000251 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000252
253 // This routine returns a DeclGroup, if the thing we parsed only contains a
254 // single decl, convert it now.
255 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000256}
257
258/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
259/// declaration-specifiers init-declarator-list[opt] ';'
260///[C90/C++]init-declarator-list ';' [TODO]
261/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000262///
263/// If RequireSemi is false, this does not check for a ';' at the end of the
264/// declaration.
265Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattner32dc41c2009-03-29 17:27:48 +0000267 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
Chris Lattner0e894622006-08-13 19:58:17 +0000272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000275 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000278 }
279
Chris Lattner53361ac2006-08-10 05:19:57 +0000280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattnerefb0f112009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000285
Chris Lattner49836b42009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattner32dc41c2009-03-29 17:27:48 +0000288 // If the client wants to check what comes after the declaration, just return
289 // immediately without checking anything!
290 if (!RequireSemi) return DG;
Chris Lattnerefb0f112009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattnerefb0f112009-03-29 17:18:04 +0000297 Diag(Tok, diag::err_expected_semi_declation);
298 // Skip to end of block or statement
299 SkipUntil(tok::r_brace, true, true);
300 if (Tok.is(tok::semi))
301 ConsumeToken();
302 return DG;
Chris Lattner53361ac2006-08-10 05:19:57 +0000303}
304
Douglas Gregor23996282009-05-12 21:31:51 +0000305/// \brief Parse 'declaration' after parsing 'declaration-specifiers
306/// declarator'. This method parses the remainder of the declaration
307/// (including any attributes or initializer, among other things) and
308/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000309///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000310/// init-declarator: [C99 6.7]
311/// declarator
312/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000313/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
314/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000315/// [C++] declarator initializer[opt]
316///
317/// [C++] initializer:
318/// [C++] '=' initializer-clause
319/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000320/// [C++0x] '=' 'default' [TODO]
321/// [C++0x] '=' 'delete'
322///
323/// According to the standard grammar, =default and =delete are function
324/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000325///
Douglas Gregor23996282009-05-12 21:31:51 +0000326Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
327 // If a simple-asm-expr is present, parse it.
328 if (Tok.is(tok::kw_asm)) {
329 SourceLocation Loc;
330 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
331 if (AsmLabel.isInvalid()) {
332 SkipUntil(tok::semi, true, true);
333 return DeclPtrTy();
334 }
335
336 D.setAsmLabel(AsmLabel.release());
337 D.SetRangeEnd(Loc);
338 }
339
340 // If attributes are present, parse them.
341 if (Tok.is(tok::kw___attribute)) {
342 SourceLocation Loc;
343 AttributeList *AttrList = ParseAttributes(&Loc);
344 D.AddAttributes(AttrList, Loc);
345 }
346
347 // Inform the current actions module that we just parsed this declarator.
348 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
349
350 // Parse declarator '=' initializer.
351 if (Tok.is(tok::equal)) {
352 ConsumeToken();
353 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
354 SourceLocation DelLoc = ConsumeToken();
355 Actions.SetDeclDeleted(ThisDecl, DelLoc);
356 } else {
357 OwningExprResult Init(ParseInitializer());
358 if (Init.isInvalid()) {
359 SkipUntil(tok::semi, true, true);
360 return DeclPtrTy();
361 }
362 Actions.AddInitializerToDecl(ThisDecl, move(Init));
363 }
364 } else if (Tok.is(tok::l_paren)) {
365 // Parse C++ direct initializer: '(' expression-list ')'
366 SourceLocation LParenLoc = ConsumeParen();
367 ExprVector Exprs(Actions);
368 CommaLocsTy CommaLocs;
369
370 if (ParseExpressionList(Exprs, CommaLocs)) {
371 SkipUntil(tok::r_paren);
372 } else {
373 // Match the ')'.
374 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
375
376 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
377 "Unexpected number of commas!");
378 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
379 move_arg(Exprs),
380 &CommaLocs[0], RParenLoc);
381 }
382 } else {
383 Actions.ActOnUninitializedDecl(ThisDecl);
384 }
385
386 return ThisDecl;
387}
388
389/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
390/// parsing 'declaration-specifiers declarator'. This method is split out this
391/// way to handle the ambiguity between top-level function-definitions and
392/// declarations.
393///
394/// init-declarator-list: [C99 6.7]
395/// init-declarator
396/// init-declarator-list ',' init-declarator
397///
398/// According to the standard grammar, =default and =delete are function
399/// definitions, but that definitely doesn't fit with the parser here.
400///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000401Parser::DeclGroupPtrTy Parser::
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000402ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000403 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
404 // that we parse together here.
405 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000406
Chris Lattner53361ac2006-08-10 05:19:57 +0000407 // At this point, we know that it is not a function definition. Parse the
408 // rest of the init-declarator-list.
409 while (1) {
Douglas Gregor23996282009-05-12 21:31:51 +0000410 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
411 if (ThisDecl.get())
412 DeclsInGroup.push_back(ThisDecl);
Chris Lattner53361ac2006-08-10 05:19:57 +0000413
Chris Lattner53361ac2006-08-10 05:19:57 +0000414 // If we don't have a comma, it is either the end of the list (a ';') or an
415 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000416 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000417 break;
418
419 // Consume the comma.
420 ConsumeToken();
421
422 // Parse the next declarator.
423 D.clear();
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000424
425 // Accept attributes in an init-declarator. In the first declarator in a
426 // declaration, these would be part of the declspec. In subsequent
427 // declarators, they become part of the declarator itself, so that they
428 // don't apply to declarators after *this* one. Examples:
429 // short __attribute__((common)) var; -> declspec
430 // short var __attribute__((common)); -> declarator
431 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000432 if (Tok.is(tok::kw___attribute)) {
433 SourceLocation Loc;
434 AttributeList *AttrList = ParseAttributes(&Loc);
435 D.AddAttributes(AttrList, Loc);
436 }
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000437
Chris Lattner53361ac2006-08-10 05:19:57 +0000438 ParseDeclarator(D);
439 }
440
Chris Lattnerefb0f112009-03-29 17:18:04 +0000441 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
442 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000443}
444
Chris Lattner1890ac82006-08-13 01:16:23 +0000445/// ParseSpecifierQualifierList
446/// specifier-qualifier-list:
447/// type-specifier specifier-qualifier-list[opt]
448/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000449/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000450///
451void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
452 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
453 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000454 ParseDeclarationSpecifiers(DS);
455
456 // Validate declspec for type-name.
457 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000458 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
459 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000460 Diag(Tok, diag::err_typename_requires_specqual);
461
Chris Lattner1b22eed2006-11-28 05:12:07 +0000462 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000463 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000464 if (DS.getStorageClassSpecLoc().isValid())
465 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
466 else
467 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000468 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000469 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000470
471 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000472 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000473 if (DS.isInlineSpecified())
474 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
475 if (DS.isVirtualSpecified())
476 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
477 if (DS.isExplicitSpecified())
478 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000479 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000480 }
481}
Chris Lattner53361ac2006-08-10 05:19:57 +0000482
Chris Lattner6cc055a2009-04-12 20:42:31 +0000483/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
484/// specified token is valid after the identifier in a declarator which
485/// immediately follows the declspec. For example, these things are valid:
486///
487/// int x [ 4]; // direct-declarator
488/// int x ( int y); // direct-declarator
489/// int(int x ) // direct-declarator
490/// int x ; // simple-declaration
491/// int x = 17; // init-declarator-list
492/// int x , y; // init-declarator-list
493/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000494/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000495/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000496///
497/// This is not, because 'x' does not immediately follow the declspec (though
498/// ')' happens to be valid anyway).
499/// int (x)
500///
501static bool isValidAfterIdentifierInDeclarator(const Token &T) {
502 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
503 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000504 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000505}
506
Chris Lattner20a0c612009-04-14 21:34:55 +0000507
508/// ParseImplicitInt - This method is called when we have an non-typename
509/// identifier in a declspec (which normally terminates the decl spec) when
510/// the declspec has no type specifier. In this case, the declspec is either
511/// malformed or is "implicit int" (in K&R and C89).
512///
513/// This method handles diagnosing this prettily and returns false if the
514/// declspec is done being processed. If it recovers and thinks there may be
515/// other pieces of declspec after it, it returns true.
516///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000517bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000518 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000519 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000520 assert(Tok.is(tok::identifier) && "should have identifier");
521
Chris Lattner20a0c612009-04-14 21:34:55 +0000522 SourceLocation Loc = Tok.getLocation();
523 // If we see an identifier that is not a type name, we normally would
524 // parse it as the identifer being declared. However, when a typename
525 // is typo'd or the definition is not included, this will incorrectly
526 // parse the typename as the identifier name and fall over misparsing
527 // later parts of the diagnostic.
528 //
529 // As such, we try to do some look-ahead in cases where this would
530 // otherwise be an "implicit-int" case to see if this is invalid. For
531 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
532 // an identifier with implicit int, we'd get a parse error because the
533 // next token is obviously invalid for a type. Parse these as a case
534 // with an invalid type specifier.
535 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
536
537 // Since we know that this either implicit int (which is rare) or an
538 // error, we'd do lookahead to try to do better recovery.
539 if (isValidAfterIdentifierInDeclarator(NextToken())) {
540 // If this token is valid for implicit int, e.g. "static x = 4", then
541 // we just avoid eating the identifier, so it will be parsed as the
542 // identifier in the declarator.
543 return false;
544 }
545
546 // Otherwise, if we don't consume this token, we are going to emit an
547 // error anyway. Try to recover from various common problems. Check
548 // to see if this was a reference to a tag name without a tag specified.
549 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000550 //
551 // C++ doesn't need this, and isTagName doesn't take SS.
552 if (SS == 0) {
553 const char *TagName = 0;
554 tok::TokenKind TagKind = tok::unknown;
Chris Lattner20a0c612009-04-14 21:34:55 +0000555
Chris Lattner20a0c612009-04-14 21:34:55 +0000556 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
557 default: break;
558 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
559 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
560 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
561 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
562 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000563
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000564 if (TagName) {
565 Diag(Loc, diag::err_use_of_tag_name_without_tag)
566 << Tok.getIdentifierInfo() << TagName
567 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
568
569 // Parse this as a tag as if the missing tag were present.
570 if (TagKind == tok::kw_enum)
571 ParseEnumSpecifier(Loc, DS, AS);
572 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000573 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000574 return true;
575 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000576 }
577
578 // Since this is almost certainly an invalid type name, emit a
579 // diagnostic that says it, eat the token, and mark the declspec as
580 // invalid.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000581 SourceRange R;
582 if (SS) R = SS->getRange();
583
584 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner20a0c612009-04-14 21:34:55 +0000585 const char *PrevSpec;
586 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
587 DS.SetRangeEnd(Tok.getLocation());
588 ConsumeToken();
589
590 // TODO: Could inject an invalid typedef decl in an enclosing scope to
591 // avoid rippling error messages on subsequent uses of the same type,
592 // could be useful if #include was forgotten.
593 return false;
594}
595
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000596/// ParseDeclarationSpecifiers
597/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000598/// storage-class-specifier declaration-specifiers[opt]
599/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000600/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000601/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000602///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000603/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000604/// 'typedef'
605/// 'extern'
606/// 'static'
607/// 'auto'
608/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000609/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000610/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000611/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000612/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000613/// [C++] 'virtual'
614/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000615/// 'friend': [C++ dcl.friend]
616
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000617///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000618void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000619 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner6cc055a2009-04-12 20:42:31 +0000620 AccessSpecifier AS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000621 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000622 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000623 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000624 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000625 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000626
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000627 switch (Tok.getKind()) {
Douglas Gregor450c75a2008-11-07 15:42:26 +0000628 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000629 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000630 // If this is not a declaration specifier token, we're done reading decl
631 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000632 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000633 return;
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000634
635 case tok::coloncolon: // ::foo::bar
636 // Annotate C++ scope specifiers. If we get one, loop.
637 if (TryAnnotateCXXScopeToken())
638 continue;
639 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000640
641 case tok::annot_cxxscope: {
642 if (DS.hasTypeSpecifier())
643 goto DoneWithDeclSpec;
644
645 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000646 Token Next = NextToken();
647 if (Next.is(tok::annot_template_id) &&
648 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000649 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000650 // We have a qualified template-id, e.g., N::A<int>
651 CXXScopeSpec SS;
652 ParseOptionalCXXScopeSpecifier(SS);
653 assert(Tok.is(tok::annot_template_id) &&
654 "ParseOptionalCXXScopeSpecifier not working");
655 AnnotateTemplateIdTokenAsType(&SS);
656 continue;
657 }
658
659 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000660 goto DoneWithDeclSpec;
661
662 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000663 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000664 SS.setRange(Tok.getAnnotationRange());
665
666 // If the next token is the name of the class type that the C++ scope
667 // denotes, followed by a '(', then this is a constructor declaration.
668 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000669 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000670 CurScope, &SS) &&
671 GetLookAheadToken(2).is(tok::l_paren))
672 goto DoneWithDeclSpec;
673
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000674 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
675 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000676
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000677 // If the referenced identifier is not a type, then this declspec is
678 // erroneous: We already checked about that it has no type specifier, and
679 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
680 // typename.
681 if (TypeRep == 0) {
682 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000683 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000684 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000685 }
Douglas Gregor52537682009-03-19 00:18:19 +0000686
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000687 ConsumeToken(); // The C++ scope.
688
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000690 TypeRep);
691 if (isInvalid)
692 break;
693
694 DS.SetRangeEnd(Tok.getLocation());
695 ConsumeToken(); // The typename.
696
697 continue;
698 }
Chris Lattnere387d9e2009-01-21 19:48:37 +0000699
700 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000701 if (Tok.getAnnotationValue())
702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
703 Tok.getAnnotationValue());
704 else
705 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000706 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
707 ConsumeToken(); // The typename
708
709 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
710 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
711 // Objective-C interface. If we don't have Objective-C or a '<', this is
712 // just a normal reference to a typedef name.
713 if (!Tok.is(tok::less) || !getLang().ObjC1)
714 continue;
715
716 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000717 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000718 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
719 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
720
721 DS.SetRangeEnd(EndProtoLoc);
722 continue;
723 }
724
Chris Lattner16fac4f2008-07-26 01:18:38 +0000725 // typedef-name
726 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000727 // In C++, check to see if this is a scope specifier like foo::bar::, if
728 // so handle it as such. This is important for ctor parsing.
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000729 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
730 continue;
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000731
Chris Lattner16fac4f2008-07-26 01:18:38 +0000732 // This identifier can only be a typedef name if we haven't already seen
733 // a type-specifier. Without this check we misparse:
734 // typedef int X; struct Y { short X; }; as 'short int'.
735 if (DS.hasTypeSpecifier())
736 goto DoneWithDeclSpec;
737
738 // It has to be available as a typedef too!
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000739 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
740 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000741
Chris Lattner6cc055a2009-04-12 20:42:31 +0000742 // If this is not a typedef name, don't parse it as part of the declspec,
743 // it must be an implicit int or an error.
744 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000745 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000746 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000747 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000748
Douglas Gregor61956c42008-10-31 09:07:45 +0000749 // C++: If the identifier is actually the name of the class type
750 // being defined and the next token is a '(', then this is a
751 // constructor declaration. We're done with the decl-specifiers
752 // and will treat this token as an identifier.
Chris Lattner6cc055a2009-04-12 20:42:31 +0000753 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000754 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
755 NextToken().getKind() == tok::l_paren)
756 goto DoneWithDeclSpec;
757
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner16fac4f2008-07-26 01:18:38 +0000759 TypeRep);
760 if (isInvalid)
761 break;
762
763 DS.SetRangeEnd(Tok.getLocation());
764 ConsumeToken(); // The identifier
765
766 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
767 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
768 // Objective-C interface. If we don't have Objective-C or a '<', this is
769 // just a normal reference to a typedef name.
770 if (!Tok.is(tok::less) || !getLang().ObjC1)
771 continue;
772
773 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000774 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000775 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000776 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000777
778 DS.SetRangeEnd(EndProtoLoc);
779
Steve Naroffcd5e7822008-09-22 10:28:57 +0000780 // Need to support trailing type qualifiers (e.g. "id<p> const").
781 // If a type specifier follows, it will be diagnosed elsewhere.
782 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000783 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000784
785 // type-name
786 case tok::annot_template_id: {
787 TemplateIdAnnotation *TemplateId
788 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000789 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000790 // This template-id does not refer to a type name, so we're
791 // done with the type-specifiers.
792 goto DoneWithDeclSpec;
793 }
794
795 // Turn the template-id annotation token into a type annotation
796 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000797 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000798 continue;
799 }
800
Chris Lattnere37e2332006-08-15 04:50:22 +0000801 // GNU attributes support.
802 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000803 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000804 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000805
806 // Microsoft declspec support.
807 case tok::kw___declspec:
808 if (!PP.getLangOptions().Microsoft)
809 goto DoneWithDeclSpec;
810 FuzzyParseMicrosoftDeclSpec();
811 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000812
Steve Naroff44ac7772008-12-25 14:16:32 +0000813 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000814 case tok::kw___forceinline:
815 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +0000816 case tok::kw___cdecl:
817 case tok::kw___stdcall:
818 case tok::kw___fastcall:
819 if (!PP.getLangOptions().Microsoft)
820 goto DoneWithDeclSpec;
821 // Just ignore it.
822 break;
823
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000824 // storage-class-specifier
825 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000826 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000827 break;
828 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000829 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000830 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4d8f8732006-11-28 05:05:08 +0000831 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000832 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000833 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000834 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
835 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000836 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000837 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000838 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000839 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4d8f8732006-11-28 05:05:08 +0000840 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000841 break;
842 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000843 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000844 break;
845 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000846 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000847 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000848 case tok::kw_mutable:
849 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
850 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000851 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000852 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000853 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +0000854
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000855 // function-specifier
856 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000857 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000858 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000859 case tok::kw_virtual:
860 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
861 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000862 case tok::kw_explicit:
863 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
864 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000865
Anders Carlssoncd8db412009-05-06 04:46:28 +0000866 // friend
867 case tok::kw_friend:
868 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
869 break;
870
Chris Lattnere387d9e2009-01-21 19:48:37 +0000871 // type-specifier
872 case tok::kw_short:
873 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
874 break;
875 case tok::kw_long:
876 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
877 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
878 else
879 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
880 break;
881 case tok::kw_signed:
882 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
883 break;
884 case tok::kw_unsigned:
885 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
886 break;
887 case tok::kw__Complex:
888 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
889 break;
890 case tok::kw__Imaginary:
891 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
892 break;
893 case tok::kw_void:
894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
895 break;
896 case tok::kw_char:
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
898 break;
899 case tok::kw_int:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
901 break;
902 case tok::kw_float:
903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
904 break;
905 case tok::kw_double:
906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
907 break;
908 case tok::kw_wchar_t:
909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
910 break;
911 case tok::kw_bool:
912 case tok::kw__Bool:
913 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
914 break;
915 case tok::kw__Decimal32:
916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
917 break;
918 case tok::kw__Decimal64:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
920 break;
921 case tok::kw__Decimal128:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
923 break;
924
925 // class-specifier:
926 case tok::kw_class:
927 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000928 case tok::kw_union: {
929 tok::TokenKind Kind = Tok.getKind();
930 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000931 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000932 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000933 }
Chris Lattnere387d9e2009-01-21 19:48:37 +0000934
935 // enum-specifier:
936 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000937 ConsumeToken();
938 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000939 continue;
940
941 // cv-qualifier:
942 case tok::kw_const:
943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
944 break;
945 case tok::kw_volatile:
946 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
947 getLang())*2;
948 break;
949 case tok::kw_restrict:
950 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
951 getLang())*2;
952 break;
953
Douglas Gregor333489b2009-03-27 23:10:48 +0000954 // C++ typename-specifier:
955 case tok::kw_typename:
956 if (TryAnnotateTypeOrScopeToken())
957 continue;
958 break;
959
Chris Lattnere387d9e2009-01-21 19:48:37 +0000960 // GNU typeof support.
961 case tok::kw_typeof:
962 ParseTypeofSpecifier(DS);
963 continue;
964
Steve Naroffcfdf6162008-06-05 00:02:44 +0000965 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000966 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000967 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
968 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000969 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000970 goto DoneWithDeclSpec;
971
972 {
973 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000974 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000975 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000976 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000977 DS.SetRangeEnd(EndProtoLoc);
978
Chris Lattner6d29c102008-11-18 07:48:38 +0000979 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +0000980 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +0000981 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +0000982 // Need to support trailing type qualifiers (e.g. "id<p> const").
983 // If a type specifier follows, it will be diagnosed elsewhere.
984 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000985 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000986 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000987 // If the specifier combination wasn't legal, issue a diagnostic.
988 if (isInvalid) {
989 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +0000990 // Pick between error or extwarn.
991 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
992 : diag::ext_duplicate_declspec;
993 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000994 }
Chris Lattner2e232092008-03-13 06:29:04 +0000995 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000996 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000997 }
998}
Douglas Gregoreb31f392008-12-01 23:54:00 +0000999
Chris Lattnera448d752009-01-06 06:59:53 +00001000/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001001/// primarily follow the C++ grammar with additions for C99 and GNU,
1002/// which together subsume the C grammar. Note that the C++
1003/// type-specifier also includes the C type-qualifier (for const,
1004/// volatile, and C99 restrict). Returns true if a type-specifier was
1005/// found (and parsed), false otherwise.
1006///
1007/// type-specifier: [C++ 7.1.5]
1008/// simple-type-specifier
1009/// class-specifier
1010/// enum-specifier
1011/// elaborated-type-specifier [TODO]
1012/// cv-qualifier
1013///
1014/// cv-qualifier: [C++ 7.1.5.1]
1015/// 'const'
1016/// 'volatile'
1017/// [C99] 'restrict'
1018///
1019/// simple-type-specifier: [ C++ 7.1.5.2]
1020/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1021/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1022/// 'char'
1023/// 'wchar_t'
1024/// 'bool'
1025/// 'short'
1026/// 'int'
1027/// 'long'
1028/// 'signed'
1029/// 'unsigned'
1030/// 'float'
1031/// 'double'
1032/// 'void'
1033/// [C99] '_Bool'
1034/// [C99] '_Complex'
1035/// [C99] '_Imaginary' // Removed in TC2?
1036/// [GNU] '_Decimal32'
1037/// [GNU] '_Decimal64'
1038/// [GNU] '_Decimal128'
1039/// [GNU] typeof-specifier
1040/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1041/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnera448d752009-01-06 06:59:53 +00001042bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1043 const char *&PrevSpec,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001044 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001045 SourceLocation Loc = Tok.getLocation();
1046
1047 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001048 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001049 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001050 // Annotate typenames and C++ scope specifiers. If we get one, just
1051 // recurse to handle whatever we get.
1052 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001053 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001054 // Otherwise, not a type specifier.
1055 return false;
1056 case tok::coloncolon: // ::foo::bar
1057 if (NextToken().is(tok::kw_new) || // ::new
1058 NextToken().is(tok::kw_delete)) // ::delete
1059 return false;
1060
1061 // Annotate typenames and C++ scope specifiers. If we get one, just
1062 // recurse to handle whatever we get.
1063 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001064 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001065 // Otherwise, not a type specifier.
1066 return false;
1067
Douglas Gregor450c75a2008-11-07 15:42:26 +00001068 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001069 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001070 if (Tok.getAnnotationValue())
1071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1072 Tok.getAnnotationValue());
1073 else
1074 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001075 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1076 ConsumeToken(); // The typename
Douglas Gregor450c75a2008-11-07 15:42:26 +00001077
1078 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1079 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1080 // Objective-C interface. If we don't have Objective-C or a '<', this is
1081 // just a normal reference to a typedef name.
1082 if (!Tok.is(tok::less) || !getLang().ObjC1)
1083 return true;
1084
1085 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001086 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001087 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1088 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1089
1090 DS.SetRangeEnd(EndProtoLoc);
1091 return true;
1092 }
1093
1094 case tok::kw_short:
1095 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1096 break;
1097 case tok::kw_long:
1098 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1099 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1100 else
1101 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1102 break;
1103 case tok::kw_signed:
1104 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1105 break;
1106 case tok::kw_unsigned:
1107 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1108 break;
1109 case tok::kw__Complex:
1110 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1111 break;
1112 case tok::kw__Imaginary:
1113 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1114 break;
1115 case tok::kw_void:
1116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1117 break;
1118 case tok::kw_char:
1119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1120 break;
1121 case tok::kw_int:
1122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1123 break;
1124 case tok::kw_float:
1125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1126 break;
1127 case tok::kw_double:
1128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1129 break;
1130 case tok::kw_wchar_t:
1131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1132 break;
1133 case tok::kw_bool:
1134 case tok::kw__Bool:
1135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1136 break;
1137 case tok::kw__Decimal32:
1138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1139 break;
1140 case tok::kw__Decimal64:
1141 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1142 break;
1143 case tok::kw__Decimal128:
1144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1145 break;
1146
1147 // class-specifier:
1148 case tok::kw_class:
1149 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001150 case tok::kw_union: {
1151 tok::TokenKind Kind = Tok.getKind();
1152 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001153 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001154 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001155 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001156
1157 // enum-specifier:
1158 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001159 ConsumeToken();
1160 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001161 return true;
1162
1163 // cv-qualifier:
1164 case tok::kw_const:
1165 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1166 getLang())*2;
1167 break;
1168 case tok::kw_volatile:
1169 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1170 getLang())*2;
1171 break;
1172 case tok::kw_restrict:
1173 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1174 getLang())*2;
1175 break;
1176
1177 // GNU typeof support.
1178 case tok::kw_typeof:
1179 ParseTypeofSpecifier(DS);
1180 return true;
1181
Steve Naroff44ac7772008-12-25 14:16:32 +00001182 case tok::kw___cdecl:
1183 case tok::kw___stdcall:
1184 case tok::kw___fastcall:
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001185 if (!PP.getLangOptions().Microsoft) return false;
1186 ConsumeToken();
1187 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001188
Douglas Gregor450c75a2008-11-07 15:42:26 +00001189 default:
1190 // Not a type-specifier; do nothing.
1191 return false;
1192 }
1193
1194 // If the specifier combination wasn't legal, issue a diagnostic.
1195 if (isInvalid) {
1196 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001197 // Pick between error or extwarn.
1198 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1199 : diag::ext_duplicate_declspec;
1200 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001201 }
1202 DS.SetRangeEnd(Tok.getLocation());
1203 ConsumeToken(); // whatever we parsed above.
1204 return true;
1205}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001206
Chris Lattner70ae4912007-10-29 04:42:53 +00001207/// ParseStructDeclaration - Parse a struct declaration without the terminating
1208/// semicolon.
1209///
Chris Lattner90a26b02007-01-23 04:38:16 +00001210/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001211/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001212/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001213/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001214/// struct-declarator-list:
1215/// struct-declarator
1216/// struct-declarator-list ',' struct-declarator
1217/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1218/// struct-declarator:
1219/// declarator
1220/// [GNU] declarator attributes[opt]
1221/// declarator[opt] ':' constant-expression
1222/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1223///
Chris Lattnera12405b2008-04-10 06:46:29 +00001224void Parser::
1225ParseStructDeclaration(DeclSpec &DS,
1226 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001227 if (Tok.is(tok::kw___extension__)) {
1228 // __extension__ silences extension warnings in the subexpression.
1229 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001230 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001231 return ParseStructDeclaration(DS, Fields);
1232 }
Steve Naroff97170802007-08-20 22:28:22 +00001233
1234 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001235 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001236 ParseSpecifierQualifierList(DS);
Steve Naroff97170802007-08-20 22:28:22 +00001237
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001238 // If there are no declarators, this is a free-standing declaration
1239 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001240 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001241 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001242 return;
1243 }
1244
1245 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001246 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +00001247 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +00001248 FieldDeclarator &DeclaratorInfo = Fields.back();
1249
Steve Naroff97170802007-08-20 22:28:22 +00001250 /// struct-declarator: declarator
1251 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001252 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001253 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +00001254
Chris Lattner76c72282007-10-09 17:33:22 +00001255 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001256 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001257 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001259 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001260 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001261 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001262 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001263
Steve Naroff97170802007-08-20 22:28:22 +00001264 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001265 if (Tok.is(tok::kw___attribute)) {
1266 SourceLocation Loc;
1267 AttributeList *AttrList = ParseAttributes(&Loc);
1268 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1269 }
1270
Steve Naroff97170802007-08-20 22:28:22 +00001271 // If we don't have a comma, it is either the end of the list (a ';')
1272 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001273 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001274 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001275
Steve Naroff97170802007-08-20 22:28:22 +00001276 // Consume the comma.
1277 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001278
Steve Naroff97170802007-08-20 22:28:22 +00001279 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001280 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001281
Steve Naroff97170802007-08-20 22:28:22 +00001282 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001283 if (Tok.is(tok::kw___attribute)) {
1284 SourceLocation Loc;
1285 AttributeList *AttrList = ParseAttributes(&Loc);
1286 Fields.back().D.AddAttributes(AttrList, Loc);
1287 }
Steve Naroff97170802007-08-20 22:28:22 +00001288 }
Steve Naroff97170802007-08-20 22:28:22 +00001289}
1290
1291/// ParseStructUnionBody
1292/// struct-contents:
1293/// struct-declaration-list
1294/// [EXT] empty
1295/// [GNU] "struct-declaration-list" without terminatoring ';'
1296/// struct-declaration-list:
1297/// struct-declaration
1298/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001299/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001300///
Chris Lattner1300fb92007-01-23 23:42:53 +00001301void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001302 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001303 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1304 PP.getSourceManager(),
1305 "parsing struct/union body");
Chris Lattner477f9902009-03-05 02:25:03 +00001306
Chris Lattner90a26b02007-01-23 04:38:16 +00001307 SourceLocation LBraceLoc = ConsumeBrace();
1308
Douglas Gregor658b9552009-01-09 22:42:13 +00001309 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001310 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1311
Chris Lattner7b9ace62007-01-23 20:11:08 +00001312 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1313 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001314 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001315 Diag(Tok, diag::ext_empty_struct_union_enum)
1316 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001317
Chris Lattner83f095c2009-03-28 19:18:32 +00001318 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001319 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1320
Chris Lattner7b9ace62007-01-23 20:11:08 +00001321 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001322 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001323 // Each iteration of this loop reads one struct-declaration.
1324
Chris Lattner736ed5d2007-06-09 05:59:07 +00001325 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001326 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001327 Diag(Tok, diag::ext_extra_struct_semi)
1328 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001329 ConsumeToken();
1330 continue;
1331 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001332
1333 // Parse all the comma separated declarators.
1334 DeclSpec DS;
1335 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +00001336 if (!Tok.is(tok::at)) {
1337 ParseStructDeclaration(DS, FieldDeclarators);
1338
1339 // Convert them all to fields.
1340 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1341 FieldDeclarator &FD = FieldDeclarators[i];
1342 // Install the declarator into the current TagDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001343 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1344 DS.getSourceRange().getBegin(),
1345 FD.D, FD.BitfieldSize);
Chris Lattner535b8302008-06-21 19:39:06 +00001346 FieldDecls.push_back(Field);
1347 }
1348 } else { // Handle @defs
1349 ConsumeToken();
1350 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1351 Diag(Tok, diag::err_unexpected_at);
1352 SkipUntil(tok::semi, true, true);
1353 continue;
1354 }
1355 ConsumeToken();
1356 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1357 if (!Tok.is(tok::identifier)) {
1358 Diag(Tok, diag::err_expected_ident);
1359 SkipUntil(tok::semi, true, true);
1360 continue;
1361 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001362 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor91f84212008-12-11 16:49:14 +00001363 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1364 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001365 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1366 ConsumeToken();
1367 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1368 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001369
Chris Lattner76c72282007-10-09 17:33:22 +00001370 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001371 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001372 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001373 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001374 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001375 } else {
1376 Diag(Tok, diag::err_expected_semi_decl_list);
1377 // Skip to end of block or statement
1378 SkipUntil(tok::r_brace, true, true);
1379 }
1380 }
1381
Steve Naroff33a1e802007-10-29 21:38:07 +00001382 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001383
Steve Naroffb8371e12007-06-09 03:39:29 +00001384 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001385 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001386 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001387 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001388
1389 Actions.ActOnFields(CurScope,
1390 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1391 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001392 AttrList);
1393 StructScope.Exit();
1394 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner90a26b02007-01-23 04:38:16 +00001395}
1396
1397
Chris Lattner3b561a32006-08-13 00:12:11 +00001398/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001399/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001400/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001401///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001402/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1403/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001404/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001405/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001406///
1407/// [C++] elaborated-type-specifier:
1408/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1409///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001410void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1411 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001412 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001413
1414 AttributeList *Attr = 0;
1415 // If attributes exist after tag, parse them.
1416 if (Tok.is(tok::kw___attribute))
1417 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001418
1419 CXXScopeSpec SS;
Chris Lattnera448d752009-01-06 06:59:53 +00001420 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001421 if (Tok.isNot(tok::identifier)) {
1422 Diag(Tok, diag::err_expected_ident);
1423 if (Tok.isNot(tok::l_brace)) {
1424 // Has no name and is not a definition.
1425 // Skip the rest of this declarator, up until the comma or semicolon.
1426 SkipUntil(tok::comma, true);
1427 return;
1428 }
1429 }
1430 }
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001431
1432 // Must have either 'enum name' or 'enum {...}'.
1433 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1434 Diag(Tok, diag::err_expected_ident_lbrace);
1435
1436 // Skip the rest of this declarator, up until the comma or semicolon.
1437 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001438 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001439 }
1440
1441 // If an identifier is present, consume and remember it.
1442 IdentifierInfo *Name = 0;
1443 SourceLocation NameLoc;
1444 if (Tok.is(tok::identifier)) {
1445 Name = Tok.getIdentifierInfo();
1446 NameLoc = ConsumeToken();
1447 }
1448
1449 // There are three options here. If we have 'enum foo;', then this is a
1450 // forward declaration. If we have 'enum foo {...' then this is a
1451 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1452 //
1453 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1454 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1455 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1456 //
1457 Action::TagKind TK;
1458 if (Tok.is(tok::l_brace))
1459 TK = Action::TK_Definition;
1460 else if (Tok.is(tok::semi))
1461 TK = Action::TK_Declaration;
1462 else
1463 TK = Action::TK_Reference;
Chris Lattner83f095c2009-03-28 19:18:32 +00001464 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1465 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner3b561a32006-08-13 00:12:11 +00001466
Chris Lattner76c72282007-10-09 17:33:22 +00001467 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001468 ParseEnumBody(StartLoc, TagDecl);
1469
Chris Lattner3b561a32006-08-13 00:12:11 +00001470 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001471 const char *PrevSpec = 0;
Chris Lattner83f095c2009-03-28 19:18:32 +00001472 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1473 TagDecl.getAs<void>()))
Chris Lattner6d29c102008-11-18 07:48:38 +00001474 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001475}
1476
Chris Lattnerc1915e22007-01-25 07:29:02 +00001477/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1478/// enumerator-list:
1479/// enumerator
1480/// enumerator-list ',' enumerator
1481/// enumerator:
1482/// enumeration-constant
1483/// enumeration-constant '=' constant-expression
1484/// enumeration-constant:
1485/// identifier
1486///
Chris Lattner83f095c2009-03-28 19:18:32 +00001487void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001488 // Enter the scope of the enum body and start the definition.
1489 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001490 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001491
Chris Lattnerc1915e22007-01-25 07:29:02 +00001492 SourceLocation LBraceLoc = ConsumeBrace();
1493
Chris Lattner37256fb2007-08-27 17:24:30 +00001494 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001495 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001496 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattnerc1915e22007-01-25 07:29:02 +00001497
Chris Lattner83f095c2009-03-28 19:18:32 +00001498 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001499
Chris Lattner83f095c2009-03-28 19:18:32 +00001500 DeclPtrTy LastEnumConstDecl;
Chris Lattner4ef40012007-06-11 01:28:17 +00001501
Chris Lattnerc1915e22007-01-25 07:29:02 +00001502 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001503 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001504 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1505 SourceLocation IdentLoc = ConsumeToken();
1506
1507 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001508 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001509 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001510 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001511 AssignedVal = ParseConstantExpression();
1512 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001513 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001514 }
1515
1516 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001517 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1518 LastEnumConstDecl,
1519 IdentLoc, Ident,
1520 EqualLoc,
1521 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001522 EnumConstantDecls.push_back(EnumConstDecl);
1523 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001524
Chris Lattner76c72282007-10-09 17:33:22 +00001525 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001526 break;
1527 SourceLocation CommaLoc = ConsumeToken();
1528
Douglas Gregore3e01a22009-04-01 22:41:11 +00001529 if (Tok.isNot(tok::identifier) &&
1530 !(getLang().C99 || getLang().CPlusPlus0x))
1531 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1532 << getLang().CPlusPlus
1533 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001534 }
1535
1536 // Eat the }.
1537 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1538
Steve Naroff30d242c2007-09-15 18:49:24 +00001539 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +00001540 EnumConstantDecls.size());
1541
Chris Lattner83f095c2009-03-28 19:18:32 +00001542 Action::AttrTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001543 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001544 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +00001545 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001546
1547 EnumScope.Exit();
1548 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001549}
Chris Lattner3b561a32006-08-13 00:12:11 +00001550
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001551/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001552/// start of a type-qualifier-list.
1553bool Parser::isTypeQualifier() const {
1554 switch (Tok.getKind()) {
1555 default: return false;
1556 // type-qualifier
1557 case tok::kw_const:
1558 case tok::kw_volatile:
1559 case tok::kw_restrict:
1560 return true;
1561 }
1562}
1563
1564/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001565/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001566bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001567 switch (Tok.getKind()) {
1568 default: return false;
Chris Lattner020bab92009-01-04 23:41:41 +00001569
1570 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001571 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001572 // Annotate typenames and C++ scope specifiers. If we get one, just
1573 // recurse to handle whatever we get.
1574 if (TryAnnotateTypeOrScopeToken())
1575 return isTypeSpecifierQualifier();
1576 // Otherwise, not a type specifier.
1577 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001578
Chris Lattner020bab92009-01-04 23:41:41 +00001579 case tok::coloncolon: // ::foo::bar
1580 if (NextToken().is(tok::kw_new) || // ::new
1581 NextToken().is(tok::kw_delete)) // ::delete
1582 return false;
1583
1584 // Annotate typenames and C++ scope specifiers. If we get one, just
1585 // recurse to handle whatever we get.
1586 if (TryAnnotateTypeOrScopeToken())
1587 return isTypeSpecifierQualifier();
1588 // Otherwise, not a type specifier.
1589 return false;
1590
Chris Lattnere37e2332006-08-15 04:50:22 +00001591 // GNU attributes support.
1592 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001593 // GNU typeof support.
1594 case tok::kw_typeof:
1595
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001596 // type-specifiers
1597 case tok::kw_short:
1598 case tok::kw_long:
1599 case tok::kw_signed:
1600 case tok::kw_unsigned:
1601 case tok::kw__Complex:
1602 case tok::kw__Imaginary:
1603 case tok::kw_void:
1604 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001605 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001606 case tok::kw_int:
1607 case tok::kw_float:
1608 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001609 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001610 case tok::kw__Bool:
1611 case tok::kw__Decimal32:
1612 case tok::kw__Decimal64:
1613 case tok::kw__Decimal128:
1614
Chris Lattner861a2262008-04-13 18:59:07 +00001615 // struct-or-union-specifier (C99) or class-specifier (C++)
1616 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001617 case tok::kw_struct:
1618 case tok::kw_union:
1619 // enum-specifier
1620 case tok::kw_enum:
1621
1622 // type-qualifier
1623 case tok::kw_const:
1624 case tok::kw_volatile:
1625 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001626
1627 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001628 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001629 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +00001630
1631 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1632 case tok::less:
1633 return getLang().ObjC1;
Steve Naroff44ac7772008-12-25 14:16:32 +00001634
1635 case tok::kw___cdecl:
1636 case tok::kw___stdcall:
1637 case tok::kw___fastcall:
1638 return PP.getLangOptions().Microsoft;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001639 }
1640}
1641
Chris Lattneracd58a32006-08-06 17:24:14 +00001642/// isDeclarationSpecifier() - Return true if the current token is part of a
1643/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001644bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001645 switch (Tok.getKind()) {
1646 default: return false;
Chris Lattner020bab92009-01-04 23:41:41 +00001647
1648 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001649 // Unfortunate hack to support "Class.factoryMethod" notation.
1650 if (getLang().ObjC1 && NextToken().is(tok::period))
1651 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001652 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001653
Douglas Gregor333489b2009-03-27 23:10:48 +00001654 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001655 // Annotate typenames and C++ scope specifiers. If we get one, just
1656 // recurse to handle whatever we get.
1657 if (TryAnnotateTypeOrScopeToken())
1658 return isDeclarationSpecifier();
1659 // Otherwise, not a declaration specifier.
1660 return false;
1661 case tok::coloncolon: // ::foo::bar
1662 if (NextToken().is(tok::kw_new) || // ::new
1663 NextToken().is(tok::kw_delete)) // ::delete
1664 return false;
1665
1666 // Annotate typenames and C++ scope specifiers. If we get one, just
1667 // recurse to handle whatever we get.
1668 if (TryAnnotateTypeOrScopeToken())
1669 return isDeclarationSpecifier();
1670 // Otherwise, not a declaration specifier.
1671 return false;
1672
Chris Lattneracd58a32006-08-06 17:24:14 +00001673 // storage-class-specifier
1674 case tok::kw_typedef:
1675 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001676 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001677 case tok::kw_static:
1678 case tok::kw_auto:
1679 case tok::kw_register:
1680 case tok::kw___thread:
1681
1682 // type-specifiers
1683 case tok::kw_short:
1684 case tok::kw_long:
1685 case tok::kw_signed:
1686 case tok::kw_unsigned:
1687 case tok::kw__Complex:
1688 case tok::kw__Imaginary:
1689 case tok::kw_void:
1690 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001691 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001692 case tok::kw_int:
1693 case tok::kw_float:
1694 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001695 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001696 case tok::kw__Bool:
1697 case tok::kw__Decimal32:
1698 case tok::kw__Decimal64:
1699 case tok::kw__Decimal128:
1700
Chris Lattner861a2262008-04-13 18:59:07 +00001701 // struct-or-union-specifier (C99) or class-specifier (C++)
1702 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001703 case tok::kw_struct:
1704 case tok::kw_union:
1705 // enum-specifier
1706 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001707
Chris Lattneracd58a32006-08-06 17:24:14 +00001708 // type-qualifier
1709 case tok::kw_const:
1710 case tok::kw_volatile:
1711 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001712
Chris Lattneracd58a32006-08-06 17:24:14 +00001713 // function-specifier
1714 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001715 case tok::kw_virtual:
1716 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001717
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001718 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001719 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001720
Chris Lattner599e47e2007-08-09 17:01:07 +00001721 // GNU typeof support.
1722 case tok::kw_typeof:
1723
1724 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001725 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001726 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001727
1728 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1729 case tok::less:
1730 return getLang().ObjC1;
Steve Naroff44ac7772008-12-25 14:16:32 +00001731
Steve Narofff192fab2009-01-06 19:34:12 +00001732 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00001733 case tok::kw___cdecl:
1734 case tok::kw___stdcall:
1735 case tok::kw___fastcall:
1736 return PP.getLangOptions().Microsoft;
Chris Lattneracd58a32006-08-06 17:24:14 +00001737 }
1738}
1739
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001740
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001741/// ParseTypeQualifierListOpt
1742/// type-qualifier-list: [C99 6.7.5]
1743/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001744/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001745/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001746/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001747///
Chris Lattnercf0bab22008-12-18 07:02:59 +00001748void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001749 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001750 int isInvalid = false;
1751 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001752 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001753
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001754 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001755 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001756 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1757 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001758 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001759 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001760 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1761 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001762 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001763 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001764 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1765 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001766 break;
Steve Narofff9c29d42008-12-25 14:41:26 +00001767 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001768 case tok::kw___cdecl:
1769 case tok::kw___stdcall:
1770 case tok::kw___fastcall:
1771 if (!PP.getLangOptions().Microsoft)
1772 goto DoneWithTypeQuals;
1773 // Just ignore it.
1774 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001775 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001776 if (AttributesAllowed) {
1777 DS.AddAttributes(ParseAttributes());
1778 continue; // do *not* consume the next token!
1779 }
1780 // otherwise, FALL THROUGH!
1781 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00001782 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001783 // If this is not a type-qualifier token, we're done reading type
1784 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001785 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00001786 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001787 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00001788
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001789 // If the specifier combination wasn't legal, issue a diagnostic.
1790 if (isInvalid) {
1791 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001792 // Pick between error or extwarn.
1793 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1794 : diag::ext_duplicate_declspec;
1795 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001796 }
1797 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001798 }
1799}
1800
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001801
1802/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1803///
1804void Parser::ParseDeclarator(Declarator &D) {
1805 /// This implements the 'declarator' production in the C grammar, then checks
1806 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001807 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001808}
1809
Sebastian Redlbd150f42008-11-21 19:14:01 +00001810/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1811/// is parsed by the function passed to it. Pass null, and the direct-declarator
1812/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001813/// ptr-operator production.
1814///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001815/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1816/// [C] pointer[opt] direct-declarator
1817/// [C++] direct-declarator
1818/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001819///
1820/// pointer: [C99 6.7.5]
1821/// '*' type-qualifier-list[opt]
1822/// '*' type-qualifier-list[opt] pointer
1823///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001824/// ptr-operator:
1825/// '*' cv-qualifier-seq[opt]
1826/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00001827/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001828/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00001829/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001830/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00001831void Parser::ParseDeclaratorInternal(Declarator &D,
1832 DirectDeclParseFunction DirectDeclParser) {
Bill Wendling3708c182007-05-27 10:15:43 +00001833
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001834 // C++ member pointers start with a '::' or a nested-name.
1835 // Member pointers get special handling, since there's no place for the
1836 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00001837 if (getLang().CPlusPlus &&
1838 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1839 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001840 CXXScopeSpec SS;
1841 if (ParseOptionalCXXScopeSpecifier(SS)) {
1842 if(Tok.isNot(tok::star)) {
1843 // The scope spec really belongs to the direct-declarator.
1844 D.getCXXScopeSpec() = SS;
1845 if (DirectDeclParser)
1846 (this->*DirectDeclParser)(D);
1847 return;
1848 }
1849
1850 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001851 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001852 DeclSpec DS;
1853 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001854 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001855
1856 // Recurse to parse whatever is left.
1857 ParseDeclaratorInternal(D, DirectDeclParser);
1858
1859 // Sema will have to catch (syntactically invalid) pointers into global
1860 // scope. It has to catch pointers into namespace scope anyway.
1861 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001862 Loc, DS.TakeAttributes()),
1863 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001864 return;
1865 }
1866 }
1867
1868 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00001869 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00001870 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00001871 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00001872 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00001873 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001874 if (DirectDeclParser)
1875 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001876 return;
1877 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001878
Sebastian Redled0f3b02009-03-15 22:02:01 +00001879 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1880 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00001881 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001882 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00001883
Chris Lattner9eac9312009-03-27 04:18:06 +00001884 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00001885 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001886 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001887
Bill Wendling3708c182007-05-27 10:15:43 +00001888 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001889 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001890
Bill Wendling3708c182007-05-27 10:15:43 +00001891 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001892 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00001893 if (Kind == tok::star)
1894 // Remember that we parsed a pointer type, and remember the type-quals.
1895 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001896 DS.TakeAttributes()),
1897 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00001898 else
1899 // Remember that we parsed a Block type, and remember the type-quals.
1900 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00001901 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001902 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00001903 } else {
1904 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001905 DeclSpec DS;
1906
Sebastian Redl3b27be62009-03-23 00:00:23 +00001907 // Complain about rvalue references in C++03, but then go on and build
1908 // the declarator.
1909 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1910 Diag(Loc, diag::err_rvalue_reference);
1911
Bill Wendling93efb222007-06-02 23:28:54 +00001912 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1913 // cv-qualifiers are introduced through the use of a typedef or of a
1914 // template type argument, in which case the cv-qualifiers are ignored.
1915 //
1916 // [GNU] Retricted references are allowed.
1917 // [GNU] Attributes on references are allowed.
1918 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001919 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00001920
1921 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1922 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1923 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00001924 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00001925 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1926 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00001927 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00001928 }
Bill Wendling3708c182007-05-27 10:15:43 +00001929
1930 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001931 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00001932
Douglas Gregor66583c52008-11-03 15:51:28 +00001933 if (D.getNumTypeObjects() > 0) {
1934 // C++ [dcl.ref]p4: There shall be no references to references.
1935 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1936 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00001937 if (const IdentifierInfo *II = D.getIdentifier())
1938 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1939 << II;
1940 else
1941 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1942 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00001943
Sebastian Redlbd150f42008-11-21 19:14:01 +00001944 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00001945 // can go ahead and build the (technically ill-formed)
1946 // declarator: reference collapsing will take care of it.
1947 }
1948 }
1949
Bill Wendling3708c182007-05-27 10:15:43 +00001950 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001951 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00001952 DS.TakeAttributes(),
1953 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001954 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00001955 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001956}
1957
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001958/// ParseDirectDeclarator
1959/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00001960/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001961/// '(' declarator ')'
1962/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001963/// [C90] direct-declarator '[' constant-expression[opt] ']'
1964/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1965/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1966/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1967/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001968/// direct-declarator '(' parameter-type-list ')'
1969/// direct-declarator '(' identifier-list[opt] ')'
1970/// [GNU] direct-declarator '(' parameter-forward-declarations
1971/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001972/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1973/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00001974/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00001975///
1976/// declarator-id: [C++ 8]
1977/// id-expression
1978/// '::'[opt] nested-name-specifier[opt] type-name
1979///
1980/// id-expression: [C++ 5.1]
1981/// unqualified-id
1982/// qualified-id [TODO]
1983///
1984/// unqualified-id: [C++ 5.1]
1985/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001986/// operator-function-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00001987/// conversion-function-id [TODO]
1988/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00001989/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00001990///
Chris Lattneracd58a32006-08-06 17:24:14 +00001991void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001992 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001993
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001994 if (getLang().CPlusPlus) {
1995 if (D.mayHaveIdentifier()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001996 // ParseDeclaratorInternal might already have parsed the scope.
1997 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1998 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001999 if (afterCXXScope) {
2000 // Change the declaration context for name lookup, until this function
2001 // is exited (and the declarator has been parsed).
2002 DeclScopeObj.EnterDeclaratorScope();
2003 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002004
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002005 if (Tok.is(tok::identifier)) {
2006 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssona0886932009-04-30 22:41:11 +00002007
2008 // If this identifier is the name of the current class, it's a
2009 // constructor name.
2010 if (!D.getDeclSpec().hasTypeSpecifier() &&
2011 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2012 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2013 Tok.getLocation(), CurScope),
2014 Tok.getLocation());
2015 // This is a normal identifier.
2016 } else
2017 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002018 ConsumeToken();
2019 goto PastIdentifier;
Douglas Gregor7f741122009-02-25 19:37:18 +00002020 } else if (Tok.is(tok::annot_template_id)) {
2021 TemplateIdAnnotation *TemplateId
2022 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2023
2024 // FIXME: Could this template-id name a constructor?
2025
2026 // FIXME: This is an egregious hack, where we silently ignore
2027 // the specialization (which should be a function template
2028 // specialization name) and use the name instead. This hack
2029 // will go away when we have support for function
2030 // specializations.
2031 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2032 TemplateId->Destroy();
2033 ConsumeToken();
2034 goto PastIdentifier;
Douglas Gregor1dc98262008-12-26 15:00:45 +00002035 } else if (Tok.is(tok::kw_operator)) {
2036 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002037 SourceLocation EndLoc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002038
Douglas Gregor1dc98262008-12-26 15:00:45 +00002039 // First try the name of an overloaded operator
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002040 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2041 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor1dc98262008-12-26 15:00:45 +00002042 } else {
2043 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002044 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2045 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2046 else {
Douglas Gregor1dc98262008-12-26 15:00:45 +00002047 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002048 }
Douglas Gregor1dc98262008-12-26 15:00:45 +00002049 }
2050 goto PastIdentifier;
2051 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002052 // This should be a C++ destructor.
2053 SourceLocation TildeLoc = ConsumeToken();
2054 if (Tok.is(tok::identifier)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002055 // FIXME: Inaccurate.
2056 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregord54dfb82009-02-25 23:52:28 +00002057 SourceLocation EndLoc;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002058 TypeResult Type = ParseClassName(EndLoc);
2059 if (Type.isInvalid())
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002060 D.SetIdentifier(0, TildeLoc);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002061 else
2062 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002063 } else {
2064 Diag(Tok, diag::err_expected_class_name);
2065 D.SetIdentifier(0, TildeLoc);
2066 }
2067 goto PastIdentifier;
2068 }
2069
2070 // If we reached this point, token is not identifier and not '~'.
2071
2072 if (afterCXXScope) {
2073 Diag(Tok, diag::err_expected_unqualified_id);
2074 D.SetIdentifier(0, Tok.getLocation());
2075 D.setInvalidType(true);
2076 goto PastIdentifier;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002077 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002078 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002079 }
2080
2081 // If we reached this point, we are either in C/ObjC or the token didn't
2082 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002083 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2084 assert(!getLang().CPlusPlus &&
2085 "There's a C++-specific check for tok::identifier above");
2086 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2087 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2088 ConsumeToken();
2089 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002090 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002091 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002092 // Example: 'char (*X)' or 'int (*XX)(void)'
2093 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002094 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002095 // This could be something simple like "int" (in which case the declarator
2096 // portion is empty), if an abstract-declarator is allowed.
2097 D.SetIdentifier(0, Tok.getLocation());
2098 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002099 if (D.getContext() == Declarator::MemberContext)
2100 Diag(Tok, diag::err_expected_member_name_or_semi)
2101 << D.getDeclSpec().getSourceRange();
2102 else if (getLang().CPlusPlus)
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002103 Diag(Tok, diag::err_expected_unqualified_id);
2104 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002105 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002106 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002107 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002108 }
2109
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002110 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002111 assert(D.isPastIdentifier() &&
2112 "Haven't past the location of the identifier yet?");
2113
2114 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002115 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002116 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2117 // In such a case, check if we actually have a function declarator; if it
2118 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002119 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2120 // When not in file scope, warn for ambiguous function declarators, just
2121 // in case the author intended it as a variable definition.
2122 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2123 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2124 break;
2125 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002126 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002127 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002128 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002129 } else {
2130 break;
2131 }
2132 }
2133}
2134
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002135/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2136/// only called before the identifier, so these are most likely just grouping
2137/// parens for precedence. If we find that these are actually function
2138/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2139///
2140/// direct-declarator:
2141/// '(' declarator ')'
2142/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002143/// direct-declarator '(' parameter-type-list ')'
2144/// direct-declarator '(' identifier-list[opt] ')'
2145/// [GNU] direct-declarator '(' parameter-forward-declarations
2146/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002147///
2148void Parser::ParseParenDeclarator(Declarator &D) {
2149 SourceLocation StartLoc = ConsumeParen();
2150 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2151
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002152 // Eat any attributes before we look at whether this is a grouping or function
2153 // declarator paren. If this is a grouping paren, the attribute applies to
2154 // the type being built up, for example:
2155 // int (__attribute__(()) *x)(long y)
2156 // If this ends up not being a grouping paren, the attribute applies to the
2157 // first argument, for example:
2158 // int (__attribute__(()) int x)
2159 // In either case, we need to eat any attributes to be able to determine what
2160 // sort of paren this is.
2161 //
2162 AttributeList *AttrList = 0;
2163 bool RequiresArg = false;
2164 if (Tok.is(tok::kw___attribute)) {
2165 AttrList = ParseAttributes();
2166
2167 // We require that the argument list (if this is a non-grouping paren) be
2168 // present even if the attribute list was empty.
2169 RequiresArg = true;
2170 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002171 // Eat any Microsoft extensions.
Douglas Gregorb37080a2009-01-10 00:48:18 +00002172 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2173 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff44ac7772008-12-25 14:16:32 +00002174 ConsumeToken();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002175
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002176 // If we haven't past the identifier yet (or where the identifier would be
2177 // stored, if this is an abstract declarator), then this is probably just
2178 // grouping parens. However, if this could be an abstract-declarator, then
2179 // this could also be the start of function arguments (consider 'void()').
2180 bool isGrouping;
2181
2182 if (!D.mayOmitIdentifier()) {
2183 // If this can't be an abstract-declarator, this *must* be a grouping
2184 // paren, because we haven't seen the identifier yet.
2185 isGrouping = true;
2186 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002187 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002188 isDeclarationSpecifier()) { // 'int(int)' is a function.
2189 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2190 // considered to be a type, not a K&R identifier-list.
2191 isGrouping = false;
2192 } else {
2193 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2194 isGrouping = true;
2195 }
2196
2197 // If this is a grouping paren, handle:
2198 // direct-declarator: '(' declarator ')'
2199 // direct-declarator: '(' attributes declarator ')'
2200 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002201 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002202 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002203 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002204 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002205
Sebastian Redlbd150f42008-11-21 19:14:01 +00002206 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002207 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002208 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002209
2210 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002211 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002212 return;
2213 }
2214
2215 // Okay, if this wasn't a grouping paren, it must be the start of a function
2216 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002217 // identifier (and remember where it would have been), then call into
2218 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002219 D.SetIdentifier(0, Tok.getLocation());
2220
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002221 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002222}
2223
2224/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2225/// declarator D up to a paren, which indicates that we are parsing function
2226/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002227///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002228/// If AttrList is non-null, then the caller parsed those arguments immediately
2229/// after the open paren - they should be considered to be the first argument of
2230/// a parameter. If RequiresArg is true, then the first argument of the
2231/// function is required to be present and required to not be an identifier
2232/// list.
2233///
Chris Lattneracd58a32006-08-06 17:24:14 +00002234/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002235/// parameter-type-list: [C99 6.7.5]
2236/// parameter-list
2237/// parameter-list ',' '...'
2238///
2239/// parameter-list: [C99 6.7.5]
2240/// parameter-declaration
2241/// parameter-list ',' parameter-declaration
2242///
2243/// parameter-declaration: [C99 6.7.5]
2244/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002245/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002246/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002247/// declaration-specifiers abstract-declarator[opt]
2248/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002249/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002250/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002251///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002252/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002253/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002254///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002255void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2256 AttributeList *AttrList,
2257 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002258 // lparen is already consumed!
2259 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002260
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002261 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002262 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002263 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002264 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002265 delete AttrList;
2266 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002267
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002268 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002269
2270 // cv-qualifier-seq[opt].
2271 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002272 bool hasExceptionSpec = false;
2273 bool hasAnyExceptionSpec = false;
2274 // FIXME: Does an empty vector ever allocate? Exception specifications are
2275 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2276 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002277 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002278 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002279 if (!DS.getSourceRange().getEnd().isInvalid())
2280 Loc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002281
2282 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002283 if (Tok.is(tok::kw_throw)) {
2284 hasExceptionSpec = true;
2285 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2286 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002287 }
2288
Chris Lattner371ed4e2008-04-06 06:57:35 +00002289 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002290 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002291 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002292 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002293 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002294 /*arglist*/ 0, 0,
2295 DS.getTypeQualifiers(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002296 hasExceptionSpec,
2297 hasAnyExceptionSpec,
2298 Exceptions.empty() ? 0 :
2299 &Exceptions[0],
2300 Exceptions.size(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002301 LParenLoc, D),
2302 Loc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002303 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002304 }
2305
2306 // Alternatively, this parameter list may be an identifier list form for a
2307 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002308 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002309 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002310 // K&R identifier lists can't have typedefs as identifiers, per
2311 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002312 if (RequiresArg) {
2313 Diag(Tok, diag::err_argument_required_after_attribute);
2314 delete AttrList;
2315 }
Steve Naroffb0486722009-01-28 19:16:40 +00002316 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2317 // normal declarators, not for abstract-declarators.
2318 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002319 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002320 }
2321
2322 // Finally, a normal, non-empty parameter type list.
2323
2324 // Build up an array of information about the parsed arguments.
2325 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002326
2327 // Enter function-declaration scope, limiting any declarators to the
2328 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002329 ParseScope PrototypeScope(this,
2330 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002331
2332 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002333 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002334 while (1) {
2335 if (Tok.is(tok::ellipsis)) {
2336 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002337 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002338 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002339 }
2340
Chris Lattner371ed4e2008-04-06 06:57:35 +00002341 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00002342
Chris Lattner371ed4e2008-04-06 06:57:35 +00002343 // Parse the declaration-specifiers.
2344 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002345
2346 // If the caller parsed attributes for the first argument, add them now.
2347 if (AttrList) {
2348 DS.AddAttributes(AttrList);
2349 AttrList = 0; // Only apply the attributes to the first parameter.
2350 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002351 ParseDeclarationSpecifiers(DS);
2352
Chris Lattner371ed4e2008-04-06 06:57:35 +00002353 // Parse the declarator. This is "PrototypeContext", because we must
2354 // accept either 'declarator' or 'abstract-declarator' here.
2355 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2356 ParseDeclarator(ParmDecl);
2357
2358 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002359 if (Tok.is(tok::kw___attribute)) {
2360 SourceLocation Loc;
2361 AttributeList *AttrList = ParseAttributes(&Loc);
2362 ParmDecl.AddAttributes(AttrList, Loc);
2363 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002364
Chris Lattner371ed4e2008-04-06 06:57:35 +00002365 // Remember this parsed parameter in ParamInfo.
2366 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2367
Douglas Gregor4d87df52008-12-16 21:30:33 +00002368 // DefArgToks is used when the parsing of default arguments needs
2369 // to be delayed.
2370 CachedTokens *DefArgToks = 0;
2371
Chris Lattner371ed4e2008-04-06 06:57:35 +00002372 // If no parameter was specified, verify that *something* was specified,
2373 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002374 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2375 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002376 // Completely missing, emit error.
2377 Diag(DSStart, diag::err_missing_param);
2378 } else {
2379 // Otherwise, we have something. Add it and let semantic analysis try
2380 // to grok it and add the result to the ParamInfo we are building.
2381
2382 // Inform the actions module about the parameter declarator, so it gets
2383 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002384 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002385
2386 // Parse the default argument, if any. We parse the default
2387 // arguments in all dialects; the semantic analysis in
2388 // ActOnParamDefaultArgument will reject the default argument in
2389 // C.
2390 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002391 SourceLocation EqualLoc = Tok.getLocation();
2392
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002393 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002394 if (D.getContext() == Declarator::MemberContext) {
2395 // If we're inside a class definition, cache the tokens
2396 // corresponding to the default argument. We'll actually parse
2397 // them when we see the end of the class definition.
2398 // FIXME: Templates will require something similar.
2399 // FIXME: Can we use a smart pointer for Toks?
2400 DefArgToks = new CachedTokens;
2401
2402 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2403 tok::semi, false)) {
2404 delete DefArgToks;
2405 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002406 Actions.ActOnParamDefaultArgumentError(Param);
2407 } else
2408 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002409 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002410 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002411 ConsumeToken();
Douglas Gregor4d87df52008-12-16 21:30:33 +00002412
2413 OwningExprResult DefArgResult(ParseAssignmentExpression());
2414 if (DefArgResult.isInvalid()) {
2415 Actions.ActOnParamDefaultArgumentError(Param);
2416 SkipUntil(tok::comma, tok::r_paren, true, true);
2417 } else {
2418 // Inform the actions module about the default argument
2419 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002420 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002421 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002422 }
2423 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002424
2425 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002426 ParmDecl.getIdentifierLoc(), Param,
2427 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002428 }
2429
2430 // If the next token is a comma, consume it and keep reading arguments.
2431 if (Tok.isNot(tok::comma)) break;
2432
2433 // Consume the comma.
2434 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002435 }
2436
Chris Lattner371ed4e2008-04-06 06:57:35 +00002437 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002438 PrototypeScope.Exit();
Chris Lattner371ed4e2008-04-06 06:57:35 +00002439
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002440 // If we have the closing ')', eat it.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002441 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002442
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002443 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002444 bool hasExceptionSpec = false;
2445 bool hasAnyExceptionSpec = false;
2446 // FIXME: Does an empty vector ever allocate? Exception specifications are
2447 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2448 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002449 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002450 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002451 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002452 if (!DS.getSourceRange().getEnd().isInvalid())
2453 Loc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002454
2455 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002456 if (Tok.is(tok::kw_throw)) {
2457 hasExceptionSpec = true;
2458 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2459 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002460 }
2461
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002462 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002463 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002464 EllipsisLoc,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002465 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002466 DS.getTypeQualifiers(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002467 hasExceptionSpec,
2468 hasAnyExceptionSpec,
2469 Exceptions.empty() ? 0 :
2470 &Exceptions[0],
2471 Exceptions.size(), LParenLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002472 Loc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002473}
Chris Lattneracd58a32006-08-06 17:24:14 +00002474
Chris Lattner6c940e62008-04-06 06:34:08 +00002475/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2476/// we found a K&R-style identifier list instead of a type argument list. The
2477/// current token is known to be the first identifier in the list.
2478///
2479/// identifier-list: [C99 6.7.5]
2480/// identifier
2481/// identifier-list ',' identifier
2482///
2483void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2484 Declarator &D) {
2485 // Build up an array of information about the parsed arguments.
2486 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2487 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2488
2489 // If there was no identifier specified for the declarator, either we are in
2490 // an abstract-declarator, or we are in a parameter declarator which was found
2491 // to be abstract. In abstract-declarators, identifier lists are not valid:
2492 // diagnose this.
2493 if (!D.getIdentifier())
2494 Diag(Tok, diag::ext_ident_list_in_param);
2495
2496 // Tok is known to be the first identifier in the list. Remember this
2497 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002498 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002499 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002500 Tok.getLocation(),
2501 DeclPtrTy()));
Chris Lattner6c940e62008-04-06 06:34:08 +00002502
Chris Lattner9186f552008-04-06 06:39:19 +00002503 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00002504
2505 while (Tok.is(tok::comma)) {
2506 // Eat the comma.
2507 ConsumeToken();
2508
Chris Lattner9186f552008-04-06 06:39:19 +00002509 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002510 if (Tok.isNot(tok::identifier)) {
2511 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002512 SkipUntil(tok::r_paren);
2513 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002514 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002515
Chris Lattner6c940e62008-04-06 06:34:08 +00002516 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002517
2518 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002519 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002520 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner6c940e62008-04-06 06:34:08 +00002521
2522 // Verify that the argument identifier has not already been mentioned.
2523 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002524 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002525 } else {
2526 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002527 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002528 Tok.getLocation(),
2529 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002530 }
Chris Lattner6c940e62008-04-06 06:34:08 +00002531
2532 // Eat the identifier.
2533 ConsumeToken();
2534 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002535
2536 // If we have the closing ')', eat it and we're done.
2537 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2538
Chris Lattner9186f552008-04-06 06:39:19 +00002539 // Remember that we parsed a function type, and remember the attributes. This
2540 // function type is always a K&R style function type, which is not varargs and
2541 // has no prototype.
2542 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002543 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002544 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002545 /*TypeQuals*/0,
2546 /*exception*/false, false, 0, 0,
2547 LParenLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002548 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002549}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002550
Chris Lattnere8074e62006-08-06 18:30:15 +00002551/// [C90] direct-declarator '[' constant-expression[opt] ']'
2552/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2553/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2554/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2555/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2556void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002557 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00002558
Chris Lattner84a11622008-12-18 07:27:21 +00002559 // C array syntax has many features, but by-far the most common is [] and [4].
2560 // This code does a fast path to handle some of the most obvious cases.
2561 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002562 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002563 // Remember that we parsed the empty array type.
2564 OwningExprResult NumElements(Actions);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002565 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2566 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002567 return;
2568 } else if (Tok.getKind() == tok::numeric_constant &&
2569 GetLookAheadToken(1).is(tok::r_square)) {
2570 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002571 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002572 ConsumeToken();
2573
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002574 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002575
2576 // If there was an error parsing the assignment-expression, recover.
2577 if (ExprRes.isInvalid())
2578 ExprRes.release(); // Deallocate expr, just use [].
2579
2580 // Remember that we parsed a array type, and remember its features.
2581 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002582 ExprRes.release(), StartLoc),
2583 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002584 return;
2585 }
2586
Chris Lattnere8074e62006-08-06 18:30:15 +00002587 // If valid, this location is the position where we read the 'static' keyword.
2588 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002589 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002590 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00002591
2592 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002593 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002594 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002595 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattnere8074e62006-08-06 18:30:15 +00002596
2597 // If we haven't already read 'static', check to see if there is one after the
2598 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002599 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002600 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00002601
2602 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002603 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002604 OwningExprResult NumElements(Actions);
Chris Lattner521ff2b2008-04-06 05:26:30 +00002605
2606 // Handle the case where we have '[*]' as the array size. However, a leading
2607 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2608 // the the token after the star is a ']'. Since stars in arrays are
2609 // infrequent, use of lookahead is not costly here.
2610 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002611 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002612
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002613 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002614 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002615 StaticLoc = SourceLocation(); // Drop the static.
2616 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002617 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002618 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002619 // Note, in C89, this production uses the constant-expr production instead
2620 // of assignment-expr. The only difference is that assignment-expr allows
2621 // things like '=' and '*='. Sema rejects these in C89 mode because they
2622 // are not i-c-e's, so we don't need to distinguish between the two here.
2623
Chris Lattnere8074e62006-08-06 18:30:15 +00002624 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00002625 NumElements = ParseAssignmentExpression();
2626 }
2627
2628 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002629 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002630 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002631 // If the expression was invalid, skip it.
2632 SkipUntil(tok::r_square);
2633 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002634 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002635
2636 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2637
Chris Lattner84a11622008-12-18 07:27:21 +00002638 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002639 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2640 StaticLoc.isValid(), isStar,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002641 NumElements.release(), StartLoc),
2642 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002643}
2644
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002645/// [GNU] typeof-specifier:
2646/// typeof ( expressions )
2647/// typeof ( type-name )
2648/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002649///
2650void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002651 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00002652 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00002653 SourceLocation StartLoc = ConsumeToken();
2654
Chris Lattner76c72282007-10-09 17:33:22 +00002655 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002656 if (!getLang().CPlusPlus) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002657 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002658 return;
2659 }
2660
Sebastian Redl59b5e512008-12-11 21:36:32 +00002661 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor220cac52009-02-18 17:45:20 +00002662 if (Result.isInvalid()) {
2663 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002664 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002665 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002666
2667 const char *PrevSpec = 0;
2668 // Check for duplicate type specifiers.
2669 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002670 Result.release()))
Chris Lattner6d29c102008-11-18 07:48:38 +00002671 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002672
2673 // FIXME: Not accurate, the range gets one token more than it should.
2674 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00002675 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002676 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002677
Steve Naroffad373bd2007-07-31 12:34:36 +00002678 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2679
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00002680 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00002681 Action::TypeResult Ty = ParseTypeName();
Steve Naroffad373bd2007-07-31 12:34:36 +00002682
Douglas Gregor220cac52009-02-18 17:45:20 +00002683 assert((Ty.isInvalid() || Ty.get()) &&
2684 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff872da802007-07-31 23:56:32 +00002685
Chris Lattner76c72282007-10-09 17:33:22 +00002686 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00002687 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00002688 return;
2689 }
2690 RParenLoc = ConsumeParen();
Douglas Gregor220cac52009-02-18 17:45:20 +00002691
2692 if (Ty.isInvalid())
2693 DS.SetTypeSpecError();
2694 else {
2695 const char *PrevSpec = 0;
2696 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2697 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2698 Ty.get()))
2699 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2700 }
Steve Naroffad373bd2007-07-31 12:34:36 +00002701 } else { // we have an expression.
Sebastian Redl59b5e512008-12-11 21:36:32 +00002702 OwningExprResult Result(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002703
2704 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00002705 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor220cac52009-02-18 17:45:20 +00002706 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002707 return;
2708 }
2709 RParenLoc = ConsumeParen();
2710 const char *PrevSpec = 0;
2711 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2712 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002713 Result.release()))
Chris Lattner6d29c102008-11-18 07:48:38 +00002714 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002715 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00002716 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00002717}
2718
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00002719