blob: cdf84bfad688164899850874d28569b004acbf65 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Chris Lattner34c61332009-04-25 08:06:05 +000040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000041 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// 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
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [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.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +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();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-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 Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000236 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000237 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000238 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000248 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000249 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000251 }
Chris Lattnera17991f2009-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 Lattnerf7b2e552007-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 Lattnerf8016042009-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 Lattner9802a0a2009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000267 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000275 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 }
279
280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattner2c41d482009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000285
Chris Lattner9802a0a2009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattnerf8016042009-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 Lattner2c41d482009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattner2c41d482009-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 Lattner4b009652007-07-25 00:24:17 +0000303}
304
Douglas Gregore3298aa2009-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 Lattner4b009652007-07-25 00:24:17 +0000309///
Chris Lattner4b009652007-07-25 00:24:17 +0000310/// init-declarator: [C99 6.7]
311/// declarator
312/// declarator '=' initializer
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
314/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000315/// [C++] declarator initializer[opt]
316///
317/// [C++] initializer:
318/// [C++] '=' initializer-clause
319/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-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 Lattner4b009652007-07-25 00:24:17 +0000325///
Douglas Gregore3298aa2009-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 Lattnera17991f2009-03-29 16:50:03 +0000401Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000402ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-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 Lattner4b009652007-07-25 00:24:17 +0000406
407 // 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 Gregore3298aa2009-05-12 21:31:51 +0000410 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
411 if (ThisDecl.get())
412 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000413
Chris Lattner4b009652007-07-25 00:24:17 +0000414 // If we don't have a comma, it is either the end of the list (a ';') or an
415 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000416 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000417 break;
418
419 // Consume the comma.
420 ConsumeToken();
421
422 // Parse the next declarator.
423 D.clear();
Chris Lattner926cf542008-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 Redl0c986032009-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 Lattner926cf542008-10-20 04:57:38 +0000437
Chris Lattner4b009652007-07-25 00:24:17 +0000438 ParseDeclarator(D);
439 }
440
Chris Lattner2c41d482009-03-29 17:18:04 +0000441 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
442 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000443}
444
445/// ParseSpecifierQualifierList
446/// specifier-qualifier-list:
447/// type-specifier specifier-qualifier-list[opt]
448/// type-qualifier specifier-qualifier-list[opt]
449/// [GNU] attributes specifier-qualifier-list[opt]
450///
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.
454 ParseDeclarationSpecifiers(DS);
455
456 // Validate declspec for type-name.
457 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000458 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
459 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000460 Diag(Tok, diag::err_typename_requires_specqual);
461
462 // Issue diagnostic and remove storage class if present.
463 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
464 if (DS.getStorageClassSpecLoc().isValid())
465 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
466 else
467 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
468 DS.ClearStorageClassSpecs();
469 }
470
471 // Issue diagnostic and remove function specfier if present.
472 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-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 Lattner4b009652007-07-25 00:24:17 +0000479 DS.ClearFunctionSpecs();
480 }
481}
482
Chris Lattnercc98d8c2009-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 Lattnera52aec42009-04-14 21:16:09 +0000494/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000495/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-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 Lattnera52aec42009-04-14 21:16:09 +0000504 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000505}
506
Chris Lattner82353c62009-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 Lattner52cd7622009-04-14 22:17:06 +0000517bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000518 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000519 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000520 assert(Tok.is(tok::identifier) && "should have identifier");
521
Chris Lattner82353c62009-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 Lattner52cd7622009-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 Lattner82353c62009-04-14 21:34:55 +0000555
Chris Lattner82353c62009-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 Lattner82353c62009-04-14 21:34:55 +0000563
Chris Lattner52cd7622009-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 Gregora9db0fa2009-05-12 23:25:50 +0000573 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000574 return true;
575 }
Chris Lattner82353c62009-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 Lattner52cd7622009-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 Lattner82353c62009-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 Lattner4b009652007-07-25 00:24:17 +0000596/// ParseDeclarationSpecifiers
597/// declaration-specifiers: [C99 6.7]
598/// storage-class-specifier declaration-specifiers[opt]
599/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000600/// [C99] function-specifier declaration-specifiers[opt]
601/// [GNU] attributes declaration-specifiers[opt]
602///
603/// storage-class-specifier: [C99 6.7.1]
604/// 'typedef'
605/// 'extern'
606/// 'static'
607/// 'auto'
608/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000609/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000610/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000611/// function-specifier: [C99 6.7.4]
612/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000613/// [C++] 'virtual'
614/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000615/// 'friend': [C++ dcl.friend]
616
Chris Lattner4b009652007-07-25 00:24:17 +0000617///
Douglas Gregor52473432008-12-24 02:52:09 +0000618void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000619 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000620 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000621 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000622 while (1) {
623 int isInvalid = false;
624 const char *PrevSpec = 0;
625 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000626
Chris Lattner4b009652007-07-25 00:24:17 +0000627 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000628 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000629 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000630 // If this is not a declaration specifier token, we're done reading decl
631 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000632 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000633 return;
Chris Lattner712f9a32009-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;
Argiris Kirtzidis311db8c2008-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 Gregor80b95c52009-03-25 15:40:00 +0000646 Token Next = NextToken();
647 if (Next.is(tok::annot_template_id) &&
648 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000649 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-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))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000660 goto DoneWithDeclSpec;
661
662 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000663 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-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 Lattner52cd7622009-04-14 22:17:06 +0000669 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000670 CurScope, &SS) &&
671 GetLookAheadToken(2).is(tok::l_paren))
672 goto DoneWithDeclSpec;
673
Douglas Gregor1075a162009-02-04 17:00:24 +0000674 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
675 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000676
Chris Lattner52cd7622009-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 Gregora9db0fa2009-05-12 23:25:50 +0000683 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000684 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000685 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000686
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000687 ConsumeToken(); // The C++ scope.
688
Douglas Gregora60c62e2009-02-09 15:09:02 +0000689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-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 Lattnerc297b722009-01-21 19:48:37 +0000699
700 case tok::annot_typename: {
Douglas Gregord7cb0372009-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 Lattnerc297b722009-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 Lattner5261d0c2009-03-28 19:18:32 +0000717 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-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 Lattnerfda18db2008-07-26 01:18:38 +0000725 // typedef-name
726 case tok::identifier: {
Chris Lattner712f9a32009-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 Lattner5bb837e2009-01-21 19:19:26 +0000729 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
730 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000731
Chris Lattnerfda18db2008-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 Gregor1075a162009-02-04 17:00:24 +0000739 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
740 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000741
Chris Lattnercc98d8c2009-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 Gregora9db0fa2009-05-12 23:25:50 +0000745 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000746 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000747 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000748
Douglas Gregorf15ac4b2008-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 Lattnercc98d8c2009-04-12 20:42:31 +0000753 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000754 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
755 NextToken().getKind() == tok::l_paren)
756 goto DoneWithDeclSpec;
757
Douglas Gregora60c62e2009-02-09 15:09:02 +0000758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-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 Lattner5261d0c2009-03-28 19:18:32 +0000774 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000775 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000776 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000777
778 DS.SetRangeEnd(EndProtoLoc);
779
Steve Narofff7683302008-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 Lattnerfda18db2008-07-26 01:18:38 +0000783 }
Douglas Gregor0c281a82009-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 Gregoraabb8502009-03-31 00:43:58 +0000789 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-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 Gregord7cb0372009-04-01 21:51:26 +0000797 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000798 continue;
799 }
800
Chris Lattner4b009652007-07-25 00:24:17 +0000801 // GNU attributes support.
802 case tok::kw___attribute:
803 DS.AddAttributes(ParseAttributes());
804 continue;
Steve Naroffc5ab14f2008-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 Lattner4b009652007-07-25 00:24:17 +0000812
Steve Naroffedd04d52008-12-25 14:16:32 +0000813 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000814 case tok::kw___forceinline:
815 case tok::kw___w64:
Steve Naroffedd04d52008-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 Lattner4b009652007-07-25 00:24:17 +0000824 // storage-class-specifier
825 case tok::kw_typedef:
826 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
827 break;
828 case tok::kw_extern:
829 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000830 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000831 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
832 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000833 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000834 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
835 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000836 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000837 case tok::kw_static:
838 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000839 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000840 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
841 break;
842 case tok::kw_auto:
843 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
844 break;
845 case tok::kw_register:
846 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
847 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000848 case tok::kw_mutable:
849 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
850 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000851 case tok::kw___thread:
852 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
853 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000854
Chris Lattner4b009652007-07-25 00:24:17 +0000855 // function-specifier
856 case tok::kw_inline:
857 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
858 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000859 case tok::kw_virtual:
860 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
861 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000862 case tok::kw_explicit:
863 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
864 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000865
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000866 // friend
867 case tok::kw_friend:
868 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
869 break;
870
Chris Lattnerc297b722009-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 Lattner197b4342009-04-12 21:49:30 +0000928 case tok::kw_union: {
929 tok::TokenKind Kind = Tok.getKind();
930 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000931 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000932 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000933 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000934
935 // enum-specifier:
936 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000937 ConsumeToken();
938 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-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 Gregord3022602009-03-27 23:10:48 +0000954 // C++ typename-specifier:
955 case tok::kw_typename:
956 if (TryAnnotateTypeOrScopeToken())
957 continue;
958 break;
959
Chris Lattnerc297b722009-01-21 19:48:37 +0000960 // GNU typeof support.
961 case tok::kw_typeof:
962 ParseTypeofSpecifier(DS);
963 continue;
964
Steve Naroff5f0466b2008-06-05 00:02:44 +0000965 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000966 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000967 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
968 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000969 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000970 goto DoneWithDeclSpec;
971
972 {
973 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000974 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000975 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000976 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000977 DS.SetRangeEnd(EndProtoLoc);
978
Chris Lattnerf006a222008-11-18 07:48:38 +0000979 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000980 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000981 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-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 Naroff5f0466b2008-06-05 00:02:44 +0000985 }
Chris Lattner4b009652007-07-25 00:24:17 +0000986 }
987 // If the specifier combination wasn't legal, issue a diagnostic.
988 if (isInvalid) {
989 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-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 Lattner4b009652007-07-25 00:24:17 +0000994 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000995 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000996 ConsumeToken();
997 }
998}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000999
Chris Lattnerd706dc82009-01-06 06:59:53 +00001000/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-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 Lattnerd706dc82009-01-06 06:59:53 +00001042bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1043 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001044 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001045 SourceLocation Loc = Tok.getLocation();
1046
1047 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001048 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001049 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-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 Gregora9db0fa2009-05-12 23:25:50 +00001053 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-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 Gregora9db0fa2009-05-12 23:25:50 +00001064 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001065 // Otherwise, not a type specifier.
1066 return false;
1067
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001068 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001069 case tok::annot_typename: {
Douglas Gregord7cb0372009-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();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001075 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1076 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-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 Lattner5261d0c2009-03-28 19:18:32 +00001086 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-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 Lattner197b4342009-04-12 21:49:30 +00001150 case tok::kw_union: {
1151 tok::TokenKind Kind = Tok.getKind();
1152 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001153 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001154 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001155 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001156
1157 // enum-specifier:
1158 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001159 ConsumeToken();
1160 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-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 Naroffedd04d52008-12-25 14:16:32 +00001182 case tok::kw___cdecl:
1183 case tok::kw___stdcall:
1184 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001185 if (!PP.getLangOptions().Microsoft) return false;
1186 ConsumeToken();
1187 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001188
Douglas Gregor3a6a3072008-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 Lattnerf006a222008-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 Gregor3a6a3072008-11-07 15:42:26 +00001201 }
1202 DS.SetRangeEnd(Tok.getLocation());
1203 ConsumeToken(); // whatever we parsed above.
1204 return true;
1205}
Chris Lattner4b009652007-07-25 00:24:17 +00001206
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001207/// ParseStructDeclaration - Parse a struct declaration without the terminating
1208/// semicolon.
1209///
Chris Lattner4b009652007-07-25 00:24:17 +00001210/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001211/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001212/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001213/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner3dd8d392008-04-10 06:46:29 +00001224void Parser::
1225ParseStructDeclaration(DeclSpec &DS,
1226 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-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 Naroffa9adf112007-08-20 22:28:22 +00001230 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001231 return ParseStructDeclaration(DS, Fields);
1232 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001233
1234 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001235 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001236 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001237
Douglas Gregorb748fc52009-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 Lattner34a01ad2007-10-09 17:33:22 +00001240 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001241 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001242 return;
1243 }
1244
1245 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001246 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001247 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001248 FieldDeclarator &DeclaratorInfo = Fields.back();
1249
Steve Naroffa9adf112007-08-20 22:28:22 +00001250 /// struct-declarator: declarator
1251 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001252 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001253 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001254
Chris Lattner34a01ad2007-10-09 17:33:22 +00001255 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001256 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001257 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001258 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001259 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001260 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001261 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001262 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001263
Steve Naroffa9adf112007-08-20 22:28:22 +00001264 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-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 Naroffa9adf112007-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 Lattner34a01ad2007-10-09 17:33:22 +00001273 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001274 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001275
Steve Naroffa9adf112007-08-20 22:28:22 +00001276 // Consume the comma.
1277 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001278
Steve Naroffa9adf112007-08-20 22:28:22 +00001279 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001280 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001281
Steve Naroffa9adf112007-08-20 22:28:22 +00001282 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-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 Naroffa9adf112007-08-20 22:28:22 +00001288 }
Steve Naroffa9adf112007-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 Lattner1bf58f62008-06-21 19:39:06 +00001299/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001300///
Chris Lattner4b009652007-07-25 00:24:17 +00001301void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001302 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001303 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1304 PP.getSourceManager(),
1305 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001306
Chris Lattner4b009652007-07-25 00:24:17 +00001307 SourceLocation LBraceLoc = ConsumeBrace();
1308
Douglas Gregorcab994d2009-01-09 22:42:13 +00001309 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001310 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1311
Chris Lattner4b009652007-07-25 00:24:17 +00001312 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1313 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001314 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001315 Diag(Tok, diag::ext_empty_struct_union_enum)
1316 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001317
Chris Lattner5261d0c2009-03-28 19:18:32 +00001318 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001319 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1320
Chris Lattner4b009652007-07-25 00:24:17 +00001321 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001322 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001323 // Each iteration of this loop reads one struct-declaration.
1324
1325 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001326 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001327 Diag(Tok, diag::ext_extra_struct_semi)
1328 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001329 ConsumeToken();
1330 continue;
1331 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001332
1333 // Parse all the comma separated declarators.
1334 DeclSpec DS;
1335 FieldDeclarators.clear();
Chris Lattner1bf58f62008-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 Lattner5261d0c2009-03-28 19:18:32 +00001343 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1344 DS.getSourceRange().getBegin(),
1345 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-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 Lattner5261d0c2009-03-28 19:18:32 +00001362 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001363 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1364 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-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 Lattner4b009652007-07-25 00:24:17 +00001369
Chris Lattner34a01ad2007-10-09 17:33:22 +00001370 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001371 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001372 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001373 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001374 break;
1375 } 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 Naroff1a7fa7b2007-10-29 21:38:07 +00001382 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001383
Chris Lattner4b009652007-07-25 00:24:17 +00001384 AttributeList *AttrList = 0;
1385 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001386 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001387 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001388
1389 Actions.ActOnFields(CurScope,
1390 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1391 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001392 AttrList);
1393 StructScope.Exit();
1394 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001395}
1396
1397
1398/// ParseEnumSpecifier
1399/// enum-specifier: [C99 6.7.2.2]
1400/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001401///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001402/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1403/// '}' attributes[opt]
1404/// 'enum' identifier
1405/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001406///
1407/// [C++] elaborated-type-specifier:
1408/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1409///
Chris Lattner197b4342009-04-12 21:49:30 +00001410void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1411 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001412 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-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();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001418
1419 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001420 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-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 }
Argiris Kirtzidis2298f012008-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 Lattner4b009652007-07-25 00:24:17 +00001438 return;
Argiris Kirtzidis2298f012008-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 Lattner5261d0c2009-03-28 19:18:32 +00001464 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1465 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001466
Chris Lattner34a01ad2007-10-09 17:33:22 +00001467 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001468 ParseEnumBody(StartLoc, TagDecl);
1469
1470 // TODO: semantic analysis on the declspec for enums.
1471 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001472 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1473 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001474 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001475}
1476
1477/// 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 Lattner5261d0c2009-03-28 19:18:32 +00001487void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001488 // Enter the scope of the enum body and start the definition.
1489 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001490 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001491
Chris Lattner4b009652007-07-25 00:24:17 +00001492 SourceLocation LBraceLoc = ConsumeBrace();
1493
Chris Lattnerc9a92452007-08-27 17:24:30 +00001494 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001495 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001496 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001497
Chris Lattner5261d0c2009-03-28 19:18:32 +00001498 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001499
Chris Lattner5261d0c2009-03-28 19:18:32 +00001500 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001501
1502 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001503 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001504 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1505 SourceLocation IdentLoc = ConsumeToken();
1506
1507 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001508 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001509 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001510 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001511 AssignedVal = ParseConstantExpression();
1512 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001513 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001514 }
1515
1516 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001517 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1518 LastEnumConstDecl,
1519 IdentLoc, Ident,
1520 EqualLoc,
1521 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001522 EnumConstantDecls.push_back(EnumConstDecl);
1523 LastEnumConstDecl = EnumConstDecl;
1524
Chris Lattner34a01ad2007-10-09 17:33:22 +00001525 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001526 break;
1527 SourceLocation CommaLoc = ConsumeToken();
1528
Douglas Gregor1ba5cb32009-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 Lattner4b009652007-07-25 00:24:17 +00001534 }
1535
1536 // Eat the }.
1537 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1538
Steve Naroff0acc9c92007-09-15 18:49:24 +00001539 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001540 EnumConstantDecls.size());
1541
Chris Lattner5261d0c2009-03-28 19:18:32 +00001542 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001543 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001544 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001545 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001546
1547 EnumScope.Exit();
1548 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001549}
1550
1551/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-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 Lattner4b009652007-07-25 00:24:17 +00001565/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001566bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001567 switch (Tok.getKind()) {
1568 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001569
1570 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001571 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-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 Gregord3022602009-03-27 23:10:48 +00001578
Chris Lattnerb75fde62009-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 Lattner4b009652007-07-25 00:24:17 +00001591 // GNU attributes support.
1592 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001593 // GNU typeof support.
1594 case tok::kw_typeof:
1595
Chris Lattner4b009652007-07-25 00:24:17 +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:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001605 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001606 case tok::kw_int:
1607 case tok::kw_float:
1608 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001609 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001610 case tok::kw__Bool:
1611 case tok::kw__Decimal32:
1612 case tok::kw__Decimal64:
1613 case tok::kw__Decimal128:
1614
Chris Lattner2e78db32008-04-13 18:59:07 +00001615 // struct-or-union-specifier (C99) or class-specifier (C++)
1616 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +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:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001626
1627 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001628 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001629 return true;
Chris Lattner9aefe722008-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 Naroffedd04d52008-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 Lattner4b009652007-07-25 00:24:17 +00001639 }
1640}
1641
1642/// isDeclarationSpecifier() - Return true if the current token is part of a
1643/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001644bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001645 switch (Tok.getKind()) {
1646 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001647
1648 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-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 Gregord3022602009-03-27 23:10:48 +00001652 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001653
Douglas Gregord3022602009-03-27 23:10:48 +00001654 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-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 Lattner4b009652007-07-25 00:24:17 +00001673 // storage-class-specifier
1674 case tok::kw_typedef:
1675 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001676 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +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:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001691 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001692 case tok::kw_int:
1693 case tok::kw_float:
1694 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001695 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001696 case tok::kw__Bool:
1697 case tok::kw__Decimal32:
1698 case tok::kw__Decimal64:
1699 case tok::kw__Decimal128:
1700
Chris Lattner2e78db32008-04-13 18:59:07 +00001701 // struct-or-union-specifier (C99) or class-specifier (C++)
1702 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001703 case tok::kw_struct:
1704 case tok::kw_union:
1705 // enum-specifier
1706 case tok::kw_enum:
1707
1708 // type-qualifier
1709 case tok::kw_const:
1710 case tok::kw_volatile:
1711 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001712
Chris Lattner4b009652007-07-25 00:24:17 +00001713 // function-specifier
1714 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001715 case tok::kw_virtual:
1716 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001717
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001718 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001719 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001720
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001721 // GNU typeof support.
1722 case tok::kw_typeof:
1723
1724 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001725 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001726 return true;
Chris Lattner1b2251c2008-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 Naroffedd04d52008-12-25 14:16:32 +00001731
Steve Naroffab1a3632009-01-06 19:34:12 +00001732 case tok::kw___declspec:
Steve Naroffedd04d52008-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 Lattner4b009652007-07-25 00:24:17 +00001737 }
1738}
1739
1740
1741/// ParseTypeQualifierListOpt
1742/// type-qualifier-list: [C99 6.7.5]
1743/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001744/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001745/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001746/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001747///
Chris Lattner460696f2008-12-18 07:02:59 +00001748void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001749 while (1) {
1750 int isInvalid = false;
1751 const char *PrevSpec = 0;
1752 SourceLocation Loc = Tok.getLocation();
1753
1754 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001755 case tok::kw_const:
1756 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1757 getLang())*2;
1758 break;
1759 case tok::kw_volatile:
1760 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1761 getLang())*2;
1762 break;
1763 case tok::kw_restrict:
1764 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1765 getLang())*2;
1766 break;
Steve Naroffad620402008-12-25 14:41:26 +00001767 case tok::kw___ptr64:
Steve Naroffedd04d52008-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 Lattner4b009652007-07-25 00:24:17 +00001775 case tok::kw___attribute:
Chris Lattner460696f2008-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 Naroffedd04d52008-12-25 14:16:32 +00001782 DoneWithTypeQuals:
Chris Lattner460696f2008-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 Gregor1ba5cb32009-04-01 22:41:11 +00001785 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001786 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001787 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001788
Chris Lattner4b009652007-07-25 00:24:17 +00001789 // If the specifier combination wasn't legal, issue a diagnostic.
1790 if (isInvalid) {
1791 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-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 Lattner4b009652007-07-25 00:24:17 +00001796 }
1797 ConsumeToken();
1798 }
1799}
1800
1801
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 Redl19fec9d2008-11-21 19:14:01 +00001807 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001808}
1809
Sebastian Redl19fec9d2008-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 Gregor3ef6c972008-11-07 20:08:42 +00001813/// ptr-operator production.
1814///
Sebastian Redl75555032009-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 Lattner4b009652007-07-25 00:24:17 +00001819///
1820/// pointer: [C99 6.7.5]
1821/// '*' type-qualifier-list[opt]
1822/// '*' type-qualifier-list[opt] pointer
1823///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001824/// ptr-operator:
1825/// '*' cv-qualifier-seq[opt]
1826/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001827/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001828/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001829/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001830/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001831void Parser::ParseDeclaratorInternal(Declarator &D,
1832 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001833
Sebastian Redl75555032009-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 Lattner053dd2d2009-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 Redl75555032009-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 Redl0c986032009-02-09 18:23:29 +00001851 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001852 DeclSpec DS;
1853 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001854 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-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 Redl0c986032009-02-09 18:23:29 +00001862 Loc, DS.TakeAttributes()),
1863 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001864 return;
1865 }
1866 }
1867
1868 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001869 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001870 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001871 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001872 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001873 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001874 if (DirectDeclParser)
1875 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001876 return;
1877 }
Sebastian Redl75555032009-01-24 21:16:55 +00001878
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001879 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1880 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001881 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001882 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001883
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001884 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001885 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001886 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001887
Chris Lattner4b009652007-07-25 00:24:17 +00001888 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001889 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001890
Chris Lattner4b009652007-07-25 00:24:17 +00001891 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001892 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-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 Redl0c986032009-02-09 18:23:29 +00001896 DS.TakeAttributes()),
1897 SourceLocation());
Steve Naroff7aa54752008-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 Stump7ff82e72009-04-21 00:51:43 +00001901 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001902 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001903 } else {
1904 // Is a reference
1905 DeclSpec DS;
1906
Sebastian Redl4e67adb2009-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl0c986032009-02-09 18:23:29 +00001919 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001920
1921 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1922 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1923 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001924 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001925 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1926 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001927 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001928 }
1929
1930 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001931 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001932
Douglas Gregorb7b28a22008-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 Lattner8f7db152008-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 Gregorb7b28a22008-11-03 15:51:28 +00001943
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001944 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-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
Chris Lattner4b009652007-07-25 00:24:17 +00001950 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001951 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001952 DS.TakeAttributes(),
1953 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001954 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001955 }
1956}
1957
1958/// ParseDirectDeclarator
1959/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001960/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001961/// '(' declarator ')'
1962/// [GNU] '(' attributes declarator ')'
1963/// [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] '*' ']'
1968/// direct-declarator '(' parameter-type-list ')'
1969/// direct-declarator '(' identifier-list[opt] ')'
1970/// [GNU] direct-declarator '(' parameter-forward-declarations
1971/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001972/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1973/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001974/// [C++] declarator-id
Douglas Gregor8210a8e2008-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
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001986/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001987/// conversion-function-id [TODO]
1988/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001989/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001990///
Chris Lattner4b009652007-07-25 00:24:17 +00001991void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001992 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001993
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001994 if (getLang().CPlusPlus) {
1995 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001996 // ParseDeclaratorInternal might already have parsed the scope.
1997 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1998 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-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 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002004
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002005 if (Tok.is(tok::identifier)) {
2006 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-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());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002018 ConsumeToken();
2019 goto PastIdentifier;
Douglas Gregor0c281a82009-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 Gregor853dd392008-12-26 15:00:45 +00002035 } else if (Tok.is(tok::kw_operator)) {
2036 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002037 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002038
Douglas Gregor853dd392008-12-26 15:00:45 +00002039 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002040 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2041 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002042 } else {
2043 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002044 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2045 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2046 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002047 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002048 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002049 }
2050 goto PastIdentifier;
2051 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002052 // This should be a C++ destructor.
2053 SourceLocation TildeLoc = ConsumeToken();
2054 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002055 // FIXME: Inaccurate.
2056 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002057 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002058 TypeResult Type = ParseClassName(EndLoc);
2059 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002060 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002061 else
2062 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-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 Gregor3ef6c972008-11-07 20:08:42 +00002077 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002078 }
Argiris Kirtzidisebdc8ea2008-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.
Argiris Kirtzidisebdc8ea2008-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 Lattner4b009652007-07-25 00:24:17 +00002090 // direct-declarator: '(' declarator ')'
2091 // direct-declarator: '(' attributes declarator ')'
2092 // Example: 'char (*X)' or 'int (*XX)(void)'
2093 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002094 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorf03265d2009-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)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002103 Diag(Tok, diag::err_expected_unqualified_id);
2104 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002105 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002106 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002107 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002108 }
2109
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002110 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002111 assert(D.isPastIdentifier() &&
2112 "Haven't past the location of the identifier yet?");
2113
2114 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002115 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-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 Lattner1f185292008-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 Lattnera0d056d2008-04-06 05:45:57 +00002126 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002127 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002128 ParseBracketDeclarator(D);
2129 } else {
2130 break;
2131 }
2132 }
2133}
2134
Chris Lattnera0d056d2008-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 Lattner1f185292008-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 Lattnera0d056d2008-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 Lattner1f185292008-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 Naroffedd04d52008-12-25 14:16:32 +00002171 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-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 Naroffedd04d52008-12-25 14:16:32 +00002174 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002175
Chris Lattnera0d056d2008-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.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002187 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-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) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002201 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002202 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002203 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002204 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002205
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002206 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002207 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002208 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002209
2210 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002211 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-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 Lattner1f185292008-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 Lattnera0d056d2008-04-06 05:45:57 +00002219 D.SetIdentifier(0, Tok.getLocation());
2220
Chris Lattner1f185292008-10-20 02:05:46 +00002221 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-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 Lattner4b009652007-07-25 00:24:17 +00002227///
Chris Lattner1f185292008-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 Lattner4b009652007-07-25 00:24:17 +00002234/// This method also handles this portion of the grammar:
2235/// 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 Lattner3e254fb2008-04-08 04:40:51 +00002245/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002246/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002247/// declaration-specifiers abstract-declarator[opt]
2248/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002249/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002250/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2251///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002252/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002253/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002254///
Chris Lattner1f185292008-10-20 02:05:46 +00002255void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2256 AttributeList *AttrList,
2257 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002258 // lparen is already consumed!
2259 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002260
Chris Lattner1f185292008-10-20 02:05:46 +00002261 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002262 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002263 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002264 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002265 delete AttrList;
2266 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002267
Sebastian Redl0c986032009-02-09 18:23:29 +00002268 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002269
2270 // cv-qualifier-seq[opt].
2271 DeclSpec DS;
Sebastian Redl35f3a5b2009-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;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002277 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002278 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002279 if (!DS.getSourceRange().getEnd().isInvalid())
2280 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002281
2282 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002283 if (Tok.is(tok::kw_throw)) {
2284 hasExceptionSpec = true;
2285 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2286 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002287 }
2288
Chris Lattner9f7564b2008-04-06 06:57:35 +00002289 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002290 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002291 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002292 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002293 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002294 /*arglist*/ 0, 0,
2295 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002296 hasExceptionSpec,
2297 hasAnyExceptionSpec,
2298 Exceptions.empty() ? 0 :
2299 &Exceptions[0],
2300 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002301 LParenLoc, D),
2302 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002303 return;
Chris Lattner1f185292008-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 Naroff3f3f3b42009-01-28 19:16:40 +00002308 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002309 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002310 // K&R identifier lists can't have typedefs as identifiers, per
2311 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002312 if (RequiresArg) {
2313 Diag(Tok, diag::err_argument_required_after_attribute);
2314 delete AttrList;
2315 }
Steve Naroff3f3f3b42009-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 Lattner1f185292008-10-20 02:05:46 +00002319 }
Chris Lattner9f7564b2008-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 Lattner3e254fb2008-04-08 04:40:51 +00002326
2327 // Enter function-declaration scope, limiting any declarators to the
2328 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002329 ParseScope PrototypeScope(this,
2330 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002331
2332 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002333 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002334 while (1) {
2335 if (Tok.is(tok::ellipsis)) {
2336 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002337 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002338 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002339 }
2340
Chris Lattner9f7564b2008-04-06 06:57:35 +00002341 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002342
Chris Lattner9f7564b2008-04-06 06:57:35 +00002343 // Parse the declaration-specifiers.
2344 DeclSpec DS;
Chris Lattner1f185292008-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 Lattner9e785f52009-02-27 18:38:20 +00002351 ParseDeclarationSpecifiers(DS);
2352
Chris Lattner9f7564b2008-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 Redl0c986032009-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 Lattner9f7564b2008-04-06 06:57:35 +00002364
Chris Lattner9f7564b2008-04-06 06:57:35 +00002365 // Remember this parsed parameter in ParamInfo.
2366 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2367
Douglas Gregor605de8d2008-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 Lattner9f7564b2008-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 Lattner9e785f52009-02-27 18:38:20 +00002374 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2375 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-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 Lattner5261d0c2009-03-28 19:18:32 +00002384 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-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 Gregor62ae25a2008-12-24 00:01:03 +00002391 SourceLocation EqualLoc = Tok.getLocation();
2392
Chris Lattner3e254fb2008-04-08 04:40:51 +00002393 // Parse the default argument
Douglas Gregor605de8d2008-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 Gregor62ae25a2008-12-24 00:01:03 +00002406 Actions.ActOnParamDefaultArgumentError(Param);
2407 } else
2408 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002409 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002410 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002411 ConsumeToken();
Douglas Gregor605de8d2008-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 Redl76bb8ec2009-03-15 17:47:39 +00002420 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002421 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002422 }
2423 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002424
2425 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002426 ParmDecl.getIdentifierLoc(), Param,
2427 DefArgToks));
Chris Lattner9f7564b2008-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 Lattner4b009652007-07-25 00:24:17 +00002435 }
2436
Chris Lattner9f7564b2008-04-06 06:57:35 +00002437 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002438 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002439
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002440 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002441 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002442
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002443 DeclSpec DS;
Sebastian Redl35f3a5b2009-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;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002449 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002450 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002451 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002452 if (!DS.getSourceRange().getEnd().isInvalid())
2453 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002454
2455 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002456 if (Tok.is(tok::kw_throw)) {
2457 hasExceptionSpec = true;
2458 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2459 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002460 }
2461
Chris Lattner4b009652007-07-25 00:24:17 +00002462 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002463 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002464 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002465 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002466 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002467 hasExceptionSpec,
2468 hasAnyExceptionSpec,
2469 Exceptions.empty() ? 0 :
2470 &Exceptions[0],
2471 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002472 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002473}
2474
Chris Lattner35d9c912008-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 Lattnerc337fa22008-04-06 06:50:56 +00002498 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002499 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002500 Tok.getLocation(),
2501 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002502
Chris Lattner113a56b2008-04-06 06:39:19 +00002503 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002504
2505 while (Tok.is(tok::comma)) {
2506 // Eat the comma.
2507 ConsumeToken();
2508
Chris Lattner113a56b2008-04-06 06:39:19 +00002509 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002510 if (Tok.isNot(tok::identifier)) {
2511 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002512 SkipUntil(tok::r_paren);
2513 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002514 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002515
Chris Lattner35d9c912008-04-06 06:34:08 +00002516 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002517
2518 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002519 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002520 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002521
2522 // Verify that the argument identifier has not already been mentioned.
2523 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002524 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002525 } else {
2526 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002527 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002528 Tok.getLocation(),
2529 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002530 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002531
2532 // Eat the identifier.
2533 ConsumeToken();
2534 }
Sebastian Redl0c986032009-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 Lattner113a56b2008-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 Gregor88a25f82009-02-18 07:07:28 +00002543 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002544 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002545 /*TypeQuals*/0,
2546 /*exception*/false, false, 0, 0,
2547 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002548 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002549}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002550
Chris Lattner4b009652007-07-25 00:24:17 +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) {
2557 SourceLocation StartLoc = ConsumeBracket();
2558
Chris Lattner1525c3a2008-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 Redl0c986032009-02-09 18:23:29 +00002562 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002563 // Remember that we parsed the empty array type.
2564 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002565 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2566 EndLoc);
Chris Lattner1525c3a2008-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 Redlcd883f72009-01-18 18:53:16 +00002571 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002572 ConsumeToken();
2573
Sebastian Redl0c986032009-02-09 18:23:29 +00002574 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-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 Redl0c986032009-02-09 18:23:29 +00002582 ExprRes.release(), StartLoc),
2583 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002584 return;
2585 }
2586
Chris Lattner4b009652007-07-25 00:24:17 +00002587 // If valid, this location is the position where we read the 'static' keyword.
2588 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002589 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002590 StaticLoc = ConsumeToken();
2591
2592 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002593 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002594 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002595 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002596
2597 // If we haven't already read 'static', check to see if there is one after the
2598 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002599 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002600 StaticLoc = ConsumeToken();
2601
2602 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2603 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002604 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-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 Lattner1bb39512008-04-06 05:27:21 +00002611 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002612
Chris Lattner306d4df2008-12-18 06:50:14 +00002613 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002614 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002615 StaticLoc = SourceLocation(); // Drop the static.
2616 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002617 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002618 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-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 Lattner4b009652007-07-25 00:24:17 +00002624 // Parse the assignment-expression now.
2625 NumElements = ParseAssignmentExpression();
2626 }
2627
2628 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002629 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002630 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002631 // If the expression was invalid, skip it.
2632 SkipUntil(tok::r_square);
2633 return;
2634 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002635
2636 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2637
Chris Lattner1525c3a2008-12-18 07:27:21 +00002638 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002639 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2640 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002641 NumElements.release(), StartLoc),
2642 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002643}
2644
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002645/// [GNU] typeof-specifier:
2646/// typeof ( expressions )
2647/// typeof ( type-name )
2648/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002649///
2650void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002651 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002652 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002653 SourceLocation StartLoc = ConsumeToken();
2654
Chris Lattner34a01ad2007-10-09 17:33:22 +00002655 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002656 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002657 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002658 return;
2659 }
2660
Sebastian Redl14ca7412008-12-11 21:36:32 +00002661 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002662 if (Result.isInvalid()) {
2663 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002664 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002665 }
Argiris Kirtzidisc2a384d2008-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 Redl6f1ee232008-12-10 00:02:53 +00002670 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002671 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-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 Naroff14bbce82007-08-02 02:53:48 +00002675 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002676 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002677
Steve Naroff7cbb1462007-07-31 12:34:36 +00002678 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2679
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002680 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002681 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002682
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002683 assert((Ty.isInvalid() || Ty.get()) &&
2684 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002685
Chris Lattner34a01ad2007-10-09 17:33:22 +00002686 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002687 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002688 return;
2689 }
2690 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-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 Naroff7cbb1462007-07-31 12:34:36 +00002701 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002702 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002703
2704 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002705 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002706 DS.SetTypeSpecError();
Steve Naroff14bbce82007-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 Redl6f1ee232008-12-10 00:02:53 +00002713 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002714 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002715 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002716 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002717}
2718
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002719