blob: 59e9555a88925a801878e016436fff2a0cf61903 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000030Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
34
35 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
38
Chris Lattnereaaebc72009-04-25 08:06:05 +000039 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000040 return true;
41
42 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
45/// ParseAttributes - Parse a non-empty attributes list.
46///
47/// [GNU] attributes:
48/// attribute
49/// attributes attribute
50///
51/// [GNU] attribute:
52/// '__attribute__' '(' '(' attribute-list ')' ')'
53///
54/// [GNU] attribute-list:
55/// attrib
56/// attribute_list ',' attrib
57///
58/// [GNU] attrib:
59/// empty
60/// attrib-name
61/// attrib-name '(' identifier ')'
62/// attrib-name '(' identifier ',' nonempty-expr-list ')'
63/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
64///
65/// [GNU] attrib-name:
66/// identifier
67/// typespec
68/// typequal
69/// storageclass
70///
71/// FIXME: The GCC grammar/code for this construct implies we need two
72/// token lookahead. Comment from gcc: "If they start with an identifier
73/// which is followed by a comma or close parenthesis, then the arguments
74/// start with that identifier; otherwise they are an expression list."
75///
76/// At the moment, I am not doing 2 token lookahead. I am also unaware of
77/// any attributes that don't work (based on my limited testing). Most
78/// attributes are very simple in practice. Until we find a bug, I don't see
79/// a pressing need to implement the 2 token lookahead.
80
Sebastian Redlab197ba2009-02-09 18:23:29 +000081AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000082 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000083
84 AttributeList *CurrAttr = 0;
85
Chris Lattner04d66662007-10-09 17:33:22 +000086 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
89 "attribute")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
99 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100
Chris Lattner04d66662007-10-09 17:33:22 +0000101 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
103 ConsumeToken();
104 continue;
105 }
106 // we have an identifier or declaration specifier (const, int, etc.)
107 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
108 SourceLocation AttrNameLoc = ConsumeToken();
109
110 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000111 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 ConsumeParen(); // ignore the left paren loc for now
113
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
116 SourceLocation ParmLoc = ConsumeToken();
117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // __attribute__(( mode(byte) ))
120 ConsumeParen(); // ignore the right paren loc for now
121 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
122 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000123 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ConsumeToken();
125 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000126 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 bool ArgExprsOk = true;
128
129 // now parse the non-empty comma separated list of expressions
130 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000131 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000132 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 ArgExprsOk = false;
134 SkipUntil(tok::r_paren);
135 break;
136 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000137 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 }
Chris Lattner04d66662007-10-09 17:33:22 +0000139 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 break;
141 ConsumeToken(); // Eat the comma, move to the next argument
142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 ConsumeParen(); // ignore the right paren loc for now
145 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000146 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
148 }
149 } else { // not an identifier
150 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000151 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // __attribute__(( nonnull() ))
153 ConsumeParen(); // ignore the right paren loc for now
154 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
155 0, SourceLocation(), 0, 0, CurrAttr);
156 } else {
157 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000158 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 bool ArgExprsOk = true;
160
161 // now parse the list of expressions
162 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000163 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000164 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 ArgExprsOk = false;
166 SkipUntil(tok::r_paren);
167 break;
168 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000169 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Chris Lattner04d66662007-10-09 17:33:22 +0000171 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 ConsumeToken(); // Eat the comma, move to the next argument
174 }
175 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000176 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
179 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 CurrAttr);
181 }
182 }
183 }
184 } else {
185 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
186 0, SourceLocation(), 0, 0, CurrAttr);
187 }
188 }
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000191 SourceLocation Loc = Tok.getLocation();;
192 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
193 SkipUntil(tok::r_paren, false);
194 }
195 if (EndLoc)
196 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
198 return CurrAttr;
199}
200
Steve Narofff59e17e2008-12-24 20:59:21 +0000201/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
202/// routine is called to skip/ignore tokens that comprise the MS declspec.
203void Parser::FuzzyParseMicrosoftDeclSpec() {
204 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
205 ConsumeToken();
206 if (Tok.is(tok::l_paren)) {
207 unsigned short savedParenCount = ParenCount;
208 do {
209 ConsumeAnyToken();
210 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
211 }
212 return;
213}
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// ParseDeclaration - Parse a full 'declaration', which consists of
216/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000217/// 'Context' should be a Declarator::TheContext value. This returns the
218/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redl50de12f2009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattner97144fc2009-04-02 04:16:50 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
232 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000233 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000234 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000235 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000236 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000237 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000238 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000239 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000240 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000241 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000242 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000243 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000244 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000245 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000246 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000247 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000248 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000249 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000250 }
Chris Lattner682bf922009-03-29 16:50:03 +0000251
252 // This routine returns a DeclGroup, if the thing we parsed only contains a
253 // single decl, convert it now.
254 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000255}
256
257/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
258/// declaration-specifiers init-declarator-list[opt] ';'
259///[C90/C++]init-declarator-list ';' [TODO]
260/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000261///
262/// If RequireSemi is false, this does not check for a ';' at the end of the
263/// declaration.
264Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000265 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000266 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // Parse the common declaration-specifiers piece.
268 DeclSpec DS;
269 ParseDeclarationSpecifiers(DS);
270
271 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
272 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000273 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000275 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
276 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
278
279 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
280 ParseDeclarator(DeclaratorInfo);
281
Chris Lattner23c4b182009-03-29 17:18:04 +0000282 DeclGroupPtrTy DG =
283 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000284
Chris Lattner97144fc2009-04-02 04:16:50 +0000285 DeclEnd = Tok.getLocation();
286
Chris Lattnercd147752009-03-29 17:27:48 +0000287 // If the client wants to check what comes after the declaration, just return
288 // immediately without checking anything!
289 if (!RequireSemi) return DG;
Chris Lattner23c4b182009-03-29 17:18:04 +0000290
291 if (Tok.is(tok::semi)) {
292 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000293 return DG;
294 }
295
Chris Lattner23c4b182009-03-29 17:18:04 +0000296 Diag(Tok, diag::err_expected_semi_declation);
297 // Skip to end of block or statement
298 SkipUntil(tok::r_brace, true, true);
299 if (Tok.is(tok::semi))
300 ConsumeToken();
301 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
Douglas Gregor1426e532009-05-12 21:31:51 +0000304/// \brief Parse 'declaration' after parsing 'declaration-specifiers
305/// declarator'. This method parses the remainder of the declaration
306/// (including any attributes or initializer, among other things) and
307/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000308///
Reid Spencer5f016e22007-07-11 17:01:13 +0000309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000324///
Douglas Gregor1426e532009-05-12 21:31:51 +0000325Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
326 // If a simple-asm-expr is present, parse it.
327 if (Tok.is(tok::kw_asm)) {
328 SourceLocation Loc;
329 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
330 if (AsmLabel.isInvalid()) {
331 SkipUntil(tok::semi, true, true);
332 return DeclPtrTy();
333 }
334
335 D.setAsmLabel(AsmLabel.release());
336 D.SetRangeEnd(Loc);
337 }
338
339 // If attributes are present, parse them.
340 if (Tok.is(tok::kw___attribute)) {
341 SourceLocation Loc;
342 AttributeList *AttrList = ParseAttributes(&Loc);
343 D.AddAttributes(AttrList, Loc);
344 }
345
346 // Inform the current actions module that we just parsed this declarator.
347 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
348
349 // Parse declarator '=' initializer.
350 if (Tok.is(tok::equal)) {
351 ConsumeToken();
352 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
353 SourceLocation DelLoc = ConsumeToken();
354 Actions.SetDeclDeleted(ThisDecl, DelLoc);
355 } else {
356 OwningExprResult Init(ParseInitializer());
357 if (Init.isInvalid()) {
358 SkipUntil(tok::semi, true, true);
359 return DeclPtrTy();
360 }
361 Actions.AddInitializerToDecl(ThisDecl, move(Init));
362 }
363 } else if (Tok.is(tok::l_paren)) {
364 // Parse C++ direct initializer: '(' expression-list ')'
365 SourceLocation LParenLoc = ConsumeParen();
366 ExprVector Exprs(Actions);
367 CommaLocsTy CommaLocs;
368
369 if (ParseExpressionList(Exprs, CommaLocs)) {
370 SkipUntil(tok::r_paren);
371 } else {
372 // Match the ')'.
373 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
374
375 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
376 "Unexpected number of commas!");
377 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
378 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000379 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000380 }
381 } else {
382 Actions.ActOnUninitializedDecl(ThisDecl);
383 }
384
385 return ThisDecl;
386}
387
388/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
389/// parsing 'declaration-specifiers declarator'. This method is split out this
390/// way to handle the ambiguity between top-level function-definitions and
391/// declarations.
392///
393/// init-declarator-list: [C99 6.7]
394/// init-declarator
395/// init-declarator-list ',' init-declarator
396///
397/// According to the standard grammar, =default and =delete are function
398/// definitions, but that definitely doesn't fit with the parser here.
399///
Chris Lattner682bf922009-03-29 16:50:03 +0000400Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000401ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000402 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
403 // that we parse together here.
404 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
406 // At this point, we know that it is not a function definition. Parse the
407 // rest of the init-declarator-list.
408 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000409 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
410 if (ThisDecl.get())
411 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 // If we don't have a comma, it is either the end of the list (a ';') or an
414 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000415 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 break;
417
418 // Consume the comma.
419 ConsumeToken();
420
421 // Parse the next declarator.
422 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000423
424 // Accept attributes in an init-declarator. In the first declarator in a
425 // declaration, these would be part of the declspec. In subsequent
426 // declarators, they become part of the declarator itself, so that they
427 // don't apply to declarators after *this* one. Examples:
428 // short __attribute__((common)) var; -> declspec
429 // short var __attribute__((common)); -> declarator
430 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000431 if (Tok.is(tok::kw___attribute)) {
432 SourceLocation Loc;
433 AttributeList *AttrList = ParseAttributes(&Loc);
434 D.AddAttributes(AttrList, Loc);
435 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 ParseDeclarator(D);
438 }
439
Jay Foadbeaaccd2009-05-21 09:52:38 +0000440 return Actions.FinalizeDeclaratorGroup(CurScope, DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000441 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000442}
443
444/// ParseSpecifierQualifierList
445/// specifier-qualifier-list:
446/// type-specifier specifier-qualifier-list[opt]
447/// type-qualifier specifier-qualifier-list[opt]
448/// [GNU] attributes specifier-qualifier-list[opt]
449///
450void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
451 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
452 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 ParseDeclarationSpecifiers(DS);
454
455 // Validate declspec for type-name.
456 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000457 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
458 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 Diag(Tok, diag::err_typename_requires_specqual);
460
461 // Issue diagnostic and remove storage class if present.
462 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
463 if (DS.getStorageClassSpecLoc().isValid())
464 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
465 else
466 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
467 DS.ClearStorageClassSpecs();
468 }
469
470 // Issue diagnostic and remove function specfier if present.
471 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000472 if (DS.isInlineSpecified())
473 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
474 if (DS.isVirtualSpecified())
475 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
476 if (DS.isExplicitSpecified())
477 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 DS.ClearFunctionSpecs();
479 }
480}
481
Chris Lattnerc199ab32009-04-12 20:42:31 +0000482/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
483/// specified token is valid after the identifier in a declarator which
484/// immediately follows the declspec. For example, these things are valid:
485///
486/// int x [ 4]; // direct-declarator
487/// int x ( int y); // direct-declarator
488/// int(int x ) // direct-declarator
489/// int x ; // simple-declaration
490/// int x = 17; // init-declarator-list
491/// int x , y; // init-declarator-list
492/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000493/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000494/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000495///
496/// This is not, because 'x' does not immediately follow the declspec (though
497/// ')' happens to be valid anyway).
498/// int (x)
499///
500static bool isValidAfterIdentifierInDeclarator(const Token &T) {
501 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
502 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000503 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000504}
505
Chris Lattnere40c2952009-04-14 21:34:55 +0000506
507/// ParseImplicitInt - This method is called when we have an non-typename
508/// identifier in a declspec (which normally terminates the decl spec) when
509/// the declspec has no type specifier. In this case, the declspec is either
510/// malformed or is "implicit int" (in K&R and C89).
511///
512/// This method handles diagnosing this prettily and returns false if the
513/// declspec is done being processed. If it recovers and thinks there may be
514/// other pieces of declspec after it, it returns true.
515///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000516bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000517 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000518 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000519 assert(Tok.is(tok::identifier) && "should have identifier");
520
Chris Lattnere40c2952009-04-14 21:34:55 +0000521 SourceLocation Loc = Tok.getLocation();
522 // If we see an identifier that is not a type name, we normally would
523 // parse it as the identifer being declared. However, when a typename
524 // is typo'd or the definition is not included, this will incorrectly
525 // parse the typename as the identifier name and fall over misparsing
526 // later parts of the diagnostic.
527 //
528 // As such, we try to do some look-ahead in cases where this would
529 // otherwise be an "implicit-int" case to see if this is invalid. For
530 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
531 // an identifier with implicit int, we'd get a parse error because the
532 // next token is obviously invalid for a type. Parse these as a case
533 // with an invalid type specifier.
534 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
535
536 // Since we know that this either implicit int (which is rare) or an
537 // error, we'd do lookahead to try to do better recovery.
538 if (isValidAfterIdentifierInDeclarator(NextToken())) {
539 // If this token is valid for implicit int, e.g. "static x = 4", then
540 // we just avoid eating the identifier, so it will be parsed as the
541 // identifier in the declarator.
542 return false;
543 }
544
545 // Otherwise, if we don't consume this token, we are going to emit an
546 // error anyway. Try to recover from various common problems. Check
547 // to see if this was a reference to a tag name without a tag specified.
548 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000549 //
550 // C++ doesn't need this, and isTagName doesn't take SS.
551 if (SS == 0) {
552 const char *TagName = 0;
553 tok::TokenKind TagKind = tok::unknown;
Chris Lattnere40c2952009-04-14 21:34:55 +0000554
Chris Lattnere40c2952009-04-14 21:34:55 +0000555 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
556 default: break;
557 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
558 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
559 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
560 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
561 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000562
Chris Lattnerf4382f52009-04-14 22:17:06 +0000563 if (TagName) {
564 Diag(Loc, diag::err_use_of_tag_name_without_tag)
565 << Tok.getIdentifierInfo() << TagName
566 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
567
568 // Parse this as a tag as if the missing tag were present.
569 if (TagKind == tok::kw_enum)
570 ParseEnumSpecifier(Loc, DS, AS);
571 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000572 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000573 return true;
574 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000575 }
576
577 // Since this is almost certainly an invalid type name, emit a
578 // diagnostic that says it, eat the token, and mark the declspec as
579 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000580 SourceRange R;
581 if (SS) R = SS->getRange();
582
583 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000584 const char *PrevSpec;
585 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
586 DS.SetRangeEnd(Tok.getLocation());
587 ConsumeToken();
588
589 // TODO: Could inject an invalid typedef decl in an enclosing scope to
590 // avoid rippling error messages on subsequent uses of the same type,
591 // could be useful if #include was forgotten.
592 return false;
593}
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// ParseDeclarationSpecifiers
596/// declaration-specifiers: [C99 6.7]
597/// storage-class-specifier declaration-specifiers[opt]
598/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000599/// [C99] function-specifier declaration-specifiers[opt]
600/// [GNU] attributes declaration-specifiers[opt]
601///
602/// storage-class-specifier: [C99 6.7.1]
603/// 'typedef'
604/// 'extern'
605/// 'static'
606/// 'auto'
607/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000608/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000609/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000610/// function-specifier: [C99 6.7.4]
611/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000612/// [C++] 'virtual'
613/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000614/// 'friend': [C++ dcl.friend]
615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000617void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000618 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerc199ab32009-04-12 20:42:31 +0000619 AccessSpecifier AS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000620 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 while (1) {
622 int isInvalid = false;
623 const char *PrevSpec = 0;
624 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000627 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000628 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 // If this is not a declaration specifier token, we're done reading decl
630 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000631 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000633
634 case tok::coloncolon: // ::foo::bar
635 // Annotate C++ scope specifiers. If we get one, loop.
636 if (TryAnnotateCXXScopeToken())
637 continue;
638 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000639
640 case tok::annot_cxxscope: {
641 if (DS.hasTypeSpecifier())
642 goto DoneWithDeclSpec;
643
644 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000645 Token Next = NextToken();
646 if (Next.is(tok::annot_template_id) &&
647 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000648 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000649 // We have a qualified template-id, e.g., N::A<int>
650 CXXScopeSpec SS;
651 ParseOptionalCXXScopeSpecifier(SS);
652 assert(Tok.is(tok::annot_template_id) &&
653 "ParseOptionalCXXScopeSpecifier not working");
654 AnnotateTemplateIdTokenAsType(&SS);
655 continue;
656 }
657
658 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000659 goto DoneWithDeclSpec;
660
661 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000662 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000663 SS.setRange(Tok.getAnnotationRange());
664
665 // If the next token is the name of the class type that the C++ scope
666 // denotes, followed by a '(', then this is a constructor declaration.
667 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000668 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000669 CurScope, &SS) &&
670 GetLookAheadToken(2).is(tok::l_paren))
671 goto DoneWithDeclSpec;
672
Douglas Gregorb696ea32009-02-04 17:00:24 +0000673 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
674 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000675
Chris Lattnerf4382f52009-04-14 22:17:06 +0000676 // If the referenced identifier is not a type, then this declspec is
677 // erroneous: We already checked about that it has no type specifier, and
678 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
679 // typename.
680 if (TypeRep == 0) {
681 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000682 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000683 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000684 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000685
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000686 ConsumeToken(); // The C++ scope.
687
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000689 TypeRep);
690 if (isInvalid)
691 break;
692
693 DS.SetRangeEnd(Tok.getLocation());
694 ConsumeToken(); // The typename.
695
696 continue;
697 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000698
699 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000700 if (Tok.getAnnotationValue())
701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
702 Tok.getAnnotationValue());
703 else
704 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000705 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
706 ConsumeToken(); // The typename
707
708 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
709 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
710 // Objective-C interface. If we don't have Objective-C or a '<', this is
711 // just a normal reference to a typedef name.
712 if (!Tok.is(tok::less) || !getLang().ObjC1)
713 continue;
714
715 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000716 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000717 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
718 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
719
720 DS.SetRangeEnd(EndProtoLoc);
721 continue;
722 }
723
Chris Lattner3bd934a2008-07-26 01:18:38 +0000724 // typedef-name
725 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000726 // In C++, check to see if this is a scope specifier like foo::bar::, if
727 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000728 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
729 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000730
Chris Lattner3bd934a2008-07-26 01:18:38 +0000731 // This identifier can only be a typedef name if we haven't already seen
732 // a type-specifier. Without this check we misparse:
733 // typedef int X; struct Y { short X; }; as 'short int'.
734 if (DS.hasTypeSpecifier())
735 goto DoneWithDeclSpec;
736
737 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000738 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
739 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000740
Chris Lattnerc199ab32009-04-12 20:42:31 +0000741 // If this is not a typedef name, don't parse it as part of the declspec,
742 // it must be an implicit int or an error.
743 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000744 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000745 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000746 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000747
Douglas Gregorb48fe382008-10-31 09:07:45 +0000748 // C++: If the identifier is actually the name of the class type
749 // being defined and the next token is a '(', then this is a
750 // constructor declaration. We're done with the decl-specifiers
751 // and will treat this token as an identifier.
Chris Lattnerc199ab32009-04-12 20:42:31 +0000752 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000753 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
754 NextToken().getKind() == tok::l_paren)
755 goto DoneWithDeclSpec;
756
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000758 TypeRep);
759 if (isInvalid)
760 break;
761
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken(); // The identifier
764
765 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
766 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
767 // Objective-C interface. If we don't have Objective-C or a '<', this is
768 // just a normal reference to a typedef name.
769 if (!Tok.is(tok::less) || !getLang().ObjC1)
770 continue;
771
772 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000773 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000774 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000775 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000776
777 DS.SetRangeEnd(EndProtoLoc);
778
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000779 // Need to support trailing type qualifiers (e.g. "id<p> const").
780 // If a type specifier follows, it will be diagnosed elsewhere.
781 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000782 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000783
784 // type-name
785 case tok::annot_template_id: {
786 TemplateIdAnnotation *TemplateId
787 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000788 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000789 // This template-id does not refer to a type name, so we're
790 // done with the type-specifiers.
791 goto DoneWithDeclSpec;
792 }
793
794 // Turn the template-id annotation token into a type annotation
795 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000796 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000797 continue;
798 }
799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 // GNU attributes support.
801 case tok::kw___attribute:
802 DS.AddAttributes(ParseAttributes());
803 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000804
805 // Microsoft declspec support.
806 case tok::kw___declspec:
807 if (!PP.getLangOptions().Microsoft)
808 goto DoneWithDeclSpec;
809 FuzzyParseMicrosoftDeclSpec();
810 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000811
Steve Naroff239f0732008-12-25 14:16:32 +0000812 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000813 case tok::kw___forceinline:
814 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000815 case tok::kw___cdecl:
816 case tok::kw___stdcall:
817 case tok::kw___fastcall:
818 if (!PP.getLangOptions().Microsoft)
819 goto DoneWithDeclSpec;
820 // Just ignore it.
821 break;
822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // storage-class-specifier
824 case tok::kw_typedef:
825 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
826 break;
827 case tok::kw_extern:
828 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000829 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
831 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000832 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000833 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
834 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000835 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 case tok::kw_static:
837 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000838 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
840 break;
841 case tok::kw_auto:
842 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
843 break;
844 case tok::kw_register:
845 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
846 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000847 case tok::kw_mutable:
848 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
849 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 case tok::kw___thread:
851 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
852 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 // function-specifier
855 case tok::kw_inline:
856 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
857 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000858 case tok::kw_virtual:
859 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
860 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000861 case tok::kw_explicit:
862 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
863 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000864
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000865 // friend
866 case tok::kw_friend:
867 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
868 break;
869
Chris Lattner80d0c892009-01-21 19:48:37 +0000870 // type-specifier
871 case tok::kw_short:
872 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
873 break;
874 case tok::kw_long:
875 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
876 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
877 else
878 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
879 break;
880 case tok::kw_signed:
881 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
882 break;
883 case tok::kw_unsigned:
884 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
885 break;
886 case tok::kw__Complex:
887 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
888 break;
889 case tok::kw__Imaginary:
890 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
891 break;
892 case tok::kw_void:
893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
894 break;
895 case tok::kw_char:
896 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
897 break;
898 case tok::kw_int:
899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
900 break;
901 case tok::kw_float:
902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
903 break;
904 case tok::kw_double:
905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
906 break;
907 case tok::kw_wchar_t:
908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
909 break;
910 case tok::kw_bool:
911 case tok::kw__Bool:
912 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
913 break;
914 case tok::kw__Decimal32:
915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
916 break;
917 case tok::kw__Decimal64:
918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
919 break;
920 case tok::kw__Decimal128:
921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
922 break;
923
924 // class-specifier:
925 case tok::kw_class:
926 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +0000927 case tok::kw_union: {
928 tok::TokenKind Kind = Tok.getKind();
929 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000930 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000931 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +0000932 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000933
934 // enum-specifier:
935 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +0000936 ConsumeToken();
937 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000938 continue;
939
940 // cv-qualifier:
941 case tok::kw_const:
942 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
943 break;
944 case tok::kw_volatile:
945 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
946 getLang())*2;
947 break;
948 case tok::kw_restrict:
949 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
950 getLang())*2;
951 break;
952
Douglas Gregord57959a2009-03-27 23:10:48 +0000953 // C++ typename-specifier:
954 case tok::kw_typename:
955 if (TryAnnotateTypeOrScopeToken())
956 continue;
957 break;
958
Chris Lattner80d0c892009-01-21 19:48:37 +0000959 // GNU typeof support.
960 case tok::kw_typeof:
961 ParseTypeofSpecifier(DS);
962 continue;
963
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000964 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000965 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000966 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
967 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000968 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000969 goto DoneWithDeclSpec;
970
971 {
972 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000973 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000974 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000975 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000976 DS.SetRangeEnd(EndProtoLoc);
977
Chris Lattner1ab3b962008-11-18 07:48:38 +0000978 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +0000979 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +0000980 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000981 // Need to support trailing type qualifiers (e.g. "id<p> const").
982 // If a type specifier follows, it will be diagnosed elsewhere.
983 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000984 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 }
986 // If the specifier combination wasn't legal, issue a diagnostic.
987 if (isInvalid) {
988 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000989 // Pick between error or extwarn.
990 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
991 : diag::ext_duplicate_declspec;
992 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000994 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 ConsumeToken();
996 }
997}
Douglas Gregoradcac882008-12-01 23:54:00 +0000998
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000999/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001000/// primarily follow the C++ grammar with additions for C99 and GNU,
1001/// which together subsume the C grammar. Note that the C++
1002/// type-specifier also includes the C type-qualifier (for const,
1003/// volatile, and C99 restrict). Returns true if a type-specifier was
1004/// found (and parsed), false otherwise.
1005///
1006/// type-specifier: [C++ 7.1.5]
1007/// simple-type-specifier
1008/// class-specifier
1009/// enum-specifier
1010/// elaborated-type-specifier [TODO]
1011/// cv-qualifier
1012///
1013/// cv-qualifier: [C++ 7.1.5.1]
1014/// 'const'
1015/// 'volatile'
1016/// [C99] 'restrict'
1017///
1018/// simple-type-specifier: [ C++ 7.1.5.2]
1019/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1020/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1021/// 'char'
1022/// 'wchar_t'
1023/// 'bool'
1024/// 'short'
1025/// 'int'
1026/// 'long'
1027/// 'signed'
1028/// 'unsigned'
1029/// 'float'
1030/// 'double'
1031/// 'void'
1032/// [C99] '_Bool'
1033/// [C99] '_Complex'
1034/// [C99] '_Imaginary' // Removed in TC2?
1035/// [GNU] '_Decimal32'
1036/// [GNU] '_Decimal64'
1037/// [GNU] '_Decimal128'
1038/// [GNU] typeof-specifier
1039/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1040/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001041bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1042 const char *&PrevSpec,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001043 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001044 SourceLocation Loc = Tok.getLocation();
1045
1046 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001047 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001048 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001049 // Annotate typenames and C++ scope specifiers. If we get one, just
1050 // recurse to handle whatever we get.
1051 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001052 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001053 // Otherwise, not a type specifier.
1054 return false;
1055 case tok::coloncolon: // ::foo::bar
1056 if (NextToken().is(tok::kw_new) || // ::new
1057 NextToken().is(tok::kw_delete)) // ::delete
1058 return false;
1059
1060 // Annotate typenames and C++ scope specifiers. If we get one, just
1061 // recurse to handle whatever we get.
1062 if (TryAnnotateTypeOrScopeToken())
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001063 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001064 // Otherwise, not a type specifier.
1065 return false;
1066
Douglas Gregor12e083c2008-11-07 15:42:26 +00001067 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001068 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001069 if (Tok.getAnnotationValue())
1070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1071 Tok.getAnnotationValue());
1072 else
1073 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001074 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1075 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001076
1077 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1078 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1079 // Objective-C interface. If we don't have Objective-C or a '<', this is
1080 // just a normal reference to a typedef name.
1081 if (!Tok.is(tok::less) || !getLang().ObjC1)
1082 return true;
1083
1084 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001085 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001086 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1087 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1088
1089 DS.SetRangeEnd(EndProtoLoc);
1090 return true;
1091 }
1092
1093 case tok::kw_short:
1094 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1095 break;
1096 case tok::kw_long:
1097 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1098 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1099 else
1100 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1101 break;
1102 case tok::kw_signed:
1103 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1104 break;
1105 case tok::kw_unsigned:
1106 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1107 break;
1108 case tok::kw__Complex:
1109 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1110 break;
1111 case tok::kw__Imaginary:
1112 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1113 break;
1114 case tok::kw_void:
1115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1116 break;
1117 case tok::kw_char:
1118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1119 break;
1120 case tok::kw_int:
1121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1122 break;
1123 case tok::kw_float:
1124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1125 break;
1126 case tok::kw_double:
1127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1128 break;
1129 case tok::kw_wchar_t:
1130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1131 break;
1132 case tok::kw_bool:
1133 case tok::kw__Bool:
1134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1135 break;
1136 case tok::kw__Decimal32:
1137 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1138 break;
1139 case tok::kw__Decimal64:
1140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1141 break;
1142 case tok::kw__Decimal128:
1143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1144 break;
1145
1146 // class-specifier:
1147 case tok::kw_class:
1148 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001149 case tok::kw_union: {
1150 tok::TokenKind Kind = Tok.getKind();
1151 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001152 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001153 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001154 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001155
1156 // enum-specifier:
1157 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001158 ConsumeToken();
1159 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001160 return true;
1161
1162 // cv-qualifier:
1163 case tok::kw_const:
1164 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1165 getLang())*2;
1166 break;
1167 case tok::kw_volatile:
1168 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1169 getLang())*2;
1170 break;
1171 case tok::kw_restrict:
1172 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1173 getLang())*2;
1174 break;
1175
1176 // GNU typeof support.
1177 case tok::kw_typeof:
1178 ParseTypeofSpecifier(DS);
1179 return true;
1180
Steve Naroff239f0732008-12-25 14:16:32 +00001181 case tok::kw___cdecl:
1182 case tok::kw___stdcall:
1183 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +00001184 if (!PP.getLangOptions().Microsoft) return false;
1185 ConsumeToken();
1186 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001187
Douglas Gregor12e083c2008-11-07 15:42:26 +00001188 default:
1189 // Not a type-specifier; do nothing.
1190 return false;
1191 }
1192
1193 // If the specifier combination wasn't legal, issue a diagnostic.
1194 if (isInvalid) {
1195 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001196 // Pick between error or extwarn.
1197 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1198 : diag::ext_duplicate_declspec;
1199 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001200 }
1201 DS.SetRangeEnd(Tok.getLocation());
1202 ConsumeToken(); // whatever we parsed above.
1203 return true;
1204}
Reid Spencer5f016e22007-07-11 17:01:13 +00001205
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001206/// ParseStructDeclaration - Parse a struct declaration without the terminating
1207/// semicolon.
1208///
Reid Spencer5f016e22007-07-11 17:01:13 +00001209/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001210/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001211/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001212/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001213/// struct-declarator-list:
1214/// struct-declarator
1215/// struct-declarator-list ',' struct-declarator
1216/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1217/// struct-declarator:
1218/// declarator
1219/// [GNU] declarator attributes[opt]
1220/// declarator[opt] ':' constant-expression
1221/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1222///
Chris Lattnere1359422008-04-10 06:46:29 +00001223void Parser::
1224ParseStructDeclaration(DeclSpec &DS,
1225 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001226 if (Tok.is(tok::kw___extension__)) {
1227 // __extension__ silences extension warnings in the subexpression.
1228 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001229 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001230 return ParseStructDeclaration(DS, Fields);
1231 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001232
1233 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001234 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001235 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001236
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001237 // If there are no declarators, this is a free-standing declaration
1238 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001239 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001240 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001241 return;
1242 }
1243
1244 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001245 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001246 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001247 FieldDeclarator &DeclaratorInfo = Fields.back();
1248
Steve Naroff28a7ca82007-08-20 22:28:22 +00001249 /// struct-declarator: declarator
1250 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001251 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001252 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001253
Chris Lattner04d66662007-10-09 17:33:22 +00001254 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001255 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001256 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001257 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001258 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001259 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001260 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001261 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001262
Steve Naroff28a7ca82007-08-20 22:28:22 +00001263 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001264 if (Tok.is(tok::kw___attribute)) {
1265 SourceLocation Loc;
1266 AttributeList *AttrList = ParseAttributes(&Loc);
1267 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1268 }
1269
Steve Naroff28a7ca82007-08-20 22:28:22 +00001270 // If we don't have a comma, it is either the end of the list (a ';')
1271 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001272 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001273 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001274
Steve Naroff28a7ca82007-08-20 22:28:22 +00001275 // Consume the comma.
1276 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001277
Steve Naroff28a7ca82007-08-20 22:28:22 +00001278 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001279 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001280
Steve Naroff28a7ca82007-08-20 22:28:22 +00001281 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001282 if (Tok.is(tok::kw___attribute)) {
1283 SourceLocation Loc;
1284 AttributeList *AttrList = ParseAttributes(&Loc);
1285 Fields.back().D.AddAttributes(AttrList, Loc);
1286 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001287 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001288}
1289
1290/// ParseStructUnionBody
1291/// struct-contents:
1292/// struct-declaration-list
1293/// [EXT] empty
1294/// [GNU] "struct-declaration-list" without terminatoring ';'
1295/// struct-declaration-list:
1296/// struct-declaration
1297/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001298/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001299///
Reid Spencer5f016e22007-07-11 17:01:13 +00001300void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001301 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001302 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1303 PP.getSourceManager(),
1304 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001305
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 SourceLocation LBraceLoc = ConsumeBrace();
1307
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001308 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001309 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1310
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1312 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001313 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001314 Diag(Tok, diag::ext_empty_struct_union_enum)
1315 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316
Chris Lattnerb28317a2009-03-28 19:18:32 +00001317 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001318 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1319
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 // Each iteration of this loop reads one struct-declaration.
1323
1324 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001325 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001326 Diag(Tok, diag::ext_extra_struct_semi)
1327 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 ConsumeToken();
1329 continue;
1330 }
Chris Lattnere1359422008-04-10 06:46:29 +00001331
1332 // Parse all the comma separated declarators.
1333 DeclSpec DS;
1334 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001335 if (!Tok.is(tok::at)) {
1336 ParseStructDeclaration(DS, FieldDeclarators);
1337
1338 // Convert them all to fields.
1339 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1340 FieldDeclarator &FD = FieldDeclarators[i];
1341 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001342 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1343 DS.getSourceRange().getBegin(),
1344 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001345 FieldDecls.push_back(Field);
1346 }
1347 } else { // Handle @defs
1348 ConsumeToken();
1349 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1350 Diag(Tok, diag::err_unexpected_at);
1351 SkipUntil(tok::semi, true, true);
1352 continue;
1353 }
1354 ConsumeToken();
1355 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1356 if (!Tok.is(tok::identifier)) {
1357 Diag(Tok, diag::err_expected_ident);
1358 SkipUntil(tok::semi, true, true);
1359 continue;
1360 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001361 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001362 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1363 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001364 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1365 ConsumeToken();
1366 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1367 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001368
Chris Lattner04d66662007-10-09 17:33:22 +00001369 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001371 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001372 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 break;
1374 } else {
1375 Diag(Tok, diag::err_expected_semi_decl_list);
1376 // Skip to end of block or statement
1377 SkipUntil(tok::r_brace, true, true);
1378 }
1379 }
1380
Steve Naroff60fccee2007-10-29 21:38:07 +00001381 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 AttributeList *AttrList = 0;
1384 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001385 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001386 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001387
1388 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001389 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001390 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001391 AttrList);
1392 StructScope.Exit();
1393 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001394}
1395
1396
1397/// ParseEnumSpecifier
1398/// enum-specifier: [C99 6.7.2.2]
1399/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001400///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001401/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1402/// '}' attributes[opt]
1403/// 'enum' identifier
1404/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001405///
1406/// [C++] elaborated-type-specifier:
1407/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1408///
Chris Lattner4c97d762009-04-12 21:49:30 +00001409void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1410 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001412
1413 AttributeList *Attr = 0;
1414 // If attributes exist after tag, parse them.
1415 if (Tok.is(tok::kw___attribute))
1416 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001417
1418 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001419 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001420 if (Tok.isNot(tok::identifier)) {
1421 Diag(Tok, diag::err_expected_ident);
1422 if (Tok.isNot(tok::l_brace)) {
1423 // Has no name and is not a definition.
1424 // Skip the rest of this declarator, up until the comma or semicolon.
1425 SkipUntil(tok::comma, true);
1426 return;
1427 }
1428 }
1429 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001430
1431 // Must have either 'enum name' or 'enum {...}'.
1432 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1433 Diag(Tok, diag::err_expected_ident_lbrace);
1434
1435 // Skip the rest of this declarator, up until the comma or semicolon.
1436 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001437 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001438 }
1439
1440 // If an identifier is present, consume and remember it.
1441 IdentifierInfo *Name = 0;
1442 SourceLocation NameLoc;
1443 if (Tok.is(tok::identifier)) {
1444 Name = Tok.getIdentifierInfo();
1445 NameLoc = ConsumeToken();
1446 }
1447
1448 // There are three options here. If we have 'enum foo;', then this is a
1449 // forward declaration. If we have 'enum foo {...' then this is a
1450 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1451 //
1452 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1453 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1454 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1455 //
1456 Action::TagKind TK;
1457 if (Tok.is(tok::l_brace))
1458 TK = Action::TK_Definition;
1459 else if (Tok.is(tok::semi))
1460 TK = Action::TK_Declaration;
1461 else
1462 TK = Action::TK_Reference;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001463 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1464 StartLoc, SS, Name, NameLoc, Attr, AS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001465
Chris Lattner04d66662007-10-09 17:33:22 +00001466 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 ParseEnumBody(StartLoc, TagDecl);
1468
1469 // TODO: semantic analysis on the declspec for enums.
1470 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001471 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1472 TagDecl.getAs<void>()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001473 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001474}
1475
1476/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1477/// enumerator-list:
1478/// enumerator
1479/// enumerator-list ',' enumerator
1480/// enumerator:
1481/// enumeration-constant
1482/// enumeration-constant '=' constant-expression
1483/// enumeration-constant:
1484/// identifier
1485///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001486void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001487 // Enter the scope of the enum body and start the definition.
1488 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001489 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 SourceLocation LBraceLoc = ConsumeBrace();
1492
Chris Lattner7946dd32007-08-27 17:24:30 +00001493 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001494 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001495 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001496
Chris Lattnerb28317a2009-03-28 19:18:32 +00001497 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001498
Chris Lattnerb28317a2009-03-28 19:18:32 +00001499 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001500
1501 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001502 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1504 SourceLocation IdentLoc = ConsumeToken();
1505
1506 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001507 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001508 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001510 AssignedVal = ParseConstantExpression();
1511 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 }
1514
1515 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001516 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1517 LastEnumConstDecl,
1518 IdentLoc, Ident,
1519 EqualLoc,
1520 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 EnumConstantDecls.push_back(EnumConstDecl);
1522 LastEnumConstDecl = EnumConstDecl;
1523
Chris Lattner04d66662007-10-09 17:33:22 +00001524 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 break;
1526 SourceLocation CommaLoc = ConsumeToken();
1527
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001528 if (Tok.isNot(tok::identifier) &&
1529 !(getLang().C99 || getLang().CPlusPlus0x))
1530 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1531 << getLang().CPlusPlus
1532 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 }
1534
1535 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001536 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001537
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001538 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001539 EnumConstantDecls.data(), EnumConstantDecls.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001540
Chris Lattnerb28317a2009-03-28 19:18:32 +00001541 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001543 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001545
1546 EnumScope.Exit();
1547 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001548}
1549
1550/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001551/// start of a type-qualifier-list.
1552bool Parser::isTypeQualifier() const {
1553 switch (Tok.getKind()) {
1554 default: return false;
1555 // type-qualifier
1556 case tok::kw_const:
1557 case tok::kw_volatile:
1558 case tok::kw_restrict:
1559 return true;
1560 }
1561}
1562
1563/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001564/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001565bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001566 switch (Tok.getKind()) {
1567 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001568
1569 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001570 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001571 // Annotate typenames and C++ scope specifiers. If we get one, just
1572 // recurse to handle whatever we get.
1573 if (TryAnnotateTypeOrScopeToken())
1574 return isTypeSpecifierQualifier();
1575 // Otherwise, not a type specifier.
1576 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001577
Chris Lattner166a8fc2009-01-04 23:41:41 +00001578 case tok::coloncolon: // ::foo::bar
1579 if (NextToken().is(tok::kw_new) || // ::new
1580 NextToken().is(tok::kw_delete)) // ::delete
1581 return false;
1582
1583 // Annotate typenames and C++ scope specifiers. If we get one, just
1584 // recurse to handle whatever we get.
1585 if (TryAnnotateTypeOrScopeToken())
1586 return isTypeSpecifierQualifier();
1587 // Otherwise, not a type specifier.
1588 return false;
1589
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 // GNU attributes support.
1591 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001592 // GNU typeof support.
1593 case tok::kw_typeof:
1594
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 // type-specifiers
1596 case tok::kw_short:
1597 case tok::kw_long:
1598 case tok::kw_signed:
1599 case tok::kw_unsigned:
1600 case tok::kw__Complex:
1601 case tok::kw__Imaginary:
1602 case tok::kw_void:
1603 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001604 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 case tok::kw_int:
1606 case tok::kw_float:
1607 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001608 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 case tok::kw__Bool:
1610 case tok::kw__Decimal32:
1611 case tok::kw__Decimal64:
1612 case tok::kw__Decimal128:
1613
Chris Lattner99dc9142008-04-13 18:59:07 +00001614 // struct-or-union-specifier (C99) or class-specifier (C++)
1615 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 case tok::kw_struct:
1617 case tok::kw_union:
1618 // enum-specifier
1619 case tok::kw_enum:
1620
1621 // type-qualifier
1622 case tok::kw_const:
1623 case tok::kw_volatile:
1624 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001625
1626 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001627 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001629
1630 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1631 case tok::less:
1632 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001633
1634 case tok::kw___cdecl:
1635 case tok::kw___stdcall:
1636 case tok::kw___fastcall:
1637 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 }
1639}
1640
1641/// isDeclarationSpecifier() - Return true if the current token is part of a
1642/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001643bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 switch (Tok.getKind()) {
1645 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001646
1647 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001648 // Unfortunate hack to support "Class.factoryMethod" notation.
1649 if (getLang().ObjC1 && NextToken().is(tok::period))
1650 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001651 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001652
Douglas Gregord57959a2009-03-27 23:10:48 +00001653 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001654 // Annotate typenames and C++ scope specifiers. If we get one, just
1655 // recurse to handle whatever we get.
1656 if (TryAnnotateTypeOrScopeToken())
1657 return isDeclarationSpecifier();
1658 // Otherwise, not a declaration specifier.
1659 return false;
1660 case tok::coloncolon: // ::foo::bar
1661 if (NextToken().is(tok::kw_new) || // ::new
1662 NextToken().is(tok::kw_delete)) // ::delete
1663 return false;
1664
1665 // Annotate typenames and C++ scope specifiers. If we get one, just
1666 // recurse to handle whatever we get.
1667 if (TryAnnotateTypeOrScopeToken())
1668 return isDeclarationSpecifier();
1669 // Otherwise, not a declaration specifier.
1670 return false;
1671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 // storage-class-specifier
1673 case tok::kw_typedef:
1674 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001675 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 case tok::kw_static:
1677 case tok::kw_auto:
1678 case tok::kw_register:
1679 case tok::kw___thread:
1680
1681 // type-specifiers
1682 case tok::kw_short:
1683 case tok::kw_long:
1684 case tok::kw_signed:
1685 case tok::kw_unsigned:
1686 case tok::kw__Complex:
1687 case tok::kw__Imaginary:
1688 case tok::kw_void:
1689 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001690 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001691 case tok::kw_int:
1692 case tok::kw_float:
1693 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001694 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 case tok::kw__Bool:
1696 case tok::kw__Decimal32:
1697 case tok::kw__Decimal64:
1698 case tok::kw__Decimal128:
1699
Chris Lattner99dc9142008-04-13 18:59:07 +00001700 // struct-or-union-specifier (C99) or class-specifier (C++)
1701 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 case tok::kw_struct:
1703 case tok::kw_union:
1704 // enum-specifier
1705 case tok::kw_enum:
1706
1707 // type-qualifier
1708 case tok::kw_const:
1709 case tok::kw_volatile:
1710 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001711
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 // function-specifier
1713 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001714 case tok::kw_virtual:
1715 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001716
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001717 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001718 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001719
Chris Lattner1ef08762007-08-09 17:01:07 +00001720 // GNU typeof support.
1721 case tok::kw_typeof:
1722
1723 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001724 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001726
1727 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1728 case tok::less:
1729 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001730
Steve Naroff47f52092009-01-06 19:34:12 +00001731 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001732 case tok::kw___cdecl:
1733 case tok::kw___stdcall:
1734 case tok::kw___fastcall:
1735 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 }
1737}
1738
1739
1740/// ParseTypeQualifierListOpt
1741/// type-qualifier-list: [C99 6.7.5]
1742/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001743/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001744/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001745/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001746///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001747void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 while (1) {
1749 int isInvalid = false;
1750 const char *PrevSpec = 0;
1751 SourceLocation Loc = Tok.getLocation();
1752
1753 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 case tok::kw_const:
1755 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1756 getLang())*2;
1757 break;
1758 case tok::kw_volatile:
1759 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1760 getLang())*2;
1761 break;
1762 case tok::kw_restrict:
1763 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1764 getLang())*2;
1765 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001766 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001767 case tok::kw___cdecl:
1768 case tok::kw___stdcall:
1769 case tok::kw___fastcall:
1770 if (!PP.getLangOptions().Microsoft)
1771 goto DoneWithTypeQuals;
1772 // Just ignore it.
1773 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001775 if (AttributesAllowed) {
1776 DS.AddAttributes(ParseAttributes());
1777 continue; // do *not* consume the next token!
1778 }
1779 // otherwise, FALL THROUGH!
1780 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001781 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001782 // If this is not a type-qualifier token, we're done reading type
1783 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001784 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001785 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001787
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 // If the specifier combination wasn't legal, issue a diagnostic.
1789 if (isInvalid) {
1790 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001791 // Pick between error or extwarn.
1792 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1793 : diag::ext_duplicate_declspec;
1794 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 }
1796 ConsumeToken();
1797 }
1798}
1799
1800
1801/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1802///
1803void Parser::ParseDeclarator(Declarator &D) {
1804 /// This implements the 'declarator' production in the C grammar, then checks
1805 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001806 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001807}
1808
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001809/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1810/// is parsed by the function passed to it. Pass null, and the direct-declarator
1811/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001812/// ptr-operator production.
1813///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001814/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1815/// [C] pointer[opt] direct-declarator
1816/// [C++] direct-declarator
1817/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001818///
1819/// pointer: [C99 6.7.5]
1820/// '*' type-qualifier-list[opt]
1821/// '*' type-qualifier-list[opt] pointer
1822///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001823/// ptr-operator:
1824/// '*' cv-qualifier-seq[opt]
1825/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001826/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001827/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001828/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001829/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001830void Parser::ParseDeclaratorInternal(Declarator &D,
1831 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001832
Sebastian Redlf30208a2009-01-24 21:16:55 +00001833 // C++ member pointers start with a '::' or a nested-name.
1834 // Member pointers get special handling, since there's no place for the
1835 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001836 if (getLang().CPlusPlus &&
1837 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1838 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001839 CXXScopeSpec SS;
1840 if (ParseOptionalCXXScopeSpecifier(SS)) {
1841 if(Tok.isNot(tok::star)) {
1842 // The scope spec really belongs to the direct-declarator.
1843 D.getCXXScopeSpec() = SS;
1844 if (DirectDeclParser)
1845 (this->*DirectDeclParser)(D);
1846 return;
1847 }
1848
1849 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001850 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001851 DeclSpec DS;
1852 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001853 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001854
1855 // Recurse to parse whatever is left.
1856 ParseDeclaratorInternal(D, DirectDeclParser);
1857
1858 // Sema will have to catch (syntactically invalid) pointers into global
1859 // scope. It has to catch pointers into namespace scope anyway.
1860 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001861 Loc, DS.TakeAttributes()),
1862 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001863 return;
1864 }
1865 }
1866
1867 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001868 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001869 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001870 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001871 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001872 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001873 if (DirectDeclParser)
1874 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001875 return;
1876 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001877
Sebastian Redl05532f22009-03-15 22:02:01 +00001878 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1879 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001880 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001881 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001882
Chris Lattner9af55002009-03-27 04:18:06 +00001883 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00001884 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001886
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001888 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001889
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001891 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001892 if (Kind == tok::star)
1893 // Remember that we parsed a pointer type, and remember the type-quals.
1894 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001895 DS.TakeAttributes()),
1896 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001897 else
1898 // Remember that we parsed a Block type, and remember the type-quals.
1899 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00001900 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001901 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 } else {
1903 // Is a reference
1904 DeclSpec DS;
1905
Sebastian Redl743de1f2009-03-23 00:00:23 +00001906 // Complain about rvalue references in C++03, but then go on and build
1907 // the declarator.
1908 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1909 Diag(Loc, diag::err_rvalue_reference);
1910
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1912 // cv-qualifiers are introduced through the use of a typedef or of a
1913 // template type argument, in which case the cv-qualifiers are ignored.
1914 //
1915 // [GNU] Retricted references are allowed.
1916 // [GNU] Attributes on references are allowed.
1917 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001918 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001919
1920 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1921 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1922 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001923 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1925 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001926 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 }
1928
1929 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001930 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001931
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001932 if (D.getNumTypeObjects() > 0) {
1933 // C++ [dcl.ref]p4: There shall be no references to references.
1934 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1935 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001936 if (const IdentifierInfo *II = D.getIdentifier())
1937 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1938 << II;
1939 else
1940 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1941 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001942
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001943 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001944 // can go ahead and build the (technically ill-formed)
1945 // declarator: reference collapsing will take care of it.
1946 }
1947 }
1948
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001950 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001951 DS.TakeAttributes(),
1952 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001953 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 }
1955}
1956
1957/// ParseDirectDeclarator
1958/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001959/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001960/// '(' declarator ')'
1961/// [GNU] '(' attributes declarator ')'
1962/// [C90] direct-declarator '[' constant-expression[opt] ']'
1963/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1964/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1965/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1966/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1967/// direct-declarator '(' parameter-type-list ')'
1968/// direct-declarator '(' identifier-list[opt] ')'
1969/// [GNU] direct-declarator '(' parameter-forward-declarations
1970/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001971/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1972/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001973/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001974///
1975/// declarator-id: [C++ 8]
1976/// id-expression
1977/// '::'[opt] nested-name-specifier[opt] type-name
1978///
1979/// id-expression: [C++ 5.1]
1980/// unqualified-id
1981/// qualified-id [TODO]
1982///
1983/// unqualified-id: [C++ 5.1]
1984/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001985/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001986/// conversion-function-id [TODO]
1987/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001988/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001989///
Reid Spencer5f016e22007-07-11 17:01:13 +00001990void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001991 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001992
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001993 if (getLang().CPlusPlus) {
1994 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001995 // ParseDeclaratorInternal might already have parsed the scope.
1996 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1997 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001998 if (afterCXXScope) {
1999 // Change the declaration context for name lookup, until this function
2000 // is exited (and the declarator has been parsed).
2001 DeclScopeObj.EnterDeclaratorScope();
2002 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002003
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002004 if (Tok.is(tok::identifier)) {
2005 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002006
2007 // If this identifier is the name of the current class, it's a
2008 // constructor name.
2009 if (!D.getDeclSpec().hasTypeSpecifier() &&
2010 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2011 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2012 Tok.getLocation(), CurScope),
2013 Tok.getLocation());
2014 // This is a normal identifier.
2015 } else
2016 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002017 ConsumeToken();
2018 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002019 } else if (Tok.is(tok::annot_template_id)) {
2020 TemplateIdAnnotation *TemplateId
2021 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2022
2023 // FIXME: Could this template-id name a constructor?
2024
2025 // FIXME: This is an egregious hack, where we silently ignore
2026 // the specialization (which should be a function template
2027 // specialization name) and use the name instead. This hack
2028 // will go away when we have support for function
2029 // specializations.
2030 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2031 TemplateId->Destroy();
2032 ConsumeToken();
2033 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002034 } else if (Tok.is(tok::kw_operator)) {
2035 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002036 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002037
Douglas Gregor70316a02008-12-26 15:00:45 +00002038 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002039 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2040 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002041 } else {
2042 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002043 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2044 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2045 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002046 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002047 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002048 }
2049 goto PastIdentifier;
2050 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002051 // This should be a C++ destructor.
2052 SourceLocation TildeLoc = ConsumeToken();
2053 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002054 // FIXME: Inaccurate.
2055 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002056 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00002057 TypeResult Type = ParseClassName(EndLoc);
2058 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002059 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002060 else
2061 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002062 } else {
2063 Diag(Tok, diag::err_expected_class_name);
2064 D.SetIdentifier(0, TildeLoc);
2065 }
2066 goto PastIdentifier;
2067 }
2068
2069 // If we reached this point, token is not identifier and not '~'.
2070
2071 if (afterCXXScope) {
2072 Diag(Tok, diag::err_expected_unqualified_id);
2073 D.SetIdentifier(0, Tok.getLocation());
2074 D.setInvalidType(true);
2075 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002076 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002077 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002078 }
2079
2080 // If we reached this point, we are either in C/ObjC or the token didn't
2081 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002082 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2083 assert(!getLang().CPlusPlus &&
2084 "There's a C++-specific check for tok::identifier above");
2085 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2086 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2087 ConsumeToken();
2088 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 // direct-declarator: '(' declarator ')'
2090 // direct-declarator: '(' attributes declarator ')'
2091 // Example: 'char (*X)' or 'int (*XX)(void)'
2092 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002093 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 // This could be something simple like "int" (in which case the declarator
2095 // portion is empty), if an abstract-declarator is allowed.
2096 D.SetIdentifier(0, Tok.getLocation());
2097 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002098 if (D.getContext() == Declarator::MemberContext)
2099 Diag(Tok, diag::err_expected_member_name_or_semi)
2100 << D.getDeclSpec().getSourceRange();
2101 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102 Diag(Tok, diag::err_expected_unqualified_id);
2103 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002104 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002106 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 }
2108
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002109 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 assert(D.isPastIdentifier() &&
2111 "Haven't past the location of the identifier yet?");
2112
2113 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002114 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002115 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2116 // In such a case, check if we actually have a function declarator; if it
2117 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002118 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2119 // When not in file scope, warn for ambiguous function declarators, just
2120 // in case the author intended it as a variable definition.
2121 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2122 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2123 break;
2124 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002125 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002126 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 ParseBracketDeclarator(D);
2128 } else {
2129 break;
2130 }
2131 }
2132}
2133
Chris Lattneref4715c2008-04-06 05:45:57 +00002134/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2135/// only called before the identifier, so these are most likely just grouping
2136/// parens for precedence. If we find that these are actually function
2137/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2138///
2139/// direct-declarator:
2140/// '(' declarator ')'
2141/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002142/// direct-declarator '(' parameter-type-list ')'
2143/// direct-declarator '(' identifier-list[opt] ')'
2144/// [GNU] direct-declarator '(' parameter-forward-declarations
2145/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002146///
2147void Parser::ParseParenDeclarator(Declarator &D) {
2148 SourceLocation StartLoc = ConsumeParen();
2149 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2150
Chris Lattner7399ee02008-10-20 02:05:46 +00002151 // Eat any attributes before we look at whether this is a grouping or function
2152 // declarator paren. If this is a grouping paren, the attribute applies to
2153 // the type being built up, for example:
2154 // int (__attribute__(()) *x)(long y)
2155 // If this ends up not being a grouping paren, the attribute applies to the
2156 // first argument, for example:
2157 // int (__attribute__(()) int x)
2158 // In either case, we need to eat any attributes to be able to determine what
2159 // sort of paren this is.
2160 //
2161 AttributeList *AttrList = 0;
2162 bool RequiresArg = false;
2163 if (Tok.is(tok::kw___attribute)) {
2164 AttrList = ParseAttributes();
2165
2166 // We require that the argument list (if this is a non-grouping paren) be
2167 // present even if the attribute list was empty.
2168 RequiresArg = true;
2169 }
Steve Naroff239f0732008-12-25 14:16:32 +00002170 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00002171 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2172 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00002173 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00002174
Chris Lattneref4715c2008-04-06 05:45:57 +00002175 // If we haven't past the identifier yet (or where the identifier would be
2176 // stored, if this is an abstract declarator), then this is probably just
2177 // grouping parens. However, if this could be an abstract-declarator, then
2178 // this could also be the start of function arguments (consider 'void()').
2179 bool isGrouping;
2180
2181 if (!D.mayOmitIdentifier()) {
2182 // If this can't be an abstract-declarator, this *must* be a grouping
2183 // paren, because we haven't seen the identifier yet.
2184 isGrouping = true;
2185 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002186 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002187 isDeclarationSpecifier()) { // 'int(int)' is a function.
2188 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2189 // considered to be a type, not a K&R identifier-list.
2190 isGrouping = false;
2191 } else {
2192 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2193 isGrouping = true;
2194 }
2195
2196 // If this is a grouping paren, handle:
2197 // direct-declarator: '(' declarator ')'
2198 // direct-declarator: '(' attributes declarator ')'
2199 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002200 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002201 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002202 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002203 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002204
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002205 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002206 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002207 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002208
2209 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002210 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002211 return;
2212 }
2213
2214 // Okay, if this wasn't a grouping paren, it must be the start of a function
2215 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002216 // identifier (and remember where it would have been), then call into
2217 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002218 D.SetIdentifier(0, Tok.getLocation());
2219
Chris Lattner7399ee02008-10-20 02:05:46 +00002220 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002221}
2222
2223/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2224/// declarator D up to a paren, which indicates that we are parsing function
2225/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002226///
Chris Lattner7399ee02008-10-20 02:05:46 +00002227/// If AttrList is non-null, then the caller parsed those arguments immediately
2228/// after the open paren - they should be considered to be the first argument of
2229/// a parameter. If RequiresArg is true, then the first argument of the
2230/// function is required to be present and required to not be an identifier
2231/// list.
2232///
Reid Spencer5f016e22007-07-11 17:01:13 +00002233/// This method also handles this portion of the grammar:
2234/// parameter-type-list: [C99 6.7.5]
2235/// parameter-list
2236/// parameter-list ',' '...'
2237///
2238/// parameter-list: [C99 6.7.5]
2239/// parameter-declaration
2240/// parameter-list ',' parameter-declaration
2241///
2242/// parameter-declaration: [C99 6.7.5]
2243/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002244/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002245/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002246/// declaration-specifiers abstract-declarator[opt]
2247/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002248/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002249/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2250///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002251/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002252/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002253///
Chris Lattner7399ee02008-10-20 02:05:46 +00002254void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2255 AttributeList *AttrList,
2256 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002257 // lparen is already consumed!
2258 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002259
Chris Lattner7399ee02008-10-20 02:05:46 +00002260 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002261 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002262 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002263 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002264 delete AttrList;
2265 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002266
Sebastian Redlab197ba2009-02-09 18:23:29 +00002267 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002268
2269 // cv-qualifier-seq[opt].
2270 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002271 bool hasExceptionSpec = false;
2272 bool hasAnyExceptionSpec = false;
2273 // FIXME: Does an empty vector ever allocate? Exception specifications are
2274 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2275 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002276 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002277 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002278 if (!DS.getSourceRange().getEnd().isInvalid())
2279 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002280
2281 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002282 if (Tok.is(tok::kw_throw)) {
2283 hasExceptionSpec = true;
2284 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2285 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002286 }
2287
Chris Lattnerf97409f2008-04-06 06:57:35 +00002288 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002289 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002290 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002291 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002292 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002293 /*arglist*/ 0, 0,
2294 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002295 hasExceptionSpec,
2296 hasAnyExceptionSpec,
2297 Exceptions.empty() ? 0 :
2298 &Exceptions[0],
2299 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002300 LParenLoc, D),
2301 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002302 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002303 }
2304
2305 // Alternatively, this parameter list may be an identifier list form for a
2306 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002307 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002308 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002309 // K&R identifier lists can't have typedefs as identifiers, per
2310 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002311 if (RequiresArg) {
2312 Diag(Tok, diag::err_argument_required_after_attribute);
2313 delete AttrList;
2314 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002315 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2316 // normal declarators, not for abstract-declarators.
2317 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002318 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002319 }
2320
2321 // Finally, a normal, non-empty parameter type list.
2322
2323 // Build up an array of information about the parsed arguments.
2324 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002325
2326 // Enter function-declaration scope, limiting any declarators to the
2327 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002328 ParseScope PrototypeScope(this,
2329 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002330
2331 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002332 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002333 while (1) {
2334 if (Tok.is(tok::ellipsis)) {
2335 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002336 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002337 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002338 }
2339
Chris Lattnerf97409f2008-04-06 06:57:35 +00002340 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002341
Chris Lattnerf97409f2008-04-06 06:57:35 +00002342 // Parse the declaration-specifiers.
2343 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002344
2345 // If the caller parsed attributes for the first argument, add them now.
2346 if (AttrList) {
2347 DS.AddAttributes(AttrList);
2348 AttrList = 0; // Only apply the attributes to the first parameter.
2349 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002350 ParseDeclarationSpecifiers(DS);
2351
Chris Lattnerf97409f2008-04-06 06:57:35 +00002352 // Parse the declarator. This is "PrototypeContext", because we must
2353 // accept either 'declarator' or 'abstract-declarator' here.
2354 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2355 ParseDeclarator(ParmDecl);
2356
2357 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002358 if (Tok.is(tok::kw___attribute)) {
2359 SourceLocation Loc;
2360 AttributeList *AttrList = ParseAttributes(&Loc);
2361 ParmDecl.AddAttributes(AttrList, Loc);
2362 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002363
Chris Lattnerf97409f2008-04-06 06:57:35 +00002364 // Remember this parsed parameter in ParamInfo.
2365 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2366
Douglas Gregor72b505b2008-12-16 21:30:33 +00002367 // DefArgToks is used when the parsing of default arguments needs
2368 // to be delayed.
2369 CachedTokens *DefArgToks = 0;
2370
Chris Lattnerf97409f2008-04-06 06:57:35 +00002371 // If no parameter was specified, verify that *something* was specified,
2372 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002373 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2374 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002375 // Completely missing, emit error.
2376 Diag(DSStart, diag::err_missing_param);
2377 } else {
2378 // Otherwise, we have something. Add it and let semantic analysis try
2379 // to grok it and add the result to the ParamInfo we are building.
2380
2381 // Inform the actions module about the parameter declarator, so it gets
2382 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002383 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002384
2385 // Parse the default argument, if any. We parse the default
2386 // arguments in all dialects; the semantic analysis in
2387 // ActOnParamDefaultArgument will reject the default argument in
2388 // C.
2389 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002390 SourceLocation EqualLoc = Tok.getLocation();
2391
Chris Lattner04421082008-04-08 04:40:51 +00002392 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002393 if (D.getContext() == Declarator::MemberContext) {
2394 // If we're inside a class definition, cache the tokens
2395 // corresponding to the default argument. We'll actually parse
2396 // them when we see the end of the class definition.
2397 // FIXME: Templates will require something similar.
2398 // FIXME: Can we use a smart pointer for Toks?
2399 DefArgToks = new CachedTokens;
2400
2401 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2402 tok::semi, false)) {
2403 delete DefArgToks;
2404 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002405 Actions.ActOnParamDefaultArgumentError(Param);
2406 } else
2407 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002408 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002409 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002410 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002411
2412 OwningExprResult DefArgResult(ParseAssignmentExpression());
2413 if (DefArgResult.isInvalid()) {
2414 Actions.ActOnParamDefaultArgumentError(Param);
2415 SkipUntil(tok::comma, tok::r_paren, true, true);
2416 } else {
2417 // Inform the actions module about the default argument
2418 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002419 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002420 }
Chris Lattner04421082008-04-08 04:40:51 +00002421 }
2422 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002423
2424 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002425 ParmDecl.getIdentifierLoc(), Param,
2426 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002427 }
2428
2429 // If the next token is a comma, consume it and keep reading arguments.
2430 if (Tok.isNot(tok::comma)) break;
2431
2432 // Consume the comma.
2433 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 }
2435
Chris Lattnerf97409f2008-04-06 06:57:35 +00002436 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002437 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002438
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002439 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002440 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002441
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002442 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002443 bool hasExceptionSpec = false;
2444 bool hasAnyExceptionSpec = false;
2445 // FIXME: Does an empty vector ever allocate? Exception specifications are
2446 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2447 std::vector<TypeTy*> Exceptions;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002448 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002449 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002450 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002451 if (!DS.getSourceRange().getEnd().isInvalid())
2452 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002453
2454 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002455 if (Tok.is(tok::kw_throw)) {
2456 hasExceptionSpec = true;
2457 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2458 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002459 }
2460
Reid Spencer5f016e22007-07-11 17:01:13 +00002461 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002462 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002463 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002464 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002465 DS.getTypeQualifiers(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002466 hasExceptionSpec,
2467 hasAnyExceptionSpec,
2468 Exceptions.empty() ? 0 :
2469 &Exceptions[0],
2470 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002471 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002472}
2473
Chris Lattner66d28652008-04-06 06:34:08 +00002474/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2475/// we found a K&R-style identifier list instead of a type argument list. The
2476/// current token is known to be the first identifier in the list.
2477///
2478/// identifier-list: [C99 6.7.5]
2479/// identifier
2480/// identifier-list ',' identifier
2481///
2482void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2483 Declarator &D) {
2484 // Build up an array of information about the parsed arguments.
2485 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2486 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2487
2488 // If there was no identifier specified for the declarator, either we are in
2489 // an abstract-declarator, or we are in a parameter declarator which was found
2490 // to be abstract. In abstract-declarators, identifier lists are not valid:
2491 // diagnose this.
2492 if (!D.getIdentifier())
2493 Diag(Tok, diag::ext_ident_list_in_param);
2494
2495 // Tok is known to be the first identifier in the list. Remember this
2496 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002497 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002498 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002499 Tok.getLocation(),
2500 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002501
Chris Lattner50c64772008-04-06 06:39:19 +00002502 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002503
2504 while (Tok.is(tok::comma)) {
2505 // Eat the comma.
2506 ConsumeToken();
2507
Chris Lattner50c64772008-04-06 06:39:19 +00002508 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002509 if (Tok.isNot(tok::identifier)) {
2510 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002511 SkipUntil(tok::r_paren);
2512 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002513 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002514
Chris Lattner66d28652008-04-06 06:34:08 +00002515 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002516
2517 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002518 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002519 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002520
2521 // Verify that the argument identifier has not already been mentioned.
2522 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002523 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002524 } else {
2525 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002526 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002527 Tok.getLocation(),
2528 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002529 }
Chris Lattner66d28652008-04-06 06:34:08 +00002530
2531 // Eat the identifier.
2532 ConsumeToken();
2533 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002534
2535 // If we have the closing ')', eat it and we're done.
2536 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2537
Chris Lattner50c64772008-04-06 06:39:19 +00002538 // Remember that we parsed a function type, and remember the attributes. This
2539 // function type is always a K&R style function type, which is not varargs and
2540 // has no prototype.
2541 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002542 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002543 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002544 /*TypeQuals*/0,
2545 /*exception*/false, false, 0, 0,
2546 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002547 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002548}
Chris Lattneref4715c2008-04-06 05:45:57 +00002549
Reid Spencer5f016e22007-07-11 17:01:13 +00002550/// [C90] direct-declarator '[' constant-expression[opt] ']'
2551/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2552/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2553/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2554/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2555void Parser::ParseBracketDeclarator(Declarator &D) {
2556 SourceLocation StartLoc = ConsumeBracket();
2557
Chris Lattner378c7e42008-12-18 07:27:21 +00002558 // C array syntax has many features, but by-far the most common is [] and [4].
2559 // This code does a fast path to handle some of the most obvious cases.
2560 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002561 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002562 // Remember that we parsed the empty array type.
2563 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002564 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2565 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002566 return;
2567 } else if (Tok.getKind() == tok::numeric_constant &&
2568 GetLookAheadToken(1).is(tok::r_square)) {
2569 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002570 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002571 ConsumeToken();
2572
Sebastian Redlab197ba2009-02-09 18:23:29 +00002573 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002574
2575 // If there was an error parsing the assignment-expression, recover.
2576 if (ExprRes.isInvalid())
2577 ExprRes.release(); // Deallocate expr, just use [].
2578
2579 // Remember that we parsed a array type, and remember its features.
2580 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002581 ExprRes.release(), StartLoc),
2582 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002583 return;
2584 }
2585
Reid Spencer5f016e22007-07-11 17:01:13 +00002586 // If valid, this location is the position where we read the 'static' keyword.
2587 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002588 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 StaticLoc = ConsumeToken();
2590
2591 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002592 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002594 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002595
2596 // If we haven't already read 'static', check to see if there is one after the
2597 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002598 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 StaticLoc = ConsumeToken();
2600
2601 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2602 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002603 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002604
2605 // Handle the case where we have '[*]' as the array size. However, a leading
2606 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2607 // the the token after the star is a ']'. Since stars in arrays are
2608 // infrequent, use of lookahead is not costly here.
2609 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002610 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002611
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002612 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002613 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002614 StaticLoc = SourceLocation(); // Drop the static.
2615 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002616 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002617 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002618 // Note, in C89, this production uses the constant-expr production instead
2619 // of assignment-expr. The only difference is that assignment-expr allows
2620 // things like '=' and '*='. Sema rejects these in C89 mode because they
2621 // are not i-c-e's, so we don't need to distinguish between the two here.
2622
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 // Parse the assignment-expression now.
2624 NumElements = ParseAssignmentExpression();
2625 }
2626
2627 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002628 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002629 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 // If the expression was invalid, skip it.
2631 SkipUntil(tok::r_square);
2632 return;
2633 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002634
2635 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2636
Chris Lattner378c7e42008-12-18 07:27:21 +00002637 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002638 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2639 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002640 NumElements.release(), StartLoc),
2641 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002642}
2643
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002644/// [GNU] typeof-specifier:
2645/// typeof ( expressions )
2646/// typeof ( type-name )
2647/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002648///
2649void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002650 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002651 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002652 SourceLocation StartLoc = ConsumeToken();
2653
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002654 bool isCastExpr;
2655 TypeTy *CastTy;
2656 SourceRange CastRange;
2657 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2658 isCastExpr,
2659 CastTy,
2660 CastRange);
2661
2662 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002663 // FIXME: Not accurate, the range gets one token more than it should.
2664 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002665 else
2666 DS.SetRangeEnd(CastRange.getEnd());
2667
2668 if (isCastExpr) {
2669 if (!CastTy) {
2670 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002671 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002672 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002673
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002674 const char *PrevSpec = 0;
2675 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2676 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2677 CastTy))
2678 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2679 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002680 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002681
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002682 // If we get here, the operand to the typeof was an expresion.
2683 if (Operand.isInvalid()) {
2684 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002685 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002686 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002687
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002688 const char *PrevSpec = 0;
2689 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2690 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2691 Operand.release()))
2692 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002693}