blob: 9d7d9d400f7a03bd519cede503a492e76bd76c7d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnereaaebc72009-04-25 08:06:05 +000040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000041 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlab197ba2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner04d66662007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101
Chris Lattner04d66662007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
Chris Lattner04d66662007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner04d66662007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redla55e52c2008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Chris Lattner04d66662007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +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))
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
199 return CurrAttr;
200}
201
Steve Narofff59e17e2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redl50de12f2009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner97144fc2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000236 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000237 if (NextToken().isNot(tok::less)) {
238 SingleDecl = ParseExplicitInstantiation(DeclEnd);
239 break;
240 }
241 // Fall through for template declarations and specializations
242
243 case tok::kw_export:
Chris Lattner97144fc2009-04-02 04:16:50 +0000244 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000245 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000246 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000247 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000248 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000249 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000250 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000251 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000252 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000253 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000254 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000255 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000256 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000257 }
Chris Lattner682bf922009-03-29 16:50:03 +0000258
259 // This routine returns a DeclGroup, if the thing we parsed only contains a
260 // single decl, convert it now.
261 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000262}
263
264/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
265/// declaration-specifiers init-declarator-list[opt] ';'
266///[C90/C++]init-declarator-list ';' [TODO]
267/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000268///
269/// If RequireSemi is false, this does not check for a ';' at the end of the
270/// declaration.
271Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000272 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000273 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 // Parse the common declaration-specifiers piece.
275 DeclSpec DS;
276 ParseDeclarationSpecifiers(DS);
277
278 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
279 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000280 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000282 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
283 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 }
285
286 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
287 ParseDeclarator(DeclaratorInfo);
288
Chris Lattner23c4b182009-03-29 17:18:04 +0000289 DeclGroupPtrTy DG =
290 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000291
Chris Lattner97144fc2009-04-02 04:16:50 +0000292 DeclEnd = Tok.getLocation();
293
Chris Lattnercd147752009-03-29 17:27:48 +0000294 // If the client wants to check what comes after the declaration, just return
295 // immediately without checking anything!
296 if (!RequireSemi) return DG;
Chris Lattner23c4b182009-03-29 17:18:04 +0000297
298 if (Tok.is(tok::semi)) {
299 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000300 return DG;
301 }
302
Chris Lattner23c4b182009-03-29 17:18:04 +0000303 Diag(Tok, diag::err_expected_semi_declation);
304 // Skip to end of block or statement
305 SkipUntil(tok::r_brace, true, true);
306 if (Tok.is(tok::semi))
307 ConsumeToken();
308 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309}
310
Douglas Gregor1426e532009-05-12 21:31:51 +0000311/// \brief Parse 'declaration' after parsing 'declaration-specifiers
312/// declarator'. This method parses the remainder of the declaration
313/// (including any attributes or initializer, among other things) and
314/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000315///
Reid Spencer5f016e22007-07-11 17:01:13 +0000316/// init-declarator: [C99 6.7]
317/// declarator
318/// declarator '=' initializer
319/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
320/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000321/// [C++] declarator initializer[opt]
322///
323/// [C++] initializer:
324/// [C++] '=' initializer-clause
325/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000326/// [C++0x] '=' 'default' [TODO]
327/// [C++0x] '=' 'delete'
328///
329/// According to the standard grammar, =default and =delete are function
330/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000331///
Douglas Gregor1426e532009-05-12 21:31:51 +0000332Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
333 // If a simple-asm-expr is present, parse it.
334 if (Tok.is(tok::kw_asm)) {
335 SourceLocation Loc;
336 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
337 if (AsmLabel.isInvalid()) {
338 SkipUntil(tok::semi, true, true);
339 return DeclPtrTy();
340 }
341
342 D.setAsmLabel(AsmLabel.release());
343 D.SetRangeEnd(Loc);
344 }
345
346 // If attributes are present, parse them.
347 if (Tok.is(tok::kw___attribute)) {
348 SourceLocation Loc;
349 AttributeList *AttrList = ParseAttributes(&Loc);
350 D.AddAttributes(AttrList, Loc);
351 }
352
353 // Inform the current actions module that we just parsed this declarator.
354 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
355
356 // Parse declarator '=' initializer.
357 if (Tok.is(tok::equal)) {
358 ConsumeToken();
359 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
360 SourceLocation DelLoc = ConsumeToken();
361 Actions.SetDeclDeleted(ThisDecl, DelLoc);
362 } else {
363 OwningExprResult Init(ParseInitializer());
364 if (Init.isInvalid()) {
365 SkipUntil(tok::semi, true, true);
366 return DeclPtrTy();
367 }
368 Actions.AddInitializerToDecl(ThisDecl, move(Init));
369 }
370 } else if (Tok.is(tok::l_paren)) {
371 // Parse C++ direct initializer: '(' expression-list ')'
372 SourceLocation LParenLoc = ConsumeParen();
373 ExprVector Exprs(Actions);
374 CommaLocsTy CommaLocs;
375
376 if (ParseExpressionList(Exprs, CommaLocs)) {
377 SkipUntil(tok::r_paren);
378 } else {
379 // Match the ')'.
380 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
381
382 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
383 "Unexpected number of commas!");
384 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
385 move_arg(Exprs),
386 &CommaLocs[0], RParenLoc);
387 }
388 } else {
389 Actions.ActOnUninitializedDecl(ThisDecl);
390 }
391
392 return ThisDecl;
393}
394
395/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
396/// parsing 'declaration-specifiers declarator'. This method is split out this
397/// way to handle the ambiguity between top-level function-definitions and
398/// declarations.
399///
400/// init-declarator-list: [C99 6.7]
401/// init-declarator
402/// init-declarator-list ',' init-declarator
403///
404/// According to the standard grammar, =default and =delete are function
405/// definitions, but that definitely doesn't fit with the parser here.
406///
Chris Lattner682bf922009-03-29 16:50:03 +0000407Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000408ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000409 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
410 // that we parse together here.
411 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
413 // At this point, we know that it is not a function definition. Parse the
414 // rest of the init-declarator-list.
415 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000416 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
417 if (ThisDecl.get())
418 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // If we don't have a comma, it is either the end of the list (a ';') or an
421 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000422 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 break;
424
425 // Consume the comma.
426 ConsumeToken();
427
428 // Parse the next declarator.
429 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000430
431 // Accept attributes in an init-declarator. In the first declarator in a
432 // declaration, these would be part of the declspec. In subsequent
433 // declarators, they become part of the declarator itself, so that they
434 // don't apply to declarators after *this* one. Examples:
435 // short __attribute__((common)) var; -> declspec
436 // short var __attribute__((common)); -> declarator
437 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000438 if (Tok.is(tok::kw___attribute)) {
439 SourceLocation Loc;
440 AttributeList *AttrList = ParseAttributes(&Loc);
441 D.AddAttributes(AttrList, Loc);
442 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000443
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 ParseDeclarator(D);
445 }
446
Chris Lattner23c4b182009-03-29 17:18:04 +0000447 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
448 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000449}
450
451/// ParseSpecifierQualifierList
452/// specifier-qualifier-list:
453/// type-specifier specifier-qualifier-list[opt]
454/// type-qualifier specifier-qualifier-list[opt]
455/// [GNU] attributes specifier-qualifier-list[opt]
456///
457void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
458 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
459 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 ParseDeclarationSpecifiers(DS);
461
462 // Validate declspec for type-name.
463 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000464 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
465 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 Diag(Tok, diag::err_typename_requires_specqual);
467
468 // Issue diagnostic and remove storage class if present.
469 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
470 if (DS.getStorageClassSpecLoc().isValid())
471 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
472 else
473 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
474 DS.ClearStorageClassSpecs();
475 }
476
477 // Issue diagnostic and remove function specfier if present.
478 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000479 if (DS.isInlineSpecified())
480 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
481 if (DS.isVirtualSpecified())
482 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
483 if (DS.isExplicitSpecified())
484 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 DS.ClearFunctionSpecs();
486 }
487}
488
Chris Lattnerc199ab32009-04-12 20:42:31 +0000489/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
490/// specified token is valid after the identifier in a declarator which
491/// immediately follows the declspec. For example, these things are valid:
492///
493/// int x [ 4]; // direct-declarator
494/// int x ( int y); // direct-declarator
495/// int(int x ) // direct-declarator
496/// int x ; // simple-declaration
497/// int x = 17; // init-declarator-list
498/// int x , y; // init-declarator-list
499/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000500/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000501/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000502///
503/// This is not, because 'x' does not immediately follow the declspec (though
504/// ')' happens to be valid anyway).
505/// int (x)
506///
507static bool isValidAfterIdentifierInDeclarator(const Token &T) {
508 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
509 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000510 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000511}
512
Chris Lattnere40c2952009-04-14 21:34:55 +0000513
514/// ParseImplicitInt - This method is called when we have an non-typename
515/// identifier in a declspec (which normally terminates the decl spec) when
516/// the declspec has no type specifier. In this case, the declspec is either
517/// malformed or is "implicit int" (in K&R and C89).
518///
519/// This method handles diagnosing this prettily and returns false if the
520/// declspec is done being processed. If it recovers and thinks there may be
521/// other pieces of declspec after it, it returns true.
522///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000523bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Chris Lattnere40c2952009-04-14 21:34:55 +0000524 TemplateParameterLists *TemplateParams,
525 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000526 assert(Tok.is(tok::identifier) && "should have identifier");
527
Chris Lattnere40c2952009-04-14 21:34:55 +0000528 SourceLocation Loc = Tok.getLocation();
529 // If we see an identifier that is not a type name, we normally would
530 // parse it as the identifer being declared. However, when a typename
531 // is typo'd or the definition is not included, this will incorrectly
532 // parse the typename as the identifier name and fall over misparsing
533 // later parts of the diagnostic.
534 //
535 // As such, we try to do some look-ahead in cases where this would
536 // otherwise be an "implicit-int" case to see if this is invalid. For
537 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
538 // an identifier with implicit int, we'd get a parse error because the
539 // next token is obviously invalid for a type. Parse these as a case
540 // with an invalid type specifier.
541 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
542
543 // Since we know that this either implicit int (which is rare) or an
544 // error, we'd do lookahead to try to do better recovery.
545 if (isValidAfterIdentifierInDeclarator(NextToken())) {
546 // If this token is valid for implicit int, e.g. "static x = 4", then
547 // we just avoid eating the identifier, so it will be parsed as the
548 // identifier in the declarator.
549 return false;
550 }
551
552 // Otherwise, if we don't consume this token, we are going to emit an
553 // error anyway. Try to recover from various common problems. Check
554 // to see if this was a reference to a tag name without a tag specified.
555 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000556 //
557 // C++ doesn't need this, and isTagName doesn't take SS.
558 if (SS == 0) {
559 const char *TagName = 0;
560 tok::TokenKind TagKind = tok::unknown;
Chris Lattnere40c2952009-04-14 21:34:55 +0000561
Chris Lattnere40c2952009-04-14 21:34:55 +0000562 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
563 default: break;
564 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
565 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
566 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
567 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
568 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000569
Chris Lattnerf4382f52009-04-14 22:17:06 +0000570 if (TagName) {
571 Diag(Loc, diag::err_use_of_tag_name_without_tag)
572 << Tok.getIdentifierInfo() << TagName
573 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
574
575 // Parse this as a tag as if the missing tag were present.
576 if (TagKind == tok::kw_enum)
577 ParseEnumSpecifier(Loc, DS, AS);
578 else
579 ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
580 return true;
581 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000582 }
583
584 // Since this is almost certainly an invalid type name, emit a
585 // diagnostic that says it, eat the token, and mark the declspec as
586 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000587 SourceRange R;
588 if (SS) R = SS->getRange();
589
590 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000591 const char *PrevSpec;
592 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
593 DS.SetRangeEnd(Tok.getLocation());
594 ConsumeToken();
595
596 // TODO: Could inject an invalid typedef decl in an enclosing scope to
597 // avoid rippling error messages on subsequent uses of the same type,
598 // could be useful if #include was forgotten.
599 return false;
600}
601
Reid Spencer5f016e22007-07-11 17:01:13 +0000602/// ParseDeclarationSpecifiers
603/// declaration-specifiers: [C99 6.7]
604/// storage-class-specifier declaration-specifiers[opt]
605/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000606/// [C99] function-specifier declaration-specifiers[opt]
607/// [GNU] attributes declaration-specifiers[opt]
608///
609/// storage-class-specifier: [C99 6.7.1]
610/// 'typedef'
611/// 'extern'
612/// 'static'
613/// 'auto'
614/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000615/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000616/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000617/// function-specifier: [C99 6.7.4]
618/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000619/// [C++] 'virtual'
620/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000621/// 'friend': [C++ dcl.friend]
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000624void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000625 TemplateParameterLists *TemplateParams,
Chris Lattnerc199ab32009-04-12 20:42:31 +0000626 AccessSpecifier AS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000627 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 while (1) {
629 int isInvalid = false;
630 const char *PrevSpec = 0;
631 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000632
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000634 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000635 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // If this is not a declaration specifier token, we're done reading decl
637 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000638 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000640
641 case tok::coloncolon: // ::foo::bar
642 // Annotate C++ scope specifiers. If we get one, loop.
643 if (TryAnnotateCXXScopeToken())
644 continue;
645 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000646
647 case tok::annot_cxxscope: {
648 if (DS.hasTypeSpecifier())
649 goto DoneWithDeclSpec;
650
651 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000652 Token Next = NextToken();
653 if (Next.is(tok::annot_template_id) &&
654 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000655 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000656 // We have a qualified template-id, e.g., N::A<int>
657 CXXScopeSpec SS;
658 ParseOptionalCXXScopeSpecifier(SS);
659 assert(Tok.is(tok::annot_template_id) &&
660 "ParseOptionalCXXScopeSpecifier not working");
661 AnnotateTemplateIdTokenAsType(&SS);
662 continue;
663 }
664
665 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000666 goto DoneWithDeclSpec;
667
668 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000669 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000670 SS.setRange(Tok.getAnnotationRange());
671
672 // If the next token is the name of the class type that the C++ scope
673 // denotes, followed by a '(', then this is a constructor declaration.
674 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000675 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000676 CurScope, &SS) &&
677 GetLookAheadToken(2).is(tok::l_paren))
678 goto DoneWithDeclSpec;
679
Douglas Gregorb696ea32009-02-04 17:00:24 +0000680 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
681 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000682
Chris Lattnerf4382f52009-04-14 22:17:06 +0000683 // If the referenced identifier is not a type, then this declspec is
684 // erroneous: We already checked about that it has no type specifier, and
685 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
686 // typename.
687 if (TypeRep == 0) {
688 ConsumeToken(); // Eat the scope spec so the identifier is current.
689 if (ParseImplicitInt(DS, &SS, TemplateParams, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000690 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000691 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000692
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000693 ConsumeToken(); // The C++ scope.
694
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000695 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000696 TypeRep);
697 if (isInvalid)
698 break;
699
700 DS.SetRangeEnd(Tok.getLocation());
701 ConsumeToken(); // The typename.
702
703 continue;
704 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000705
706 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000707 if (Tok.getAnnotationValue())
708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
709 Tok.getAnnotationValue());
710 else
711 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000712 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
713 ConsumeToken(); // The typename
714
715 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
716 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
717 // Objective-C interface. If we don't have Objective-C or a '<', this is
718 // just a normal reference to a typedef name.
719 if (!Tok.is(tok::less) || !getLang().ObjC1)
720 continue;
721
722 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000723 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000724 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
725 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
726
727 DS.SetRangeEnd(EndProtoLoc);
728 continue;
729 }
730
Chris Lattner3bd934a2008-07-26 01:18:38 +0000731 // typedef-name
732 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000733 // In C++, check to see if this is a scope specifier like foo::bar::, if
734 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000735 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
736 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000737
Chris Lattner3bd934a2008-07-26 01:18:38 +0000738 // This identifier can only be a typedef name if we haven't already seen
739 // a type-specifier. Without this check we misparse:
740 // typedef int X; struct Y { short X; }; as 'short int'.
741 if (DS.hasTypeSpecifier())
742 goto DoneWithDeclSpec;
743
744 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000745 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
746 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000747
Chris Lattnerc199ab32009-04-12 20:42:31 +0000748 // If this is not a typedef name, don't parse it as part of the declspec,
749 // it must be an implicit int or an error.
750 if (TypeRep == 0) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000751 if (ParseImplicitInt(DS, 0, TemplateParams, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000752 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000753 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000754
Douglas Gregorb48fe382008-10-31 09:07:45 +0000755 // C++: If the identifier is actually the name of the class type
756 // being defined and the next token is a '(', then this is a
757 // constructor declaration. We're done with the decl-specifiers
758 // and will treat this token as an identifier.
Chris Lattnerc199ab32009-04-12 20:42:31 +0000759 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000760 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
761 NextToken().getKind() == tok::l_paren)
762 goto DoneWithDeclSpec;
763
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000765 TypeRep);
766 if (isInvalid)
767 break;
768
769 DS.SetRangeEnd(Tok.getLocation());
770 ConsumeToken(); // The identifier
771
772 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
773 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
774 // Objective-C interface. If we don't have Objective-C or a '<', this is
775 // just a normal reference to a typedef name.
776 if (!Tok.is(tok::less) || !getLang().ObjC1)
777 continue;
778
779 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000780 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000781 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000782 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000783
784 DS.SetRangeEnd(EndProtoLoc);
785
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000786 // Need to support trailing type qualifiers (e.g. "id<p> const").
787 // If a type specifier follows, it will be diagnosed elsewhere.
788 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000789 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000790
791 // type-name
792 case tok::annot_template_id: {
793 TemplateIdAnnotation *TemplateId
794 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000795 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000796 // This template-id does not refer to a type name, so we're
797 // done with the type-specifiers.
798 goto DoneWithDeclSpec;
799 }
800
801 // Turn the template-id annotation token into a type annotation
802 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000803 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000804 continue;
805 }
806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // GNU attributes support.
808 case tok::kw___attribute:
809 DS.AddAttributes(ParseAttributes());
810 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000811
812 // Microsoft declspec support.
813 case tok::kw___declspec:
814 if (!PP.getLangOptions().Microsoft)
815 goto DoneWithDeclSpec;
816 FuzzyParseMicrosoftDeclSpec();
817 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000818
Steve Naroff239f0732008-12-25 14:16:32 +0000819 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000820 case tok::kw___forceinline:
821 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000822 case tok::kw___cdecl:
823 case tok::kw___stdcall:
824 case tok::kw___fastcall:
825 if (!PP.getLangOptions().Microsoft)
826 goto DoneWithDeclSpec;
827 // Just ignore it.
828 break;
829
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 // storage-class-specifier
831 case tok::kw_typedef:
832 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
833 break;
834 case tok::kw_extern:
835 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000836 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
838 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000839 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000840 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
841 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000842 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 case tok::kw_static:
844 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000845 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
847 break;
848 case tok::kw_auto:
849 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
850 break;
851 case tok::kw_register:
852 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
853 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000854 case tok::kw_mutable:
855 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
856 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 case tok::kw___thread:
858 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
859 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 // function-specifier
862 case tok::kw_inline:
863 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
864 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000865 case tok::kw_virtual:
866 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
867 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000868 case tok::kw_explicit:
869 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
870 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000871
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000872 // friend
873 case tok::kw_friend:
874 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
875 break;
876
Chris Lattner80d0c892009-01-21 19:48:37 +0000877 // type-specifier
878 case tok::kw_short:
879 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
880 break;
881 case tok::kw_long:
882 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
883 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
884 else
885 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
886 break;
887 case tok::kw_signed:
888 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
889 break;
890 case tok::kw_unsigned:
891 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
892 break;
893 case tok::kw__Complex:
894 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
895 break;
896 case tok::kw__Imaginary:
897 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
898 break;
899 case tok::kw_void:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
901 break;
902 case tok::kw_char:
903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
904 break;
905 case tok::kw_int:
906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
907 break;
908 case tok::kw_float:
909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
910 break;
911 case tok::kw_double:
912 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
913 break;
914 case tok::kw_wchar_t:
915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
916 break;
917 case tok::kw_bool:
918 case tok::kw__Bool:
919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
920 break;
921 case tok::kw__Decimal32:
922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
923 break;
924 case tok::kw__Decimal64:
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
926 break;
927 case tok::kw__Decimal128:
928 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
929 break;
930
931 // class-specifier:
932 case tok::kw_class:
933 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +0000934 case tok::kw_union: {
935 tok::TokenKind Kind = Tok.getKind();
936 ConsumeToken();
937 ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000938 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +0000939 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000940
941 // enum-specifier:
942 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +0000943 ConsumeToken();
944 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000945 continue;
946
947 // cv-qualifier:
948 case tok::kw_const:
949 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
950 break;
951 case tok::kw_volatile:
952 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
953 getLang())*2;
954 break;
955 case tok::kw_restrict:
956 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
957 getLang())*2;
958 break;
959
Douglas Gregord57959a2009-03-27 23:10:48 +0000960 // C++ typename-specifier:
961 case tok::kw_typename:
962 if (TryAnnotateTypeOrScopeToken())
963 continue;
964 break;
965
Chris Lattner80d0c892009-01-21 19:48:37 +0000966 // GNU typeof support.
967 case tok::kw_typeof:
968 ParseTypeofSpecifier(DS);
969 continue;
970
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000971 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000972 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000973 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
974 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000975 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000976 goto DoneWithDeclSpec;
977
978 {
979 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000980 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000981 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000982 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000983 DS.SetRangeEnd(EndProtoLoc);
984
Chris Lattner1ab3b962008-11-18 07:48:38 +0000985 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +0000986 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +0000987 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000988 // Need to support trailing type qualifiers (e.g. "id<p> const").
989 // If a type specifier follows, it will be diagnosed elsewhere.
990 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000991 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 }
993 // If the specifier combination wasn't legal, issue a diagnostic.
994 if (isInvalid) {
995 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000996 // Pick between error or extwarn.
997 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
998 : diag::ext_duplicate_declspec;
999 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001001 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 ConsumeToken();
1003 }
1004}
Douglas Gregoradcac882008-12-01 23:54:00 +00001005
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001006/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001007/// primarily follow the C++ grammar with additions for C99 and GNU,
1008/// which together subsume the C grammar. Note that the C++
1009/// type-specifier also includes the C type-qualifier (for const,
1010/// volatile, and C99 restrict). Returns true if a type-specifier was
1011/// found (and parsed), false otherwise.
1012///
1013/// type-specifier: [C++ 7.1.5]
1014/// simple-type-specifier
1015/// class-specifier
1016/// enum-specifier
1017/// elaborated-type-specifier [TODO]
1018/// cv-qualifier
1019///
1020/// cv-qualifier: [C++ 7.1.5.1]
1021/// 'const'
1022/// 'volatile'
1023/// [C99] 'restrict'
1024///
1025/// simple-type-specifier: [ C++ 7.1.5.2]
1026/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1027/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1028/// 'char'
1029/// 'wchar_t'
1030/// 'bool'
1031/// 'short'
1032/// 'int'
1033/// 'long'
1034/// 'signed'
1035/// 'unsigned'
1036/// 'float'
1037/// 'double'
1038/// 'void'
1039/// [C99] '_Bool'
1040/// [C99] '_Complex'
1041/// [C99] '_Imaginary' // Removed in TC2?
1042/// [GNU] '_Decimal32'
1043/// [GNU] '_Decimal64'
1044/// [GNU] '_Decimal128'
1045/// [GNU] typeof-specifier
1046/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1047/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001048bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1049 const char *&PrevSpec,
1050 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +00001051 SourceLocation Loc = Tok.getLocation();
1052
1053 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001054 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001055 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001056 // Annotate typenames and C++ scope specifiers. If we get one, just
1057 // recurse to handle whatever we get.
1058 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001059 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001060 // Otherwise, not a type specifier.
1061 return false;
1062 case tok::coloncolon: // ::foo::bar
1063 if (NextToken().is(tok::kw_new) || // ::new
1064 NextToken().is(tok::kw_delete)) // ::delete
1065 return false;
1066
1067 // Annotate typenames and C++ scope specifiers. If we get one, just
1068 // recurse to handle whatever we get.
1069 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001070 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001071 // Otherwise, not a type specifier.
1072 return false;
1073
Douglas Gregor12e083c2008-11-07 15:42:26 +00001074 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001075 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001076 if (Tok.getAnnotationValue())
1077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1078 Tok.getAnnotationValue());
1079 else
1080 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001081 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1082 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001083
1084 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1085 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1086 // Objective-C interface. If we don't have Objective-C or a '<', this is
1087 // just a normal reference to a typedef name.
1088 if (!Tok.is(tok::less) || !getLang().ObjC1)
1089 return true;
1090
1091 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001092 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001093 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1094 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1095
1096 DS.SetRangeEnd(EndProtoLoc);
1097 return true;
1098 }
1099
1100 case tok::kw_short:
1101 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1102 break;
1103 case tok::kw_long:
1104 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1105 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1106 else
1107 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1108 break;
1109 case tok::kw_signed:
1110 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1111 break;
1112 case tok::kw_unsigned:
1113 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1114 break;
1115 case tok::kw__Complex:
1116 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1117 break;
1118 case tok::kw__Imaginary:
1119 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1120 break;
1121 case tok::kw_void:
1122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1123 break;
1124 case tok::kw_char:
1125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1126 break;
1127 case tok::kw_int:
1128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1129 break;
1130 case tok::kw_float:
1131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1132 break;
1133 case tok::kw_double:
1134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1135 break;
1136 case tok::kw_wchar_t:
1137 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1138 break;
1139 case tok::kw_bool:
1140 case tok::kw__Bool:
1141 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1142 break;
1143 case tok::kw__Decimal32:
1144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1145 break;
1146 case tok::kw__Decimal64:
1147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1148 break;
1149 case tok::kw__Decimal128:
1150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1151 break;
1152
1153 // class-specifier:
1154 case tok::kw_class:
1155 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001156 case tok::kw_union: {
1157 tok::TokenKind Kind = Tok.getKind();
1158 ConsumeToken();
1159 ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001160 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001161 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001162
1163 // enum-specifier:
1164 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001165 ConsumeToken();
1166 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001167 return true;
1168
1169 // cv-qualifier:
1170 case tok::kw_const:
1171 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1172 getLang())*2;
1173 break;
1174 case tok::kw_volatile:
1175 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1176 getLang())*2;
1177 break;
1178 case tok::kw_restrict:
1179 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1180 getLang())*2;
1181 break;
1182
1183 // GNU typeof support.
1184 case tok::kw_typeof:
1185 ParseTypeofSpecifier(DS);
1186 return true;
1187
Steve Naroff239f0732008-12-25 14:16:32 +00001188 case tok::kw___cdecl:
1189 case tok::kw___stdcall:
1190 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +00001191 if (!PP.getLangOptions().Microsoft) return false;
1192 ConsumeToken();
1193 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001194
Douglas Gregor12e083c2008-11-07 15:42:26 +00001195 default:
1196 // Not a type-specifier; do nothing.
1197 return false;
1198 }
1199
1200 // If the specifier combination wasn't legal, issue a diagnostic.
1201 if (isInvalid) {
1202 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001203 // Pick between error or extwarn.
1204 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1205 : diag::ext_duplicate_declspec;
1206 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001207 }
1208 DS.SetRangeEnd(Tok.getLocation());
1209 ConsumeToken(); // whatever we parsed above.
1210 return true;
1211}
Reid Spencer5f016e22007-07-11 17:01:13 +00001212
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001213/// ParseStructDeclaration - Parse a struct declaration without the terminating
1214/// semicolon.
1215///
Reid Spencer5f016e22007-07-11 17:01:13 +00001216/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001217/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001218/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001219/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001220/// struct-declarator-list:
1221/// struct-declarator
1222/// struct-declarator-list ',' struct-declarator
1223/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1224/// struct-declarator:
1225/// declarator
1226/// [GNU] declarator attributes[opt]
1227/// declarator[opt] ':' constant-expression
1228/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1229///
Chris Lattnere1359422008-04-10 06:46:29 +00001230void Parser::
1231ParseStructDeclaration(DeclSpec &DS,
1232 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001233 if (Tok.is(tok::kw___extension__)) {
1234 // __extension__ silences extension warnings in the subexpression.
1235 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001236 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001237 return ParseStructDeclaration(DS, Fields);
1238 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001239
1240 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001241 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001242 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001243
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001244 // If there are no declarators, this is a free-standing declaration
1245 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001246 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001247 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001248 return;
1249 }
1250
1251 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001252 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001253 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001254 FieldDeclarator &DeclaratorInfo = Fields.back();
1255
Steve Naroff28a7ca82007-08-20 22:28:22 +00001256 /// struct-declarator: declarator
1257 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001258 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001259 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001260
Chris Lattner04d66662007-10-09 17:33:22 +00001261 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001262 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001263 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001264 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001265 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001266 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001267 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001268 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001269
Steve Naroff28a7ca82007-08-20 22:28:22 +00001270 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001271 if (Tok.is(tok::kw___attribute)) {
1272 SourceLocation Loc;
1273 AttributeList *AttrList = ParseAttributes(&Loc);
1274 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1275 }
1276
Steve Naroff28a7ca82007-08-20 22:28:22 +00001277 // If we don't have a comma, it is either the end of the list (a ';')
1278 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001279 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001280 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001281
Steve Naroff28a7ca82007-08-20 22:28:22 +00001282 // Consume the comma.
1283 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001284
Steve Naroff28a7ca82007-08-20 22:28:22 +00001285 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001286 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001287
Steve Naroff28a7ca82007-08-20 22:28:22 +00001288 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001289 if (Tok.is(tok::kw___attribute)) {
1290 SourceLocation Loc;
1291 AttributeList *AttrList = ParseAttributes(&Loc);
1292 Fields.back().D.AddAttributes(AttrList, Loc);
1293 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001294 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001295}
1296
1297/// ParseStructUnionBody
1298/// struct-contents:
1299/// struct-declaration-list
1300/// [EXT] empty
1301/// [GNU] "struct-declaration-list" without terminatoring ';'
1302/// struct-declaration-list:
1303/// struct-declaration
1304/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001305/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001306///
Reid Spencer5f016e22007-07-11 17:01:13 +00001307void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001308 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001309 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1310 PP.getSourceManager(),
1311 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001312
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 SourceLocation LBraceLoc = ConsumeBrace();
1314
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001315 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001316 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1317
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1319 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001320 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001321 Diag(Tok, diag::ext_empty_struct_union_enum)
1322 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001323
Chris Lattnerb28317a2009-03-28 19:18:32 +00001324 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001325 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1326
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001328 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // Each iteration of this loop reads one struct-declaration.
1330
1331 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001332 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001333 Diag(Tok, diag::ext_extra_struct_semi)
1334 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 ConsumeToken();
1336 continue;
1337 }
Chris Lattnere1359422008-04-10 06:46:29 +00001338
1339 // Parse all the comma separated declarators.
1340 DeclSpec DS;
1341 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001342 if (!Tok.is(tok::at)) {
1343 ParseStructDeclaration(DS, FieldDeclarators);
1344
1345 // Convert them all to fields.
1346 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1347 FieldDeclarator &FD = FieldDeclarators[i];
1348 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001349 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1350 DS.getSourceRange().getBegin(),
1351 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001352 FieldDecls.push_back(Field);
1353 }
1354 } else { // Handle @defs
1355 ConsumeToken();
1356 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1357 Diag(Tok, diag::err_unexpected_at);
1358 SkipUntil(tok::semi, true, true);
1359 continue;
1360 }
1361 ConsumeToken();
1362 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1363 if (!Tok.is(tok::identifier)) {
1364 Diag(Tok, diag::err_expected_ident);
1365 SkipUntil(tok::semi, true, true);
1366 continue;
1367 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001368 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001369 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1370 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001371 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1372 ConsumeToken();
1373 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1374 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001375
Chris Lattner04d66662007-10-09 17:33:22 +00001376 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001378 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001379 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 break;
1381 } else {
1382 Diag(Tok, diag::err_expected_semi_decl_list);
1383 // Skip to end of block or statement
1384 SkipUntil(tok::r_brace, true, true);
1385 }
1386 }
1387
Steve Naroff60fccee2007-10-29 21:38:07 +00001388 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001389
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 AttributeList *AttrList = 0;
1391 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001392 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001393 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001394
1395 Actions.ActOnFields(CurScope,
1396 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1397 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001398 AttrList);
1399 StructScope.Exit();
1400 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001401}
1402
1403
1404/// ParseEnumSpecifier
1405/// enum-specifier: [C99 6.7.2.2]
1406/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001407///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001408/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1409/// '}' attributes[opt]
1410/// 'enum' identifier
1411/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001412///
1413/// [C++] elaborated-type-specifier:
1414/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1415///
Chris Lattner4c97d762009-04-12 21:49:30 +00001416void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1417 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001419
1420 AttributeList *Attr = 0;
1421 // If attributes exist after tag, parse them.
1422 if (Tok.is(tok::kw___attribute))
1423 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001424
1425 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001426 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001427 if (Tok.isNot(tok::identifier)) {
1428 Diag(Tok, diag::err_expected_ident);
1429 if (Tok.isNot(tok::l_brace)) {
1430 // Has no name and is not a definition.
1431 // Skip the rest of this declarator, up until the comma or semicolon.
1432 SkipUntil(tok::comma, true);
1433 return;
1434 }
1435 }
1436 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001437
1438 // Must have either 'enum name' or 'enum {...}'.
1439 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1440 Diag(Tok, diag::err_expected_ident_lbrace);
1441
1442 // Skip the rest of this declarator, up until the comma or semicolon.
1443 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001445 }
1446
1447 // If an identifier is present, consume and remember it.
1448 IdentifierInfo *Name = 0;
1449 SourceLocation NameLoc;
1450 if (Tok.is(tok::identifier)) {
1451 Name = Tok.getIdentifierInfo();
1452 NameLoc = ConsumeToken();
1453 }
1454
1455 // There are three options here. If we have 'enum foo;', then this is a
1456 // forward declaration. If we have 'enum foo {...' then this is a
1457 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1458 //
1459 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1460 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1461 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1462 //
1463 Action::TagKind TK;
1464 if (Tok.is(tok::l_brace))
1465 TK = Action::TK_Definition;
1466 else if (Tok.is(tok::semi))
1467 TK = Action::TK_Declaration;
1468 else
1469 TK = Action::TK_Reference;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001470 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1471 StartLoc, SS, Name, NameLoc, Attr, AS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001472
Chris Lattner04d66662007-10-09 17:33:22 +00001473 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 ParseEnumBody(StartLoc, TagDecl);
1475
1476 // TODO: semantic analysis on the declspec for enums.
1477 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001478 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1479 TagDecl.getAs<void>()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001480 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001481}
1482
1483/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1484/// enumerator-list:
1485/// enumerator
1486/// enumerator-list ',' enumerator
1487/// enumerator:
1488/// enumeration-constant
1489/// enumeration-constant '=' constant-expression
1490/// enumeration-constant:
1491/// identifier
1492///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001493void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001494 // Enter the scope of the enum body and start the definition.
1495 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001496 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001497
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 SourceLocation LBraceLoc = ConsumeBrace();
1499
Chris Lattner7946dd32007-08-27 17:24:30 +00001500 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001501 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001502 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001503
Chris Lattnerb28317a2009-03-28 19:18:32 +00001504 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001505
Chris Lattnerb28317a2009-03-28 19:18:32 +00001506 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001507
1508 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001509 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1511 SourceLocation IdentLoc = ConsumeToken();
1512
1513 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001514 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001515 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001517 AssignedVal = ParseConstantExpression();
1518 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 }
1521
1522 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001523 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1524 LastEnumConstDecl,
1525 IdentLoc, Ident,
1526 EqualLoc,
1527 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 EnumConstantDecls.push_back(EnumConstDecl);
1529 LastEnumConstDecl = EnumConstDecl;
1530
Chris Lattner04d66662007-10-09 17:33:22 +00001531 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 break;
1533 SourceLocation CommaLoc = ConsumeToken();
1534
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001535 if (Tok.isNot(tok::identifier) &&
1536 !(getLang().C99 || getLang().CPlusPlus0x))
1537 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1538 << getLang().CPlusPlus
1539 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 }
1541
1542 // Eat the }.
1543 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1544
Steve Naroff08d92e42007-09-15 18:49:24 +00001545 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 EnumConstantDecls.size());
1547
Chris Lattnerb28317a2009-03-28 19:18:32 +00001548 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001549 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001550 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001552
1553 EnumScope.Exit();
1554 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001555}
1556
1557/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001558/// start of a type-qualifier-list.
1559bool Parser::isTypeQualifier() const {
1560 switch (Tok.getKind()) {
1561 default: return false;
1562 // type-qualifier
1563 case tok::kw_const:
1564 case tok::kw_volatile:
1565 case tok::kw_restrict:
1566 return true;
1567 }
1568}
1569
1570/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001571/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001572bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 switch (Tok.getKind()) {
1574 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001575
1576 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001577 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001578 // Annotate typenames and C++ scope specifiers. If we get one, just
1579 // recurse to handle whatever we get.
1580 if (TryAnnotateTypeOrScopeToken())
1581 return isTypeSpecifierQualifier();
1582 // Otherwise, not a type specifier.
1583 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001584
Chris Lattner166a8fc2009-01-04 23:41:41 +00001585 case tok::coloncolon: // ::foo::bar
1586 if (NextToken().is(tok::kw_new) || // ::new
1587 NextToken().is(tok::kw_delete)) // ::delete
1588 return false;
1589
1590 // Annotate typenames and C++ scope specifiers. If we get one, just
1591 // recurse to handle whatever we get.
1592 if (TryAnnotateTypeOrScopeToken())
1593 return isTypeSpecifierQualifier();
1594 // Otherwise, not a type specifier.
1595 return false;
1596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 // GNU attributes support.
1598 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001599 // GNU typeof support.
1600 case tok::kw_typeof:
1601
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 // type-specifiers
1603 case tok::kw_short:
1604 case tok::kw_long:
1605 case tok::kw_signed:
1606 case tok::kw_unsigned:
1607 case tok::kw__Complex:
1608 case tok::kw__Imaginary:
1609 case tok::kw_void:
1610 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001611 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 case tok::kw_int:
1613 case tok::kw_float:
1614 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001615 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 case tok::kw__Bool:
1617 case tok::kw__Decimal32:
1618 case tok::kw__Decimal64:
1619 case tok::kw__Decimal128:
1620
Chris Lattner99dc9142008-04-13 18:59:07 +00001621 // struct-or-union-specifier (C99) or class-specifier (C++)
1622 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 case tok::kw_struct:
1624 case tok::kw_union:
1625 // enum-specifier
1626 case tok::kw_enum:
1627
1628 // type-qualifier
1629 case tok::kw_const:
1630 case tok::kw_volatile:
1631 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001632
1633 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001634 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001636
1637 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1638 case tok::less:
1639 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001640
1641 case tok::kw___cdecl:
1642 case tok::kw___stdcall:
1643 case tok::kw___fastcall:
1644 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 }
1646}
1647
1648/// isDeclarationSpecifier() - Return true if the current token is part of a
1649/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001650bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 switch (Tok.getKind()) {
1652 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001653
1654 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001655 // Unfortunate hack to support "Class.factoryMethod" notation.
1656 if (getLang().ObjC1 && NextToken().is(tok::period))
1657 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001658 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001659
Douglas Gregord57959a2009-03-27 23:10:48 +00001660 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001661 // Annotate typenames and C++ scope specifiers. If we get one, just
1662 // recurse to handle whatever we get.
1663 if (TryAnnotateTypeOrScopeToken())
1664 return isDeclarationSpecifier();
1665 // Otherwise, not a declaration specifier.
1666 return false;
1667 case tok::coloncolon: // ::foo::bar
1668 if (NextToken().is(tok::kw_new) || // ::new
1669 NextToken().is(tok::kw_delete)) // ::delete
1670 return false;
1671
1672 // Annotate typenames and C++ scope specifiers. If we get one, just
1673 // recurse to handle whatever we get.
1674 if (TryAnnotateTypeOrScopeToken())
1675 return isDeclarationSpecifier();
1676 // Otherwise, not a declaration specifier.
1677 return false;
1678
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 // storage-class-specifier
1680 case tok::kw_typedef:
1681 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001682 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 case tok::kw_static:
1684 case tok::kw_auto:
1685 case tok::kw_register:
1686 case tok::kw___thread:
1687
1688 // type-specifiers
1689 case tok::kw_short:
1690 case tok::kw_long:
1691 case tok::kw_signed:
1692 case tok::kw_unsigned:
1693 case tok::kw__Complex:
1694 case tok::kw__Imaginary:
1695 case tok::kw_void:
1696 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001697 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 case tok::kw_int:
1699 case tok::kw_float:
1700 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001701 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 case tok::kw__Bool:
1703 case tok::kw__Decimal32:
1704 case tok::kw__Decimal64:
1705 case tok::kw__Decimal128:
1706
Chris Lattner99dc9142008-04-13 18:59:07 +00001707 // struct-or-union-specifier (C99) or class-specifier (C++)
1708 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 case tok::kw_struct:
1710 case tok::kw_union:
1711 // enum-specifier
1712 case tok::kw_enum:
1713
1714 // type-qualifier
1715 case tok::kw_const:
1716 case tok::kw_volatile:
1717 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001718
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 // function-specifier
1720 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001721 case tok::kw_virtual:
1722 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001723
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001724 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001725 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001726
Chris Lattner1ef08762007-08-09 17:01:07 +00001727 // GNU typeof support.
1728 case tok::kw_typeof:
1729
1730 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001731 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001733
1734 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1735 case tok::less:
1736 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001737
Steve Naroff47f52092009-01-06 19:34:12 +00001738 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001739 case tok::kw___cdecl:
1740 case tok::kw___stdcall:
1741 case tok::kw___fastcall:
1742 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 }
1744}
1745
1746
1747/// ParseTypeQualifierListOpt
1748/// type-qualifier-list: [C99 6.7.5]
1749/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001750/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001751/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001752/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001753///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001754void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 while (1) {
1756 int isInvalid = false;
1757 const char *PrevSpec = 0;
1758 SourceLocation Loc = Tok.getLocation();
1759
1760 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 case tok::kw_const:
1762 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1763 getLang())*2;
1764 break;
1765 case tok::kw_volatile:
1766 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1767 getLang())*2;
1768 break;
1769 case tok::kw_restrict:
1770 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1771 getLang())*2;
1772 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001773 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001774 case tok::kw___cdecl:
1775 case tok::kw___stdcall:
1776 case tok::kw___fastcall:
1777 if (!PP.getLangOptions().Microsoft)
1778 goto DoneWithTypeQuals;
1779 // Just ignore it.
1780 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001782 if (AttributesAllowed) {
1783 DS.AddAttributes(ParseAttributes());
1784 continue; // do *not* consume the next token!
1785 }
1786 // otherwise, FALL THROUGH!
1787 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001788 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001789 // If this is not a type-qualifier token, we're done reading type
1790 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001791 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001792 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001794
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 // If the specifier combination wasn't legal, issue a diagnostic.
1796 if (isInvalid) {
1797 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001798 // Pick between error or extwarn.
1799 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1800 : diag::ext_duplicate_declspec;
1801 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 }
1803 ConsumeToken();
1804 }
1805}
1806
1807
1808/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1809///
1810void Parser::ParseDeclarator(Declarator &D) {
1811 /// This implements the 'declarator' production in the C grammar, then checks
1812 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001813 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001814}
1815
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001816/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1817/// is parsed by the function passed to it. Pass null, and the direct-declarator
1818/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001819/// ptr-operator production.
1820///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001821/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1822/// [C] pointer[opt] direct-declarator
1823/// [C++] direct-declarator
1824/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001825///
1826/// pointer: [C99 6.7.5]
1827/// '*' type-qualifier-list[opt]
1828/// '*' type-qualifier-list[opt] pointer
1829///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001830/// ptr-operator:
1831/// '*' cv-qualifier-seq[opt]
1832/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001833/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001834/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001835/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001836/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001837void Parser::ParseDeclaratorInternal(Declarator &D,
1838 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001839
Sebastian Redlf30208a2009-01-24 21:16:55 +00001840 // C++ member pointers start with a '::' or a nested-name.
1841 // Member pointers get special handling, since there's no place for the
1842 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001843 if (getLang().CPlusPlus &&
1844 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1845 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001846 CXXScopeSpec SS;
1847 if (ParseOptionalCXXScopeSpecifier(SS)) {
1848 if(Tok.isNot(tok::star)) {
1849 // The scope spec really belongs to the direct-declarator.
1850 D.getCXXScopeSpec() = SS;
1851 if (DirectDeclParser)
1852 (this->*DirectDeclParser)(D);
1853 return;
1854 }
1855
1856 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001857 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001858 DeclSpec DS;
1859 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001860 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001861
1862 // Recurse to parse whatever is left.
1863 ParseDeclaratorInternal(D, DirectDeclParser);
1864
1865 // Sema will have to catch (syntactically invalid) pointers into global
1866 // scope. It has to catch pointers into namespace scope anyway.
1867 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001868 Loc, DS.TakeAttributes()),
1869 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001870 return;
1871 }
1872 }
1873
1874 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001875 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001876 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001877 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001878 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001879 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001880 if (DirectDeclParser)
1881 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001882 return;
1883 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001884
Sebastian Redl05532f22009-03-15 22:02:01 +00001885 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1886 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001887 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001888 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001889
Chris Lattner9af55002009-03-27 04:18:06 +00001890 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00001891 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001893
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001895 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001896
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001898 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001899 if (Kind == tok::star)
1900 // Remember that we parsed a pointer type, and remember the type-quals.
1901 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001902 DS.TakeAttributes()),
1903 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001904 else
1905 // Remember that we parsed a Block type, and remember the type-quals.
1906 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00001907 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001908 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 } else {
1910 // Is a reference
1911 DeclSpec DS;
1912
Sebastian Redl743de1f2009-03-23 00:00:23 +00001913 // Complain about rvalue references in C++03, but then go on and build
1914 // the declarator.
1915 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1916 Diag(Loc, diag::err_rvalue_reference);
1917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1919 // cv-qualifiers are introduced through the use of a typedef or of a
1920 // template type argument, in which case the cv-qualifiers are ignored.
1921 //
1922 // [GNU] Retricted references are allowed.
1923 // [GNU] Attributes on references are allowed.
1924 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001925 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001926
1927 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1928 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1929 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001930 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1932 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001933 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 }
1935
1936 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001937 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001938
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001939 if (D.getNumTypeObjects() > 0) {
1940 // C++ [dcl.ref]p4: There shall be no references to references.
1941 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1942 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001943 if (const IdentifierInfo *II = D.getIdentifier())
1944 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1945 << II;
1946 else
1947 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1948 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001949
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001950 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001951 // can go ahead and build the (technically ill-formed)
1952 // declarator: reference collapsing will take care of it.
1953 }
1954 }
1955
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001957 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001958 DS.TakeAttributes(),
1959 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001960 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 }
1962}
1963
1964/// ParseDirectDeclarator
1965/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001966/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001967/// '(' declarator ')'
1968/// [GNU] '(' attributes declarator ')'
1969/// [C90] direct-declarator '[' constant-expression[opt] ']'
1970/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1971/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1972/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1973/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1974/// direct-declarator '(' parameter-type-list ')'
1975/// direct-declarator '(' identifier-list[opt] ')'
1976/// [GNU] direct-declarator '(' parameter-forward-declarations
1977/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001978/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1979/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001980/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001981///
1982/// declarator-id: [C++ 8]
1983/// id-expression
1984/// '::'[opt] nested-name-specifier[opt] type-name
1985///
1986/// id-expression: [C++ 5.1]
1987/// unqualified-id
1988/// qualified-id [TODO]
1989///
1990/// unqualified-id: [C++ 5.1]
1991/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001992/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001993/// conversion-function-id [TODO]
1994/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001995/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001996///
Reid Spencer5f016e22007-07-11 17:01:13 +00001997void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001998 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001999
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002000 if (getLang().CPlusPlus) {
2001 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002002 // ParseDeclaratorInternal might already have parsed the scope.
2003 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2004 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002005 if (afterCXXScope) {
2006 // Change the declaration context for name lookup, until this function
2007 // is exited (and the declarator has been parsed).
2008 DeclScopeObj.EnterDeclaratorScope();
2009 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002010
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002011 if (Tok.is(tok::identifier)) {
2012 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002013
2014 // If this identifier is the name of the current class, it's a
2015 // constructor name.
2016 if (!D.getDeclSpec().hasTypeSpecifier() &&
2017 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2018 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2019 Tok.getLocation(), CurScope),
2020 Tok.getLocation());
2021 // This is a normal identifier.
2022 } else
2023 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002024 ConsumeToken();
2025 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002026 } else if (Tok.is(tok::annot_template_id)) {
2027 TemplateIdAnnotation *TemplateId
2028 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2029
2030 // FIXME: Could this template-id name a constructor?
2031
2032 // FIXME: This is an egregious hack, where we silently ignore
2033 // the specialization (which should be a function template
2034 // specialization name) and use the name instead. This hack
2035 // will go away when we have support for function
2036 // specializations.
2037 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2038 TemplateId->Destroy();
2039 ConsumeToken();
2040 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002041 } else if (Tok.is(tok::kw_operator)) {
2042 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002043 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002044
Douglas Gregor70316a02008-12-26 15:00:45 +00002045 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002046 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2047 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002048 } else {
2049 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002050 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2051 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2052 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002053 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002054 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002055 }
2056 goto PastIdentifier;
2057 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002058 // This should be a C++ destructor.
2059 SourceLocation TildeLoc = ConsumeToken();
2060 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002061 // FIXME: Inaccurate.
2062 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002063 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00002064 TypeResult Type = ParseClassName(EndLoc);
2065 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002066 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002067 else
2068 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002069 } else {
2070 Diag(Tok, diag::err_expected_class_name);
2071 D.SetIdentifier(0, TildeLoc);
2072 }
2073 goto PastIdentifier;
2074 }
2075
2076 // If we reached this point, token is not identifier and not '~'.
2077
2078 if (afterCXXScope) {
2079 Diag(Tok, diag::err_expected_unqualified_id);
2080 D.SetIdentifier(0, Tok.getLocation());
2081 D.setInvalidType(true);
2082 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002083 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002084 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002085 }
2086
2087 // If we reached this point, we are either in C/ObjC or the token didn't
2088 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002089 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2090 assert(!getLang().CPlusPlus &&
2091 "There's a C++-specific check for tok::identifier above");
2092 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2093 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2094 ConsumeToken();
2095 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 // direct-declarator: '(' declarator ')'
2097 // direct-declarator: '(' attributes declarator ')'
2098 // Example: 'char (*X)' or 'int (*XX)(void)'
2099 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002100 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 // This could be something simple like "int" (in which case the declarator
2102 // portion is empty), if an abstract-declarator is allowed.
2103 D.SetIdentifier(0, Tok.getLocation());
2104 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002105 if (D.getContext() == Declarator::MemberContext)
2106 Diag(Tok, diag::err_expected_member_name_or_semi)
2107 << D.getDeclSpec().getSourceRange();
2108 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002109 Diag(Tok, diag::err_expected_unqualified_id);
2110 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002111 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002113 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 }
2115
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002116 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 assert(D.isPastIdentifier() &&
2118 "Haven't past the location of the identifier yet?");
2119
2120 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002121 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002122 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2123 // In such a case, check if we actually have a function declarator; if it
2124 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002125 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2126 // When not in file scope, warn for ambiguous function declarators, just
2127 // in case the author intended it as a variable definition.
2128 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2129 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2130 break;
2131 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002132 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002133 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002134 ParseBracketDeclarator(D);
2135 } else {
2136 break;
2137 }
2138 }
2139}
2140
Chris Lattneref4715c2008-04-06 05:45:57 +00002141/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2142/// only called before the identifier, so these are most likely just grouping
2143/// parens for precedence. If we find that these are actually function
2144/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2145///
2146/// direct-declarator:
2147/// '(' declarator ')'
2148/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002149/// direct-declarator '(' parameter-type-list ')'
2150/// direct-declarator '(' identifier-list[opt] ')'
2151/// [GNU] direct-declarator '(' parameter-forward-declarations
2152/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002153///
2154void Parser::ParseParenDeclarator(Declarator &D) {
2155 SourceLocation StartLoc = ConsumeParen();
2156 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2157
Chris Lattner7399ee02008-10-20 02:05:46 +00002158 // Eat any attributes before we look at whether this is a grouping or function
2159 // declarator paren. If this is a grouping paren, the attribute applies to
2160 // the type being built up, for example:
2161 // int (__attribute__(()) *x)(long y)
2162 // If this ends up not being a grouping paren, the attribute applies to the
2163 // first argument, for example:
2164 // int (__attribute__(()) int x)
2165 // In either case, we need to eat any attributes to be able to determine what
2166 // sort of paren this is.
2167 //
2168 AttributeList *AttrList = 0;
2169 bool RequiresArg = false;
2170 if (Tok.is(tok::kw___attribute)) {
2171 AttrList = ParseAttributes();
2172
2173 // We require that the argument list (if this is a non-grouping paren) be
2174 // present even if the attribute list was empty.
2175 RequiresArg = true;
2176 }
Steve Naroff239f0732008-12-25 14:16:32 +00002177 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00002178 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2179 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00002180 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00002181
Chris Lattneref4715c2008-04-06 05:45:57 +00002182 // If we haven't past the identifier yet (or where the identifier would be
2183 // stored, if this is an abstract declarator), then this is probably just
2184 // grouping parens. However, if this could be an abstract-declarator, then
2185 // this could also be the start of function arguments (consider 'void()').
2186 bool isGrouping;
2187
2188 if (!D.mayOmitIdentifier()) {
2189 // If this can't be an abstract-declarator, this *must* be a grouping
2190 // paren, because we haven't seen the identifier yet.
2191 isGrouping = true;
2192 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002193 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002194 isDeclarationSpecifier()) { // 'int(int)' is a function.
2195 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2196 // considered to be a type, not a K&R identifier-list.
2197 isGrouping = false;
2198 } else {
2199 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2200 isGrouping = true;
2201 }
2202
2203 // If this is a grouping paren, handle:
2204 // direct-declarator: '(' declarator ')'
2205 // direct-declarator: '(' attributes declarator ')'
2206 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002207 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002208 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002209 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002210 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002211
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002212 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002213 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002214 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002215
2216 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002217 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002218 return;
2219 }
2220
2221 // Okay, if this wasn't a grouping paren, it must be the start of a function
2222 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002223 // identifier (and remember where it would have been), then call into
2224 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002225 D.SetIdentifier(0, Tok.getLocation());
2226
Chris Lattner7399ee02008-10-20 02:05:46 +00002227 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002228}
2229
2230/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2231/// declarator D up to a paren, which indicates that we are parsing function
2232/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002233///
Chris Lattner7399ee02008-10-20 02:05:46 +00002234/// If AttrList is non-null, then the caller parsed those arguments immediately
2235/// after the open paren - they should be considered to be the first argument of
2236/// a parameter. If RequiresArg is true, then the first argument of the
2237/// function is required to be present and required to not be an identifier
2238/// list.
2239///
Reid Spencer5f016e22007-07-11 17:01:13 +00002240/// This method also handles this portion of the grammar:
2241/// parameter-type-list: [C99 6.7.5]
2242/// parameter-list
2243/// parameter-list ',' '...'
2244///
2245/// parameter-list: [C99 6.7.5]
2246/// parameter-declaration
2247/// parameter-list ',' parameter-declaration
2248///
2249/// parameter-declaration: [C99 6.7.5]
2250/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002251/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002252/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002253/// declaration-specifiers abstract-declarator[opt]
2254/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002255/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002256/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2257///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002258/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002259/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002260///
Chris Lattner7399ee02008-10-20 02:05:46 +00002261void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2262 AttributeList *AttrList,
2263 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002264 // lparen is already consumed!
2265 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002266
Chris Lattner7399ee02008-10-20 02:05:46 +00002267 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002268 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002269 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002270 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002271 delete AttrList;
2272 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002273
Sebastian Redlab197ba2009-02-09 18:23:29 +00002274 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002275
2276 // cv-qualifier-seq[opt].
2277 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002278 bool hasExceptionSpec = false;
2279 bool hasAnyExceptionSpec = false;
2280 // FIXME: Does an empty vector ever allocate? Exception specifications are
2281 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2282 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002283 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002284 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002285 if (!DS.getSourceRange().getEnd().isInvalid())
2286 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002287
2288 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002289 if (Tok.is(tok::kw_throw)) {
2290 hasExceptionSpec = true;
2291 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2292 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002293 }
2294
Chris Lattnerf97409f2008-04-06 06:57:35 +00002295 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002296 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002297 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002298 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002299 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002300 /*arglist*/ 0, 0,
2301 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002302 hasExceptionSpec,
2303 hasAnyExceptionSpec,
2304 Exceptions.empty() ? 0 :
2305 &Exceptions[0],
2306 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002307 LParenLoc, D),
2308 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002309 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002310 }
2311
2312 // Alternatively, this parameter list may be an identifier list form for a
2313 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002314 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002315 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002316 // K&R identifier lists can't have typedefs as identifiers, per
2317 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002318 if (RequiresArg) {
2319 Diag(Tok, diag::err_argument_required_after_attribute);
2320 delete AttrList;
2321 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002322 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2323 // normal declarators, not for abstract-declarators.
2324 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002325 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002326 }
2327
2328 // Finally, a normal, non-empty parameter type list.
2329
2330 // Build up an array of information about the parsed arguments.
2331 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002332
2333 // Enter function-declaration scope, limiting any declarators to the
2334 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002335 ParseScope PrototypeScope(this,
2336 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002337
2338 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002339 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002340 while (1) {
2341 if (Tok.is(tok::ellipsis)) {
2342 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002343 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002344 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 }
2346
Chris Lattnerf97409f2008-04-06 06:57:35 +00002347 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002348
Chris Lattnerf97409f2008-04-06 06:57:35 +00002349 // Parse the declaration-specifiers.
2350 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002351
2352 // If the caller parsed attributes for the first argument, add them now.
2353 if (AttrList) {
2354 DS.AddAttributes(AttrList);
2355 AttrList = 0; // Only apply the attributes to the first parameter.
2356 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002357 ParseDeclarationSpecifiers(DS);
2358
Chris Lattnerf97409f2008-04-06 06:57:35 +00002359 // Parse the declarator. This is "PrototypeContext", because we must
2360 // accept either 'declarator' or 'abstract-declarator' here.
2361 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2362 ParseDeclarator(ParmDecl);
2363
2364 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002365 if (Tok.is(tok::kw___attribute)) {
2366 SourceLocation Loc;
2367 AttributeList *AttrList = ParseAttributes(&Loc);
2368 ParmDecl.AddAttributes(AttrList, Loc);
2369 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002370
Chris Lattnerf97409f2008-04-06 06:57:35 +00002371 // Remember this parsed parameter in ParamInfo.
2372 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2373
Douglas Gregor72b505b2008-12-16 21:30:33 +00002374 // DefArgToks is used when the parsing of default arguments needs
2375 // to be delayed.
2376 CachedTokens *DefArgToks = 0;
2377
Chris Lattnerf97409f2008-04-06 06:57:35 +00002378 // If no parameter was specified, verify that *something* was specified,
2379 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002380 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2381 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002382 // Completely missing, emit error.
2383 Diag(DSStart, diag::err_missing_param);
2384 } else {
2385 // Otherwise, we have something. Add it and let semantic analysis try
2386 // to grok it and add the result to the ParamInfo we are building.
2387
2388 // Inform the actions module about the parameter declarator, so it gets
2389 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002390 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002391
2392 // Parse the default argument, if any. We parse the default
2393 // arguments in all dialects; the semantic analysis in
2394 // ActOnParamDefaultArgument will reject the default argument in
2395 // C.
2396 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002397 SourceLocation EqualLoc = Tok.getLocation();
2398
Chris Lattner04421082008-04-08 04:40:51 +00002399 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002400 if (D.getContext() == Declarator::MemberContext) {
2401 // If we're inside a class definition, cache the tokens
2402 // corresponding to the default argument. We'll actually parse
2403 // them when we see the end of the class definition.
2404 // FIXME: Templates will require something similar.
2405 // FIXME: Can we use a smart pointer for Toks?
2406 DefArgToks = new CachedTokens;
2407
2408 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2409 tok::semi, false)) {
2410 delete DefArgToks;
2411 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002412 Actions.ActOnParamDefaultArgumentError(Param);
2413 } else
2414 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002415 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002416 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002417 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002418
2419 OwningExprResult DefArgResult(ParseAssignmentExpression());
2420 if (DefArgResult.isInvalid()) {
2421 Actions.ActOnParamDefaultArgumentError(Param);
2422 SkipUntil(tok::comma, tok::r_paren, true, true);
2423 } else {
2424 // Inform the actions module about the default argument
2425 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002426 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002427 }
Chris Lattner04421082008-04-08 04:40:51 +00002428 }
2429 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002430
2431 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002432 ParmDecl.getIdentifierLoc(), Param,
2433 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002434 }
2435
2436 // If the next token is a comma, consume it and keep reading arguments.
2437 if (Tok.isNot(tok::comma)) break;
2438
2439 // Consume the comma.
2440 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 }
2442
Chris Lattnerf97409f2008-04-06 06:57:35 +00002443 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002444 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002445
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002446 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002447 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002448
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002449 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002450 bool hasExceptionSpec = false;
2451 bool hasAnyExceptionSpec = false;
2452 // FIXME: Does an empty vector ever allocate? Exception specifications are
2453 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2454 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002455 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002456 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002457 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002458 if (!DS.getSourceRange().getEnd().isInvalid())
2459 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002460
2461 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002462 if (Tok.is(tok::kw_throw)) {
2463 hasExceptionSpec = true;
2464 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2465 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002466 }
2467
Reid Spencer5f016e22007-07-11 17:01:13 +00002468 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002469 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002470 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002471 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002472 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002473 hasExceptionSpec,
2474 hasAnyExceptionSpec,
2475 Exceptions.empty() ? 0 :
2476 &Exceptions[0],
2477 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002478 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002479}
2480
Chris Lattner66d28652008-04-06 06:34:08 +00002481/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2482/// we found a K&R-style identifier list instead of a type argument list. The
2483/// current token is known to be the first identifier in the list.
2484///
2485/// identifier-list: [C99 6.7.5]
2486/// identifier
2487/// identifier-list ',' identifier
2488///
2489void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2490 Declarator &D) {
2491 // Build up an array of information about the parsed arguments.
2492 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2493 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2494
2495 // If there was no identifier specified for the declarator, either we are in
2496 // an abstract-declarator, or we are in a parameter declarator which was found
2497 // to be abstract. In abstract-declarators, identifier lists are not valid:
2498 // diagnose this.
2499 if (!D.getIdentifier())
2500 Diag(Tok, diag::ext_ident_list_in_param);
2501
2502 // Tok is known to be the first identifier in the list. Remember this
2503 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002504 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002505 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002506 Tok.getLocation(),
2507 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002508
Chris Lattner50c64772008-04-06 06:39:19 +00002509 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002510
2511 while (Tok.is(tok::comma)) {
2512 // Eat the comma.
2513 ConsumeToken();
2514
Chris Lattner50c64772008-04-06 06:39:19 +00002515 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002516 if (Tok.isNot(tok::identifier)) {
2517 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002518 SkipUntil(tok::r_paren);
2519 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002520 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002521
Chris Lattner66d28652008-04-06 06:34:08 +00002522 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002523
2524 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002525 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002526 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002527
2528 // Verify that the argument identifier has not already been mentioned.
2529 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002530 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002531 } else {
2532 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002533 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002534 Tok.getLocation(),
2535 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002536 }
Chris Lattner66d28652008-04-06 06:34:08 +00002537
2538 // Eat the identifier.
2539 ConsumeToken();
2540 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002541
2542 // If we have the closing ')', eat it and we're done.
2543 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2544
Chris Lattner50c64772008-04-06 06:39:19 +00002545 // Remember that we parsed a function type, and remember the attributes. This
2546 // function type is always a K&R style function type, which is not varargs and
2547 // has no prototype.
2548 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002549 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002550 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002551 /*TypeQuals*/0,
2552 /*exception*/false, false, 0, 0,
2553 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002554 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002555}
Chris Lattneref4715c2008-04-06 05:45:57 +00002556
Reid Spencer5f016e22007-07-11 17:01:13 +00002557/// [C90] direct-declarator '[' constant-expression[opt] ']'
2558/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2559/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2560/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2561/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2562void Parser::ParseBracketDeclarator(Declarator &D) {
2563 SourceLocation StartLoc = ConsumeBracket();
2564
Chris Lattner378c7e42008-12-18 07:27:21 +00002565 // C array syntax has many features, but by-far the most common is [] and [4].
2566 // This code does a fast path to handle some of the most obvious cases.
2567 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002568 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002569 // Remember that we parsed the empty array type.
2570 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002571 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2572 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002573 return;
2574 } else if (Tok.getKind() == tok::numeric_constant &&
2575 GetLookAheadToken(1).is(tok::r_square)) {
2576 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002577 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002578 ConsumeToken();
2579
Sebastian Redlab197ba2009-02-09 18:23:29 +00002580 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002581
2582 // If there was an error parsing the assignment-expression, recover.
2583 if (ExprRes.isInvalid())
2584 ExprRes.release(); // Deallocate expr, just use [].
2585
2586 // Remember that we parsed a array type, and remember its features.
2587 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002588 ExprRes.release(), StartLoc),
2589 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002590 return;
2591 }
2592
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 // If valid, this location is the position where we read the 'static' keyword.
2594 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002595 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 StaticLoc = ConsumeToken();
2597
2598 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002599 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002600 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002601 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002602
2603 // If we haven't already read 'static', check to see if there is one after the
2604 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002605 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 StaticLoc = ConsumeToken();
2607
2608 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2609 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002610 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002611
2612 // Handle the case where we have '[*]' as the array size. However, a leading
2613 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2614 // the the token after the star is a ']'. Since stars in arrays are
2615 // infrequent, use of lookahead is not costly here.
2616 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002617 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002618
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002619 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002620 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002621 StaticLoc = SourceLocation(); // Drop the static.
2622 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002623 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002624 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002625 // Note, in C89, this production uses the constant-expr production instead
2626 // of assignment-expr. The only difference is that assignment-expr allows
2627 // things like '=' and '*='. Sema rejects these in C89 mode because they
2628 // are not i-c-e's, so we don't need to distinguish between the two here.
2629
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 // Parse the assignment-expression now.
2631 NumElements = ParseAssignmentExpression();
2632 }
2633
2634 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002635 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002636 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002637 // If the expression was invalid, skip it.
2638 SkipUntil(tok::r_square);
2639 return;
2640 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002641
2642 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2643
Chris Lattner378c7e42008-12-18 07:27:21 +00002644 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2646 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647 NumElements.release(), StartLoc),
2648 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002649}
2650
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002651/// [GNU] typeof-specifier:
2652/// typeof ( expressions )
2653/// typeof ( type-name )
2654/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002655///
2656void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002657 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002658 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002659 SourceLocation StartLoc = ConsumeToken();
2660
Chris Lattner04d66662007-10-09 17:33:22 +00002661 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002662 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002663 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002664 return;
2665 }
2666
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002667 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002668 if (Result.isInvalid()) {
2669 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002670 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002671 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002672
2673 const char *PrevSpec = 0;
2674 // Check for duplicate type specifiers.
2675 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002676 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002677 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002678
2679 // FIXME: Not accurate, the range gets one token more than it should.
2680 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002681 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002682 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002683
Steve Naroffd1861fd2007-07-31 12:34:36 +00002684 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2685
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002686 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002687 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002688
Douglas Gregor809070a2009-02-18 17:45:20 +00002689 assert((Ty.isInvalid() || Ty.get()) &&
2690 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002691
Chris Lattner04d66662007-10-09 17:33:22 +00002692 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002693 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002694 return;
2695 }
2696 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002697
2698 if (Ty.isInvalid())
2699 DS.SetTypeSpecError();
2700 else {
2701 const char *PrevSpec = 0;
2702 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2703 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2704 Ty.get()))
2705 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2706 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002707 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002708 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002709
2710 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002711 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002712 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002713 return;
2714 }
2715 RParenLoc = ConsumeParen();
2716 const char *PrevSpec = 0;
2717 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2718 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002719 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002720 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002721 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002722 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002723}
2724
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002725